void GeoXGLWidget3D::initializeGL()
{
  camera.setPosition(makeVector3f(0.0f,0.0f,10.0f));
  camera.setLookAt(makeVector3f(0.0f,0.0f,0.0f));
  camera.setUp(makeVector3f(0.0f,1.0f,0.0f));
  glViewport(0,0,width(),height());
  fillMode = GL_FILL;
  glPolygonMode(GL_FRONT_AND_BACK,fillMode);

  glEnable(GL_DEPTH_TEST);
  glShadeModel(GL_SMOOTH);
  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);
  lightOnOff = true;
  glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE );

  glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient);
  glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse);
  glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular);
  glLightfv(GL_LIGHT0,GL_POSITION,light_position);
  glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,light_spotDirection);
  glLightf (GL_LIGHT0,GL_SPOT_CUTOFF,light_spotCutOff);
  glLightf (GL_LIGHT0,GL_SPOT_EXPONENT,light_spotExponent);
  glLightf (GL_LIGHT0,GL_CONSTANT_ATTENUATION,light_attenuationConst);
  glLightf (GL_LIGHT0,GL_LINEAR_ATTENUATION,light_attenuationLinear);
  glLightf (GL_LIGHT0,GL_QUADRATIC_ATTENUATION,light_attenuationQuadratic);

  glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,material_ambient);
  glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,material_specular);
  glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,material_emission);
  glMaterialf (GL_FRONT_AND_BACK,GL_SHININESS,material_shininess);
}
Ejemplo n.º 2
0
                                                //
                                                // remark: do not forget to register your class in InitGeoX.cpp
                                                //         (have a look at that file for details)
                                                //
                                                //
