Commit 35c0f343 authored by Christian Schulte zu Berge's avatar Christian Schulte zu Berge
Browse files

Two improvements:

* MultiIndexedGeometry supports instanced rendering
* FontAtlas supports writing picking information
parent 35531771
......@@ -59,8 +59,6 @@ namespace campvis {
MultiIndexedGeometry::MultiIndexedGeometry(const MultiIndexedGeometry& rhs)
: GeometryData(rhs)
, _indices(rhs._indices)
, _offsets(rhs._offsets)
, _counts(rhs._counts)
, _vertices(rhs._vertices)
, _textureCoordinates(rhs._textureCoordinates)
, _colors(rhs._colors)
......@@ -80,8 +78,6 @@ namespace campvis {
GeometryData::operator=(rhs);
_indices = rhs._indices;
_offsets = rhs._offsets;
_counts = rhs._counts;
_vertices = rhs._vertices;
_textureCoordinates = rhs._textureCoordinates;
......@@ -97,8 +93,6 @@ namespace campvis {
MultiIndexedGeometry* MultiIndexedGeometry::clone() const {
MultiIndexedGeometry* toReturn = new MultiIndexedGeometry(_vertices, _textureCoordinates, _colors, _normals);
toReturn->_indices = _indices;
toReturn->_offsets = _offsets;
toReturn->_counts = _counts;
return toReturn;
}
......@@ -121,12 +115,11 @@ namespace campvis {
}
void MultiIndexedGeometry::addPrimitive(const std::vector<uint16_t>& indices) {
_offsets.push_back(reinterpret_cast<void*>(_indices.size() * 2));
_counts.push_back(static_cast<GLsizei>(indices.size()));
_indices.insert(_indices.end(), indices.begin(), indices.end());
if (! _indices.empty())
_indices.push_back(65535);
_indices.insert(_indices.end(), indices.begin(), indices.end());
_buffersDirty = true;
}
const std::vector<cgt::col4>& MultiIndexedGeometry::getPickingInformation() const {
......@@ -140,7 +133,38 @@ namespace campvis {
}
void MultiIndexedGeometry::render(GLenum mode) const {
if (_counts.empty())
if (_indices.empty())
return;
createGLBuffers();
if (_buffersDirty) {
LERROR("Cannot render without initialized OpenGL buffers.");
return;
}
cgt::VertexArrayObject vao;
if (_verticesBuffer)
vao.setVertexAttributePointer(0, _verticesBuffer);
if (_texCoordsBuffer)
vao.setVertexAttributePointer(1, _texCoordsBuffer);
if (_colorsBuffer)
vao.setVertexAttributePointer(2, _colorsBuffer);
if (_normalsBuffer)
vao.setVertexAttributePointer(3, _normalsBuffer);
if (_pickingBuffer)
vao.setVertexAttributePointer(4, _pickingBuffer);
vao.bindIndexBuffer(_indicesBuffer);
glEnable(GL_PRIMITIVE_RESTART);
glPrimitiveRestartIndex(65535);
glDrawElements(mode, static_cast<GLsizei>(_indices.size()), GL_UNSIGNED_SHORT, 0);
glDisable(GL_PRIMITIVE_RESTART);
LGL_ERROR;
}
void MultiIndexedGeometry::renderInstanced(GLsizei count, GLenum mode /*= GL_TRIANGLE_FAN*/) const {
if (_indices.empty())
return;
createGLBuffers();
......@@ -162,8 +186,10 @@ namespace campvis {
vao.setVertexAttributePointer(4, _pickingBuffer);
vao.bindIndexBuffer(_indicesBuffer);
const GLvoid** ptr = (const GLvoid**)(&_offsets.front()); // <- hidden reinterpret_cast<const GLvoid**> here, ugly OpenGL...
glMultiDrawElements(mode, &_counts.front(), GL_UNSIGNED_SHORT, ptr, static_cast<GLsizei>(_offsets.size()));
glEnable(GL_PRIMITIVE_RESTART);
glPrimitiveRestartIndex(65535);
glDrawElementsInstanced(mode, static_cast<GLsizei>(_indices.size()), GL_UNSIGNED_SHORT, 0, count);
glDisable(GL_PRIMITIVE_RESTART);
LGL_ERROR;
}
......
......@@ -109,6 +109,14 @@ namespace campvis {
* \param mode OpenGL rendering mode for this mesh
*/
virtual void render(GLenum mode) const;
/**
* Renders multiple instances of this GeometryData.
* Must be called from a valid OpenGL context.
* \param count Number of instances
* \param mode OpenGL rendering mode
*/
virtual void renderInstanced(GLsizei count, GLenum mode = GL_TRIANGLE_FAN) const;
/// \see GeometryData::getWorldBounds
virtual cgt::Bounds getWorldBounds() const;
......@@ -130,8 +138,6 @@ namespace campvis {
void deleteIndicesBuffer() const;
std::vector<uint16_t> _indices; ///< Index list defining the faces
std::vector<void*> _offsets; ///< Byte offsets for each primitive to render
std::vector<GLsizei> _counts; ///< Numer of vertices for each primitive to render
std::vector<cgt::vec3> _vertices; ///< The list of the vertex positions of the face.
std::vector<cgt::vec3> _textureCoordinates; ///< The list of vertex texture coordinates, may be empty.
......
......@@ -25,11 +25,17 @@ IF(${ModuleEnabled})
# Header files (including GLSL files so that they'll appear in VS projects)
FILE(GLOB ThisModHeaders RELATIVE ${ModulesDir}
modules/fontrendering/glsl/*.frag
modules/fontrendering/glsl/*.geom
modules/fontrendering/glsl/*.vert
modules/fontrendering/pipelines/*.h
modules/fontrendering/processors/*.h
modules/fontrendering/tools/*.h
)
LIST (APPEND ThisModShaderDirectories "modules/fontrendering/fonts")
LIST (APPEND ThisModShaderDirectories "modules/fontrendering/glsl")
SET(ThisModDependencies base)
ENDIF(${ModuleEnabled})
......
......@@ -23,11 +23,15 @@
// ================================================================================================
in vec3 ex_TexCoord;
out vec4 out_Color;
layout(location = 0) out vec4 out_Color;
layout(location = 1) out vec4 out_Picking;
uniform sampler2D _fontTexture;
uniform vec4 _color = vec4(1.0, 1.0, 1.0, 1.0);
uniform vec4 _pickingColor = vec4(0.0);
void main() {
out_Color = vec4(texture2D(_fontTexture, ex_TexCoord.xy).r) * _color;
out_Picking = _pickingColor;
}
......@@ -196,5 +196,13 @@ namespace campvis {
LGL_ERROR;
}
void FontAtlas::renderTextWithPickingInformation(const std::string& text, const cgt::vec2& position, const cgt::vec4& color, const cgt::vec4& pickingInfo, const cgt::mat4& transformationMatrix /*= cgt::mat4::identity*/) {
_shader->activate();
_shader->setUniform("_pickingColor", pickingInfo);
_shader->deactivate();
renderText(text, position, color, transformationMatrix);
}
}
}
......@@ -82,6 +82,18 @@ namespace fontrendering {
*/
void renderText(const std::string& text, const cgt::vec2& position, const cgt::vec4& color, const cgt::mat4& transformationMatrix = cgt::mat4::identity);
/**
* Renders the given text with the given parameters into the current framebuffer.
* \param text The text to render (currently only ASCII-256 support).
* \param position Position where to start rendering the text (in normalized [-1, 1] coordinates).
* \param color Color of the text to render.
* \param pickingInformation Color to render into the picking info texture (second FBO color attachment).
* \param transformationMatrix Additional transformation matrix that is applied to each rendered vertex.
*/
void renderTextWithPickingInformation(const std::string& text, const cgt::vec2& position, const cgt::vec4& color, const cgt::vec4& pickingInfo, const cgt::mat4& transformationMatrix = cgt::mat4::identity);
private:
/// Struct storing glyph parameters of each printable ASCII character
struct {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment