func SetOrthographic(width, height int) { // Projection := mathgl.Perspective(45.0, winWidth/winHeight, 0.1, 100.0) // Projection := mathgl.Ortho2D(0.0, winWidth, winHeight, 0.0) // aspect := float32(s.width / s.height) Projection := mathgl.Ortho(0.0, float32(width), float32(height), 0.0, -5.0, 5.0) viewM = mathgl.LookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) projectionM = Projection gl.Disable(gl.DEPTH_TEST) }
func SetPerspective(width, height int) { Projection := mathgl.Perspective(mathgl.DegToRad(45.0), float32(width/height), 0.1, 100.0) viewM = mathgl.LookAt(0.0, 0.0, 20.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) projectionM = Projection gl.Disable(gl.CULL_FACE) gl.Enable(gl.DEPTH_TEST) }
// GetCameraTransform gets the matrix to transform from world coordinates to // this camera's coordinates. func (camera *FpsCamera) GetTransform() mgl32.Mat4 { cameraTarget := camera.pos.Add(camera.front) return mgl32.LookAt( camera.pos.X(), camera.pos.Y(), camera.pos.Z(), cameraTarget.X(), cameraTarget.Y(), cameraTarget.Z(), camera.up.X(), camera.up.Y(), camera.up.Z(), ) }
func main() { vertices, normals := obj.Parse(os.Args[1]) // initialize GLFW if err := glfw.Init(); err != nil { panic(err) } defer glfw.Terminate() // set opengl core profile 3.3 glfw.WindowHint(glfw.ContextVersionMajor, 3) glfw.WindowHint(glfw.ContextVersionMinor, 3) glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile) glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True) window, err := glfw.CreateWindow(640, 480, "GOpenGL", nil, nil) if err != nil { panic(err) } window.MakeContextCurrent() // initialise OpenGL library if err := gl.Init(); err != nil { panic(err) } // link program from shaders program, err := newProgram("vertex.glsl", "fragment.glsl") if err != nil { panic(err) } gl.UseProgram(program) // vertex attribute object holds links between attributes and vbo var vao uint32 gl.GenVertexArrays(1, &vao) gl.BindVertexArray(vao) // vertex buffer with per-vertex data var vbo [2]uint32 gl.GenBuffers(2, &vbo[0]) // position data gl.BindBuffer(gl.ARRAY_BUFFER, vbo[0]) gl.BufferData(gl.ARRAY_BUFFER, len(vertices)*4, gl.Ptr(vertices), gl.STATIC_DRAW) // set up position attribute with layout of vertices posAttrib := uint32(gl.GetAttribLocation(program, gl.Str("position\x00"))) gl.VertexAttribPointer(posAttrib, 3, gl.FLOAT, false, 3*4, gl.PtrOffset(0)) gl.EnableVertexAttribArray(posAttrib) // normal data gl.BindBuffer(gl.ARRAY_BUFFER, vbo[1]) gl.BufferData(gl.ARRAY_BUFFER, len(normals)*4, gl.Ptr(normals), gl.STATIC_DRAW) normAttrib := uint32(gl.GetAttribLocation(program, gl.Str("normal\x00"))) gl.VertexAttribPointer(normAttrib, 3, gl.FLOAT, false, 3*4, gl.PtrOffset(0)) gl.EnableVertexAttribArray(normAttrib) uniModel := gl.GetUniformLocation(program, gl.Str("model\x00")) uniView := gl.GetUniformLocation(program, gl.Str("view\x00")) uniProj := gl.GetUniformLocation(program, gl.Str("proj\x00")) matView := mgl32.LookAt(2.0, 2.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0) gl.UniformMatrix4fv(uniView, 1, false, &matView[0]) matProj := mgl32.Perspective(mgl32.DegToRad(45.0), 640.0/480.0, 1.0, 10.0) gl.UniformMatrix4fv(uniProj, 1, false, &matProj[0]) uniLightDir := gl.GetUniformLocation(program, gl.Str("lightDir\x00")) uniLightCol := gl.GetUniformLocation(program, gl.Str("lightCol\x00")) gl.Uniform3f(uniLightDir, -0.5, 0.0, -1.0) gl.Uniform3f(uniLightCol, 0.0, 0.5, 0.5) startTime := glfw.GetTime() gl.Enable(gl.DEPTH_TEST) gl.ClearColor(1.0, 1.0, 1.0, 1.0) for !window.ShouldClose() { // clear buffer gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) matRot := mgl32.HomogRotate3DZ(float32(glfw.GetTime() - startTime)) gl.UniformMatrix4fv(uniModel, 1, false, &matRot[0]) gl.DrawArrays(gl.TRIANGLES, 0, int32(len(vertices))) window.SwapBuffers() glfw.PollEvents() } }
//LookAtVec sets the camera view direction by vectors. func (c *Camera) LookAtVec(eye, center, up *glm.Vec3) { c.View = glm.LookAt(eye[0], eye[1], eye[2], center[0], center[1], center[2], up[0], up[1], up[2]) c.Pos = *eye }
//LookAtval sets the camera view direction by value. func (c *Camera) LookAtval(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ float32) { c.View = glm.LookAt(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ) c.Pos[0], c.Pos[1], c.Pos[2] = eyeX, eyeY, eyeZ }
//LookAt sets the view matrix to look at (tx,ty,tz) from (ex,ey,ez), the up direction is always (0,1,0). func (sfbo *ShadowFBO) LookAt(ex, ey, ez, tx, ty, tz float32) { sfbo.view = glm.LookAt(ex, ey, ez, tx, ty, tz, 0, 1, 0) }
func main() { runtime.LockOSThread() if !glfw.Init() { fmt.Fprintf(os.Stderr, "Can't open GLFW") return } defer glfw.Terminate() glfw.WindowHint(glfw.Samples, 4) glfw.WindowHint(glfw.ContextVersionMajor, 3) glfw.WindowHint(glfw.ContextVersionMinor, 3) glfw.WindowHint(glfw.OpenglProfile, glfw.OpenglCoreProfile) glfw.WindowHint(glfw.OpenglForwardCompatible, glfw.True) // needed for macs window, err := glfw.CreateWindow(1024, 768, "Tutorial 3", nil, nil) if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) return } window.MakeContextCurrent() gl.Init() gl.GetError() // Ignore error window.SetInputMode(glfw.StickyKeys, 1) gl.ClearColor(0., 0., 0.4, 0.) vertexArray := gl.GenVertexArray() defer vertexArray.Delete() vertexArray.Bind() prog := helper.MakeProgram("SimpleTransform.vertexshader", "SingleColor.fragmentshader") defer prog.Delete() matrixID := prog.GetUniformLocation("MVP") Projection := mgl32.Perspective(45.0, 4.0/3.0, 0.1, 100.0) //Projection := mathgl.Identity(4,mathgl.FLOAT64) //Projection := mathgl.Ortho2D(-5,5,-5,5) View := mgl32.LookAt(4.0, 3.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) //View := mathgl.Identity(4,mathgl.FLOAT64) Model := mgl32.Ident4() //Model := mathgl.Scale3D(2.,2.,2.).Mul(mathgl.HomogRotate3DX(25.0)).Mul(mathgl.Translate3D(.5,.2,-.7)) MVP := Projection.Mul4(View).Mul4(Model) // Remember, transform multiplication order is "backwards" vBufferData := [...]float32{ -1., -1., 0., 1., -1., 0., 0., 1., 0.} //elBufferData := [...]uint8{0, 1, 2} // Not sure why this is here buffer := gl.GenBuffer() defer buffer.Delete() buffer.Bind(gl.ARRAY_BUFFER) gl.BufferData(gl.ARRAY_BUFFER, len(vBufferData)*4, &vBufferData, gl.STATIC_DRAW) // Equivalent to a do... while for ok := true; ok; ok = (window.GetKey(glfw.KeyEscape) != glfw.Press && !window.ShouldClose()) { gl.Clear(gl.COLOR_BUFFER_BIT) prog.Use() matrixID.UniformMatrix4fv(false, MVP) attribLoc := gl.AttribLocation(0) attribLoc.EnableArray() buffer.Bind(gl.ARRAY_BUFFER) attribLoc.AttribPointer(3, gl.FLOAT, false, 0, nil) gl.DrawArrays(gl.TRIANGLES, 0, 3) attribLoc.DisableArray() window.SwapBuffers() glfw.PollEvents() } }
func main() { runtime.LockOSThread() if !glfw.Init() { fmt.Fprintf(os.Stderr, "Can't open GLFW") return } defer glfw.Terminate() glfw.WindowHint(glfw.Samples, 4) glfw.WindowHint(glfw.ContextVersionMajor, 3) glfw.WindowHint(glfw.ContextVersionMinor, 3) glfw.WindowHint(glfw.OpenglProfile, glfw.OpenglCoreProfile) glfw.WindowHint(glfw.OpenglForwardCompatible, glfw.True) // needed for macs window, err := glfw.CreateWindow(1024, 768, "Tutorial 4", nil, nil) if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) return } window.MakeContextCurrent() gl.Init() gl.GetError() // Ignore error window.SetInputMode(glfw.StickyKeys, 1) gl.ClearColor(0., 0., 0.4, 0.) gl.Enable(gl.DEPTH_TEST) gl.DepthFunc(gl.LESS) vertexArray := gl.GenVertexArray() defer vertexArray.Delete() vertexArray.Bind() prog := helper.MakeProgram("TransformVertexShader.vertexshader", "ColorFragmentShader.fragmentshader") defer prog.Delete() matrixID := prog.GetUniformLocation("MVP") Projection := mgl32.Perspective(45.0, 4.0/3.0, 0.1, 100.0) View := mgl32.LookAt(4.0, 3.0, -3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) Model := mgl32.Ident4() MVP := Projection.Mul4(View).Mul4(Model) // Remember, transform multiplication order is "backwards" vBufferData := [...]float32{ -1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0, -1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, -1.0, 1.0} colorBufferData := [...]float32{ 0.583, 0.771, 0.014, 0.609, 0.115, 0.436, 0.327, 0.483, 0.844, 0.822, 0.569, 0.201, 0.435, 0.602, 0.223, 0.310, 0.747, 0.185, 0.597, 0.770, 0.761, 0.559, 0.436, 0.730, 0.359, 0.583, 0.152, 0.483, 0.596, 0.789, 0.559, 0.861, 0.639, 0.195, 0.548, 0.859, 0.014, 0.184, 0.576, 0.771, 0.328, 0.970, 0.406, 0.615, 0.116, 0.676, 0.977, 0.133, 0.971, 0.572, 0.833, 0.140, 0.616, 0.489, 0.997, 0.513, 0.064, 0.945, 0.719, 0.592, 0.543, 0.021, 0.978, 0.279, 0.317, 0.505, 0.167, 0.620, 0.077, 0.347, 0.857, 0.137, 0.055, 0.953, 0.042, 0.714, 0.505, 0.345, 0.783, 0.290, 0.734, 0.722, 0.645, 0.174, 0.302, 0.455, 0.848, 0.225, 0.587, 0.040, 0.517, 0.713, 0.338, 0.053, 0.959, 0.120, 0.393, 0.621, 0.362, 0.673, 0.211, 0.457, 0.820, 0.883, 0.371, 0.982, 0.099, 0.879} //elBufferData := [...]uint8{0, 1, 2} // Not sure why this is here vertexBuffer := gl.GenBuffer() defer vertexBuffer.Delete() vertexBuffer.Bind(gl.ARRAY_BUFFER) gl.BufferData(gl.ARRAY_BUFFER, len(vBufferData)*4, &vBufferData, gl.STATIC_DRAW) colorBuffer := gl.GenBuffer() defer colorBuffer.Delete() colorBuffer.Bind(gl.ARRAY_BUFFER) gl.BufferData(gl.ARRAY_BUFFER, len(colorBufferData)*4, &colorBufferData, gl.STATIC_DRAW) // Equivalent to a do... while for ok := true; ok; ok = (window.GetKey(glfw.KeyEscape) != glfw.Press && !window.ShouldClose()) { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) prog.Use() matrixID.UniformMatrix4fv(false, MVP) vertexAttrib := gl.AttribLocation(0) vertexAttrib.EnableArray() vertexBuffer.Bind(gl.ARRAY_BUFFER) vertexAttrib.AttribPointer(3, gl.FLOAT, false, 0, nil) colorAttrib := gl.AttribLocation(1) colorAttrib.EnableArray() colorBuffer.Bind(gl.ARRAY_BUFFER) colorAttrib.AttribPointer(3, gl.FLOAT, false, 0, nil) gl.DrawArrays(gl.TRIANGLES, 0, 12*3) vertexAttrib.DisableArray() colorAttrib.DisableArray() window.SwapBuffers() glfw.PollEvents() } }
func main() { runtime.LockOSThread() if !glfw.Init() { fmt.Fprintf(os.Stderr, "Can't open GLFW") return } defer glfw.Terminate() glfw.WindowHint(glfw.Samples, 4) glfw.WindowHint(glfw.ContextVersionMajor, 3) glfw.WindowHint(glfw.ContextVersionMinor, 3) glfw.WindowHint(glfw.OpenglProfile, glfw.OpenglCoreProfile) glfw.WindowHint(glfw.OpenglForwardCompatible, glfw.True) // needed for macs window, err := glfw.CreateWindow(1024, 768, "Tutorial 5", nil, nil) if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) return } window.MakeContextCurrent() gl.Init() gl.GetError() // Ignore error window.SetInputMode(glfw.StickyKeys, 1) gl.ClearColor(0., 0., 0.4, 0.) gl.Enable(gl.DEPTH_TEST) gl.DepthFunc(gl.LESS) vertexArray := gl.GenVertexArray() defer vertexArray.Delete() vertexArray.Bind() prog := helper.MakeProgram("TransformVertexShader.vertexshader", "TextureFragmentShader.fragmentshader") defer prog.Delete() matrixID := prog.GetUniformLocation("MVP") Projection := mgl32.Perspective(45.0, 4.0/3.0, 0.1, 100.0) View := mgl32.LookAt(4.0, 3.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) Model := mgl32.Ident4() MVP := Projection.Mul4(View).Mul4(Model) // Remember, transform multiplication order is "backwards" texture, err := helper.TextureFromDDS("uvtemplate.DDS") if err != nil { fmt.Printf("Couldn't load texture: %v\n", err) return } defer texture.Delete() texSampler := prog.GetUniformLocation("myTextureSampler") vBufferData := [...]float32{ -1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0, -1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, -1.0, 1.0, } uvBufferData := [...]float32{ 0.000059, 1.0 - 0.000004, 0.000103, 1.0 - 0.336048, 0.335973, 1.0 - 0.335903, 1.000023, 1.0 - 0.000013, 0.667979, 1.0 - 0.335851, 0.999958, 1.0 - 0.336064, 0.667979, 1.0 - 0.335851, 0.336024, 1.0 - 0.671877, 0.667969, 1.0 - 0.671889, 1.000023, 1.0 - 0.000013, 0.668104, 1.0 - 0.000013, 0.667979, 1.0 - 0.335851, 0.000059, 1.0 - 0.000004, 0.335973, 1.0 - 0.335903, 0.336098, 1.0 - 0.000071, 0.667979, 1.0 - 0.335851, 0.335973, 1.0 - 0.335903, 0.336024, 1.0 - 0.671877, 1.000004, 1.0 - 0.671847, 0.999958, 1.0 - 0.336064, 0.667979, 1.0 - 0.335851, 0.668104, 1.0 - 0.000013, 0.335973, 1.0 - 0.335903, 0.667979, 1.0 - 0.335851, 0.335973, 1.0 - 0.335903, 0.668104, 1.0 - 0.000013, 0.336098, 1.0 - 0.000071, 0.000103, 1.0 - 0.336048, 0.000004, 1.0 - 0.671870, 0.336024, 1.0 - 0.671877, 0.000103, 1.0 - 0.336048, 0.336024, 1.0 - 0.671877, 0.335973, 1.0 - 0.335903, 0.667969, 1.0 - 0.671889, 1.000004, 1.0 - 0.671847, 0.667979, 1.0 - 0.335851, } // Invert V because we're using a compressed texture for i := 1; i < len(uvBufferData); i += 2 { uvBufferData[i] = 1.0 - uvBufferData[i] } vertexBuffer := gl.GenBuffer() defer vertexBuffer.Delete() vertexBuffer.Bind(gl.ARRAY_BUFFER) gl.BufferData(gl.ARRAY_BUFFER, len(vBufferData)*4, &vBufferData, gl.STATIC_DRAW) uvBuffer := gl.GenBuffer() defer uvBuffer.Delete() uvBuffer.Bind(gl.ARRAY_BUFFER) gl.BufferData(gl.ARRAY_BUFFER, len(uvBufferData)*4, &uvBufferData, gl.STATIC_DRAW) // Equivalent to a do... while for ok := true; ok; ok = (window.GetKey(glfw.KeyEscape) != glfw.Press && !window.ShouldClose()) { func() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) prog.Use() defer gl.ProgramUnuse() matrixID.UniformMatrix4fv(false, MVP) gl.ActiveTexture(gl.TEXTURE0) texture.Bind(gl.TEXTURE_2D) defer texture.Unbind(gl.TEXTURE_2D) texSampler.Uniform1i(0) vertexAttrib := gl.AttribLocation(0) vertexAttrib.EnableArray() defer vertexAttrib.DisableArray() vertexBuffer.Bind(gl.ARRAY_BUFFER) defer vertexBuffer.Unbind(gl.ARRAY_BUFFER) vertexAttrib.AttribPointer(3, gl.FLOAT, false, 0, nil) uvAttrib := gl.AttribLocation(1) uvAttrib.EnableArray() defer uvAttrib.DisableArray() uvBuffer.Bind(gl.ARRAY_BUFFER) defer uvBuffer.Unbind(gl.ARRAY_BUFFER) uvAttrib.AttribPointer(2, gl.FLOAT, false, 0, nil) gl.DrawArrays(gl.TRIANGLES, 0, 12*3) window.SwapBuffers() glfw.PollEvents() }() // Defers unbinds and disables to here, end of the loop } }