Retrieving 3D position by colors

Recommended Posts

WiredCat    1452
Hello, i want to get 3D position from screen, I do as follows:

-draw primitive then i get cursor position and i check if i hit the face (if i hit i call RECOVER_3D_COORDINATES function)
what exactly i do in this function:

clear screen
draw primitive the face is drawn two times: first from LEFT to right (left side is black and blends to right in red)
then i pick up red color value from mouse point and calculate UX

second is that i draw face from up to down from red to black

then i pick up red color value from mouse point and calculate UY

i know where are vertices so i can calculate the range (vectors) from first vertex to clicked point described as UX,UY
model->VBO_V[vbo_i+2], - this is the first vertex (top left)
model->VBO_V[vbo_i+3], this is top right vertex
model->VBO_V[vbo_i] this is bottom right
model->VBO_V[vbo_i+1], is bottom left vert

then i make vectors from first vertex to top right vertex (rangeX)
and i make vector from first vertex to bottom left vertex (rangeY)

then i multiple them by UX and UY and then add those two vectors to first vertex to get coordinate

THE PROBLEM IS when i approach closely to face (in example few kilometers) and i click to show where clicked in 3d space the GREEN SPHERE APPEARS IN DIFFERENT POSITION: shown on this vid:

i have set shade model to GL_SMOOTH and there is no fog and lighting it should work but it does not =0

[CODE]

t3dpoint __fastcall T3DPlanet::RECOVER_3D_COORDINATES(TachoGLModel * model,
int X, int Y, int FACE_INDEX)
{

//first vertex is in upper left corner (second is in upper right corner)

int result = -1;
int i;
int vbo_i; vbo_i = model->VBO_BE[ FACE_INDEX ].INDEX_START;
float UX, UY; int kr,kg,kb,ka;

glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);
glPushMatrix();

gluLookAt(EYEPOS.x,EYEPOS.y,EYEPOS.z,
EYEPOS.x+EYELOOKATPOINT.x,
EYEPOS.y-EYELOOKATPOINT.y,
EYEPOS.z-EYELOOKATPOINT.z,
0,1,0);

//glTranslatef(model->translacja.x,model->translacja.y,model->translacja.z);
// glRotatef(rlongitude,0,1,0);
// glRotatef(rlatitude,0,0,1);

glColor3f(0.0,0.0,0.0);
glVertex3f(model->VBO_V[vbo_i+2].x,model->VBO_V[vbo_i+2].y,model->VBO_V[vbo_i+2].z);

glColor3f(1.0,0.0,0.0);
glVertex3f(model->VBO_V[vbo_i+3].x,model->VBO_V[vbo_i+3].y,model->VBO_V[vbo_i+3].z);

glColor3f(1.0,0.0,0.0);
glVertex3f(model->VBO_V[vbo_i].x,model->VBO_V[vbo_i].y,model->VBO_V[vbo_i].z);

glColor3f(0.0,0.0,0.0);
glVertex3f(model->VBO_V[vbo_i+1].x,model->VBO_V[vbo_i+1].y,model->VBO_V[vbo_i+1].z);

glEnd();

UX = (float(kr) / 255.0f);

glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);

glColor3f(1.0,0.0,0.0);
glVertex3f(model->VBO_V[vbo_i+2].x,model->VBO_V[vbo_i+2].y,model->VBO_V[vbo_i+2].z);

glColor3f(1.0,0.0,0.0);
glVertex3f(model->VBO_V[vbo_i+3].x,model->VBO_V[vbo_i+3].y,model->VBO_V[vbo_i+3].z);

glColor3f(0.0,0.0,0.0);
glVertex3f(model->VBO_V[vbo_i].x,model->VBO_V[vbo_i].y,model->VBO_V[vbo_i].z);

glColor3f(0.0,0.0,0.0);
glVertex3f(model->VBO_V[vbo_i+1].x,model->VBO_V[vbo_i+1].y,model->VBO_V[vbo_i+1].z);
glEnd();

UY = 1.0f - (float(kr) / 255.0f);

glPopMatrix();

t3dpoint rangeX; t3dpoint rangeY;

t3dpoint tx; t3dpoint ty;

rangeX = vectorAB(model->VBO_V[vbo_i+2], model->VBO_V[vbo_i+3]);
rangeY = vectorAB(model->VBO_V[vbo_i+2], model->VBO_V[vbo_i+1]);

rangeX = vector_multiple(rangeX, UX);
rangeY = vector_multiple(rangeY, UY);

t3dpoint p;
p = model->VBO_V[vbo_i+2];

return p;

}
[/CODE]

and i have similar code that works fine: but this code is for something else

[CODE]
//panel verts
//X1----X2
//
//
//X4----X3
void __fastcall TInteractiveDisplayPanel_missle_lock_system::OnClickEvent(int X, int Y,int wholescreenheight, int wholescreenwidth, float glopa, float headinga, t3dpoint olda)
{
FoundObject = -1;
glPushMatrix();
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);

gluLookAt(olda.x,olda.y,olda.z,olda.x+SUPERPOINT.x,
olda.y-SUPERPOINT.y,olda.z-SUPERPOINT.z,
0.0f,1.0f,0.0f);
int R;
int G;
int B;
int A;
// PanelModel->DrawSimpleModel();
glColor3f(0.0,1.0,0.0);
glVertex3f(PanelModel->VBO_V[0].x,PanelModel->VBO_V[0].y,PanelModel->VBO_V[0].z);
glVertex3f(PanelModel->VBO_V[1].x,PanelModel->VBO_V[1].y,PanelModel->VBO_V[1].z);
glVertex3f(PanelModel->VBO_V[2].x,PanelModel->VBO_V[2].y,PanelModel->VBO_V[2].z);
glVertex3f(PanelModel->VBO_V[3].x,PanelModel->VBO_V[3].y,PanelModel->VBO_V[3].z);
glEnd();
glColor3f(1.0,1.0,1.0);
// ZAPISZ_WIDOK_DO_PLIKU_BITMAPY("E:\ p1.bmp",wholescreenwidth,wholescreenheight);
if ( (R == 0) && (G == 255) && (B == 0) ) { //clicked at panel
glColor3f(1.0,0.0,0.0);
glVertex3f(PanelModel->VBO_V[0].x,PanelModel->VBO_V[0].y,PanelModel->VBO_V[0].z);
glColor3f(1.0,0.0,0.0);
glVertex3f(PanelModel->VBO_V[1].x,PanelModel->VBO_V[1].y,PanelModel->VBO_V[1].z);
glColor3f(0.0,0.0,0.0);
glVertex3f(PanelModel->VBO_V[2].x,PanelModel->VBO_V[2].y,PanelModel->VBO_V[2].z);
glColor3f(0.0,0.0,0.0);
glVertex3f(PanelModel->VBO_V[3].x,PanelModel->VBO_V[3].y,PanelModel->VBO_V[3].z);
glEnd();
// ZAPISZ_WIDOK_DO_PLIKU_BITMAPY("E:\ p2.bmp",wholescreenwidth,wholescreenheight);
//float supY = wholescreenheight * float(R)/255.0f;
PanelCurY = 240.0f * (float(R) / 255.0f);
glDisable(GL_DEPTH_TEST);

glColor3f(0.0,0.0,0.0);
glVertex3f(PanelModel->VBO_V[0].x,PanelModel->VBO_V[0].y,PanelModel->VBO_V[0].z);
glColor3f(1.0,0.0,0.0);
glVertex3f(PanelModel->VBO_V[1].x,PanelModel->VBO_V[1].y,PanelModel->VBO_V[1].z);
glColor3f(1.0,0.0,0.0);
glVertex3f(PanelModel->VBO_V[2].x,PanelModel->VBO_V[2].y,PanelModel->VBO_V[2].z);
glColor3f(0.0,0.0,0.0);
glVertex3f(PanelModel->VBO_V[3].x,PanelModel->VBO_V[3].y,PanelModel->VBO_V[3].z);
glEnd();
// ZAPISZ_WIDOK_DO_PLIKU_BITMAPY("E:\ p3.bmp",wholescreenwidth,wholescreenheight);

//float supX = wholescreenwidth * float(R)/255.0f;
glEnable(GL_DEPTH_TEST);
PanelCurX = 320.0f * (float(R) / 255.0f);
}//if we hit teh panel
glPopMatrix();

}

[/CODE]

AHH and one more thing please do not reply that i should use gluUnProject because it does not work for me Edited by ___