// drawScene renders the 3D models consisting of one VAO func (tag *trtag) drawScene() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) tag.checkError("gl.Clear") gl.UseProgram(tag.shaders) tag.checkError("gl.UseProgram") gl.BindVertexArray(tag.vao) tag.checkError("gl.BindVertexArray") // Use a modelview matrix and quaternion to rotate the 3D object. tag.mvp64.SetQ(lin.NewQ().SetAa(0, 1, 0, lin.Rad(-tag.rotateAngle))) tag.mvp64.TranslateMT(0, 0, -4) tag.mvp.Set(tag.mvp64.Mult(tag.mvp64, tag.persp)) gl.UniformMatrix4fv(tag.mvpRef, 1, false, tag.mvp.Pointer()) if err := gl.GetError(); err != 0 { fmt.Printf("gl.UniformMatrix error %d\n", err) } gl.DrawElements(gl.TRIANGLES, int32(len(tag.faces)), gl.UNSIGNED_BYTE, 0) if err := gl.GetError(); err != 0 { fmt.Printf("gl.DrawElements error %d\n", err) } // cleanup gl.UseProgram(0) tag.checkError("gl.UseProgram-0") gl.BindVertexArray(0) tag.checkError("gl.BindVertexArray-0") // rotate based on time... not on how fast the computer runs. if time.Now().Sub(tag.lastTime).Seconds() > 0.01 { tag.rotateAngle += 1 tag.lastTime = time.Now() } }
// drawScene renders the shader-only scene. func (sf *sftag) drawScene() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.UseProgram(sf.shaders) gl.BindVertexArray(sf.vao) timeSinceStart := time.Since(sf.sTime).Seconds() gl.Uniform1f(sf.gTime, float32(timeSinceStart)) gl.Uniform2f(sf.sizes, 500, 500) gl.UniformMatrix4fv(sf.mvpref, 1, false, sf.mvp.Pointer()) gl.DrawElements(gl.TRIANGLES, int32(len(sf.faces)), gl.UNSIGNED_BYTE, 0) // cleanup gl.UseProgram(0) gl.BindVertexArray(0) }
// drawScene renders the single texture on the quad. func (rt *rtrace) drawScene() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.UseProgram(rt.shaders) gl.Uniform1i(rt.tex2D, 0) gl.ActiveTexture(gl.TEXTURE0 + 0) gl.BindVertexArray(rt.vao) gl.UniformMatrix4fv(rt.mvpId, 1, false, rt.mvp.Pointer()) gl.DrawElements(gl.TRIANGLES, int32(len(rt.faces)), gl.UNSIGNED_BYTE, 0) // cleanup gl.ActiveTexture(0) gl.UseProgram(0) gl.BindVertexArray(0) }
// render draws the scene consisting of one VAO func (ld *ldtag) render() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.UseProgram(ld.shaders) gl.BindVertexArray(ld.vao) // use a model-view-projection matrix ld.mvp64.Set(lin.M4I).ScaleSM(0.5, 0.5, 0.5).TranslateMT(0, 0, -2) ld.mvp.Set(ld.mvp64.Mult(ld.mvp64, ld.persp)) gl.UniformMatrix4fv(ld.mvpref, 1, false, ld.mvp.Pointer()) gl.DrawElements(gl.TRIANGLES, ld.faceCount, gl.UNSIGNED_SHORT, 0) // cleanup gl.UseProgram(0) gl.BindVertexArray(0) }
// sh is used to test and showcase the vu/device package. Just getting a window // to appear demonstrates that the majority of the functionality is working. // The remainder of the example dumps keyboard and mouse events showing that // user input is being processed. func sh() { sh := &shtag{} dev := device.New("Shell", 400, 100, 800, 600) gl.Init() fmt.Printf("%s %s", gl.GetString(gl.RENDERER), gl.GetString(gl.VERSION)) fmt.Printf(" GLSL %s\n", gl.GetString(gl.SHADING_LANGUAGE_VERSION)) dev.Open() gl.ClearColor(0.3, 0.6, 0.4, 1.0) for dev.IsAlive() { sh.update(dev) gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) dev.SwapBuffers() // slow things down so that the loop is closer // to the engine update loop timing. time.Sleep(10 * time.Millisecond) } dev.ShowCursor(true) dev.Dispose() }
// Render implementation. // FUTURE: all kinds of possible optimizations that would need to be // profiled before implementing. // • group by vao to avoid switching vao's. // • group by texture to avoid switching textures. // • use interleaved vertex data. // • uniform buffers http://www.opengl.org/wiki/Uniform_Buffer_Object. // • ... lots more possiblities... leave your fav here. func (gc *opengl) Render(dr Draw) { d, ok := dr.(*draw) if !ok || d == nil { return } // switch state only if necessary. if gc.depthTest != d.depth { if d.depth { gl.Enable(gl.DEPTH_TEST) } else { gl.Disable(gl.DEPTH_TEST) } gc.depthTest = d.depth } // switch render framebuffer only if necessary. The framebuffer // is used to render to a texture associated with a framebuffer. if gc.fbo != d.fbo { gl.BindFramebuffer(gl.FRAMEBUFFER, d.fbo) if d.fbo == 0 { gl.Viewport(0, 0, gc.vw, gc.vh) } else { gl.Clear(gl.DEPTH_BUFFER_BIT) gl.Viewport(0, 0, 1024, 1024) // size convention for framebuffer texture. } gc.fbo = d.fbo } // switch shaders only if necessary. if gc.shader != d.shader { gl.UseProgram(d.shader) gc.shader = d.shader } // Ask the model to bind its provisioned uniforms. // FUTURE: only need to bind uniforms that have changed. gc.bindUniforms(d) // bind the data buffers and render. gl.BindVertexArray(d.vao) switch d.mode { case LINES: gl.PolygonMode(gl.FRONT_AND_BACK, gl.LINE) gl.DrawElements(gl.LINES, d.numFaces, gl.UNSIGNED_SHORT, 0) gl.PolygonMode(gl.FRONT_AND_BACK, gl.FILL) case POINTS: gl.Enable(gl.PROGRAM_POINT_SIZE) gl.DrawArrays(gl.POINTS, 0, d.numVerts) gl.Disable(gl.PROGRAM_POINT_SIZE) case TRIANGLES: if len(d.texs) > 1 && d.texs[0].fn > 0 { // Multiple textures on one model specify which verticies they apply to. for _, tex := range d.texs { // Use the same texture unit and sampler. Just update which // image is being sampled. gl.BindTexture(gl.TEXTURE_2D, tex.tid) // fn is the number of triangles, 3 indicies per triangle. // f0 is the offset in triangles where each triangle has 3 indicies // of 2 bytes (uShort) each. gl.DrawElements(gl.TRIANGLES, tex.fn*3, gl.UNSIGNED_SHORT, int64(3*2*tex.f0)) } } else { // Single textures are handled with a standard bindUniforms gl.DrawElements(gl.TRIANGLES, d.numFaces, gl.UNSIGNED_SHORT, 0) } } }
func (gc *opengl) Clear() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) }