Dbg.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. #include "Renderer.h"
  2. #include "Fbo.h"
  3. #include "Scene.h"
  4. #include "Texture.h"
  5. #include "Fbo.h"
  6. #include "Node.h"
  7. #include "SkelNode.h"
  8. #include "btBulletCollisionCommon.h"
  9. #include "btBulletDynamicsCommon.h"
  10. #include "BulletDebuger.h"
  11. extern btDefaultCollisionConfiguration* collisionConfiguration;
  12. extern btCollisionDispatcher* dispatcher;
  13. extern btDbvtBroadphase* broadphase;
  14. extern btSequentialImpulseConstraintSolver* sol;
  15. extern btDiscreteDynamicsWorld* dynamicsWorld;
  16. void renderscene( int pass )
  17. {
  18. btScalar m[16];
  19. btMatrix3x3 rot;
  20. rot.setIdentity();
  21. const int numObjects = dynamicsWorld->getNumCollisionObjects();
  22. btVector3 wireColor( 1, 0, 0 );
  23. for( int i = 0; i < numObjects; i++ )
  24. {
  25. btCollisionObject* colObj = dynamicsWorld->getCollisionObjectArray()[i];
  26. btRigidBody* body = btRigidBody::upcast( colObj );
  27. if( body && body->getMotionState() )
  28. {
  29. btDefaultMotionState* myMotionState = (btDefaultMotionState*)body->getMotionState();
  30. myMotionState->m_graphicsWorldTrans.getOpenGLMatrix( m );
  31. rot = myMotionState->m_graphicsWorldTrans.getBasis();
  32. }
  33. else
  34. {
  35. colObj->getWorldTransform().getOpenGLMatrix( m );
  36. rot = colObj->getWorldTransform().getBasis();
  37. }
  38. glPushMatrix();
  39. glMultMatrixf(m);
  40. R::Dbg::renderCube( true, 2.0 );
  41. glPopMatrix();
  42. }
  43. }
  44. namespace R {
  45. namespace Dbg {
  46. static void renderSun();
  47. //=====================================================================================================================================
  48. // DATA VARS =
  49. //=====================================================================================================================================
  50. bool showAxis = true;
  51. bool showFnormals = false;
  52. bool showVnormals = false;
  53. bool showLights = true;
  54. bool showSkeletons = false;
  55. bool showCameras = true;
  56. bool showBvolumes = true;
  57. static Fbo fbo;
  58. static ShaderProg* shdr;
  59. /*
  60. =======================================================================================================================================
  61. init =
  62. =======================================================================================================================================
  63. */
  64. void init()
  65. {
  66. // create FBO
  67. fbo.Create();
  68. fbo.bind();
  69. // inform in what buffers we draw
  70. fbo.setNumOfColorAttachements(1);
  71. // attach the textures
  72. glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, R::Pps::fai.getGlId(), 0 );
  73. glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, R::Ms::depthFai.getGlId(), 0 );
  74. // test if success
  75. if( !fbo.isGood() )
  76. FATAL( "Cannot create debug FBO" );
  77. // unbind
  78. fbo.Unbind();
  79. // shader
  80. shdr = rsrc::shaders.load( "shaders/dbg.glsl" );
  81. }
  82. /*
  83. =======================================================================================================================================
  84. runStage =
  85. =======================================================================================================================================
  86. */
  87. void runStage( const Camera& cam )
  88. {
  89. fbo.bind();
  90. shdr->bind();
  91. // OGL stuff
  92. R::setProjectionViewMatrices( cam );
  93. R::setViewport( 0, 0, R::w, R::h );
  94. glEnable( GL_DEPTH_TEST );
  95. glDisable( GL_BLEND );
  96. //R::renderGrid();
  97. for( uint i=0; i<Scene::nodes.size(); i++ )
  98. {
  99. if
  100. (
  101. (Scene::nodes[i]->type == Node::NT_LIGHT && showLights) ||
  102. (Scene::nodes[i]->type == Node::NT_CAMERA && showCameras)
  103. )
  104. {
  105. Scene::nodes[i]->render();
  106. }
  107. else if( Scene::nodes[i]->type == Node::NT_SKELETON && showSkeletons )
  108. {
  109. SkelNode* skel_node = static_cast<SkelNode*>( Scene::nodes[i] );
  110. glDisable( GL_DEPTH_TEST );
  111. skel_node->render();
  112. glEnable( GL_DEPTH_TEST );
  113. }
  114. }
  115. // the sun
  116. //RenderSun();
  117. renderscene(1);
  118. // unbind
  119. fbo.Unbind();
  120. }
  121. /*
  122. =======================================================================================================================================
  123. renderGrid =
  124. =======================================================================================================================================
  125. */
  126. void renderGrid()
  127. {
  128. float col0[] = { 0.5f, 0.5f, 0.5f };
  129. float col1[] = { 0.0f, 0.0f, 1.0f };
  130. float col2[] = { 1.0f, 0.0f, 0.0f };
  131. glDisable( GL_TEXTURE_2D );
  132. glDisable( GL_LIGHTING );
  133. glDisable( GL_LINE_STIPPLE );
  134. //glLineWidth(1.0);
  135. glColor3fv( col0 );
  136. const float space = 1.0; // space between lines
  137. const int num = 57; // lines number. must be odd
  138. float opt = ((num-1)*space/2);
  139. glBegin( GL_LINES );
  140. for( int x=0; x<num; x++ )
  141. {
  142. if( x==num/2 ) // if the middle line then change color
  143. glColor3fv( col1 );
  144. else if( x==(num/2)+1 ) // if the next line after the middle one change back to default col
  145. glColor3fv( col0 );
  146. float opt1 = (x*space);
  147. // line in z
  148. glVertex3f( opt1-opt, 0.0, -opt );
  149. glVertex3f( opt1-opt, 0.0, opt );
  150. if( x==num/2 ) // if middle line change col so you can highlight the x-axis
  151. glColor3fv( col2 );
  152. // line in the x
  153. glVertex3f( -opt, 0.0, opt1-opt );
  154. glVertex3f( opt, 0.0, opt1-opt );
  155. }
  156. glEnd();
  157. }
  158. /*
  159. =======================================================================================================================================
  160. renderQuad =
  161. =======================================================================================================================================
  162. */
  163. void renderQuad( float w, float h )
  164. {
  165. float wdiv2 = w/2, hdiv2 = h/2;
  166. float points [][2] = { {wdiv2,hdiv2}, {-wdiv2,hdiv2}, {-wdiv2,-hdiv2}, {wdiv2,-hdiv2} };
  167. float uvs [][2] = { {1.0,1.0}, {0.0,1.0}, {0.0,0.0}, {1.0,0.0} };
  168. glBegin( GL_QUADS );
  169. glNormal3fv( &(-Vec3( 0.0, 0.0, 1.0 ))[0] );
  170. glTexCoord2fv( uvs[0] );
  171. glVertex2fv( points[0] );
  172. glTexCoord2fv( uvs[1] );
  173. glVertex2fv( points[1] );
  174. glTexCoord2fv( uvs[2] );
  175. glVertex2fv( points[2] );
  176. glTexCoord2fv( uvs[3] );
  177. glVertex2fv( points[3] );
  178. glEnd();
  179. }
  180. /*
  181. =======================================================================================================================================
  182. renderSphere =
  183. =======================================================================================================================================
  184. */
  185. void renderSphere( float r, int p )
  186. {
  187. const float twopi = PI*2;
  188. const float pidiv2 = PI/2;
  189. float theta1 = 0.0;
  190. float theta2 = 0.0;
  191. float theta3 = 0.0;
  192. float ex = 0.0f;
  193. float ey = 0.0f;
  194. float ez = 0.0f;
  195. float px = 0.0f;
  196. float py = 0.0f;
  197. float pz = 0.0f;
  198. for( int i = 0; i < p/2; ++i )
  199. {
  200. theta1 = i * twopi / p - pidiv2;
  201. theta2 = (i + 1) * twopi / p - pidiv2;
  202. glBegin( GL_QUAD_STRIP );
  203. {
  204. for( int j = p; j >= 0; --j )
  205. {
  206. theta3 = j * twopi / p;
  207. float sintheta1, costheta1;
  208. sinCos( theta1, sintheta1, costheta1 );
  209. float sintheta2, costheta2;
  210. sinCos( theta2, sintheta2, costheta2 );
  211. float sintheta3, costheta3;
  212. sinCos( theta3, sintheta3, costheta3 );
  213. ex = costheta2 * costheta3;
  214. ey = sintheta2;
  215. ez = costheta2 * sintheta3;
  216. px = r * ex;
  217. py = r * ey;
  218. pz = r * ez;
  219. glNormal3f( ex, ey, ez );
  220. glTexCoord2f( -(j/(float)p) , 2*(i+1)/(float)p );
  221. glVertex3f( px, py, pz );
  222. ex = costheta1 * costheta3;
  223. ey = sintheta1;
  224. ez = costheta1 * sintheta3;
  225. px = r * ex;
  226. py = r * ey;
  227. pz = r * ez;
  228. glNormal3f( ex, ey, ez );
  229. glTexCoord2f( -(j/(float)p), 2*i/(float)p );
  230. glVertex3f( px, py, pz );
  231. }
  232. }
  233. glEnd();
  234. }
  235. }
  236. /*
  237. =======================================================================================================================================
  238. renderCube =
  239. =======================================================================================================================================
  240. */
  241. void renderCube( bool cols, float size )
  242. {
  243. size *= 0.5f;
  244. glBegin(GL_QUADS);
  245. // Front Face
  246. if(cols) glColor3f( 0.0, 0.0, 1.0 );
  247. glNormal3f( 0.0f, 0.0f, 1.0f);
  248. glTexCoord2f(0.0, 0.0); glVertex3f(-size, -size, size);
  249. glTexCoord2f(1.0, 0.0); glVertex3f( size, -size, size);
  250. glTexCoord2f(1.0, 1.0); glVertex3f( size, size, size);
  251. glTexCoord2f(0.0, 1.0); glVertex3f(-size, size, size);
  252. // Back Face
  253. if(cols) glColor3f( 0.0, 0.0, size );
  254. glNormal3f( 0.0f, 0.0f,-1.0f);
  255. glTexCoord2f(1.0, 0.0); glVertex3f(-size, -size, -size);
  256. glTexCoord2f(1.0, 1.0); glVertex3f(-size, size, -size);
  257. glTexCoord2f(0.0, 1.0); glVertex3f( size, size, -size);
  258. glTexCoord2f(0.0, 0.0); glVertex3f( size, -size, -size);
  259. // Top Face
  260. if(cols) glColor3f( 0.0, 1.0, 0.0 );
  261. glNormal3f( 0.0f, 1.0f, 0.0f);
  262. glTexCoord2f(0.0, 1.0); glVertex3f(-size, size, -size);
  263. glTexCoord2f(0.0, 0.0); glVertex3f(-size, size, size);
  264. glTexCoord2f(1.0, 0.0); glVertex3f( size, size, size);
  265. glTexCoord2f(1.0, 1.0); glVertex3f( size, size, -size);
  266. // Bottom Face
  267. if(cols) glColor3f( 0.0, size, 0.0 );
  268. glNormal3f( 0.0f,-1.0f, 0.0f);
  269. glTexCoord2f(1.0, 1.0); glVertex3f(-size, -size, -size);
  270. glTexCoord2f(0.0, 1.0); glVertex3f( size, -size, -size);
  271. glTexCoord2f(0.0, 0.0); glVertex3f( size, -size, size);
  272. glTexCoord2f(1.0, 0.0); glVertex3f(-size, -size, size);
  273. // Right face
  274. if(cols) glColor3f( 1.0, 0.0, 0.0 );
  275. glNormal3f( 1.0f, 0.0f, 0.0f);
  276. glTexCoord2f(1.0, 0.0); glVertex3f( size, -size, -size);
  277. glTexCoord2f(1.0, 1.0); glVertex3f( size, size, -size);
  278. glTexCoord2f(0.0, 1.0); glVertex3f( size, size, size);
  279. glTexCoord2f(0.0, 0.0); glVertex3f( size, -size, size);
  280. // Left Face
  281. if(cols) glColor3f( size, 0.0, 0.0 );
  282. glNormal3f(-1.0f, 0.0f, 0.0f);
  283. glTexCoord2f(0.0, 0.0); glVertex3f(-size, -size, -size);
  284. glTexCoord2f(1.0, 0.0); glVertex3f(-size, -size, size);
  285. glTexCoord2f(1.0, 1.0); glVertex3f(-size, size, size);
  286. glTexCoord2f(0.0, 1.0); glVertex3f(-size, size, -size);
  287. glEnd();
  288. }
  289. //=====================================================================================================================================
  290. // RenderSun =
  291. //=====================================================================================================================================
  292. static void renderSun()
  293. {
  294. glPushMatrix();
  295. R::multMatrix( Mat4( Scene::getSunPos(), Mat3::getIdentity(), 50.0 ) );
  296. R::color3( Vec3(1.0, 1.0, 0.0) );
  297. R::Dbg::renderSphere( 1.0/8.0, 8 );
  298. glPopMatrix();
  299. /* /////////////////////////////////////////////////////
  300. glMatrixMode( GL_PROJECTION );
  301. glPushMatrix();
  302. glLoadIdentity();
  303. glOrtho( 0, 1, 0, 1, -1, 1 );
  304. glMatrixMode( GL_MODELVIEW );
  305. glPushMatrix();
  306. glLoadIdentity();
  307. Vec4 p = Vec4( Scene::getSunPos(), 1.0 );
  308. p = mainCam->getProjectionMatrix() * (mainCam->getViewMatrix() * p);
  309. p /= p.w;
  310. p = p/2 + 0.5;
  311. glPointSize( 10 );
  312. glBegin( GL_POINTS );
  313. R::color3( Vec3(0.0,1.0,0.0) );
  314. glVertex3fv( &p[0] );
  315. glEnd();
  316. glPopMatrix();
  317. glMatrixMode( GL_PROJECTION );
  318. glPopMatrix();*/
  319. }
  320. } } // end namespaces