RenderObject.cc 16.3 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
 *   $Revision: 15868 $                                                       *
 *   $Author: tenter $                                                      *
 *   $Date: 2012-11-26 12:37:58 +0100 (Mo, 26 Nov 2012) $                   *
 *                                                                           *
\*===========================================================================*/

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <QFile>
#include <QTextStream>

#include <ACG/GL/gl.hh>

#include <ACG/GL/IRenderer.hh>

#include <ACG/GL/VertexDeclaration.hh>

#include <ACG/GL/ShaderCache.hh>



namespace ACG
{

void RenderObject::initFromState( GLState* _glState )
{
  culling = true;
  blending = false;
  depthTest = true;
  depthWrite = true;
  alphaTest = false;

  colorWriteMask[0] = colorWriteMask[1] = colorWriteMask[2] = colorWriteMask[3] = 1;

  fillMode = GL_FILL;

  depthRange = Vec2f(0.0f, 1.0f);
  depthFunc = GL_LESS;

  blendSrc = GL_ONE;
  blendDest = GL_ZERO;

  alpha = 1.0f;


  if (_glState)
  {
    modelview = _glState->modelview();
    proj = _glState->projection();

    culling =_glState->isStateEnabled(GL_CULL_FACE);
    blending = _glState->isStateEnabled(GL_BLEND);
    depthTest = _glState->isStateEnabled(GL_DEPTH_TEST);

    _glState->getBlendFunc(&blendSrc, &blendDest);

    glGetFloatv(GL_DEPTH_RANGE, depthRange.data());

    depthFunc = _glState->depthFunc();


    alphaTest = _glState->isStateEnabled(GL_ALPHA_TEST);

    for (int i = 0; i < 3; ++i)
    {
      diffuse[i] = _glState->diffuse_color()[i];
      ambient[i] = _glState->ambient_color()[i];
      specular[i] = _glState->specular_color()[i];
      emissive[i] = _glState->base_color()[i];
    }
    shininess = _glState->shininess();
  }


  // get texcoord generation params
  if (glIsEnabled(GL_TEXTURE_GEN_Q))
    shaderDesc.texGenDim = 4;
  else if (glIsEnabled(GL_TEXTURE_GEN_R))
    shaderDesc.texGenDim = 3;
  else if (glIsEnabled(GL_TEXTURE_GEN_T))
    shaderDesc.texGenDim = 2;
  else if (glIsEnabled(GL_TEXTURE_GEN_S))
    shaderDesc.texGenDim = 1;

  if (shaderDesc.texGenDim)
  {
    GLint genMode = 0;
    glGetTexGeniv(GL_S, GL_TEXTURE_GEN_MODE, &genMode);
    shaderDesc.texGenMode = genMode;
  }
}

void RenderObject::setupShaderGenFromDrawmode( const SceneGraph::DrawModes::DrawModeProperties* _props )
{
  if (_props)
  {
    shaderDesc.vertexColors = _props->colored();
    if (_props->textured())
      shaderDesc.addTextureType(GL_TEXTURE_2D,false,0);
    else
      shaderDesc.clearTextures();
    shaderDesc.numLights = _props->lighting() ? 0 : -1;

    switch (_props->lightStage()) {
      case SceneGraph::DrawModes::LIGHTSTAGE_SMOOTH:
        shaderDesc.shadeMode = SG_SHADE_GOURAUD;
        break;
      case SceneGraph::DrawModes::LIGHTSTAGE_PHONG:
        shaderDesc.shadeMode = SG_SHADE_PHONG;
        break;
      case SceneGraph::DrawModes::LIGHTSTAGE_UNLIT:
        shaderDesc.shadeMode = SG_SHADE_UNLIT;
        break;
      default:
        break;
    }

    if (_props->flatShaded())
      shaderDesc.shadeMode = SG_SHADE_FLAT;

165
166
167
    if (_props->normalSource() == SceneGraph::DrawModes::NORMAL_PER_FACE)
      shaderDesc.vertexNormalInterpolator = "flat";

Jan Möbius's avatar
Jan Möbius committed
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
    if (_props->primitive() == SceneGraph::DrawModes::PRIMITIVE_WIREFRAME ||
        _props->primitive() == SceneGraph::DrawModes::PRIMITIVE_HIDDENLINE ||
        _props->primitive() == SceneGraph::DrawModes::PRIMITIVE_EDGE ||
        _props->primitive() == SceneGraph::DrawModes::PRIMITIVE_HALFEDGE)
      shaderDesc.shadeMode = SG_SHADE_UNLIT;
  }
}

void RenderObject::setMaterial( const SceneGraph::Material* _mat )
{
  for (int i = 0; i < 3; ++i)
  {
    diffuse[i] = _mat->diffuseColor()[i];
    ambient[i] = _mat->ambientColor()[i];
    specular[i] = _mat->specularColor()[i];
    emissive[i] = _mat->baseColor()[i];
  }
  shininess = _mat->shininess();
  alpha = _mat->diffuseColor()[3];

  // material node sets the alpha test function to GL_GREATER
  alphaFunc = GL_GREATER;
  alphaRef = _mat->alphaValue();
}


RenderObject::RenderObject()
: priority(0),
  overlay(false),
  modelview(GLMatrixf(ACG::Vec3f(1.0,0.0,0.0),ACG::Vec3f(0.0,1.0,0.0),ACG::Vec3f(0.0,0.0,1.0))),
  proj(modelview),
  vertexArrayObject(0),
  vertexBuffer(0), indexBuffer(0), sysmemIndexBuffer(0),
  primitiveMode(GL_TRIANGLES), patchVertices(0), numIndices(0), indexOffset(0), indexType(GL_UNSIGNED_INT),
  numInstances(0),
  vertexDecl(0), 
  culling(true), blending(false), alphaTest(false),
  depthTest(true), depthWrite(true),
  fillMode(GL_FILL), depthFunc(GL_LESS), 
  alphaFunc(GL_ALWAYS), alphaRef(0.0f),
  blendSrc(GL_SRC_ALPHA), blendDest(GL_ONE_MINUS_SRC_ALPHA),
  depthRange(0.0f, 1.0f), 

  clipDistanceMask(0),

  patchDefaultInnerLevel(1.0f, 1.0f),
  patchDefaultOuterLevel(1.0f, 1.0f, 1.0f, 1.0f),

  diffuse(0.6f, 0.6f, 0.6f), ambient(0.1f, 0.1f, 0.1f),
  specular(0.0f, 0.0f, 0.0f), emissive(0.05f, 0.05f, 0.05f),
  alpha(1.0f), shininess(100.0f),
  
  inZPrePass(true),
  depthMapUniformName(0),

  debugID(0), debugName(0),
  internalFlags_(0)
{
  colorWriteMask[0] = colorWriteMask[1] = colorWriteMask[2] = colorWriteMask[3] = 1;
}

RenderObject::~RenderObject() {
  uniformPool_.clear();
}

QString RenderObject::toString() const
{
  // several mappings: (int)GLEnum -> string

  const char* primitiveString[] = 
  {
    "GL_POINTS",
    "GL_LINES",
    "GL_LINE_LOOP",
    "GL_LINE_STRIP",
    "GL_TRIANGLES",
    "GL_TRIANGLE_STRIP",
    "GL_TRIANGLE_FAN",
    "GL_QUADS",
    "GL_QUAD_STRIP",
    "GL_POLYGON",
    "GL_LINES_ADJACENCY",
    "GL_LINE_STRIP_ADJACENCY",
    "GL_TRIANGLES_ADJACENCY",
    "GL_TRIANGLE_STRIP_ADJACENCY",
    "GL_PATCHES"
  };

  const char* fillModeString[] = 
  {
    "GL_POINT",
    "GL_LINE",
    "GL_FILL"
  };

  const char* depthFunString[] =
  {
    "GL_NEVER",
    "GL_LESS",
    "GL_EQUAL",
    "GL_LEQUAL",
    "GL_GREATER",
    "GL_NOTEQUAL",
    "GL_GEQUAL",
    "GL_ALWAYS"
  };

  QString result;
  QTextStream resultStrm(&result);


#if !defined(GL_VERSION_3_2)
  const GLenum maxSupportedPrimitiveMode = GL_POLYGON;
#elif !defined(GL_ARB_tessellation_shader)
  const GLenum maxSupportedPrimitiveMode = GL_TRIANGLE_STRIP_ADJACENCY;
#else
  const GLenum maxSupportedPrimitiveMode = GL_PATCHES;
#endif

  resultStrm << "name: " << debugName
             << "\ndebugID: " << debugID
             << "\npriority: " << priority
             << "\nprimitiveMode: " << (primitiveMode <= maxSupportedPrimitiveMode ? primitiveString[primitiveMode] : "undefined")
             << "\nfillMode: " << fillModeString[fillMode - GL_POINT]
             << "\nnumIndices: " << numIndices
             << "\nindexOffset: " << indexOffset;


#ifdef GL_ARB_tessellation_shader
  if (primitiveMode == GL_PATCHES)
    resultStrm << "\npatchVertices: " << patchVertices;
#endif

  resultStrm << "\nvao-id: " << vertexArrayObject
             << "\nvbo-id: " << vertexBuffer
             << "\nibo-id: " << indexBuffer
             << "\nsysmemIndexBuffer: " << sysmemIndexBuffer;



  resultStrm << "\n" << shaderDesc.toString();


  resultStrm << "\nmodelview: " 
    << "{" << modelview(0, 0) << ", " << modelview(0, 1) << ", " << modelview(0, 2) << ", " << modelview(0, 3) << "} "
    << "{" << modelview(1, 0) << ", " << modelview(1, 1) << ", " << modelview(1, 2) << ", " << modelview(1, 3) << "} "
    << "{" << modelview(2, 0) << ", " << modelview(2, 1) << ", " << modelview(2, 2) << ", " << modelview(2, 3) << "} "
    << "{" << modelview(3, 0) << ", " << modelview(3, 1) << ", " << modelview(3, 2) << ", " << modelview(3, 3) << "} ";

  resultStrm << "\nproj: " 
    << "{" << proj(0, 0) << ", " << proj(0, 1) << ", " << proj(0, 2) << ", " << proj(0, 3) << "} "
    << "{" << proj(1, 0) << ", " << proj(1, 1) << ", " << proj(1, 2) << ", " << proj(1, 3) << "} "
    << "{" << proj(2, 0) << ", " << proj(2, 1) << ", " << proj(2, 2) << ", " << proj(2, 3) << "} "
    << "{" << proj(3, 0) << ", " << proj(3, 1) << ", " << proj(3, 2) << ", " << proj(3, 3) << "} ";


  resultStrm << "\nculling: " << culling
    << "\nblending: " << blending
    << "\nalphaTest: " << alphaTest;


  resultStrm << "\ndepthTest: " << depthTest
    << "\ndepthWrite: " << depthWrite
    << "\ndepthFunc: " << depthFunString[depthFunc - GL_NEVER]
    << "\ndepthRange: [" << depthRange[0] << ", " << depthRange[1] << "]"
    << "\ncolorWriteMask: " << colorWriteMask[0] << ", " << colorWriteMask[1] << ", "<< colorWriteMask[2] << ", "<< colorWriteMask[2];

  resultStrm << "\nalphaFunc: " << depthFunString[alphaFunc - GL_NEVER]
    << "\nalphaRef: " << alphaRef;

  resultStrm << "\ndiffuse: [" << diffuse[0] << ", " << diffuse[1] << ", " << diffuse[2] << "]";
  resultStrm << "\nambient: [" << ambient[0] << ", " << ambient[1] << ", " << ambient[2] << "]";
  resultStrm << "\nspecular: [" << specular[0] << ", " << specular[1] << ", " << specular[2] << "]";
  resultStrm << "\nemissive: [" << emissive[0] << ", " << emissive[1] << ", " << emissive[2] << "]";

  resultStrm << "\nshininess: " << shininess;
  resultStrm << "\nalpha: " << alpha;


  resultStrm << "\ninZPrePass: " << inZPrePass;
  resultStrm << "\ndepthMapUniformName: " << depthMapUniformName;


  resultStrm << "\ninternalFlags: " << internalFlags_;

  // textures
  for (std::map<size_t, Texture>::const_iterator it = textures_.begin(); it != textures_.end(); ++it)
  {
    resultStrm << "\ntexture unit " << it->first << ": ";

    switch (it->second.type)
    {
    case GL_TEXTURE_1D: resultStrm << "GL_TEXTURE_1D"; break;
    case GL_TEXTURE_2D: resultStrm << "GL_TEXTURE_2D"; break;
    case GL_TEXTURE_3D: resultStrm << "GL_TEXTURE_3D"; break;
#ifdef GL_TEXTURE_RECTANGLE
    case GL_TEXTURE_RECTANGLE: resultStrm << "GL_TEXTURE_RECTANGLE"; break;
#endif
#ifdef GL_TEXTURE_CUBE_MAP
    case GL_TEXTURE_CUBE_MAP: resultStrm << "GL_TEXTURE_CUBE_MAP"; break;
#endif 
#ifdef GL_TEXTURE_1D_ARRAY
    case GL_TEXTURE_1D_ARRAY: resultStrm << "GL_TEXTURE_1D_ARRAY"; break;
#endif 
#ifdef GL_TEXTURE_2D_ARRAY
    case GL_TEXTURE_2D_ARRAY: resultStrm << "GL_TEXTURE_2D_ARRAY"; break;
#endif 
#ifdef GL_TEXTURE_CUBE_MAP_ARRAY
    case GL_TEXTURE_CUBE_MAP_ARRAY: resultStrm << "GL_TEXTURE_CUBE_MAP_ARRAY"; break;
#endif
#ifdef GL_TEXTURE_BUFFER
    case GL_TEXTURE_BUFFER: resultStrm << "GL_TEXTURE_BUFFER"; break;
#endif
#ifdef GL_TEXTURE_2D_MULTISAMPLE
    case GL_TEXTURE_2D_MULTISAMPLE: resultStrm << "GL_TEXTURE_2D_MULTISAMPLE"; break;
#endif
#ifdef GL_TEXTURE_2D_MULTISAMPLE_ARRAY
    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY: resultStrm << "GL_TEXTURE_2D_MULTISAMPLE_ARRAY"; break;
#endif
    default: resultStrm << "unknown_type"; break;
    }

    resultStrm << " - id " << it->second.id;
  }

  if (vertexDecl)
    resultStrm << "\n" << vertexDecl->toString();


  return result;
}


void RenderObject::setUniform( const char *_name, GLint _value )
{
  uniformPool_.setUniform(_name, _value);
}

void RenderObject::setUniform( const char *_name, GLfloat _value )
{
  uniformPool_.setUniform(_name, _value);
}

void RenderObject::setUniform( const char *_name, const ACG::Vec2f &_value )
{
  uniformPool_.setUniform(_name, _value);
}

void RenderObject::setUniform( const char *_name, const ACG::Vec3f &_value )
{
  uniformPool_.setUniform(_name, _value);
}

void RenderObject::setUniform( const char *_name, const ACG::Vec4f &_value )
{
  uniformPool_.setUniform(_name, _value);
}

void RenderObject::setUniform( const char *_name, const ACG::GLMatrixf &_value, bool _transposed /*= false*/ )
{
  uniformPool_.setUniform(_name, _value, _transposed);
}

void RenderObject::setUniformMat3( const char *_name, const ACG::GLMatrixf &_value, bool _transposed /*= false*/ )
{
  uniformPool_.setUniform(_name, _value, _transposed);
}

void RenderObject::setUniform( const char *_name, GLint *_values, int _count )
{
  uniformPool_.setUniform(_name, _values, _count);
}

void RenderObject::setUniform( const char *_name, GLfloat *_values, int _count )
{
  uniformPool_.setUniform(_name, _values, _count);
}

void RenderObject::addUniformPool( const GLSL::UniformPool& _pool )
{
  uniformPool_.addPool(_pool);
}


void RenderObject::setupLineRendering( float _lineWidth, const Vec2f& _screenSize )
{
  shaderDesc.geometryTemplateFile = "Wireframe/geom_line2quad.tpl";

  setUniform("lineWidth", _lineWidth);
  setUniform("screenSize", _screenSize);
}

bool RenderObject::isDefaultLineObject() const
{
  return shaderDesc.geometryTemplateFile == "Wireframe/geom_line2quad.tpl" ||
    (shaderDesc.geometryTemplateFile == "Wireframe/gl42/geometry.tpl" &&
    shaderDesc.fragmentTemplateFile == "Wireframe/gl42/fragment.tpl");
}

void RenderObject::resetLineRendering()
{
  shaderDesc.geometryTemplateFile.clear();
}

void RenderObject::setupPointRendering( float _pointSize, const Vec2f& _screenSize )
{
  shaderDesc.geometryTemplateFile = "PointSize/geometry.tpl";
  shaderDesc.fragmentTemplateFile = "PointSize/fragment.tpl";

  setUniform("pointSize", _pointSize);
  setUniform("screenSize", _screenSize);
}

bool RenderObject::isDefaultPointObject() const
{
  return shaderDesc.geometryTemplateFile == "PointSize/geometry.tpl" &&
    shaderDesc.fragmentTemplateFile == "PointSize/fragment.tpl";
}

void RenderObject::resetPointRendering()
{
  shaderDesc.geometryTemplateFile.clear();
  shaderDesc.fragmentTemplateFile.clear();
}



} // namespace ACG end