2011-12-13 3 views
-2

사용자가 마우스로 클릭 한 다음 사용자가 사각형을 그려서 프로그램을 작성하면 사용자가 위, 아래, 왼쪽 및 오른쪽으로 이동할 수 있습니다. 사용자가 객체를 이동할 때의 문제는 hide입니다.이동했을 때보기에서 삼각형이 사라짐

누구든 나를 도울 수 있습니다.

이 코드의 샘플입니다

#include <GL/glut.h>  
    #include<iostream.h> 

    #define ORTHO_SCALE 10. 

    GLint triangle_vertices[] = {15, 20,30, 20,10, 15}; 
    GLint triangle_vertices2[] = {50, 70,80, 90,100, 130}; 

    struct { 
     struct { 
      struct { 
       GLfloat x, y; 
      } pos; 
      GLfloat rot; 
     } triangle; 
    } sceneinfo; 
    void clearScreen() 
    { 

      glutSwapBuffers(); 

     glColor3f (1.0, 1.0, 1.0); 

     int ver[]={38,0,1143,0,1143,800,38,1105}; 
     glPushMatrix(); 
     glEnableClientState(GL_VERTEX_ARRAY); 


     glVertexPointer(2, GL_INT, 0, ver); 
     glDrawArrays(GL_QUADS, 0, 4); 
     glDisableClientState(GL_VERTEX_ARRAY); 
     glPopMatrix(); 


      glutSwapBuffers(); 

    } 
    void mouse(int btn, int state, int x, int y) 
    { 
     if(btn==GLUT_RIGHT_BUTTON && state==GLUT_DOWN) 
      exit(0); 
     if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN) 
     { 
      glutSwapBuffers(); 

      glPushMatrix(); 



      glTranslatef(sceneinfo.triangle.pos.x, sceneinfo.triangle.pos.y, 0); 



      glEnableClientState(GL_VERTEX_ARRAY); 
      glVertexPointer(2, GL_INT, 0, triangle_vertices2); 
      glDrawArrays(GL_TRIANGLES, 0, 3); 

      glPopMatrix(); 

      glutSwapBuffers(); 
     } 

    } 
    void display(void); 
    void special(int key, int x, int y); 
    void init (void) 
    { 
     glClearColor (1.0, 1.0, 1.0, 0.0); // Set display-window color to white. 

     glMatrixMode (GL_PROJECTION);  // Set projection parameters. 

     gluOrtho2D (0.0, 200.0, 0.0, 150.0); 
    } 
    int main(int argc, char *argv[]) 
    { 
     glutInit(&argc, argv); 
     glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE); 

     glutCreateWindow("test"); 

     init(); 

     glutDisplayFunc(display); 
     glutSpecialFunc(special); 
     glutMouseFunc(mouse); 
     glutMainLoop(); 

     return 0; 
    } 

    void display(void) 
    { 

     glColor3f(0.0, 0.0, 0.0);//set color of object 


     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 



     glPushMatrix(); 



     glTranslatef(sceneinfo.triangle.pos.x, sceneinfo.triangle.pos.y, 0.); 

     glEnableClientState(GL_VERTEX_ARRAY); 
     glVertexPointer(2, GL_INT, 0, triangle_vertices); 
     glDrawArrays(GL_TRIANGLES, 0, 3); 

     glPopMatrix(); 

     glutSwapBuffers(); 
    } 


    void special(int key, int x, int y) 
    { 
     switch(key) { 
     case GLUT_KEY_LEFT: 
      sceneinfo.triangle.pos.x -= 0.2; 
      break; 
     case GLUT_KEY_RIGHT: 
      sceneinfo.triangle.pos.x += 0.2; 
      break; 
     case GLUT_KEY_UP: 
      sceneinfo.triangle.pos.y += 0.2; 
      break; 
     case GLUT_KEY_DOWN: 
      sceneinfo.triangle.pos.y -= 0.2; 
      break; 
     } 

    glutPostRedisplay(); 


} 

답변

1

나는 과잉을 사용하지 않는했지만 내가이 잠재적 인 문제를 볼 수 있습니다 당신은 아마 당신의 mouse() 기능에 그리기해서는 안

  1. 합니다. 또한 여기에 버퍼를 두 번 바꿔서 원하는 결과를 얻을 수 없습니다.

  2. 처음부터 렌더링에 영향을 미치지 만 어느 곳에서나 sceneinfo을 초기화하지 않는 것처럼 보입니다.

다음 단계 : sceneinfo를 초기화한다 그래서 당신은 유효한 값을 포함 알고 마우스() 함수에서 드로잉 코드를 제거합니다. 문제가 계속되는 경우 디버거를 사용하여 (또는 최악의 경우 로그에 기록하십시오) sceneinfo.triangle.pos의 상황을 확인하십시오. 키보드 입력이 어떻게 처리되는지에 따라 예상보다 훨씬 빠르게 커질 수 있습니다.

+0

sceneinfo.triangle.pos에 문제가 없습니다. 내 문제 어떻게 새 위치에있는 모든 객체를 다시 그릴 수 있습니까? – Developer

+0

나는 단지 여러분이 버퍼를 바꾸고'clearscreen()'을 그리는 것을 보았을 뿐이므로 그것도 제거해야한다. 렌더링 프로세스에서 사용되는 깊이, 렌더링 및 기타 버퍼를 지울 때만 사용해야합니다. –

+0

도와 주셔서 감사합니다. – Developer