Skip to main content
GameDev.net gamedev.net
🔒 Locked

gluProject/gluUnProject problems.

Started by slicer4ever Jan 8, 2012 at 12:21 AM 0 replies 1.4k views
Original Post
slicer4ever
slicer4ever
hello all, i'm having a couple of problems with gluProject/gluUnProject.

basically, i'm attempting to implement my own versions, and am following the exact documentation on the openGL websiste:

http://www.opengl.or.../gluProject.xml

http://www.opengl.or...luUnProject.xml

to which i've come out with this:



void NVGfxUnProject(float WinX, float WinY, float WinZ, float *ModelMatrix, float *ProjMatrix, NVWindow *Wnd, float *ObjX, float *ObjY, float *ObjZ){
float a[16], i[3];
NVMatrix4x4Multi(ProjMatrix, ModelMatrix, a);
NVMatrix4x4Invert(a, a);
i[0] = (WinX*2.0f)/(float)Wnd->GetWidth()-1.0f;
i[1] = (WinY*2.0f)/(float)Wnd->GetHeight()-1.0f;
i[2] = WinZ*2.0f-1.0f;
*ObjX=(a[0x0]*i[0]+a[0x4]*i[1]+a[0x8]*i[2]+a[0xC]);
*ObjY=(a[0x1]*i[0]+a[0x5]*i[1]+a[0x9]*i[2]+a[0xD]);
*ObjZ=(a[0x2]*i[0]+a[0x6]*i[1]+a[0xA]*i[2]+a[0xE]);
return;
}
void NVGfxProject(float ObjX, float ObjY, float ObjZ, float *ModelMatrix, float *ProjMatrix, NVWindow *Wnd, float *WinX, float *WinY, float *WinZ){
float m[16], temp[8];
NVMatrix4x4Multi(ProjMatrix, ModelMatrix, m);
temp[0] = m[0x0]*ObjX+m[0x4]*ObjY+m[0x8]*ObjZ+m[0xC];
temp[1] = m[0x1]*ObjX+m[0x5]*ObjY+m[0x9]*ObjZ+m[0xD];
temp[2] = m[0x2]*ObjX+m[0x6]*ObjY+m[0xA]*ObjZ+m[0xE];
temp[3] = m[0x3]*ObjX+m[0x7]*ObjY+m[0xB]*ObjZ+m[0xF];
if(temp[3]==0.0f) return;
temp[0]/=temp[3];
temp[1]/=temp[3];
temp[2]/=temp[3];
*WinX = (Wnd->GetWidthf()*(temp[0]+1.0f))/2.0f;
*WinY = (Wnd->GetHeightf()*(temp[1]+1.0f))/2.0f;
*WinZ = (temp[2]+1.0f)/2.0f;
}


however, neither version works, so i looked up mesa's to see what i've done wrong, and i came up with this:

void NVGfxUnProject(float WinX, float WinY, float WinZ, float *ModelMatrix, float *ProjMatrix, NVWindow *Wnd, float *ObjX, float *ObjY, float *ObjZ){
float a[16], i[4];
NVMatrix4x4Invert(ModelMatrix, a);
NVMatrix4x4Multi(a, ProjMatrix, a);
NVMatrix4x4Invert(a, a);
i[0] = WinX/(float)Wnd->GetWidth()*2.0f-1.0f;
i[1] = WinY/(float)Wnd->GetHeight()*2.0f-1.0f;
i[2] = WinZ*2.0f-1.0f;
i[3] = 1.0f;
float w=a[0x3]*i[0]+a[0x7]*i[1]+a[0xB]*i[2]+a[0xF];
if(w==0.0f) return;
w=1.0f/w;
*ObjX=(a[0x0]*i[0]+a[0x4]*i[1]+a[0x8]*i[2]+a[0xC])*w;
*ObjY=(a[0x1]*i[0]+a[0x5]*i[1]+a[0x9]*i[2]+a[0xD])*w;
*ObjZ=(a[0x2]*i[0]+a[0x6]*i[1]+a[0xA]*i[2]+a[0xE])*w;
return;
}

void NVGfxProject(float ObjX, float ObjY, float ObjZ, float *ModelMatrix, float *ProjMatrix, NVWindow *Wnd, float *WinX, float *WinY, float *WinZ){
float temp[8];
temp[0] = ModelMatrix[0x0]*ObjX+ModelMatrix[0x4]*ObjY+ModelMatrix[0x8]*ObjZ+ModelMatrix[0xC];
temp[1] = ModelMatrix[0x1]*ObjX+ModelMatrix[0x5]*ObjY+ModelMatrix[0x9]*ObjZ+ModelMatrix[0xD];
temp[2] = ModelMatrix[0x2]*ObjX+ModelMatrix[0x6]*ObjY+ModelMatrix[0xA]*ObjZ+ModelMatrix[0xE];
temp[3] = ModelMatrix[0x3]*ObjX+ModelMatrix[0x7]*ObjY+ModelMatrix[0xB]*ObjZ+ModelMatrix[0xF];
temp[4] = ProjMatrix[0x0]*temp[0]+ProjMatrix[0x4]*temp[1]+ProjMatrix[0x8]*temp[2]+ProjMatrix[0xC]*temp[3];
temp[5] = ProjMatrix[0x1]*temp[0]+ProjMatrix[0x5]*temp[1]+ProjMatrix[0x9]*temp[2]+ProjMatrix[0xD]*temp[3];
temp[6] = ProjMatrix[0x2]*temp[0]+ProjMatrix[0x6]*temp[1]+ProjMatrix[0xA]*temp[2]+ProjMatrix[0xE]*temp[3];
temp[7] = ProjMatrix[0x3]*temp[0]+ProjMatrix[0x7]*temp[1]+ProjMatrix[0xB]*temp[2]+ProjMatrix[0xF]*temp[3];
if(temp[7]==0.0f) return;
temp[4]/=temp[7];
temp[5]/=temp[7];
temp[6]/=temp[7];
*WinX = (temp[4]*0.5f+0.5f)*Wnd->GetWidthf();
*WinY = (temp[5]*0.5f+0.5f)*Wnd->GetHeightf();
*WinZ = (temp[6]*0.5f+0.5f);
return;
}


so, first of all, why when following the documentation provided by openGL does my two functions not work?
secondly, my NVGfxProject with mesa's code, does not seem to work correctly.

for example, if i feed object space coordinates into it, then take the results, and feed that into NVGfxUnProject, i do not return the same results as what i initially feed into NVGfxProject.

edit: nvm, i've discovered my issue, it has to do with my game engine, i'm inverting the view matrix before i draw anything, and i have to invert my model matrix before i submit it to my nvGfxUnProject/Project functions.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.