add LineBuffer and change DebugFontBuffer naming

This commit is contained in:
Konstantin Käfer
2014-01-28 11:47:21 +01:00
parent a6434168b3
commit edf2a532af
10 changed files with 132 additions and 92 deletions

View File

@@ -5,10 +5,10 @@
namespace llmr {
class debug_font_buffer {
class DebugFontBuffer {
public:
debug_font_buffer();
~debug_font_buffer();
DebugFontBuffer();
~DebugFontBuffer();
void addText(const char *text, double left, double baseline, double scale = 1);
void clear();

View File

@@ -0,0 +1,65 @@
#ifndef LLMR_GEOMETRY_LINE_BUFFER
#define LLMR_GEOMETRY_LINE_BUFFER
#include <vector>
#include <cmath>
namespace llmr {
class LineBuffer {
public:
~LineBuffer();
/*
* Scale the extrusion vector so that the normal length is this value.
* Contains the "texture" normals (-1..1). This is distinct from the extrude
* normals for line joins, because the x-value remains 0 for the texture
* normal array, while the extrude normal actually moves the vertex to create
* the acute/bevelled line join.
*/
const int8_t extrudeScale = 63;
/*
* Add a vertex to this buffer
*
* @param {number} x vertex position
* @param {number} y vertex position
* @param {number} ex extrude normal
* @param {number} ey extrude normal
* @param {number} tx texture normal
* @param {number} ty texture normal
*/
void add(int16_t x, int16_t y, float ex, float ey, int8_t tx, int8_t ty, int32_t linesofar = 0);
/*
* Add a degenerate triangle to the buffer
*
* > So we need a way to get from the end of one triangle strip
* to the beginning of the next strip without actually filling triangles
* on the way. We can do this with "degenerate" triangles: We simply
* repeat the last coordinate of the first triangle strip and the first
* coordinate of the next triangle strip.
*/
void addDegenerate();
/*
* Returns the number of elements in this buffer. This is not the number of
* bytes, but rather the number of coordinates with associated information.
*/
uint32_t length() const;
/*
* Transfers this buffer to the GPU and binds the buffer to the GL context.
*/
void bind();
private:
bool dirty = true;
std::vector<int16_t> array;
uint32_t buffer = 0;
};
}
#endif

View File

@@ -1,26 +0,0 @@
#ifndef LLMR_GEOMETRY_LINEVERTEXBUFFER
#define LLMR_GEOMETRY_LINEVERTEXBUFFER
#include <vector>
namespace llmr {
class linevertexbuffer {
public:
linevertexbuffer();
~linevertexbuffer();
void addDegenerate();
void addCoordinate(int16_t x, int16_t y);
uint32_t length();
void bind();
private:
std::vector<int16_t> array;
uint32_t buffer;
};
}
#endif

View File

@@ -2,8 +2,8 @@
#define LLMR_MAP_TILE
#include "../geometry/debug_font_buffer.hpp"
#include "../geometry/linevertexbuffer.hpp"
#include "../geometry/fill_buffer.hpp"
#include "../geometry/line_buffer.hpp"
#include "layer.hpp"
@@ -72,9 +72,9 @@ public:
state state;
// Holds the actual geometries in this tile.
std::shared_ptr<linevertexbuffer> lineVertex;
std::shared_ptr<debug_font_buffer> debugFontVertex;
std::shared_ptr<DebugFontBuffer> debugFontBuffer;
std::shared_ptr<FillBuffer> fillBuffer;
std::shared_ptr<LineBuffer> lineBuffer;
// Holds the buckets of this tile.
// They contain the location offsets in the buffers stored above

View File

@@ -1,7 +1,7 @@
SET(llmr_SOURCES
geometry/debug_font_buffer.cpp
geometry/linevertexbuffer.cpp
geometry/fill_buffer.cpp
geometry/line_buffer.cpp
map/map.cpp
map/tile.cpp
map/transform.cpp
@@ -24,8 +24,8 @@ SET(llmr_SOURCES
SET(llmr_HEADERS
../include/llmr/geometry/debug_font_buffer.hpp
../include/llmr/geometry/linevertexbuffer.hpp
../include/llmr/geometry/fill_buffer.hpp
../include/llmr/geometry/line_buffer.hpp
../include/llmr/llmr.hpp
../include/llmr/map/map.hpp
../include/llmr/map/tile.hpp

View File

@@ -6,20 +6,20 @@
using namespace llmr;
debug_font_buffer::debug_font_buffer()
DebugFontBuffer::DebugFontBuffer()
: buffer(0),
dirty(true) {
}
debug_font_buffer::~debug_font_buffer() {
DebugFontBuffer::~DebugFontBuffer() {
if (buffer != 0) {
glDeleteBuffers(1, &buffer);
buffer = 0;
}
}
void debug_font_buffer::addText(const char *text, double left, double baseline, double scale) {
void DebugFontBuffer::addText(const char *text, double left, double baseline, double scale) {
int32_t length = strlen(text);
for (int32_t i = 0; i < length; i++) {
if (text[i] < 32 || text[i] > 127) {
@@ -52,24 +52,23 @@ void debug_font_buffer::addText(const char *text, double left, double baseline,
dirty = true;
}
void debug_font_buffer::clear() {
void DebugFontBuffer::clear() {
array.clear();
dirty = true;
}
uint32_t debug_font_buffer::length() {
uint32_t DebugFontBuffer::length() {
// We store 2 coordinates per vertex
return array.size() / 2;
}
void debug_font_buffer::bind() {
void DebugFontBuffer::bind() {
if (buffer == 0) {
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
} else {
glBindBuffer(GL_ARRAY_BUFFER, buffer);
}
glBindBuffer(GL_ARRAY_BUFFER, buffer);
if (dirty) {
glBufferData(GL_ARRAY_BUFFER, array.size() * sizeof(uint16_t), array.data(), GL_STATIC_DRAW);
dirty = false;

View File

@@ -0,0 +1,45 @@
#include <llmr/geometry/line_buffer.hpp>
#include <llmr/platform/gl.hpp>
// #include <cmath>
using namespace llmr;
LineBuffer::~LineBuffer() {
if (buffer != 0) {
glDeleteBuffers(1, &buffer);
buffer = 0;
}
}
uint32_t LineBuffer::length() const {
// We store 2 coordinates per vertex + 1 linesofar + 1 extrude coord pair == 4 (== 8 bytes)
return array.size() / 4;
}
void LineBuffer::add(int16_t x, int16_t y, float ex, float ey, int8_t tx, int8_t ty, int32_t linesofar) {
array.push_back((x * 2) | tx);
array.push_back((y * 2) | ty);
array.push_back(linesofar);
array.push_back(
((int16_t)round(extrudeScale * ex) << 8) |
((int16_t)round(extrudeScale * ey) & 0xFF)
);
if (!dirty) dirty = true;
};
void LineBuffer::addDegenerate() {
add(16383, 16383, 0, 0, 1, 1, 0);
}
void LineBuffer::bind() {
if (buffer == 0) {
glGenBuffers(1, &buffer);
}
glBindBuffer(GL_ARRAY_BUFFER, buffer);
if (dirty) {
glBufferData(GL_ARRAY_BUFFER, array.size() * sizeof(int16_t), array.data(), GL_STATIC_DRAW);
dirty = false;
}
}

View File

@@ -1,43 +0,0 @@
#include <llmr/geometry/linevertexbuffer.hpp>
#include <llmr/platform/gl.hpp>
#include <cstdio>
using namespace llmr;
linevertexbuffer::linevertexbuffer()
: buffer(0) {
}
linevertexbuffer::~linevertexbuffer() {
if (buffer != 0) {
glDeleteBuffers(1, &buffer);
buffer = 0;
}
}
void linevertexbuffer::addDegenerate() {
array.push_back(32767);
array.push_back(0);
}
void linevertexbuffer::addCoordinate(int16_t x, int16_t y) {
array.push_back(x);
array.push_back(y);
}
uint32_t linevertexbuffer::length() {
// We store 2 coordinates per vertex
return array.size() / 2;
}
void linevertexbuffer::bind() {
if (buffer == 0) {
glGenBuffers(1, &buffer);
// fprintf(stderr, "new buffer id: %d\n", buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, array.size() * sizeof(uint16_t), array.data(), GL_STATIC_DRAW);
} else {
glBindBuffer(GL_ARRAY_BUFFER, buffer);
}
}

View File

@@ -44,9 +44,9 @@ std::forward_list<Tile::ID> Tile::children(const ID& id, int32_t z) {
Tile::Tile(ID id, const Style& style)
: id(id),
state(initial),
lineVertex(std::make_shared<linevertexbuffer>()),
debugFontVertex(std::make_shared<debug_font_buffer>()),
debugFontBuffer(std::make_shared<DebugFontBuffer>()),
fillBuffer(std::make_shared<FillBuffer>()),
lineBuffer(std::make_shared<LineBuffer>()),
data(0),
bytes(0),
style(style) {
@@ -54,7 +54,7 @@ Tile::Tile(ID id, const Style& style)
// Initialize tile debug coordinates
char coord[32];
snprintf(coord, sizeof(coord), "%d/%d/%d", id.z, id.x, id.y);
debugFontVertex->addText(coord, 50, 200, 5);
debugFontBuffer->addText(coord, 50, 200, 5);
}
Tile::~Tile() {

View File

@@ -344,14 +344,14 @@ void Painter::renderDebug(const Tile::Ptr& tile) {
// draw debug info
switchShader(lineShader);
glUniformMatrix4fv(lineShader->u_matrix, 1, GL_FALSE, matrix);
tile->debugFontVertex->bind();
tile->debugFontBuffer->bind();
glVertexAttribPointer(lineShader->a_pos, 2, GL_SHORT, GL_FALSE, 0, BUFFER_OFFSET(0));
glUniform4f(lineShader->u_color, 1.0f, 1.0f, 1.0f, 1.0f);
glLineWidth(4.0f);
glDrawArrays(GL_LINES, 0, tile->debugFontVertex->length());
glDrawArrays(GL_LINES, 0, tile->debugFontBuffer->length());
glUniform4f(lineShader->u_color, 0.0f, 0.0f, 0.0f, 1.0f);
glLineWidth(2.0f);
glDrawArrays(GL_LINES, 0, tile->debugFontVertex->length());
glDrawArrays(GL_LINES, 0, tile->debugFontBuffer->length());
// Revert blending mode to blend to the back.
glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_ONE);