Dbg.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /**
  2. * @file
  3. *
  4. * Debugging stage
  5. */
  6. #include "Renderer.h"
  7. #include "App.h"
  8. #include "Scene.h"
  9. #include "SkelNode.h"
  10. #include "Camera.h"
  11. //======================================================================================================================
  12. // Statics =
  13. //======================================================================================================================
  14. ShaderProg* Renderer::Dbg::sProg = NULL;
  15. Mat4 Renderer::Dbg::viewProjectionMat;
  16. //======================================================================================================================
  17. // Constructor =
  18. //======================================================================================================================
  19. Renderer::Dbg::Dbg(Renderer& r_):
  20. RenderingStage(r_),
  21. showAxisEnabled(false),
  22. showLightsEnabled(true),
  23. showSkeletonsEnabled(false),
  24. showCamerasEnabled(true)
  25. {
  26. }
  27. //======================================================================================================================
  28. // renderGrid =
  29. //======================================================================================================================
  30. void Renderer::Dbg::renderGrid()
  31. {
  32. float col0[] = { 0.5, 0.5, 0.5 };
  33. float col1[] = { 0.0, 0.0, 1.0 };
  34. float col2[] = { 1.0, 0.0, 0.0 };
  35. glDisable(GL_TEXTURE_2D);
  36. glDisable(GL_LIGHTING);
  37. glDisable(GL_LINE_STIPPLE);
  38. //glLineWidth(1.0);
  39. glColor3fv(col0);
  40. const float space = 1.0; // space between lines
  41. const int num = 57; // lines number. must be odd
  42. float opt = ((num-1)*space/2);
  43. glBegin(GL_LINES);
  44. for(int x=0; x<num; x++)
  45. {
  46. if(x==num/2) // if the middle line then change color
  47. glColor3fv(col1);
  48. else if(x==(num/2)+1) // if the next line after the middle one change back to default col
  49. glColor3fv(col0);
  50. float opt1 = (x*space);
  51. // line in z
  52. glVertex3f(opt1-opt, 0.0, -opt);
  53. glVertex3f(opt1-opt, 0.0, opt);
  54. if(x==num/2) // if middle line change col so you can highlight the x-axis
  55. glColor3fv(col2);
  56. // line in the x
  57. glVertex3f(-opt, 0.0, opt1-opt);
  58. glVertex3f(opt, 0.0, opt1-opt);
  59. }
  60. glEnd();
  61. }
  62. //======================================================================================================================
  63. // renderSphere =
  64. //======================================================================================================================
  65. void Renderer::Dbg::renderSphere(int complexity, float radius)
  66. {
  67. const float twopi = M::PI*2;
  68. const float pidiv2 = M::PI/2;
  69. float theta1 = 0.0;
  70. float theta2 = 0.0;
  71. float theta3 = 0.0;
  72. float ex = 0.0;
  73. float ey = 0.0;
  74. float ez = 0.0;
  75. float px = 0.0;
  76. float py = 0.0;
  77. float pz = 0.0;
  78. Vec<Vec3> positions;
  79. Vec<Vec3> normals;
  80. Vec<Vec2> texCoodrs;
  81. for(int i = 0; i < complexity/2; ++i)
  82. {
  83. theta1 = i * twopi / complexity - pidiv2;
  84. theta2 = (i + 1) * twopi / complexity - pidiv2;
  85. for(int j = complexity; j >= 0; --j)
  86. {
  87. theta3 = j * twopi / complexity;
  88. float sintheta1, costheta1;
  89. sinCos(theta1, sintheta1, costheta1);
  90. float sintheta2, costheta2;
  91. sinCos(theta2, sintheta2, costheta2);
  92. float sintheta3, costheta3;
  93. sinCos(theta3, sintheta3, costheta3);
  94. ex = costheta2 * costheta3;
  95. ey = sintheta2;
  96. ez = costheta2 * sintheta3;
  97. px = radius * ex;
  98. py = radius * ey;
  99. pz = radius * ez;
  100. positions.push_back(Vec3(px, py, pz));
  101. normals.push_back(Vec3(ex, ey, ez));
  102. texCoodrs.push_back(Vec2(-(j/(float)complexity), 2*(i+1)/(float)complexity));
  103. ex = costheta1 * costheta3;
  104. ey = sintheta1;
  105. ez = costheta1 * sintheta3;
  106. px = radius * ex;
  107. py = radius * ey;
  108. pz = radius * ez;
  109. positions.push_back(Vec3(px, py, pz));
  110. normals.push_back(Vec3(ex, ey, ez));
  111. texCoodrs.push_back(Vec2(-(j/(float)complexity), 2*i/(float)complexity));
  112. }
  113. }
  114. glEnableVertexAttribArray(0);
  115. glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, &(positions[0][0]));
  116. glDrawArrays(GL_QUAD_STRIP, 0, positions.size());
  117. glDisableVertexAttribArray(0);
  118. }
  119. //======================================================================================================================
  120. // renderCube =
  121. //======================================================================================================================
  122. void Renderer::Dbg::renderCube(float size)
  123. {
  124. Vec3 maxPos = Vec3(0.5 * size);
  125. Vec3 minPos = Vec3(-0.5 * size);
  126. Vec3 points [] = {
  127. Vec3(maxPos.x, maxPos.y, maxPos.z), // right top front
  128. Vec3(minPos.x, maxPos.y, maxPos.z), // left top front
  129. Vec3(minPos.x, minPos.y, maxPos.z), // left bottom front
  130. Vec3(maxPos.x, minPos.y, maxPos.z), // right bottom front
  131. Vec3(maxPos.x, maxPos.y, minPos.z), // right top back
  132. Vec3(minPos.x, maxPos.y, minPos.z), // left top back
  133. Vec3(minPos.x, minPos.y, minPos.z), // left bottom back
  134. Vec3(maxPos.x, minPos.y, minPos.z) // right bottom back
  135. };
  136. const ushort indeces [] = { 0, 1, 2, 3, 4, 0, 3, 7, 1, 5, 6, 2, 5, 4, 7, 6, 0, 4, 5, 1, 3, 2, 6, 7 };
  137. glEnableVertexAttribArray(0);
  138. glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, &(points[0][0]));
  139. glDrawElements(GL_QUADS, sizeof(indeces)/sizeof(ushort), GL_UNSIGNED_SHORT, indeces);
  140. glDisableVertexAttribArray(0);
  141. }
  142. //======================================================================================================================
  143. // init =
  144. //======================================================================================================================
  145. void Renderer::Dbg::init()
  146. {
  147. // create FBO Captain Blood
  148. fbo.create();
  149. fbo.bind();
  150. // inform in what buffers we draw
  151. fbo.setNumOfColorAttachements(1);
  152. // attach the textures
  153. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, r.pps.fai.getGlId(), 0);
  154. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, r.ms.depthFai.getGlId(), 0);
  155. // test if success
  156. if(!fbo.isGood())
  157. FATAL("Cannot create debug FBO");
  158. // unbind
  159. fbo.unbind();
  160. // shader
  161. if(sProg == NULL)
  162. {
  163. sProg = new ShaderProg;
  164. sProg->customLoad("shaders/Dbg.glsl");
  165. }
  166. }
  167. //======================================================================================================================
  168. // runStage =
  169. //======================================================================================================================
  170. void Renderer::Dbg::run()
  171. {
  172. if(!enabled) return;
  173. const Camera& cam = *r.cam;
  174. fbo.bind();
  175. sProg->bind();
  176. viewProjectionMat = cam.getProjectionMatrix() * cam.getViewMatrix();
  177. // OGL stuff
  178. r.setProjectionViewMatrices(cam);
  179. Renderer::setViewport(0, 0, r.width, r.height);
  180. glEnable(GL_DEPTH_TEST);
  181. glDisable(GL_BLEND);
  182. //R::renderGrid();
  183. for(uint i=0; i<app->getScene()->nodes.size(); i++)
  184. {
  185. sProg->bind();
  186. SceneNode* node = app->getScene()->nodes[i];
  187. if
  188. (
  189. (node->type == SceneNode::NT_LIGHT && showLightsEnabled) ||
  190. (node->type == SceneNode::NT_CAMERA && showCamerasEnabled) ||
  191. node->type == SceneNode::NT_PARTICLE_EMITTER
  192. )
  193. {
  194. node->render();
  195. }
  196. else if(app->getScene()->nodes[i]->type == SceneNode::NT_SKELETON && showSkeletonsEnabled)
  197. {
  198. SkelNode* skelNode = static_cast<SkelNode*>(node);
  199. glDisable(GL_DEPTH_TEST);
  200. skelNode->render();
  201. glEnable(GL_DEPTH_TEST);
  202. }
  203. }
  204. }
  205. //======================================================================================================================
  206. // setColor =
  207. //======================================================================================================================
  208. void Renderer::Dbg::setColor(const Vec4& color)
  209. {
  210. sProg->findUniVar("color")->setVec4(&color);
  211. }
  212. //======================================================================================================================
  213. // setModelMat =
  214. //======================================================================================================================
  215. void Renderer::Dbg::setModelMat(const Mat4& modelMat)
  216. {
  217. Mat4 pmv = viewProjectionMat * modelMat;
  218. sProg->findUniVar("modelViewProjectionMat")->setMat4(&pmv);
  219. }