2010-03-21 3 views
1

안녕하세요 저는 C++ 용 openGl을 배우기 시작했으나 설명하는 시점에 갇혀있었습니다. 개체를 그리는 좌표가 두 가지 질문이 있습니까? 나는 X, Y 및 Z가 어디에 있는지 의미합니까?OpenGL의 true 좌표와 glutTimerFunc() 문제 C++

두 번째 튜토리얼을 일부 사이트에서 만들고 있습니다. 내 삼각형을 움직이기 위해 노력 중입니다. 튜토리얼에서는 작동하지만 컴퓨터에서는 작동하지 않습니다. 또한 소스 코드를 다운로드했지만 움직이지 않습니다. 그리고 컴파일러 오류가 없습니다.

편집 : 문제가 해결되었습니다. 컴퓨터를 다시 시작하는 것. 내 컴퓨터에 관한 포 블림이라고 생각합니다.

여기에 샘플 코드가 나와 있습니다. 문제는 glutTimerFunc()입니다.

#include <iostream> 
#include <stdlib.h> 

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

using namespace std; 

//Called when a key is pressed 
void handleKeypress(unsigned char key, int x, int y) { 
    switch (key) { 
     case 27: //Escape key 
      exit(0); 
    } 
} 

//Initializes 3D rendering 
void initRendering() { 
    glEnable(GL_DEPTH_TEST); 
} 

//Called when the window is resized 
void handleResize(int w, int h) { 
    glViewport(0, 0, w, h); 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    gluPerspective(45.0, (double)w/(double)h, 1.0, 200.0); 
} 

float _angle = 30.0f; 
float _cameraAngle = 0.0f; 

//Draws the 3D scene 
void drawScene() { 

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

    glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective 
    glLoadIdentity(); //Reset the drawing perspective 
    glRotatef(-_cameraAngle, 0.0f, 1.0f, 0.0f); //Rotate the camera 
    glTranslatef(0.0f, 0.0f, -5.0f); //Move forward 5 units 

    glPushMatrix(); //Save the transformations performed thus far 
    glTranslatef(0.0f, -1.0f, 0.0f); //Move to the center of the trapezoid 
    glRotatef(_angle, 0.0f, 0.0f, 1.0f); //Rotate about the z-axis 

    glBegin(GL_QUADS); 

    //Trapezoid 
    glVertex3f(-0.7f, -0.5f, 0.0f); 
    glVertex3f(0.7f, -0.5f, 0.0f); 
    glVertex3f(0.4f, 0.5f, 0.0f); 
    glVertex3f(-0.4f, 0.5f, 0.0f); 

    glEnd(); 

    glPopMatrix(); //Undo the move to the center of the trapezoid 
    glPushMatrix(); //Save the current state of transformations 
    glTranslatef(1.0f, 1.0f, 0.0f); //Move to the center of the pentagon 
    glRotatef(_angle, 0.0f, 1.0f, 0.0f); //Rotate about the y-axis 
    glScalef(0.7f, 0.7f, 0.7f); //Scale by 0.7 in the x, y, and z directions 

    glBegin(GL_TRIANGLES); 

    //Pentagon 
    glVertex3f(-0.5f, -0.5f, 0.0f); 
    glVertex3f(0.5f, -0.5f, 0.0f); 
    glVertex3f(-0.5f, 0.0f, 0.0f); 

    glVertex3f(-0.5f, 0.0f, 0.0f); 
    glVertex3f(0.5f, -0.5f, 0.0f); 
    glVertex3f(0.5f, 0.0f, 0.0f); 

    glVertex3f(-0.5f, 0.0f, 0.0f); 
    glVertex3f(0.5f, 0.0f, 0.0f); 
    glVertex3f(0.0f, 0.5f, 0.0f); 

    glEnd(); 

    glPopMatrix(); //Undo the move to the center of the pentagon 
    glPushMatrix(); //Save the current state of transformations 
    glTranslatef(-1.0f, 1.0f, 0.0f); //Move to the center of the triangle 
    glRotatef(_angle, 1.0f, 2.0f, 3.0f); //Rotate about the the vector (1, 2, 3) 

    glBegin(GL_TRIANGLES); 

    //Triangle 
    glVertex3f(0.5f, -0.5f, 0.0f); 
    glVertex3f(0.0f, 0.5f, 0.0f); 
    glVertex3f(-0.5f, -0.5f, 0.0f); 

    glEnd(); 

    glPopMatrix(); //Undo the move to the center of the triangle 

    glutSwapBuffers(); 
} 

void update(int value) { 
    _angle += 2.0f; 
    if (_angle > 360) { 
     _angle -= 260; 
    } 

    glutPostRedisplay(); //Tell GLUT that the display has changed 

    //Tell GLUT to call update again in 25 milliseconds 
    glutTimerFunc(25, update, 0); 
} 

int main(int argc, char** argv) { 
    //Initialize GLUT 
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); 
    glutInitWindowSize(400, 400); 

    //Create the window 
    glutCreateWindow("Transformations and Timers - videotutorialsrock.com"); 
    initRendering(); 

    //Set handler functions 
    glutDisplayFunc(drawScene); 
    glutKeyboardFunc(handleKeypress); 
    glutReshapeFunc(handleResize); 

    glutTimerFunc(24, update, 0); //Add a timer 
    glutMainLoop(); 

    return 0; 
} 
+2

코드 마크 업을 위해 '010'버튼 또는 4 개의 스페이스 들여 쓰기를 사용하고 게시하기 전에 미리보기를보십시오. –

+0

컴파일 오류가 무엇입니까? 어떤 플랫폼을 사용하고 있습니까? 어떤 컴파일러입니까? – hhafez

+0

@ hhafez- 나는 visual studio 2008을 사용하고 있습니다. 컴파일러 오류가 없습니다. 그러나 애니메이션이 작동하지 않습니다. 모양은 그대로 유지되지만 움직이지 않습니다. – Ercan

답변