// Render renders the model func (m *Model) Render() { gl.UseProgram(m.currentProgram) gl.UniformMatrix4fv(m.modelUniform, 1, false, &m.model[0]) gl.BindVertexArray(m.vao) gl.ActiveTexture(gl.TEXTURE0) texture, isLoaded := m.textures.GetTexture(m.data.TextureFile) if isLoaded { gl.BindTexture(gl.TEXTURE_2D, texture) } else { if m.data.TextureFile != "" { go fmt.Printf("Unable to load texture %s", m.data.TextureFile) } } if m.data.Indexed { gl.DrawElements(gl.TRIANGLE_FAN, int32(len(m.data.Indices)), gl.UNSIGNED_INT, gl.PtrOffset(0)) } else { gl.DrawArrays(gl.TRIANGLES, 0, int32(len(m.data.Verts))/m.data.VertSize) } gl.BindVertexArray(0) }
func programLoop(window *glfw.Window) { // the linked shader program determines how the data will be rendered shaders := compileShaders() shaderProgram := linkShaders(shaders) // VAO contains all the information about the data to be rendered VAO := createTriangleVAO() for !window.ShouldClose() { // poll events and call their registered callbacks glfw.PollEvents() // perform rendering gl.ClearColor(0.2, 0.5, 0.5, 1.0) gl.Clear(gl.COLOR_BUFFER_BIT) // draw loop gl.UseProgram(shaderProgram) // ensure the right shader program is being used gl.BindVertexArray(VAO) // bind data gl.DrawArrays(gl.TRIANGLES, 0, 3) // perform draw call gl.BindVertexArray(0) // unbind data (so we don't mistakenly use/modify it) // end of draw loop // swap in the rendered buffer window.SwapBuffers() } }
func (self *Application) setup() error { gl.GenVertexArrays(1, &self.Triangles) gl.BindVertexArray(self.Triangles) trianglesVertices := []float32{ -0.9, -0.9, 0.85, -0.9, -0.9, 0.85, 0.9, -0.85, 0.9, 0.9, -0.85, 0.9, } gl.GenBuffers(1, &self.ArrayBuffer) gl.BindBuffer(gl.ARRAY_BUFFER, self.ArrayBuffer) gl.BufferData(gl.ARRAY_BUFFER, len(trianglesVertices)*4, gl.Ptr(trianglesVertices), gl.STATIC_DRAW) program, err := util.LoadShaders([]util.ShaderInfo{ util.ShaderInfo{gl.VERTEX_SHADER, "triangles.vert"}, util.ShaderInfo{gl.FRAGMENT_SHADER, "triangles.frag"}, }) if err != nil { return err } gl.UseProgram(program) gl.VertexAttribPointer(vPosition, 2, gl.FLOAT, false, 0, gl.Ptr(nil)) gl.EnableVertexAttribArray(vPosition) return nil }
// New creates a new shader object. The shader can later be activated by the given name with the Use function func New(name, vertexShaderSource, fragmentShaderSource string) error { if _, found := shaderList[name]; found { return fmt.Errorf("Shader already exists: %v", name) } program, err := newProgram(vertexShaderSource, fragmentShaderSource) if err != nil { return err } shader := Shader{} shader.id = program shader.loaded = true shader.vert = vertexShaderSource shader.frag = fragmentShaderSource gl.UseProgram(shader.id) shader.Projection = gl.GetUniformLocation(program, gl.Str("projection\x00")) shader.Camera = gl.GetUniformLocation(program, gl.Str("camera\x00")) shader.Model = gl.GetUniformLocation(program, gl.Str("model\x00")) shaderList[name] = shader if activeShader := GetActive(); activeShader != nil { activeShader.Activate() } return nil }
func (glRenderer *OpenglRenderer) enableShader() { if glRenderer.shader == glRenderer.activeShader { return } glRenderer.activeShader = glRenderer.shader gl.UseProgram(glRenderer.activeShader.Program) }
func (me *GlObj) Draw(c *Core) { gl.UseProgram(c.Progs[me.Prog]) gl.BindVertexArray(c.Vao[me.Vao]) // shoud check if texture has been set for current object gl.ActiveTexture(gl.TEXTURE0) gl.BindTexture(gl.TEXTURE_2D, c.Textures[me.Texture]) gl.DrawArrays(gl.TRIANGLES, 0, me.nbTriangles) }
// Load loads and sets up the model func (m *Model) Load(fileName string) { m.loadFile(fileName) shader := sm.Shader{VertSrcFile: m.data.VertShaderFile, FragSrcFile: m.data.FragShaderFile, Name: fmt.Sprintf("%s:%s", m.data.VertShaderFile, m.data.FragShaderFile)} program, err := m.shaders.LoadProgram(shader, false) if err != nil { return } m.currentProgram = program gl.UseProgram(m.currentProgram) m.projection = mgl32.Perspective(mgl32.DegToRad(45.0), float32(windowWidth)/windowHeight, 0.1, 10.0) m.projectionUniform = gl.GetUniformLocation(m.currentProgram, gl.Str("projection\x00")) gl.UniformMatrix4fv(m.projectionUniform, 1, false, &m.projection[0]) m.camera = mgl32.LookAtV(mgl32.Vec3{3, 3, 3}, mgl32.Vec3{0, 0, 0}, mgl32.Vec3{0, 1, 0}) m.cameraUniform = gl.GetUniformLocation(m.currentProgram, gl.Str("camera\x00")) gl.UniformMatrix4fv(m.cameraUniform, 1, false, &m.camera[0]) m.modelUniform = gl.GetUniformLocation(m.currentProgram, gl.Str("model\x00")) gl.UniformMatrix4fv(m.modelUniform, 1, false, &m.model[0]) m.textureUniform = gl.GetUniformLocation(m.currentProgram, gl.Str("tex\x00")) gl.Uniform1i(m.textureUniform, 0) gl.BindFragDataLocation(m.currentProgram, 0, gl.Str("outputColor\x00")) // Load the texture m.textures.LoadTexture(m.data.TextureFile, m.data.TextureFile) // Configure the vertex data gl.GenVertexArrays(1, &m.vao) gl.BindVertexArray(m.vao) var vbo uint32 gl.GenBuffers(1, &vbo) gl.BindBuffer(gl.ARRAY_BUFFER, vbo) gl.BufferData(gl.ARRAY_BUFFER, len(m.data.Verts)*4, gl.Ptr(m.data.Verts), gl.STATIC_DRAW) vertAttrib := uint32(gl.GetAttribLocation(m.currentProgram, gl.Str("vert\x00"))) gl.EnableVertexAttribArray(vertAttrib) gl.VertexAttribPointer(vertAttrib, 3, gl.FLOAT, false, m.data.VertSize*4, gl.PtrOffset(0)) // 4:number of bytes in a float32 texCoordAttrib := uint32(gl.GetAttribLocation(m.currentProgram, gl.Str("vertTexCoord\x00"))) gl.EnableVertexAttribArray(texCoordAttrib) gl.VertexAttribPointer(texCoordAttrib, 2, gl.FLOAT, true, m.data.VertSize*4, gl.PtrOffset(3*4)) // 4:number of bytes in a float32 if m.data.Indexed { var indices uint32 gl.GenBuffers(1, &indices) gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, indices) gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, len(m.data.Indices)*4, gl.Ptr(m.data.Indices), gl.STATIC_DRAW) } gl.BindVertexArray(0) }
// Activate will draw everything with this program and load it if it isnt loaded. func (shader *Shader) Activate() { if !shader.loaded { program, err := newProgram(shader.vert, shader.frag) if err != nil { panic(err) } shader.id = program shader.loaded = true gl.UseProgram(shader.id) shader.Projection = gl.GetUniformLocation(program, gl.Str("projection\x00")) shader.Camera = gl.GetUniformLocation(program, gl.Str("camera\x00")) shader.Model = gl.GetUniformLocation(program, gl.Str("model\x00")) } state.shader = shader gl.UseProgram(shader.id) }
func (me *GlObj) Draw(c *Core) { gl.UseProgram(c.Progs[me.Prog]) gl.BindVertexArray(c.Vao[me.Vao]) // shoud check if texture has been set for current object // no need to be set again, if only one texture has been loaded // already binded/actived in new texture gl.ActiveTexture(gl.TEXTURE0) gl.BindTexture(gl.TEXTURE_2D, c.Textures[me.Texture]) gl.DrawArrays(gl.TRIANGLES, 0, me.nbTriangles) }
func drawLoop(win *glfw.Window, vao uint32, shader uint32) { for !win.ShouldClose() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.BindVertexArray(vao) gl.UseProgram(shader) gl.DrawArrays(gl.TRIANGLES, 0, 3) glfw.PollEvents() win.SwapBuffers() } }
func (glRenderer *OpenglRenderer) renderPostEffect(pe postEffect) { gl.UseProgram(pe.program) gl.ActiveTexture(gl.TEXTURE0) gl.BindTexture(gl.TEXTURE_2D, pe.textureId) gl.Disable(gl.CULL_FACE) gl.BindBuffer(gl.ARRAY_BUFFER, glRenderer.postEffectVbo) setupUniforms(pe.shader) vertAttrib := uint32(gl.GetAttribLocation(pe.program, gl.Str("vert\x00"))) gl.EnableVertexAttribArray(vertAttrib) gl.VertexAttribPointer(vertAttrib, 2, gl.FLOAT, false, 4*4, gl.PtrOffset(0)) texCoordAttrib := uint32(gl.GetAttribLocation(pe.program, gl.Str("vertTexCoord\x00"))) gl.EnableVertexAttribArray(texCoordAttrib) gl.VertexAttribPointer(texCoordAttrib, 2, gl.FLOAT, false, 4*4, gl.PtrOffset(2*4)) gl.DrawArrays(gl.TRIANGLE_STRIP, 0, 4) gl.DisableVertexAttribArray(texCoordAttrib) }
func (glRenderer *OpenglRenderer) CreatePostEffect(shader *renderer.Shader) { //Create program glRenderer.CreateShader(shader) gl.UseProgram(shader.Program) //Create Texture var fbo_texture uint32 gl.ActiveTexture(gl.TEXTURE0) gl.GenTextures(1, &fbo_texture) gl.BindTexture(gl.TEXTURE_2D, fbo_texture) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE) gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(glRenderer.WindowWidth), int32(glRenderer.WindowHeight), 0, gl.RGBA, gl.UNSIGNED_BYTE, nil) //Create depth buffer var dbo uint32 gl.GenRenderbuffers(1, &dbo) gl.BindRenderbuffer(gl.RENDERBUFFER, dbo) gl.RenderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, int32(glRenderer.WindowWidth), int32(glRenderer.WindowHeight)) gl.BindRenderbuffer(gl.RENDERBUFFER, 0) //Create frame buffer var fbo uint32 gl.GenFramebuffers(1, &fbo) gl.BindFramebuffer(gl.FRAMEBUFFER, fbo) gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, fbo_texture, 0) gl.FramebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, dbo) //add new postEffect to the queue newPe := postEffect{ program: shader.Program, textureId: fbo_texture, dboId: dbo, fboId: fbo, shader: shader, } glRenderer.postEffects = append(glRenderer.postEffects, newPe) }
func (glRenderer *OpenglRenderer) CreateShader(shader *renderer.Shader) { if shader.Loaded { return } var shaders []uint32 if len(shader.VertSrc) > 0 { if s, err := compileShader(shader.VertSrc+"\x00", gl.VERTEX_SHADER); err == nil { shaders = append(shaders, s) } else { fmt.Println("Error Compiling Vert Shader: ", err) } } if len(shader.FragSrc) > 0 { if s, err := compileShader(shader.FragSrc+"\x00", gl.FRAGMENT_SHADER); err == nil { shaders = append(shaders, s) } else { fmt.Println("Error Compiling Frag Shader: ", err) } } if len(shader.GeoSrc) > 0 { if s, err := compileShader(shader.GeoSrc+"\x00", gl.GEOMETRY_SHADER); err == nil { shaders = append(shaders, s) } else { fmt.Println("Error Compiling Geo Shader: ", err) } } program, err := newProgram(shaders...) if err != nil { fmt.Println("Error Creating Shader Program: ", err) } shader.Program = program shader.Loaded = true gl.UseProgram(program) gl.BindFragDataLocation(program, 0, gl.Str(fmt.Sprintf("%v\x00", shader.FragDataLocation))) }
func (program *GLSLProgram) UnUse() { gl.UseProgram(0) for i := uint32(0); i < program.numAttributes; i++ { gl.DisableVertexAttribArray(i) } }
func (p *Program) UnuseProgram() { gl.UseProgram(0) }
func (p *Program) UseProgram() { gl.UseProgram(p.prog) }
func main() { if err := glfw.Init(); err != nil { log.Fatalln("failed to initialize glfw:", 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) window, err := glfw.CreateWindow(windowWidth, windowHeight, "Cube", nil, nil) if err != nil { panic(err) } window.MakeContextCurrent() // Initialize Glow if err := gl.Init(); err != nil { panic(err) } version := gl.GoStr(gl.GetString(gl.VERSION)) fmt.Println("OpenGL version", version) // Configure the vertex and fragment shaders 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]) textureUniform := gl.GetUniformLocation(program, gl.Str("tex\x00")) gl.Uniform1i(textureUniform, 0) gl.BindFragDataLocation(program, 0, gl.Str("outputColor\x00")) // Load the texture texture, err := newTexture("square.png") if err != nil { panic(err) } // Configure the vertex data var vao uint32 gl.GenVertexArrays(1, &vao) gl.BindVertexArray(vao) var vbo uint32 gl.GenBuffers(1, &vbo) gl.BindBuffer(gl.ARRAY_BUFFER, vbo) gl.BufferData(gl.ARRAY_BUFFER, len(cubeVertices)*4, gl.Ptr(cubeVertices), gl.STATIC_DRAW) vertAttrib := uint32(gl.GetAttribLocation(program, gl.Str("vert\x00"))) gl.EnableVertexAttribArray(vertAttrib) gl.VertexAttribPointer(vertAttrib, 3, gl.FLOAT, false, 5*4, gl.PtrOffset(0)) texCoordAttrib := uint32(gl.GetAttribLocation(program, gl.Str("vertTexCoord\x00"))) gl.EnableVertexAttribArray(texCoordAttrib) gl.VertexAttribPointer(texCoordAttrib, 2, gl.FLOAT, false, 5*4, gl.PtrOffset(3*4)) // Configure global settings gl.Enable(gl.DEPTH_TEST) gl.DepthFunc(gl.LESS) gl.ClearColor(1.0, 1.0, 1.0, 1.0) angle := 0.0 previousTime := glfw.GetTime() for !window.ShouldClose() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) // Update time := glfw.GetTime() elapsed := time - previousTime previousTime = time angle += elapsed model = mgl32.HomogRotate3D(float32(angle), mgl32.Vec3{0, 1, 0}) // Render gl.UseProgram(program) gl.UniformMatrix4fv(modelUniform, 1, false, &model[0]) gl.BindVertexArray(vao) gl.ActiveTexture(gl.TEXTURE0) gl.BindTexture(gl.TEXTURE_2D, texture) gl.DrawArrays(gl.TRIANGLES, 0, 6*2*3) // Maintenance window.SwapBuffers() glfw.PollEvents() } }
func (p *GLSLProgram) Use() { gl.UseProgram(p.program) }
func (prog *Program) Use() { gl.UseProgram(prog.handle) }
func Main() { err := glfw.Init() if err != nil { panic(err) } defer glfw.Terminate() glfw.WindowHint(glfw.Resizable, glfw.False) glfw.WindowHint(glfw.ContextVersionMajor, 3) glfw.WindowHint(glfw.ContextVersionMinor, 2) glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile) glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True) window, err := glfw.CreateWindow(WindowWidth, WindowHeight, "Cube", nil, nil) Window = window if err != nil { panic(err) } window.MakeContextCurrent() // Initialize Glow if err := gl.Init(); err != nil { panic(err) } version := gl.GoStr(gl.GetString(gl.VERSION)) fmt.Println("OpenGL version", version) // Configure the vertex and fragment shaders program, err := newProgram("SimpleVertexShader.vertexshader", "SimpleFragmentShader.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]) textureUniform := gl.GetUniformLocation(program, gl.Str("tex\x00")) gl.Uniform1i(textureUniform, 0) gl.BindFragDataLocation(program, 0, gl.Str("outputColor\x00")) // Configure global settings gl.Enable(gl.DEPTH_TEST) gl.DepthFunc(gl.LESS) gl.ClearColor(1.0, 1.0, 1.0, 1.0) angle := 0.0 previousTime := glfw.GetTime() width, height := window.GetSize() window.SetCursorPos(float64(width/2), float64(height/2)) window.SetKeyCallback(input.OnKey) window.SetCursorPosCallback(input.OnCursor) window.SetMouseButtonCallback(input.OnMouse) meshes.LoadColladaCube("cube.dae") for !player.ShouldClose { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) // Update time := glfw.GetTime() elapsed := time - previousTime previousTime = time angle += elapsed model = mgl32.HomogRotate3D(float32(angle), mgl32.Vec3{0, 1, 0}) // Render gl.UseProgram(program) // gl.UniformMatrix4fv(modelUniform, 1, false, &model[0]) player.MainPlayer.Draw(program) for _, element := range game.Universe { (element).Draw(program) } // Maintenance window.SwapBuffers() glfw.PollEvents() } }
func programLoop(window *glfw.Window) { // the linked shader program determines how the data will be rendered vertexShader := compileShader(vertexShaderSource, gl.VERTEX_SHADER) fragmentShaderBlue := compileShader(fragmentShaderBlueSource, gl.FRAGMENT_SHADER) shaderProgramBlue := linkShaders([]uint32{vertexShader, fragmentShaderBlue}) fragmentShaderRed := compileShader(fragmentShaderRedSource, gl.FRAGMENT_SHADER) shaderProgramRed := linkShaders([]uint32{vertexShader, fragmentShaderRed}) // shader objects are not needed after they are linked into a program object gl.DeleteShader(vertexShader) gl.DeleteShader(fragmentShaderBlue) gl.DeleteShader(fragmentShaderRed) vertices1 := []float32{ 0.2, 0.2, 0.0, // top right 0.2, -0.8, 0.0, // bottom right -0.8, -0.8, 0.0, // bottom left -0.8, 0.2, 0.0, // top left } indices1 := []uint32{ 0, 1, 3, // first triangle 1, 2, 3, // second triangle } VAO1 := createTriangleVAO(vertices1, indices1) vertices2 := []float32{ 0.2, 0.6, 0.0, // top 0.6, -0.2, 0.0, // bottom right -0.2, -0.2, 0.0, // bottom left } indices2 := []uint32{ 0, 1, 2, // only triangle } VAO2 := createTriangleVAO(vertices2, indices2) for !window.ShouldClose() { // poll events and call their registered callbacks glfw.PollEvents() // perform rendering gl.ClearColor(0.2, 0.5, 0.5, 1.0) gl.Clear(gl.COLOR_BUFFER_BIT) // draw loop // draw rectangle gl.PolygonMode(gl.FRONT_AND_BACK, gl.LINE) gl.UseProgram(shaderProgramRed) gl.BindVertexArray(VAO1) gl.DrawElements(gl.TRIANGLES, 6, gl.UNSIGNED_INT, unsafe.Pointer(nil)) gl.BindVertexArray(0) // draw triangle gl.PolygonMode(gl.FRONT_AND_BACK, gl.FILL) gl.UseProgram(shaderProgramBlue) gl.BindVertexArray(VAO2) gl.DrawElements(gl.TRIANGLES, 3, gl.UNSIGNED_INT, unsafe.Pointer(nil)) gl.BindVertexArray(0) // end of draw loop // swap in the rendered buffer window.SwapBuffers() } }