RioEngine  0.1
My first attempt to create a 3D WYSIWYG Game Engine
clrenderer.cpp
Go to the documentation of this file.
1 //--------------------------------------------------------------- @License begins
2 // RioEngine: The late night Coke -without whores- debugging sessions
3 // 2012-2015 Leopoldo Lomas Flores. Torreon, Coahuila. MEXICO
4 // leopoldolomas [at] gmail
5 // www.rioengine.com
6 // www.leopoldolomas.info
7 // "You have a problem, you face it like a man."
8 //
9 // This is free and unencumbered software released into the public domain.
10 //
11 // Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
12 // software, either in source code form or as a compiled binary, for any purpose,
13 // commercial or non-commercial, and by any means.
14 //
15 // In jurisdictions that recognize copyright laws, the author or authors of this
16 // software dedicate any and all copyright interest in the software to the public
17 // domain. We make this dedication for the benefit of the public at large and to
18 // the detriment of our heirs and successors. We intend this dedication to be
19 // an overt act of relinquishment in perpetuity of all present and future
20 // rights to this software under copyright law.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS
24 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
26 // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //--------------------------------------------------------------- @License ends
29 
30 #include "constants.h"
31 #include "misc/color.h"
32 #include "misc/transform.h"
33 #include "misc/stringhelper.hpp"
34 #include "misc/matrixstack.h"
35 #include "clengine/clpolygon.h"
36 #include "clengine/clscene.h"
37 #include "clengine/clrenderer.h"
38 
39 //-----------------------------------------------------------------------------
40 
41 bool CLRenderer::drawNode(QGLShaderProgram* program, CLScene* collada_scene,
42  CLNode* node, QColor* color) {
43  if (!node->visible()) {
44  return false;
45  }
46 
47  for (unsigned int i = 0; i < node->getInstanceGeometries()->size(); i++) {
48  CLInstanceGeometry *instance_geometry = &(node->getInstanceGeometries()->data()[i]);
49 
50  std::string geometry_id = instance_geometry->getMeshURL();
51  geometry_id = StringHelper::removeOccurrencesOfChar(geometry_id, '#');
52  CLGeometry* geometry_object = collada_scene->getGeometryObjHavingId(geometry_id);
53  QColor renderColor;
54 
55  // let's draw each polygon
56  for (unsigned int i = 0; i < geometry_object->getPolygons()->size(); i++) {
57  CLPolygon* polygon_obj = &(geometry_object->getPolygons()->data()[i]);
58  const GLfloat* vertices = polygon_obj->getTriangleVerticesConstData();
59  const GLfloat* text_coords = polygon_obj->getTriangleMapsConstData();
60 
61  bool has_texture = false;
62  GLuint *texture_id = NULL;
63 
64  if (!color) { // if no color was specified, then try to render the texture {
65  const std::string& material_id = polygon_obj->getMaterialId();
66  if (strcmp(material_id.c_str(), "") != 0) {
67  CLMaterial* material = collada_scene->getMaterialHavingName(material_id);
68 
69  if (material) {
70  // TODO get color from phong effect
71  renderColor = QColor(210, 210, 210);
72 
73  std::string& effect_id = const_cast<std::string&>(material->instanceEffectURL());
74  effect_id = StringHelper::removeOccurrencesOfChar(effect_id, '#');
75  CLEffect* effect = collada_scene->getEffectHavingId(effect_id);
76 
77  if (effect->hasImage()) {
78  texture_id = static_cast<GLuint*>(&collada_scene->getImageDetailsHavingId(effect->imageId())->texture);
79  has_texture = true;
80  program->setUniformValue("renderTexture", 1.0f);
81  } else {
82  program->setUniformValue("renderTexture", 0.0f);
83  }
84  } else {
85  program->setUniformValue("renderTexture", 0.0f);
86  renderColor = QColor(210, 210, 210);
87  }
88  }
89  } else {
90  renderColor = *color;
91  program->setUniformValue("renderTexture", 0.0f);
92  }
93 
94  // if no material was loaded then instantiate text_coords to avoid
95  // a GL error
96  bool release_text_coords = false;
97  if (!text_coords) {
98  text_coords = new GLfloat[polygon_obj->getTriangleVertices()->size() * 2];
99  release_text_coords = true;
100  }
101 
102  GLfloat* color_array = new GLfloat[3];
103  color_array[0] = (float)renderColor.red() / 255.0f;
104  color_array[1] = (float)renderColor.green() / 255.0f;
105  color_array[2] = (float)renderColor.blue() / 255.0f;
106  program->setAttributeValue(10, (const GLfloat*)color_array, 1, 3);
107  SAFE_RELEASE(color_array);
108 
109  const float* opengl_matrix = MATRIXSTACK->getMatrix().get();
110  QMatrix4x4 qmatrix_model = QMatrix4x4(opengl_matrix);
111 
112  float* raw_matrix = qmatrix_model.data();
113 
114  // scale down object's position
115  raw_matrix[12] = raw_matrix[12] * k_QGLGameEditorViewport_SizeFactor;
116  raw_matrix[13] = raw_matrix[13] * k_QGLGameEditorViewport_SizeFactor;
117  raw_matrix[14] = raw_matrix[14] * k_QGLGameEditorViewport_SizeFactor;
118 
119  program->setUniformValue("modelMatrix", qmatrix_model);
120 
121  program->enableAttributeArray(0);
122  program->enableAttributeArray(1);
123  program->setAttributeArray(0, vertices, 3);
124  program->setAttributeArray(1, text_coords, 2);
125 
126  if (has_texture) {
127  glBindTexture(GL_TEXTURE_2D, *texture_id);
128  }
129 
130  glDrawArrays(GL_TRIANGLES, 0, // TODO use GL_LINES to draw wireframe, have in mind that special shaders should be used for wireframe mode
131  polygon_obj->getTriangleVertices()->size());
132 
133  if(release_text_coords) {
134  SAFE_RELEASE_ARRAY(text_coords);
135  }
136  }
137  }
138  return true;
139 }
140 
141 //-----------------------------------------------------------------------------
142 
143 bool CLRenderer::drawLine(QGLShaderProgram* program, btVector3 &from, btVector3 &to, QColor* color) {
144  if (!color) {
145  color = &QColor();
146  }
147 
148  GLfloat color_array[3];
149  color_array[0] = (float)color->red() / 255.0f;
150  color_array[1] = (float)color->green() / 255.0f;
151  color_array[2] = (float)color->blue() / 255.0f;
152  program->setAttributeValue(10, (const GLfloat*)color_array, 1, 3);
153 
154  GLfloat vertices[] = {
155  (GLfloat)(from.x()), (GLfloat)(from.y()), (GLfloat)(from.z()),
156  (GLfloat)(to.x()), (GLfloat)(to.y()), (GLfloat)(to.z()),
157  };
158 
159  QMatrix4x4 mat;
160  mat.setToIdentity();
161  program->setUniformValue("modelMatrix", mat);
162  program->setUniformValue("renderTexture", 0.0f);
163  program->enableAttributeArray(0);
164  program->setAttributeArray(0, vertices, 3);
165  glDrawArrays(GL_LINES, 0, 2);
166 
167  return true;
168 }
169 
170 //-----------------------------------------------------------------------------
171 
172 bool CLRenderer::drawDebugLines(QGLShaderProgram* program, std::map<QString, std::vector<bDebugDraw::LineInfo>>* lines) {
173  QMatrix4x4 mat;
174  mat.setToIdentity();
175  program->setUniformValue("modelMatrix", mat);
176  program->setUniformValue("renderTexture", 0.0f);
177 
178  GLfloat* color_array = NULL;
179  GLfloat* vertices = NULL;
180 
181  for(std::map<QString, std::vector<bDebugDraw::LineInfo>>::const_iterator it = lines->begin();
182  it != lines->end();
183  ++it) {
184  QColor color = QColor(it->first);
185  color_array = new GLfloat[3];
186  color_array[0] = (float)color.red() / 255.0f;
187  color_array[1] = (float)color.green() / 255.0f;
188  color_array[2] = (float)color.blue() / 255.0f;
189  program->setAttributeValue(10, (const GLfloat*)color_array, 1, 3);
190  SAFE_RELEASE(color_array);
191 
192  for(std::vector<bDebugDraw::LineInfo>::const_iterator lineinfo_it = it->second.begin();
193  lineinfo_it != it->second.end();
194  ++lineinfo_it) {
195  vertices = new GLfloat[6];
196  const bDebugDraw::LineInfo *line = &(*lineinfo_it);
197  vertices[0] = (GLfloat)(line->from.x());
198  vertices[1] = (GLfloat)(line->from.y());
199  vertices[2] = (GLfloat)(line->from.z());
200  vertices[3] = (GLfloat)(line->to.x());
201  vertices[4] = (GLfloat)(line->to.y());
202  vertices[5] = (GLfloat)(line->to.z());
203 
204  program->enableAttributeArray(0);
205  program->setAttributeArray(0, vertices, 3);
206  glDrawArrays(GL_LINES, 0, 2);
207 
208  SAFE_RELEASE_ARRAY(vertices);
209  }
210  }
211  return true;
212 }
CArray< CLPolygon > * getPolygons() const
Definition: clgeometry.cpp:80
bool hasImage()
Definition: cleffect.cpp:92
float * getTriangleMapsConstData()
Definition: clpolygon.cpp:159
static bool drawDebugLines(QGLShaderProgram *program, std::map< QString, std::vector< bDebugDraw::LineInfo >> *lines)
Definition: clrenderer.cpp:172
CArray< CLInstanceGeometry > * getInstanceGeometries() const
Definition: clnode.cpp:155
CArray< btVector3 > * getTriangleVertices() const
Definition: clpolygon.cpp:119
Definition: clnode.h:37
static std::string & removeOccurrencesOfChar(std::string &original_str, char char_to_remove)
T * data() const
Definition: carray.h:95
CLMaterial * getMaterialHavingName(const std::string &material_name)
Definition: clscene.cpp:505
#define MATRIXSTACK
Definition: matrixstack.h:38
float * getTriangleVerticesConstData()
Definition: clpolygon.cpp:125
CLGeometry * getGeometryObjHavingId(const std::string &geometry_obj_id)
Definition: clscene.cpp:135
#define SAFE_RELEASE(x)
Definition: cobject.h:36
CLEffect * getEffectHavingId(const std::string &effect_id)
Definition: clscene.cpp:123
#define SAFE_RELEASE_ARRAY(x)
Definition: cobject.h:37
const std::string & imageId() const
Definition: cleffect.cpp:72
const std::string & getMeshURL() const
const std::string & instanceEffectURL() const
Definition: clmaterial.cpp:62
const std::string & getMaterialId() const
Definition: clpolygon.cpp:86
static bool drawLine(QGLShaderProgram *program, btVector3 &from, btVector3 &to, QColor *color)
Definition: clrenderer.cpp:143
unsigned int size() const
Definition: carray.h:104
bool visible() const
Definition: clnode.cpp:87
static bool drawNode(QGLShaderProgram *program, CLScene *collada_scene, CLNode *node, QColor *color)
Definition: clrenderer.cpp:41
CLImageDetails * getImageDetailsHavingId(const std::string &image_id)
Definition: clscene.cpp:129