func (node *Node) Draw(renderer Renderer, transform mgl32.Mat4) { node.load(renderer) tx := transform.Mul4(node.Transform) for _, child := range node.children { node.DrawChild(renderer, tx, child) } node.cleanupDeleted(renderer) }
func GetInverseMatrix(m mgl32.Mat4) (out mgl32.Mat4, err error) { var ( empty = mgl32.Mat4{} ) if out = m.Inv(); out == empty { err = fmt.Errorf("Matrix %v not invertible", m) return } return }
func Unproject(invproj mgl32.Mat4, x float32, y float32) (wx, wy float32) { var ( screen = mgl32.Vec4{x, y, 1, 1} out mgl32.Vec4 ) out = invproj.Mul4x1(screen) out = out.Mul(1.0 / out[3]) wx = out[0] wy = out[1] return }
func (i *Instance) GetModel() mgl32.Mat4 { if i.dirty { var model mgl32.Mat4 model = mgl32.Translate3D( i.position.X(), i.position.Y(), i.position.Z(), ) model = model.Mul4(mgl32.HomogRotate3DZ(mgl32.DegToRad(i.rotation))) model = model.Mul4(mgl32.Scale3D(i.scale.X(), i.scale.Y(), i.scale.Z())) i.model = model i.dirty = false } return i.model }
// MultiMul multiplies every non-nil Mat4 reference and returns the result. If // none are given, then it returns the identity matrix. func MultiMul(matrices ...*mgl.Mat4) mgl.Mat4 { var r mgl.Mat4 ok := false for _, m := range matrices { if m == nil { continue } if !ok { r = *m ok = true continue } r = r.Mul4(*m) } if ok { return r } return mgl.Ident4() }
func Project(proj mgl32.Mat4, x float32, y float32) (sx, sy float32) { var out mgl32.Vec4 out = proj.Mul4x1(mgl32.Vec4{x, y, 1, 1}) return out[0], out[1] }
// Left multiplies the current top of the matrix by the // argument. func (ms *MatStack) LeftMul(m mgl32.Mat4) { (*ms)[len(*ms)-1] = m.Mul4((*ms)[len(*ms)-1]) }
func (r *Renderable) Draw(perspective mgl.Mat4, view mgl.Mat4) { gl.UseProgram(r.Shader) gl.BindVertexArray(r.Vao) model := r.GetTransformMat4() var mvp mgl.Mat4 shaderMvp := getUniformLocation(r.Shader, "MVP_MATRIX") if shaderMvp >= 0 { mvp = perspective.Mul4(view).Mul4(model) gl.UniformMatrix4fv(shaderMvp, 1, false, &mvp[0]) } shaderMv := getUniformLocation(r.Shader, "MV_MATRIX") if shaderMv >= 0 { mv := view.Mul4(model) gl.UniformMatrix4fv(shaderMv, 1, false, &mv[0]) } shaderTex0 := getUniformLocation(r.Shader, "DIFFUSE_TEX") if shaderTex0 >= 0 { gl.ActiveTexture(gl.TEXTURE0) gl.BindTexture(gl.TEXTURE_2D, r.Tex0) gl.Uniform1i(shaderTex0, 0) } shaderColor := getUniformLocation(r.Shader, "MATERIAL_DIFFUSE") if shaderColor >= 0 { gl.Uniform4f(shaderColor, r.Color[0], r.Color[1], r.Color[2], r.Color[3]) } shaderTex1 := getUniformLocation(r.Shader, "MATERIAL_TEX_0") if shaderTex1 >= 0 { gl.ActiveTexture(gl.TEXTURE0) gl.BindTexture(gl.TEXTURE_2D, r.Tex0) gl.Uniform1i(shaderTex1, 0) } shaderCameraWorldPos := getUniformLocation(r.Shader, "CAMERA_WORLD_POSITION") if shaderCameraWorldPos >= 0 { gl.Uniform3f(shaderCameraWorldPos, -view[12], -view[13], -view[14]) } shaderPosition := getAttribLocation(r.Shader, "VERTEX_POSITION") if shaderPosition >= 0 { gl.BindBuffer(gl.ARRAY_BUFFER, r.VertVBO) gl.EnableVertexAttribArray(uint32(shaderPosition)) gl.VertexAttribPointer(uint32(shaderPosition), 3, gl.FLOAT, false, 0, gl.PtrOffset(0)) } shaderNormal := getAttribLocation(r.Shader, "VERTEX_NORMAL") if shaderNormal >= 0 { gl.BindBuffer(gl.ARRAY_BUFFER, r.NormsVBO) gl.EnableVertexAttribArray(uint32(shaderNormal)) gl.VertexAttribPointer(uint32(shaderNormal), 3, gl.FLOAT, false, 0, gl.PtrOffset(0)) } shaderVertUv := getAttribLocation(r.Shader, "VERTEX_UV_0") if shaderVertUv >= 0 { gl.BindBuffer(gl.ARRAY_BUFFER, r.UvVBO) gl.EnableVertexAttribArray(uint32(shaderVertUv)) gl.VertexAttribPointer(uint32(shaderVertUv), 2, gl.FLOAT, false, 0, gl.PtrOffset(0)) } gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, r.ElementsVBO) gl.DrawElements(gl.TRIANGLES, int32(r.FaceCount*3), gl.UNSIGNED_INT, gl.PtrOffset(0)) gl.BindVertexArray(0) }
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)) }
func (node *Node) Optimize(geometry *Geometry, transform mgl32.Mat4) { newTransform := transform.Mul4(node.Transform) for _, child := range node.children { child.Optimize(geometry, newTransform) } }