20 #include "renderworld.h"
21 #include "physphere.h"
23 #include "phycylinder.h"
28 #include "phycompoundobject.h"
30 #include "graphicalwobject.h"
31 #include "private/renderwobjecthierarchy.h"
43 # include <GLUT/glut.h>
48 using namespace qglviewer;
50 #define GLMultMatrix glMultMatrixf
65 virtual void render( QGLContext* ) {
69 calculateAABB( minpoint, maxpoint, obj->matrix() );
74 minPoint = tm.w_pos - rds;
75 maxPoint = tm.w_pos + rds;
79 dimension[0] = rad*2.0;
80 dimension[1] = rad*2.0;
81 dimension[2] = rad*2.0;
96 virtual void render( QGLContext* gw ) {
99 if ( wmesh->attachedTo() ) {
100 m = m * wmesh->attachedTo()->matrix();
102 GLMultMatrix(&m[0][0]);
103 int m_numMeshes = wmesh->meshesCount();
111 for (
int i = 0; i < m_numMeshes; i++ ) {
112 int materialIndex = m_pMeshes[i].m_materialIndex;
113 if ( materialIndex >= 0 ) {
114 glMaterialfv( GL_FRONT, GL_AMBIENT, m_pMaterials[materialIndex].m_ambient );
115 glMaterialfv( GL_FRONT, GL_DIFFUSE, m_pMaterials[materialIndex].m_diffuse );
116 glMaterialfv( GL_FRONT, GL_SPECULAR, m_pMaterials[materialIndex].m_specular );
117 glMaterialfv( GL_FRONT, GL_EMISSION, m_pMaterials[materialIndex].m_emissive );
118 glMaterialf( GL_FRONT, GL_SHININESS, m_pMaterials[materialIndex].m_shininess );
122 glBegin( GL_TRIANGLES );
123 for (
int j = 0; j < m_pMeshes[i].m_numTriangles; j++ ) {
124 int triangleIndex = m_pMeshes[i].m_pTriangleIndices[j];
126 for (
int k = 0; k < 3; k++ ) {
127 int index = pTri->m_vertexIndices[k];
128 glNormal3fv( pTri->m_vertexNormals[k] );
129 glTexCoord2f( pTri->m_s[k], pTri->m_t[k] );
130 glVertex3fv( m_pVertices[index].m_location );
156 virtual void render( QGLContext* gw ) {
159 const wMatrix& m = box->matrix();
160 GLMultMatrix(&m[0][0]);
166 float hdx = (dx/2.0);
167 float hdy = (dy/2.0);
168 float hdz = (dz/2.0);
170 glNormal3f(0.0, 0.0, 1.0);
171 glTexCoord2f(0.0, 0.0); glVertex3f(-hdx, -hdy, hdz);
172 glTexCoord2f(1.0, 0.0); glVertex3f( hdx, -hdy, hdz);
173 glTexCoord2f(1.0, 1.0); glVertex3f( hdx, hdy, hdz);
174 glTexCoord2f(0.0, 1.0); glVertex3f(-hdx, hdy, hdz);
177 glNormal3f(0.0, 0.0, -1.0);
178 glTexCoord2f(0.0, 0.0); glVertex3f( hdx, -hdy, -hdz);
179 glTexCoord2f(1.0, 0.0); glVertex3f(-hdx, -hdy, -hdz);
180 glTexCoord2f(1.0, 1.0); glVertex3f(-hdx, hdy, -hdz);
181 glTexCoord2f(0.0, 1.0); glVertex3f( hdx, hdy, -hdz);
184 glNormal3f(0.0, 1.0, 0.0);
185 glTexCoord2f(0.0, 0.0); glVertex3f(-hdx, hdy, hdz);
186 glTexCoord2f(1.0, 0.0); glVertex3f( hdx, hdy, hdz);
187 glTexCoord2f(1.0, 1.0); glVertex3f( hdx, hdy, -hdz);
188 glTexCoord2f(0.0, 1.0); glVertex3f(-hdx, hdy, -hdz);
191 glNormal3f(0.0, -1.0, 0.0);
192 glTexCoord2f(0.0, 0.0); glVertex3f(-hdx, -hdy, -hdz);
193 glTexCoord2f(1.0, 0.0); glVertex3f( hdx, -hdy, -hdz);
194 glTexCoord2f(1.0, 1.0); glVertex3f( hdx, -hdy, hdz);
195 glTexCoord2f(0.0, 1.0); glVertex3f(-hdx, -hdy, hdz);
198 glNormal3f(-1.0, 0.0, 0.0);
199 glTexCoord2f(0.0, 0.0); glVertex3f(-hdx, -hdy, -hdz);
200 glTexCoord2f(1.0, 0.0); glVertex3f(-hdx, -hdy, hdz);
201 glTexCoord2f(1.0, 1.0); glVertex3f(-hdx, hdy, hdz);
202 glTexCoord2f(0.0, 1.0); glVertex3f(-hdx, hdy, -hdz);
205 glNormal3f(1.0, 0.0, 0.0);
206 glTexCoord2f(0.0, 0.0); glVertex3f(hdx, -hdy, hdz);
207 glTexCoord2f(1.0, 0.0); glVertex3f(hdx, -hdy, -hdz);
208 glTexCoord2f(1.0, 1.0); glVertex3f(hdx, hdy, -hdz);
209 glTexCoord2f(0.0, 1.0); glVertex3f(hdx, hdy, hdz);
216 calculateAABB( minpoint, maxpoint, obj->matrix() );
220 real tdx = fabs(tm.x_ax[0]*dx) + fabs(tm.y_ax[0]*dy) + fabs(tm.z_ax[0]*dz);
221 real tdy = fabs(tm.x_ax[1]*dx) + fabs(tm.y_ax[1]*dy) + fabs(tm.z_ax[1]*dz);
222 real tdz = fabs(tm.x_ax[2]*dx) + fabs(tm.y_ax[2]*dy) + fabs(tm.z_ax[2]*dz);
224 wVector hds( tdx/2.0, tdy/2.0, tdz/2.0 );
225 minPoint = tm.w_pos - hds;
226 maxPoint = tm.w_pos + hds;
233 wVector hds( dx/2.0, dy/2.0, dz/2.0 );
250 virtual void render( QGLContext* gw ) {
255 mat.x_ax = mat.x_ax.
scale( rad );
256 mat.y_ax = mat.y_ax.
scale( rad );
257 mat.z_ax = mat.z_ax.
scale( rad );
258 GLMultMatrix(&mat[0][0]);
261 pObj = gluNewQuadric();
263 gluQuadricTexture(pObj,
true);
264 gluSphere(pObj, 1.0f, 20, 20);
265 gluDeleteQuadric(pObj);
270 calculateAABB( minpoint, maxpoint, obj->matrix() );
275 minPoint = tm.w_pos - rds;
276 maxPoint = tm.w_pos + rds;
280 dimension[0] = rad*2.0;
281 dimension[1] = rad*2.0;
282 dimension[2] = rad*2.0;
300 virtual void render( QGLContext* gw ) {
307 wMatrix matrix = wMatrix::yaw( PI_GRECO * 0.5f );
309 matrix = matrix * cil->matrix();
310 GLMultMatrix(&matrix[0][0]);
313 pObj = gluNewQuadric();
314 gluQuadricTexture(pObj,
true);
315 gluCylinder(pObj, rad, rad, hei, 20, 2);
318 gluQuadricOrientation(pObj, GLU_INSIDE);
319 gluDisk(pObj, 0.0f, rad, 20, 1);
321 glTranslatef (0.0f, 0.0f, hei);
322 gluQuadricOrientation(pObj, GLU_OUTSIDE);
323 gluDisk(pObj, 0.0f, rad, 20, 1);
325 gluDeleteQuadric(pObj);
330 calculateAABB( minpoint, maxpoint, obj->matrix() );
335 real tdx = fabs(tm.x_ax[0]*h2) + fabs(tm.y_ax[0]*rad) + fabs(tm.z_ax[0]*rad);
336 real tdy = fabs(tm.x_ax[1]*h2) + fabs(tm.y_ax[1]*rad) + fabs(tm.z_ax[1]*rad);
337 real tdz = fabs(tm.x_ax[2]*h2) + fabs(tm.y_ax[2]*rad) + fabs(tm.z_ax[2]*rad);
339 minPoint = tm.w_pos - hds;
340 maxPoint = tm.w_pos + hds;
345 dimension[1] = rad*2.0;
346 dimension[2] = rad*2.0;
361 rad = cone->radius();
362 hei = cone->height();
365 virtual void render( QGLContext* gw ) {
372 wMatrix matrix = wMatrix::yaw( PI_GRECO * 0.5 );
374 matrix = matrix * cone->matrix();
375 GLMultMatrix(&matrix[0][0]);
378 pObj = gluNewQuadric();
379 gluQuadricTexture(pObj,
true);
380 gluCylinder(pObj, rad, 0, hei, 20, 2);
383 gluQuadricOrientation(pObj, GLU_INSIDE);
384 gluDisk(pObj, 0.0f, rad, 20, 1);
386 gluDeleteQuadric(pObj);
391 calculateAABB( minpoint, maxpoint, obj->matrix() );
396 real tdx = fabs(tm.x_ax[0]*h2) + fabs(tm.y_ax[0]*rad) + fabs(tm.z_ax[0]*rad);
397 real tdy = fabs(tm.x_ax[1]*h2) + fabs(tm.y_ax[1]*rad) + fabs(tm.z_ax[1]*rad);
398 real tdz = fabs(tm.x_ax[2]*h2) + fabs(tm.y_ax[2]*rad) + fabs(tm.z_ax[2]*rad);
400 minPoint = tm.w_pos - hds;
401 maxPoint = tm.w_pos + hds;
406 dimension[1] = rad*2.0;
407 dimension[2] = rad*2.0;
508 #ifdef FARSA_USE_YARP_AND_ICUB
510 RenderICub::RenderICub( WObject* wobj, RenderWObjectContainer* container ) :
511 RenderWObject( wobj, container )
513 icub = (PhyiCub*)wobj;
514 icub->forceKinematicChainsUpdate(
true);
515 initRobjv(container);
518 RenderICub::~RenderICub()
521 icub->forceKinematicChainsUpdate(
false);
525 void RenderICub::render( QGLContext* gw )
530 void RenderICub::renderAABB( RenderWorld* gw )
532 wVector minpoint, maxpoint;
533 calculateAABB( minpoint, maxpoint, obj->matrix() );
534 gw->drawWireBox( minpoint, maxpoint );
537 void RenderICub::calculateAABB( wVector& minPoint, wVector& maxPoint,
const wMatrix tm )
540 QList<RenderWObject*> robjs = robjv.values();
541 robjs[0]->calculateAABB( minPoint, maxPoint, robjs[0]->
object()->matrix() );
543 for(
int i=1; i<robjs.size(); i++ ) {
544 robjs[i]->calculateAABB( minP, maxP, robjs[i]->
object()->matrix() );
545 mergeAABB( minPoint, maxPoint, minP, maxP );
549 void RenderICub::calculateOBB( wVector& dimension, wVector& minPoint, wVector& maxPoint )
560 dimension[0] = fabs( maxPoint[0] - minPoint[0] );
561 dimension[1] = fabs( maxPoint[1] - minPoint[1] );
562 dimension[2] = fabs( maxPoint[2] - minPoint[2] );
565 void RenderICub::objectAlreadyDestroyed()
570 void RenderICub::initRobjv( RenderWObjectContainer* container )
573 foreach( RenderWObject* wobj, container->graphics() ) {
574 if ( wobj->object()->owner() == icub ) {
575 robjv[wobj->object()] = wobj;
578 foreach( WObject* obj, icub->leftLeg() ) {
579 obj->setUseColorTextureOfOwner(
true);
581 foreach( WObject* obj, icub->rightLeg() ) {
582 obj->setUseColorTextureOfOwner(
true);
584 foreach( WObject* obj, icub->torso() ) {
585 obj->setUseColorTextureOfOwner(
true);
587 foreach( WObject* obj, icub->leftArm() ) {
588 obj->setUseColorTextureOfOwner(
true);
590 foreach( WObject* obj, icub->rightArm() ) {
591 obj->setUseColorTextureOfOwner(
true);
594 foreach( WObject* obj, icub->headNeck() ) {
596 if ( ( i == 4 ) || ( i == 5 ) ) {
597 obj->setUseColorTextureOfOwner(
false);
599 obj->setUseColorTextureOfOwner(
true);
603 foreach( WObject* obj, icub->covers() ) {
604 obj->setUseColorTextureOfOwner(
false);
608 void RenderICub::mergeAABB( wVector& minPointA, wVector& maxPointA,
const wVector& minPointB,
const wVector& maxPointB )
610 minPointA[0] =
min( minPointA[0], minPointB[0] );
611 minPointA[1] =
min( minPointA[1], minPointB[1] );
612 minPointA[2] =
min( minPointA[2], minPointB[2] );
613 maxPointA[0] =
max( maxPointA[0], maxPointB[0] );
614 maxPointA[1] =
max( maxPointA[1], maxPointB[1] );
615 maxPointA[2] =
max( maxPointA[2], maxPointB[2] );
624 initRobjv( container );
631 virtual void render( QGLContext* gw ) {
635 GLMultMatrix(&matrix[0][0]);
647 robjv[0]->calculateAABB( minPoint, maxPoint, robjv[0]->
object()->matrix()*tm );
649 for(
int i=1; i<robjv.size(); i++ ) {
650 robjv[i]->calculateAABB( minP, maxP, robjv[i]->
object()->matrix()*tm );
651 mergeAABB( minPoint, maxPoint, minP, maxP );
658 if ( robjv.size() == 0 ) {
659 qWarning(
"== this point should never reached: renderwobjecthierarchy.cpp:435" );
662 robjv[0]->calculateAABB( minPoint, maxPoint, robjv[0]->
object()->matrix() );
663 for(
int i=1; i<robjv.size(); i++ ) {
664 robjv[i]->calculateAABB( minP, maxP, robjv[i]->
object()->matrix() );
665 mergeAABB( minPoint, maxPoint, minP, maxP );
667 dimension[0] = fabs( maxPoint[0] - minPoint[0] );
668 dimension[1] = fabs( maxPoint[1] - minPoint[1] );
669 dimension[2] = fabs( maxPoint[2] - minPoint[2] );
673 QVector<RenderWObject*> robjv;
680 void mergeAABB( wVector& minPointA, wVector& maxPointA,
const wVector& minPointB,
const wVector& maxPointB ) {
681 minPointA[0] =
min( minPointA[0], minPointB[0] );
682 minPointA[1] =
min( minPointA[1], minPointB[1] );
683 minPointA[2] =
min( minPointA[2], minPointB[2] );
684 maxPointA[0] =
max( maxPointA[0], maxPointB[0] );
685 maxPointA[1] =
max( maxPointA[1], maxPointB[1] );
686 maxPointA[2] =
max( maxPointA[2], maxPointB[2] );
690 #ifdef FARSA_USE_YARP_AND_ICUB
701 virtual void render( QGLContext* gw ) {
706 GLMultMatrix(&m[0][0]);
711 float hdx = (dx/2.0);
712 float hdy = (dy/2.0);
713 float hdz = (dz/2.0);
715 glNormal3f(0.0, 0.0, 1.0);
716 glTexCoord2f(0.0, 0.0); glVertex3f(-hdx, -hdy, hdz);
717 glTexCoord2f(1.0, 0.0); glVertex3f( hdx, -hdy, hdz);
718 glTexCoord2f(1.0, 1.0); glVertex3f( hdx, hdy, hdz);
719 glTexCoord2f(0.0, 1.0); glVertex3f(-hdx, hdy, hdz);
721 glNormal3f(0.0, 0.0, -1.0);
722 glTexCoord2f(0.0, 0.0); glVertex3f( hdx, -hdy, -hdz);
723 glTexCoord2f(1.0, 0.0); glVertex3f(-hdx, -hdy, -hdz);
724 glTexCoord2f(1.0, 1.0); glVertex3f(-hdx, hdy, -hdz);
725 glTexCoord2f(0.0, 1.0); glVertex3f( hdx, hdy, -hdz);
727 glNormal3f(0.0, 1.0, 0.0);
728 glTexCoord2f(0.0, 0.0); glVertex3f(-hdx, hdy, hdz);
729 glTexCoord2f(1.0, 0.0); glVertex3f( hdx, hdy, hdz);
730 glTexCoord2f(1.0, 1.0); glVertex3f( hdx, hdy, -hdz);
731 glTexCoord2f(0.0, 1.0); glVertex3f(-hdx, hdy, -hdz);
733 glNormal3f(0.0, -1.0, 0.0);
734 glTexCoord2f(0.0, 0.0); glVertex3f(-hdx, -hdy, -hdz);
735 glTexCoord2f(1.0, 0.0); glVertex3f( hdx, -hdy, -hdz);
736 glTexCoord2f(1.0, 1.0); glVertex3f( hdx, -hdy, hdz);
737 glTexCoord2f(0.0, 1.0); glVertex3f(-hdx, -hdy, hdz);
739 glNormal3f(-1.0, 0.0, 0.0);
740 glTexCoord2f(0.0, 0.0); glVertex3f(-hdx, -hdy, -hdz);
741 glTexCoord2f(1.0, 0.0); glVertex3f(-hdx, -hdy, hdz);
742 glTexCoord2f(1.0, 1.0); glVertex3f(-hdx, hdy, hdz);
743 glTexCoord2f(0.0, 1.0); glVertex3f(-hdx, hdy, -hdz);
745 glNormal3f(1.0, 0.0, 0.0);
746 glTexCoord2f(0.0, 0.0); glVertex3f(hdx, -hdy, hdz);
747 glTexCoord2f(1.0, 0.0); glVertex3f(hdx, -hdy, -hdz);
748 glTexCoord2f(1.0, 1.0); glVertex3f(hdx, hdy, -hdz);
749 glTexCoord2f(0.0, 1.0); glVertex3f(hdx, hdy, hdz);
764 real tdx = fabs(tm.x_ax[0]*dx) + fabs(tm.y_ax[0]*dy) + fabs(tm.z_ax[0]*dz);
765 real tdy = fabs(tm.x_ax[1]*dx) + fabs(tm.y_ax[1]*dy) + fabs(tm.z_ax[1]*dz);
766 real tdz = fabs(tm.x_ax[2]*dx) + fabs(tm.y_ax[2]*dy) + fabs(tm.z_ax[2]*dz);
767 wVector hds( tdx/2.0, tdy/2.0, tdz/2.0 );
768 minPoint = tm.w_pos - hds;
769 maxPoint = tm.w_pos + hds;
775 wVector hds( dx/2.0, dy/2.0, dz/2.0 );
784 #endif // FARSA_USE_YARP_AND_ICUB
793 m_graphicalWObject(dynamic_cast<GraphicalWObject*>(obj))
801 virtual void render(QGLContext *gw)
825 bool RenderWObjectContainer::facInited =
false;
826 QMap<QString, WAbstractCreator*>* RenderWObjectContainer::fac;
827 void RenderWObjectContainer::initFactory() {
828 if ( facInited )
return;
830 fac =
new QMap<QString, WAbstractCreator*>();
832 (*fac)[
"farsa::WMesh"] =
new WCreator<RenderWMesh>();
833 (*fac)[
"farsa::PhyBox"] =
new WCreator<RenderPhyBox>();
834 (*fac)[
"farsa::PhySphere"] =
new WCreator<RenderPhySphere>();
835 (*fac)[
"farsa::PhyCylinder"] =
new WCreator<RenderPhyCylinder>();
836 (*fac)[
"farsa::PhyCone"] =
new WCreator<RenderPhyCone>();
838 (*fac)[
"farsa::PhyCompoundObject"] =
new WCreator<RenderCompoundObject>();
839 #ifdef FARSA_USE_YARP_AND_ICUB
840 (*fac)[
"farsa::WCamera"] =
new WCreator<RenderWCamera>();
841 (*fac)[PhyiCub::staticMetaObject.className()] =
new WCreator<RenderICub>();
843 (*fac)[
"farsa::GraphicalWObject"] =
new WCreator<GraphicalWObjectRenderer>();
846 (*fac)[
"farsa::WObject"] =
new WCreator<RenderGenericObject>();
847 (*fac)[
"farsa::WorldController"] =
new WCreator<RenderGenericObject>();