// // Draw Loop Function // This function gets called on every update. // func drawLoop(glw *wrapper.Glw) { // Sets the Clear Color (Background Color) gl.ClearColor(0.0, 0.0, 0.0, 1.0) // Clears the Window gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) // Enables Depth gl.Enable(gl.DEPTH_TEST) // Sets the Shader program to Use gl.UseProgram(shaderProgram) // Define the model transformations for the cube cube.ResetModel() cube.Translate(x+0.5, y, z) cube.Scale(scale, scale, scale) //scale equally in all axis cube.Rotate(-angle_x, mgl32.Vec3{1, 0, 0}) //rotating in clockwise direction around x-axis cube.Rotate(-angle_y, mgl32.Vec3{0, 1, 0}) //rotating in clockwise direction around y-axis cube.Rotate(-angle_z, mgl32.Vec3{0, 0, 1}) //rotating in clockwise direction around z-axis // Define the model transformations for our sphere sphere.ResetModel() sphere.Translate(-x-0.5, 0, 0) sphere.Scale(scale/3.0, scale/3.0, scale/3.0) //scale equally in all axis sphere.Rotate(-angle_x, mgl32.Vec3{1, 0, 0}) //rotating in clockwise direction around x-axis sphere.Rotate(-angle_y, mgl32.Vec3{0, 1, 0}) //rotating in clockwise direction around y-axis sphere.Rotate(-angle_z, mgl32.Vec3{0, 0, 1}) //rotating in clockwise direction around z-axis // Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units var Projection mgl32.Mat4 = mgl32.Perspective(30.0, aspect_ratio, 0.1, 100.0) // Camera matrix var View mgl32.Mat4 = mgl32.LookAtV( mgl32.Vec3{0, 0, 4}, // Camera is at (0,0,4), in World Space mgl32.Vec3{0, 0, 0}, // and looks at the origin mgl32.Vec3{0, 1, 0}, // Head is up (set to 0,-1,0 to look upside-down) ) // Send our uniforms variables to the currently bound shader, gl.Uniform1ui(colourmodeUniform, uint32(colourmode)) gl.UniformMatrix4fv(viewUniform, 1, false, &View[0]) gl.UniformMatrix4fv(projectionUniform, 1, false, &Projection[0]) // Draws the Cube gl.UniformMatrix4fv(modelUniform, 1, false, &cube.Model[0]) cube.Draw() // Draw our sphere gl.UniformMatrix4fv(modelUniform, 1, false, &sphere.Model[0]) sphere.DrawSphere() gl.DisableVertexAttribArray(0) gl.UseProgram(0) /* Modify our animation variables */ angle_x += angle_inc_x angle_y += angle_inc_y angle_z += angle_inc_z }
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 render(w *glfw.Window, r *gResources) { /* width, height := w.GetFramebufferSize() gl.Viewport(0, 0, int32(width), int32(height)) gl.Clear(gl.COLOR_BUFFER_BIT) gl.MatrixMode(gl.PROJECTION) gl.LoadIdentity() gl.MatrixMode(gl.MODELVIEW) gl.LoadIdentity() */ //////////////// gl.UseProgram(r.program) gl.Uniform1f(r.uniforms.fadeFactor, r.fadeFactor) gl.ActiveTexture(gl.TEXTURE0) gl.BindTexture(gl.TEXTURE_2D, r.textures[0]) gl.Uniform1i(r.uniforms.textures[0], 0) gl.ActiveTexture(gl.TEXTURE1) gl.BindTexture(gl.TEXTURE_2D, r.textures[1]) gl.Uniform1i(r.uniforms.textures[1], 1) gl.BindBuffer(gl.ARRAY_BUFFER, r.vertexBuffer) gl.VertexAttribPointer( uint32(r.attributes.position), /* attribute */ 2, /* size */ gl.FLOAT, /* type */ false, /* normalized? */ 8, /* stride */ gl.PtrOffset(0)) /* array buffer offset */ gl.EnableVertexAttribArray(uint32(r.attributes.position)) gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, r.elementBuffer) gl.DrawElements( gl.TRIANGLE_STRIP, /* mode */ 4, /* count */ gl.UNSIGNED_INT, /* type */ gl.PtrOffset(0)) /* element array buffer offset */ gl.DisableVertexAttribArray(uint32(r.attributes.position)) }
func (glr *GlRenderer) initScreen() { var err error glr.program, err = createScreenShader() if err != nil { panic(err) } gl.UseProgram(glr.program) projection := mgl32.Ortho2D(-1, 1, 1, -1) projectionUniform := gl.GetUniformLocation(glr.program, gl.Str("projection\x00")) gl.UniformMatrix4fv(projectionUniform, 1, false, &projection[0]) textureUniform := gl.GetUniformLocation(glr.program, gl.Str("tex\x00")) gl.Uniform1i(textureUniform, 0) glr.texture = createScreenTexture(glr.width, glr.height) gl.GenVertexArrays(1, &glr.vao) gl.BindVertexArray(glr.vao) var quadVertices = []float32{ // X, Y, Z, U, V 1.0, -1.0, 0.0, 1.0, 0.0, -1.0, -1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, -1.0, -1.0, 0.0, 0.0, 0.0, -1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, } var vbo uint32 gl.GenBuffers(1, &vbo) gl.BindBuffer(gl.ARRAY_BUFFER, vbo) gl.BufferData(gl.ARRAY_BUFFER, len(quadVertices)*4, gl.Ptr(quadVertices), gl.STATIC_DRAW) vertAttrib := uint32(gl.GetAttribLocation(glr.program, gl.Str("vert\x00"))) gl.EnableVertexAttribArray(vertAttrib) gl.VertexAttribPointer(vertAttrib, 3, gl.FLOAT, false, 5*4, gl.PtrOffset(0)) texCoordAttrib := uint32(gl.GetAttribLocation(glr.program, gl.Str("vertTexCoord\x00"))) gl.EnableVertexAttribArray(texCoordAttrib) gl.VertexAttribPointer(texCoordAttrib, 2, gl.FLOAT, false, 5*4, gl.PtrOffset(3*4)) gl.Enable(gl.DEPTH_TEST) gl.ClearColor(1.0, 1.0, 1.0, 1.0) }
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 } }() }
// // DisableShader // Sets the Active Shader to 0 (none) // func (shaderManager *ShaderManager) DisableShader() { gl.UseProgram(0) shaderManager.ActiveShader = "" }
// // EnableShader // Sets the Active Shader to the one given // // @param name (string) the name of the shader to enable // func (shaderManager *ShaderManager) EnableShader(name string) { gl.UseProgram(shaderManager.Shaders[name].Shader) shaderManager.ActiveShader = name }
func render(w *glfw.Window, r *gResources) { width, height := w.GetFramebufferSize() ratio := float32(width) / float32(height) var xmul, ymul float32 if ratio > 1 { xmul, ymul = ra/ratio, ra } else { xmul, ymul = ra, ra*ratio } d := time.Since(start).Seconds() sin := float32(math.Sin(d)) cos := float32(math.Cos(d)) gl.Viewport(0, 0, int32(width), int32(height)) gl.Clear(gl.COLOR_BUFFER_BIT) //////////////// // axes gl.UseProgram(r.program1) gl.BindBuffer(gl.ARRAY_BUFFER, r.vertexBuffer1) gl.VertexAttribPointer( uint32(r.attributes1.position), // attribute 2, // size gl.FLOAT, // type false, // normalized? 8, // stride gl.PtrOffset(0)) // array buffer offset gl.EnableVertexAttribArray(uint32(r.attributes1.position)) gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, r.elementBuffer1) gl.LineWidth(1) gl.DrawElements( gl.LINES, // mode 4, // count gl.UNSIGNED_INT, // type gl.PtrOffset(0)) // element array buffer offset gl.DisableVertexAttribArray(uint32(r.attributes1.position)) //////////////// // triangle gl.UseProgram(r.program2) gl.Uniform1f(r.uniforms2.xmul, xmul) gl.Uniform1f(r.uniforms2.ymul, ymul) gl.Uniform1f(r.uniforms2.sin, sin) gl.Uniform1f(r.uniforms2.cos, cos) gl.BindBuffer(gl.ARRAY_BUFFER, r.vertexBuffer2) gl.VertexAttribPointer( uint32(r.attributes2.position), // attribute 2, // size gl.FLOAT, // type false, // normalized? 8, // stride gl.PtrOffset(0)) // array buffer offset gl.EnableVertexAttribArray(uint32(r.attributes2.position)) gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, r.elementBuffer2) gl.EnableVertexAttribArray(uint32(r.attributes2.color)) gl.BindBuffer(gl.ARRAY_BUFFER, r.colorBuffer2) gl.VertexAttribPointer( uint32(r.attributes2.color), // attribute 3, // size gl.FLOAT, // type false, // normalized? 0, // stride gl.PtrOffset(0)) // array buffer offset gl.DrawElements( gl.TRIANGLES, // mode 3, // count gl.UNSIGNED_INT, // type gl.PtrOffset(0)) // element array buffer offset //////////////// // circle gl.BindBuffer(gl.ARRAY_BUFFER, r.vertexBuffer3) gl.VertexAttribPointer( uint32(r.attributes2.position), // attribute 2, // size gl.FLOAT, // type false, // normalized? 8, // stride gl.PtrOffset(0)) // array buffer offset gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, r.elementBuffer3) gl.BindBuffer(gl.ARRAY_BUFFER, r.colorBuffer3) gl.VertexAttribPointer( uint32(r.attributes2.color), // attribute 3, // size gl.FLOAT, // type false, // normalized? 0, // stride gl.PtrOffset(0)) // array buffer offset gl.LineWidth(5) gl.DrawElements( gl.LINE_LOOP, // mode r.len3, // count gl.UNSIGNED_INT, // type gl.PtrOffset(0)) // element array buffer offset gl.DisableVertexAttribArray(uint32(r.attributes2.color)) gl.DisableVertexAttribArray(uint32(r.attributes2.position)) }
func createProgram(vertexShaderFile, fragmentShaderFile string, uniforms, attributes []string) (*Shader, error) { // Configure the vertex and fragment shaders vertexShaderSource, err := ioutil.ReadFile(vertexShaderFile) vertexShaderSource = append(vertexShaderSource, 0x00) fragmentShaderSource, err := ioutil.ReadFile(fragmentShaderFile) fragmentShaderSource = append(fragmentShaderSource, 0x00) vertexShader, err := compileShader(string(vertexShaderSource), gl.VERTEX_SHADER) if err != nil { return nil, err } fragmentShader, err := compileShader(string(fragmentShaderSource), gl.FRAGMENT_SHADER) if err != nil { return nil, err } program := gl.CreateProgram() gl.AttachShader(program, vertexShader) gl.AttachShader(program, fragmentShader) gl.LinkProgram(program) var status int32 gl.GetProgramiv(program, gl.LINK_STATUS, &status) if status == gl.FALSE { var logLength int32 gl.GetProgramiv(program, gl.INFO_LOG_LENGTH, &logLength) log := strings.Repeat("\x00", int(logLength+1)) gl.GetProgramInfoLog(program, logLength, nil, gl.Str(log)) return nil, fmt.Errorf("failed to link program: %v", log) } gl.DeleteShader(vertexShader) gl.DeleteShader(fragmentShader) gl.UseProgram(program) programUniforms := make(map[string]int32) for _, uniformName := range uniforms { if uniformName != "" { glstr := gl.Str(uniformName + "\x00") programUniforms[uniformName] = gl.GetUniformLocation(program, glstr) } } programAttributes := make(map[string]uint32) for _, attribName := range attributes { if attribName != "" { glstr := gl.Str(attribName + "\x00") programAttributes[attribName] = uint32(gl.GetAttribLocation(program, glstr)) } } return &Shader{ program: program, uniforms: programUniforms, attributes: programAttributes, }, nil }