func display() { // Clear the background as white gl.ClearColor(1.0, 1.0, 1.0, 1.0) gl.Clear(gl.COLOR_BUFFER_BIT) // Use the GLSL program gl.UseProgram(program) gl.BindBuffer(gl.ARRAY_BUFFER, vboTriangle) gl.EnableVertexAttribArray(attributeCoord2d) // Describe our vertices array to OpenGL (it can't guess its format automatically) gl.VertexAttribPointer(attributeCoord2d, 2, gl.FLOAT, gl.FALSE, 0, gl.Pointer(nil)) gl.EnableVertexAttribArray(attributeColor) gl.BindBuffer(gl.ARRAY_BUFFER, vboTriangleColors) gl.VertexAttribPointer(attributeColor, 3, gl.FLOAT, gl.FALSE, 0, gl.Pointer(nil)) // Push each element in buffer_vertices to the vertex shader gl.DrawArrays(gl.TRIANGLES, 0, 3) gl.DisableVertexAttribArray(attributeCoord2d) gl.DisableVertexAttribArray(attributeColor) gl.BindBuffer(gl.ARRAY_BUFFER, 0) // Unbind // Display the result glfw.SwapBuffers() }
func initResources() { var err error // Load shaders vs, err = createShader("triangle.v.glsl", VertexShaderType) if err != nil { fmt.Printf("Shader: %s\n", err) return } fs, err = createShader("triangle.f.glsl", FragmentShaderType) if err != nil { fmt.Printf("Shader: %s\n", err) return } // Create GLSL program with loaded shaders var compileOk gl.Int program = gl.CreateProgram() gl.AttachShader(program, vs) gl.AttachShader(program, fs) gl.LinkProgram(program) gl.GetProgramiv(program, gl.LINK_STATUS, &compileOk) if compileOk == 0 { fmt.Printf("Error in program.\n") } // Generate a buffer for the VertexBufferObject gl.GenBuffers(1, &vboTriangle) gl.BindBuffer(gl.ARRAY_BUFFER, vboTriangle) // Submit the vertices of the triangle to the graphic card gl.BufferData(gl.ARRAY_BUFFER, gl.Sizeiptr(len(triangleVertices)*4), gl.Pointer(&triangleVertices[0]), gl.STATIC_DRAW) // Unset the active buffer gl.BindBuffer(gl.ARRAY_BUFFER, 0) // Generate a buffer for the Color-VBO gl.GenBuffers(1, &vboTriangleColors) gl.BindBuffer(gl.ARRAY_BUFFER, vboTriangleColors) gl.BufferData(gl.ARRAY_BUFFER, gl.Sizeiptr(len(triangleColors)*4), gl.Pointer(&triangleColors[0]), gl.STATIC_DRAW) gl.BindBuffer(gl.ARRAY_BUFFER, 0) // Get the attribute location from the GLSL program (here from the vertex shader) attributeName := gl.GLString("coord2d") defer gl.GLStringFree(attributeName) attributeTemp := gl.GetAttribLocation(program, attributeName) if attributeTemp == -1 { fmt.Printf("Could not bind attribute %s\n", gl.GoString(attributeName)) } attributeCoord2d = gl.Uint(attributeTemp) attributeName = gl.GLString("v_color") defer gl.GLStringFree(attributeName) attributeTemp = gl.GetAttribLocation(program, attributeName) if attributeTemp == -1 { fmt.Printf("Could not bind attribute %s\n", gl.GoString(attributeName)) } attributeColor = gl.Uint(attributeTemp) }
func drawPrimitives(color color.Color, mode gl.Enum, vecs ...geometry.Vector2) { vertices := make([]float32, len(vecs)*2) r, g, b, a := glcolor.ConvertColorF(color) var colors []float32 for i := range vecs { vertices[i*2] = vecs[i].X vertices[i*2+1] = vecs[i].Y colors = append(colors, r, g, b, a) } DefaultPrimitivesShaderProgram.Use() primitivesUniformMatrix.UniformMatrix4fv(1, false, DefaultCamera.Transposed()) primitivesAttributeCoord.Enable() primitivesAttributeCoord.AttribPointer(2, gl.FLOAT, false, 0, gl.Pointer(&vertices[0])) primitivesAttributeColor.Enable() primitivesAttributeColor.AttribPointer(4, gl.FLOAT, false, 0, gl.Pointer(&colors[0])) gl.DrawArrays(mode, 0, gl.Sizei(len(vecs))) primitivesAttributeColor.Disable() primitivesAttributeCoord.Disable() }
func (buffer *Buffer) Update() { buffer.Bind() data := gl33.Pointer(buffer.Value.Pointer()) size := gl33.Sizeiptr(buffer.Value.Len() * int(buffer.Value.Type().Elem().Size())) gl33.BufferData(buffer.Target, size, data, buffer.Type) }
func (vbo *VertexBufferObject) BufferData(data []float32) { gl.BindBuffer(gl.ARRAY_BUFFER, vbo.id) gl.BufferData(gl.ARRAY_BUFFER, gl.Sizeiptr(len(data)*4), gl.Pointer(&data[0]), gl.STATIC_DRAW) gl.BindBuffer(gl.ARRAY_BUFFER, 0) }
func main() { var window *sdl.Window var context sdl.GLContext var event sdl.Event var running bool var err error runtime.LockOSThread() if err = sdl.Init(sdl.INIT_EVERYTHING); err != nil { panic(err) } defer sdl.Quit() window, err = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, winWidth, winHeight, sdl.WINDOW_OPENGL) if err != nil { panic(err) } defer window.Destroy() context, err = sdl.GL_CreateContext(window) if err != nil { panic(err) } defer sdl.GL_DeleteContext(context) gl.Init() gl.Viewport(0, 0, gl.Sizei(winWidth), gl.Sizei(winHeight)) // OPENGL FLAGS gl.ClearColor(0.0, 0.1, 0.0, 1.0) gl.Enable(gl.DEPTH_TEST) gl.DepthFunc(gl.LESS) gl.Enable(gl.BLEND) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) // VERTEX BUFFER var vertexbuffer gl.Uint gl.GenBuffers(1, &vertexbuffer) gl.BindBuffer(gl.ARRAY_BUFFER, vertexbuffer) gl.BufferData(gl.ARRAY_BUFFER, gl.Sizeiptr(len(triangle_vertices)*4), gl.Pointer(&triangle_vertices[0]), gl.STATIC_DRAW) // COLOUR BUFFER var colourbuffer gl.Uint gl.GenBuffers(1, &colourbuffer) gl.BindBuffer(gl.ARRAY_BUFFER, colourbuffer) gl.BufferData(gl.ARRAY_BUFFER, gl.Sizeiptr(len(triangle_colours)*4), gl.Pointer(&triangle_colours[0]), gl.STATIC_DRAW) // GUESS WHAT program := createprogram() // VERTEX ARRAY var VertexArrayID gl.Uint gl.GenVertexArrays(1, &VertexArrayID) gl.BindVertexArray(VertexArrayID) gl.EnableVertexAttribArray(0) gl.BindBuffer(gl.ARRAY_BUFFER, vertexbuffer) gl.VertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, 0, nil) // VERTEX ARRAY HOOK COLOURS gl.EnableVertexAttribArray(1) gl.BindBuffer(gl.ARRAY_BUFFER, colourbuffer) gl.VertexAttribPointer(1, 3, gl.FLOAT, gl.FALSE, 0, nil) //UNIFORM HOOK unistring := gl.GLString("scaleMove") UniScale = gl.GetUniformLocation(program, unistring) fmt.Printf("Uniform Link: %v\n", UniScale+1) gl.UseProgram(program) running = true for running { for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() { switch t := event.(type) { case *sdl.QuitEvent: running = false case *sdl.MouseMotionEvent: xrot = float32(t.Y) / 2 yrot = float32(t.X) / 2 fmt.Printf("[%dms]MouseMotion\tid:%d\tx:%d\ty:%d\txrel:%d\tyrel:%d\n", t.Timestamp, t.Which, t.X, t.Y, t.XRel, t.YRel) } } drawgl() sdl.GL_SwapWindow(window) } }
func LoadTextureArray(filenames []string, minFilter int, magFilter int) *Texture { ch := make(chan *Texture) GL <- func() { tex := NewTextureArray() tex.SetFilters(minFilter, magFilter) tex.Bind(0) surfaces := make([]*sdl.Surface, len(filenames)) for i, filename := range filenames { surfaces[i] = sdl.Load(filename) if surfaces[i] == nil { panic(sdl.GetError()) } defer surfaces[i].Free() } tex.Width = int(surfaces[0].W) tex.Height = int(surfaces[0].H) var internalFormat gl33.Int var format gl33.Enum var size int if surfaces[0].Format.BitsPerPixel == 32 { internalFormat = gl33.RGBA8 format = gl33.RGBA size = 4 } else { internalFormat = gl33.RGB8 format = gl33.RGB size = 3 } pixels := make([]byte, tex.Width*tex.Height*len(surfaces)*size) for i, surface := range surfaces { p := uintptr(surface.Pixels) for j := 0; j < tex.Width*tex.Height*size; j++ { pixels[i*tex.Width*tex.Height*size+j] = *(*byte)(unsafe.Pointer(p + uintptr(j))) } } gl33.TexImage3D(gl33.TEXTURE_2D_ARRAY, 0, internalFormat, gl33.Sizei(tex.Width), gl33.Sizei(tex.Height), gl33.Sizei(len(surfaces)), 0, format, gl33.UNSIGNED_BYTE, gl33.Pointer(&pixels[0])) gl33.GenerateMipmap(gl33.TEXTURE_2D_ARRAY) ch <- tex } return <-ch }
func uploadSurface(target gl33.Enum, surface *sdl.Surface) { if target == gl33.TEXTURE_CUBE_MAP && surface.W != surface.H { panic("Non-square texture in cube map") } var internalFormat gl33.Int var format gl33.Enum if surface.Format.BitsPerPixel == 32 { internalFormat = gl33.RGBA8 format = gl33.RGBA } else { internalFormat = gl33.RGB8 format = gl33.RGB } gl33.TexImage2D(target, 0, internalFormat, gl33.Sizei(surface.W), gl33.Sizei(surface.H), 0, format, gl33.UNSIGNED_BYTE, gl33.Pointer(surface.Pixels)) }
func setupAttrib(location gl33.Uint, type_ gl33.Enum, dimensions int, offset uintptr, vertexSize int) { gl33.EnableVertexAttribArray(location) if type_ == gl33.FLOAT || type_ == gl33.DOUBLE { gl33.VertexAttribPointer(location, gl33.Int(dimensions), type_, gl33.FALSE, gl33.Sizei(vertexSize), gl33.Pointer(offset)) } else { gl33.VertexAttribIPointer(location, gl33.Int(dimensions), type_, gl33.Sizei(vertexSize), gl33.Pointer(offset)) } }
func (mesh *Mesh) Render() { gl33.BindVertexArray(mesh.vao) gl33.DrawElements(gl33.TRIANGLES, gl33.Sizei(len(mesh.Indicies)), gl33.UNSIGNED_INT, gl33.Pointer(uintptr(0))) }
// LoadTexture buffers an image.Image into the graphic cards memory. func LoadTexture(img image.Image) (*Texture, error) { w := img.Bounds().Dx() h := img.Bounds().Dy() rgba := image.NewRGBA(image.Rect(0, 0, w, h)) for x := 0; x < w; x++ { for y := 0; y < h; y++ { rgba.Set(x, y, img.At(x, y)) } } var textureId gl.Uint //gl.ActiveTexture(gl.TEXTURE0) gl.GenTextures(1, &textureId) gl.BindTexture(gl.TEXTURE_2D, textureId) //gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) gl.TexImage2D(gl.TEXTURE_2D, 0, 4, gl.Sizei(rgba.Rect.Dx()), gl.Sizei(rgba.Rect.Dy()), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Pointer(&rgba.Pix[0])) gl.BindTexture(gl.TEXTURE_2D, 0) return &Texture{id: textureId}, nil }