ExampleExperiment::ExampleExperiment() {        // <--- constructor - set reasonable default parameters
   // important number                          //
   numberA = 42;                                //
   // suspicious number (float)                 //
   numberB = 23.0f;                             //
   // example vector -- x-axis                  //
   vector1 = makeVector3f(1,0,0);               //
   // example vector -- y-axis                  //
   vector2 = makeVector3f(0,1,0);               //
   // rotation by 90°                           //
   matrix = makeMatrix3f(0,-1, 0,               //
                         1, 0, 0,               //
                         0, 0, 1);              //
}                                               //
Vector3f Camera::calculateRotationAngles() 
{
	// http://de.wikipedia.org/wiki/Roll-Pitch-Yaw-Winkel
	Matrix3f mat = getLocalCoordsMatrix();
	float beta = atan2( -mat[2][0], sqrt(pow(mat[0][0],2.0f)+pow(mat[1][0],2.0f)) );
	float alpha, gamma;
	if(fabs(beta - M_PI/2.0f) < 1e-10f)
	{
		alpha = 0;
		gamma = atan2(mat[0][1],mat[1][1]);
	}
	else
	{
		if(fabs(beta + M_PI/2.0f) < 1e-10f)
		{
			alpha = 0;
			gamma = -atan2(mat[0][1],mat[1][1]);
		}
		else
		{
			float cos_beta = cos(beta);
			alpha = atan2(mat[1][0] / cos_beta,mat[0][0] / cos_beta);
			gamma = atan2(mat[2][1] / cos_beta,mat[2][2] / cos_beta);
		}
	}

	return makeVector3f(alpha,beta,gamma);
}
void GeoXGLWidget3D::mouseMoveEvent(QMouseEvent *event)
{
  if(mode == CAM || pickedPointIndex == -1)
  {
    camControl->mouseMoved(event->x(),event->y());
    GLfloat new_light_pos[] = {camera.getPosition()[0], camera.getPosition()[1], camera.getPosition()[2], 0.0f};
    glLightfv(GL_LIGHT0,GL_POSITION,new_light_pos);
    GLfloat new_light_spotDir[] = {-camera.getPosition()[0], -camera.getPosition()[1], -camera.getPosition()[2]};
    glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,new_light_spotDir);
  }
  else if(mode == PICK)
  {
    double modelview[16], projection[16];
    int viewport[4];
    glGetDoublev( GL_PROJECTION_MATRIX, projection );
    glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
    glGetIntegerv( GL_VIEWPORT, viewport );
    Vector3d objPos;
    gluUnProject(event->x(), viewport[3]-event->y(), pickedZ, modelview, projection, viewport, &objPos[0], &objPos[1], &objPos[2] );
    points[pickedPointIndex].position = makeVector3f(objPos[0], objPos[1],objPos[2]);
    recomputeNormal = true;
  }

  updateGL();
}
Ejemplo n.º 5
0
BoundingBox3f transformBoundingBox(const Matrix4f &transformation, const BoundingBox3f &boundingBox) {
   Vector3f v[8];
   v[0] = makeVector3f(boundingBox.lowerCorner[0], boundingBox.lowerCorner[1], boundingBox.lowerCorner[2]);
   v[1] = makeVector3f(boundingBox.lowerCorner[0], boundingBox.lowerCorner[1], boundingBox.upperCorner[2]);
   v[2] = makeVector3f(boundingBox.lowerCorner[0], boundingBox.upperCorner[1], boundingBox.lowerCorner[2]);
   v[3] = makeVector3f(boundingBox.lowerCorner[0], boundingBox.upperCorner[1], boundingBox.upperCorner[2]);
   v[4] = makeVector3f(boundingBox.upperCorner[0], boundingBox.lowerCorner[1], boundingBox.lowerCorner[2]);
   v[5] = makeVector3f(boundingBox.upperCorner[0], boundingBox.lowerCorner[1], boundingBox.upperCorner[2]);
   v[6] = makeVector3f(boundingBox.upperCorner[0], boundingBox.upperCorner[1], boundingBox.lowerCorner[2]);
   v[7] = makeVector3f(boundingBox.upperCorner[0], boundingBox.upperCorner[1], boundingBox.upperCorner[2]);
   Vector3f p = transformVector3f(transformation, v[0]);
   BoundingBox3f result(p);
   for (int i=1; i<8; i++) {
      result.addPoint(transformVector3f(transformation, v[i]));
   }
   return result;
}
Camera::Camera() {
   position = makeVector3f(0,0,0);
   lookAt = makeVector3f(0,0,1);
   up = makeVector3f(0,1,0);
   //verticalViewAngle = 45;

   orthoNormViewDirection = makeVector3f(0,0,1);
   orthoNormRightDirection = makeVector3f(1,0,0);
   orthoNormUpDirection = makeVector3f(0,1,0);
}
GeoXGLWidget3D::GeoXGLWidget3D(QWidget *parent, const QGLWidget *shareWidget, Qt::WindowFlags f)
: QGLWidget(parent, shareWidget, f)
{
  initializeGL();
  camControl = new ExaminerCameraController();
  camControl->bindCamera(&camera);
  camControl->setNotifyOnCameraChange(QDelegate(this, &GeoXGLWidget3D::updateGL));
  mode = CAM;
  pickedPointIndex = -1;
  pickedZ = -1;
  fillMode = GL_FILL;
  bgColor = makeVector3f(0.2f,0.2f,0.4f);
  drawAxisFlag = true;
  drawHandlePoints = false;
  recomputeNormal = false;
  faceLighting = true;
}
void GeoXGLWidget3D::mouseReleaseEvent (QMouseEvent * event)
{
  if((mode == PICK) && (pickedPointIndex != -1))
  {
    double modelview[16], projection[16];
    int viewport[4];
    glGetDoublev( GL_PROJECTION_MATRIX, projection );
    glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
    glGetIntegerv( GL_VIEWPORT, viewport );
    Vector3d objPos;
    gluUnProject(event->x(), viewport[3]-event->y(), pickedZ, modelview, projection, viewport, &objPos[0], &objPos[1], &objPos[2] );
    points[pickedPointIndex].position = makeVector3f(objPos[0], objPos[1],objPos[2]);
    pickedPointIndex = -1;
    pickedZ = -1;
    recomputeNormal = false;
    emit widgetContentChanged();
  }
  if (mode == CAM || pickedPointIndex == -1)
  {
    camControl->mouseUp(event->x(),event->y(),MouseButtons(event->button()==Qt::LeftButton,event->button()==Qt::RightButton, event->button()==Qt::MidButton),event->modifiers());
  }
  updateGL();
}
void GeoXGLWidget3D::resetCamera()
{
  camControl->setup(makeVector3f(0.0f,0.0f,10.0f),makeVector3f(0.0f,0.0f,0.0f),makeVector3f(0.0f,1.0f,0.0f));
}
void GeoXGLWidget3D::paintGL()
{
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  float32 aspect;
  if (height() > 0) {
    aspect = ((float)width())/((float)height());
  } else {
    aspect = 1;
  }
  vf.setXToYAspectRatio(aspect);
  if (mode == CAM_ORTHO)
    glOrtho(-10.0, 10.0, -10.0, 10.0, vf.getZNearClip(), vf.getZFarClip());
  else
    gluPerspective(vf.getVerticalFieldOfView(), aspect, vf.getZNearClip(), vf.getZFarClip());
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  Vector3f pos = camera.getPosition();
  Vector3f lookAt = camera.getLookAt();
  Vector3f up = camera.getUp();
  gluLookAt(pos[0],pos[1],pos[2],lookAt[0],lookAt[1],lookAt[2],up[0],up[1],up[2]);
  glClearColor(bgColor[0],bgColor[1],bgColor[2],0.0f);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  if(drawAxisFlag)
    drawAxis();

  if(drawHandlePoints)
  {
    if(lightOnOff)
      glDisable(GL_LIGHTING);
    for(unsigned int i = 0;i<points.size();i++)
    {
      if(!points[i].canBeModified)
        continue;

      glPointSize(points[i].size);
      glColor4f(points[i].color[0],points[i].color[1],points[i].color[2],points[i].color[3]);
      glBegin(GL_POINTS);
        glVertex3f(	points[i].position[0],
                points[i].position[1],
                points[i].position[2]);
      glEnd();
    }
    if(lightOnOff)
      glEnable(GL_LIGHTING);
  }
  for(unsigned int j = 0;j<lines.size();j++)
  {
     glLineWidth(lines[j].thickness);
    glColor4f(lines[j].color[0],lines[j].color[1],lines[j].color[2],lines[j].color[3]);
    glBegin(GL_LINES);
      glVertex3f(	points[lines[j].vertices[0]].position[0],
              points[lines[j].vertices[0]].position[1],
              points[lines[j].vertices[0]].position[2]);
      glVertex3f(	points[lines[j].vertices[1]].position[0],
              points[lines[j].vertices[1]].position[1],
              points[lines[j].vertices[1]].position[2]);
    glEnd();
  }

  for(unsigned int k = 0;k<triangles.size();k++)
  {
    glColor4f(triangles[k].color[0],triangles[k].color[1],triangles[k].color[2],triangles[k].color[3]);
    glBegin(GL_TRIANGLES);
    if(faceLighting)
    {
      GLfloat material_diffuse[] = {triangles[k].color[0], triangles[k].color[1],
                          triangles[k].color[2], triangles[k].color[3]};
      glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,material_diffuse);

      Vector3f zero = makeVector3f(0.0,0.0,0.0);
      if((triangles[k].normal == zero) || recomputeNormal)
      {
        Vector3f d1 = points[triangles[k].vertices[0]].position - points[triangles[k].vertices[1]].position;
        Vector3f d2 = points[triangles[k].vertices[0]].position - points[triangles[k].vertices[2]].position;
        Vector3f n = d1.crossProduct(d2);
        n = normalize(n);
        triangles[k].normal = n;
      }
      glNormal3f(triangles[k].normal[0],triangles[k].normal[1],triangles[k].normal[2]);

      glVertex3f(	points[triangles[k].vertices[0]].position[0],
              points[triangles[k].vertices[0]].position[1],
              points[triangles[k].vertices[0]].position[2]);
      glVertex3f(	points[triangles[k].vertices[1]].position[0],
              points[triangles[k].vertices[1]].position[1],
              points[triangles[k].vertices[1]].position[2]);
      glVertex3f( points[triangles[k].vertices[2]].position[0],
              points[triangles[k].vertices[2]].position[1],
              points[triangles[k].vertices[2]].position[2]);
    }
    else
    {
      GLfloat material_diffuse0[] = {points[triangles[k].vertices[0]].color[0],
                           points[triangles[k].vertices[0]].color[1],
                           points[triangles[k].vertices[0]].color[2],
                           points[triangles[k].vertices[0]].color[3]};
      glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,material_diffuse0);
      glNormal3f(points[triangles[k].vertices[0]].normal[0],
                points[triangles[k].vertices[0]].normal[1],
                points[triangles[k].vertices[0]].normal[2]);
      glVertex3f(	points[triangles[k].vertices[0]].position[0],
              points[triangles[k].vertices[0]].position[1],
              points[triangles[k].vertices[0]].position[2]);

      GLfloat material_diffuse1[] = {points[triangles[k].vertices[1]].color[0],
                           points[triangles[k].vertices[1]].color[1],
                               points[triangles[k].vertices[1]].color[2],
                                    points[triangles[k].vertices[1]].color[3]};
      glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,material_diffuse1);
      glNormal3f(points[triangles[k].vertices[1]].normal[0],
               points[triangles[k].vertices[1]].normal[1],
                points[triangles[k].vertices[1]].normal[2]);
      glVertex3f(	points[triangles[k].vertices[1]].position[0],
              points[triangles[k].vertices[1]].position[1],
              points[triangles[k].vertices[1]].position[2]);

      GLfloat material_diffuse2[] = {points[triangles[k].vertices[2]].color[0],
                           points[triangles[k].vertices[2]].color[1],
                           points[triangles[k].vertices[2]].color[2],
                           points[triangles[k].vertices[2]].color[3]};
      glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,material_diffuse2);
      glNormal3f(points[triangles[k].vertices[2]].normal[0],
              points[triangles[k].vertices[2]].normal[1],
              points[triangles[k].vertices[2]].normal[2]);
      glVertex3f(	points[triangles[k].vertices[2]].position[0],
              points[triangles[k].vertices[2]].position[1],
              points[triangles[k].vertices[2]].position[2]);
    }
    glEnd();
  }
}
Ejemplo n.º 11
0
Vector3f polarCoordsToNormal(const Vector2f &pCoords) {
   return makeVector3f(sin(pCoords[0])*sin(pCoords[1]),
                       sin(pCoords[0])*cos(pCoords[1]),
                       cos(pCoords[0]));
}
Ejemplo n.º 12
0
void SimpleGLMeshMaterial::draw( TriangleMesh *mesh )
{
   if (mesh != NULL) {
      DynamicArrayOfStructures *pts = mesh->getVertices();

      if (!pts->providesAttribute("position")) return;
      AAT POS = pts->getAAT("position");

      AAT COL;
      bool hasCol = pts->providesAttribute("color");
      if (hasCol) COL = pts->getAAT("color");

      AAT NRM;
      bool hasNormal = pts->providesAttribute("normal");
      if (hasNormal) NRM = pts->getAAT("normal");

      DynamicArrayOfStructures *idx = mesh->getTriangles();
      if (!idx->providesAttribute("index")) return;
      AAT IDX = idx->getAAT("index");
      
      glPushAttrib(GL_ENABLE_BIT);

      if( showWireFrameOnly && showWireFrameOnlyDepthTest )
      {
         glEnable(GL_DEPTH_TEST);
			if( cullBackFace )
		      glEnable(GL_CULL_FACE);
			else
			   glDisable(GL_CULL_FACE);
         glEnable( GL_POLYGON_OFFSET_FILL );
         glPolygonOffset( 1.2f, 4.0f );

         const card32 numTri = idx->getNumEntries();
         glBegin(GL_TRIANGLES);
         glColor3f(1,1,1);
         for (card32 i=0; i<numTri; i++) {
            Vector3i tind = idx->get<int32, 3>(i, IDX);
            Vector3f pos[3];
            pos[0] = pts->get<float32, 3>(tind[0], POS);
            pos[1] = pts->get<float32, 3>(tind[1], POS);
            pos[2] = pts->get<float32, 3>(tind[2], POS);


            glVertex3fv(pos[0].data());
            glVertex3fv(pos[1].data());
            glVertex3fv(pos[2].data());
         }
         glEnd();
      }
      else
      if( !showWireFrameOnly ) {
          if( normalsDirectionColors )
          {
             glEnable(GL_DEPTH_TEST);
                if( cullBackFace )
                  glEnable(GL_CULL_FACE);
                else
                   glDisable(GL_CULL_FACE);

             const card32 numTri = idx->getNumEntries();
             glBegin(GL_TRIANGLES);
             for (card32 i=0; i<numTri; i++) {
                Vector3i tind = idx->get<int32, 3>(i, IDX);
                Vector3f pos[3];
                Vector3f color[3];
                Vector3f normal[3];
                pos[0] = pts->get<float32, 3>(tind[0], POS);
                pos[1] = pts->get<float32, 3>(tind[1], POS);
                pos[2] = pts->get<float32, 3>(tind[2], POS);

                if (!hasNormal || !useVertexNormals) {
                   Vector3f tnormal = (pos[2]-pos[0]).crossProduct(pos[1]-pos[0]);
                   float32 n = norm(tnormal);
                   if (n > 1e-7)
                      tnormal /= n;
                   else
                      continue;
                   normal[0] = tnormal;
                   normal[1] = tnormal;
                   normal[2] = tnormal;
                }

                if (hasNormal && useVertexNormals) {
                   normal[0] = pts->get<float32, 3>(tind[0], NRM);
                   normal[1] = pts->get<float32, 3>(tind[1], NRM);
                   normal[2] = pts->get<float32, 3>(tind[2], NRM);
                }

                for( card32 j=0; j<3; j++ ) {
                   color[j] = (normal[j] +  makeVector3f(1,1,1))* 0.5f;
                }

                glColor3fv(color[0].data());
                glVertex3fv(pos[0].data());
                glColor3fv(color[1].data());
                glVertex3fv(pos[1].data());
                glColor3fv(color[2].data());
                glVertex3fv(pos[2].data());
             }
             glEnd();
          }
          else {

             glEnable(GL_DEPTH_TEST);
             glEnable(GL_LIGHTING);
             glEnable(GL_NORMALIZE);
             glEnable(GL_COLOR_MATERIAL);
             glDisable(GL_CULL_FACE);
             glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
             glEnable(GL_LIGHT0);
             glColor4f(1,1,1,1);
             float lightPos[] = {100.0f,1000.0f,100.0f,1.0f};
             glLightfv( GL_LIGHT0, GL_POSITION, lightPos );

             const card32 numTri = idx->getNumEntries();
             glBegin(GL_TRIANGLES);
             for (card32 i=0; i<numTri; i++) {
                Vector3i tind = idx->get<int32, 3>(i, IDX);
                Vector3f pos[3];
                Vector3f color[3];
                Vector3f normal[3];

                pos[0] = pts->get<float32, 3>(tind[0], POS);
                pos[1] = pts->get<float32, 3>(tind[1], POS);
                pos[2] = pts->get<float32, 3>(tind[2], POS);

                if (!hasNormal || !useVertexNormals) {
                   Vector3f tnormal = (pos[2]-pos[0]).crossProduct(pos[1]-pos[0]);
                   float32 n = norm(tnormal);
                   if (n > 1e-20) tnormal /= n;
                   normal[0] = tnormal;
                   normal[1] = tnormal;
                   normal[2] = tnormal;
                }

                if (hasCol) {
                   color[0] = pts->get<float32, 3>(tind[0], COL);
                   color[1] = pts->get<float32, 3>(tind[1], COL);
                   color[2] = pts->get<float32, 3>(tind[2], COL);
                }
                if (hasNormal && useVertexNormals) {
                   normal[0] = pts->get<float32, 3>(tind[0], NRM);
                   normal[1] = pts->get<float32, 3>(tind[1], NRM);
                   normal[2] = pts->get<float32, 3>(tind[2], NRM);
                }

                if (hasCol) glColor3fv(color[0].data());
                glNormal3fv(normal[0].data());
                glVertex3fv(pos[0].data());
                if (hasCol) glColor3fv(color[1].data());
                glNormal3fv(normal[1].data());
                glVertex3fv(pos[1].data());
                if (hasCol) glColor3fv(color[2].data());
                glNormal3fv(normal[2].data());
                glVertex3fv(pos[2].data());
             }
             glEnd();
          }
       }
      glPopAttrib();

      glPushAttrib(GL_ENABLE_BIT);

       if (wireframe || showWireFrameOnly) {
         if( showWireFrameOnlyDepthTest && showWireFrameOnly)
            glEnable(GL_DEPTH_TEST);
         else
            glDisable(GL_DEPTH_TEST);
         glDisable(GL_LIGHTING);
         glDisable(GL_COLOR_MATERIAL);
         if( !blackWireFrame )
            glColor4f(1,1,1,0.2f);
         else
            glColor4f(0,0,0,0.2f);

			glBlendFunc(GL_SRC_ALPHA, GL_ONE);
         glDepthMask(false);
         glEnable(GL_LINE_SMOOTH);
         glDisable( GL_CULL_FACE );

         const card32 numTri = idx->getNumEntries();
         glBegin(GL_LINES);
         for (card32 i=0; i<numTri; i++) {
            Vector3i tind = idx->get<int32, 3>(i, IDX);
            Vector3f pos[3];
            pos[0] = pts->get<float32, 3>(tind[0], POS);
            pos[1] = pts->get<float32, 3>(tind[1], POS);
            pos[2] = pts->get<float32, 3>(tind[2], POS);

            glVertex3fv(pos[0].data());
            glVertex3fv(pos[1].data());

            glVertex3fv(pos[1].data());
            glVertex3fv(pos[2].data());

            glVertex3fv(pos[2].data());
            glVertex3fv(pos[0].data());
         }
         glEnd();

         glDepthMask(true);
      }

      glPopAttrib();

      glPushAttrib(GL_ENABLE_BIT);
      if( drawNormalsVectors && hasNormal)
      {
         glBegin( GL_LINES );
         glColor3f( 1,0,0);
         for( card32 i=0;i<pts->getNumEntries();i++ )
         {
            Vector3f pos = pts->get<float32,3>(i, POS );
            Vector3f normal = pts->get<float32,3>(i, NRM );
            Vector3f p2 = pos + normal * normalLength;
            glVertex3fv( &pos[0] );
            glVertex3fv( &p2[0] );
         }
         glEnd();
      }

      glPopAttrib();
   }
}
void Camera::setup(const StaticVector<float,6> &p, float lookAtDistance) 
{
	setup( makeVector3f(p[0],p[1],p[2]), makeVector3f(p[3],p[4],p[5]), lookAtDistance );
}