20 #include "renderworld.h"
21 #include "physphere.h"
23 #include "phycylinder.h"
27 #include "phycompoundobject.h"
29 #include "graphicalwobject.h"
30 #include "private/renderwobjecthierarchy.h"
41 # include <GLUT/glut.h>
46 using namespace qglviewer;
48 #define GLMultMatrix glMultMatrixf
63 virtual void render( QGLContext* ) {
67 calculateAABB( minpoint, maxpoint, obj->matrix() );
72 minPoint = tm.w_pos - rds;
73 maxPoint = tm.w_pos + rds;
77 dimension[0] = rad*2.0;
78 dimension[1] = rad*2.0;
79 dimension[2] = rad*2.0;
94 virtual void render( QGLContext* gw ) {
97 if ( wmesh->attachedTo() ) {
98 m = m * wmesh->attachedTo()->matrix();
100 GLMultMatrix(&m[0][0]);
101 int m_numMeshes = wmesh->meshesCount();
109 for (
int i = 0; i < m_numMeshes; i++ ) {
110 int materialIndex = m_pMeshes[i].m_materialIndex;
111 if ( materialIndex >= 0 ) {
112 glMaterialfv( GL_FRONT, GL_AMBIENT, m_pMaterials[materialIndex].m_ambient );
113 glMaterialfv( GL_FRONT, GL_DIFFUSE, m_pMaterials[materialIndex].m_diffuse );
114 glMaterialfv( GL_FRONT, GL_SPECULAR, m_pMaterials[materialIndex].m_specular );
115 glMaterialfv( GL_FRONT, GL_EMISSION, m_pMaterials[materialIndex].m_emissive );
116 glMaterialf( GL_FRONT, GL_SHININESS, m_pMaterials[materialIndex].m_shininess );
120 glBegin( GL_TRIANGLES );
121 for (
int j = 0; j < m_pMeshes[i].m_numTriangles; j++ ) {
122 int triangleIndex = m_pMeshes[i].m_pTriangleIndices[j];
124 for (
int k = 0; k < 3; k++ ) {
125 int index = pTri->m_vertexIndices[k];
126 glNormal3fv( pTri->m_vertexNormals[k] );
127 glTexCoord2f( pTri->m_s[k], pTri->m_t[k] );
128 glVertex3fv( m_pVertices[index].m_location );
154 virtual void render( QGLContext* gw ) {
157 const wMatrix& m = box->matrix();
158 GLMultMatrix(&m[0][0]);
164 float hdx = (dx/2.0);
165 float hdy = (dy/2.0);
166 float hdz = (dz/2.0);
168 glNormal3f(0.0, 0.0, 1.0);
169 glTexCoord2f(0.0, 0.0); glVertex3f(-hdx, -hdy, hdz);
170 glTexCoord2f(1.0, 0.0); glVertex3f( hdx, -hdy, hdz);
171 glTexCoord2f(1.0, 1.0); glVertex3f( hdx, hdy, hdz);
172 glTexCoord2f(0.0, 1.0); glVertex3f(-hdx, hdy, hdz);
175 glNormal3f(0.0, 0.0, -1.0);
176 glTexCoord2f(0.0, 0.0); glVertex3f( hdx, -hdy, -hdz);
177 glTexCoord2f(1.0, 0.0); glVertex3f(-hdx, -hdy, -hdz);
178 glTexCoord2f(1.0, 1.0); glVertex3f(-hdx, hdy, -hdz);
179 glTexCoord2f(0.0, 1.0); glVertex3f( hdx, hdy, -hdz);
182 glNormal3f(0.0, 1.0, 0.0);
183 glTexCoord2f(0.0, 0.0); glVertex3f(-hdx, hdy, hdz);
184 glTexCoord2f(1.0, 0.0); glVertex3f( hdx, hdy, hdz);
185 glTexCoord2f(1.0, 1.0); glVertex3f( hdx, hdy, -hdz);
186 glTexCoord2f(0.0, 1.0); glVertex3f(-hdx, hdy, -hdz);
189 glNormal3f(0.0, -1.0, 0.0);
190 glTexCoord2f(0.0, 0.0); glVertex3f(-hdx, -hdy, -hdz);
191 glTexCoord2f(1.0, 0.0); glVertex3f( hdx, -hdy, -hdz);
192 glTexCoord2f(1.0, 1.0); glVertex3f( hdx, -hdy, hdz);
193 glTexCoord2f(0.0, 1.0); glVertex3f(-hdx, -hdy, hdz);
196 glNormal3f(-1.0, 0.0, 0.0);
197 glTexCoord2f(0.0, 0.0); glVertex3f(-hdx, -hdy, -hdz);
198 glTexCoord2f(1.0, 0.0); glVertex3f(-hdx, -hdy, hdz);
199 glTexCoord2f(1.0, 1.0); glVertex3f(-hdx, hdy, hdz);
200 glTexCoord2f(0.0, 1.0); glVertex3f(-hdx, hdy, -hdz);
203 glNormal3f(1.0, 0.0, 0.0);
204 glTexCoord2f(0.0, 0.0); glVertex3f(hdx, -hdy, hdz);
205 glTexCoord2f(1.0, 0.0); glVertex3f(hdx, -hdy, -hdz);
206 glTexCoord2f(1.0, 1.0); glVertex3f(hdx, hdy, -hdz);
207 glTexCoord2f(0.0, 1.0); glVertex3f(hdx, hdy, hdz);
214 calculateAABB( minpoint, maxpoint, obj->matrix() );
218 real tdx = fabs(tm.x_ax[0]*dx) + fabs(tm.y_ax[0]*dy) + fabs(tm.z_ax[0]*dz);
219 real tdy = fabs(tm.x_ax[1]*dx) + fabs(tm.y_ax[1]*dy) + fabs(tm.z_ax[1]*dz);
220 real tdz = fabs(tm.x_ax[2]*dx) + fabs(tm.y_ax[2]*dy) + fabs(tm.z_ax[2]*dz);
222 wVector hds( tdx/2.0, tdy/2.0, tdz/2.0 );
223 minPoint = tm.w_pos - hds;
224 maxPoint = tm.w_pos + hds;
231 wVector hds( dx/2.0, dy/2.0, dz/2.0 );
248 virtual void render( QGLContext* gw ) {
253 mat.x_ax = mat.x_ax.
scale( rad );
254 mat.y_ax = mat.y_ax.
scale( rad );
255 mat.z_ax = mat.z_ax.
scale( rad );
256 GLMultMatrix(&mat[0][0]);
259 pObj = gluNewQuadric();
261 gluQuadricTexture(pObj,
true);
262 gluSphere(pObj, 1.0f, 20, 20);
263 gluDeleteQuadric(pObj);
268 calculateAABB( minpoint, maxpoint, obj->matrix() );
273 minPoint = tm.w_pos - rds;
274 maxPoint = tm.w_pos + rds;
278 dimension[0] = rad*2.0;
279 dimension[1] = rad*2.0;
280 dimension[2] = rad*2.0;
298 virtual void render( QGLContext* gw ) {
305 wMatrix matrix = wMatrix::yaw( PI_GRECO * 0.5f );
307 matrix = matrix * cil->matrix();
308 GLMultMatrix(&matrix[0][0]);
311 pObj = gluNewQuadric();
312 gluQuadricTexture(pObj,
true);
313 gluCylinder(pObj, rad, rad, hei, 20, 2);
316 gluQuadricOrientation(pObj, GLU_INSIDE);
317 gluDisk(pObj, 0.0f, rad, 20, 1);
319 glTranslatef (0.0f, 0.0f, hei);
320 gluQuadricOrientation(pObj, GLU_OUTSIDE);
321 gluDisk(pObj, 0.0f, rad, 20, 1);
323 gluDeleteQuadric(pObj);
328 calculateAABB( minpoint, maxpoint, obj->matrix() );
333 real tdx = fabs(tm.x_ax[0]*h2) + fabs(tm.y_ax[0]*rad) + fabs(tm.z_ax[0]*rad);
334 real tdy = fabs(tm.x_ax[1]*h2) + fabs(tm.y_ax[1]*rad) + fabs(tm.z_ax[1]*rad);
335 real tdz = fabs(tm.x_ax[2]*h2) + fabs(tm.y_ax[2]*rad) + fabs(tm.z_ax[2]*rad);
337 minPoint = tm.w_pos - hds;
338 maxPoint = tm.w_pos + hds;
343 dimension[1] = rad*2.0;
344 dimension[2] = rad*2.0;
359 rad = cone->radius();
360 hei = cone->height();
363 virtual void render( QGLContext* gw ) {
370 wMatrix matrix = wMatrix::yaw( PI_GRECO * 0.5 );
372 matrix = matrix * cone->matrix();
373 GLMultMatrix(&matrix[0][0]);
376 pObj = gluNewQuadric();
377 gluQuadricTexture(pObj,
true);
378 gluCylinder(pObj, rad, 0, hei, 20, 2);
381 gluQuadricOrientation(pObj, GLU_INSIDE);
382 gluDisk(pObj, 0.0f, rad, 20, 1);
384 gluDeleteQuadric(pObj);
389 calculateAABB( minpoint, maxpoint, obj->matrix() );
394 real tdx = fabs(tm.x_ax[0]*h2) + fabs(tm.y_ax[0]*rad) + fabs(tm.z_ax[0]*rad);
395 real tdy = fabs(tm.x_ax[1]*h2) + fabs(tm.y_ax[1]*rad) + fabs(tm.z_ax[1]*rad);
396 real tdz = fabs(tm.x_ax[2]*h2) + fabs(tm.y_ax[2]*rad) + fabs(tm.z_ax[2]*rad);
398 minPoint = tm.w_pos - hds;
399 maxPoint = tm.w_pos + hds;
404 dimension[1] = rad*2.0;
405 dimension[2] = rad*2.0;
416 #ifdef FARSA_USE_YARP_AND_ICUB
418 RenderICub::RenderICub( WObject* wobj, RenderWObjectContainer* container ) :
419 RenderWObject( wobj, container )
421 icub = (PhyiCub*)wobj;
422 icub->forceKinematicChainsUpdate(
true);
423 initRobjv(container);
426 RenderICub::~RenderICub()
429 icub->forceKinematicChainsUpdate(
false);
433 void RenderICub::render( QGLContext* gw )
438 void RenderICub::renderAABB( RenderWorld* gw )
440 wVector minpoint, maxpoint;
441 calculateAABB( minpoint, maxpoint, obj->matrix() );
442 gw->drawWireBox( minpoint, maxpoint );
445 void RenderICub::calculateAABB( wVector& minPoint, wVector& maxPoint,
const wMatrix tm )
448 QList<RenderWObject*> robjs = robjv.values();
449 robjs[0]->calculateAABB( minPoint, maxPoint, robjs[0]->
object()->matrix() );
451 for(
int i=1; i<robjs.size(); i++ ) {
452 robjs[i]->calculateAABB( minP, maxP, robjs[i]->
object()->matrix() );
453 mergeAABB( minPoint, maxPoint, minP, maxP );
457 void RenderICub::calculateOBB( wVector& dimension, wVector& minPoint, wVector& maxPoint )
468 dimension[0] = fabs( maxPoint[0] - minPoint[0] );
469 dimension[1] = fabs( maxPoint[1] - minPoint[1] );
470 dimension[2] = fabs( maxPoint[2] - minPoint[2] );
473 void RenderICub::objectAlreadyDestroyed()
478 void RenderICub::initRobjv( RenderWObjectContainer* container )
481 foreach( RenderWObject* wobj, container->graphics() ) {
482 if ( wobj->object()->owner() == icub ) {
483 robjv[wobj->object()] = wobj;
486 foreach( WObject* obj, icub->leftLeg() ) {
487 obj->setUseColorTextureOfOwner(
true);
489 foreach( WObject* obj, icub->rightLeg() ) {
490 obj->setUseColorTextureOfOwner(
true);
492 foreach( WObject* obj, icub->torso() ) {
493 obj->setUseColorTextureOfOwner(
true);
495 foreach( WObject* obj, icub->leftArm() ) {
496 obj->setUseColorTextureOfOwner(
true);
498 foreach( WObject* obj, icub->rightArm() ) {
499 obj->setUseColorTextureOfOwner(
true);
502 foreach( WObject* obj, icub->headNeck() ) {
504 if ( ( i == 4 ) || ( i == 5 ) ) {
505 obj->setUseColorTextureOfOwner(
false);
507 obj->setUseColorTextureOfOwner(
true);
511 foreach( WObject* obj, icub->covers() ) {
512 obj->setUseColorTextureOfOwner(
false);
516 void RenderICub::mergeAABB( wVector& minPointA, wVector& maxPointA,
const wVector& minPointB,
const wVector& maxPointB )
518 minPointA[0] =
min( minPointA[0], minPointB[0] );
519 minPointA[1] =
min( minPointA[1], minPointB[1] );
520 minPointA[2] =
min( minPointA[2], minPointB[2] );
521 maxPointA[0] =
max( maxPointA[0], maxPointB[0] );
522 maxPointA[1] =
max( maxPointA[1], maxPointB[1] );
523 maxPointA[2] =
max( maxPointA[2], maxPointB[2] );
532 initRobjv( container );
539 virtual void render( QGLContext* gw ) {
543 GLMultMatrix(&matrix[0][0]);
555 robjv[0]->calculateAABB( minPoint, maxPoint, robjv[0]->
object()->matrix()*tm );
557 for(
int i=1; i<robjv.size(); i++ ) {
558 robjv[i]->calculateAABB( minP, maxP, robjv[i]->
object()->matrix()*tm );
559 mergeAABB( minPoint, maxPoint, minP, maxP );
566 if ( robjv.size() == 0 ) {
567 qWarning(
"== this point should never reached: renderwobjecthierarchy.cpp:435" );
570 robjv[0]->calculateAABB( minPoint, maxPoint, robjv[0]->
object()->matrix() );
571 for(
int i=1; i<robjv.size(); i++ ) {
572 robjv[i]->calculateAABB( minP, maxP, robjv[i]->
object()->matrix() );
573 mergeAABB( minPoint, maxPoint, minP, maxP );
575 dimension[0] = fabs( maxPoint[0] - minPoint[0] );
576 dimension[1] = fabs( maxPoint[1] - minPoint[1] );
577 dimension[2] = fabs( maxPoint[2] - minPoint[2] );
581 QVector<RenderWObject*> robjv;
588 void mergeAABB( wVector& minPointA, wVector& maxPointA,
const wVector& minPointB,
const wVector& maxPointB ) {
589 minPointA[0] =
min( minPointA[0], minPointB[0] );
590 minPointA[1] =
min( minPointA[1], minPointB[1] );
591 minPointA[2] =
min( minPointA[2], minPointB[2] );
592 maxPointA[0] =
max( maxPointA[0], maxPointB[0] );
593 maxPointA[1] =
max( maxPointA[1], maxPointB[1] );
594 maxPointA[2] =
max( maxPointA[2], maxPointB[2] );
598 #ifdef FARSA_USE_YARP_AND_ICUB
609 virtual void render( QGLContext* gw ) {
614 GLMultMatrix(&m[0][0]);
619 float hdx = (dx/2.0);
620 float hdy = (dy/2.0);
621 float hdz = (dz/2.0);
623 glNormal3f(0.0, 0.0, 1.0);
624 glTexCoord2f(0.0, 0.0); glVertex3f(-hdx, -hdy, hdz);
625 glTexCoord2f(1.0, 0.0); glVertex3f( hdx, -hdy, hdz);
626 glTexCoord2f(1.0, 1.0); glVertex3f( hdx, hdy, hdz);
627 glTexCoord2f(0.0, 1.0); glVertex3f(-hdx, hdy, hdz);
629 glNormal3f(0.0, 0.0, -1.0);
630 glTexCoord2f(0.0, 0.0); glVertex3f( hdx, -hdy, -hdz);
631 glTexCoord2f(1.0, 0.0); glVertex3f(-hdx, -hdy, -hdz);
632 glTexCoord2f(1.0, 1.0); glVertex3f(-hdx, hdy, -hdz);
633 glTexCoord2f(0.0, 1.0); glVertex3f( hdx, hdy, -hdz);
635 glNormal3f(0.0, 1.0, 0.0);
636 glTexCoord2f(0.0, 0.0); glVertex3f(-hdx, hdy, hdz);
637 glTexCoord2f(1.0, 0.0); glVertex3f( hdx, hdy, hdz);
638 glTexCoord2f(1.0, 1.0); glVertex3f( hdx, hdy, -hdz);
639 glTexCoord2f(0.0, 1.0); glVertex3f(-hdx, hdy, -hdz);
641 glNormal3f(0.0, -1.0, 0.0);
642 glTexCoord2f(0.0, 0.0); glVertex3f(-hdx, -hdy, -hdz);
643 glTexCoord2f(1.0, 0.0); glVertex3f( hdx, -hdy, -hdz);
644 glTexCoord2f(1.0, 1.0); glVertex3f( hdx, -hdy, hdz);
645 glTexCoord2f(0.0, 1.0); glVertex3f(-hdx, -hdy, hdz);
647 glNormal3f(-1.0, 0.0, 0.0);
648 glTexCoord2f(0.0, 0.0); glVertex3f(-hdx, -hdy, -hdz);
649 glTexCoord2f(1.0, 0.0); glVertex3f(-hdx, -hdy, hdz);
650 glTexCoord2f(1.0, 1.0); glVertex3f(-hdx, hdy, hdz);
651 glTexCoord2f(0.0, 1.0); glVertex3f(-hdx, hdy, -hdz);
653 glNormal3f(1.0, 0.0, 0.0);
654 glTexCoord2f(0.0, 0.0); glVertex3f(hdx, -hdy, hdz);
655 glTexCoord2f(1.0, 0.0); glVertex3f(hdx, -hdy, -hdz);
656 glTexCoord2f(1.0, 1.0); glVertex3f(hdx, hdy, -hdz);
657 glTexCoord2f(0.0, 1.0); glVertex3f(hdx, hdy, hdz);
672 real tdx = fabs(tm.x_ax[0]*dx) + fabs(tm.y_ax[0]*dy) + fabs(tm.z_ax[0]*dz);
673 real tdy = fabs(tm.x_ax[1]*dx) + fabs(tm.y_ax[1]*dy) + fabs(tm.z_ax[1]*dz);
674 real tdz = fabs(tm.x_ax[2]*dx) + fabs(tm.y_ax[2]*dy) + fabs(tm.z_ax[2]*dz);
675 wVector hds( tdx/2.0, tdy/2.0, tdz/2.0 );
676 minPoint = tm.w_pos - hds;
677 maxPoint = tm.w_pos + hds;
683 wVector hds( dx/2.0, dy/2.0, dz/2.0 );
692 #endif // FARSA_USE_YARP_AND_ICUB
701 m_graphicalWObject(dynamic_cast<GraphicalWObject*>(obj))
709 virtual void render(QGLContext *gw)
733 bool RenderWObjectContainer::facInited =
false;
734 QMap<QString, WAbstractCreator*>* RenderWObjectContainer::fac;
735 void RenderWObjectContainer::initFactory() {
736 if ( facInited )
return;
738 fac =
new QMap<QString, WAbstractCreator*>();
740 (*fac)[
"farsa::WMesh"] =
new WCreator<RenderWMesh>();
741 (*fac)[
"farsa::PhyBox"] =
new WCreator<RenderPhyBox>();
742 (*fac)[
"farsa::PhySphere"] =
new WCreator<RenderPhySphere>();
743 (*fac)[
"farsa::PhyCylinder"] =
new WCreator<RenderPhyCylinder>();
744 (*fac)[
"farsa::PhyCone"] =
new WCreator<RenderPhyCone>();
745 (*fac)[
"farsa::PhyCompoundObject"] =
new WCreator<RenderCompoundObject>();
746 #ifdef FARSA_USE_YARP_AND_ICUB
747 (*fac)[
"farsa::WCamera"] =
new WCreator<RenderWCamera>();
748 (*fac)[PhyiCub::staticMetaObject.className()] =
new WCreator<RenderICub>();
750 (*fac)[
"farsa::GraphicalWObject"] =
new WCreator<GraphicalWObjectRenderer>();
753 (*fac)[
"farsa::WObject"] =
new WCreator<RenderGenericObject>();
754 (*fac)[
"farsa::WorldController"] =
new WCreator<RenderGenericObject>();