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  * *
44  * $Revision: 15868 $ *
45  * $Author: tenter $ *
46  * $Date: 2012-11-26 12:37:58 +0100 (Mo, 26 Nov 2012) $ *
47  * *
48 \*===========================================================================*/
49 
50 #pragma once
51 
52 
53 #include <ACG/GL/gl.hh>
54 #include <ACG/Math/GLMatrixT.hh>
55 #include <ACG/GL/ShaderGenerator.hh>
56 #include <ACG/ShaderUtils/UniformPool.hh>
57 
58 #include <map>
59 
60 
61 namespace GLSL{
62  class Program;
63 }
64 
65 namespace ACG
66 {
67 
68 // forward declaration
69 class VertexDeclaration;
70 class GLState;
71 
72 namespace SceneGraph {
73  namespace DrawModes {
74  class DrawModeProperties;
75  }
76  class Material;
77 }
78 
79 
105 struct ACGDLLEXPORT RenderObject
106 {
107  friend class IRenderer;
108 
112  RenderObject();
113 
114  ~RenderObject();
115 
116 
123  int priority;
124 
128  std::string name;
129 
142  bool overlay;
143 
146 
149 
150 
151  //===========================================================================
155  //===========================================================================
156 
166 
167 
169  GLuint vertexBuffer,
170  indexBuffer;
171 
180  const void* sysmemIndexBuffer;
181 
190 
192  unsigned int patchVertices; // GL_PATCH_VERTICES
193 
195  unsigned int numIndices;
196 
198  unsigned int indexOffset;
199 
204  GLenum indexType;
205 
211  GLsizei numInstances;
212 
213 
216 
233 
234  // opengl states
235  // queried from glState in initFromState()
236  bool culling;
237  bool blending;
238  bool alphaTest;
239  bool depthTest;
240  bool depthWrite;
241 
242  GLenum fillMode; // GL_POINT, GL_LINE, GL_FILL, default: GL_FILL
243 
244  GLboolean colorWriteMask[4]; // {r,g,b,a}, default: all true
245 
246 // GLenum shadeModel; // GL_FACE, GL_SMOOTH obsolete in shader pipeline
247  GLenum depthFunc;
248 
249  // alpha testing function
250  GLenum alphaFunc;
251  float alphaRef; // reference value for alpha function
252 
253  // alpha blending
254  GLenum blendSrc, blendDest;
255 
257 
258 
259  // enable/disable clip planes
260  // bit i decides whether the vertex shader output gl_ClipDistance[i] is enabled or disabled
261  // default: all zero (disabled)
262  GLuint clipDistanceMask;
263 
264  // ---------------------------
265  // default tessellation lod, if only a tess-eval, but no tess-control shader is specified
266  // this is ignored otherwise
267 
268  Vec2f patchDefaultInnerLevel; // GL_PATCH_DEFAULT_INNER_LEVEL
269  Vec4f patchDefaultOuterLevel; // GL_PATCH_DEFAULT_OUTER_LEVEL
270 
271  // ---------------------------
274  ambient,
275  specular,
276  emissive;
277 
278  float alpha,
279  shininess;
280 
281 
291 
301  const char* depthMapUniformName;
302 
303 
312  struct Texture
313  {
314  GLuint id;
315  GLenum type;
316  bool shadow;
317  Texture(GLuint _id = 0, GLenum _type = GL_TEXTURE_2D, bool _shadow = false):
318  id(_id),
319  type(_type),
320  shadow(_shadow){}
321  };
322 
323 
325  void addTexture(const Texture& _t)
326  {
327  addTexture(_t,numTextures());
328  }
329 
333  void addTexture(const Texture& _t,const size_t _stage, bool _addToShaderGen = true)
334  {
335  if (GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS < numTextures())
336  {
337  std::cerr << "Texturestage " << _stage << " is too big. Allowed stages: "<< GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS << std::endl;
338  return;
339  }
340  textures_[_stage] = _t;
341  if (_addToShaderGen)
342  shaderDesc.addTextureType(_t.type,_t.shadow,_stage);
343  }
344 
346  void clearTextures() {textures_.clear(); shaderDesc.clearTextures();}
347 
348  const std::map<size_t,Texture>& textures(){return textures_;}
349 
350  size_t numTextures() {return textures_.size();}
351 
352 private:
354  std::map<size_t,Texture> textures_;
355 public:
356 
357 
359  int debugID;
360  const char* debugName;
361 
363  unsigned int internalFlags_;
364 
365 
366  // opengl style helper function interface:
367  // provided for easier setup of RenderObjects,
368  // usage is not necessary
369  inline void glBindBuffer(GLenum target, GLuint buffer)
370  {
371  switch (target)
372  {
373  case GL_ARRAY_BUFFER: vertexBuffer = buffer; break;
374  case GL_ELEMENT_ARRAY_BUFFER: indexBuffer = buffer; break;
375  }
376  }
377 
378  inline void glDrawArrays(GLenum mode, GLint first, GLsizei count)
379  {
380  this->glDrawInstancedArrays(mode, first, count, 0);
381  }
382 
383  inline void glDrawInstancedArrays(GLenum mode, GLint first, GLsizei count, GLsizei primcount)
384  {
385  indexBuffer = 0;
386  sysmemIndexBuffer = 0;
387 
388  primitiveMode = mode;
389  indexOffset = first;
390  numIndices = count;
391  numInstances = primcount;
392  }
393 
394  inline void glDrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices)
395  {
396  this->glDrawElementsInstanced(mode, count, type, indices, 0);
397  }
398 
399  inline void glDrawElementsInstanced(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, GLsizei primcount)
400  {
401  primitiveMode = mode;
402  numIndices = count;
403  indexType = type;
404 
405  sysmemIndexBuffer = indices;
406 
407  numInstances = primcount;
408  }
409 
410  inline void glColorMask(GLboolean r, GLboolean g, GLboolean b, GLboolean a)
411  {
412  colorWriteMask[0] = r; colorWriteMask[1] = g; colorWriteMask[2] = b; colorWriteMask[3] = a;
413  }
414 
415  inline void glAlphaFunc(GLenum func, float ref)
416  {
417  alphaFunc = func;
418  alphaRef = ref;
419  }
420 
425  void initFromState(GLState* _glState);
426 
427  void setMaterial(const SceneGraph::Material* _mat);
428 
431  void setupShaderGenFromDrawmode(const SceneGraph::DrawModes::DrawModeProperties* _props);
432 
433 
444  void setupLineRendering(float _lineWidth, const Vec2f& _screenSize);
445 
447  bool isDefaultLineObject() const;
448 
450  void resetLineRendering();
451 
458  void setupPointRendering(float _pointSize, const Vec2f& _screenSize);
459 
461  bool isDefaultPointObject() const;
462 
464  void resetPointRendering();
465 
466 
467 
470  QString toString() const;
471 
478  void setUniform(const char *_name, GLint _value);
479 
486  void setUniform(const char *_name, GLfloat _value);
487 
494  void setUniform(const char *_name, const ACG::Vec2f &_value);
495 
502  void setUniform(const char *_name, const ACG::Vec3f &_value);
503 
510  void setUniform(const char *_name, const ACG::Vec4f &_value);
511 
512  void setUniform(const char *_name, const ACG::GLMatrixf &_value, bool _transposed = false);
513  void setUniformMat3(const char *_name, const ACG::GLMatrixf &_value, bool _transposed = false);
514 
515 
516  void setUniform(const char *_name, GLint *_values, int _count);
517  void setUniform(const char *_name, GLfloat *_values, int _count);
518 
519 
525  void addUniformPool(const GLSL::UniformPool& _pool);
526 
527 private:
528  GLSL::UniformPool uniformPool_;
529 };
530 
531 
539 class ACGDLLEXPORT RenderObjectModifier
540 {
541 public:
542  RenderObjectModifier(const std::string& _name = "") : name_(_name) {}
543  virtual ~RenderObjectModifier() {}
544 
550  virtual void apply(RenderObject* _obj) = 0;
551 
553  const std::string& name() const { return name_; }
554 
555 private:
556  std::string name_;
557 };
558 
559 
560 
561 //=============================================================================
562 } // namespace ACG
563 //=============================================================================
Vec3f diffuse
material definitions
void clearTextures()
disables texture support and removes all texture types
int debugID
used internally for renderer debugging
void addTexture(const Texture &_t)
adds a texture to stage RenderObjects::numTextures()
DrawModeProperties stores a set of properties that defines, how to render an object.
Definition: DrawModes.hh:183
GLSL uniform pool.
Definition: UniformPool.hh:71
bool overlay
Layer based rendering.
void addTexture(const Texture &_t, const size_t _stage, bool _addToShaderGen=true)
GLenum primitiveMode
Primitive type.
void addTextureType(GLenum _type, bool _shadow, size_t _stage)
adds a texture type to the shader and enables texturing.
void clearTextures()
clear all textures. Also affected on shaderDesc
int priority
Priority to allow sorting of objects.
std::map< size_t, Texture > textures_
holds the textures (second) and the stage id (first)
Class to define the vertex input layout.
unsigned int internalFlags_
may be used internally by the renderer
const VertexDeclaration * vertexDecl
Defines the vertex buffer layout, ignored if VAO is provided.
const void * sysmemIndexBuffer
Use system memory index buffer.
Interface for modifying render objects.
GLuint vertexArrayObject
Use vertex array object.
GLenum indexType
Index element type.
unsigned int patchVertices
patch size if primitiveMode is GL_PATCHES for rendering with tessellation shaders ...
unsigned int numIndices
Number indices to render.
GLMatrixd modelview
Modelview transform.
GLenum blendDest
glBlendFunc: GL_SRC_ALPHA, GL_ZERO, GL_ONE, GL_ONE_MINUS_SRC_ALPHA ...
Texture to be used.
unsigned int indexOffset
Offset to first index in the index buffer or vertex buffer respectively.
std::string name
Name for logging.
const std::string & name() const
Get name of the modifier.
Namespace providing different geometric functions concerning angles.
Definition: DBSCANT.cc:51
GLenum depthFunc
GL_LESS, GL_LEQUAL, GL_GREATER ..
const char * depthMapUniformName
Uniform name of the depth map in the used shader.
GLenum alphaFunc
GL_LESS, GL_LEQUAL, GL_GREATER ..
This namespace contains all the classes and functions for handling GLSL shader and program objects...
Definition: AntiAliasing.hh:75
bool inZPrePass
Specify whether this object should be rendered in a z-prepass.
GLMatrixd proj
Projection transform.
Interface class between scenegraph and renderer.
GLuint vertexBuffer
VBO, IBO ids, ignored if VAO is provided.
ShaderGenDesc shaderDesc
Drawmode and other shader params.
Vec2f depthRange
glDepthRange: (znear, zmax)