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 (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() }
// 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 (node *Node) Optimize(geometry *Geometry, transform mgl32.Mat4) { newTransform := transform.Mul4(node.Transform) for _, child := range node.children { child.Optimize(geometry, newTransform) } }