예제 #1
0
 int GLUtility::unProject(float winx, float winy, float winz, float *modelview, float *projection, int *viewport, float *objectCoordinate)
 {
     //Transformation matrices
     float m[16], A[16];
     float in[4], out[4];
     //Calculation for inverting a matrix, compute projection x modelview
     //and store in A[16]
     MultiplyMatrices4by4OpenGL_FLOAT(A, projection, modelview);
     //Now compute the inverse of matrix A
     if(glhInvertMatrixf2(A, m)==0)
        return 0;
     //Transformation of normalized coordinates between -1 and 1
     in[0]=(winx-(float)viewport[0])/(float)viewport[2]*2.0-1.0;
     in[1]=(winy-(float)viewport[1])/(float)viewport[3]*2.0-1.0;
     in[2]=2.0*winz-1.0;
     in[3]=1.0;
     //Objects coordinates
     MultiplyMatrixByVector4by4OpenGL_FLOAT(out, m, in);
     if(out[3]==0.0)
        return 0;
     out[3]=1.0/out[3];
     objectCoordinate[0]=out[0]*out[3];
     objectCoordinate[1]=out[1]*out[3];
     objectCoordinate[2]=out[2]*out[3];
     return 1;
 }
예제 #2
0
파일: DGL.cpp 프로젝트: gibyfred/Starbird
static void glhLookAtf2( float *matrix, float *eyePosition3D,
                  float *center3D, float *upVector3D )
{
#define ComputeNormalOfPlane ComputeNormalOfPlaneFLOAT_2
#define NormalizeVector NormalizeVectorFLOAT_2

#ifdef BUILD_USE_GLHLIB
	float resultMatrix[16];
#endif

	float forward[3], side[3], up[3];
	float matrix2[16];
	//------------------
	forward[0] = center3D[0] - eyePosition3D[0];
	forward[1] = center3D[1] - eyePosition3D[1];
	forward[2] = center3D[2] - eyePosition3D[2];
	NormalizeVector(forward);
	//------------------
	//Side = forward x up
	ComputeNormalOfPlane(side, forward, upVector3D);
	NormalizeVector(side);
	//------------------
	//Recompute up as: up = side x forward
	ComputeNormalOfPlane(up, side, forward);
	//------------------
	matrix2[0] = side[0];
	matrix2[4] = side[1];
	matrix2[8] = side[2];
	matrix2[12] = 0.0;
	//------------------
	matrix2[1] = up[0];
	matrix2[5] = up[1];
	matrix2[9] = up[2];
	matrix2[13] = 0.0;
	//------------------
	matrix2[2] = -forward[0];
	matrix2[6] = -forward[1];
	matrix2[10] = -forward[2];
	matrix2[14] = 0.0;
	//------------------
	matrix2[3] = matrix2[7] = matrix2[11] = 0.0;
	matrix2[15] = 1.0;
	//------------------
#ifdef BUILD_USE_GLHLIB
	MultiplyMatrices4by4OpenGL_FLOAT(resultMatrix, matrix, matrix2);
	glhTranslatef2(resultMatrix,
				-eyePosition3D[0], -eyePosition3D[1], -eyePosition3D[2]);
	//------------------
	memcpy(matrix, resultMatrix, 16*sizeof(float));
#else
	glMultMatrixf(matrix2);
	glTranslatef( -eyePosition3D[0], -eyePosition3D[1], -eyePosition3D[2] );
#endif
}