Merge branch 'tessellation' into nostencil

Conflicts:
	include/llmr/renderer/fill_bucket.hpp
	src/renderer/painter.cpp
This commit is contained in:
Konstantin Käfer
2014-02-26 10:44:09 +01:00
28 changed files with 5521 additions and 121 deletions

View File

@@ -0,0 +1,25 @@
** SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
** Copyright (C) [dates of first publication] Silicon Graphics, Inc.
** All Rights Reserved.
**
** Permission is hereby granted, free of charge, to any person obtaining a copy
** of this software and associated documentation files (the "Software"), to deal
** in the Software without restriction, including without limitation the rights
** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
** of the Software, and to permit persons to whom the Software is furnished to do so,
** subject to the following conditions:
**
** The above copyright notice including the dates of first publication and either this
** permission notice or a reference to http://oss.sgi.com/projects/FreeB/ shall be
** included in all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
** INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
** PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL SILICON GRAPHICS, INC.
** BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
** OR OTHER DEALINGS IN THE SOFTWARE.
**
** Except as contained in this notice, the name of Silicon Graphics, Inc. shall not
** be used in advertising or otherwise to promote the sale, use or other dealings in
** this Software without prior written authorization from Silicon Graphics, Inc.

209
include/libtess2/tesselator.h Executable file
View File

