// Draw draws the Sprite object. func (sprite *Sprite) Draw(frame int) error { gl.BindBuffer(gl.ARRAY_BUFFER, sprite.shape.vertexBuffer) gl.VertexAttribPointer(gl.Uint(0), 2, gl.FLOAT, gl.FALSE, 0, gl.Offset(nil, 0)) gl.BindAttribLocation(paunchEffect.program, gl.Uint(0), gl.GLString("position")) gl.BindBuffer(gl.ARRAY_BUFFER, 0) if sprite.texcoordBuffer != 0 { gl.ActiveTexture(gl.TEXTURE0) gl.BindBuffer(gl.ARRAY_BUFFER, sprite.texcoordBuffer) gl.VertexAttribPointer(gl.Uint(1), 2, gl.FLOAT, gl.FALSE, 0, gl.Offset(nil, 0)) gl.BindAttribLocation(paunchEffect.program, gl.Uint(0), gl.GLString("texcoord")) gl.BindBuffer(gl.ARRAY_BUFFER, 0) gl.BindTexture(gl.TEXTURE_2D, sprite.texture[frame]) gl.EnableVertexAttribArray(gl.Uint(1)) } gl.EnableVertexAttribArray(gl.Uint(0)) gl.DrawArrays(gl.TRIANGLES, 0, gl.Sizei(sprite.shape.size)) gl.DisableVertexAttribArray(gl.Uint(0)) gl.DisableVertexAttribArray(gl.Uint(1)) gl.BindTexture(gl.TEXTURE_2D, 0) return checkForErrors() }
func texinit(id int) { gl.BindTexture(gl.TEXTURE_2D, gl.Uint(id)) gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE) gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE) }
// Draw draws the Shape object. func (shape *Shape) Draw() error { gl.BindBuffer(gl.ARRAY_BUFFER, shape.vertexBuffer) vertexAttribLoc := gl.GetAttribLocation(paunchEffect.program, gl.GLString("position")) gl.VertexAttribPointer(gl.Uint(vertexAttribLoc), 2, gl.FLOAT, gl.FALSE, 0, gl.Offset(nil, 0)) gl.BindBuffer(gl.ARRAY_BUFFER, 0) gl.EnableVertexAttribArray(gl.Uint(vertexAttribLoc)) gl.DrawArrays(shape.mode, 0, gl.Sizei(shape.size/2)) gl.DisableVertexAttribArray(gl.Uint(vertexAttribLoc)) //gl.DisableVertexAttribArray(gl.Uint(1)) //gl.BindTexture(gl.TEXTURE_2D, 0) return checkForErrors() }
func (s *Shader) initAttribute(name string, att *gl.Uint) { att_name := gl.GLString(name) defer gl.GLStringFree(att_name) att_tmp := gl.GetAttribLocation(s.program, att_name) if att_tmp == -1 { fmt.Println("Error in getting attribute ", gl.GoString(att_name)) } else { *att = gl.Uint(att_tmp) } }
// NewSprite creates a new Sprite object using the given data, which is // expected to be in RGBA format. If you use PNG image files, you can use the // NewSpriteFromImage shortcut function instead. func NewSprite(x, y, width, height float64, data []byte, clip int) (*Sprite, error) { verticies := []float64{ x, y, x + width, y, x, y + height, x + width, y + height, x + width, y, x, y + height} shape, err := NewShape(gl.TRIANGLES, verticies) if err != nil { return nil, err } sprite := &Sprite{texcoordBuffer: 0, texture: nil, shape: shape} texCoords := []float32{ 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1} gl.GenBuffers(1, &sprite.texcoordBuffer) gl.BindBuffer(gl.ARRAY_BUFFER, gl.Uint(sprite.texcoordBuffer)) gl.BufferData(gl.ARRAY_BUFFER, gl.Sizeiptr(len(texCoords)*4), gl.Pointer(&texCoords[0]), gl.STREAM_DRAW) gl.BindBuffer(gl.ARRAY_BUFFER, 0) sprite.texture = make([]gl.Uint, clip) gl.GenTextures(gl.Sizei(clip), &sprite.texture[0]) clips := make([][]byte, clip) for i := range clips { clips[i] = data[i*(len(data)/len(clips)) : (i+1)*(len(data)/len(clips))] gl.BindTexture(gl.TEXTURE_2D, sprite.texture[len(clips)-1-i]) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT) gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.Sizei(width), gl.Sizei(height), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Pointer(&clips[i][0])) } gl.BindTexture(gl.TEXTURE_2D, 0) return sprite, checkForErrors() }
// SetVariablef sets a specified variable to the supplied integer to be passed // into an effect. func (effect *Effect) SetVariablef(variable string, val float32) error { var currEffect gl.Int gl.GetIntegerv(gl.CURRENT_PROGRAM, &currEffect) if gl.Uint(currEffect) != effect.program { return errors.New("effect is not currently in use") } effect.checkUniformVariable(variable) gl.Uniform1f(effect.uniforms[variable], gl.Float(val)) return nil }
// SetVariable4i sets a specified variable to the four supplied integers to be // passed into an effect. func (effect *Effect) SetVariable4i(variable string, val1 int, val2 int, val3 int, val4 int) error { var currEffect gl.Int gl.GetIntegerv(gl.CURRENT_PROGRAM, &currEffect) if gl.Uint(currEffect) != effect.program { return errors.New("effect is not currently in use") } effect.checkUniformVariable(variable) gl.Uniform4i(effect.uniforms[variable], gl.Int(val1), gl.Int(val2), gl.Int(val3), gl.Int(val4)) return nil }
// Sets up anything that wouldn't have been loaded from disk, including // all opengl data, and sets up finalizers for that data. func (d *Dictionary) setupGlStuff() { d.dlists = make(map[string]uint32) d.strs = make(map[string]strBuffer) d.pars = make(map[string]strBuffer) // TODO: This finalizer is untested runtime.SetFinalizer(d, func(d *Dictionary) { render.Queue(func() { for _, v := range d.dlists { gl.DeleteLists(gl.Uint(v), 1) } }) }) render.Queue(func() { gl.Enable(gl.TEXTURE_2D) gl.GenTextures(1, (*gl.Uint)(&d.texture)) gl.BindTexture(gl.TEXTURE_2D, gl.Uint(d.texture)) gl.PixelStorei(gl.UNPACK_ALIGNMENT, 1) gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.MODULATE) gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT) gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT) gl.TexImage2D( gl.TEXTURE_2D, 0, gl.ALPHA, gl.Sizei(d.data.Dx), gl.Sizei(d.data.Dy), 0, gl.ALPHA, gl.UNSIGNED_BYTE, gl.Pointer(&d.data.Pix[0])) gl.Disable(gl.TEXTURE_2D) }) }
// 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() }
func (mr *ModelReader) readModel(path string, m *Model) { var num uint16 mr.readuint16(&num) for i := 0; i < int(num); i++ { var x, y, z float32 mr.readfloat32(&x) mr.readfloat32(&y) mr.readfloat32(&z) m.vertices = append(m.vertices, gl.Float(x), gl.Float(y), gl.Float(z)) //fmt.Printf("vertices %f, %f, %f \n", x, y, z) } //fmt.Printf("m.vertices size %d \n", len(m.vertices)) var faces uint16 mr.readuint16(&faces) //fmt.Println("nb faces : ", faces) for i := 0; i < int(faces); i++ { var x, y, z uint16 mr.readuint16(&x) mr.readuint16(&y) mr.readuint16(&z) m.indices = append(m.indices, gl.Uint(x), gl.Uint(y), gl.Uint(z)) //fmt.Printf("face indices :%d, %d, %d \n", x, y, z) } var normals_num uint16 mr.readuint16(&normals_num) for i := 0; i < int(normals_num); i++ { var x, y, z float32 mr.readfloat32(&x) mr.readfloat32(&y) mr.readfloat32(&z) m.normals = append(m.normals, gl.Float(x), gl.Float(y), gl.Float(z)) //fmt.Printf("normal %f, %f, %f \n", x, y, z) } var uvs_num uint16 mr.readuint16(&uvs_num) //fmt.Printf("uvs num %d \n", uvs_num) m.has_uv = uvs_num > 0 if m.has_uv { for i := 0; i < int(uvs_num); i++ { var x, y float32 mr.readfloat32(&x) mr.readfloat32(&y) m.uvs = append(m.uvs, gl.Float(x), gl.Float(y)) //fmt.Printf("uvs %f, %f \n", x, y) } } }
func (d *Dictionary) RenderString(s string, x, y, z, height float64, just Justification) { if len(s) == 0 { return } strbuf, ok := d.strs[s] if !ok { defer d.RenderString(s, x, y, z, height, just) } else { render.EnableShader("glop.font") diff := 20/math.Pow(height, 1.0) + 5*math.Pow(d.data.Scale, 1.0)/math.Pow(height, 1.0) if diff > 0.4 { // TODO: Need to come up with decent values here diff = 0.4 } render.SetUniformF("glop.font", "dist_min", float32(0.5-diff)) render.SetUniformF("glop.font", "dist_max", float32(0.5+diff)) defer render.EnableShader("") } size := unsafe.Sizeof(dictVert{}) scale := height / float64(d.data.Maxy-d.data.Miny) width := float32(d.figureWidth(s) * scale) x_pos := float32(x) switch just { case Center: x_pos -= width / 2 case Right: x_pos -= width } if ok { gl.PushMatrix() defer gl.PopMatrix() gl.Translated(gl.Double(x_pos), gl.Double(y), gl.Double(z)) gl.Scaled(gl.Double(scale), gl.Double(scale), 1) gl.PushAttrib(gl.COLOR_BUFFER_BIT) defer gl.PopAttrib() gl.Enable(gl.BLEND) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.Enable(gl.TEXTURE_2D) gl.BindTexture(gl.TEXTURE_2D, gl.Uint(d.texture)) gl.BindBuffer(gl.ARRAY_BUFFER, gl.Uint(strbuf.vbuffer)) gl.EnableClientState(gl.VERTEX_ARRAY) gl.VertexPointer(2, gl.FLOAT, gl.Sizei(size), nil) gl.EnableClientState(gl.TEXTURE_COORD_ARRAY) gl.TexCoordPointer(2, gl.FLOAT, gl.Sizei(size), gl.Pointer(unsafe.Offsetof(strbuf.vs[0].u))) gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, gl.Uint(strbuf.ibuffer)) gl.DrawElements(gl.TRIANGLES, gl.Sizei(len(strbuf.is)), gl.UNSIGNED_SHORT, nil) gl.DisableClientState(gl.VERTEX_ARRAY) gl.DisableClientState(gl.TEXTURE_COORD_ARRAY) gl.Disable(gl.TEXTURE_2D) return } x_pos = 0 var prev rune for _, r := range s { if _, ok := d.data.Kerning[prev]; ok { x_pos += float32(d.data.Kerning[prev][r]) } prev = r info := d.getInfo(r) xleft := x_pos + float32(info.Full_bounds.Min.X) xright := x_pos + float32(info.Full_bounds.Max.X) ytop := float32(info.Full_bounds.Max.Y) + float32(-d.data.Miny) ybot := float32(info.Full_bounds.Min.Y) + float32(-d.data.Miny) start := uint16(len(strbuf.vs)) strbuf.is = append(strbuf.is, start+0) strbuf.is = append(strbuf.is, start+1) strbuf.is = append(strbuf.is, start+2) strbuf.is = append(strbuf.is, start+0) strbuf.is = append(strbuf.is, start+2) strbuf.is = append(strbuf.is, start+3) strbuf.vs = append(strbuf.vs, dictVert{ x: xleft, y: ytop, u: float32(info.Pos.Min.X) / float32(d.data.Dx), v: float32(info.Pos.Max.Y) / float32(d.data.Dy), }) strbuf.vs = append(strbuf.vs, dictVert{ x: xleft, y: ybot, u: float32(info.Pos.Min.X) / float32(d.data.Dx), v: float32(info.Pos.Min.Y) / float32(d.data.Dy), }) strbuf.vs = append(strbuf.vs, dictVert{ x: xright, y: ybot, u: float32(info.Pos.Max.X) / float32(d.data.Dx), v: float32(info.Pos.Min.Y) / float32(d.data.Dy), }) strbuf.vs = append(strbuf.vs, dictVert{ x: xright, y: ytop, u: float32(info.Pos.Max.X) / float32(d.data.Dx), v: float32(info.Pos.Max.Y) / float32(d.data.Dy), }) x_pos += float32(info.Advance) // - float32((info.Full_bounds.Dx() - info.Bounds.Dx())) } gl.GenBuffers(1, (*gl.Uint)(&strbuf.vbuffer)) gl.BindBuffer(gl.ARRAY_BUFFER, gl.Uint(strbuf.vbuffer)) gl.BufferData(gl.ARRAY_BUFFER, gl.Sizeiptr(int(size)*len(strbuf.vs)), gl.Pointer(&strbuf.vs[0].x), gl.STATIC_DRAW) gl.GenBuffers(1, (*gl.Uint)(&strbuf.ibuffer)) gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, gl.Uint(strbuf.ibuffer)) gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, gl.Sizeiptr(int(unsafe.Sizeof(strbuf.is[0]))*len(strbuf.is)), gl.Pointer(&strbuf.is[0]), gl.STATIC_DRAW) d.strs[s] = strbuf }