Beispiel #1
0
func initScene() {
	halfW, halfH := float32(Width)*0.5, float32(Height)*0.5

	Projection = mgl.Ortho2D(-halfW, halfW, -halfH, halfH).Mul4(
		mgl.Translate3D(-halfW, -halfH, 0))

	ModelView = mgl.Ident4f().Mul4(mgl.Scale3D(100, 100, 0))
	MVP <- Projection.Mul4(ModelView)

	gl.Disable(gl.DEPTH_TEST)
	gl.DepthMask(false)

	program := Program(FragmentShader(fsh), VertexShader(vsh))
	gl.UseProgram(uint(program))
	uMVP = gl.GetUniformLocation(program, "uMVP")
	attrPos = gl.GetAttribLocation(program, "pos")
	attrColor = gl.GetAttribLocation(program, "color")

	gl.EnableVertexAttribArray(uint(attrPos))
	gl.EnableVertexAttribArray(uint(attrColor))
	gl.ClearColor(0.0, 0.0, 0.0, 1.0)
	gl.VertexAttribPointer(uint(attrPos), 2, gl.FLOAT, false, 0, uintptr(gl.Void(&POSITION[0])))
	gl.VertexAttribPointer(uint(attrColor), 4, gl.FLOAT, false, 0, uintptr(gl.Void(&COLOR[0])))

}
Beispiel #2
0
func NewWorld(width, height int) *World {
	gl.Enable(gl.DEPTH_TEST)
	gl.ClearColor(0.0, 0.0, 0.0, 0.0)
	gl.Viewport(0, 0, gl.Sizei(width), gl.Sizei(height))

	return &World{
		Width:      width,
		Height:     height,
		objects:    make([]*Cube, 0),
		projection: mathgl.Perspective(60, float32(width)/float32(height), 1, 20), // makePerspective(60.0, float32(width)/float32(height), 1.0, 20.0),
		view:       mathgl.Ident4f(),
	}
}
Beispiel #3
0
func NewCube() *Cube {
	cube := new(Cube)
	cube.model = mathgl.Ident4f()

	cube.Vertices = NewBufferFloat([]float32{
		// Front
		1, -1, 1, 1, TEX_COORD_MAX, 0,
		1, 1, 1, 1, TEX_COORD_MAX, TEX_COORD_MAX,
		-1, 1, 1, 1, 0, TEX_COORD_MAX,
		-1, -1, 1, 1, 0, 0,
		// Back
		1, 1, -1, 1, TEX_COORD_MAX, 0,
		-1, -1, -1, 1, 0, TEX_COORD_MAX,
		1, -1, -1, 1, TEX_COORD_MAX, TEX_COORD_MAX,
		-1, 1, -1, 1, 0, 0,
		// Left
		-1, -1, 1, 1, TEX_COORD_MAX, 0,
		-1, 1, 1, 1, TEX_COORD_MAX, TEX_COORD_MAX,
		-1, 1, -1, 1, 0, TEX_COORD_MAX,
		-1, -1, -1, 1, 0, 0,
		// Right
		1, -1, -1, 1, TEX_COORD_MAX, 0,
		1, 1, -1, 1, TEX_COORD_MAX, TEX_COORD_MAX,
		1, 1, 1, 1, 0, TEX_COORD_MAX,
		1, -1, 1, 1, 0, 0,
		// Top
		1, 1, 1, 1, TEX_COORD_MAX, 0,
		1, 1, -1, 1, TEX_COORD_MAX, TEX_COORD_MAX,
		-1, 1, -1, 1, 0, TEX_COORD_MAX,
		-1, 1, 1, 1, 0, 0,
		// Bottom
		1, -1, -1, 1, TEX_COORD_MAX, 0,
		1, -1, 1, 1, TEX_COORD_MAX, TEX_COORD_MAX,
		-1, -1, 1, 1, 0, TEX_COORD_MAX,
		-1, -1, -1, 1, 0, 0,
	})
	cube.indices = NewBufferByte([]byte{
		// Front
		0, 1, 2,
		2, 3, 0,
		// Back
		4, 5, 6,
		4, 5, 7,
		// Left
		8, 9, 10,
		10, 11, 8,
		// Right
		12, 13, 14,
		14, 15, 12,
		// Top
		16, 17, 18,
		18, 19, 16,
		// Bottom
		20, 21, 22,
		22, 23, 20,
	})

	fragmentShader := (FragmentShader)(`
        precision highp float;
	varying vec2 texOut;
        uniform sampler2D texture;

	void main() {
		gl_FragColor = texture2D(texture, texOut);
	}
        `)
	vertexShader := (VertexShader)(`
        uniform mat4 model;
        uniform mat4 projection_view;
        attribute vec4 pos;
        attribute vec2 texIn;
        varying vec2 texOut;

        void main() {
          gl_Position = projection_view*model*pos;
          texOut = texIn;
        }
        `)

	fsh := fragmentShader.Compile()
	vsh := vertexShader.Compile()
	cube.Program.Link(fsh, vsh)

	cube.Program.Use()
	cube.attrPos = cube.Program.GetAttribute("pos")
	cube.attrColor = cube.Program.GetAttribute("color")
	cube.attrTexIn = cube.Program.GetAttribute("texIn")
	cube.uniformTexture = cube.Program.GetUniform("texture")

	cube.uniformModel = cube.Program.GetUniform("model")
	cube.uniformProjectionView = cube.Program.GetUniform("projection_view")

	gl.EnableVertexAttribArray(cube.attrPos)
	gl.EnableVertexAttribArray(cube.attrColor)
	gl.EnableVertexAttribArray(cube.attrTexIn)

	return cube
}