Developer Documentation
RenderObject.hh
1/*===========================================================================*\
2 * *
3 * OpenFlipper *
4 * Copyright (c) 2001-2015, RWTH-Aachen University *
5 * Department of Computer Graphics and Multimedia *
6 * All rights reserved. *
7 * www.openflipper.org *
8 * *
9 *---------------------------------------------------------------------------*
10 * This file is part of OpenFlipper. *
11 *---------------------------------------------------------------------------*
12 * *
13 * Redistribution and use in source and binary forms, with or without *
14 * modification, are permitted provided that the following conditions *
15 * are met: *
16 * *
17 * 1. Redistributions of source code must retain the above copyright notice, *
18 * this list of conditions and the following disclaimer. *
19 * *
20 * 2. Redistributions in binary form must reproduce the above copyright *
21 * notice, this list of conditions and the following disclaimer in the *
22 * documentation and/or other materials provided with the distribution. *
23 * *
24 * 3. Neither the name of the copyright holder nor the names of its *
25 * contributors may be used to endorse or promote products derived from *
26 * this software without specific prior written permission. *
27 * *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40\*===========================================================================*/
41
42
43#pragma once
44
45
46#include <ACG/GL/gl.hh>
47#include <ACG/Math/GLMatrixT.hh>
48#include <ACG/GL/ShaderGenerator.hh>
49#include <ACG/ShaderUtils/UniformPool.hh>
50
51#include <map>
52
53
54namespace GLSL{
55 class Program;
56}
57
58namespace ACG
59{
60
61// forward declaration
62class VertexDeclaration;
63class GLState;
64
65namespace SceneGraph {
66 namespace DrawModes {
67 class DrawModeProperties;
68 }
69 class Material;
70}
71
72
98struct ACGDLLEXPORT RenderObject
99{
100 friend class IRenderer;
101
105 RenderObject();
106
108
109
117
121 std::string name;
122
136
139
142
143
144 //===========================================================================
148 //===========================================================================
149
159
160
164
173 const void* sysmemIndexBuffer;
174
183
185 unsigned int patchVertices; // GL_PATCH_VERTICES
186
188 unsigned int numIndices;
189
191 unsigned int indexOffset;
192
197 GLenum indexType;
198
205
206
209
226
227 // opengl states
228 // queried from glState in initFromState()
229 bool culling;
230 bool blending;
231 bool alphaTest;
232 bool depthTest;
233 bool depthWrite;
234
235 GLenum fillMode; // GL_POINT, GL_LINE, GL_FILL, default: GL_FILL
236
237 GLboolean colorWriteMask[4]; // {r,g,b,a}, default: all true
238
239// GLenum shadeModel; // GL_FACE, GL_SMOOTH obsolete in shader pipeline
240 GLenum depthFunc;
241
242 // alpha testing function
243 GLenum alphaFunc;
244 float alphaRef; // reference value for alpha function
245
246 // alpha blending
247 GLenum blendSrc, blendDest;
248
250
251
252 // enable/disable clip planes
253 // bit i decides whether the vertex shader output gl_ClipDistance[i] is enabled or disabled
254 // default: all zero (disabled)
255 GLuint clipDistanceMask;
256
257 // GL_PROGRAM_POINT_SIZE : default false
258 // if true, use gl_PointSize from shader
259 // otherwise, use pointSize from render object
260 bool programPointSize;
261
262 // glPointSize(), default: 0.1
263 float pointSize;
264
265 // ---------------------------
266 // default tessellation lod, if only a tess-eval, but no tess-control shader is specified
267 // this is ignored otherwise
268
269 Vec2f patchDefaultInnerLevel; // GL_PATCH_DEFAULT_INNER_LEVEL
270 Vec4f patchDefaultOuterLevel; // GL_PATCH_DEFAULT_OUTER_LEVEL
271
272 // ---------------------------
275 ambient,
276 specular,
277 emissive;
278
279 float alpha,
280 shininess;
281
282
292
303
304
313 struct Texture
314 {
315 GLuint id;
316 GLenum type;
317 bool shadow;
318 Texture(GLuint _id = 0, GLenum _type = GL_TEXTURE_2D, bool _shadow = false):
319 id(_id),
320 type(_type),
321 shadow(_shadow){}
322 };
323
324
326 void addTexture(const Texture& _t)
327 {
328 addTexture(_t,numTextures());
329 }
330
334 void addTexture(const Texture& _t,const size_t _stage, bool _addToShaderGen = true)
335 {
336 if (GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS < numTextures())
337 {
338 std::cerr << "Texturestage " << _stage << " is too big. Allowed stages: "<< GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS << std::endl;
339 return;
340 }
341 textures_[_stage] = _t;
342 if (_addToShaderGen)
343 shaderDesc.addTextureType(_t.type,_t.shadow,_stage);
344 }
345
347 void clearTextures() {textures_.clear(); shaderDesc.clearTextures();}
348
349 const std::map<size_t,Texture>& textures(){return textures_;}
350
351 size_t numTextures() {return textures_.size();}
352
353private:
355 std::map<size_t,Texture> textures_;
356public:
357
358
361 std::string debugName;
362
364 unsigned int internalFlags_;
365
366
367 // opengl style helper function interface:
368 // provided for easier setup of RenderObjects,
369 // usage is not necessary
370 inline void glBindBuffer(GLenum target, GLuint buffer)
371 {
372 switch (target)
373 {
374 case GL_ARRAY_BUFFER: vertexBuffer = buffer; break;
375 case GL_ELEMENT_ARRAY_BUFFER: indexBuffer = buffer; break;
376 }
377 }
378
379 inline void glDrawArrays(GLenum mode, GLint first, GLsizei count)
380 {
381 this->glDrawInstancedArrays(mode, first, count, 0);
382 }
383
384 inline void glDrawInstancedArrays(GLenum mode, GLint first, GLsizei count, GLsizei primcount)
385 {
386 indexBuffer = 0;
387 sysmemIndexBuffer = 0;
388
389 primitiveMode = mode;
390 indexOffset = first;
391 numIndices = count;
392 numInstances = primcount;
393 }
394
395 inline void glDrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices)
396 {
397 this->glDrawElementsInstanced(mode, count, type, indices, 0);
398 }
399
400 inline void glDrawElementsInstanced(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, GLsizei primcount)
401 {
402 primitiveMode = mode;
403 numIndices = count;
404 indexType = type;
405
406 sysmemIndexBuffer = indices;
407
408 numInstances = primcount;
409 }
410
411 inline void glColorMask(GLboolean r, GLboolean g, GLboolean b, GLboolean a)
412 {
413 colorWriteMask[0] = r; colorWriteMask[1] = g; colorWriteMask[2] = b; colorWriteMask[3] = a;
414 }
415
416 inline void glAlphaFunc(GLenum func, float ref)
417 {
418 alphaFunc = func;
419 alphaRef = ref;
420 }
421
426 void initFromState(GLState* _glState);
427
428 void setMaterial(const SceneGraph::Material* _mat);
429
432 void setupShaderGenFromDrawmode(const SceneGraph::DrawModes::DrawModeProperties* _props);
433
434
445 void setupLineRendering(float _lineWidth, const Vec2f& _screenSize);
446
448 bool isDefaultLineObject() const;
449
451 void resetLineRendering();
452
459 void setupPointRendering(float _pointSize, const Vec2f& _screenSize);
460
462 bool isDefaultPointObject() const;
463
465 void resetPointRendering();
466
467
468
471 QString toString() const;
472
479 void setUniform(const char *_name, GLint _value);
480
487 void setUniform(const char *_name, GLfloat _value);
488
495 void setUniform(const char *_name, const ACG::Vec2f &_value);
496
503 void setUniform(const char *_name, const ACG::Vec3f &_value);
504
511 void setUniform(const char *_name, const ACG::Vec4f &_value);
512
513 void setUniform(const char *_name, const ACG::GLMatrixf &_value, bool _transposed = false);
514 void setUniformMat3(const char *_name, const ACG::GLMatrixf &_value, bool _transposed = false);
515
516
517 void setUniform(const char *_name, GLint *_values, int _count);
518 void setUniform(const char *_name, GLfloat *_values, int _count);
519
520
526 void addUniformPool(const GLSL::UniformPool& _pool);
527
528private:
529 GLSL::UniformPool uniformPool_;
530};
531
532
540class ACGDLLEXPORT RenderObjectModifier
541{
542public:
543 RenderObjectModifier(const std::string& _name = "") : name_(_name) {}
544 virtual ~RenderObjectModifier() {}
545
551 virtual void apply(RenderObject* _obj) = 0;
552
554 const std::string& name() const { return name_; }
555
556private:
557 std::string name_;
558};
559
560
561
562//=============================================================================
563} // namespace ACG
564//=============================================================================
Interface for modifying render objects.
virtual void apply(RenderObject *_obj)=0
apply the modifier
const std::string & name() const
Get name of the modifier.
void addTextureType(GLenum _type, bool _shadow, size_t _stage)
adds a texture type to the shader and enables texturing.
void clearTextures()
disables texture support and removes all texture types
Class to define the vertex input layout.
GLSL uniform pool.
Definition: UniformPool.hh:64
Namespace providing different geometric functions concerning angles.
This namespace contains all the classes and functions for handling GLSL shader and program objects.
Definition: AntiAliasing.hh:66
Texture to be used.
Interface class between scenegraph and renderer.
Definition: RenderObject.hh:99
int debugID
used internally for renderer debugging
const char * depthMapUniformName
Uniform name of the depth map in the used shader.
Vec3f diffuse
material definitions
ShaderGenDesc shaderDesc
Drawmode and other shader params.
GLuint vertexArrayObject
Use vertex array object.
const VertexDeclaration * vertexDecl
Defines the vertex buffer layout, ignored if VAO is provided.
const void * sysmemIndexBuffer
Use system memory index buffer.
void addTexture(const Texture &_t)
adds a texture to stage RenderObjects::numTextures()
GLuint indexBuffer
Use vertex array object.
int priority
Priority to allow sorting of objects.
unsigned int numIndices
Number indices to render.
GLMatrixd modelview
Modelview transform.
GLMatrixd proj
Projection transform.
std::map< size_t, Texture > textures_
holds the textures (second) and the stage id (first)
std::string name
Name for logging.
bool inZPrePass
Specify whether this object should be rendered in a z-prepass.
bool overlay
Layer based rendering.
void addTexture(const Texture &_t, const size_t _stage, bool _addToShaderGen=true)
unsigned int indexOffset
Offset to first index in the index buffer or vertex buffer respectively.
GLuint vertexBuffer
VBO, IBO ids, ignored if VAO is provided.
GLenum primitiveMode
Primitive type.
GLenum alphaFunc
GL_LESS, GL_LEQUAL, GL_GREATER ..
unsigned int patchVertices
patch size if primitiveMode is GL_PATCHES for rendering with tessellation shaders
GLenum blendDest
glBlendFunc: GL_SRC_ALPHA, GL_ZERO, GL_ONE, GL_ONE_MINUS_SRC_ALPHA ...
unsigned int internalFlags_
may be used internally by the renderer
Vec2f depthRange
glDepthRange: (znear, zmax)
GLenum indexType
Index element type.
GLenum depthFunc
GL_LESS, GL_LEQUAL, GL_GREATER ..
void clearTextures()
clear all textures. Also affected on shaderDesc