func (r *GlowRenderer) Draw() (err error) { gl.UseProgram(r.shader) gl.Uniform1i(r.textureUnitLoc, 0) gl.BindBuffer(gl.ARRAY_BUFFER, r.coords) gl.EnableVertexAttribArray(r.positionLoc) gl.VertexAttribPointer(r.positionLoc, 3, gl.FLOAT, false, 5*4, gl.PtrOffset(0)) gl.EnableVertexAttribArray(r.textureLoc) gl.VertexAttribPointer(r.textureLoc, 2, gl.FLOAT, false, 5*4, gl.PtrOffset(3*4)) gl.Uniform1i(r.blurAmountLoc, int32(r.blur)) gl.Uniform1f(r.blurScaleLoc, r.scale) gl.Uniform1f(r.blurStrengthLoc, r.strength) gl.Uniform2f(r.bufferDimensionsLoc, float32(r.width), float32(r.height)) gl.BindFramebuffer(gl.FRAMEBUFFER, r.BlurFb) gl.Viewport(0, 0, int32(r.width), int32(r.height)) gl.ActiveTexture(gl.TEXTURE0) gl.BindTexture(gl.TEXTURE_2D, r.GlowTex) gl.Uniform1i(r.orientationLoc, 0) gl.DrawArrays(gl.TRIANGLE_STRIP, 0, 4) gl.BindFramebuffer(gl.FRAMEBUFFER, 0) gl.Viewport(0, 0, int32(r.oldwidth), int32(r.oldheight)) gl.BlendFunc(gl.ONE, gl.ONE) gl.BindTexture(gl.TEXTURE_2D, r.BlurTex) gl.Uniform1i(r.orientationLoc, 1) gl.DrawArrays(gl.TRIANGLE_STRIP, 0, 4) gl.BindBuffer(gl.ARRAY_BUFFER, 0) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) return nil }
func (r *BatchRenderer) Bind() error { gl.UseProgram(r.Program) gl.ActiveTexture(gl.TEXTURE0) gl.Uniform1i(r.TextureUnitLoc, 0) gl.UniformMatrix4fv(r.ProjectionLoc, 1, false, &r.Camera.Projection[0]) return nil }
func (t *Text) Draw() { if t.IsDebug { t.BoundingBox.Draw() } gl.UseProgram(t.font.program) gl.ActiveTexture(gl.TEXTURE0) gl.BindTexture(gl.TEXTURE_2D, t.font.textureID) // uniforms gl.Uniform1i(t.font.fragmentTextureUniform, 0) gl.Uniform4fv(t.font.colorUniform, 1, &t.color[0]) gl.Uniform2fv(t.font.finalPositionUniform, 1, &t.finalPosition[0]) gl.UniformMatrix4fv(t.font.orthographicMatrixUniform, 1, false, &t.font.OrthographicMatrix[0]) gl.UniformMatrix4fv(t.font.scaleMatrixUniform, 1, false, &t.scaleMatrix[0]) // draw drawCount := int32(t.RuneCount * 6) if drawCount > int32(t.eboIndexCount) { drawCount = int32(t.eboIndexCount) } if drawCount < 0 { drawCount = 0 } gl.Enable(gl.BLEND) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.BindVertexArray(t.vao) gl.DrawElements(gl.TRIANGLES, drawCount, gl.UNSIGNED_INT, nil) gl.BindVertexArray(0) gl.Disable(gl.BLEND) }
func (tr *TextRenderer) Bind() error { gl.UseProgram(tr.Program) if e := gl.GetError(); e != 0 { return fmt.Errorf("ERROR: %X", e) } gl.Uniform1i(tr.TextureUnitLoc, 0) if e := gl.GetError(); e != 0 { return fmt.Errorf("ERROR: %X", e) } gl.BindBuffer(gl.ARRAY_BUFFER, tr.VBO) if e := gl.GetError(); e != 0 { return fmt.Errorf("ERROR: %X", e) } gl.EnableVertexAttribArray(tr.PositionLoc) gl.VertexAttribPointer(tr.PositionLoc, 3, gl.FLOAT, false, 5*4, gl.PtrOffset(0)) if e := gl.GetError(); e != 0 { return fmt.Errorf("ERROR: %X", e) } gl.EnableVertexAttribArray(tr.TextureLoc) gl.VertexAttribPointer(tr.TextureLoc, 2, gl.FLOAT, false, 5*4, gl.PtrOffset(3*4)) if e := gl.GetError(); e != 0 { return fmt.Errorf("ERROR: %X", e) } gl.UniformMatrix4fv(tr.ProjectionLoc, 1, false, &tr.Renderer.Camera.Projection[0]) if e := gl.GetError(); e != 0 { return fmt.Errorf("ERROR: %X", e) } return nil }
func (rt *renderableText) render(x, y float32) { m := newScaleMatrix(rt.width, rt.height, 1) m = m.mult(newTranslationMatrix(x, y, 0)) gl.UniformMatrix4fv(modelMatrixUniform, 1, false, &m[0]) gl.Uniform1i(textureUniform, int32(rt.texture)-1) textLineMesh.drawElements() }
func renderMarker(metrics *metrics, m *game.Marker, x, y int) { switch m.State { case game.MarkerShowing: var val string switch { case m.ChainLevel > 0: val = "x" + strconv.Itoa(m.ChainLevel+1) case m.ComboLevel > 3: val = strconv.Itoa(m.ComboLevel) default: return } sc := float32(0.5) tx := -float32(len(val)-1) * sc ty := metrics.globalTranslationY + cellTranslationY*-float32(y) + easeOutCubic(m.StateProgress(metrics.fudge), 0, 0.5) tz := metrics.globalTranslationZ + cellTranslationZ/2 + 0.1 ry := metrics.globalRotationY + metrics.cellRotationY*-float32(x) yq := newAxisAngleQuaternion(yAxis, ry) qm := newQuaternionMatrix(yq.normalize()) gl.Uniform1f(brightnessUniform, 0) gl.Uniform1f(alphaUniform, easeOutCubic(m.StateProgress(metrics.fudge), 1, -1)) for _, rune := range val { text := markerRuneText[rune] m := newScaleMatrix(sc, sc, sc) m = m.mult(newTranslationMatrix(tx, ty, tz)) m = m.mult(qm) gl.UniformMatrix4fv(modelMatrixUniform, 1, false, &m[0]) gl.Uniform1i(textureUniform, int32(text.texture)-1) squareMesh.drawElements() tx++ } gl.Uniform1i(textureUniform, int32(boardTexture)-1) } }
func SetUniformI(shader, variable string, n int32) error { prog, ok := shader_progs[shader] if !ok { return fmt.Errorf("Tried to set a uniform in an unknown shader '%s'", shader) } bvariable := []byte(fmt.Sprintf("%s\x00", variable)) loc := gl.GetUniformLocation(prog, (*uint8)(unsafe.Pointer(&bvariable[0]))) gl.Uniform1i(loc, n) return nil }
// RenderString must be called on the render thread. x and y are the initial position of the pen, // in screen coordinates, and height is the height of a full line of text, in screen coordinates. func (d *Dictionary) RenderString(str string, x, y, height float64) { if str == "" { return } // No synchronization necessary because everything is run serially on the render thread anyway. if d.strs == nil { d.strs = make(map[string]strData) } data, ok := d.strs[str] if !ok { data = d.bindString(str) d.strs[str] = data } render.EnableShader("glop.font") defer render.EnableShader("") gl.ActiveTexture(gl.TEXTURE0) gl.BindTexture(gl.TEXTURE_2D, d.atlas.texture) location, _ := render.GetUniformLocation("glop.font", "tex") gl.Uniform1i(location, 0) gl.BindSampler(0, d.atlas.sampler) location, _ = render.GetUniformLocation("glop.font", "height") gl.Uniform1f(location, float32(height)) var viewport [4]int32 gl.GetIntegerv(gl.VIEWPORT, &viewport[0]) location, _ = render.GetUniformLocation("glop.font", "screen") gl.Uniform2f(location, float32(viewport[2]), float32(viewport[3])) location, _ = render.GetUniformLocation("glop.font", "pen") gl.Uniform2f(location, float32(x)+float32(viewport[0]), float32(y)+float32(viewport[1])) location, _ = render.GetUniformLocation("glop.font", "textColor") gl.Uniform3f(location, d.color[0], d.color[1], d.color[2]) gl.Enable(gl.BLEND) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.BindVertexArray(data.varrays[0]) gl.DrawArrays(gl.TRIANGLES, 0, data.count) }
func (r *EffectsRenderer) Draw() (err error) { gl.UseProgram(r.shader) gl.Uniform1i(r.textureUnitLoc, 0) gl.Uniform3f(r.colorLoc, r.Color[0], r.Color[1], r.Color[2]) gl.BindBuffer(gl.ARRAY_BUFFER, r.coords) gl.EnableVertexAttribArray(r.positionLoc) gl.VertexAttribPointer(r.positionLoc, 3, gl.FLOAT, false, 5*4, gl.PtrOffset(0)) gl.EnableVertexAttribArray(r.textureLoc) gl.VertexAttribPointer(r.textureLoc, 2, gl.FLOAT, false, 5*4, gl.PtrOffset(3*4)) gl.BindFramebuffer(gl.FRAMEBUFFER, 0) gl.Viewport(0, 0, int32(r.oldwidth), int32(r.oldheight)) gl.BlendFunc(gl.ONE, gl.ONE) gl.BindTexture(gl.TEXTURE_2D, r.texture) gl.DrawArrays(gl.TRIANGLE_STRIP, 0, 4) gl.BindBuffer(gl.ARRAY_BUFFER, 0) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) return nil }
func bindAggregateImage(img image.Image, idx int) uint32 { newIdx := idx if rgba, ok := img.(*image.RGBA); ok { gl.GenTextures(1, &textures[newIdx]) gl.ActiveTexture(gl.TEXTURE0 + uint32(newIdx)) gl.BindTexture(gl.TEXTURE_2D, textures[newIdx]) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_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, int32(rgba.Rect.Size().X), int32(rgba.Rect.Size().Y), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(rgba.Pix)) gl.GenerateMipmap(gl.TEXTURE_2D) idxS := strconv.Itoa(newIdx) fmt.Println("idx: ", int32(newIdx), "myTextureSampler["+idxS+"]\x00") gl.Uniform1i(gl.GetUniformLocation(program, gl.Str("myTextureSampler["+idxS+"]\x00")), int32(newIdx)) if ok := gl.GetError(); ok != gl.NO_ERROR { fmt.Println("1- Cannot load Image in location: ./: ", ok) os.Exit(-1) } return textures[newIdx] } else { fmt.Println("Image not RGBA at location: ./") os.Exit(-1) } return 0 }
func renderBoard(g *game.Game, fudge float32) bool { if g.Board == nil { return false } b := g.Board metrics := newMetrics(g, fudge) gl.UniformMatrix4fv(projectionViewMatrixUniform, 1, false, &perspectiveProjectionViewMatrix[0]) gl.Uniform3fv(mixColorUniform, 1, &blackColor[0]) globalGrayscale := float32(1) globalDarkness := float32(0.8) var boardDarkness float32 gameEase := func(start, change float32) float32 { return easeOutCubic(g.StateProgress(fudge), start, change) } boardEase := func(start, change float32) float32 { return easeOutCubic(b.StateProgress(fudge), start, change) } switch g.State { case game.GamePlaying: globalGrayscale = gameEase(1, -1) globalDarkness = gameEase(0.8, -0.8) case game.GamePaused: globalGrayscale = gameEase(0, 1) globalDarkness = gameEase(0, 0.8) case game.GameExiting: globalGrayscale = 1 globalDarkness = gameEase(0.8, 1) } switch b.State { case game.BoardEntering: boardDarkness = boardEase(1, -1) case game.BoardExiting: boardDarkness = boardEase(0, 1) } finalDarkness := globalDarkness if finalDarkness < boardDarkness { finalDarkness = boardDarkness } gl.Uniform1f(mixAmountUniform, finalDarkness) gl.Uniform1i(textureUniform, int32(boardTexture)-1) for i := 0; i <= 2; i++ { gl.Uniform1f(grayscaleUniform, globalGrayscale) gl.Uniform1f(brightnessUniform, 0) gl.Uniform1f(alphaUniform, 1) if i == 0 { renderSelector(metrics) } for y, r := range b.Rings { for x, c := range r.Cells { switch i { case 0: // draw opaque objects switch c.Block.State { case game.BlockStatic, game.BlockSwappingFromLeft, game.BlockSwappingFromRight, game.BlockDroppingFromAbove, game.BlockFlashing: renderCellBlock(metrics, c, x, y) case game.BlockCracking, game.BlockCracked: renderCellFragments(metrics, c, x, y) } case 1: // draw transparent objects switch c.Block.State { case game.BlockExploding: renderCellFragments(metrics, c, x, y) } renderMarker(metrics, c.Marker, x, y) } } } } // Render the spare rings. // Set brightness to zero for all spare rings. gl.Uniform1f(brightnessUniform, 0) for y, r := range b.SpareRings { // Set grayscale value. First spare rings becomes colored. Rest are gray. grayscale := float32(1) if y == 0 { grayscale = easeInExpo(b.RiseProgress(fudge), 1, -1) } if grayscale < globalGrayscale { grayscale = globalGrayscale } gl.Uniform1f(grayscaleUniform, grayscale) // Set alpha value. Last spare ring fades in. Rest are opaque. alpha := float32(1) if y == len(b.SpareRings)-1 { alpha = easeInExpo(b.RiseProgress(fudge), 0, 1) } gl.Uniform1f(alphaUniform, alpha) // Render the spare rings below the normal rings. for x, c := range r.Cells { renderCellBlock(metrics, c, x, y+b.RingCount) } } return true }
func (tr *SpriteRenderer) Draw(instances []SpriteConfig) error { var ( bytesNeeded int byteoffset int count int32 data unsafe.Pointer float float32 = 0 floatSize uint32 i uint32 offset unsafe.Pointer sprite SpriteConfig stride int32 ) floatSize = uint32(unsafe.Sizeof(float)) stride = int32(unsafe.Sizeof(sprite)) gl.UseProgram(tr.program) gl.Uniform1i(tr.textureUnitLoc, 0) // Instance data binding gl.BindBuffer(gl.ARRAY_BUFFER, tr.instanceVBO) count = int32(len(instances)) bytesNeeded = int(stride * count) data = gl.Ptr(instances) if bytesNeeded > tr.instanceBytes { gl.BufferData(gl.ARRAY_BUFFER, bytesNeeded, data, gl.STREAM_DRAW) tr.instanceBytes = bytesNeeded } else { gl.BufferSubData(gl.ARRAY_BUFFER, 0, bytesNeeded, data) } gl.EnableVertexAttribArray(tr.translationLoc) gl.VertexAttribPointer(tr.translationLoc, 3, gl.FLOAT, false, stride, tr.offAttrX) gl.VertexAttribDivisor(tr.translationLoc, 1) gl.EnableVertexAttribArray(tr.rotationLoc) gl.VertexAttribPointer(tr.rotationLoc, 3, gl.FLOAT, false, stride, tr.offAttrRotationX) gl.VertexAttribDivisor(tr.rotationLoc, 1) gl.EnableVertexAttribArray(tr.scaleLoc) gl.VertexAttribPointer(tr.scaleLoc, 3, gl.FLOAT, false, stride, tr.offAttrScaleX) gl.VertexAttribDivisor(tr.scaleLoc, 1) gl.EnableVertexAttribArray(tr.colorLoc) gl.VertexAttribPointer(tr.colorLoc, 4, gl.FLOAT, false, stride, tr.offAttrColor) gl.VertexAttribDivisor(tr.colorLoc, 1) for i = 0; i < 4; i++ { byteoffset = int(i * 4 * floatSize) offset = gl.PtrOffset(tr.offAttrPointAdj + byteoffset) gl.EnableVertexAttribArray(tr.pointAdjLoc + i) gl.VertexAttribPointer(tr.pointAdjLoc+i, 4, gl.FLOAT, false, stride, offset) gl.VertexAttribDivisor(tr.pointAdjLoc+i, 1) offset = gl.PtrOffset(tr.offAttrTextureAdj + byteoffset) gl.EnableVertexAttribArray(tr.textureAdjLoc + i) gl.VertexAttribPointer(tr.textureAdjLoc+i, 4, gl.FLOAT, false, stride, offset) gl.VertexAttribDivisor(tr.textureAdjLoc+i, 1) } // Projection gl.UniformMatrix4fv(tr.projectionLoc, 1, false, &tr.Renderer.Camera.Projection[0]) // Actually draw. gl.DrawArraysInstanced(gl.TRIANGLES, 0, 6, int32(len(instances))) // Undo instance attr repetition. gl.VertexAttribDivisor(tr.translationLoc, 0) gl.VertexAttribDivisor(tr.rotationLoc, 0) gl.VertexAttribDivisor(tr.scaleLoc, 0) gl.VertexAttribDivisor(tr.colorLoc, 0) for i = 0; i < 4; i++ { gl.VertexAttribDivisor(tr.pointAdjLoc+i, 0) gl.VertexAttribDivisor(tr.textureAdjLoc+i, 0) } gl.BindBuffer(gl.ARRAY_BUFFER, 0) return nil }
func (r *Renderable) Draw(perspective mgl.Mat4, view mgl.Mat4) { gl.UseProgram(r.Shader) gl.BindVertexArray(r.Vao) model := r.GetTransformMat4() var mvp mgl.Mat4 shaderMvp := getUniformLocation(r.Shader, "MVP_MATRIX") if shaderMvp >= 0 { mvp = perspective.Mul4(view).Mul4(model) gl.UniformMatrix4fv(shaderMvp, 1, false, &mvp[0]) } shaderMv := getUniformLocation(r.Shader, "MV_MATRIX") if shaderMv >= 0 { mv := view.Mul4(model) gl.UniformMatrix4fv(shaderMv, 1, false, &mv[0]) } shaderTex0 := getUniformLocation(r.Shader, "DIFFUSE_TEX") if shaderTex0 >= 0 { gl.ActiveTexture(gl.TEXTURE0) gl.BindTexture(gl.TEXTURE_2D, r.Tex0) gl.Uniform1i(shaderTex0, 0) } shaderColor := getUniformLocation(r.Shader, "MATERIAL_DIFFUSE") if shaderColor >= 0 { gl.Uniform4f(shaderColor, r.Color[0], r.Color[1], r.Color[2], r.Color[3]) } shaderTex1 := getUniformLocation(r.Shader, "MATERIAL_TEX_0") if shaderTex1 >= 0 { gl.ActiveTexture(gl.TEXTURE0) gl.BindTexture(gl.TEXTURE_2D, r.Tex0) gl.Uniform1i(shaderTex1, 0) } shaderCameraWorldPos := getUniformLocation(r.Shader, "CAMERA_WORLD_POSITION") if shaderCameraWorldPos >= 0 { gl.Uniform3f(shaderCameraWorldPos, -view[12], -view[13], -view[14]) } shaderPosition := getAttribLocation(r.Shader, "VERTEX_POSITION") if shaderPosition >= 0 { gl.BindBuffer(gl.ARRAY_BUFFER, r.VertVBO) gl.EnableVertexAttribArray(uint32(shaderPosition)) gl.VertexAttribPointer(uint32(shaderPosition), 3, gl.FLOAT, false, 0, gl.PtrOffset(0)) } shaderNormal := getAttribLocation(r.Shader, "VERTEX_NORMAL") if shaderNormal >= 0 { gl.BindBuffer(gl.ARRAY_BUFFER, r.NormsVBO) gl.EnableVertexAttribArray(uint32(shaderNormal)) gl.VertexAttribPointer(uint32(shaderNormal), 3, gl.FLOAT, false, 0, gl.PtrOffset(0)) } shaderVertUv := getAttribLocation(r.Shader, "VERTEX_UV_0") if shaderVertUv >= 0 { gl.BindBuffer(gl.ARRAY_BUFFER, r.UvVBO) gl.EnableVertexAttribArray(uint32(shaderVertUv)) gl.VertexAttribPointer(uint32(shaderVertUv), 2, gl.FLOAT, false, 0, gl.PtrOffset(0)) } gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, r.ElementsVBO) gl.DrawElements(gl.TRIANGLES, int32(r.FaceCount*3), gl.UNSIGNED_INT, gl.PtrOffset(0)) gl.BindVertexArray(0) }