Simple game engine with complete export to scripting language
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

241 lines
5.0 KiB

  1. #include "RenderDevice.h"
  2. #include "Utilities/Log.h"
  3. #include "Math/Matrix.h"
  4. #include "GL/gl.h"
  5. #include "GL/glu.h"
  6. namespace BlueCore
  7. {
  8. void RenderDevice::WindowResizeSlot(int width, int height)
  9. {
  10. glViewport( 0, 0, width, height);
  11. _ViewportWidth = width;
  12. _ViewportHeight = height;
  13. }
  14. RenderDevice::RenderDevice(RenderWindow* renderWindow) :
  15. _RenderWindow(renderWindow), _ActiveTextures(0)
  16. {
  17. if (_RenderWindow.valid())
  18. {
  19. _RenderWindow->WindowResizeSignal.connect(this,
  20. &RenderDevice::WindowResizeSlot);
  21. _RenderWindow->WindowCloseSignal.connect(this,
  22. &RenderDevice::WindowCloseSlot);
  23. _ViewportWidth = _RenderWindow->getWidth();
  24. _ViewportHeight = _RenderWindow->getHeight();
  25. glViewport( 0, 0, _ViewportWidth, _ViewportHeight);
  26. }
  27. clog << ">>> RenderDevice constructed..." << endlog;
  28. }
  29. RenderDevice::~RenderDevice()
  30. {
  31. clog << ">>> RenderDevice destructed..."<< endlog;
  32. }
  33. void RenderDevice::WindowCloseSlot()
  34. {
  35. DeviceShutdownSignal();
  36. }
  37. int RenderDevice::getViewportWidth()
  38. {
  39. return _ViewportWidth;
  40. }
  41. int RenderDevice::getViewportHeight()
  42. {
  43. return _ViewportHeight;
  44. }
  45. void RenderDevice::begin2D()
  46. {
  47. // prepare state
  48. glDisable(GL_DEPTH_TEST);
  49. glDisable(GL_LIGHTING);
  50. glNormal3f( 0.0, 0.0, 1.0);
  51. // set projection matrix
  52. glMatrixMode(GL_PROJECTION);
  53. glPushMatrix();
  54. glLoadIdentity();
  55. //int newheight = (_width / 16.0) * 9.0;
  56. gluOrtho2D( 0, _ViewportWidth-1, 0, _ViewportHeight-1);
  57. // prepare model matrix
  58. glMatrixMode(GL_MODELVIEW);
  59. glPushMatrix();
  60. glLoadIdentity();
  61. }
  62. void RenderDevice::end2D()
  63. {
  64. // restore projection matrix
  65. glMatrixMode(GL_PROJECTION);
  66. glPopMatrix();
  67. // restore model matrix
  68. glMatrixMode(GL_MODELVIEW);
  69. glPopMatrix();
  70. // restore old state
  71. glEnable(GL_DEPTH_TEST);
  72. glEnable(GL_LIGHTING);
  73. }
  74. void RenderDevice::clear()
  75. {
  76. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  77. }
  78. void RenderDevice::swap()
  79. {
  80. glfwSwapBuffers();
  81. }
  82. void RenderDevice::setAmbientLight(float r, float g, float b)
  83. {
  84. GLfloat lightAmbient[] =
  85. { r, g, b, 1.0f };
  86. glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lightAmbient);
  87. }
  88. void RenderDevice::begin3D(Camera *camera)
  89. {
  90. _Camera = camera;
  91. setupProjectionMatrix();
  92. setupViewMatrix();
  93. }
  94. void RenderDevice::end3D()
  95. {
  96. }
  97. void RenderDevice::setupProjectionMatrix()
  98. {
  99. if (_Camera.valid())
  100. {
  101. Scalar fH = tan( (_Camera->getFov() / 2) / 180 * Pi)
  102. * _Camera->getNearPlane();
  103. Scalar fW = fH * _ViewportWidth / _ViewportHeight;
  104. // setup projectiom matrix
  105. glMatrixMode(GL_PROJECTION);
  106. glLoadIdentity();
  107. glFrustum( -fW, fW, -fH, fH, _Camera->getNearPlane(),
  108. _Camera->getFarPlane());
  109. }
  110. }
  111. void RenderDevice::setupViewMatrix()
  112. {
  113. if (_Camera.valid())
  114. {
  115. glMatrixMode(GL_MODELVIEW);
  116. glLoadIdentity();
  117. Matrix4x4 m(_Camera->getRotation(), _Camera->getRotation().applyInversed(_Camera->getPosition() * -1.0) );
  118. glMultMatrixd( ( GLdouble * ) &m.m );
  119. }
  120. }
  121. void RenderDevice::pushTransformation(const Vector3& position,
  122. const Quaternion& orientation)
  123. {
  124. glMatrixMode( GL_MODELVIEW);
  125. glPushMatrix();
  126. Matrix4x4 m(orientation, position);
  127. glMultMatrixd( ( GLdouble * ) &m.m );
  128. }
  129. void RenderDevice::pushAbsoluteTransformation(const Vector3& position,
  130. const Quaternion& orientation)
  131. {
  132. glMatrixMode( GL_MODELVIEW);
  133. glPushMatrix();
  134. Matrix4x4 m(orientation, position);
  135. glLoadMatrixd( ( GLdouble * ) &m.m );
  136. }
  137. void RenderDevice::popTransformation()
  138. {
  139. glMatrixMode( GL_MODELVIEW);
  140. glPopMatrix();
  141. }
  142. void RenderDevice::setTexture(unsigned int unit, Texture* texture, bool last)
  143. {
  144. glActiveTextureARB(GL_TEXTURE0_ARB + unit);
  145. glBindTexture(GL_TEXTURE_2D, texture->getId());
  146. glEnable(GL_TEXTURE_2D);
  147. if (unit > _ActiveTextures)
  148. _ActiveTextures = unit;
  149. if (last)
  150. {
  151. for (unsigned int i = unit + 1; i < _ActiveTextures; i++)
  152. {
  153. glActiveTextureARB(GL_TEXTURE0_ARB + i);
  154. glDisable( GL_TEXTURE_2D);
  155. }
  156. _ActiveTextures = unit;
  157. }
  158. }
  159. void RenderDevice::setShaderProgram(ShaderProgram shader_program)
  160. {
  161. // if ( !_usingShaders)
  162. // return;
  163. glUseProgramObjectARB(shader_program);
  164. }
  165. void RenderDevice::setTexture(ShaderProgram shader_program,
  166. unsigned int texture, const std::string &name)
  167. {
  168. // if ( !_usingShaders)
  169. // return;
  170. GLint location = glGetUniformLocationARB(shader_program, name.c_str() );
  171. //if( location > 0 )
  172. glUniform1iARB(location, texture);
  173. }
  174. #if 0
  175. void RenderDevice::setTangentBuffer(ShaderProgram shader_program,
  176. const Buffer<Vector3Float>& tangent_buffer, const std::string &name)
  177. {
  178. // if ( !_usingShaders || !tangent_buffer)
  179. // return;
  180. GLuint location = glGetAttribLocationARB(shader_program, name.c_str() );
  181. glEnableVertexAttribArrayARB(location);
  182. //tangent_buffer->bind();
  183. glVertexAttribPointerARB(location, 3, GL_FLOAT, 0, 0, 0);
  184. }
  185. //------------------------------------------------------------------------------
  186. void ShaderManager::disableTangentBuffer(ShaderProgram shader_program,
  187. const std::string &name)
  188. {
  189. // if ( !_usingShaders)
  190. // return;
  191. GLuint location = glGetAttribLocationARB(shader_program, name.c_str() );
  192. glDisableVertexAttribArrayARB(location);
  193. }
  194. #endif
  195. } // namespace BlueCore