func (r *GlowRenderer) Draw() (err error) { gl.UseProgram(r.shader) gl.Uniform1i(r.textureUnitLoc, 0) gl.BindBuffer(gl.ARRAY_BUFFER, r.coords) gl.EnableVertexAttribArray(r.positionLoc) gl.VertexAttribPointer(r.positionLoc, 3, gl.FLOAT, false, 5*4, gl.PtrOffset(0)) gl.EnableVertexAttribArray(r.textureLoc) gl.VertexAttribPointer(r.textureLoc, 2, gl.FLOAT, false, 5*4, gl.PtrOffset(3*4)) gl.Uniform1i(r.blurAmountLoc, int32(r.blur)) gl.Uniform1f(r.blurScaleLoc, r.scale) gl.Uniform1f(r.blurStrengthLoc, r.strength) gl.Uniform2f(r.bufferDimensionsLoc, float32(r.width), float32(r.height)) gl.BindFramebuffer(gl.FRAMEBUFFER, r.BlurFb) gl.Viewport(0, 0, int32(r.width), int32(r.height)) gl.ActiveTexture(gl.TEXTURE0) gl.BindTexture(gl.TEXTURE_2D, r.GlowTex) gl.Uniform1i(r.orientationLoc, 0) gl.DrawArrays(gl.TRIANGLE_STRIP, 0, 4) gl.BindFramebuffer(gl.FRAMEBUFFER, 0) gl.Viewport(0, 0, int32(r.oldwidth), int32(r.oldheight)) gl.BlendFunc(gl.ONE, gl.ONE) gl.BindTexture(gl.TEXTURE_2D, r.BlurTex) gl.Uniform1i(r.orientationLoc, 1) gl.DrawArrays(gl.TRIANGLE_STRIP, 0, 4) gl.BindBuffer(gl.ARRAY_BUFFER, 0) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) return nil }
func main() { width := 1024 height := 768 title := "Simple Triangle" if err := glfw.Init(); err != nil { log.Fatalln("failed to initialize glfw:", err) } defer glfw.Terminate() glfw.WindowHint(glfw.Samples, 4) // 4x antialiasing glfw.WindowHint(glfw.ContextVersionMajor, 3) // We want OpenGL 3.3 glfw.WindowHint(glfw.ContextVersionMinor, 3) glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True) // To make MacOS happy; should not be needed glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile) //We don't want the old OpenGL window, err := glfw.CreateWindow(width, height, title, nil, nil) if err != nil { panic(err) } window.MakeContextCurrent() if err := gl.Init(); err != nil { panic(err) } window.SetKeyCallback(keypress) window.SetMouseButtonCallback(mousepress) gl.Viewport(0, 0, 1024, 768) for !window.ShouldClose() { draw() window.SwapBuffers() glfw.PollEvents() } }
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 (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 }
func main() { err := glfw.Init() if err != nil { panic(err) } defer glfw.Terminate() glfw.WindowHint(glfw.Resizable, glfw.True) glfw.WindowHint(glfw.ContextVersionMajor, 3) glfw.WindowHint(glfw.ContextVersionMinor, 3) window, err := glfw.CreateWindow(640, 480, "Testing", nil, nil) if err != nil { panic(err) } defer window.Destroy() window.MakeContextCurrent() glfw.SwapInterval(1) window.SetSizeCallback(func(w *glfw.Window, width int, height int) { fmt.Printf("resize: %d, %d\n", width, height) gl.Viewport(0, 0, int32(width), int32(height)) }) window.SetCursorPosCallback(func(w *glfw.Window, xpos float64, ypos float64) { fmt.Printf("mouse: %f, %f\n", xpos, ypos) }) window.SetFocusCallback(func(w *glfw.Window, focused bool) { fmt.Printf("focus: %t\n", focused) }) window.SetKeyCallback(func(w *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) { if key == glfw.KeyEscape { window.SetShouldClose(true) } fmt.Printf("key\n") }) window.SetMouseButtonCallback(func(w *glfw.Window, button glfw.MouseButton, action glfw.Action, mod glfw.ModifierKey) { fmt.Printf("mouse\n") }) window.SetCharCallback(func(w *glfw.Window, char rune) { fmt.Printf("char: '%c'\n", char) }) if err := gl.Init(); err != nil { panic(err) } for !window.ShouldClose() { draw() window.SwapBuffers() glfw.PollEvents() } }
func initOpenGl(window *glfw.Window, w, h int) { w, h = window.GetSize() // query window to get screen pixels width, height := window.GetFramebufferSize() gl.Viewport(0, 0, width, height) gl.MatrixMode(gl.PROJECTION) gl.LoadIdentity() gl.Ortho(0, float64(w), 0, float64(h), -1, 1) gl.MatrixMode(gl.MODELVIEW) gl.LoadIdentity() gl.ClearColor(.25, .88, .83, 1) // turquoise }
func ResizeViewport() float32 { w, h := window.GetFramebufferSize() if w != width || h != height { width = w height = h gl.Viewport(0, 0, int32(width), int32(height)) tlog.Println("buffer size: ", w, h) ratio := float32(width) / float32(height) return ratio } return 0 }
func (r *EffectsRenderer) Draw() (err error) { gl.UseProgram(r.shader) gl.Uniform1i(r.textureUnitLoc, 0) gl.Uniform3f(r.colorLoc, r.Color[0], r.Color[1], r.Color[2]) gl.BindBuffer(gl.ARRAY_BUFFER, r.coords) gl.EnableVertexAttribArray(r.positionLoc) gl.VertexAttribPointer(r.positionLoc, 3, gl.FLOAT, false, 5*4, gl.PtrOffset(0)) gl.EnableVertexAttribArray(r.textureLoc) gl.VertexAttribPointer(r.textureLoc, 2, gl.FLOAT, false, 5*4, gl.PtrOffset(3*4)) gl.BindFramebuffer(gl.FRAMEBUFFER, 0) gl.Viewport(0, 0, int32(r.oldwidth), int32(r.oldheight)) gl.BlendFunc(gl.ONE, gl.ONE) gl.BindTexture(gl.TEXTURE_2D, r.texture) gl.DrawArrays(gl.TRIANGLE_STRIP, 0, 4) gl.BindBuffer(gl.ARRAY_BUFFER, 0) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) return nil }
func main() { //initializing glfw and OpenGL err := glfw.Init() if err != nil { panic(err) } defer glfw.Terminate() window, err := glfw.CreateWindow(graphics.WIDTH, graphics.HEIGHT, "Testing", nil, nil) if err != nil { panic(err) } window.MakeContextCurrent() if err := gl.Init(); err != nil { panic(err) } gl.Viewport(0, 0, graphics.WIDTH, graphics.HEIGHT) gl.ClearColor(0.0, 0.0, 0.0, 1.0) //game.Loop(window) game.EngineLoop(window) }
func (r *EffectsRenderer) Unbind() error { gl.Viewport(0, 0, int32(r.oldwidth), int32(r.oldheight)) gl.BindFramebuffer(gl.FRAMEBUFFER, 0) gl.Disable(gl.STENCIL_TEST) return nil }
func SetViewPort(width, height int32) { // set viewport for window gl.Viewport(0, 0, int32(width), int32(height)) }
func Init() error { if err := gl.Init(); err != nil { return err } log.Printf("OpenGL version: %s", gl.GoStr(gl.GetString(gl.VERSION))) vs, err := asset.String("shader.vert") if err != nil { return err } fs, err := asset.String("shader.frag") if err != nil { return err } program, err := createProgram(vs, fs) if err != nil { return err } gl.UseProgram(program) var shaderErr error uniform := func(name string) int32 { var loc int32 loc, shaderErr = getUniformLocation(program, name) return loc } projectionViewMatrixUniform = uniform("u_projectionViewMatrix") modelMatrixUniform = uniform("u_modelMatrix") normalMatrixUniform = uniform("u_normalMatrix") ambientLightColorUniform = uniform("u_ambientLightColor") directionalLightColorUniform = uniform("u_directionalLightColor") directionalVectorUniform = uniform("u_directionalVector") textureUniform = uniform("u_texture") grayscaleUniform = uniform("u_grayscale") brightnessUniform = uniform("u_brightness") alphaUniform = uniform("u_alpha") mixColorUniform = uniform("u_mixColor") mixAmountUniform = uniform("u_mixAmount") if shaderErr != nil { return shaderErr } vm := newViewMatrix(cameraPosition, targetPosition, up) nm := vm.inverse().transpose() gl.UniformMatrix4fv(normalMatrixUniform, 1, false, &nm[0]) gl.Uniform3fv(ambientLightColorUniform, 1, &ambientLightColor[0]) gl.Uniform3fv(directionalLightColorUniform, 1, &directionalLightColor[0]) gl.Uniform3fv(directionalVectorUniform, 1, &directionalVector[0]) SizeCallback = func(width, height int) { if winWidth == width && winHeight == height { return } log.Printf("window size changed (%dx%d -> %dx%d)", int(winWidth), int(winHeight), width, height) gl.Viewport(0, 0, int32(width), int32(height)) // Calculate new perspective projection view matrix. winWidth, winHeight = width, height fw, fh := float32(width), float32(height) aspect := fw / fh fovRadians := float32(math.Pi) / 3 perspectiveProjectionViewMatrix = vm.mult(newPerspectiveMatrix(fovRadians, aspect, 1, 2000)) // Calculate new ortho projection view matrix. orthoProjectionViewMatrix = newOrthoMatrix(fw, fh, fw /* use width as depth */) } if err := initMeshes(); err != nil { return err } if err := initTextures(); err != nil { return err } gl.Enable(gl.CULL_FACE) gl.CullFace(gl.BACK) gl.Enable(gl.DEPTH_TEST) gl.DepthFunc(gl.LESS) gl.Enable(gl.BLEND) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.ClearColor(0, 0, 0, 0) return nil }
// 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() }