func (cube *Cube) Draw() { /* Bind cube vertices. Note that this is in attribute index 0 */ gl.BindBuffer(gl.ARRAY_BUFFER, cube.bufferObject) gl.EnableVertexAttribArray(0) gl.VertexAttribPointer(0, 3, gl.FLOAT, false, 0, nil) /* Bind cube colours. Note that this is in attribute index 1 */ gl.BindBuffer(gl.ARRAY_BUFFER, cube.coloursObject) gl.EnableVertexAttribArray(1) gl.VertexAttribPointer(1, 4, gl.FLOAT, false, 0, nil) /* Bind cube normals. Note that this is in attribute index 2 */ gl.EnableVertexAttribArray(2) gl.BindBuffer(gl.ARRAY_BUFFER, cube.normalsObject) gl.VertexAttribPointer(2, 3, gl.FLOAT, false, 0, nil) // Enable this line to show model in wireframe if cube.DrawMode == DRAW_LINES { gl.PolygonMode(gl.FRONT_AND_BACK, gl.LINE) } else { gl.PolygonMode(gl.FRONT_AND_BACK, gl.FILL) } /* Draw our cube*/ if cube.DrawMode == DRAW_POINTS { gl.DrawArrays(gl.POINTS, 0, int32(32)) } else { gl.DrawArrays(gl.TRIANGLES, 0, 36) } }
// Draws the sphere form the previously defined vertex and index buffers func (sphere *Sphere) Draw() { // Adds the Sphere Model to the Active Shader sphere.ShaderManager.SetUniformMatrix4fv(sphere.ShaderManager.ActiveShader, "model", 1, false, &sphere.Model[0]) /* Draw the vertices as GL_POINTS */ gl.BindBuffer(gl.ARRAY_BUFFER, sphere.sphereBufferObject) gl.VertexAttribPointer(0, 3, gl.FLOAT, false, 0, nil) gl.EnableVertexAttribArray(0) /* Bind the sphere colours */ gl.BindBuffer(gl.ARRAY_BUFFER, sphere.sphereColours) gl.VertexAttribPointer(1, 4, gl.FLOAT, false, 0, nil) gl.EnableVertexAttribArray(1) /* Bind the sphere normals */ gl.BindBuffer(gl.ARRAY_BUFFER, sphere.sphereNormals) gl.VertexAttribPointer(2, 3, gl.FLOAT, false, 0, nil) gl.EnableVertexAttribArray(2) gl.PointSize(3.0) // Enable this line to show model in wireframe if sphere.DrawMode == DRAW_LINES { gl.PolygonMode(gl.FRONT_AND_BACK, gl.LINE) } else { gl.PolygonMode(gl.FRONT_AND_BACK, gl.FILL) } if sphere.DrawMode == DRAW_POINTS { gl.DrawArrays(gl.POINTS, 0, int32(sphere.numSphereVertices)) } else { /* Bind the indexed vertex buffer */ gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, sphere.elementBuffer) /* Draw the north pole regions as a triangle */ gl.DrawElements(gl.TRIANGLE_FAN, int32(sphere.numLongs+2), gl.UNSIGNED_INT, nil) /* Calculate offsets into the indexed array. Note that we multiply offsets by 4 because it is a memory offset the indices are type GLuint which is 4-bytes */ var lat_offset_jump int = int((sphere.numLongs * 2) + 2) var lat_offset_start int = int(sphere.numLongs + 2) var lat_offset_current int = lat_offset_start * 4 var i uint32 /* Draw the triangle strips of latitudes */ for i = 0; i < sphere.numLats-2; i++ { gl.DrawElements(gl.TRIANGLE_STRIP, int32(sphere.numLongs*2+2), gl.UNSIGNED_INT, gl.PtrOffset(lat_offset_current)) lat_offset_current += (lat_offset_jump * 4) } /* Draw the south pole as a triangle fan */ gl.DrawElements(gl.TRIANGLE_FAN, int32(sphere.numLongs+2), gl.UNSIGNED_INT, gl.PtrOffset(lat_offset_current)) } }
// Draws the cog form the previously defined vertex and index buffers func (cog *Cog) Draw() { // Adds the Sphere Model to the Active Shader cog.ShaderManager.SetUniformMatrix4fv(cog.ShaderManager.ActiveShader, "model", 1, false, &cog.Model[0]) /* Draw the vertices as GL_POINTS */ gl.BindBuffer(gl.ARRAY_BUFFER, cog.cogBufferObject) gl.VertexAttribPointer(0, 3, gl.FLOAT, false, 0, nil) gl.EnableVertexAttribArray(0) /* Bind the sphere colours */ gl.BindBuffer(gl.ARRAY_BUFFER, cog.cogColours) gl.VertexAttribPointer(1, 4, gl.FLOAT, false, 0, nil) gl.EnableVertexAttribArray(1) /* Bind the sphere normals */ gl.BindBuffer(gl.ARRAY_BUFFER, cog.cogNormals) gl.VertexAttribPointer(2, 3, gl.FLOAT, false, 0, nil) gl.EnableVertexAttribArray(2) gl.PointSize(3.0) // Enable this line to show model in wireframe if cog.DrawMode == DRAW_LINES { gl.PolygonMode(gl.FRONT_AND_BACK, gl.LINE) } else { gl.PolygonMode(gl.FRONT_AND_BACK, gl.FILL) } if cog.DrawMode == DRAW_POINTS { gl.DrawArrays(gl.POINTS, 0, int32(cog.numCogVertices)) } else { // Bind the indexed vertex buffer gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, cog.elementBuffer) // Draw the north pole regions as a triangle gl.DrawElements(gl.TRIANGLE_FAN, int32(cog.VerticesPerDisk+2), gl.UNSIGNED_INT, nil) // Calculate offsets into the indexed array. Note that we multiply offsets by 4 // because it is a memory offset the indices are type GLuint which is 4-bytes var lat_offset_jump int = int((cog.VerticesPerDisk * 2) + 2) var lat_offset_start int = int(cog.VerticesPerDisk + 2) var lat_offset_current int = lat_offset_start * 4 // Draw the triangle strips of Sides gl.DrawElements(gl.TRIANGLE_STRIP, int32(cog.VerticesPerDisk*2+2), gl.UNSIGNED_INT, gl.PtrOffset(lat_offset_current)) lat_offset_current += (lat_offset_jump * 4) // Draw the south pole as a triangle fan gl.DrawElements(gl.TRIANGLE_FAN, int32(cog.VerticesPerDisk+2), gl.UNSIGNED_INT, gl.PtrOffset(lat_offset_current)) } }
func (r *Renderer) Render() { // defer glfw.Terminate() shader := r.Shaders.textureFlat program := shader.program // gl.UseProgram(program) // gl.BindFragDataLocation(program, 0, gl.Str("outputColor\x00")) // // Configure global settings gl.Enable(gl.DEPTH_TEST) gl.DepthFunc(gl.LESS) gl.ClearColor(1.0, 1.0, 1.0, 1.0) // // angle += elapsed // r.Mesh.modelView = mgl32.HomogRotate3D(float32(angle), mgl32.Vec3{0, 1, 0}) // Render // gl.UniformMatrix4fv(shader.uniforms["modelView"], 1, false, &r.Mesh.modelView[0]) time := glfw.GetTime() _ = time - r.PreviousTime r.PreviousTime = time // fmt.Println(elapsed * 100) gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.UniformMatrix4fv(shader.uniforms["projection"], 1, false, &r.Projection[0]) gl.UniformMatrix4fv(shader.uniforms["camera"], 1, false, &r.Camera[0]) // TODO : batch triangles and use multiple textures for _, mesh := range r.Meshes { gl.UniformMatrix4fv(shader.uniforms["modelView"], 1, false, &mesh.modelView[0]) gl.Uniform1i(shader.uniforms["tex"], 0) gl.BindVertexArray(mesh.vao) gl.ActiveTexture(gl.TEXTURE0) gl.BindTexture(gl.TEXTURE_2D, mesh.textures[0]) gl.DrawArrays(gl.TRIANGLES, 0, int32(len(mesh.verticies)/5)) } // Maintenance r.Window.SwapBuffers() glfw.PollEvents() if r.Ready == false { r.Ready = true } }
func (glr *GlRenderer) Start() { go func() { glr.initSystem() glr.initScreen() // Prepare 2 frames to double buffer for i := 0; i < 2; i++ { glr.freeFrames <- &Frame{ glr.window.ShouldClose(), make([]maths.Rgba, glr.width*glr.height), glr.width, glr.height} } // Loop until stop is requested for nextFrame := range glr.framesToRender { if nextFrame.ShouldStop { break } gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.UseProgram(glr.program) gl.BindVertexArray(glr.vao) updateScreenTexture(glr.texture, nextFrame.Pixels, glr.width, glr.height) gl.DrawArrays(gl.TRIANGLES, 0, 6) glr.window.SwapBuffers() glfw.PollEvents() // Consider frame rendered, so push back to be reused nextFrame.ShouldStop = glr.window.ShouldClose() glr.freeFrames <- nextFrame } }() }
/* Enable vertex attributes and draw object Could improve efficiency by moving the vertex attribute pointer functions to the create object but this method is more general This code is almost untouched fomr the tutorial code except that I changed the number of elements per vertex from 4 to 3*/ func (terrain *Terrain) DrawObject(shaderProgram uint32) { toneUniform := gl.GetAttribLocation(shaderProgram, gl.Str("tone\x00")) gl.Uniform4f(toneUniform, terrain.ColorTone.X(), terrain.ColorTone.Y(), terrain.ColorTone.Z(), terrain.ColorTone.W()) // gl.Uniform4f(toneUniform, 0.0, 1.0, 0.5, 1.0) // gl.Uniform4fv(toneUniform, 1, &terrain.ColorTone[0]) // Reads the uniform Locations modelUniform := gl.GetUniformLocation(shaderProgram, gl.Str("model\x00")) // Send our uniforms variables to the currently bound shader gl.UniformMatrix4fv(modelUniform, 1, false, &terrain.Model[0]) // Get the vertices uniform position verticesUniform := uint32(gl.GetAttribLocation(shaderProgram, gl.Str("position\x00"))) normalsUniform := uint32(gl.GetAttribLocation(shaderProgram, gl.Str("normal\x00"))) colorsUniform := uint32(gl.GetAttribLocation(shaderProgram, gl.Str("colour\x00"))) // Describe our vertices array to OpenGL (it can't guess its format automatically) gl.EnableVertexAttribArray(verticesUniform) gl.BindBuffer(gl.ARRAY_BUFFER, terrain.VBOVertices) gl.VertexAttribPointer( verticesUniform, // attribute index 3, // number of elements per vertex, here (x,y,z) gl.FLOAT, // the type of each element false, // take our values as-is 0, // no extra data between each position nil, // offset of first element ) gl.EnableVertexAttribArray(normalsUniform) gl.BindBuffer(gl.ARRAY_BUFFER, terrain.VBONormals) gl.VertexAttribPointer( normalsUniform, // attribute 3, // number of elements per vertex, here (x,y,z) gl.FLOAT, // the type of each element false, // take our values as-is 0, // no extra data between each position nil, // offset of first element ) gl.EnableVertexAttribArray(colorsUniform) gl.BindBuffer(gl.ARRAY_BUFFER, terrain.VBOColors) gl.VertexAttribPointer( colorsUniform, // attribute 3, // number of elements per vertex, here (x,y,z) gl.FLOAT, // the type of each element false, // take our values as-is 0, // no extra data between each position nil, // offset of first element ) size := int32(len(terrain.Indices)) gl.PointSize(3.0) gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, terrain.VBOIndices) gl.GetBufferParameteriv(gl.ELEMENT_ARRAY_BUFFER, gl.BUFFER_SIZE, &size) // Enable this line to show model in wireframe switch terrain.DrawMode { case 1: gl.PolygonMode(gl.FRONT_AND_BACK, gl.LINE) case 2: gl.DrawArrays(gl.POINTS, 0, int32(len(terrain.Vertices))) // gl.DrawElements(gl.POINTS, int32(len(terrain.Indices)), gl.UNSIGNED_SHORT, nil) return default: gl.PolygonMode(gl.FRONT_AND_BACK, gl.FILL) } var location int = 0 /* Draw the triangle strips */ for i := uint32(0); i < terrain.XSize-1; i++ { location = SizeOfUint16 * int(i*terrain.ZSize*2) gl.DrawElements(gl.TRIANGLE_STRIP, int32(terrain.ZSize*2), gl.UNSIGNED_SHORT, gl.PtrOffset(location)) } // gl.DrawElements( // gl.TRIANGLE_STRIP, // len(terrain.Indices), // gl.UNSIGNED_SHORT, // nil, // ) }