// renders the display on each update func render() { gl.Clear(gl.COLOR_BUFFER_BIT) gl.Enable(gl.BLEND) gl.Enable(gl.POINT_SMOOTH) gl.Enable(gl.LINE_SMOOTH) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.LoadIdentity() // draw pipes for _, pipeBox := range pipe { gl.PushMatrix() pos := pipeBox.Body.Position() gl.Translatef(float32(pos.X), float32(pos.Y), 0.0) drawSquare(.19, .8, .19, 1) // limegreen gl.PopMatrix() } // draw flappy for _, flappyBird := range flappyBirds { gl.PushMatrix() pos := flappyBird.Body.Position() gl.Translatef(float32(pos.X), float32(pos.Y), 0.0) drawSquare(1, .84, 0, 1) // gold gl.PopMatrix() } gl.Color4f(1, 0, 1, 1) scoreStr := "[ Score: " + strconv.Itoa(score) + " ]" // draw score drawScore(scoreStr) }
func renderCallback(delta float64) { gl.Viewport(0, 0, int32(app.Width), int32(app.Height)) gl.ClearColor(0.196078, 0.6, 0.8, 1.0) // some pov-ray sky blue gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) // make the projection and view matrixes projection := mgl.Perspective(mgl.DegToRad(60.0), float32(app.Width)/float32(app.Height), 1.0, 200.0) view := app.CameraRotation.Mat4() view = view.Mul4(mgl.Translate3D(-app.CameraPos[0], -app.CameraPos[1], -app.CameraPos[2])) // draw the cube cube.Node.Draw(projection, view) // draw all of the bullets for _, bullet := range bullets { bullet.Node.Draw(projection, view) } // draw the backboard backboard.Node.Draw(projection, view) // draw the ground ground.Draw(projection, view) //time.Sleep(10 * time.Millisecond) }
func Render(g *game.Game, fudge float32) { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) if renderBoard(g, fudge) { renderHUD(g, fudge) } renderMenu(g, fudge) }
// Runs the visualization with a set of DelayedNoteData. The DelayedNote data is // used to push information to the synth as well as represent the data visually. func RunVisualization(notes *synth.NoteArrangement, oNoteChannel chan synth.DelayedNoteData) error { window, assets, err := initialize() if err != nil { return err } defer destroy(window, assets) // The main update loop. ct, lt, dt := 0.0, 0.0, 0.0 for !window.ShouldClose() { // Keeping the current time. lt = ct ct = glfw.GetTime() dt = ct - lt // Real render loop. gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) if config.DebugMode { glErr := gl.GetError() if glErr != gl.NO_ERROR { fmt.Printf("OpenGL error: %d\n", glErr) } } window.SwapBuffers() glfw.PollEvents() if dt < 1/1000.0 { // Delay the thread to keep up w/ updating? } } return nil }
func Run(g *Game) error { if g.Scene == nil { return fmt.Errorf("Scene property of given Game struct is empty") } // GLFW event handling must run on the main OS thread runtime.LockOSThread() if err := glfw.Init(); err != nil { return fmt.Errorf("glfw.Init failed: %s", err) } defer glfw.Terminate() glfw.WindowHint(glfw.ContextVersionMajor, OpenGLVerMinor) glfw.WindowHint(glfw.ContextVersionMinor, OpenGLVerMinor) glfw.WindowHint(glfw.Resizable, glfw.False) if g.Resizable { glfw.WindowHint(glfw.Resizable, glfw.True) } glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile) glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True) window, err := glfw.CreateWindow(int(g.Width), int(g.Height), g.Title, nil, nil) if err != nil { return fmt.Errorf("glfw.CreateWindow failed: %s", err) } window.MakeContextCurrent() // Initialize Glow if err := gl.Init(); err != nil { return fmt.Errorf("gl.Init failed:", err) } version := gl.GoStr(gl.GetString(gl.VERSION)) log.Println("gl.Init successful, OpenGL version:", version) var previousTime, deltaTime, time float32 previousTime = float32(glfw.GetTime()) - 1.0/60.0 g.Scene.Setup() for !window.ShouldClose() { time = float32(glfw.GetTime()) deltaTime = time - previousTime glfw.PollEvents() gl.ClearColor(0.2, 0.3, 0.3, 0.5) gl.Clear(gl.COLOR_BUFFER_BIT) g.Scene.Update(deltaTime) g.Scene.Draw(deltaTime) previousTime = time window.SwapBuffers() } return nil }
func (engine *Engine) render(time int64) { gl.Clear(gl.COLOR_BUFFER_BIT) engine.shader.Bind() engine.mesh.Bind() engine.mesh.SetMVCData(engine.mat) engine.mesh.Draw() engine.window.SwapBuffers() }
//BindForDrawing binds this fbo, change face culling for back face, start using the shadow program, calculate projection and clears the texture. func (sfbo *ShadowFBO) BindForDrawing() { sfbo.framebuffer.Bind(gl2.FRAMEBUFFER) sfbo.program.Use() sfbo.vp = sfbo.projection.Mul4(sfbo.view) gl.Clear(gl.DEPTH_BUFFER_BIT | gl.COLOR_BUFFER_BIT) ViewportChange(sfbo.width, sfbo.height) gl.CullFace(gl.FRONT) }
//Bind binds the FBO and calcualte view-projection. func (gb *GBuffer) Bind(cam *Camera) { gb.framebuffer.Bind(gl2.FRAMEBUFFER) gb.program.Use() gb.vp = cam.Projection.Mul4(cam.View) gb.view = cam.View ViewportChange(gb.width, gb.height) gl.Clear(gl2.COLOR_BUFFER_BIT | gl2.DEPTH_BUFFER_BIT | gl2.STENCIL_BUFFER_BIT) }
func (r *EffectsRenderer) Bind() error { gl.BindFramebuffer(gl.FRAMEBUFFER, r.framebuffer) gl.Enable(gl.STENCIL_TEST) gl.Viewport(0, 0, int32(r.width), int32(r.height)) gl.ClearStencil(0) gl.ClearColor(0.0, 0.0, 0.0, 0.0) gl.StencilMask(0xFF) // Write to buffer gl.Clear(gl.COLOR_BUFFER_BIT | gl.STENCIL_BUFFER_BIT) gl.StencilMask(0x00) // Don't write to buffer return nil }
// Clear clears the screen using the color provided. func (c Context) Clear(color Color) { gl.ClearColor(color.R, color.G, color.B, 1.0) gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) }
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() } }
func main() { // init glfw if err := glfw.Init(); err != nil { panic(err) } defer glfw.Terminate() glfw.WindowHint(glfw.Resizable, glfw.False) glfw.WindowHint(glfw.ContextVersionMajor, 4) glfw.WindowHint(glfw.ContextVersionMinor, 1) glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile) glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True) // make an application window window, err := glfw.CreateWindow(windowWidth, windowHeight, "Hello", nil, nil) if err != nil { panic(err) } window.MakeContextCurrent() // init gl if err := gl.Init(); err != nil { panic(err) } fmt.Println("OpenGL version", gl.GoStr(gl.GetString(gl.VERSION))) // create vertex & fragment shader program, err := newProgram(vertexShader, fragmentShader) if err != nil { panic(err) } gl.UseProgram(program) gl.BindFragDataLocation(program, 0, gl.Str("fc\x00")) points := []float32{ -0.5, -0.5, 0.5, 0.5, 0.5, -0.5, -0.5, 0.5, } vertices := []uint32{ 0, 2, 1, 3, } // configure the vertex data var vao uint32 gl.GenVertexArrays(1, &vao) gl.BindVertexArray(vao) defer gl.BindVertexArray(0) var vbo uint32 gl.GenBuffers(1, &vbo) gl.BindBuffer(gl.ARRAY_BUFFER, vbo) gl.BufferData(gl.ARRAY_BUFFER, len(points)*4, gl.Ptr(points), gl.STATIC_DRAW) var ibo uint32 gl.GenBuffers(1, &ibo) gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, ibo) gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, len(vertices)*4, gl.Ptr(vertices), gl.STATIC_DRAW) vertAttrib := uint32(gl.GetAttribLocation(program, gl.Str("pv\x00"))) gl.EnableVertexAttribArray(vertAttrib) gl.VertexAttribPointer(vertAttrib, 2, gl.FLOAT, false, 2*4, gl.PtrOffset(0)) // global settings gl.Enable(gl.DEPTH_TEST) gl.DepthFunc(gl.LESS) gl.ClearColor(1.0, 1.0, 1.0, 1.0) for !window.ShouldClose() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.UseProgram(program) gl.BindVertexArray(vao) gl.DrawElements(gl.LINE_LOOP, 4, gl.UNSIGNED_INT, gl.PtrOffset(0)) window.SwapBuffers() glfw.PollEvents() } }
// Clear instructs opengl to clear the background to a certain color func Clear(r, g, b, a float32) { gl.ClearColor(1.0, 1.0, 1.0, 1.0) gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) }
// Testing stuff to do with opening an OpenGL context. func Testing() error { window, assets, err := initialize() if err != nil { return nil } defer destroy(window, assets) shaderProgram, _ := assets.GetProgram("res/shaders/texrenderer") texture, _ := assets.GetTexture("res/textures/texture.jpg") renderObject := CreateRenderObject(shaderProgram, texture, []float32{ -1.0, -1.0, 0.0, 0.0, 1.0, -1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 0.0, 1.0, }) defer renderObject.Destroy() // Testing a LineRender. lineShader, err := LoadShaderProgram("res/shaders/lineshader") if err != nil { fmt.Println("LSP: " + err.Error()) } defer DestroyShaderProgram(lineShader) lineRenders := []*LineRender{ NewLineRender(lineShader, GetPastel(1), true, 3.0, []Point{ Point{-1.0, 0}, Point{1.0, 0}, }), NewLineRender(lineShader, GetPastel(2), true, 8.0, []Point{ Point{-0.5, -0.5}, Point{0.5, 0.5}, Point{1, 0}, }), } defer func() { for _, lr := range lineRenders { lr.Destroy() } }() // Creating a 2nd LineRender from DefaultGenerateSinePoints. var phase float32 = 0 lineRender2 := NewLineRender( lineShader, GetPastel(0), false, 4.0, DefaultGenerateSinePoints(440, phase)) defer lineRender2.Destroy() gl.ClearColor(1.0, 1.0, 1.0, 1.0) for !window.ShouldClose() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) // Rendering the outputs. renderObject.Render() for _, lr := range lineRenders { lr.Render() } lineRender2.Render() // Updating render for lineRender2. phase += 2 * math.Pi * (440.0 / 44100.0) if phase > 2*math.Pi { phase -= 2 * math.Pi } lineRender2.UpdatePoints(DefaultGenerateSinePoints(440, phase)) if config.DebugMode { // Reporting OpenGL errors. glErr := gl.GetError() if glErr != gl.NO_ERROR { fmt.Printf("OpenGL error: %d\n", glErr) } } window.SwapBuffers() glfw.PollEvents() time.Sleep(10 * time.Millisecond) } return nil }
func (a *Application) Draw() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) }
func (c *Context) Clear() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) }
func main() { // init glfw if err := glfw.Init(); err != nil { panic(err) } defer glfw.Terminate() glfw.WindowHint(glfw.Resizable, glfw.False) glfw.WindowHint(glfw.ContextVersionMajor, 4) glfw.WindowHint(glfw.ContextVersionMinor, 1) glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile) glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True) // make an application window window, err := glfw.CreateWindow(windowWidth, windowHeight, "Transform", nil, nil) if err != nil { panic(err) } window.MakeContextCurrent() // init gl if err := gl.Init(); err != nil { panic(err) } fmt.Println("OpenGL version", gl.GoStr(gl.GetString(gl.VERSION))) // create vertex & fragment shader program, err := newProgram(vertexShader, fragmentShader) if err != nil { panic(err) } gl.UseProgram(program) projection := mgl32.Perspective(mgl32.DegToRad(45.0), float32(windowWidth)/windowHeight, 0.1, 10.0) projectionUniform := gl.GetUniformLocation(program, gl.Str("projection\x00")) gl.UniformMatrix4fv(projectionUniform, 1, false, &projection[0]) camera := mgl32.LookAtV( mgl32.Vec3{3, 3, 3}, mgl32.Vec3{0, 0, 0}, mgl32.Vec3{0, 1, 0}, ) cameraUniform := gl.GetUniformLocation(program, gl.Str("camera\x00")) gl.UniformMatrix4fv(cameraUniform, 1, false, &camera[0]) model := mgl32.Ident4() modelUniform := gl.GetUniformLocation(program, gl.Str("model\x00")) gl.UniformMatrix4fv(modelUniform, 1, false, &model[0]) gl.BindFragDataLocation(program, 0, gl.Str("vert\x00")) points := []float32{ -0.9, -0.9, -0.9, 0.9, -0.9, -0.9, 0.9, -0.9, 0.9, -0.9, -0.9, 0.9, -0.9, 0.9, -0.9, 0.9, 0.9, -0.9, 0.9, 0.9, 0.9, -0.9, 0.9, 0.9, } vertices := []uint32{ 0, 1, 1, 2, 2, 3, 3, 0, 0, 4, 1, 5, 2, 6, 3, 7, 4, 5, 5, 6, 6, 7, 7, 4, } // configure the vertex data var vao uint32 gl.GenVertexArrays(1, &vao) gl.BindVertexArray(vao) defer gl.BindVertexArray(0) var vbo uint32 gl.GenBuffers(1, &vbo) gl.BindBuffer(gl.ARRAY_BUFFER, vbo) gl.BufferData(gl.ARRAY_BUFFER, len(points)*4, gl.Ptr(points), gl.STATIC_DRAW) var ibo uint32 gl.GenBuffers(1, &ibo) gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, ibo) gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, len(vertices)*4, gl.Ptr(vertices), gl.STATIC_DRAW) vertAttrib := uint32(gl.GetAttribLocation(program, gl.Str("vert\x00"))) gl.EnableVertexAttribArray(vertAttrib) gl.VertexAttribPointer(vertAttrib, 3, gl.FLOAT, false, 3*4, gl.PtrOffset(0)) // global settings gl.Enable(gl.DEPTH_TEST) gl.DepthFunc(gl.LESS) gl.ClearColor(0.0, 0.0, 0.0, 1.0) angleX := 0.0 angleY := 0.0 angleZ := 0.0 previousTime := glfw.GetTime() for !window.ShouldClose() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) time := glfw.GetTime() elapsed := time - previousTime previousTime = time angleX += math.Sin((elapsed / period) * math.Pi * 2.0) angleY += math.Sin((elapsed / period) / 6.0 * math.Pi * 2.0) angleZ += math.Sin((elapsed / period) / 3.0 * math.Pi * 2.0) model = mgl32.HomogRotate3DY(float32(angleY)).Mul4(mgl32.HomogRotate3DX(float32(angleX))).Mul4(mgl32.HomogRotate3DZ(float32(angleZ))) gl.UseProgram(program) gl.UniformMatrix4fv(modelUniform, 1, false, &model[0]) gl.BindVertexArray(vao) gl.DrawElements(gl.LINES, int32(len(vertices)), gl.UNSIGNED_INT, gl.PtrOffset(0)) window.SwapBuffers() glfw.PollEvents() } }
func Clear() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) Check() }
func draw() { gl.Clear(gl.COLOR_BUFFER_BIT) }
func (a *Application) Draw() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) a.layers.Render() }
// DoRender does the graphical rendering of a single frame. func (gr *GameRenderer) DoRender(delta float32) { // detect any changes to size of the window tempW, tempH := gr.MainWindow.GetFramebufferSize() currentWidth, currentHeight := int32(tempW), int32(tempH) width, height := gr.Renderer.GetResolution() if width == 0 || height == 0 { groggy.Logsf("ERROR", "GameRenderer.DoRender() window size w/h: %d / %d", width, height) } if width != currentWidth || height != currentHeight { width, height = currentWidth, currentHeight gr.Renderer.ChangeResolution(width, height) } // clear the screen gl.Viewport(0, 0, int32(width), int32(height)) gl.ClearColor(0.05, 0.05, 0.05, 1.0) gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) // make the projection and view matrixes perspective := mgl.Perspective(mgl.DegToRad(60.0), float32(width)/float32(height), 1.0, 400.0) view := gr.camera.GetViewMatrix() // Get the landscape chunks to render -- which for now is everything landMan := gameManager.GetLandscapeManager() allChunks := landMan.GetAllChunks() // draw everything you crazy devil for _, c := range allChunks { node := c.GetTheRenderable() if node.Core.Shader == nil { node.Core.Shader = gr.Shaders["landscape"] } gr.Renderer.DrawRenderable(node, customLandscapeBinder, perspective, view) } // get the entity manager and all the entities entMan := gameManager.GetEntityManager() allEnts := entMan.GetAllEntities() // draw everything, because we're crazy for _, e := range allEnts { // make sure we have something to draw if e.Renderable == nil { // try to clone a new renderable from the asset prototypes proto, okay := loadedAssets[e.AssetPath] if okay { e.Renderable = proto.Clone() } } // sync the loc/rot to the renderable e.SyncToRenderable() // did we sort things out and make a renderable? if e.Renderable != nil { gr.Renderer.DrawRenderable(e.Renderable, nil, perspective, view) } } // draw the screen gr.Renderer.EndRenderFrame() }