func BoxFlipbook(geometry *renderer.Geometry, frame, framesX, framesY int) { frameSizeX := 1.0 / float32(framesX) frameSizeY := 1.0 / float32(framesY) indexX := float32(frame % framesX) indexY := float32(framesY - (frame / framesY) - 1) u1, u2 := frameSizeX*indexX, frameSizeX*(indexX+1.0) v1, v2 := frameSizeY*indexY, frameSizeY*(indexY+1.0) geometry.SetUVs(u1, v1, u2, v1, u2, v2, u1, v2) }
// CreateGeometry - add geometry to the renderer func (glRenderer *OpenglRenderer) CreateGeometry(geometry *renderer.Geometry) { var vbo uint32 gl.GenBuffers(1, &vbo) gl.BindBuffer(gl.ARRAY_BUFFER, vbo) gl.BufferData(gl.ARRAY_BUFFER, len(geometry.Verticies)*4, gl.Ptr(geometry.Verticies), gl.DYNAMIC_DRAW) geometry.VboId = vbo var ibo uint32 gl.GenBuffers(1, &ibo) gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, ibo) gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, len(geometry.Indicies)*4, gl.Ptr(geometry.Indicies), gl.DYNAMIC_DRAW) geometry.IboId = ibo }
func (glRenderer *OpenglRenderer) DrawGeometry(geometry *renderer.Geometry, transform mgl32.Mat4) { glRenderer.enableShader() glRenderer.enableMaterial() glRenderer.enableCubeMap() if glRenderer.activeShader == nil { panic("ERROR: No shader is configured.") } shader := glRenderer.activeShader program := shader.Program params := glRenderer.rendererParams glRenderer.enableDepthTest(params.DepthTest) glRenderer.enableDepthMask(params.DepthMask) glRenderer.enableCullFace(params.CullBackface) glRenderer.enableUnlit(params.Unlit) glRenderer.setTransparency(params.Transparency) // set buffers gl.BindBuffer(gl.ARRAY_BUFFER, geometry.VboId) gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, geometry.IboId) // update buffers if geometry.VboDirty && len(geometry.Verticies) > 0 && len(geometry.Indicies) > 0 { gl.BufferData(gl.ARRAY_BUFFER, len(geometry.Verticies)*4, gl.Ptr(geometry.Verticies), gl.DYNAMIC_DRAW) gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, len(geometry.Indicies)*4, gl.Ptr(geometry.Indicies), gl.DYNAMIC_DRAW) geometry.VboDirty = false } // set uniforms modelNormal := transform.Inv().Transpose() shader.Uniforms["model"] = transform shader.Uniforms["modelNormal"] = modelNormal // set camera uniforms cam := glRenderer.camera win := glRenderer.WindowDimensions() shader.Uniforms["cameraTranslation"] = cam.Translation if cam.Ortho { shader.Uniforms["projection"] = mgl32.Ortho2D(0, win.X(), win.Y(), 0) shader.Uniforms["camera"] = mgl32.Ident4() } else { shader.Uniforms["projection"] = mgl32.Perspective(mgl32.DegToRad(cam.Angle), win.X()/win.Y(), cam.Near, cam.Far) shader.Uniforms["camera"] = mgl32.LookAtV(cam.Translation, cam.Lookat, cam.Up) } shader.Uniforms["unlit"] = glRenderer.unlit shader.Uniforms["useTextures"] = glRenderer.useTextures shader.Uniforms["ambientLightValue"] = glRenderer.ambientLightValue shader.Uniforms["nbPointLights"] = glRenderer.nbPointLights shader.Uniforms["pointLightValues"] = glRenderer.pointLightValues shader.Uniforms["pointLightPositions"] = glRenderer.pointLightPositions shader.Uniforms["nbDirectionalLights"] = glRenderer.nbDirectionalLights shader.Uniforms["directionalLightValues"] = glRenderer.directionalLightValues shader.Uniforms["directionalLightVectors"] = glRenderer.directionalLightVectors // set custom uniforms setupUniforms(shader) // set verticies attribute vertAttrib := uint32(gl.GetAttribLocation(program, gl.Str("vert\x00"))) gl.EnableVertexAttribArray(vertAttrib) gl.VertexAttribPointer(vertAttrib, 3, gl.FLOAT, false, renderer.VertexStride*4, gl.PtrOffset(0)) // set normals attribute normAttrib := uint32(gl.GetAttribLocation(program, gl.Str("normal\x00"))) gl.EnableVertexAttribArray(normAttrib) gl.VertexAttribPointer(normAttrib, 3, gl.FLOAT, false, renderer.VertexStride*4, gl.PtrOffset(3*4)) // set texture coord attribute texCoordAttrib := uint32(gl.GetAttribLocation(program, gl.Str("texCoord\x00"))) gl.EnableVertexAttribArray(texCoordAttrib) gl.VertexAttribPointer(texCoordAttrib, 2, gl.FLOAT, false, renderer.VertexStride*4, gl.PtrOffset(6*4)) // vertex color attribute colorAttrib := uint32(gl.GetAttribLocation(program, gl.Str("color\x00"))) gl.EnableVertexAttribArray(colorAttrib) gl.VertexAttribPointer(colorAttrib, 4, gl.FLOAT, false, renderer.VertexStride*4, gl.PtrOffset(8*4)) gl.DrawElements(gl.TRIANGLES, (int32)(len(geometry.Indicies)), gl.UNSIGNED_INT, gl.PtrOffset(0)) }