r_dbg.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #include "renderer.h"
  2. #include "r_private.h"
  3. #include "fbo.h"
  4. #include "scene.h"
  5. #include "texture.h"
  6. #include "fbo.h"
  7. namespace r {
  8. namespace dbg {
  9. /*
  10. =======================================================================================================================================
  11. DATA VARS =
  12. =======================================================================================================================================
  13. */
  14. bool show_axis = true;
  15. bool show_fnormals = false;
  16. bool show_vnormals = false;
  17. bool show_lights = true;
  18. bool show_skeletons = false;
  19. bool show_cameras = true;
  20. bool show_bvolumes = true;
  21. static fbo_t fbo;
  22. static shader_prog_t* shdr;
  23. /*
  24. =======================================================================================================================================
  25. Init =
  26. =======================================================================================================================================
  27. */
  28. void Init()
  29. {
  30. // create FBO
  31. fbo.Create();
  32. fbo.Bind();
  33. // inform in what buffers we draw
  34. fbo.SetNumOfColorAttachements(1);
  35. // attach the textures
  36. glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, r::pps::fai.GetGLID(), 0 );
  37. glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, r::ms::depth_fai.GetGLID(), 0 );
  38. // test if success
  39. if( !fbo.CheckStatus() )
  40. FATAL( "Cannot create debug FBO" );
  41. // unbind
  42. fbo.Unbind();
  43. // shader
  44. shdr = rsrc::shaders.Load( "shaders/dbg.glsl" );
  45. }
  46. /*
  47. =======================================================================================================================================
  48. RunStage =
  49. =======================================================================================================================================
  50. */
  51. void RunStage( const camera_t& cam )
  52. {
  53. fbo.Bind();
  54. shdr->Bind();
  55. // OGL stuff
  56. SetProjectionViewMatrices( cam );
  57. SetViewport( 0, 0, r::w*r::rendering_quality, r::h*r::rendering_quality );
  58. glEnable( GL_DEPTH_TEST );
  59. glDisable( GL_BLEND );
  60. //r::RenderGrid();
  61. for( uint i=0; i<scene::objects.size(); i++ )
  62. {
  63. if
  64. (
  65. (scene::objects[i]->type == object_t::LIGHT && show_lights) ||
  66. (scene::objects[i]->type == object_t::CAMERA && show_cameras)
  67. )
  68. {
  69. scene::objects[i]->Render();
  70. }
  71. else if( scene::objects[i]->type == object_t::MESH && show_vnormals )
  72. {
  73. mesh_t* mesh = static_cast<mesh_t*>( scene::objects[i] );
  74. mesh->RenderNormals();
  75. mesh->RenderTangents();
  76. }
  77. else if( scene::objects[i]->type == object_t::MODEL && show_skeletons )
  78. {
  79. smodel_t* model = static_cast<smodel_t*>( scene::objects[i] );
  80. glDisable( GL_DEPTH_TEST );
  81. model->RenderSkeleton();
  82. glEnable( GL_DEPTH_TEST );
  83. }
  84. }
  85. // unbind
  86. fbo.Unbind();
  87. }
  88. /*
  89. =======================================================================================================================================
  90. RenderGrid =
  91. =======================================================================================================================================
  92. */
  93. void RenderGrid()
  94. {
  95. float col0[] = { 0.5f, 0.5f, 0.5f };
  96. float col1[] = { 0.0f, 0.0f, 1.0f };
  97. float col2[] = { 1.0f, 0.0f, 0.0f };
  98. glDisable( GL_TEXTURE_2D );
  99. glDisable( GL_LIGHTING );
  100. glDisable( GL_LINE_STIPPLE );
  101. //glLineWidth(1.0);
  102. glColor3fv( col0 );
  103. const float space = 1.0; // space between lines
  104. const int num = 57; // lines number. must be odd
  105. float opt = ((num-1)*space/2);
  106. glBegin( GL_LINES );
  107. for( int x=0; x<num; x++ )
  108. {
  109. if( x==num/2 ) // if the middle line then change color
  110. glColor3fv( col1 );
  111. else if( x==(num/2)+1 ) // if the next line after the middle one change back to default col
  112. glColor3fv( col0 );
  113. float opt1 = (x*space);
  114. // line in z
  115. glVertex3f( opt1-opt, 0.0, -opt );
  116. glVertex3f( opt1-opt, 0.0, opt );
  117. if( x==num/2 ) // if middle line change col so you can highlight the x-axis
  118. glColor3fv( col2 );
  119. // line in the x
  120. glVertex3f( -opt, 0.0, opt1-opt );
  121. glVertex3f( opt, 0.0, opt1-opt );
  122. }
  123. glEnd();
  124. }
  125. /*
  126. =======================================================================================================================================
  127. RenderQuad =
  128. =======================================================================================================================================
  129. */
  130. void RenderQuad( float w, float h )
  131. {
  132. float wdiv2 = w/2, hdiv2 = h/2;
  133. float points [][2] = { {wdiv2,hdiv2}, {-wdiv2,hdiv2}, {-wdiv2,-hdiv2}, {wdiv2,-hdiv2} };
  134. float uvs [][2] = { {1.0,1.0}, {0.0,1.0}, {0.0,0.0}, {1.0,0.0} };
  135. glBegin( GL_QUADS );
  136. glNormal3fv( &(-vec3_t( 0.0, 0.0, 1.0 ))[0] );
  137. glTexCoord2fv( uvs[0] );
  138. glVertex2fv( points[0] );
  139. glTexCoord2fv( uvs[1] );
  140. glVertex2fv( points[1] );
  141. glTexCoord2fv( uvs[2] );
  142. glVertex2fv( points[2] );
  143. glTexCoord2fv( uvs[3] );
  144. glVertex2fv( points[3] );
  145. glEnd();
  146. }
  147. /*
  148. =======================================================================================================================================
  149. RenderSphere =
  150. =======================================================================================================================================
  151. */
  152. void RenderSphere( float r, int p )
  153. {
  154. const float twopi = PI*2;
  155. const float pidiv2 = PI/2;
  156. float theta1 = 0.0;
  157. float theta2 = 0.0;
  158. float theta3 = 0.0;
  159. float ex = 0.0f;
  160. float ey = 0.0f;
  161. float ez = 0.0f;
  162. float px = 0.0f;
  163. float py = 0.0f;
  164. float pz = 0.0f;
  165. for( int i = 0; i < p/2; ++i )
  166. {
  167. theta1 = i * twopi / p - pidiv2;
  168. theta2 = (i + 1) * twopi / p - pidiv2;
  169. glBegin( GL_QUAD_STRIP );
  170. {
  171. for( int j = p; j >= 0; --j )
  172. {
  173. theta3 = j * twopi / p;
  174. float sintheta1, costheta1;
  175. SinCos( theta1, sintheta1, costheta1 );
  176. float sintheta2, costheta2;
  177. SinCos( theta2, sintheta2, costheta2 );
  178. float sintheta3, costheta3;
  179. SinCos( theta3, sintheta3, costheta3 );
  180. ex = costheta2 * costheta3;
  181. ey = sintheta2;
  182. ez = costheta2 * sintheta3;
  183. px = r * ex;
  184. py = r * ey;
  185. pz = r * ez;
  186. glNormal3f( ex, ey, ez );
  187. glTexCoord2f( -(j/(float)p) , 2*(i+1)/(float)p );
  188. glVertex3f( px, py, pz );
  189. ex = costheta1 * costheta3;
  190. ey = sintheta1;
  191. ez = costheta1 * sintheta3;
  192. px = r * ex;
  193. py = r * ey;
  194. pz = r * ez;
  195. glNormal3f( ex, ey, ez );
  196. glTexCoord2f( -(j/(float)p), 2*i/(float)p );
  197. glVertex3f( px, py, pz );
  198. }
  199. }
  200. glEnd();
  201. }
  202. }
  203. /*
  204. =======================================================================================================================================
  205. RenderCube =
  206. =======================================================================================================================================
  207. */
  208. void RenderCube( bool cols, float size )
  209. {
  210. size *= 0.5f;
  211. glBegin(GL_QUADS);
  212. // Front Face
  213. if(cols) glColor3f( 0.0, 0.0, 1.0 );
  214. glNormal3f( 0.0f, 0.0f, 1.0f);
  215. glTexCoord2f(0.0, 0.0); glVertex3f(-size, -size, size);
  216. glTexCoord2f(1.0, 0.0); glVertex3f( size, -size, size);
  217. glTexCoord2f(1.0, 1.0); glVertex3f( size, size, size);
  218. glTexCoord2f(0.0, 1.0); glVertex3f(-size, size, size);
  219. // Back Face
  220. if(cols) glColor3f( 0.0, 0.0, size );
  221. glNormal3f( 0.0f, 0.0f,-1.0f);
  222. glTexCoord2f(1.0, 0.0); glVertex3f(-size, -size, -size);
  223. glTexCoord2f(1.0, 1.0); glVertex3f(-size, size, -size);
  224. glTexCoord2f(0.0, 1.0); glVertex3f( size, size, -size);
  225. glTexCoord2f(0.0, 0.0); glVertex3f( size, -size, -size);
  226. // Top Face
  227. if(cols) glColor3f( 0.0, 1.0, 0.0 );
  228. glNormal3f( 0.0f, 1.0f, 0.0f);
  229. glTexCoord2f(0.0, 1.0); glVertex3f(-size, size, -size);
  230. glTexCoord2f(0.0, 0.0); glVertex3f(-size, size, size);
  231. glTexCoord2f(1.0, 0.0); glVertex3f( size, size, size);
  232. glTexCoord2f(1.0, 1.0); glVertex3f( size, size, -size);
  233. // Bottom Face
  234. if(cols) glColor3f( 0.0, size, 0.0 );
  235. glNormal3f( 0.0f,-1.0f, 0.0f);
  236. glTexCoord2f(1.0, 1.0); glVertex3f(-size, -size, -size);
  237. glTexCoord2f(0.0, 1.0); glVertex3f( size, -size, -size);
  238. glTexCoord2f(0.0, 0.0); glVertex3f( size, -size, size);
  239. glTexCoord2f(1.0, 0.0); glVertex3f(-size, -size, size);
  240. // Right face
  241. if(cols) glColor3f( 1.0, 0.0, 0.0 );
  242. glNormal3f( 1.0f, 0.0f, 0.0f);
  243. glTexCoord2f(1.0, 0.0); glVertex3f( size, -size, -size);
  244. glTexCoord2f(1.0, 1.0); glVertex3f( size, size, -size);
  245. glTexCoord2f(0.0, 1.0); glVertex3f( size, size, size);
  246. glTexCoord2f(0.0, 0.0); glVertex3f( size, -size, size);
  247. // Left Face
  248. if(cols) glColor3f( size, 0.0, 0.0 );
  249. glNormal3f(-1.0f, 0.0f, 0.0f);
  250. glTexCoord2f(0.0, 0.0); glVertex3f(-size, -size, -size);
  251. glTexCoord2f(1.0, 0.0); glVertex3f(-size, -size, size);
  252. glTexCoord2f(1.0, 1.0); glVertex3f(-size, size, size);
  253. glTexCoord2f(0.0, 1.0); glVertex3f(-size, size, -size);
  254. glEnd();
  255. }
  256. } } // end namespaces