func (ps *ParticleSystem) DoStep(gs *gamestate.GameState) {
	ps.TransformProg.Use()
	ps.VaoTff1.Bind()

	gl.Enable(gl.RASTERIZER_DISCARD)
	defer gl.Disable(gl.RASTERIZER_DISCARD)

	// ps.Data1.Bind(gl.ARRAY_BUFFER)
	// SetAttribPointers(&ps.TransformLoc, &ParticleVertex{}, false)

	//var orientation mgl.Quat = gs.Player.Camera.Orientation
	var model mgl.Mat4 = gs.Camera.Model()
	pPos := model.Mul4x1(mgl.Vec4{1, -1, 0, 1})
	ps.TransformLoc.Origin.Uniform3f(pPos[0], pPos[1], pPos[2])
	dir := model.Mul4x1(mgl.Vec4{0, 0, 100, 0})
	ps.TransformLoc.Dir.Uniform3f(dir[0], dir[1], dir[2])

	ps.Data2.BindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 0)

	gl.BeginTransformFeedback(gl.POINTS)
	gl.DrawArrays(gl.POINTS, 0, ps.NumParticles)
	gl.EndTransformFeedback()

	ps.Data1, ps.Data2 = ps.Data2, ps.Data1
	ps.VaoRender1, ps.VaoRender2 = ps.VaoRender2, ps.VaoRender1
	ps.VaoTff1, ps.VaoTff2 = ps.VaoTff2, ps.VaoTff1
}
Esempio n. 2
0
func (ui *UIText) Draw() {
	if ui.text == "" {
		return
	}

	v := ui.align.Vector()
	v.X = (v.X * ui.width)
	v.Y = (v.Y * ui.height)

	mat := engine.TextureMaterial
	if ui.Font.IsSDF() {
		mat = engine.SDFMaterial
	}
	mat.Begin(ui.GameObject())

	mp := mat.ProjMatrix
	mv := mat.ViewMatrix
	mm := mat.ModelMatrix
	tx := mat.Texture
	ti := mat.Tiling
	of := mat.Offset
	color := mat.AddColor
	_ = color

	ui.buffer.Bind(gl.ARRAY_BUFFER)
	mat.Verts.EnableArray()
	mat.Verts.AttribPointer(3, gl.FLOAT, false, 0, uintptr(0))
	mat.UV.EnableArray()
	mat.UV.AttribPointer(2, gl.FLOAT, false, 0, uintptr(ui.texcoordsIndex))

	camera := engine.GetScene().SceneBase().Camera

	view := camera.InvertedMatrix()
	model := engine.Identity()
	model.Translate(v.X, v.Y, 0)
	model.Mul(ui.GameObject().Transform().Matrix())
	model.Translate(0.75, 0.75, 0)
	/*
		view := camera.Transform().Matrix()
		view = view.Invert()
		model := ui.GameObject().Transform().Matrix()
	*/

	mv.UniformMatrix4fv(false, view)
	mp.UniformMatrix4f(false, (*[16]float32)(camera.Projection))
	mm.UniformMatrix4fv(false, model)
	ti.Uniform2f(1, 1)
	of.Uniform2f(0, 0)

	ui.Font.Bind()
	tx.Uniform1i(0)

	color.Uniform4f(ui.Color.R, ui.Color.G, ui.Color.B, ui.Color.A)

	gl.DrawArrays(gl.QUADS, 0, ui.vertexCount)

}
Esempio n. 3
0
func DrawSprites(tex *Texture, uvs []UV, positions []Vector, scales []Vector, rotations []float32, alings []Align, colors []Color) {

	internalMaterial.Begin(nil)

	mp := internalMaterial.ProjMatrix
	mv := internalMaterial.ViewMatrix
	mm := internalMaterial.ModelMatrix
	tx := internalMaterial.Texture
	ac := internalMaterial.AddColor
	ti := internalMaterial.Tiling
	of := internalMaterial.Offset

	defaultBuffer.Bind(gl.ARRAY_BUFFER)
	internalMaterial.Verts.EnableArray()
	internalMaterial.Verts.AttribPointer(3, gl.FLOAT, false, 0, uintptr(0))
	internalMaterial.UV.EnableArray()
	internalMaterial.UV.AttribPointer(2, gl.FLOAT, false, 0, uintptr(12*4))

	camera := GetScene().SceneBase().Camera
	view := camera.InvertedMatrix()
	mv.UniformMatrix4fv(false, view)
	mp.UniformMatrix4f(false, (*[16]float32)(camera.Projection))

	tex.Bind()
	tx.Uniform1i(0)

	for i := 0; i < len(uvs); i++ {

		uv, position, scale := uvs[i], positions[i], scales[i]

		if !InsideScreen(uv.Ratio, position, scale) {
			continue
		}

		rotation, aling, color := rotations[i], alings[i], colors[i]

		v := aling.Vector()
		v.X *= uv.Ratio

		model := Identity()
		model.Translate(v.X, v.Y, 0)

		model.Scale((scale.X * uv.Ratio), scale.Y, scale.Z)
		model.RotateZ(rotation, -1)
		model.Translate(position.X+0.75, position.Y+0.75, position.Z)

		mm.UniformMatrix4fv(false, model)
		ac.Uniform4f(color.R, color.G, color.B, color.A)
		ti.Uniform2f(uv.U2-uv.U1, uv.V2-uv.V1)
		of.Uniform2f(uv.U1, uv.V1)

		gl.DrawArrays(gl.QUADS, 0, 4)
	}

	internalMaterial.End(nil)
}
Esempio n. 4
0
func (sp *Sprite) Draw() {
	if sp.Texture != nil && sp.Render {
		/*
			Temporal camera distance check
		*/
		currentUV := sp.UVs[int(sp.animation)]
		if !InsideScreen(currentUV.Ratio, sp.Transform().WorldPosition(), sp.Transform().WorldScale()) {
			return
		}

		renders++

		TextureMaterial.Begin(sp.GameObject())

		mp := TextureMaterial.ProjMatrix
		mv := TextureMaterial.ViewMatrix
		mm := TextureMaterial.ModelMatrix
		tx := TextureMaterial.Texture
		ac := TextureMaterial.AddColor
		ti := TextureMaterial.Tiling
		of := TextureMaterial.Offset

		defaultBuffer.Bind(gl.ARRAY_BUFFER)
		TextureMaterial.Verts.EnableArray()
		TextureMaterial.Verts.AttribPointer(3, gl.FLOAT, false, 0, uintptr(0))
		TextureMaterial.UV.EnableArray()
		TextureMaterial.UV.AttribPointer(2, gl.FLOAT, false, 0, uintptr(12*4))

		v := sp.align.Vector()
		v.X *= currentUV.Ratio

		camera := GetScene().SceneBase().Camera
		view := camera.InvertedMatrix()
		model := Identity()
		model.Scale(currentUV.Ratio, 1, 1)
		model.Translate(v.X, v.Y, 0)
		model.Mul(sp.GameObject().Transform().Matrix())

		mv.UniformMatrix4f(false, (*[16]float32)(&view))
		mp.UniformMatrix4f(false, (*[16]float32)(camera.Projection))
		mm.UniformMatrix4f(false, (*[16]float32)(&model))

		sp.Bind()
		tx.Uniform1i(0)

		ac.Uniform4f(sp.Color.R, sp.Color.G, sp.Color.B, sp.Color.A)
		ti.Uniform2f((currentUV.U2-currentUV.U1)*sp.Tiling.X, (currentUV.V2-currentUV.V1)*sp.Tiling.Y)
		of.Uniform2f(currentUV.U1, currentUV.V1)

		gl.DrawArrays(gl.QUADS, 0, 4)

		TextureMaterial.End(sp.GameObject())
	}
}
func (this *LineRenderer) Render(Proj, View mgl.Mat4) {

	data := debug.Read()
	if len(data) > 0 {
		this.Prog.Use()
		this.vao.Bind()
		this.buffer.Bind(gl.ARRAY_BUFFER)
		gl.BufferData(gl.ARRAY_BUFFER, helpers.ByteSizeOfSlice(data), data, gl.STREAM_DRAW)
		this.RenLoc.Proj.UniformMatrix4f(false, renderstuff.GlMat4(&Proj))
		this.RenLoc.View.UniformMatrix4f(false, renderstuff.GlMat4(&View))
		gl.DrawArrays(gl.LINES, 0, len(data))
	}
}
Esempio n. 6
0
func (sp *Sprite) DrawScreen() {
	if sp.Texture != nil && sp.Render {

		camera := GetScene().SceneBase().Camera
		pos := sp.Transform().WorldPosition()
		scale := sp.Transform().WorldScale()

		TextureMaterial.Begin(sp.GameObject())

		mp := TextureMaterial.ProjMatrix
		mv := TextureMaterial.ViewMatrix
		mm := TextureMaterial.ModelMatrix
		tx := TextureMaterial.Texture
		ac := TextureMaterial.AddColor
		ti := TextureMaterial.Tiling
		of := TextureMaterial.Offset

		defaultBuffer.Bind(gl.ARRAY_BUFFER)
		TextureMaterial.Verts.EnableArray()
		TextureMaterial.Verts.AttribPointer(3, gl.FLOAT, false, 0, uintptr(0))
		TextureMaterial.UV.EnableArray()
		TextureMaterial.UV.AttribPointer(2, gl.FLOAT, false, 0, uintptr(12*4))

		currentUV := sp.UVs[int(sp.animation)]

		view := Identity()
		model := Identity()
		model.Scale(scale.X*currentUV.Ratio, scale.Y, 1)
		model.Translate((float32(Width)/2)+pos.X+0.75, (float32(Height)/2)+pos.Y+0.75, 1)

		mv.UniformMatrix4fv(false, view)
		mp.UniformMatrix4f(false, (*[16]float32)(camera.Projection))
		mm.UniformMatrix4fv(false, model)

		ti.Uniform2f((currentUV.U2-currentUV.U1)*sp.Tiling.X, (currentUV.V2-currentUV.V1)*sp.Tiling.Y)
		of.Uniform2f(currentUV.U1, currentUV.V1)

		sp.Bind()
		gl.ActiveTexture(gl.TEXTURE0)
		tx.Uniform1i(0)

		//ac.Uniform4f(1, 1, 1, 0)
		ac.Uniform4f(sp.Color.R, sp.Color.G, sp.Color.B, sp.Color.A)

		gl.DrawArrays(gl.QUADS, 0, 4)

		TextureMaterial.End(sp.GameObject())
	}
}
Esempio n. 7
0
func DrawSprite(tex *Texture, uv UV, position Vector, scale Vector, rotation float32, aling Align, color Color) {
	if !InsideScreen(uv.Ratio, position, scale) {
		return
	}

	internalMaterial.Begin(nil)

	mp := internalMaterial.ProjMatrix
	mv := internalMaterial.ViewMatrix
	mm := internalMaterial.ModelMatrix
	tx := internalMaterial.Texture
	ac := internalMaterial.AddColor
	ti := internalMaterial.Tiling
	of := internalMaterial.Offset

	defaultBuffer.Bind(gl.ARRAY_BUFFER)
	internalMaterial.Verts.EnableArray()
	internalMaterial.Verts.AttribPointer(3, gl.FLOAT, false, 0, uintptr(0))
	internalMaterial.UV.EnableArray()
	internalMaterial.UV.AttribPointer(2, gl.FLOAT, false, 0, uintptr(12*4))

	v := aling.Vector()
	v.X *= uv.Ratio

	camera := GetScene().SceneBase().Camera
	view := camera.InvertedMatrix()
	model := Identity()
	model.Translate(v.X, v.Y, 0)

	model.Scale(scale.X*uv.Ratio, scale.Y, scale.Z)
	model.RotateZ(rotation, -1)
	model.Translate(position.X+0.75, position.Y+0.75, position.Z)

	mv.UniformMatrix4fv(false, view)
	mp.UniformMatrix4f(false, (*[16]float32)(camera.Projection))
	mm.UniformMatrix4fv(false, model)
	ac.Uniform4i(int(color.R), int(color.G), int(color.B), int(color.A))
	ti.Uniform2f(uv.U2-uv.U1, uv.V2-uv.V1)
	of.Uniform2f(uv.U1, uv.V1)

	tx.Uniform1i(0)

	tex.Bind()

	gl.DrawArrays(gl.QUADS, 0, 4)

	internalMaterial.End(nil)
}
Esempio n. 8
0
func Draw(renData *RenderData) {
	numverts := renData.Numverts
	if renData.InstanceData == 0 {
		if renData.Indices == 0 {
			gl.DrawArrays(renData.Mode, 0, renData.Numverts)
		} else {
			gl.DrawElements(renData.Mode, numverts, renData.IndexType, uintptr(0))
		}
	} else {
		if renData.Indices == 0 {
			gl.DrawArraysInstanced(renData.Mode, 0, renData.Numverts, renData.NumInstances)
		} else {
			gl.DrawElementsInstanced(renData.Mode, renData.Numverts, renData.IndexType, 0, renData.NumInstances)
		}
	}
}
Esempio n. 9
0
func (drawer *SpriteDrawer) Draw(sprites []Sprite) {

	gl.Viewport(0, 0, drawer.width, drawer.height)

	if len(sprites) == 0 {
		return
	}

	drawer.Use()
	drawer.Texture.Bind(gl.TEXTURE_2D_ARRAY)

	tmp := drawer.GetTransform().To32()
	drawer.camera_uniform.UniformMatrix2x3f(false, &tmp)

	vertexbuffer := gl.GenBuffer()
	defer vertexbuffer.Delete()
	vertexbuffer.Bind(gl.ARRAY_BUFFER)

	stride := int(unsafe.Sizeof(sprites[0]))

	gl.BufferData(gl.ARRAY_BUFFER, stride*len(sprites), sprites, gl.STREAM_DRAW)

	var transform1, transform2, texcoords, texlevel gl.AttribLocation
	transform1 = 0
	transform2 = 1
	texcoords = 2
	texlevel = 3

	transform1.AttribPointer(3, gl.FLOAT, false, stride, unsafe.Offsetof(sprites[0].Transform))
	transform2.AttribPointer(3, gl.FLOAT, false, stride, unsafe.Offsetof(sprites[0].Transform)+unsafe.Sizeof(sprites[0].Transform[0])*3)
	texcoords.AttribPointer(4, gl.FLOAT, false, stride, unsafe.Offsetof(sprites[0].TextureLeft))
	texlevel.AttribPointer(1, gl.FLOAT, false, stride, unsafe.Offsetof(sprites[0].Layer))

	transform1.EnableArray()
	transform2.EnableArray()
	texcoords.EnableArray()
	texlevel.EnableArray()

	gl.DrawArrays(gl.POINTS, 0, len(sprites))

	transform1.DisableArray()
	transform2.DisableArray()
	texcoords.DisableArray()
	texlevel.DisableArray()
}
Esempio n. 10
0
func main() {
	glfw.SetErrorCallback(errorCallback)
	// lock glfw/gl calls to a single thread
	runtime.LockOSThread()
	runtime.GOMAXPROCS(8) // Render, read commands, send input, extra for file loading, etc

	if !glfw.Init() {
		panic("Could not init glfw!")
	}

	defer glfw.Terminate()

	glfw.WindowHint(glfw.ContextVersionMajor, 3)
	glfw.WindowHint(glfw.ContextVersionMinor, 3)
	glfw.WindowHint(glfw.OpenglForwardCompatible, glfw.True)
	glfw.WindowHint(glfw.OpenglProfile, glfw.OpenglCoreProfile)

	window, err := glfw.CreateWindow(800, 600, "Example", nil, nil)
	if err != nil {
		panic(err)
	}

	window.SetFramebufferSizeCallback(func(w *glfw.Window, width, height int) {
		fmt.Printf("Framebuffer size is now %vx%v\n", width, height)
		// Keep aspect ratio from camwidth/camheight
		camRatio := camWidth / camHeight
		bufRatio := float32(width) / float32(height)
		var newWidth, newHeight float32

		switch {
		case camRatio > bufRatio:
			newHeight = float32(width) / camRatio
			newWidth = float32(width)
		case bufRatio > camRatio:
			newWidth = float32(height) * camRatio
			newHeight = float32(height)
		}

		fmt.Printf("Viewport size is now %vx%v; cam ratio is %v; viewport ratio is %v;\n", newWidth, newHeight, camRatio, newWidth/newHeight)

		gl.Viewport((width-int(newWidth))/2, (height-int(newHeight))/2, int(newWidth), int(newHeight))
	})

	defer window.Destroy()

	window.MakeContextCurrent()
	glfw.SwapInterval(1)

	gl.Init()

	// Enable blending
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)

	vao := gl.GenVertexArray()
	vao.Bind()

	vbo := gl.GenBuffer()
	vbo.Bind(gl.ARRAY_BUFFER)

	verticies := []float32{-0.5, 0.5,
		0.5, 0.5,
		0.5, -0.5,
		-0.5, -0.5}

	gl.BufferData(gl.ARRAY_BUFFER, len(verticies)*4, verticies, gl.STATIC_DRAW)

	vertex_shader := gl.CreateShader(gl.VERTEX_SHADER)
	vertex_shader.Source(vertex)
	vertex_shader.Compile()
	fmt.Println(vertex_shader.GetInfoLog())
	defer vertex_shader.Delete()

	fragment_shader := gl.CreateShader(gl.FRAGMENT_SHADER)
	fragment_shader.Source(fragment)
	fragment_shader.Compile()
	fmt.Println(fragment_shader.GetInfoLog())
	defer fragment_shader.Delete()

	program := gl.CreateProgram()
	program.AttachShader(vertex_shader)
	program.AttachShader(fragment_shader)

	program.BindFragDataLocation(0, "outColor")
	program.Link()
	program.Use()
	defer program.Delete()

	positionAttrib := program.GetAttribLocation("position")
	positionAttrib.AttribPointer(2, gl.FLOAT, false, 0, nil)
	positionAttrib.EnableArray()
	defer positionAttrib.DisableArray()

	modelMat := program.GetUniformLocation("modelView")
	projMat := program.GetUniformLocation("projection")
	spriteSize := program.GetUniformLocation("size")

	cmd := exec.Command(os.Args[1], os.Args[2:]...)
	stdoutReader, stdoutWriter := io.Pipe()
	cmd.Stdout = stdoutWriter
	input := bufio.NewReader(stdoutReader)

	stdinReader, stdinWriter := io.Pipe()
	cmd.Stdin = stdinReader

	stderr, err := cmd.StderrPipe()
	chkErr(err)

	go io.Copy(os.Stderr, stderr)

	err = cmd.Start()
	chkErr(err)

	window.SetKeyCallback(handleKey)

	go func() {
		runtime.LockOSThread()
		for !window.ShouldClose() {
			sendInput(stdinWriter)
			//fmt.Fprintf(stdinWriter, "T %v\n", time.Now())
			<-ticks
			readCommands(input)
		}
	}()

	frameCnt := 0
	queueDepth := 0
	then := time.Now()

	for !window.ShouldClose() {
		frameCnt++
		queueDepth += len(commandBus)
		if frameCnt%120 == 0 {
			fmt.Printf("Queue depth: %v. Render time: %v. Decode time: %v.\n", queueDepth/120, time.Since(then)/120, decodeTime/time.Duration(decodes))
			queueDepth = 0
			then = time.Now()
			decodeTime = time.Duration(0)
			decodes = 0
		}

		for len(commandBus) > 0 {
			(<-commandBus)()
		}

		halfwidth := camWidth / 2.0
		halfheight := camHeight / 2.0
		projection := mathgl.Ortho2D(camera.x-halfwidth, camera.x+halfwidth, camera.y+halfheight, camera.y-halfheight)
		projection = mathgl.Scale3D(camera.sx, camera.sy, 1).Mul4(projection)
		projection = mathgl.HomogRotate3DZ(camera.rot).Mul4(projection)

		projMat.UniformMatrix4f(false, (*[16]float32)(&projection))

		gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

		for _, sprite := range sprites {
			sMat := mathgl.Scale3D(sprite.sx, sprite.sy, 1)
			sMat = mathgl.HomogRotate3DZ(sprite.rot).Mul4(sMat)
			sMat = mathgl.Translate3D(sprite.x, sprite.y, sprite.z).Mul4(sMat)

			// temp hack bank in
			sprite.bank = 1

			sMap := findSMap(sprite.smap)

			spriteSize.Uniform2f(float32(sMap.getWidth()*sprite.cellwidth), float32(sMap.getHeight()*sprite.cellheight))

			modelMat.UniformMatrix4f(false, (*[16]float32)(&sMat))
			gl.DrawArrays(gl.TRIANGLE_FAN, 0, 4)
		}

		window.SwapBuffers()
		glfw.PollEvents()

		if window.GetKey(glfw.KeyEscape) == glfw.Press {
			window.SetShouldClose(true)
		}
	}
}
Esempio n. 11
0
func (v *Video) Render() {
	for running {
		select {
		case buf := <-v.videoTick:
			gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

			v.prog.Use()

			gl.ActiveTexture(gl.TEXTURE0)
			v.texture.Bind(gl.TEXTURE_2D)

			gl.TexImage2D(gl.TEXTURE_2D, 0, 3, 240, 224, 0, gl.RGBA,
				gl.UNSIGNED_INT_8_8_8_8, buf)

			gl.DrawArrays(gl.TRIANGLES, 0, 6)

			if v.screen != nil {
				sdl.GL_SwapBuffers()
				v.fpsmanager.FramerateDelay()
			}
		case ev := <-sdl.Events:
			switch e := ev.(type) {
			case sdl.ResizeEvent:
				v.ResizeEvent(int(e.W), int(e.H))
			case sdl.QuitEvent:
				os.Exit(0)
			case sdl.KeyboardEvent:
				switch e.Keysym.Sym {
				case sdl.K_ESCAPE:
					running = false
				case sdl.K_r:
					// Trigger reset interrupt
					if e.Type == sdl.KEYDOWN {
						// cpu.RequestInterrupt(InterruptReset)
					}
				case sdl.K_l:
					if e.Type == sdl.KEYDOWN {
						nes.LoadGameState()
					}
				case sdl.K_s:
					if e.Type == sdl.KEYDOWN {
						nes.SaveGameState()
					}
				case sdl.K_i:
					if e.Type == sdl.KEYDOWN {
						nes.AudioEnabled = !nes.AudioEnabled
					}
				case sdl.K_p:
					if e.Type == sdl.KEYDOWN {
						nes.TogglePause()
					}
				case sdl.K_d:
					if e.Type == sdl.KEYDOWN {
						jsHandler.ReloadFile(debugfile)
					}
				case sdl.K_m:
					if e.Type == sdl.KEYDOWN {
						nes.Handler.Handle("debug-mode")
					}
				case sdl.K_BACKSLASH:
					if e.Type == sdl.KEYDOWN {
						nes.Pause()
						nes.StepFrame()
					}
				case sdl.K_1:
					if e.Type == sdl.KEYDOWN {
						v.ResizeEvent(256, 240)
					}
				case sdl.K_2:
					if e.Type == sdl.KEYDOWN {
						v.ResizeEvent(512, 480)
					}
				case sdl.K_3:
					if e.Type == sdl.KEYDOWN {
						v.ResizeEvent(768, 720)
					}
				case sdl.K_4:
					if e.Type == sdl.KEYDOWN {
						v.ResizeEvent(1024, 960)
					}
				}

				switch e.Type {
				case sdl.KEYDOWN:
					nes.Pads[0].KeyDown(e, 0)
				case sdl.KEYUP:
					nes.Pads[0].KeyUp(e, 0)
				}
			}
		}
	}
}