move to vertex array objects
This commit is contained in:
42
include/llmr/geometry/vao.hpp
Normal file
42
include/llmr/geometry/vao.hpp
Normal file
@@ -0,0 +1,42 @@
|
||||
#ifndef LLMR_GEOMETRY_VAO
|
||||
#define LLMR_GEOMETRY_VAO
|
||||
|
||||
#include <llmr/platform/gl.hpp>
|
||||
|
||||
#include <cassert>
|
||||
|
||||
namespace llmr {
|
||||
|
||||
class VertexArrayObject {
|
||||
public:
|
||||
template <typename Shader, typename VertexBuffer>
|
||||
inline void setup(Shader& shader, VertexBuffer& vertex_buffer, char *vertex_offset) {
|
||||
glGenVertexArrays(1, &vao);
|
||||
glBindVertexArray(vao);
|
||||
|
||||
vertex_buffer.bind();
|
||||
shader.bind_vertex(vertex_offset);
|
||||
}
|
||||
|
||||
inline ~VertexArrayObject() {
|
||||
if (vao) {
|
||||
glDeleteVertexArrays(1, &vao);
|
||||
}
|
||||
}
|
||||
|
||||
inline operator bool() const {
|
||||
return vao != 0;
|
||||
}
|
||||
|
||||
inline void bind() {
|
||||
assert(vao);
|
||||
glBindVertexArray(vao);
|
||||
}
|
||||
|
||||
private:
|
||||
GLuint vao = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -2,6 +2,8 @@
|
||||
#define LLMR_MAP_TILE
|
||||
|
||||
#include <llmr/util/vec.hpp>
|
||||
#include <llmr/geometry/debug_font_buffer.hpp>
|
||||
#include <llmr/geometry/vao.hpp>
|
||||
|
||||
#include <cstdint>
|
||||
#include <forward_list>
|
||||
@@ -18,7 +20,6 @@ class LayerDescription;
|
||||
class BucketDescription;
|
||||
class VectorTile;
|
||||
class VectorTileLayer;
|
||||
class DebugFontBuffer;
|
||||
class FillBuffer;
|
||||
class LineBuffer;
|
||||
|
||||
@@ -71,7 +72,9 @@ public:
|
||||
state state;
|
||||
|
||||
// Holds the actual geometries in this tile.
|
||||
std::shared_ptr<DebugFontBuffer> debugFontBuffer;
|
||||
DebugFontBuffer debugFontBuffer;
|
||||
VertexArrayObject debugFontArray;
|
||||
|
||||
std::shared_ptr<FillBuffer> fillBuffer;
|
||||
std::shared_ptr<LineBuffer> lineBuffer;
|
||||
|
||||
|
||||
@@ -8,6 +8,9 @@
|
||||
#if TARGET_OS_IPHONE
|
||||
#include <OpenGLES/ES2/gl.h>
|
||||
#include <OpenGLES/ES2/glext.h>
|
||||
#define glGenVertexArrays glGenVertexArraysOES
|
||||
#define glBindVertexArray glBindVertexArrayOES
|
||||
#define glDeleteVertexArrays glDeleteVertexArraysOES
|
||||
#elif TARGET_IPHONE_SIMULATOR
|
||||
#include <OpenGLES/ES2/gl.h>
|
||||
#include <OpenGLES/ES2/glext.h>
|
||||
|
||||
@@ -5,8 +5,8 @@
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
#define kTileURL "http://mapbox:magic@kkaefer.net/gl/tiles/plain/%d-%d-%d.vector.pbf"
|
||||
#define kSpriteURL "http://mapbox:magic@kkaefer.net/gl/debug/img/sprite"
|
||||
#define kTileURL "http://pepper.local:3333/gl/tiles/plain/%d-%d-%d.vector.pbf"
|
||||
#define kSpriteURL "http://pepper.local:3333/gl/debug/img/sprite"
|
||||
|
||||
namespace llmr {
|
||||
|
||||
|
||||
@@ -3,10 +3,16 @@
|
||||
|
||||
#include "bucket.hpp"
|
||||
#include <llmr/style/bucket_description.hpp>
|
||||
#include <llmr/geometry/vao.hpp>
|
||||
#include <llmr/geometry/fill_buffer.hpp>
|
||||
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
#ifndef BUFFER_OFFSET
|
||||
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
|
||||
#endif
|
||||
|
||||
namespace llmr {
|
||||
|
||||
class Style;
|
||||
@@ -24,8 +30,9 @@ public:
|
||||
void addGeometry(pbf& data);
|
||||
void addGeometry(const std::vector<Coordinate>& line);
|
||||
uint32_t size() const;
|
||||
void drawElements(int32_t attrib);
|
||||
void drawVertices(int32_t attrib);
|
||||
|
||||
template <typename Shader> void drawElements(Shader& shader);
|
||||
template <typename Shader> void drawVertices(Shader& shader);
|
||||
|
||||
public:
|
||||
const BucketGeometryDescription geom_desc;
|
||||
@@ -34,6 +41,7 @@ private:
|
||||
std::shared_ptr<FillBuffer> buffer;
|
||||
|
||||
struct group {
|
||||
VertexArrayObject array;
|
||||
uint32_t vertex_length;
|
||||
uint32_t elements_length;
|
||||
|
||||
@@ -48,9 +56,40 @@ private:
|
||||
uint32_t vertex_start;
|
||||
uint32_t elements_start;
|
||||
uint32_t length;
|
||||
VertexArrayObject array;
|
||||
std::vector<group> groups;
|
||||
};
|
||||
|
||||
|
||||
template <typename Shader>
|
||||
void FillBucket::drawElements(Shader& shader) {
|
||||
char *vertex_index = BUFFER_OFFSET(vertex_start * 2 * sizeof(int16_t));
|
||||
char *elements_index = BUFFER_OFFSET(elements_start * 3 * sizeof(int16_t));
|
||||
for (group& group : groups) {
|
||||
if (!group.array) {
|
||||
group.array.setup(shader, *buffer, vertex_index);
|
||||
} else {
|
||||
group.array.bind();
|
||||
}
|
||||
glDrawElements(GL_TRIANGLES, group.elements_length * 3 - 3, GL_UNSIGNED_SHORT, elements_index);
|
||||
vertex_index += group.vertex_length * 2 * sizeof(uint16_t);
|
||||
elements_index += group.elements_length * 3 * sizeof(uint16_t);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Shader>
|
||||
void FillBucket::drawVertices(Shader& shader) {
|
||||
// Draw the entire line
|
||||
if (!array) {
|
||||
char *vertex_index = BUFFER_OFFSET(vertex_start * 2 * sizeof(int16_t));
|
||||
array.setup(shader, *buffer, vertex_index);
|
||||
} else {
|
||||
array.bind();
|
||||
}
|
||||
glDrawArrays(GL_LINE_STRIP, 0, length);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include "bucket.hpp"
|
||||
#include <llmr/style/bucket_description.hpp>
|
||||
#include <llmr/geometry/vao.hpp>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
@@ -10,6 +11,7 @@ namespace llmr {
|
||||
|
||||
class Style;
|
||||
class LineBuffer;
|
||||
class LineShader;
|
||||
struct Coordinate;
|
||||
struct pbf;
|
||||
|
||||
@@ -21,10 +23,12 @@ public:
|
||||
|
||||
void addGeometry(pbf& data);
|
||||
void addGeometry(const std::vector<Coordinate>& line);
|
||||
uint32_t size() const;
|
||||
|
||||
void bind();
|
||||
char *vertexOffset() const;
|
||||
size_t size() const;
|
||||
|
||||
void bind(LineShader& shader);
|
||||
void drawLines();
|
||||
void drawPoints();
|
||||
|
||||
public:
|
||||
const BucketGeometryDescription geometry;
|
||||
@@ -33,6 +37,7 @@ private:
|
||||
std::shared_ptr<LineBuffer> buffer;
|
||||
uint32_t start;
|
||||
uint32_t length;
|
||||
VertexArrayObject array;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define LLMR_RENDERER_PAINTER
|
||||
|
||||
#include <llmr/map/tile.hpp>
|
||||
#include <llmr/geometry/vao.hpp>
|
||||
#include <llmr/geometry/vertex_buffer.hpp>
|
||||
#include <llmr/util/mat4.hpp>
|
||||
|
||||
@@ -86,6 +87,8 @@ private:
|
||||
4096, 4096
|
||||
};
|
||||
|
||||
VertexArrayObject tileStencilArray;
|
||||
|
||||
// Set up the tile boundary lines we're using to draw the tile outlines.
|
||||
VertexBuffer tileBorderBuffer = {
|
||||
0, 0,
|
||||
@@ -95,6 +98,8 @@ private:
|
||||
0, 0
|
||||
};
|
||||
|
||||
VertexArrayObject tileBorderArray;
|
||||
|
||||
// Set up the matte buffer we're using to draw the filling background.
|
||||
VertexBuffer matteBuffer = {
|
||||
// top left triangle
|
||||
@@ -108,6 +113,7 @@ private:
|
||||
16384, 16384
|
||||
};
|
||||
|
||||
VertexArrayObject matteArray;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -9,6 +9,8 @@ class FillShader : public Shader {
|
||||
public:
|
||||
FillShader();
|
||||
|
||||
void bind_vertex(char *offset);
|
||||
|
||||
int32_t a_pos;
|
||||
int32_t u_matrix;
|
||||
int32_t u_color;
|
||||
|
||||
@@ -9,6 +9,8 @@ class LineShader : public Shader {
|
||||
public:
|
||||
LineShader();
|
||||
|
||||
void bind_vertex(char *offset);
|
||||
|
||||
int32_t a_pos;
|
||||
int32_t a_extrude;
|
||||
int32_t a_linesofar;
|
||||
|
||||
@@ -9,6 +9,8 @@ class OutlineShader : public Shader {
|
||||
public:
|
||||
OutlineShader();
|
||||
|
||||
void bind_vertex(char *offset);
|
||||
|
||||
int32_t a_pos;
|
||||
int32_t u_matrix;
|
||||
int32_t u_color;
|
||||
|
||||
@@ -9,6 +9,8 @@ class PlainShader : public Shader {
|
||||
public:
|
||||
PlainShader();
|
||||
|
||||
void bind_vertex(char *offset);
|
||||
|
||||
int32_t a_pos;
|
||||
int32_t u_matrix;
|
||||
int32_t u_color;
|
||||
|
||||
16
include/llmr/util/std.hpp
Normal file
16
include/llmr/util/std.hpp
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef LLMR_UTIL_STD
|
||||
#define LLMR_UTIL_STD
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace std {
|
||||
|
||||
// C++14 backfill from http://herbsutter.com/gotw/_102/
|
||||
template<typename T, typename ...Args>
|
||||
::std::unique_ptr<T> make_unique(Args&& ...args) {
|
||||
return ::std::unique_ptr<T>(new T(::std::forward<Args>(args)...));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user