Ejemplo n.º 1
0
func TestStackMultiPush(t *testing.T) {
	stack := NewTransformStack()

	scale := mgl64.Scale3D(2, 2, 2)
	rot := mgl64.HomogRotate3DY(mgl64.DegToRad(90))
	trans := mgl64.Translate3D(4, 5, 6)

	stack.Push(trans)
	stack.Push(rot)

	if !stack.Peek().ApproxEqualThreshold(trans.Mul4(rot), 1e-4) {
		t.Errorf("Stack does not multiply first two pushes correctly")
	}

	stack.Push(scale)

	if !stack.Peek().ApproxEqualThreshold(trans.Mul4(rot).Mul4(scale), 1e-4) {
		t.Errorf("Stack does not multiple third push correctly")
	}

	stack.Unwind(2)
	stack.Push(scale)

	if !stack.Peek().ApproxEqualThreshold(trans.Mul4(scale), 1e-4) {
		t.Errorf("Unwinding and multiplying does not work correctly")
	}
}
Ejemplo n.º 2
0
func ExampleRebase() {
	parent1 := NewTransformStack()

	scale := mgl64.Scale3D(2, 2, 2)
	rot := mgl64.HomogRotate3DY(mgl64.DegToRad(90))
	trans := mgl64.Translate3D(5, 5, 5)

	parent1.Push(trans)
	parent1.Push(rot)
	parent1.Push(scale)

	parent2 := parent1.Copy()

	trans2 := mgl64.Translate3D(1, 1, 1)
	rot2 := mgl64.HomogRotate3DX(mgl64.DegToRad(45))
	parent1.Push(trans2)
	parent1.Push(rot2)

	// Replay the pushes the changes from parent1 after the copy onto parent2, as if
	// they had been done on parent2 instead
	parent2, err := Rebase(parent1, 4, parent2)

	if err != nil {
		panic(err)
	}

	// Now parent2 and parent 1 should be the same!
	fmt.Println(parent2.Peek().ApproxEqualThreshold(parent1.Peek(), 1e-4))
	// Output: true
}
// Render renders
func (renderable *TileTextureMapRenderable) Render(context *RenderContext) {
	gl := renderable.gl

	renderable.withShader(func() {
		renderable.setMatrix32(renderable.viewMatrixUniform, context.ViewMatrix())
		renderable.setMatrix32(renderable.projectionMatrixUniform, context.ProjectionMatrix())

		gl.BindBuffer(opengl.ARRAY_BUFFER, renderable.vertexPositionBuffer)
		gl.VertexAttribOffset(uint32(renderable.vertexPositionAttrib), 3, opengl.FLOAT, false, 0, 0)

		textureUnit := int32(0)
		gl.ActiveTexture(opengl.TEXTURE0 + uint32(textureUnit))
		gl.BindTexture(opengl.TEXTURE_2D, renderable.paletteTexture.Handle())
		gl.Uniform1i(renderable.paletteUniform, textureUnit)

		textureUnit = 1
		gl.ActiveTexture(opengl.TEXTURE0 + uint32(textureUnit))

		scaling := mgl.Scale3D(32.0, 32.0, 1.0)
		for y, row := range renderable.tiles {
			for x, tile := range row {
				if tile != nil && *tile.Type != model.Solid && tile.RealWorld != nil {
					texture := renderable.textureQuery(*tile.RealWorld.FloorTexture)
					if texture != nil {
						modelMatrix := mgl.Translate3D(float64(x)*32.0, float64(y)*32.0, 0.0).
							Mul4(scaling)

						uvMatrix := uvRotations[*tile.RealWorld.FloorTextureRotations]
						renderable.setMatrix32(renderable.uvMatrixUniform, uvMatrix)
						renderable.setMatrix64(renderable.modelMatrixUniform, &modelMatrix)
						verticeCount := renderable.ensureTileType(*tile.Type)
						gl.BindTexture(opengl.TEXTURE_2D, texture.Handle())
						gl.Uniform1i(renderable.bitmapUniform, textureUnit)

						gl.DrawArrays(opengl.TRIANGLES, 0, int32(verticeCount))
					}
				}
			}
		}

		gl.BindTexture(opengl.TEXTURE_2D, 0)
	})
}
Ejemplo n.º 4
0
func TestReseed(t *testing.T) {
	stack := NewTransformStack()

	scale := mgl64.Scale3D(2, 2, 2)
	rot := mgl64.HomogRotate3DY(mgl64.DegToRad(90))
	trans := mgl64.Translate3D(4, 5, 6)

	stack.Push(trans)
	stack.Push(rot)
	stack.Push(scale)

	trans2 := mgl64.Translate3D(1, 2, 3)
	err := stack.Reseed(1, trans2)

	if err != nil {
		t.Fatalf("Rebase returned error when it should not %v", err)
	}

	if !stack.Peek().ApproxEqualThreshold(trans2.Mul4(rot).Mul4(scale), 1e-4) {
		t.Fatalf("Rebase does not remultiply correctly. Got\n %v expected\n %v. (Previous state:\n %v)", stack.Peek(), trans2.Mul4(rot).Mul4(scale), trans.Mul4(rot).Mul4(scale))
	}
}
Ejemplo n.º 5
0
func TestRebase(t *testing.T) {
	stack := NewTransformStack()
	stack2 := NewTransformStack()

	scale := mgl64.Scale3D(2, 2, 2)
	rot := mgl64.HomogRotate3DY(mgl64.DegToRad(90))
	trans := mgl64.Translate3D(4, 5, 6)
	trans2 := mgl64.Translate3D(1, 2, 3)

	stack.Push(trans)
	stack.Push(rot)

	stack2.Push(trans2)
	stack2.Push(scale)

	out, _ := Rebase(stack2, 1, stack)

	if !out.Peek().ApproxEqualThreshold(trans.Mul4(rot).Mul4(trans2).Mul4(scale), 1e-4) {
		t.Log("\n", out)
		t.Errorf("Rebase unsuccessful. Got\n %v, expected\n %v", out.Peek(), trans.Mul4(rot).Mul4(trans2).Mul4(scale))
	}
}
Ejemplo n.º 6
0
func ExampleReseed() {
	stack := NewTransformStack()

	scale := mgl64.Scale3D(2, 2, 2)
	rot := mgl64.HomogRotate3DY(mgl64.DegToRad(90))
	trans := mgl64.Translate3D(4, 5, 6)

	stack.Push(trans)
	stack.Push(rot)
	stack.Push(scale)

	fmt.Println("Initial state:\n", stack.Peek())

	trans2 := mgl64.Translate3D(1, 2, 3)

	err := stack.Reseed(1, trans2)
	if err == nil {
		panic("Rebase failed")
	}

	fmt.Println("After rebase:\n", stack.Peek())
	fmt.Println("Should be:\n", trans2.Mul4(rot).Mul4(scale))
}