| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396 |
- #include "Renderer.h"
- #include "Fbo.h"
- #include "Scene.h"
- #include "Texture.h"
- #include "Fbo.h"
- #include "Node.h"
- #include "SkelNode.h"
- #include "btBulletCollisionCommon.h"
- #include "btBulletDynamicsCommon.h"
- #include "BulletDebuger.h"
- extern btDefaultCollisionConfiguration* collisionConfiguration;
- extern btCollisionDispatcher* dispatcher;
- extern btDbvtBroadphase* broadphase;
- extern btSequentialImpulseConstraintSolver* sol;
- extern btDiscreteDynamicsWorld* dynamicsWorld;
- void renderscene( int pass )
- {
- btScalar m[16];
- btMatrix3x3 rot;
- rot.setIdentity();
- const int numObjects = dynamicsWorld->getNumCollisionObjects();
- btVector3 wireColor( 1, 0, 0 );
- for( int i = 0; i < numObjects; i++ )
- {
- btCollisionObject* colObj = dynamicsWorld->getCollisionObjectArray()[i];
- btRigidBody* body = btRigidBody::upcast( colObj );
- if( body && body->getMotionState() )
- {
- btDefaultMotionState* myMotionState = (btDefaultMotionState*)body->getMotionState();
- myMotionState->m_graphicsWorldTrans.getOpenGLMatrix( m );
- rot = myMotionState->m_graphicsWorldTrans.getBasis();
- }
- else
- {
- colObj->getWorldTransform().getOpenGLMatrix( m );
- rot = colObj->getWorldTransform().getBasis();
- }
- glPushMatrix();
- glMultMatrixf(m);
- R::Dbg::renderCube( true, 2.0 );
- glPopMatrix();
- }
- }
- namespace R {
- namespace Dbg {
- static void renderSun();
- //=====================================================================================================================================
- // DATA VARS =
- //=====================================================================================================================================
- bool showAxis = true;
- bool showFnormals = false;
- bool showVnormals = false;
- bool showLights = true;
- bool showSkeletons = false;
- bool showCameras = true;
- bool showBvolumes = true;
- static Fbo fbo;
- static ShaderProg* shdr;
- /*
- =======================================================================================================================================
- init =
- =======================================================================================================================================
- */
- void init()
- {
- // create FBO
- fbo.Create();
- fbo.bind();
- // inform in what buffers we draw
- fbo.setNumOfColorAttachements(1);
- // attach the textures
- glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, R::Pps::fai.getGlId(), 0 );
- glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, R::Ms::depthFai.getGlId(), 0 );
- // test if success
- if( !fbo.isGood() )
- FATAL( "Cannot create debug FBO" );
- // unbind
- fbo.Unbind();
- // shader
- shdr = rsrc::shaders.load( "shaders/dbg.glsl" );
- }
- /*
- =======================================================================================================================================
- runStage =
- =======================================================================================================================================
- */
- void runStage( const Camera& cam )
- {
- fbo.bind();
- shdr->bind();
- // OGL stuff
- R::setProjectionViewMatrices( cam );
- R::setViewport( 0, 0, R::w, R::h );
- glEnable( GL_DEPTH_TEST );
- glDisable( GL_BLEND );
- //R::renderGrid();
- for( uint i=0; i<Scene::nodes.size(); i++ )
- {
- if
- (
- (Scene::nodes[i]->type == Node::NT_LIGHT && showLights) ||
- (Scene::nodes[i]->type == Node::NT_CAMERA && showCameras)
- )
- {
- Scene::nodes[i]->render();
- }
- else if( Scene::nodes[i]->type == Node::NT_SKELETON && showSkeletons )
- {
- SkelNode* skel_node = static_cast<SkelNode*>( Scene::nodes[i] );
- glDisable( GL_DEPTH_TEST );
- skel_node->render();
- glEnable( GL_DEPTH_TEST );
- }
- }
- // the sun
- //RenderSun();
- renderscene(1);
- // unbind
- fbo.Unbind();
- }
- /*
- =======================================================================================================================================
- renderGrid =
- =======================================================================================================================================
- */
- void renderGrid()
- {
- float col0[] = { 0.5f, 0.5f, 0.5f };
- float col1[] = { 0.0f, 0.0f, 1.0f };
- float col2[] = { 1.0f, 0.0f, 0.0f };
- glDisable( GL_TEXTURE_2D );
- glDisable( GL_LIGHTING );
- glDisable( GL_LINE_STIPPLE );
- //glLineWidth(1.0);
- glColor3fv( col0 );
- const float space = 1.0; // space between lines
- const int num = 57; // lines number. must be odd
- float opt = ((num-1)*space/2);
- glBegin( GL_LINES );
- for( int x=0; x<num; x++ )
- {
- if( x==num/2 ) // if the middle line then change color
- glColor3fv( col1 );
- else if( x==(num/2)+1 ) // if the next line after the middle one change back to default col
- glColor3fv( col0 );
- float opt1 = (x*space);
- // line in z
- glVertex3f( opt1-opt, 0.0, -opt );
- glVertex3f( opt1-opt, 0.0, opt );
- if( x==num/2 ) // if middle line change col so you can highlight the x-axis
- glColor3fv( col2 );
- // line in the x
- glVertex3f( -opt, 0.0, opt1-opt );
- glVertex3f( opt, 0.0, opt1-opt );
- }
- glEnd();
- }
- /*
- =======================================================================================================================================
- renderQuad =
- =======================================================================================================================================
- */
- void renderQuad( float w, float h )
- {
- float wdiv2 = w/2, hdiv2 = h/2;
- float points [][2] = { {wdiv2,hdiv2}, {-wdiv2,hdiv2}, {-wdiv2,-hdiv2}, {wdiv2,-hdiv2} };
- float uvs [][2] = { {1.0,1.0}, {0.0,1.0}, {0.0,0.0}, {1.0,0.0} };
- glBegin( GL_QUADS );
- glNormal3fv( &(-Vec3( 0.0, 0.0, 1.0 ))[0] );
- glTexCoord2fv( uvs[0] );
- glVertex2fv( points[0] );
- glTexCoord2fv( uvs[1] );
- glVertex2fv( points[1] );
- glTexCoord2fv( uvs[2] );
- glVertex2fv( points[2] );
- glTexCoord2fv( uvs[3] );
- glVertex2fv( points[3] );
- glEnd();
- }
- /*
- =======================================================================================================================================
- renderSphere =
- =======================================================================================================================================
- */
- void renderSphere( float r, int p )
- {
- const float twopi = PI*2;
- const float pidiv2 = PI/2;
- float theta1 = 0.0;
- float theta2 = 0.0;
- float theta3 = 0.0;
- float ex = 0.0f;
- float ey = 0.0f;
- float ez = 0.0f;
- float px = 0.0f;
- float py = 0.0f;
- float pz = 0.0f;
- for( int i = 0; i < p/2; ++i )
- {
- theta1 = i * twopi / p - pidiv2;
- theta2 = (i + 1) * twopi / p - pidiv2;
- glBegin( GL_QUAD_STRIP );
- {
- for( int j = p; j >= 0; --j )
- {
- theta3 = j * twopi / p;
- float sintheta1, costheta1;
- sinCos( theta1, sintheta1, costheta1 );
- float sintheta2, costheta2;
- sinCos( theta2, sintheta2, costheta2 );
- float sintheta3, costheta3;
- sinCos( theta3, sintheta3, costheta3 );
- ex = costheta2 * costheta3;
- ey = sintheta2;
- ez = costheta2 * sintheta3;
- px = r * ex;
- py = r * ey;
- pz = r * ez;
- glNormal3f( ex, ey, ez );
- glTexCoord2f( -(j/(float)p) , 2*(i+1)/(float)p );
- glVertex3f( px, py, pz );
- ex = costheta1 * costheta3;
- ey = sintheta1;
- ez = costheta1 * sintheta3;
- px = r * ex;
- py = r * ey;
- pz = r * ez;
- glNormal3f( ex, ey, ez );
- glTexCoord2f( -(j/(float)p), 2*i/(float)p );
- glVertex3f( px, py, pz );
- }
- }
- glEnd();
- }
- }
- /*
- =======================================================================================================================================
- renderCube =
- =======================================================================================================================================
- */
- void renderCube( bool cols, float size )
- {
- size *= 0.5f;
- glBegin(GL_QUADS);
- // Front Face
- if(cols) glColor3f( 0.0, 0.0, 1.0 );
- glNormal3f( 0.0f, 0.0f, 1.0f);
- glTexCoord2f(0.0, 0.0); glVertex3f(-size, -size, size);
- glTexCoord2f(1.0, 0.0); glVertex3f( size, -size, size);
- glTexCoord2f(1.0, 1.0); glVertex3f( size, size, size);
- glTexCoord2f(0.0, 1.0); glVertex3f(-size, size, size);
- // Back Face
- if(cols) glColor3f( 0.0, 0.0, size );
- glNormal3f( 0.0f, 0.0f,-1.0f);
- glTexCoord2f(1.0, 0.0); glVertex3f(-size, -size, -size);
- glTexCoord2f(1.0, 1.0); glVertex3f(-size, size, -size);
- glTexCoord2f(0.0, 1.0); glVertex3f( size, size, -size);
- glTexCoord2f(0.0, 0.0); glVertex3f( size, -size, -size);
- // Top Face
- if(cols) glColor3f( 0.0, 1.0, 0.0 );
- glNormal3f( 0.0f, 1.0f, 0.0f);
- glTexCoord2f(0.0, 1.0); glVertex3f(-size, size, -size);
- glTexCoord2f(0.0, 0.0); glVertex3f(-size, size, size);
- glTexCoord2f(1.0, 0.0); glVertex3f( size, size, size);
- glTexCoord2f(1.0, 1.0); glVertex3f( size, size, -size);
- // Bottom Face
- if(cols) glColor3f( 0.0, size, 0.0 );
- glNormal3f( 0.0f,-1.0f, 0.0f);
- glTexCoord2f(1.0, 1.0); glVertex3f(-size, -size, -size);
- glTexCoord2f(0.0, 1.0); glVertex3f( size, -size, -size);
- glTexCoord2f(0.0, 0.0); glVertex3f( size, -size, size);
- glTexCoord2f(1.0, 0.0); glVertex3f(-size, -size, size);
- // Right face
- if(cols) glColor3f( 1.0, 0.0, 0.0 );
- glNormal3f( 1.0f, 0.0f, 0.0f);
- glTexCoord2f(1.0, 0.0); glVertex3f( size, -size, -size);
- glTexCoord2f(1.0, 1.0); glVertex3f( size, size, -size);
- glTexCoord2f(0.0, 1.0); glVertex3f( size, size, size);
- glTexCoord2f(0.0, 0.0); glVertex3f( size, -size, size);
- // Left Face
- if(cols) glColor3f( size, 0.0, 0.0 );
- glNormal3f(-1.0f, 0.0f, 0.0f);
- glTexCoord2f(0.0, 0.0); glVertex3f(-size, -size, -size);
- glTexCoord2f(1.0, 0.0); glVertex3f(-size, -size, size);
- glTexCoord2f(1.0, 1.0); glVertex3f(-size, size, size);
- glTexCoord2f(0.0, 1.0); glVertex3f(-size, size, -size);
- glEnd();
- }
- //=====================================================================================================================================
- // RenderSun =
- //=====================================================================================================================================
- static void renderSun()
- {
- glPushMatrix();
- R::multMatrix( Mat4( Scene::getSunPos(), Mat3::getIdentity(), 50.0 ) );
- R::color3( Vec3(1.0, 1.0, 0.0) );
- R::Dbg::renderSphere( 1.0/8.0, 8 );
- glPopMatrix();
- /* /////////////////////////////////////////////////////
- glMatrixMode( GL_PROJECTION );
- glPushMatrix();
- glLoadIdentity();
- glOrtho( 0, 1, 0, 1, -1, 1 );
- glMatrixMode( GL_MODELVIEW );
- glPushMatrix();
- glLoadIdentity();
- Vec4 p = Vec4( Scene::getSunPos(), 1.0 );
- p = mainCam->getProjectionMatrix() * (mainCam->getViewMatrix() * p);
- p /= p.w;
- p = p/2 + 0.5;
- glPointSize( 10 );
- glBegin( GL_POINTS );
- R::color3( Vec3(0.0,1.0,0.0) );
- glVertex3fv( &p[0] );
- glEnd();
- glPopMatrix();
- glMatrixMode( GL_PROJECTION );
- glPopMatrix();*/
- }
- } } // end namespaces
|