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.

186 lines
4.3KB

  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)
  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::setupProjectionMatrix()
  95. {
  96. if (_Camera.valid())
  97. {
  98. Scalar fH = tan( (_Camera->getFov() / 2) / 180 * Pi)
  99. * _Camera->getNearPlane();
  100. Scalar fW = fH * _ViewportWidth / _ViewportHeight;
  101. // setup projectiom matrix
  102. glMatrixMode(GL_PROJECTION);
  103. glLoadIdentity();
  104. glFrustum( -fW, fW, -fH, fH, _Camera->getNearPlane(),
  105. _Camera->getFarPlane());
  106. }
  107. }
  108. void RenderDevice::setupViewMatrix()
  109. {
  110. // set the view matrix
  111. glMatrixMode(GL_MODELVIEW);
  112. glLoadIdentity();
  113. Matrix4x4 m(_Camera->getRotation(), _Camera->getRotation().applyInversed(_Camera->getPosition() * -1.0) );
  114. glMultMatrixd( ( GLdouble * ) &m.m );
  115. /*
  116. // calculate frustum planes
  117. Vector3 up = q.apply ( Vector3 ( 0.0, 1.0, 0.0 ) );
  118. Vector3 right = q.apply ( Vector3 ( 1.0, 0.0, 0.0 ) );
  119. Vector3 d = q.apply ( Vector3 ( 0.0, 0.0, -1.0 ) );
  120. Vector3 fc = p + d * _far;
  121. Vector3 ftl = fc + ( up * _hFar ) - ( right * _wFar );
  122. Vector3 ftr = fc + ( up * _hFar ) + ( right * _wFar );
  123. Vector3 fbl = fc - ( up * _hFar ) - ( right * _wFar );
  124. Vector3 fbr = fc - ( up * _hFar ) + ( right * _wFar );
  125. Vector3 nc = p + d * _near;
  126. Vector3 ntl = nc + ( up * _hNear ) - ( right * _wNear );
  127. Vector3 ntr = nc + ( up * _hNear ) + ( right * _wNear );
  128. Vector3 nbl = nc - ( up * _hNear ) - ( right * _wNear );
  129. Vector3 nbr = nc - ( up * _hNear ) + ( right * _wNear );
  130. _frustumPlanes[RightPlane] = Plane ( nbr, fbr, ntr );
  131. _frustumPlanes[LeftPlane] = Plane ( ntl, fbl, nbl );
  132. _frustumPlanes[BottomPlane] = Plane ( nbl, fbr, nbr );
  133. _frustumPlanes[TopPlane] = Plane ( ntr, ftl, ntl );
  134. _frustumPlanes[FarPlane] = Plane ( ftl, ftr, fbl );
  135. _frustumPlanes[NearPlane] = Plane ( ntl, nbl, ntr );
  136. */
  137. }
  138. void RenderDevice::setTransformation (const Vector3& position, const Quaternion& orientation)
  139. {
  140. glMatrixMode ( GL_MODELVIEW );
  141. glPushMatrix();
  142. Matrix4x4 m ( orientation, position );
  143. glMultMatrixd ( ( GLdouble * ) &m.m );
  144. }
  145. #if 0
  146. void RenderDevice::setu
  147. // set the view matrix
  148. glMatrixMode (GL_MODELVIEW );
  149. glLoadIdentity();
  150. Matrix4x4 m(_Rotation, _Rotation.applyInversed(_Position * -1.0) );
  151. glMultMatrixd ( ( GLdouble * ) &m.m );
  152. } // namespace BlueCore
  153. #endif
  154. }