Esempio n. 1
0
void updateViewMy(int uploadIt)
{

    int viewport[4]={0};
    glm::mat4 Projection = glm::perspective(fov, 1.0f, near , far);

    buildOpenGLProjectionForIntrinsics   (
                                             projectionMatrix ,
                                             viewport ,
                                             fx,
                                             fy,
                                             0.0 , // SKEW
                                             windowWidth/2.0, windowHeight/2.0,
                                             windowWidth, windowHeight,
                                             near,
                                             far
                                           );

    glmMatToFloat(projectionMatrix, Projection);

    float rotation[16];
    float translation[16];
    MatrixF4x42Quaternion(camera.angle,qXqYqZqW,rotation);
    create4x4IdentityFMatrix(rotation);

    create4x4TranslationMatrix(translation , camera.pos[0] , camera.pos[1] , camera.pos[2]);

    multiplyTwo4x4FMatrices(modelViewMatrix, translation , rotation );

    multiplyTwo4x4FMatrices(modelViewProjectionMatrix, modelViewMatrix , projectionMatrix  );
    transpose4x4MatrixF(modelViewMatrix);
    transpose4x4MatrixF(modelViewProjectionMatrix);

    if (uploadIt)
    {
	 glUniformMatrix4fv(modelViewProjectionMatrixLocation  , 1 /*Only setting one matrix*/ , GL_FALSE /* dont transpose */, (const float * ) modelViewProjectionMatrix);
     checkOpenGLError(__FILE__, __LINE__);
    }
}
Esempio n. 2
0
void buildOpenGLProjectionForIntrinsics   (
                                             float * frustum,
                                             int * viewport ,
                                             float fx,
                                             float fy,
                                             float skew,
                                             float cx, float cy,
                                             unsigned int imageWidth, unsigned int imageHeight,
                                             float nearPlane,
                                             float farPlane
                                           )
{
   fprintf(stderr,"buildOpenGLProjectionForIntrinsics using old Ammar code Image(%ux%u)\n",imageWidth,imageHeight);
   fprintf(stderr,"fx %0.2f, fy %0.2f, cx %0.2f, cy %0.2f, skew %0.2f, ",fx,fy,cx,cy,skew);
   fprintf(stderr,"Near %0.2f, Far %0.2f\n",nearPlane,farPlane);

   if (farPlane==0.0)
   {
    fprintf(stderr,RED "Far plane is zero, argument bug..? \n" NORMAL);
    exit(1);
   }


    // These parameters define the final viewport that is rendered into by
    // the camera.
    //     Left    Bottom   Right       Top
    float L = 0.0 , B = 0.0  , R = imageWidth , T = imageHeight;

    // near and far clipping planes, these only matter for the mapping from
    // world-space z-coordinate into the depth coordinate for OpenGL
    float N = nearPlane , F = farPlane;
    float R_sub_L = R-L , T_sub_B = T-B , F_sub_N = F-N , F_plus_N = F+N , F_mul_N = F*N;

    if  ( (R_sub_L==0) || (R_sub_L-1.0f==0) ||
          (T_sub_B==0) || (T_sub_B-1.0f==0) ||
          (F_sub_N==0) ) { fprintf(stderr,"Problem with image limits R-L=%f , T-B=%f , F-N=%f\n",R_sub_L,T_sub_B,F_sub_N); }


   // set the viewport parameters
   viewport[0] = L; viewport[1] = B; viewport[2] = R_sub_L; viewport[3] = T_sub_B;

   //OpenGL Projection Matrix ready for loading ( column-major ) , also axis compensated
   frustum[0] = -2.0f*fx/R_sub_L;     frustum[1] = 0.0f;                 frustum[2] = 0.0f;                              frustum[3] = 0.0f;
   frustum[4] = 0.0f;                 frustum[5] = 2.0f*fy/T_sub_B;      frustum[6] = 0.0f;                              frustum[7] = 0.0f;
   frustum[8] = 2.0f*cx/R_sub_L-1.0f; frustum[9] = 2.0f*cy/T_sub_B-1.0f; frustum[10]=-1.0*(F_plus_N/F_sub_N);            frustum[11] = -1.0f;
   frustum[12]= 0.0f;                 frustum[13]= 0.0f;                 frustum[14]=-2.0f*F_mul_N/(F_sub_N);            frustum[15] = 0.0f;
   //Matrix already in OpenGL column major format



   //TROUBLESHOOTING Left To Right Hand conventions , Thanks Damien 24-06-15
   float identMat[16];
   float finalFrutstrum[16];
   create4x4IdentityMatrixF(identMat);
   identMat[10]=-1;
   multiplyTwo4x4FMatrices(finalFrutstrum,identMat,frustum);
   copy4x4FMatrix(frustum,finalFrutstrum);

   //This should produce our own Row Major Format
   transpose4x4Matrix(finalFrutstrum);
}
Esempio n. 3
0
void lookAt(
             float * matrix ,
             float eyex, float eyey, float eyez,
	         float centerx, float centery, float centerz,
	         float upx, float upy, float upz
	        )
{
   float x[3], y[3], z[3];
   float mag;

   /* Make rotation matrix */

   /* Z vector */
   z[0] = eyex - centerx;
   z[1] = eyey - centery;
   z[2] = eyez - centerz;
   mag = sqrt( z[0] * z[0] + z[1] * z[1] + z[2] * z[2]);
   if (mag)
    {
      z[0] /= mag;
      z[1] /= mag;
      z[2] /= mag;
    }

   /* Y vector */
   y[0] = upx;
   y[1] = upy;
   y[2] = upz;

   /* X vector = Y cross Z */
   x[0] = y[1] * z[2] - y[2] * z[1];
   x[1] = -y[0] * z[2] + y[2] * z[0];
   x[2] = y[0] * z[1] - y[1] * z[0];

   /* Recompute Y = Z cross X */
   y[0] = z[1] * x[2] - z[2] * x[1];
   y[1] = -z[0] * x[2] + z[2] * x[0];
   y[2] = z[0] * x[1] - z[1] * x[0];

   /* mpichler, 19950515 */
   /* cross product gives area of parallelogram, which is < 1.0 for
    * non-perpendicular unit-length vectors; so normalize x, y here
    */

   mag = sqrt(  x[0] * x[0] + x[1] * x[1] + x[2] * x[2]);
   if (mag)
    {
      x[0] /= mag;
      x[1] /= mag;
      x[2] /= mag;
    }

   mag = sqrt( y[0] * y[0] + y[1] * y[1] + y[2] * y[2]);
   if (mag)
    {
      y[0] /= mag;
      y[1] /= mag;
      y[2] /= mag;
    }

   float initial[16];
   initial[0] = x[0]; initial[1] = x[1]; initial[2] = x[2]; initial[3] = 0.0;
   initial[4] = y[0]; initial[5] = y[1]; initial[6] = y[2]; initial[7] = 0.0;
   initial[8] = z[0]; initial[9] = z[1]; initial[10]= z[2]; initial[11]= 0.0;
   initial[12]= 0.0;  initial[13]= 0.0;  initial[14]= 0.0;  initial[15]= 1.0;


   /* Translate Eye to Origin */
   //glTranslatef(-eyex, -eyey, -eyez);
   float translation[16];
   create4x4FTranslationMatrix(translation , -eyex, -eyey, -eyez );
   multiplyTwo4x4FMatrices(matrix , initial , translation);

}
Esempio n. 4
0
int simpleRendererRender(
                         struct simpleRenderer * sr ,
                         float * position3D,
                         float * center3D,
                         float * objectRotation,
                         unsigned int rotationOrder,
                         ///---------------
                         float * output3DX,
                         float * output3DY,
                         float * output3DZ,
                         ///---------------
                         float * output2DX,
                         float * output2DY,
                         float * output2DW
                        )
{
 double modelTransformationD[16];
 float  modelTransformationF[16];
 ///--------------------------------------------------------------------
 ///                       CAMERA MATRICES ETC
 ///--------------------------------------------------------------------
 create4x4ModelTransformation(
                              modelTransformationD,
                              //Rotation Component
                              (double) sr->cameraOffsetRotation[0],//heading,
                              (double) sr->cameraOffsetRotation[1],//pitch,
                              (double) sr->cameraOffsetRotation[2],//roll,
                              ROTATION_ORDER_RPY,
                              //Translation Component
                              (double) sr->cameraOffsetPosition[0],
                              (double) sr->cameraOffsetPosition[1],
                              (double) sr->cameraOffsetPosition[2],
                              //Scale Component
                              (double) 1.0,
                              (double) 1.0,
                              (double) 1.0
                             );
 ///--------------------------------------------------------------------
 copy4x4DMatrixToF(modelTransformationF,modelTransformationD);
 multiplyTwo4x4FMatrices(sr->modelViewMatrix,sr->viewMatrix,modelTransformationF);
 ///--------------------------------------------------------------------





 ///--------------------------------------------------------------------
 ///                       OBJECT MATRICES ETC
 ///--------------------------------------------------------------------
  double objectMatrixRotation[16];

  if (objectRotation==0)
    {
      create4x4IdentityMatrix(objectMatrixRotation);
    } else
  if ( (objectRotation[0]==0) && (objectRotation[1]==0) && (objectRotation[2]==0) )
    {
      create4x4IdentityMatrix(objectMatrixRotation);
    } else
  if (rotationOrder==ROTATION_ORDER_RPY)
    {
     //This is the old way to do this rotation
     doRPYTransformation(
                         objectMatrixRotation,
                         (double) objectRotation[0],
                         (double) objectRotation[1],
                         (double) objectRotation[2]
                        );
    } else
    {
     //fprintf(stderr,"Using new model transform code\n");
     create4x4MatrixFromEulerAnglesWithRotationOrder(
                                                     objectMatrixRotation ,
                                                     (double) objectRotation[0],
                                                     (double) objectRotation[1],
                                                     (double) objectRotation[2],
                                                     rotationOrder
                                                    );
    }



   double point3D[4];
   double resultPoint3D[4];


  point3D[0]=(double) (position3D[0]-center3D[0]);
  point3D[1]=(double) (position3D[1]-center3D[1]);
  point3D[2]=(double) (position3D[2]-center3D[2]);
  point3D[3]=(double) (1.0);


  transform3DPointVectorUsing4x4Matrix(
                                       resultPoint3D,
                                       objectMatrixRotation,
                                       point3D
                                      );


  float final3DPosition[4];

  if (sr->removeObjectPosition)
   {
    final3DPosition[0]=(float) resultPoint3D[0]+sr->cameraOffsetPosition[0];
    final3DPosition[1]=(float) resultPoint3D[1]+sr->cameraOffsetPosition[1];
    final3DPosition[2]=(float) resultPoint3D[2]+sr->cameraOffsetPosition[2];
   } else
   {
    final3DPosition[0]=(float) resultPoint3D[0]+center3D[0]+sr->cameraOffsetPosition[0];
    final3DPosition[1]=(float) resultPoint3D[1]+center3D[1]+sr->cameraOffsetPosition[1];
    final3DPosition[2]=(float) resultPoint3D[2]+center3D[2]+sr->cameraOffsetPosition[2];
   }
  final3DPosition[3]=(float) 0.0;//resultPoint3D[3];
 ///--------------------------------------------------------------------




 ///--------------------------------------------------------------------
 ///                         FINAL PROJECTION
 ///--------------------------------------------------------------------
  float windowCoordinates[3]={0};

  if (
       !_glhProjectf(
                     final3DPosition,
                     sr->modelViewMatrix,
                     sr->projectionMatrix,
                     sr->viewport,
                     windowCoordinates
                    )
     )
     { fprintf(stderr,"Could not project 3D Point (%0.2f,%0.2f,%0.2f)\n",final3DPosition[0],final3DPosition[1],final3DPosition[2]); }
 ///--------------------------------------------------------------------
  *output2DX = windowCoordinates[0];//windowCoordinates[2];
  *output2DY = windowCoordinates[1];//windowCoordinates[2];
  *output2DW = windowCoordinates[2];
  return 1;
}