func Cylinder(r, h gl.Float, slices int, hollow, solid bool) { res := 2 * math.Pi / float64(slices) mode := gl.LINE_LOOP if solid { mode = gl.QUADS } gl.Begin(gl.Enum(mode)) for a := 0.0; a < 2*math.Pi; a += res { gl.Normal3f(gl.Float(math.Cos(a)), gl.Float(math.Sin(a)), 0) gl.Vertex3f(r*gl.Float(math.Cos(a)), r*gl.Float(math.Sin(a)), 0) gl.Vertex3f(r*gl.Float(math.Cos(a)), r*gl.Float(math.Sin(a)), h) a += res gl.Vertex3f(r*gl.Float(math.Cos(a)), r*gl.Float(math.Sin(a)), h) gl.Vertex3f(r*gl.Float(math.Cos(a)), r*gl.Float(math.Sin(a)), 0) } gl.End() if !hollow { // X Y plane if h < 0 { gl.Normal3f(0, 0, 1) } else { gl.Normal3f(0, 0, -1) } Circle(r, slices, solid) // Top (or bottom) if h < 0 { gl.Normal3f(0, 0, -1) } else { gl.Normal3f(0, 0, 1) } gl.Translatef(0, 0, h) Circle(r, slices, solid) } }
func Triangle(one, two, three Point, solid bool) { mode := gl.LINE_LOOP if solid { mode = gl.TRIANGLES } gl.Begin(gl.Enum(mode)) gl.Vertex3f(one.X, one.Y, one.Z) gl.Vertex3f(two.X, two.Y, two.Z) gl.Vertex3f(three.X, three.Y, three.Z) gl.End() }
func compileShader(shaderType ShaderType, scripts []*gl.Char) (gl.Uint, error) { shaderID := gl.CreateShader(gl.Enum(shaderType)) gl.ShaderSource(shaderID, gl.Sizei(len(scripts)), &scripts[0], nil) gl.CompileShader(shaderID) if err := checkShaderCompiled(shaderID, shaderType); err != nil { return 0, err } return shaderID, checkForErrors() }
func Rectangle(one, two Point, solid bool) { mode := gl.LINE_LOOP if solid { mode = gl.QUADS } gl.Begin(gl.Enum(mode)) // Clockwise from top right gl.Vertex3f(one.X, one.Y, one.Z) // Q1 gl.Vertex3f(one.X, two.Y, one.Z) // Q4 gl.Vertex3f(two.X, two.Y, two.Z) // Q3 gl.Vertex3f(two.X, one.Y, two.Z) // Q2 gl.End() }
func Ellipse(rX, rY gl.Float, slices int, solid bool) { mode := gl.LINE_LOOP if solid { mode = gl.POLYGON } res := 2 * math.Pi / float64(slices) gl.Begin(gl.Enum(mode)) gl.Vertex2f(0, 0) for a := 0.0; a < 2*math.Pi; a += res { gl.Vertex2f(rX*gl.Float(math.Cos(a)), rY*gl.Float(math.Sin(a))) } gl.End() }
func Box(x, y, z float32, solid bool) { mode := gl.LINE_LOOP if solid { mode = gl.QUADS } X := gl.Float(x) / 2 Y := gl.Float(y) / 2 Z := gl.Float(z) / 2 gl.Begin(gl.Enum(mode)) gl.Normal3f(0, 0, 1) // Positive Z Face gl.Vertex3f(-X, -Y, Z) gl.Vertex3f(X, -Y, Z) gl.Vertex3f(X, Y, Z) gl.Vertex3f(-X, Y, Z) gl.Normal3f(0, 0, -1) // Negative Z Face gl.Vertex3f(-X, -Y, -Z) gl.Vertex3f(-X, Y, -Z) gl.Vertex3f(X, Y, -Z) gl.Vertex3f(X, -Y, -Z) gl.Normal3f(0, 1, 0) // Positive Y Face gl.Vertex3f(-X, Y, -Z) gl.Vertex3f(-X, Y, Z) gl.Vertex3f(X, Y, Z) gl.Vertex3f(X, Y, -Z) gl.Normal3f(0, -1, 0) // Negative Y Face gl.Vertex3f(-X, -Y, -Z) gl.Vertex3f(X, -Y, -Z) gl.Vertex3f(X, -Y, Z) gl.Vertex3f(-X, -Y, Z) gl.Normal3f(1, 0, 0) // Positive X Face gl.Vertex3f(X, -Y, -Z) gl.Vertex3f(X, Y, -Z) gl.Vertex3f(X, Y, Z) gl.Vertex3f(X, -Y, Z) gl.Normal3f(-1, 0, 0) // Negative X Face gl.Vertex3f(-X, -Y, -Z) gl.Vertex3f(-X, -Y, Z) gl.Vertex3f(-X, Y, Z) gl.Vertex3f(-X, Y, -Z) gl.End() }
// NewShape creates a new Shape object based on the verticies and shape type. func NewShape(shapeType ShapeType, verticies []float64) (*Shape, error) { verticies32 := make([]float32, len(verticies)) for i, val := range verticies { verticies32[i] = float32(val) } shape := &Shape{mode: gl.Enum(shapeType), size: len(verticies), vertexBuffer: 0, verticies: verticies32, scaleX: 1, scaleY: 1} gl.GenBuffers(1, &shape.vertexBuffer) gl.BindBuffer(gl.ARRAY_BUFFER, gl.Uint(shape.vertexBuffer)) gl.BufferData(gl.ARRAY_BUFFER, gl.Sizeiptr(shape.size*4), gl.Pointer(&shape.verticies[0]), gl.DYNAMIC_DRAW) gl.BindBuffer(gl.ARRAY_BUFFER, 0) return shape, checkForErrors() }