RioEngine  0.1
My first attempt to create a 3D WYSIWYG Game Engine
qglbaseviewport.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 <QtOpenGL>
31 #include "constants.h"
32 #include "misc/btcustom.h"
33 #include "misc/directoryhelper.hpp"
34 #include "cengine/ccamera.h"
35 #include "cengine/cdirector.h"
36 #include "clengine/clrenderer.h"
38 
39 //-----------------------------------------------------------------------------
40 
41 QGLBaseViewport::QGLBaseViewport(QWidget*, QGLWidget* shared_widget)
42  : QGLWidget(0, shared_widget) {
43  m_drawMode = TEXTURED;
44  projectionZNear = k_QGLViewport_zNear;
45  projectionZFar = k_QGLViewport_zFar;
46  projectionFovYAngle = k_QGLViewport_fovYAngle;
47  m_clearColor = QColor(160, 200, 250); // TODO move this values to constants.h
51 
52  setFocusPolicy(Qt::ClickFocus);
53  setHasFocus(false);
55 }
56 
57 //-----------------------------------------------------------------------------
58 
60  return m_drawMode;
61 }
62 
63 //-----------------------------------------------------------------------------
64 
66  m_drawMode = value;
67 }
68 
69 //-----------------------------------------------------------------------------
70 
72  return m_hasFocus;
73 }
74 
75 //-----------------------------------------------------------------------------
76 
78  m_projectionRatio = 1.0f * width() / height();
79 }
80 
81 //-----------------------------------------------------------------------------
82 
84  glEnable(GL_DEPTH_TEST);
85  glEnable(GL_CULL_FACE);
86 
87 #define PROGRAM_VERTEX_ATTRIBUTE 0
88 #define PROGRAM_TEXCOORD_ATTRIBUTE 1
89 
90  QString path = QString::fromStdString(DirectoryHelper::getProjectDirectory()) + k_QGLViewport_ShadersFolderName + "\\";
91 
92  QGLShader *solidColor_vshader = new QGLShader(QGLShader::Vertex, this);
93  bool result = solidColor_vshader->compileSourceFile(path + "solidcolor.vert");
94 
95  QGLShader *solidColor_fshader = new QGLShader(QGLShader::Fragment, this);
96  result = solidColor_fshader->compileSourceFile(path + "solidcolor.frag");
97 
98  m_solidColorsProgram = new QGLShaderProgram(this);
99  m_solidColorsProgram->addShader(solidColor_vshader);
100  m_solidColorsProgram->addShader(solidColor_fshader);
101  m_solidColorsProgram->bindAttributeLocation("vertex", 0);
102  m_solidColorsProgram->bindAttributeLocation("texCoord", 1);
103  m_solidColorsProgram->bindAttributeLocation("color", 2);
104  result = m_solidColorsProgram->link();
105  result = m_solidColorsProgram->bind();
106 
107  QGLShader *vshader = new QGLShader(QGLShader::Vertex, this);
108  result = vshader->compileSourceFile(path + "simple.vert");
109 
110  QGLShader *fshader = new QGLShader(QGLShader::Fragment, this);
111  result = fshader->compileSourceFile(path + "simple.frag");
112 
113  m_texturedProgram = new QGLShaderProgram(this);
114  m_texturedProgram->addShader(vshader);
115  m_texturedProgram->addShader(fshader);
116  m_texturedProgram->bindAttributeLocation("vertex", PROGRAM_VERTEX_ATTRIBUTE);
117  m_texturedProgram->bindAttributeLocation("texCoord", PROGRAM_TEXCOORD_ATTRIBUTE);
118  m_texturedProgram->bindAttributeLocation("solidColor", 10);
119  result = m_texturedProgram->link();
120  result = m_texturedProgram->bind();
121  m_texturedProgram->setUniformValue("texture", 0);
122 }
123 
124 //-----------------------------------------------------------------------------
125 
128 }
129 
130 //-----------------------------------------------------------------------------
131 
133 }
134 
135 //-----------------------------------------------------------------------------
136 
138  preDraw();
139  step();
140 
141  qglClearColor(m_clearColor);
142  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
143 
144  CCamera *active_camera = CDIRECTOR->getActiveCamera();
145  Matrix4 q_model, q_clip, q_camera, q_cameraToClip;
146  q_camera = btCustom::btQuaternionToMatrix4(active_camera->transform().localRotation());
147  q_clip.setPerspective(projectionFovYAngle, m_projectionRatio, projectionZNear, projectionZFar);
148 
149  const btVector3* camera_position = &active_camera->transform().localPosition();
150 
151  btVector3 axis_x = btVector3(q_camera[0], q_camera[4], q_camera[8]);
152  btVector3 axis_y = btVector3(q_camera[1], q_camera[5], q_camera[9]);
153  btVector3 axis_z = btVector3(q_camera[2], q_camera[6], q_camera[10]);
154 
155  btVector3 fixed_location(-axis_x.dot(*camera_position),
156  -axis_y.dot(*camera_position),
157  -axis_z.dot(*camera_position));
158 
159  q_camera[12] = fixed_location.x();
160  q_camera[13] = fixed_location.y();
161  q_camera[14] = fixed_location.z();
162  q_cameraToClip = q_camera * q_clip;
163 
164  QMatrix4x4 qmatrix_model(QMatrix4x4(q_model.get()));
165  QMatrix4x4 qmatrix_camera_to_clip(QMatrix4x4(q_cameraToClip.getTranspose()));
166 
167  m_selectedProgram->setUniformValue("modelMatrix", qmatrix_model);
168  m_selectedProgram->setUniformValue("cameraToClipMatrix", qmatrix_camera_to_clip);
169 
170  m_modelViewMatrix = q_camera;
171 
172  customDraw();
173  postDraw();
174 }
175 
176 //-----------------------------------------------------------------------------
177 
179 }
180 
181 //-----------------------------------------------------------------------------
182 
184 }
185 
186 //-----------------------------------------------------------------------------
187 
188 void QGLBaseViewport::resizeGL(int width, int height) {
190  glViewport(0, 0, width, height);
191 }
192 
193 //-----------------------------------------------------------------------------
194 
195 void QGLBaseViewport::mousePressEvent(QMouseEvent *event) {
196  m_lastPos = event->pos();
197 }
198 
199 //-----------------------------------------------------------------------------
200 
201 void QGLBaseViewport::focusInEvent(QFocusEvent*) {
202  setHasFocus(true);
203 }
204 
205 //-----------------------------------------------------------------------------
206 
207 void QGLBaseViewport::focusOutEvent(QFocusEvent*) {
208  setHasFocus(false);
209 }
210 
211 //-----------------------------------------------------------------------------
212 
213 void QGLBaseViewport::setHasFocus(bool has_focus) {
214  m_hasFocus = has_focus;
215 }
216 
217 //-----------------------------------------------------------------------------
218 
220 }
void setDrawMode(const QGLViewportDrawMode &value)
virtual void paintGL()
#define PROGRAM_TEXCOORD_ATTRIBUTE
Matrix4 m_modelViewMatrix
GLdouble m_projectionRatio
QGLShaderProgram * m_solidColorsProgram
btQuaternion localRotation() const
Definition: transform.cpp:132
virtual void focusInEvent(QFocusEvent *)
virtual void initializeGL()
QGLViewportDrawMode drawMode() const
QGLBaseViewport(QWidget *parent, QGLWidget *shared_widget)
const btVector3 & localPosition(UnitType unit_type=WORLD) const
Definition: transform.cpp:105
#define PROGRAM_VERTEX_ATTRIBUTE
virtual void preDraw()
QGLShaderProgram * m_texturedProgram
void mousePressEvent(QMouseEvent *event)
CameraTransform & transform()
Definition: ccamera.cpp:42
void recalculateAspectRatio()
virtual void customDraw()
bool hasFocus() const
static std::string getProjectDirectory()
virtual void postDraw()
Matrix4 btQuaternionToMatrix4(const btQuaternion &q)
Definition: btcustom.cpp:132
virtual void step()
#define CDIRECTOR
Definition: cdirector.h:37
virtual void resizeGL(int, int)
QGLShaderProgram * m_selectedProgram
virtual void focusOutEvent(QFocusEvent *)