Hi,
I’m very new to OpenGL and for various reasons I’m rolling my own functions to calculate the various parts of my MVP matrix (described here). Anyway the code based off of them doesn’t work (naturally…) and i would greatly appreciate if someone with a few minutes could take a look at them and check whether they’re at fault, since I can’t find a good way to isolate them from the rest of the code, so can’t tell where the error is.
Thanks in advance
Angus
void perspective(float fovy, float aspect, float zNear, float zFar, float result[4][4])
{
float f=1/tanf(fovy/2);
result[0][0]=f/aspect;
result[0][1]=0;
result[0][2]=0;
result[0][3]=0;
result[1][0]=0;
result[1][1]=f;
result[1][2]=0;
result[1][3]=0;
result[2][0]=0;
result[2][1]=0;
result[2][2]=(zFar+zNear)/(zNear-zFar);
result[2][3]=(2*zNear*zFar)/(zNear-zFar);
result[3][0]=0;
result[3][1]=0;
result[3][2]=-1;
result[3][3]=0;
}
void crossproduct(float a[4], float b[4], float result[4]);
void lookat(float eyeX, float eyeY, float eyeZ, float centreX, float centreY, float centreZ, float upX, float upY, float upZ, float result[4][4])
{
float f[4] = {centreX-eyeX, centreY-eyeY, centreZ-eyeZ, 1};
float u[4] = {upX, upY, upZ, 1};
float s[4];
float temp[4][4];
float temp2[4][4];
normalise(f);
normalise(u);
crossproduct(f, u, s);
crossproduct(s, f, u);
temp[0][0]=s[0];
temp[0][1]=s[1];
temp[0][2]=s[2];
temp[0][3]=0;
temp[1][0]=u[0];
temp[1][1]=u[1];
temp[1][2]=u[2];
temp[1][3]=0;
temp[2][0]=-f[0];
temp[2][1]=-f[1];
temp[2][2]=-f[2];
temp[2][3]=0;
temp[3][0]=0;
temp[3][1]=0;
temp[3][2]=0;
temp[3][3]=1;
translate(temp2, -eyeX, -eyeY, -eyeZ);
multmatrix(temp, temp2, result);
}
void crossproduct(float a[4], float b[4], float result[4])
{
result[0] = (a[1]*b[2]) - (a[2] * b[1]);
result[1] = (a[2]*b[0]) - (a[0] * b[2]);
result[2] = (a[0]*b[1]) - (a[1] * b[0]);
result[3] = 1;
}
void multmatrix(float a[4][4], float b[4][4], float result[4][4])
{
int i;
int j;
int k;
float total;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
total=0;
for(k=0;k<4;k++)
total+=a[i][k]*b[k][j];
result[i][j]=total;
}
}
void translate(float result[4][4], float x, float y, float z)
{
result[0][0]=1;
result[0][1]=0;
result[0][2]=0;
result[0][3]=x;
result[1][0]=0;
result[1][1]=1;
result[1][2]=0;
result[1][3]=y;
result[2][0]=0;
result[2][1]=0;
result[2][2]=1;
result[2][3]=z;
result[3][0]=0;
result[3][1]=0;
result[3][2]=0;
result[3][3]=1;
}