我已經用C和freeglut編碼了OpenGl Super Bible的例子https://github.com/openglsuperbible/sb7code/blob/master/src/grass/grass.cpp 。結果一直是這些觀點之外的觀點。我是否正確創建外觀和透視矩陣?
我是否正確計算並使用外觀和透視矩陣?渲染功能的
部分:
Matrix proj=CreateProjectionMatrix(45.0f,CurrentWidth/CurrentHeight,0.1f,1000.0f);
float t=glutGet(GLUT_ELAPSED_TIME)*0.002f;
float eye[]={sinf(t)*550.0f,25.0f,cosf(t)*550.0f};
float center[]={0.0f,-50.0f,0.0f};
float up[]={0.0f,1.0f,0.0f};
Matrix look=lookAt(eye,center,up);
Matrix final=MultiplyMatrices(&proj,&look);
glUniformMatrix4fv(UniformsMvpMatrix,1,GL_FALSE,final.m);
計算:
typedef struct Matrix{
GLfloat m[16];
}Matrix;
Matrix CreateProjectionMatrix(float fovy,float aspect,float Znear,float Zfar){
// Array index in the matrix
// _ _
// | 0 4 8 12 |
// | 1 5 9 13 |
// | 2 6 10 14 |
// |_3 7 11 15_|
//
Matrix out;
float tanhalffovy = tanf(DegreesToRadians(fovy/2));
out.m[ 0]=1/(aspect*tanhalffovy);
out.m[ 5]=1/(tanhalffovy);
out.m[10]=(Zfar+Znear)/(Znear-Zfar);
out.m[11]=(2*Zfar*Znear)/(Znear-Zfar);
out.m[14]=-1;
out.m[ 1]=0;
out.m[ 2]=0;
out.m[ 3]=0;
out.m[ 4]=0;
out.m[ 6]=0;
out.m[ 7]=0;
out.m[ 8]=0;
out.m[ 9]=0;
out.m[12]=0;
out.m[13]=0;
out.m[15]=0;
return out;
}
Matrix lookAt(float eye[3],float center[3],float up[3]){
// Array index in the matrix
// _ _
// | 0 4 8 12 |
// | 1 5 9 13 |
// | 2 6 10 14 |
// |_3 7 11 15_|
//
double help=sqrt(up[0]*up[0]+up[1]*up[1]+up[2]*up[2]);
float upN[3];
upN[0]=up[0]/help;
upN[1]=up[1]/help;
upN[2]=up[2]/help;
float f[3];
f[0]=(center[0]-eye[0]);
f[1]=(center[1]-eye[1]);
f[2]=(center[2]-eye[2]);
help=sqrt(f[0]*f[0]+f[1]*f[1]+f[2]*f[2]);
f[0]/=help;
f[1]/=help;
f[2]/=help;
float s[3];
s[0]=f[1]*upN[2]-upN[1]*f[2];
s[1]=f[2]*upN[0]-upN[2]*f[0];
s[2]=f[0]*upN[1]-upN[0]*f[1];
float u[3];
u[0]=s[1]*f[2]-f[1]*s[2];
u[1]=s[2]*f[0]-f[2]*s[0];
u[2]=s[0]*f[1]-f[0]*s[1];
Matrix out={{s[0],s[1],s[2],0, u[0],u[1],u[2],0, f[0],f[1],f[2],0, -eye[0],-eye[1],-eye[2],1}};
return out;
}
Matrix MultiplyMatrices(const Matrix* m1, const Matrix* m2){
Matrix out;
// First column
out.m[ 0]=(m1->m[0])*(m2->m[0])+(m1->m[4])*(m2->m[1])+(m1->m[ 8])*(m2->m[2])+(m1->m[12])*(m2->m[3]);
out.m[ 1]=(m1->m[1])*(m2->m[0])+(m1->m[5])*(m2->m[1])+(m1->m[ 9])*(m2->m[2])+(m1->m[13])*(m2->m[3]);
out.m[ 2]=(m1->m[2])*(m2->m[0])+(m1->m[6])*(m2->m[1])+(m1->m[10])*(m2->m[2])+(m1->m[14])*(m2->m[3]);
out.m[ 3]=(m1->m[3])*(m2->m[0])+(m1->m[7])*(m2->m[1])+(m1->m[11])*(m2->m[2])+(m1->m[15])*(m2->m[3]);
// Second column
out.m[ 4]=(m1->m[0])*(m2->m[4])+(m1->m[4])*(m2->m[5])+(m1->m[ 8])*(m2->m[6])+(m1->m[12])*(m2->m[7]);
out.m[ 5]=(m1->m[1])*(m2->m[4])+(m1->m[5])*(m2->m[5])+(m1->m[ 9])*(m2->m[6])+(m1->m[13])*(m2->m[7]);
out.m[ 6]=(m1->m[2])*(m2->m[4])+(m1->m[6])*(m2->m[5])+(m1->m[10])*(m2->m[6])+(m1->m[14])*(m2->m[7]);
out.m[ 7]=(m1->m[3])*(m2->m[4])+(m1->m[7])*(m2->m[5])+(m1->m[11])*(m2->m[6])+(m1->m[15])*(m2->m[7]);
// Third column
out.m[ 8]=(m1->m[0])*(m2->m[8])+(m1->m[4])*(m2->m[9])+(m1->m[ 8])*(m2->m[10])+(m1->m[12])*(m2->m[11]);
out.m[ 9]=(m1->m[1])*(m2->m[8])+(m1->m[5])*(m2->m[9])+(m1->m[ 9])*(m2->m[10])+(m1->m[13])*(m2->m[11]);
out.m[10]=(m1->m[2])*(m2->m[8])+(m1->m[6])*(m2->m[9])+(m1->m[10])*(m2->m[10])+(m1->m[14])*(m2->m[11]);
out.m[11]=(m1->m[3])*(m2->m[8])+(m1->m[7])*(m2->m[9])+(m1->m[11])*(m2->m[10])+(m1->m[15])*(m2->m[11]);
// Fourth
out.m[12]=(m1->m[0])*(m2->m[12])+(m1->m[4])*(m2->m[13])+(m1->m[ 8])*(m2->m[14])+(m1->m[12])*(m2->m[15]);
out.m[13]=(m1->m[1])*(m2->m[12])+(m1->m[5])*(m2->m[13])+(m1->m[ 9])*(m2->m[14])+(m1->m[13])*(m2->m[15]);
out.m[14]=(m1->m[2])*(m2->m[12])+(m1->m[6])*(m2->m[13])+(m1->m[10])*(m2->m[14])+(m1->m[14])*(m2->m[15]);
out.m[15]=(m1->m[3])*(m2->m[12])+(m1->m[7])*(m2->m[13])+(m1->m[11])*(m2->m[14])+(m1->m[15])*(m2->m[15]);
return out;
}
我99.9%肯定着色器相同的例子。
謝謝derhass:D – lohikaarme2