// 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 (w *glfwBackend) Render(console *Console) { if w.Running() { w.mouse.Left.Released = false w.mouse.Right.Released = false w.mouse.Middle.Released = false w.key = NOKEY gl.Clear(gl.COLOR_BUFFER_BIT) gl.BindTexture(gl.TEXTURE_2D, textures[1]) for y := 0; y < console.Height(); y++ { for x := 0; x < console.Width(); x++ { _, bg, _ := console.Get(x, y) w.letter(x, y, 0, bg) } } gl.BindTexture(gl.TEXTURE_2D, textures[0]) for y := 0; y < console.Height(); y++ { for x := 0; x < console.Width(); x++ { fg, _, ch := console.Get(x, y) if position, ok := w.font.Map(ch); ok { w.letter(x, y, position, fg) } } } glfw.SwapBuffers() } }
func (ms *ManaSource) Draw(local *LocalData, zoom float64, dx float64, dy float64) { if local.nodeTextureData == nil { // gl.Enable(gl.TEXTURE_2D) local.nodeTextureData = make([]byte, ms.options.NumNodeRows*ms.options.NumNodeCols*3) gl.GenTextures(1, &local.nodeTextureId) gl.BindTexture(gl.TEXTURE_2D, local.nodeTextureId) 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.RGB, gl.Sizei(ms.options.NumNodeRows), gl.Sizei(ms.options.NumNodeCols), 0, gl.RGB, gl.UNSIGNED_BYTE, gl.Pointer(&local.nodeTextureData[0])) } for i := range ms.rawNodes { for c := 0; c < 3; c++ { color_frac := ms.rawNodes[i].Mana[c] * 1.0 / ms.options.NodeMagnitude color_range := float64(ms.options.MaxNodeBrightness - ms.options.MinNodeBrightness) local.nodeTextureData[i*3+c] = byte( color_frac*color_range + float64(ms.options.MinNodeBrightness)) } } gl.Enable(gl.TEXTURE_2D) //gl.ActiveTexture(gl.TEXTURE0) gl.BindTexture(gl.TEXTURE_2D, local.nodeTextureId) gl.TexSubImage2D( gl.TEXTURE_2D, 0, 0, 0, gl.Sizei(ms.options.NumNodeRows), gl.Sizei(ms.options.NumNodeCols), gl.RGB, gl.UNSIGNED_BYTE, gl.Pointer(&local.nodeTextureData[0])) base.EnableShader("nodes") base.SetUniformI("nodes", "width", ms.options.NumNodeRows*3) base.SetUniformI("nodes", "height", ms.options.NumNodeCols*3) base.SetUniformI("nodes", "drains", 1) base.SetUniformI("nodes", "tex0", 0) base.SetUniformI("nodes", "tex1", 1) base.SetUniformF("nodes", "zoom", float32(zoom)) gl.ActiveTexture(gl.TEXTURE0) gl.BindTexture(gl.TEXTURE_2D, local.nodeTextureId) // I have no idea why this value for move works, but it does. So, hooray. move := (dx - dy) / 2 texture.RenderAdvanced(move, -move, dy, dx, 3.1415926535/2, true) base.EnableShader("") gl.Disable(gl.TEXTURE_2D) }
func (d *Data) Bind() { if d.texture == 0 { if error_texture == 0 { makeErrorTexture() } gl.BindTexture(gl.TEXTURE_2D, error_texture) } else { gl.BindTexture(gl.TEXTURE_2D, d.texture) } }
func (d *Data) Bind() { if d.texture == 0 { if error_texture == 0 { makeErrorTexture() } gl.BindTexture(gl.TEXTURE_2D, error_texture) base.Log().Printf("Ninindg error etextures") } else { gl.BindTexture(gl.TEXTURE_2D, d.texture) } }
// 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() }
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) }
func createTexture(r io.Reader) (textureId gl.Uint, err error) { img, err := png.Decode(r) if err != nil { return 0, err } rgbaImg, ok := img.(*image.NRGBA) if !ok { return 0, errors.New("texture must be an NRGBA image") } 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) // flip image: first pixel is lower left corner imgWidth, imgHeight := img.Bounds().Dx(), img.Bounds().Dy() data := make([]byte, imgWidth*imgHeight*4) lineLen := imgWidth * 4 dest := len(data) - lineLen for src := 0; src < len(rgbaImg.Pix); src += rgbaImg.Stride { copy(data[dest:dest+lineLen], rgbaImg.Pix[src:src+rgbaImg.Stride]) dest -= lineLen } gl.TexImage2D(gl.TEXTURE_2D, 0, 4, gl.Sizei(imgWidth), gl.Sizei(imgHeight), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Pointer(&data[0])) return textureId, nil }
func upload(id gl.Uint, data []byte, stride int, w int, h int) { gl.BindTexture(gl.TEXTURE_2D, id) gl.PixelStorei(gl.UNPACK_ROW_LENGTH, gl.Int(stride)) gl.PixelStorei(gl.UNPACK_ALIGNMENT, 1) gl.TexImage2D(gl.TEXTURE_2D, 0, gl.LUMINANCE, gl.Sizei(w), gl.Sizei(h), 0, gl.LUMINANCE, gl.UNSIGNED_BYTE, gl.Pointer(&data[0])) }
func (self *Tile) Draw() { switch self.CurrentLOD { case -1: // TODO: draw single color r, i := self.What.Center() c := complex(r, i) color := mandelbrotAt(c) gl.BindTexture(gl.TEXTURE_2D, 0) gl.Color3ub(color.R, color.G, color.B) drawQuad(self.X, self.Y, self.W, self.H, 0, 0, 1, 1) gl.Color3ub(255, 255, 255) case 0: gl.BindTexture(gl.TEXTURE_2D, self.Texture[0]) drawQuad(self.X, self.Y, self.W, self.H, 0, 0, 1, 1) case 1: gl.BindTexture(gl.TEXTURE_2D, self.Texture[1]) drawQuad(self.X, self.Y, self.W, self.H, 0, 0, 1, 1) default: panic("unreachable") } }
func makeErrorTexture() { gl.Enable(gl.TEXTURE_2D) gl.GenTextures(1, (*gl.Uint)(&error_texture)) gl.BindTexture(gl.TEXTURE_2D, error_texture) gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.MODULATE) gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_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) pink := []byte{255, 0, 255, 255} gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.BYTE, gl.Pointer(&pink[0])) }
func (m *Model) draw() { m.shader.use() gl.ActiveTexture(gl.TEXTURE0) gl.BindTexture(gl.TEXTURE_2D, m.texture_id) gl.Uniform1i(m.shader.uniform_texture, 0) //texcoord if m.has_uv { gl.BindBuffer(gl.ARRAY_BUFFER, m.texcoord) gl.EnableVertexAttribArray(m.shader.attribute_texcoord) gl.VertexAttribPointer( m.shader.attribute_texcoord, 2, gl.FLOAT, gl.FALSE, 0, gl.Pointer(nil)) } gl.BindBuffer(gl.ARRAY_BUFFER, m.buffer) gl.EnableVertexAttribArray(m.shader.attribute_vertex) gl.VertexAttribPointer( m.shader.attribute_vertex, 3, gl.FLOAT, gl.FALSE, 0, gl.Pointer(nil)) gl.BindBuffer(gl.ARRAY_BUFFER, m.normal_buf) gl.EnableVertexAttribArray(m.shader.attribute_normal) gl.VertexAttribPointer( m.shader.attribute_normal, 3, gl.FLOAT, gl.FALSE, 0, gl.Pointer(nil)) gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, m.index) gl.DrawElements(gl.TRIANGLES, gl.Sizei(len(m.indices)), gl.UNSIGNED_INT, gl.Pointer(nil)) gl.BindBuffer(gl.ARRAY_BUFFER, 0) gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, 0) gl.DisableVertexAttribArray(m.shader.attribute_vertex) gl.DisableVertexAttribArray(m.shader.attribute_normal) if m.has_uv { gl.DisableVertexAttribArray(m.shader.attribute_texcoord) } }
func reuploadTexture(tex *gl.Uint, w, h int, data []byte) { if *tex > 0 { gl.BindTexture(gl.TEXTURE_2D, *tex) gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.Sizei(w), gl.Sizei(h), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Pointer(&data[0])) if gl.GetError() != gl.NO_ERROR { gl.DeleteTextures(1, tex) panic("Failed to reupload texture") } return } *tex = uploadTexture_RGBA32(w, h, data) }
func (s *Sprite) Bind() (x, y, x2, y2 float64) { var rect FrameRect var sh *sheet var ok bool fid := frameId{facing: s.facing, node: s.anim_node.Id()} var dx, dy float64 if rect, ok = s.shared.connector.rects[fid]; ok { sh = s.shared.connector } else if rect, ok = s.shared.facings[s.facing].rects[fid]; ok { sh = s.shared.facings[s.facing] } else { gl.BindTexture(gl.TEXTURE_2D, error_texture) return } gl.BindTexture(gl.TEXTURE_2D, sh.texture) dx = float64(sh.dx) dy = float64(sh.dy) x = float64(rect.X) / dx y = float64(rect.Y) / dy x2 = float64(rect.X2) / dx y2 = float64(rect.Y2) / dy return }
func (m *Model) initTexture() { //file, err := os.Open("model/test.png") file, err := os.Open("model/ceil.png") if err != nil { log.Fatal(err) } defer file.Close() img, _, err := image.Decode(file) if err != nil { log.Fatal(err) } rgbaImg, ok := img.(*image.NRGBA) if !ok { //return 0, errors.New("texture must be an NRGBA image") log.Fatal("image is not rgba") } gl.GenTextures(1, &m.texture_id) gl.BindTexture(gl.TEXTURE_2D, m.texture_id) gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) // flip image: first pixel is lower left corner img_width, img_height := img.Bounds().Dx(), img.Bounds().Dy() data := make([]byte, img_width*img_height*4) line_len := img_width * 4 dest := len(data) - line_len for src := 0; src < len(rgbaImg.Pix); src += rgbaImg.Stride { copy(data[dest:dest+line_len], rgbaImg.Pix[src:src+rgbaImg.Stride]) dest -= line_len } gl.TexImage2D( gl.TEXTURE_2D, 0, gl.RGBA, //4, gl.Sizei(img_width), gl.Sizei(img_height), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Pointer(&data[0])) }
func uploadTexture_RGBA32(w, h int, data []byte) gl.Uint { var id gl.Uint gl.GenTextures(1, &id) gl.BindTexture(gl.TEXTURE_2D, id) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_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.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_R, gl.CLAMP_TO_EDGE) gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.Sizei(w), gl.Sizei(h), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Pointer(&data[0])) if gl.GetError() != gl.NO_ERROR { gl.DeleteTextures(1, &id) panic("Failed to load a texture") } return id }
func DrawStarTex(t *data.Star) { // Find the center point of the texture to rotate around xav := (t.X1 + t.X2) / 2 yav := (t.Y1 + t.Y2) / 2 //Translate there, rotate, translate back gl.MatrixMode(gl.MODELVIEW) gl.Translatef(xav, yav, 0) gl.Rotatef(gl.Float(t.Theta), 0, 0, 1) gl.Translatef(-xav, -yav, 0) //Bind our texture to be drawn by id gl.Color3f(1, 1, 1) gl.Enable(gl.TEXTURE_2D) gl.BindTexture(gl.TEXTURE_2D, t.TexId) // Draw a rectangle with the texture stretched to the corners gl.Begin(gl.QUADS) // Stretch the texture to its 4 corners. gl.TexCoord2d(0, 0) gl.Vertex2f(t.X1, t.Y1) gl.TexCoord2d(0, 1) gl.Vertex2f(t.X1, t.Y2) gl.TexCoord2d(1, 1) gl.Vertex2f(t.X2, t.Y2) gl.TexCoord2d(1, 0) gl.Vertex2f(t.X2, t.Y1) gl.End() // Unbind the texture in case something else wants to draw gl.Disable(gl.TEXTURE_2D) // Reset the matrix gl.LoadIdentity() }
func (m *Manager) LoadSprite(path string) (*Sprite, error) { // We can't run this during an init() function because it will get queued to // run before the opengl context is created, so we just check here and run // it if we haven't run it before. gen_tex_once.Do(func() { render.Queue(func() { gl.Enable(gl.TEXTURE_2D) gl.GenTextures(1, &error_texture) gl.BindTexture(gl.TEXTURE_2D, error_texture) gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.MODULATE) gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_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) pink := []byte{255, 0, 255, 255} gl.TexImage2D( gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_INT, gl.Pointer(&pink[0])) }) }) path = filepath.Clean(path) err := m.loadSharedSprite(path) if err != nil { return nil, err } var s Sprite m.mutex.Lock() s.shared = m.shared[path] m.mutex.Unlock() s.anim_node = s.shared.anim_start s.state_node = s.shared.state_start return &s, 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) }) }
func (s *sheet) makeTexture(pixer <-chan []byte) { gl.Enable(gl.TEXTURE_2D) gl.GenTextures(1, &s.texture) gl.BindTexture(gl.TEXTURE_2D, s.texture) gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.MODULATE) gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_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) data := <-pixer gl.TexImage2D( gl.TEXTURE_2D, 0, gl.RGBA, gl.Sizei(s.dx), gl.Sizei(s.dy), 0, gl.RGBA, gl.UNSIGNED_INT, gl.Pointer(&data[0])) memory.FreeBlock(data) }
func (ms *ManaSource) Draw(gw *GameWindow, dx float64, dy float64) { if gw.nodeTextureData == nil { // gl.Enable(gl.TEXTURE_2D) gw.nodeTextureData = make([]byte, ms.options.NumNodeRows*ms.options.NumNodeCols*3) gl.GenTextures(1, &gw.nodeTextureId) gl.BindTexture(gl.TEXTURE_2D, gw.nodeTextureId) 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.RGB, gl.Sizei(ms.options.NumNodeCols), gl.Sizei(ms.options.NumNodeRows), 0, gl.RGB, gl.UNSIGNED_BYTE, gl.Pointer(&gw.nodeTextureData[0])) // gl.ActiveTexture(gl.TEXTURE1) gl.GenTextures(1, &gw.nodeWarpingTexture) gl.BindTexture(gl.TEXTURE_1D, gw.nodeWarpingTexture) gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.MODULATE) gl.TexParameterf(gl.TEXTURE_1D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) gl.TexParameterf(gl.TEXTURE_1D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexParameterf(gl.TEXTURE_1D, gl.TEXTURE_WRAP_S, gl.REPEAT) gl.TexParameterf(gl.TEXTURE_1D, gl.TEXTURE_WRAP_T, gl.REPEAT) gw.nodeWarpingData = make([]byte, 4*10) gl.TexImage1D( gl.TEXTURE_1D, 0, gl.RGBA, gl.Sizei(len(gw.nodeWarpingData)/4), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Pointer(&gw.nodeWarpingData[0])) } for x := range ms.nodes { for y, node := range ms.nodes[x] { pos := 3 * (y*ms.options.NumNodeCols + x) for c := 0; c < 3; c++ { color_frac := node.Mana[c] * 1.0 / ms.options.NodeMagnitude color_range := float64(ms.options.MaxNodeBrightness - ms.options.MinNodeBrightness) gw.nodeTextureData[pos+c] = byte( color_frac*color_range + float64(ms.options.MinNodeBrightness)) } } } gl.Enable(gl.TEXTURE_1D) gl.Enable(gl.TEXTURE_2D) //gl.ActiveTexture(gl.TEXTURE0) gl.BindTexture(gl.TEXTURE_2D, gw.nodeTextureId) gl.TexSubImage2D( gl.TEXTURE_2D, 0, 0, 0, gl.Sizei(ms.options.NumNodeCols), gl.Sizei(ms.options.NumNodeRows), gl.RGB, gl.UNSIGNED_BYTE, gl.Pointer(&gw.nodeTextureData[0])) gl.ActiveTexture(gl.TEXTURE1) for i, ent := range gw.game.Ents { p := ent.Pos() gw.nodeWarpingData[3*i+0] = byte(p.X / float64(gw.game.Dx) * 255) gw.nodeWarpingData[3*i+1] = -byte(p.Y / float64(gw.game.Dy) * 255) gw.nodeWarpingData[3*i+2] = 255 } gl.TexImage1D( gl.TEXTURE_1D, 0, gl.RGBA, gl.Sizei(len(gw.nodeWarpingData)/4), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Pointer(&gw.nodeWarpingData[0])) base.EnableShader("nodes") base.SetUniformI("nodes", "width", ms.options.NumNodeCols) base.SetUniformI("nodes", "height", ms.options.NumNodeRows) base.SetUniformI("nodes", "drains", 1) base.SetUniformI("nodes", "tex0", 0) base.SetUniformI("nodes", "tex1", 1) gl.ActiveTexture(gl.TEXTURE1) gl.BindTexture(gl.TEXTURE_1D, gw.nodeWarpingTexture) gl.ActiveTexture(gl.TEXTURE0) gl.BindTexture(gl.TEXTURE_2D, gw.nodeTextureId) texture.Render(0, dy, dx, -dy) base.EnableShader("") gl.Disable(gl.TEXTURE_2D) gl.Disable(gl.TEXTURE_1D) }
func drawScene() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.MatrixMode(gl.MODELVIEW) gl.LoadIdentity() gl.Translatef(0, 0, -3.0) gl.Rotatef(rotx, 1, 0, 0) gl.Rotatef(roty, 0, 1, 0) rotx += 0.5 roty += 0.5 gl.BindTexture(gl.TEXTURE_2D, texture) gl.Color4f(1, 1, 1, 1) gl.Begin(gl.QUADS) gl.Normal3f(0, 0, 1) gl.TexCoord2f(0, 0) gl.Vertex3f(-1, -1, 1) gl.TexCoord2f(1, 0) gl.Vertex3f(1, -1, 1) gl.TexCoord2f(1, 1) gl.Vertex3f(1, 1, 1) gl.TexCoord2f(0, 1) gl.Vertex3f(-1, 1, 1) gl.Normal3f(0, 0, -1) gl.TexCoord2f(1, 0) gl.Vertex3f(-1, -1, -1) gl.TexCoord2f(1, 1) gl.Vertex3f(-1, 1, -1) gl.TexCoord2f(0, 1) gl.Vertex3f(1, 1, -1) gl.TexCoord2f(0, 0) gl.Vertex3f(1, -1, -1) gl.Normal3f(0, 1, 0) gl.TexCoord2f(0, 1) gl.Vertex3f(-1, 1, -1) gl.TexCoord2f(0, 0) gl.Vertex3f(-1, 1, 1) gl.TexCoord2f(1, 0) gl.Vertex3f(1, 1, 1) gl.TexCoord2f(1, 1) gl.Vertex3f(1, 1, -1) gl.Normal3f(0, -1, 0) gl.TexCoord2f(1, 1) gl.Vertex3f(-1, -1, -1) gl.TexCoord2f(0, 1) gl.Vertex3f(1, -1, -1) gl.TexCoord2f(0, 0) gl.Vertex3f(1, -1, 1) gl.TexCoord2f(1, 0) gl.Vertex3f(-1, -1, 1) gl.Normal3f(1, 0, 0) gl.TexCoord2f(1, 0) gl.Vertex3f(1, -1, -1) gl.TexCoord2f(1, 1) gl.Vertex3f(1, 1, -1) gl.TexCoord2f(0, 1) gl.Vertex3f(1, 1, 1) gl.TexCoord2f(0, 0) gl.Vertex3f(1, -1, 1) gl.Normal3f(-1, 0, 0) gl.TexCoord2f(0, 0) gl.Vertex3f(-1, -1, -1) gl.TexCoord2f(1, 0) gl.Vertex3f(-1, -1, 1) gl.TexCoord2f(1, 1) gl.Vertex3f(-1, 1, 1) gl.TexCoord2f(0, 1) gl.Vertex3f(-1, 1, -1) gl.End() }
func (w *glfwBackend) Open(width, height, zoom int, fs bool, font *FontData) { if err := glfw.Init(); err != nil { panic(err) } w.font = font w.zoom = zoom w.width = width w.height = height var fwidth = width * font.CellWidth * zoom var fheight = height * font.CellHeight * zoom var twidth = fwidth var theight = fheight flag := glfw.Windowed if fs { flag = glfw.Fullscreen dm := glfw.DesktopMode() twidth = dm.W theight = dm.H } glfw.OpenWindowHint(glfw.WindowNoResize, gl.TRUE) err := glfw.OpenWindow(twidth, theight, 8, 8, 8, 8, 0, 0, flag) if err != nil { panic(err) } w.key = NOKEY glfw.SetWindowCloseCallback(func() int { w.Close(); return 0 }) glfw.SetKeyCallback(func(key, state int) { w.setKey(key, state) }) glfw.SetCharCallback(func(key, state int) { w.setKey(key, state) }) glfw.Enable(glfw.KeyRepeat) w.mouse = new(MouseData) glfw.Enable(glfw.MouseCursor) glfw.SetMousePosCallback(func(x, y int) { w.mouseMove(x, y) }) glfw.SetMouseButtonCallback(func(but, state int) { w.mousePress(but, state) }) glfw.Enable(glfw.MouseCursor) xoff := float32(twidth-fwidth) / 2.0 yoff := float32(theight-fheight) / 2.0 fc := float32(font.CellWidth * zoom) fch := float32(font.CellHeight * zoom) for y := 0; y < height; y++ { for x := 0; x < width; x++ { cx := xoff + float32(x)*fc cy := yoff + float32(y)*fch w.verts = append(w.verts, cx, cy, cx, cy+fch, cx+fc, cy+fch, cx+fc, cy) } } runtime.LockOSThread() glInit(twidth, theight) m := font.Image.(*image.RGBA) w.s = float32(font.CellWidth) / float32(m.Bounds().Max.X) w.t = float32(font.CellHeight) / float32(m.Bounds().Max.Y) textures = make([]gl.Uint, 2) gl.GenTextures(2, &textures[0]) gl.BindTexture(gl.TEXTURE_2D, textures[0]) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST) gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.Sizei(m.Bounds().Max.X), gl.Sizei(m.Bounds().Max.Y), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Pointer(&m.Pix[0])) m = image.NewRGBA(image.Rect(0, 0, font.CellWidth, font.CellHeight)) draw.Draw(m, m.Bounds(), &image.Uniform{White}, image.ZP, draw.Src) gl.BindTexture(gl.TEXTURE_2D, textures[1]) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST) gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.Sizei(m.Bounds().Max.X), gl.Sizei(m.Bounds().Max.Y), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Pointer(&m.Pix[0])) w.open = true }
func handleLoadRequest(req loadRequest) { f, _ := os.Open(req.path) im, _, err := image.Decode(f) f.Close() if err != nil { return } gray := true dx := im.Bounds().Dx() dy := im.Bounds().Dy() for i := 0; i < dx; i++ { for j := 0; j < dy; j++ { r, g, b, _ := im.At(i, j).RGBA() if r != g || g != b { gray = false break } } if !gray { break } } var canvas draw.Image var pix []byte if gray { ga := NewGrayAlpha(im.Bounds()) pix = ga.Pix canvas = ga } else { pix = memory.GetBlock(4 * req.data.dx * req.data.dy) canvas = &image.RGBA{pix, 4 * req.data.dx, im.Bounds()} } draw.Draw(canvas, im.Bounds(), im, image.Point{}, draw.Src) load_mutex.Lock() load_count += len(pix) manual_unlock := false // This prevents us from trying to send too much to opengl in a single // frame. If we go over the threshold then we hold the lock until we're // done sending data to opengl, then other requests will be free to // queue up and they will run on the next frame. if load_count < load_threshold { load_mutex.Unlock() } else { manual_unlock = true } render.Queue(func() { { gl.Enable(gl.TEXTURE_2D) gl.GenTextures(1, (*gl.Uint)(&req.data.texture)) gl.BindTexture(gl.TEXTURE_2D, req.data.texture) gl.PixelStorei(gl.UNPACK_ALIGNMENT, 1) 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) } if gray { gl.TexImage2D( gl.TEXTURE_2D, 0, gl.LUMINANCE_ALPHA, gl.Sizei(req.data.dx), gl.Sizei(req.data.dy), 0, gl.LUMINANCE_ALPHA, gl.BYTE, gl.Pointer(&pix[0])) } else { gl.TexImage2D( gl.TEXTURE_2D, 0, gl.RGBA, gl.Sizei(req.data.dx), gl.Sizei(req.data.dy), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Pointer(&pix[0])) // gl.TexImage2D(target, level, internalformat, width, height, border, format, type_, pixels) // glu.Build2DMipmaps(gl.TEXTURE_2D, gl.RGBA, req.data.dx, req.data.dy, gl.RGBA, pix) } memory.FreeBlock(pix) if manual_unlock { load_count = 0 load_mutex.Unlock() } }) }
func main() { runtime.LockOSThread() flag.Parse() if *workers <= 0 { *workers = 1 } runtime.GOMAXPROCS(*workers + 1) buildPalette() sdl.Init(sdl.INIT_VIDEO) defer sdl.Quit() if !*noVSync { sdl.GL_SetAttribute(sdl.GL_SWAP_CONTROL, 1) } if sdl.SetVideoMode(512, 512, 32, sdl.OPENGL) == nil { panic("sdl error") } sdl.WM_SetCaption("Gomandel", "Gomandel") if err := gl.Init(); err != nil { panic(err) } gl.Enable(gl.TEXTURE_2D) gl.Viewport(0, 0, 512, 512) gl.MatrixMode(gl.PROJECTION) gl.LoadIdentity() gl.Ortho(0, 512, 512, 0, -1, 1) gl.ClearColor(0, 0, 0, 0) //----------------------------------------------------------------------------- var dndDragging bool = false var dnd3 bool = false var dndStart Point var dndEnd Point initialRect := Rect{-1.5, -1.5, 3, 3} rect := initialRect tm := NewTileManager(512, 512) tm.ZoomRequest(&rect) running := true for running { for { var event interface{} select { case event = <-sdl.Events: default: } if event == nil { break } switch e := event.(type) { case sdl.QuitEvent: running = false case sdl.MouseButtonEvent: if e.Type == sdl.MOUSEBUTTONDOWN { dndDragging = true dndStart.X = gl.Int(e.X) dndStart.Y = gl.Int(e.Y) dndEnd = dndStart if e.Button == 3 { dnd3 = true } else { dndDragging = true } } else { dndDragging = false dndEnd.X = gl.Int(e.X) dndEnd.Y = gl.Int(e.Y) switch e.Button { case 1: rect = rectFromSelection(dndStart, dndEnd, 512, 512, rect) tm.ZoomRequest(&rect) case 2: rect = initialRect tm.ZoomRequest(&rect) case 3: dnd3 = false } } case sdl.MouseMotionEvent: if dnd3 { dndEnd.X = gl.Int(e.X) dndEnd.Y = gl.Int(e.Y) rect = moveRectBy(rect, dndStart, dndEnd, 512, 512) tm.MoveRequest(rect) dndStart = dndEnd } else if dndDragging { dndEnd.X = gl.Int(e.X) dndEnd.Y = gl.Int(e.Y) } } } tm.Update() gl.Clear(gl.COLOR_BUFFER_BIT) tm.Draw() gl.BindTexture(gl.TEXTURE_2D, 0) if dndDragging { drawSelection(dndStart, dndEnd) } sdl.GL_SwapBuffers() } }
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 }