@@ -0,0 +1,209 @@
/*
** SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
** Copyright (C) [dates of first publication] Silicon Graphics, Inc.
** All Rights Reserved.
**
** Permission is hereby granted, free of charge, to any person obtaining a copy
** of this software and associated documentation files (the "Software"), to deal
** in the Software without restriction, including without limitation the rights
** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
** of the Software, and to permit persons to whom the Software is furnished to do so,
** subject to the following conditions:
**
** The above copyright notice including the dates of first publication and either this
** permission notice or a reference to http://oss.sgi.com/projects/FreeB/ shall be
** included in all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
** INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
** PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL SILICON GRAPHICS, INC.
** BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
** OR OTHER DEALINGS IN THE SOFTWARE.
**
** Except as contained in this notice, the name of Silicon Graphics, Inc. shall not
** be used in advertising or otherwise to promote the sale, use or other dealings in
** this Software without prior written authorization from Silicon Graphics, Inc.
*/
/*
** Author: Mikko Mononen, July 2009.
*/
#ifndef TESSELATOR_H
#define TESSELATOR_H
#ifdef __cplusplus
extern "C" {
#endif
// See OpenGL Red Book for description of the winding rules
// http://www.glprogramming.com/red/chapter11.html
enum TessWindingRule
{
TESS_WINDING_ODD,
TESS_WINDING_NONZERO,
TESS_WINDING_POSITIVE,
TESS_WINDING_NEGATIVE,
TESS_WINDING_ABS_GEQ_TWO,
};
// The contents of the tessGetElements() depends on element type being passed to tessTesselate().
// Tesselation result element types:
// TESS_POLYGONS
// Each element in the element array is polygon defined as 'polySize' number of vertex indices.
// If a polygon has than 'polySize' vertices, the remaining indices are stored as TESS_UNDEF.
// Example, drawing a polygon:
// const int nelems = tessGetElementCount(tess);
// const TESSindex* elems = tessGetElements(tess);
// for (int i = 0; i < nelems; i++) {
// const TESSindex* poly = &elems[i * polySize];
// glBegin(GL_POLYGON);
// for (int j = 0; j < polySize; j++) {
// if (poly[j] == TESS_UNDEF) break;
// glVertex2fv(&verts[poly[j]*vertexSize]);
// }
// glEnd();
// }
//
// TESS_CONNECTED_POLYGONS
// Each element in the element array is polygon defined as 'polySize' number of vertex indices,
// followed by 'polySize' indices to neighour polygons, that is each element is 'polySize' * 2 indices.
// If a polygon has than 'polySize' vertices, the remaining indices are stored as TESS_UNDEF.
// If a polygon edge is a boundary, that is, not connected to another polygon, the neighbour index is TESS_UNDEF.
// Example, flood fill based on seed polygon:
// const int nelems = tessGetElementCount(tess);
// const TESSindex* elems = tessGetElements(tess);
// unsigned char* visited = (unsigned char*)calloc(nelems);
// TESSindex stack[50];
// int nstack = 0;
// stack[nstack++] = seedPoly;
// visited[startPoly] = 1;
// while (nstack > 0) {
// TESSindex idx = stack[--nstack];
// const TESSindex* poly = &elems[idx * polySize * 2];
// const TESSindex* nei = &poly[polySize];
// for (int i = 0; i < polySize; i++) {
// if (poly[i] == TESS_UNDEF) break;
// if (nei[i] != TESS_UNDEF && !visited[nei[i]])
// stack[nstack++] = nei[i];
// visited[nei[i]] = 1;
// }
// }
// }
//
// TESS_BOUNDARY_CONTOURS
// Each element in the element array is [base index, count] pair defining a range of vertices for a contour.
// The first value is index to first vertex in contour and the second value is number of vertices in the contour.
// Example, drawing contours:
// const int nelems = tessGetElementCount(tess);
// const TESSindex* elems = tessGetElements(tess);
// for (int i = 0; i < nelems; i++) {
// const TESSindex base = elems[i * 2];
// const TESSindex count = elems[i * 2 + 1];
// glBegin(GL_LINE_LOOP);
// for (int j = 0; j < count; j++) {
// glVertex2fv(&verts[(base+count) * vertexSize]);
// }
// glEnd();
// }
//
enum TessElementType
{
TESS_POLYGONS,
TESS_CONNECTED_POLYGONS,
TESS_BOUNDARY_CONTOURS,
};
typedef float TESSreal;
typedef int TESSindex;
typedef struct TESStesselator TESStesselator;
typedef struct TESSalloc TESSalloc;
#define TESS_UNDEF (~(TESSindex)0)
// Custom memory allocator interface.
// The internal memory allocator allocates mesh edges, vertices and faces
// as well as dictionary nodes and active regions in buckets and uses simple
// freelist to speed up the allocation. The bucket size should roughly match your
// expected input data. For example if you process only hundreds of vertices,
// a bucket size of 128 might be ok, where as when processing thousands of vertices
// bucket size of 1024 might be approproate. The bucket size is a compromise between
// how often to allocate memory from the system versus how much extra space the system
// should allocate. Reasonable defaults are show in commects below, they will be used if
// the bucket sizes are zero.
//
// The use may left the memrealloc to be null. In that case, the tesselator will not try to
// dynamically grow int's internal arrays. The tesselator only needs the reallocation when it
// has found intersecting segments and needs to add new vertex. This defency can be cured by
// allocating some extra vertices beforehand. The 'extraVertices' variable allows to specify
// number of expected extra vertices.
struct TESSalloc
{
void *(*memalloc)( void *userData, unsigned int size );
void *(*memrealloc)( void *userData, void* ptr, unsigned int size );
void (*memfree)( void *userData, void *ptr );
void* userData; // User data passed to the allocator functions.
int meshEdgeBucketSize; // 512
int meshVertexBucketSize; // 512
int meshFaceBucketSize; // 256
int dictNodeBucketSize; // 512
int regionBucketSize; // 256
int extraVertices; // Number of extra vertices allocated for the priority queue.
};
// tessNewTess() - Creates a new tesselator.
// Use tessDeleteTess() to delete the tesselator.
// Returns:
// new tesselator object.
TESStesselator* tessNewTess( TESSalloc* alloc );
// tessDeleteTess() - Deletes a tesselator.
// Parameters:
// tess - pointer to tesselator object to be deleted.
void tessDeleteTess( TESStesselator *tess );
// tessAddContour() - Adds a contour to be tesselated.
// The type of the vertex coordinates is assumed to be TESSreal.
// Parameters:
// tess - pointer to tesselator object.
// size - number of coordinates per vertex. Must be 2 or 3.
// pointer - pointer to the first coordinate of the first vertex in the array.
// stride - defines offset in bytes between consecutive vertices.
// count - number of vertices in contour.
void tessAddContour( TESStesselator *tess, int size, const void* pointer, int stride, int count );
// tessTesselate() - tesselate contours.
// Parameters:
// tess - pointer to tesselator object.
// windingRule - winding rules used for tesselation, must be one of TessWindingRule.
// elementType - defines the tesselation result element type, must be one of TessElementType.
// polySize - defines maximum vertices per polygons if output is polygons.
// vertexSize - defines the number of coordinates in tesselation result vertex, must be 2 or 3.
// normal - defines the normal of the input contours, of null the normal is calculated automatically.
// Returns:
// 1 if succeed, 0 if failed.
int tessTesselate( TESStesselator *tess, int windingRule, int elementType, int polySize, int vertexSize, const TESSreal* normal );
// tessGetVertexCount() - Returns number of vertices in the tesselated output.
int tessGetVertexCount( TESStesselator *tess );
// tessGetVertices() - Returns pointer to first coordinate of first vertex.
const TESSreal* tessGetVertices( TESStesselator *tess );
// tessGetVertexIndices() - Returns pointer to first vertex index.
// Vertex indices can be used to map the generated vertices to the original vertices.
// Every point added using tessAddContour() will get a new index starting at 0.
// New vertices generated at the intersections of segments are assigned value TESS_UNDEF.
const TESSindex* tessGetVertexIndices( TESStesselator *tess );
// tessGetElementCount() - Returns number of elements in the the tesselated output.
int tessGetElementCount( TESStesselator *tess );
// tessGetElements() - Returns pointer to the first element.
const TESSindex* tessGetElements( TESStesselator *tess );
#ifdef __cplusplus
};
#endif
#endif // TESSELATOR_H

