Sign in to follow this  

Retrieving 3D position by colors

This topic is 2050 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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)
[img]https://sites.google.com/site/imoomohomozumo/basic-3d-collision-detection/home/COORD_RET_X.jpg[/img]
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

[img]https://sites.google.com/site/imoomohomozumo/basic-3d-collision-detection/home/COORD_RET_Y.jpg[/img]

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:

[media]http://www.youtube.com/watch?v=eqxPZkSYgi4&feature=youtu.be[/media]

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();

glLoadIdentity();
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);


glBegin(GL_QUADS); // Get X coordinate
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();

READ_RGBA_FROM_OGL_WINDOW(X,DISPLAY_HEIGHT,Y,kr,kg,kb,ka);
UX = (float(kr) / 255.0f);


glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);


glBegin(GL_QUADS); // Get Y coordinate
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();

READ_RGBA_FROM_OGL_WINDOW(X,DISPLAY_HEIGHT,Y,kr,kg,kb,ka);
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];

p = vectors_add(p, rangeX);
p = vectors_add(p, rangeY);
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();
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);

SUPERPOINT.x = 100000.0f*(sin(glopa*imopi)*cos(headinga*imopi));
SUPERPOINT.z = 100000.0f*(cos(glopa*imopi)*cos(headinga*imopi));
SUPERPOINT.y = 100000.0f*(sin(headinga*imopi));


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);
glBegin(GL_QUADS);
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);
READ_RGBA_FROM_OGL_WINDOW(X,wholescreenheight,Y,R,G,B,A);
if ( (R == 0) && (G == 255) && (B == 0) ) { //clicked at panel
glBegin(GL_QUADS); // Get Y coordinate
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);
READ_RGBA_FROM_OGL_WINDOW(X,wholescreenheight,Y,R,G,B,A);
//float supY = wholescreenheight * float(R)/255.0f;
PanelCurY = 240.0f * (float(R) / 255.0f);
glDisable(GL_DEPTH_TEST);

glBegin(GL_QUADS); // Get X coordinate
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);
READ_RGBA_FROM_OGL_WINDOW(X,wholescreenheight,Y,R,G,B,A);

//float supX = wholescreenwidth * float(R)/255.0f;
glEnable(GL_DEPTH_TEST);
PanelCurX = 320.0f * (float(R) / 255.0f);
FoundObject = OpticalRecognitionTHREAD->FindObjectByCoords(PanelCurX,PanelCurY);
}//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 ___

Share this post


Link to post
Share on other sites

This topic is 2050 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this