2012-04-21 4 views
-1

3D 좌표를 화면 좌표로 변환하는 데 문제가 있습니다. 구의 중심을 화면 좌표로 변환해야합니다. 내 코드는 여기에 있습니다 :OpenGL에서 3D 좌표를 화면 좌표로 변환

// OpenGL problem 

#ifdef __APPLE__ 
# include <GLUT/glut.h> 
#else 
# include <GL/glut.h> 
#endif 

void passive(int,int); 
void reshape(int,int); 
void init(void); 
void display(void); 
void camera(void); 

int cursorX,cursorY,width,height; 
double centerX,centerY,centerZ; 
//GLfloat modelviewMatrix[16],projectionMatrix[16]; 
GLdouble modelviewMatrix[16],projectionMatrix[16]; 
GLint viewport[4]; 

int main (int argc,char **argv) { 
    glutInit (&argc,argv); 
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA); 
    glutInitWindowSize(1364,689); 
    glutInitWindowPosition(0,0); 
    glutCreateWindow("Sample"); 
    init(); 
    glutDisplayFunc(display); 
    glutIdleFunc(display); 
    glutPassiveMotionFunc(passive); 
    glutReshapeFunc(reshape); 
    glutMainLoop(); 
    return 0; 
} 

void display() { 
    glClearColor (0.0,0.0,0.0,1.0); 
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

    // Render 3D content 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    gluPerspective(60,(GLfloat)width/(GLfloat)height,1.0,100.0); // create 3D perspective projection matrix 
    glMatrixMode(GL_MODELVIEW); 
    glPushMatrix(); 
    camera(); 
    glTranslatef(-6,-2,0); 
    glColor3f(1,0,0); 
    glutSolidSphere(5,50,50); 
    glPopMatrix(); 

    glGetDoublev(GL_MODELVIEW_MATRIX, modelviewMatrix); 
    glGetDoublev(GL_PROJECTION_MATRIX, projectionMatrix); 
    glGetIntegerv(GL_VIEWPORT, viewport); 
    // get 3D coordinates based on window coordinates 
    gluProject(-6, -2, 0, modelviewMatrix, projectionMatrix, viewport, &centerX, &centerY, &centerZ); 

    // Render 2D content 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    gluOrtho2D(0, width,height, 0);     // create 2D orthographic projection matrix 
    glMatrixMode(GL_MODELVIEW); 
    glColor3f(1,1,1); 
    glBegin(GL_LINES); 
     glVertex2f(centerX,centerY);   // coordinate of center of the sphere in orthographic projection 
     glVertex2f(cursorX,cursorY); 
    glEnd(); 

    glutSwapBuffers(); 
} 

void camera(void) { 
    glRotatef(0.0,1.0,0.0,0.0); 
    glRotatef(0.0,0.0,1.0,0.0); 
    glTranslated(0,0,-20); 
} 

void init(void) { 
    glEnable (GL_DEPTH_TEST); 
    glEnable (GL_BLEND); 
    glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 
    glEnable(GL_COLOR_MATERIAL); 
} 

void reshape(int w, int h) { 
    width=w; height=h; 
} 

void passive(int x1,int y1) { 
    cursorX=x1; cursorY=y1; 
} 

내 코드에서 centerX와 centerY 좌표가 화면 밖으로 나옵니다. 구의 중심과 마우스 포인터 사이에 선을 그려야합니다. 코드를 수정하면 제대로 작동합니까?

+1

어떻게 이런 일에서 어떤 다른 [질문의 해상도를 확인하기위한 내 기능 당신은 지난 달에 물었다?] (http://stackoverflow.com/questions/9926763/mapping-coordinates-from-3d-perspective-projection-to-2d-orthographic-projection) –

+0

그 스레드에 단 하나의 대답이있었습니다. 그것을 시도했지만 작동시키지 못했습니다. 해당 스레드에서 제안 된 내용을 포함하도록 코드가 수정되었습니다. 아직도 이상한 결과를 얻고 있습니다. –

+0

당신은 좋은 대답을 얻지 못했기 때문에 똑같은 질문을 계속하지 않습니다. –

답변

0

일반적으로 3D/2D 좌표간에 전환하는 데 사용하는 기능입니다. 그것이 당신을 위해 작동하는지보십시오.

void toggle2DMode(bool flag, GLint Width, GLint Height) const 
{ 
     if(flag) 
     { 
       glMatrixMode(GL_PROJECTION); 
       glPushMatrix(); 
       glLoadIdentity(); 

       glOrtho(0.0, Width, Height, 0.0, -1.0f, 1.0f); 
       glMatrixMode(GL_MODELVIEW); 
       glPushMatrix(); 
       glLoadIdentity(); 
       glDisable(GL_DEPTH_TEST); 
       glEnable(GL_BLEND); 
     } 
     else 
     { 
       glMatrixMode(GL_PROJECTION); 
       glPopMatrix(); 
       glMatrixMode(GL_MODELVIEW); 
       glPopMatrix(); 
       glEnable(GL_DEPTH_TEST); 
       glDisable(GL_BLEND); 
     } 
} 
+0

내 질문의 틀을 다시 짜게하십시오. 3D 좌표계에 점 x, y, z가 있으면 화면 좌표계에서 해당하는 x, y는 무엇입니까? –

+0

오. [gluProject] (http://www.opengl.org/sdk/docs/man/xhtml/gluProject.xml) 함수를 사용하여 간단하게 시도 했습니까? 함수 정의 : 객체 좌표를 윈도우 좌표에 매핑합니다. – Chris911

+0

위 글에서 작성한 코드에서 gluProject를 사용했습니다. 그것은 별난 결과를 준다. gluProject가 화면 밖으로 나왔던 x, y, z 값. 마우스 위치 (2D)와 구의 중심 (3D)이 같은 좌표계에 있어야합니다. 3D를 2D로 변환 할 수 없었습니다. 하지만 이제 2D 마우스 좌표를 3D 좌표계로 변환했습니다. 이것은 내 문제를 해결했다. NeHe 튜토리얼에서 코드를 선택했습니다. 내 업데이트 된 코드 : (gluUnProject를 사용하여 2D에서 3D로 적용) : [link] (http://pastebin.com/PSSkkJJV) –

0

쿼드

void WINDOW_TEXT::calcResolution(GLshort & x , GLshort & y) 
{ 
    GLdouble tx, ty, ttx, tty, z; // target 2d coords (z is 'unused') 

    GLdouble model_view[16]; 
    glGetDoublev(GL_MODELVIEW_MATRIX, model_view); 

    GLdouble projection[16]; 
    glGetDoublev(GL_PROJECTION_MATRIX, projection); 

    GLint viewport[4]; 
    glGetIntegerv(GL_VIEWPORT, viewport); 

    // get window coords based on 3D coordinates 
    gluProject(up_right.x, up_right.y, up_right.z, 
       model_view, projection, viewport, 
       &tx, &ty, &z); 

    gluProject(down_left.x, down_left.y, down_left.z, 
       model_view, projection, viewport, 
       &ttx, &tty, &z); 

    x = (GLshort)(tx-ttx); 
    y = (GLshort)(tty-ty); 
}; 

"z는"무시하고 X에 저장 순수 해상도를 얻고, Y

관련 문제