View File

@@ -2,15 +2,12 @@
#define LLMR_GEOMETRY_TRIANGLE_ELEMENTS_BUFFER
#include <llmr/geometry/buffer.hpp>
#include <llmr/geometry/vao.hpp>
namespace llmr {
template <typename Shader>
class VertexArrayObject;
template <typename Shader>
struct ElementGroup {
VertexArrayObject<Shader> array;
VertexArrayObject array;
uint32_t vertex_length;
uint32_t elements_length;

View File

@@ -7,18 +7,26 @@
namespace llmr {
template <typename Shader>
class VertexArrayObject {
public:
template <typename VertexBuffer>
template <typename Shader, typename VertexBuffer>
void bind(Shader& shader, VertexBuffer& vertex_buffer, char *offset) {
if (!vao) {
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
} else {
// We have been given the correct information.
glBindVertexArray(vao);
}
if (shader_ptr != &shader) {
if (shader_ptr != nullptr) {
fprintf(stderr, "shader rebind!");
}
vertex_buffer.bind();
shader.bind(offset);
shader_ptr = &shader;
vertex_buffer_ptr = &vertex_buffer;
elements_buffer_ptr = nullptr;
offset_ptr = offset;
@@ -28,22 +36,25 @@ public:
throw std::runtime_error("trying to bind VAO to another elements buffer");
} else if (offset_ptr != offset) {
throw std::runtime_error("trying to bind VAO to another offset");
} else {
// We have been given the correct information.
glBindVertexArray(vao);
}
}
template <typename VertexBuffer, typename ElementsBuffer>
template <typename Shader, typename VertexBuffer, typename ElementsBuffer>
void bind(Shader& shader, VertexBuffer& vertex_buffer, ElementsBuffer& elements_buffer, char *offset) {
if (!vao) {
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
} else {
// We have been given the correct information.
glBindVertexArray(vao);
}
if (shader_ptr != &shader) {
vertex_buffer.bind();
elements_buffer.bind();
shader.bind(offset);
shader_ptr = &shader;
vertex_buffer_ptr = &vertex_buffer;
elements_buffer_ptr = &elements_buffer;
offset_ptr = offset;
@@ -53,9 +64,6 @@ public:
throw std::runtime_error("trying to bind VAO to another elements buffer");
} else if (offset_ptr != offset) {
throw std::runtime_error("trying to bind VAO to another offset");
} else {
// We have been given the correct information.
glBindVertexArray(vao);
}
}
@@ -70,6 +78,7 @@ private:
// For debug reasons, we're storing the bind information so that we can
// detect errors and report
void *shader_ptr = nullptr;
void *vertex_buffer_ptr = nullptr;
void *elements_buffer_ptr = nullptr;
char *offset_ptr = 0;

View File

@@ -75,7 +75,7 @@ public:
// Holds the actual geometries in this tile.
DebugFontBuffer debugFontBuffer;
VertexArrayObject<PlainShader> debugFontArray;
VertexArrayObject debugFontArray;
std::shared_ptr<FillVertexBuffer> fillVertexBuffer;
std::shared_ptr<LineVertexBuffer> lineVertexBuffer;

View File

@@ -6,6 +6,8 @@
#include <llmr/geometry/elements_buffer.hpp>
#include <llmr/geometry/fill_buffer.hpp>
#include <libtess2/tesselator.h>
#include <vector>
#include <memory>
@@ -22,31 +24,45 @@ class LineElementsBuffer;
class BucketDescription;
class OutlineShader;
class PlainShader;
class PatternShader;
struct Coordinate;
struct pbf;
class FillBucket : public Bucket {
typedef ElementGroup<PlainShader> triangle_group_type;
typedef ElementGroup<OutlineShader> line_group_type;
static void *alloc(void *data, unsigned int size);
static void *realloc(void *data, void *ptr, unsigned int size);
static void free(void *userData, void *ptr);
typedef ElementGroup triangle_group_type;
typedef ElementGroup line_group_type;
public:
FillBucket(const std::shared_ptr<FillVertexBuffer>& vertexBuffer,
const std::shared_ptr<TriangleElementsBuffer>& triangleElementsBuffer,
const std::shared_ptr<LineElementsBuffer>& lineElementsBuffer,
const BucketDescription& bucket_desc);
~FillBucket();
virtual void render(Painter& painter, const std::string& layer_name, const Tile::ID& id);
void addGeometry(pbf& data);
void addGeometry(const std::vector<Coordinate>& line);
void addGeometry(const std::vector<std::vector<TESSreal>>& lines);
bool empty() const;
void tessellate();
void drawElements(PlainShader& shader);
void drawElements(PatternShader& shader);
void drawVertices(OutlineShader& shader);
public:
const BucketGeometryDescription geom_desc;
private:
TESSalloc *allocator;
TESStesselator *tesselator;
std::shared_ptr<FillVertexBuffer> vertexBuffer;
std::shared_ptr<TriangleElementsBuffer> triangleElementsBuffer;
std::shared_ptr<LineElementsBuffer> lineElementsBuffer;
@@ -55,9 +71,17 @@ private:
const size_t vertex_start;
const size_t triangle_elements_start;
const size_t line_elements_start;
VertexArrayObject<OutlineShader> array;
VertexArrayObject array;
std::vector<triangle_group_type> triangleGroups;
std::vector<line_group_type> lineGroups;
std::vector<TESSreal> line;
bool hasVertices = false;
static const int vertexSize = 2;
static const int stride = sizeof(TESSreal) *vertexSize;
static const int vertices_per_group = 3;
};
}

View File

@@ -19,8 +19,8 @@ struct Coordinate;
struct pbf;
class LineBucket : public Bucket {
typedef ElementGroup<LineShader> triangle_group_type;
typedef ElementGroup<LinejoinShader> point_group_type;
typedef ElementGroup triangle_group_type;
typedef ElementGroup point_group_type;
public:
LineBucket(const std::shared_ptr<LineVertexBuffer>& vertexBuffer,
const std::shared_ptr<TriangleElementsBuffer>& triangleElementsBuffer,

View File

@@ -81,8 +81,8 @@ private:
4096, 4096
};
VertexArrayObject<PlainShader> coveringPlainArray;
VertexArrayObject<PatternShader> coveringPatternArray;
VertexArrayObject coveringPlainArray;
VertexArrayObject coveringPatternArray;
// Set up the tile boundary lines we're using to draw the tile outlines.
VertexBuffer tileBorderBuffer = {
@@ -93,7 +93,7 @@ private:
0, 0
};
VertexArrayObject<PlainShader> tileBorderArray;
VertexArrayObject tileBorderArray;
// Set up the matte buffer we're using to draw the filling background.
VertexBuffer matteBuffer = {
@@ -108,7 +108,7 @@ private:
16384, 16384
};
VertexArrayObject<PlainShader> matteArray;
VertexArrayObject matteArray;
};
}