func NewEffectsRenderer(w, h int) (r *EffectsRenderer, err error) { r = &EffectsRenderer{ width: w, height: h, Color: mgl32.Vec3{0.0, 0.0, 0.0}, } _, _, r.oldwidth, r.oldheight = twodee.GetInteger4(gl.VIEWPORT) if r.shader, err = twodee.BuildProgram(EFFECTS_VERTEX, EFFECTS_FRAGMENT); err != nil { return } r.positionLoc = uint32(gl.GetAttribLocation(r.shader, gl.Str("a_Position\x00"))) r.textureLoc = uint32(gl.GetAttribLocation(r.shader, gl.Str("a_TextureCoordinates\x00"))) r.textureUnitLoc = gl.GetUniformLocation(r.shader, gl.Str("u_TextureUnit\x00")) r.colorLoc = gl.GetUniformLocation(r.shader, gl.Str("v_Color\x00")) gl.BindFragDataLocation(r.shader, 0, gl.Str("v_FragData\x00")) var size float32 = 1.0 var rect = []float32{ -size, -size, 0.0, 0, 0, -size, size, 0.0, 0, 1, size, -size, 0.0, 1, 0, size, size, 0.0, 1, 1, } if r.coords, err = twodee.CreateVBO(len(rect)*4, rect, gl.STATIC_DRAW); err != nil { return } if r.framebuffer, r.texture, err = r.initFramebuffer(w, h); err != nil { return } return }
// Rendering this LineRender. func (lr *LineRender) Render() { // Binding the appropriate information. gl.BindVertexArray(lr.vao) gl.BindBuffer(gl.ARRAY_BUFFER, lr.vbo) gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, lr.ebo) gl.UseProgram(lr.shaderProgram) // Loading up vertex attributes. vertAttrib := uint32(gl.GetAttribLocation(lr.shaderProgram, gl.Str("vert\x00"))) gl.EnableVertexAttribArray(vertAttrib) gl.VertexAttribPointer(vertAttrib, 2, gl.FLOAT, false, 2*4, gl.PtrOffset(0)) // Line thickness information. gl.Uniform1f( gl.GetUniformLocation(lr.shaderProgram, gl.Str("in_thickness\x00")), lr.weight) // Fragment shader color information. gl.Uniform4f( gl.GetUniformLocation(lr.shaderProgram, gl.Str("in_color\x00")), lr.color.Red, lr.color.Green, lr.color.Blue, lr.color.Alpha) gl.BindFragDataLocation(lr.shaderProgram, 0, gl.Str("out_color\x00")) // Performing the render. gl.DrawElements(gl.LINE_STRIP, lr.points, gl.UNSIGNED_INT, nil) }
func (renderObject *RenderObject) Render() { gl.BindVertexArray(renderObject.vao) gl.BindBuffer(gl.ARRAY_BUFFER, renderObject.vbo) gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, renderObject.ebo) gl.UseProgram(renderObject.shaderProgram) // Binding the texture. gl.ActiveTexture(gl.TEXTURE0) gl.BindTexture(gl.TEXTURE_2D, renderObject.texture) gl.BindFragDataLocation(renderObject.shaderProgram, 0, gl.Str("outputColor\x00")) // Drawing the object. gl.DrawElements(gl.TRIANGLES, 6, gl.UNSIGNED_INT, nil) }
func CreateBuffers() { var err error program, err = MakeProgram(VertexShader(), FragmentShader()) // defer program.Delete() if err != nil { fmt.Println("Error loading shaders: " + err.Error()) panic("error loading shaders") } gl.BindFragDataLocation(program, 0, gl.Str("color\x00")) MVPid = gl.GetUniformLocation(program, gl.Str("MVP\x00")) lightpositionID = gl.GetUniformLocation(program, gl.Str("light.position\x00")) lightintensitiesID = gl.GetUniformLocation(program, gl.Str("light.intensities\x00")) lightattenuationID = gl.GetUniformLocation(program, gl.Str("light.attenuation\x00")) lightambientCoeficientID = gl.GetUniformLocation(program, gl.Str("light.ambientCoeficient\x00")) cameraPositionID = gl.GetUniformLocation(program, gl.Str("cameraPosition\x00")) // View := mathgl.LookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) // viewM = View gl.DepthFunc(gl.LEQUAL) gl.Enable(gl.BLEND) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) // var vao uint32 gl.GenVertexArrays(1, &vao) gl.BindVertexArray(vao) // var vbo uint32 gl.GenBuffers(1, &vbo) // gl.GenBuffers(1, &elementvbo) // fmt.Println(program) gl.UseProgram(program) for idx, img := range aggregateImages { bindAggregateImage(img, idx) } }
func NewGlowRenderer(w, h int, blur int, strength float32, scale float32) (r *GlowRenderer, err error) { r = &GlowRenderer{ width: w, height: h, blur: blur, strength: strength, scale: scale, } _, _, r.oldwidth, r.oldheight = GetInteger4(gl.VIEWPORT) if r.shader, err = BuildProgram(GLOW_VERTEX, GLOW_FRAGMENT); err != nil { return } r.orientationLoc = gl.GetUniformLocation(r.shader, gl.Str("Orientation\x00")) r.blurAmountLoc = gl.GetUniformLocation(r.shader, gl.Str("BlurAmount\x00")) r.blurScaleLoc = gl.GetUniformLocation(r.shader, gl.Str("BlurScale\x00")) r.blurStrengthLoc = gl.GetUniformLocation(r.shader, gl.Str("BlurStrength\x00")) r.bufferDimensionsLoc = gl.GetUniformLocation(r.shader, gl.Str("BufferDimensions\x00")) r.positionLoc = uint32(gl.GetAttribLocation(r.shader, gl.Str("a_Position\x00"))) r.textureLoc = uint32(gl.GetAttribLocation(r.shader, gl.Str("a_TextureCoordinates\x00"))) r.textureUnitLoc = gl.GetUniformLocation(r.shader, gl.Str("u_TextureUnit\x00")) gl.BindFragDataLocation(r.shader, 0, gl.Str("v_FragData\x00")) var size float32 = 1.0 var rect = []float32{ -size, -size, 0.0, 0, 0, -size, size, 0.0, 0, 1, size, -size, 0.0, 1, 0, size, size, 0.0, 1, 1, } if r.coords, err = CreateVBO(len(rect)*4, rect, gl.STATIC_DRAW); err != nil { return } if r.GlowFb, r.GlowTex, err = r.initFramebuffer(w, h); err != nil { return } if r.BlurFb, r.BlurTex, err = r.initFramebuffer(w, h); err != nil { return } return }
func (p *Program) linkProgram(vertex uint32, fragment uint32) (program uint32, err error) { program = gl.CreateProgram() gl.AttachShader(program, vertex) gl.AttachShader(program, fragment) gl.BindFragDataLocation(program, 0, gl.Str("v_FragData\x00")) if e := gl.GetError(); e != 0 { err = fmt.Errorf("ERROR program.BindFragDataLocation %X", e) return } gl.LinkProgram(program) var status int32 if gl.GetProgramiv(program, gl.LINK_STATUS, &status); status == gl.FALSE { var length int32 gl.GetProgramiv(program, gl.INFO_LOG_LENGTH, &length) log := strings.Repeat("\x00", int(length+1)) gl.GetProgramInfoLog(program, length, nil, gl.Str(log)) err = fmt.Errorf("ERROR program link:\n%s", log) } gl.DeleteShader(vertex) gl.DeleteShader(fragment) return }
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() } }
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() } }