RioEngine  0.1
My first attempt to create a 3D WYSIWYG Game Engine
qglgameviewport.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/matrixstack.h"
32 #include "misc/stringhelper.hpp"
33 #include "misc/directoryhelper.hpp"
34 #include "cengine/time_.h"
35 #include "cengine/cgameobject.h"
36 #include "cengine/ccamera.h"
37 #include "cengine/cdirector.h"
38 #include "cengine/cgamescene.h"
40 #include "clengine/clrenderer.h"
43 
44 //-----------------------------------------------------------------------------
45 
47  QGLWidget* shared_widget) : QGLBaseViewport(this, shared_widget) {
48  setColladaScene(collada_scene);
49  m_bDebugDraw = NULL;
50 }
51 
52 //-----------------------------------------------------------------------------
53 
55  RE_ASSERT(collada_scene);
56  m_colladaScene = collada_scene;
57 }
58 
59 //-----------------------------------------------------------------------------
60 
62  return m_colladaScene;
63 }
64 
65 //-----------------------------------------------------------------------------
66 
68  CLNode* skybox_node = m_colladaScene->getNodeHavingId(k_CLScene_SkyboxNodeId);
69  if (skybox_node) {
71  }
72 }
73 
74 //-----------------------------------------------------------------------------
75 
77  CLRenderer::drawLine(m_texturedProgram, btVector3(-k_QGLGameViewport_WorldAxisLength, 0, 0),
78  btVector3(k_QGLGameViewport_WorldAxisLength, 0, 0), &QColor(255, 0, 0));
79 
80  CLRenderer::drawLine(m_texturedProgram, btVector3(0, -k_QGLGameViewport_WorldAxisLength, 0),
81  btVector3(0, k_QGLGameViewport_WorldAxisLength, 0), &QColor(0, 255, 0));
82 
83  CLRenderer::drawLine(m_texturedProgram, btVector3(0, 0, -k_QGLGameViewport_WorldAxisLength),
84  btVector3(0, 0, k_QGLGameViewport_WorldAxisLength), &QColor(0, 0, 255));
85 }
86 
87 //-----------------------------------------------------------------------------
88 
90  btScalar raw_matrix[16];
91  node->transform().global().getOpenGLMatrix(raw_matrix);
92  Matrix4 matrix(raw_matrix);
93  MATRIXSTACK->setMatrix(matrix.getTranspose());
95  QObjectList children = node->children();
96 
97  for (QObjectList::iterator it = children.begin();
98  it != children.end();
99  ++it) {
100  CGameObject* gameobj_child = qobject_cast<CGameObject*>(*it);
101  if (gameobj_child != NULL) {
102  drawGameObject(gameobj_child);
103  }
104  }
105 }
106 
107 //-----------------------------------------------------------------------------
108 
110  CDIRECTOR->step();
111 }
112 
113 //-----------------------------------------------------------------------------
114 
117  if (!CDIRECTOR->currentGameScene()->dynamicsWorld()->getDebugDrawer()) {
118  m_bDebugDraw = new bDebugDraw(m_texturedProgram);
119  CDIRECTOR->currentGameScene()->dynamicsWorld()->setDebugDrawer(m_bDebugDraw);
120  }
121  bindTextures();
122 
123  emit afterInitializeGL(this);
124 }
125 
126 //-----------------------------------------------------------------------------
127 
130 
131  if (CDIRECTOR->gameState() == CDirector::RUNNING) {
132  Time::___update();
133  }
134 }
135 
136 //-----------------------------------------------------------------------------
137 
139  if (CDIRECTOR->currentGameScene()) {
140  for (std::vector<CGameObject*>::iterator it = CDIRECTOR->currentGameScene()->getGameObjects()->begin();
141  it != CDIRECTOR->currentGameScene()->getGameObjects()->end();
142  ++it) {
143  MATRIXSTACK->pushMatrix();
144  drawGameObject(*it);
145  MATRIXSTACK->popMatrix();
146  }
147  }
148 
149  // draw sky box
150  MATRIXSTACK->pushMatrix();
151  MATRIXSTACK->translate(CDIRECTOR->getActiveCamera()->transform().localPosition());
152  MATRIXSTACK->rotate(DEGTORAD(90.0f), 0, 0);
153  MATRIXSTACK->scale(5000.0f);
154  drawSkybox();
155  MATRIXSTACK->popMatrix();
156 
157  // draw debug lines
158  CDIRECTOR->currentGameScene()->dynamicsWorld()->debugDrawWorld(); // generates debug lines
159  glDisable(GL_DEPTH_TEST);
160  MATRIXSTACK->pushMatrix();
161  bDebugDraw* debug_drawer = static_cast<bDebugDraw*>(CDIRECTOR->currentGameScene()->dynamicsWorld()->getDebugDrawer());
162  RE_ASSERT(debug_drawer != NULL);
164  debug_drawer->lines());
165 
166  MATRIXSTACK->popMatrix();
167  debug_drawer->clearLines();
168  MATRIXSTACK->pushMatrix();
169  drawWorldAxis();
170  MATRIXSTACK->popMatrix();
171  glEnable(GL_DEPTH_TEST);
172 }
173 
174 //-----------------------------------------------------------------------------
175 
177  if (CDIRECTOR->gameState() == CDirector::RUNNING) {
178  CDIRECTOR->currentGameScene()->postUpdate();
179  }
180 
181  drawCrosshair(); // HACK
182 }
183 
184 //-----------------------------------------------------------------------------
185 
187  const ImagesMapType* images_map = m_colladaScene->images();
188  for (ImagesMapType::const_iterator i = images_map->begin();
189  i != images_map->end();
190  ++i) {
191  CLImageDetails* img_detail = i->second;
192  if (img_detail->texture == 0) {
193  std::string fixed_filename = DirectoryHelper::getAssetsDirectory() + img_detail->filename;
194  GLuint texture_id = bindTexture(QPixmap(QString::fromStdString(fixed_filename)),
195  GL_TEXTURE_2D);
196  img_detail->texture = texture_id;
197  }
198  }
199 }
200 
201 //-----------------------------------------------------------------------------
202 
204  MATRIXSTACK->pushMatrix();
205  MATRIXSTACK->identity();
206  m_selectedProgram->setUniformValue(
207  "cameraToClipMatrix", QMatrix4x4());
210  btVector3(-k_QGLGameViewport_CrosshairXLength, 0, 0),
211  btVector3(k_QGLGameViewport_CrosshairXLength, 0, 0),
212  &QColor(255, 255, 255));
215  btVector3(0, -k_QGLGameViewport_CrosshairYLength, 0),
216  btVector3(0, k_QGLGameViewport_CrosshairYLength, 0),
217  &QColor(255, 255, 255));
218  MATRIXSTACK->popMatrix();
219 }
220 
221 //-----------------------------------------------------------------------------
222 
224  SAFE_RELEASE(m_bDebugDraw);
225 }
virtual void postDraw()
CLScene * m_colladaScene
virtual void drawCrosshair()
void getOpenGLMatrix(btScalar *m)
btCustomTransform global(bool scale=true) const
Definition: transform.cpp:322
static bool drawDebugLines(QGLShaderProgram *program, std::map< QString, std::vector< bDebugDraw::LineInfo >> *lines)
Definition: clrenderer.cpp:172
virtual void drawWorldAxis()
virtual void drawGameObject(CGameObject *game_object)
const ImagesMapType * images() const
Definition: clscene.cpp:360
std::string filename
Definition: typedefs.h:52
virtual void initializeGL()
std::map< QString, std::vector< LineInfo > > * lines()
Definition: bdebugdraw.cpp:44
void setColladaScene(CLScene *collada_scene)
virtual void customDraw()
Definition: clnode.h:37
virtual void drawSkybox()
void afterInitializeGL(QGLGameViewport *game_viewport)
std::map< std::string, CLImageDetails * > ImagesMapType
Definition: typedefs.h:62
virtual void preDraw()
#define MATRIXSTACK
Definition: matrixstack.h:38
QGLShaderProgram * m_texturedProgram
CLScene * colladaScene() const
unsigned int texture
Definition: typedefs.h:54
virtual void preDraw()
virtual void step()
static void ___update()
Definition: time_.h:74
CLNode * getNodeHavingId(const std::string &node_id)
Definition: clscene.cpp:111
#define SAFE_RELEASE(x)
Definition: cobject.h:36
virtual void initializeGL()
#define RE_ASSERT
Definition: macro.h:57
#define DEGTORAD(x)
Definition: macro.h:52
void clearLines()
Definition: bdebugdraw.cpp:50
#define CDIRECTOR
Definition: cdirector.h:37
static bool drawLine(QGLShaderProgram *program, btVector3 &from, btVector3 &to, QColor *color)
Definition: clrenderer.cpp:143
QGLGameViewport(CLScene *collada_scene, QGLWidget *shared_widget)
QGLShaderProgram * m_selectedProgram
static std::string getAssetsDirectory()
Transform & transform()
Definition: clnode.cpp:69
static bool drawNode(QGLShaderProgram *program, CLScene *collada_scene, CLNode *node, QColor *color)
Definition: clrenderer.cpp:41