// NewTileTextureMapRenderable returns a new instance of a renderable for tile maps func NewTileTextureMapRenderable(gl opengl.OpenGl, paletteTexture graphics.Texture, textureQuery TextureQuery) *TileTextureMapRenderable { vertexShader, err1 := opengl.CompileNewShader(gl, opengl.VERTEX_SHADER, mapTileVertexShaderSource) defer gl.DeleteShader(vertexShader) fragmentShader, err2 := opengl.CompileNewShader(gl, opengl.FRAGMENT_SHADER, mapTileFragmentShaderSource) defer gl.DeleteShader(fragmentShader) program, _ := opengl.LinkNewProgram(gl, vertexShader, fragmentShader) if err1 != nil { fmt.Fprintf(os.Stderr, "Failed to compile shader 1:\n", err1) } if err2 != nil { fmt.Fprintf(os.Stderr, "Failed to compile shader 2:\n", err2) } renderable := &TileTextureMapRenderable{ gl: gl, program: program, vertexArrayObject: gl.GenVertexArrays(1)[0], vertexPositionBuffer: gl.GenBuffers(1)[0], vertexPositionAttrib: gl.GetAttribLocation(program, "vertexPosition"), modelMatrixUniform: gl.GetUniformLocation(program, "modelMatrix"), viewMatrixUniform: gl.GetUniformLocation(program, "viewMatrix"), projectionMatrixUniform: gl.GetUniformLocation(program, "projectionMatrix"), uvMatrixUniform: gl.GetUniformLocation(program, "uvMatrix"), paletteUniform: gl.GetUniformLocation(program, "palette"), bitmapUniform: gl.GetUniformLocation(program, "bitmap"), paletteTexture: paletteTexture, textureQuery: textureQuery, tiles: make([][]*model.TileProperties, 64), lastTileType: model.Solid} for i := 0; i < 64; i++ { renderable.tiles[i] = make([]*model.TileProperties, 64) } renderable.withShader(func() { gl.BindBuffer(opengl.ARRAY_BUFFER, renderable.vertexPositionBuffer) limit := float32(1.0) var vertices = []float32{ 0.0, 0.0, 0.0, limit, 0.0, 0.0, limit, limit, 0.0, limit, limit, 0.0, 0.0, limit, 0.0, 0.0, 0.0, 0.0} gl.BufferData(opengl.ARRAY_BUFFER, len(vertices)*4, vertices, opengl.STATIC_DRAW) }) return renderable }
// NewTextureRenderable returns a new instance of a texture renderable func NewTextureRenderable(gl opengl.OpenGl, positionX, positionY float32, displaySize float32, paletteTexture graphics.Texture, bitmapTexture graphics.Texture) *TextureRenderable { vertexShader, err1 := opengl.CompileNewShader(gl, opengl.VERTEX_SHADER, textureVertexShaderSource) defer gl.DeleteShader(vertexShader) fragmentShader, err2 := opengl.CompileNewShader(gl, opengl.FRAGMENT_SHADER, textureFragmentShaderSource) defer gl.DeleteShader(fragmentShader) program, _ := opengl.LinkNewProgram(gl, vertexShader, fragmentShader) if err1 != nil { fmt.Fprintf(os.Stderr, "Failed to compile shader 1:\n", err1) } if err2 != nil { fmt.Fprintf(os.Stderr, "Failed to compile shader 2:\n", err2) } renderable := &TextureRenderable{ gl: gl, program: program, modelMatrix: mgl.Ident4(). Mul4(mgl.Translate3D(positionX, positionY, 0.0)). Mul4(mgl.Scale3D(displaySize, displaySize, 1.0)), vertexArrayObject: gl.GenVertexArrays(1)[0], vertexPositionBuffer: gl.GenBuffers(1)[0], vertexPositionAttrib: gl.GetAttribLocation(program, "vertexPosition"), modelMatrixUniform: gl.GetUniformLocation(program, "modelMatrix"), viewMatrixUniform: gl.GetUniformLocation(program, "viewMatrix"), projectionMatrixUniform: gl.GetUniformLocation(program, "projectionMatrix"), paletteTexture: paletteTexture, paletteUniform: gl.GetUniformLocation(program, "palette"), bitmapTexture: bitmapTexture, bitmapUniform: gl.GetUniformLocation(program, "bitmap")} renderable.withShader(func() { gl.BindBuffer(opengl.ARRAY_BUFFER, renderable.vertexPositionBuffer) limit := float32(1.0) var vertices = []float32{ 0.0, 0.0, 0.0, limit, 0.0, 0.0, limit, limit, 0.0, limit, limit, 0.0, 0.0, limit, 0.0, 0.0, 0.0, 0.0} gl.BufferData(opengl.ARRAY_BUFFER, len(vertices)*4, vertices, opengl.STATIC_DRAW) }) return renderable }
// NewSimpleBitmapRenderable returns a new instance of a simple bitmap renderable func NewSimpleBitmapRenderable(gl opengl.OpenGl, paletteTexture graphics.Texture) *SimpleBitmapRenderable { vertexShader, err1 := opengl.CompileNewShader(gl, opengl.VERTEX_SHADER, simpleBitmapVertexShaderSource) defer gl.DeleteShader(vertexShader) fragmentShader, err2 := opengl.CompileNewShader(gl, opengl.FRAGMENT_SHADER, simpleBitmapFragmentShaderSource) defer gl.DeleteShader(fragmentShader) program, _ := opengl.LinkNewProgram(gl, vertexShader, fragmentShader) if err1 != nil { fmt.Fprintf(os.Stderr, "Failed to compile shader 1:\n", err1) } if err2 != nil { fmt.Fprintf(os.Stderr, "Failed to compile shader 2:\n", err2) } renderable := &SimpleBitmapRenderable{ gl: gl, program: program, vertexArrayObject: gl.GenVertexArrays(1)[0], vertexPositionBuffer: gl.GenBuffers(1)[0], vertexPositionAttrib: gl.GetAttribLocation(program, "vertexPosition"), uvPositionBuffer: gl.GenBuffers(1)[0], uvPositionAttrib: gl.GetAttribLocation(program, "uvPosition"), modelMatrixUniform: gl.GetUniformLocation(program, "modelMatrix"), viewMatrixUniform: gl.GetUniformLocation(program, "viewMatrix"), projectionMatrixUniform: gl.GetUniformLocation(program, "projectionMatrix"), paletteTexture: paletteTexture, paletteUniform: gl.GetUniformLocation(program, "palette"), bitmapUniform: gl.GetUniformLocation(program, "bitmap")} renderable.withShader(func() { gl := renderable.gl half := float32(0.5) var vertices = []float32{ -half, -half, 0.0, half, -half, 0.0, half, half, 0.0, half, half, 0.0, -half, half, 0.0, -half, -half, 0.0} gl.BindBuffer(opengl.ARRAY_BUFFER, renderable.vertexPositionBuffer) gl.BufferData(opengl.ARRAY_BUFFER, len(vertices)*4, vertices, opengl.STATIC_DRAW) }) return renderable }
// NewBasicHighlighter returns a new instance of BasicHighlighter. func NewBasicHighlighter(gl opengl.OpenGl, color [4]float32) *BasicHighlighter { vertexShader, err1 := opengl.CompileNewShader(gl, opengl.VERTEX_SHADER, basicHighlighterVertexShaderSource) defer gl.DeleteShader(vertexShader) fragmentShader, err2 := opengl.CompileNewShader(gl, opengl.FRAGMENT_SHADER, basicHighlighterFragmentShaderSource) defer gl.DeleteShader(fragmentShader) program, _ := opengl.LinkNewProgram(gl, vertexShader, fragmentShader) if err1 != nil { fmt.Fprintf(os.Stderr, "Failed to compile shader 1:\n", err1) } if err2 != nil { fmt.Fprintf(os.Stderr, "Failed to compile shader 2:\n", err2) } highlighter := &BasicHighlighter{ gl: gl, program: program, vertexArrayObject: gl.GenVertexArrays(1)[0], vertexPositionBuffer: gl.GenBuffers(1)[0], vertexPositionAttrib: gl.GetAttribLocation(program, "vertexPosition"), modelMatrixUniform: gl.GetUniformLocation(program, "modelMatrix"), viewMatrixUniform: gl.GetUniformLocation(program, "viewMatrix"), projectionMatrixUniform: gl.GetUniformLocation(program, "projectionMatrix"), inColorUniform: gl.GetUniformLocation(program, "inColor")} highlighter.withShader(func() { gl.BindBuffer(opengl.ARRAY_BUFFER, highlighter.vertexPositionBuffer) half := float32(0.5) var vertices = []float32{ -half, -half, 0.0, half, -half, 0.0, half, half, 0.0, half, half, 0.0, -half, half, 0.0, -half, -half, 0.0} gl.BufferData(opengl.ARRAY_BUFFER, len(vertices)*4, vertices, opengl.STATIC_DRAW) gl.Uniform4fv(highlighter.inColorUniform, &color) }) return highlighter }
// NewTileSelectionRenderable returns a new instance of a renderable for tile selections func NewTileSelectionRenderable(gl opengl.OpenGl, query TileSelectionQuery) *TileSelectionRenderable { vertexShader, err1 := opengl.CompileNewShader(gl, opengl.VERTEX_SHADER, mapTileSelectionVertexShaderSource) defer gl.DeleteShader(vertexShader) fragmentShader, err2 := opengl.CompileNewShader(gl, opengl.FRAGMENT_SHADER, mapTileSelectionFragmentShaderSource) defer gl.DeleteShader(fragmentShader) program, _ := opengl.LinkNewProgram(gl, vertexShader, fragmentShader) if err1 != nil { fmt.Fprintf(os.Stderr, "Failed to compile shader 1:\n", err1) } if err2 != nil { fmt.Fprintf(os.Stderr, "Failed to compile shader 2:\n", err2) } renderable := &TileSelectionRenderable{ gl: gl, program: program, vertexArrayObject: gl.GenVertexArrays(1)[0], vertexPositionBuffer: gl.GenBuffers(1)[0], vertexPositionAttrib: gl.GetAttribLocation(program, "vertexPosition"), modelMatrixUniform: gl.GetUniformLocation(program, "modelMatrix"), viewMatrixUniform: gl.GetUniformLocation(program, "viewMatrix"), projectionMatrixUniform: gl.GetUniformLocation(program, "projectionMatrix"), query: query} renderable.withShader(func() { limit := float32(32.0) var vertices = []float32{ 0.0, 0.0, 0.0, limit, 0.0, 0.0, limit, limit, 0.0, limit, limit, 0.0, 0.0, limit, 0.0, 0.0, 0.0, 0.0} gl.BindBuffer(opengl.ARRAY_BUFFER, renderable.vertexPositionBuffer) gl.BufferData(opengl.ARRAY_BUFFER, len(vertices)*4, vertices, opengl.STATIC_DRAW) }) return renderable }