2010-05-01 111 views
1

也许我设置了错误的GLUT。我想verticies相对于它们的大小以像素为单位。现在,如果我创建了一个六边形,它占据了整个屏幕,即使单位为6OpenGL坐标系统混淆

#include <iostream> 
#include <stdlib.h> //Needed for "exit" function 
#include <cmath> 
//Include OpenGL header files, so that we can use OpenGL 
#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, //The key that was pressed 
        int x, int y) { //The current mouse coordinates 
    switch (key) { 
     case 27: //Escape key 
      exit(0); //Exit the program 
    } 
} 

//Initializes 3D rendering 
void initRendering() { 
    //Makes 3D drawing work when something is in front of something else 
    glEnable(GL_DEPTH_TEST); 
} 

//Called when the window is resized 
void handleResize(int w, int h) { 
    //Tell OpenGL how to convert from coordinates to pixel values 
    glViewport(0, 0, w, h); 

    glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective 

    //Set the camera perspective 
    glLoadIdentity(); //Reset the camera 
    gluPerspective(45.0,     //The camera angle 
        (double)w/(double)h, //The width-to-height ratio 
        1.0,     //The near z clipping coordinate 
        200.0);    //The far z clipping coordinate 
} 

//Draws the 3D scene 
void drawScene() { 
    //Clear information from last draw 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 


    glLoadIdentity(); //Reset the drawing perspective 
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); 

    glBegin(GL_POLYGON); //Begin quadrilateral coordinates 

    //Trapezoid 
    glColor3f(255,0,0); 

    for(int i = 0; i < 6; ++i) { 
     glVertex2d(sin(i/6.0*2* 3.1415), 
      cos(i/6.0*2* 3.1415)); 
    } 

    glEnd(); //End quadrilateral coordinates 

    glutSwapBuffers(); //Send the 3D scene to the screen 
} 

int main(int argc, char** argv) { 
    //Initialize GLUT 
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); 
    glutInitWindowSize(400, 400); //Set the window size 

    //Create the window 
    glutCreateWindow("Basic Shapes - videotutorialsrock.com"); 
    initRendering(); //Initialize rendering 

    //Set handler functions for drawing, keypresses, and window resizes 
    glutDisplayFunc(drawScene); 
    glutKeyboardFunc(handleKeypress); 
    glutReshapeFunc(handleResize); 

    glutMainLoop(); //Start the main loop. glutMainLoop doesn't return. 
    return 0; //This line is never reached 
} 

我怎样才能让这个坐标: (0,0)(10,0)(10,10), 和(0,10)定义一个从屏幕左上角开始的多边形,宽度和高度为10像素?

回答

3

如果您希望按照某种方式缩放对象,则应使用正交投影。

现在,有了透视图,事物不仅可以按照它们的大小缩放,而且可以按照它们的Z轴位置缩放。因此,使用此功能,而不是gluPerspective

gluOrtho2D(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top); 

这个功能基本上定义可以看到的空间,这就像一个大长方体。这使得远远的事物看起来和近乎事物的尺寸相同。

至于确切的缩放比例,它也会相对于视口大小而改变。为了正确地获取像素,您必须不断更改投影,或保持视口大小不变。

为了使其工作为1:1,如果您的视口为x像素宽,则正交投影应为x像素宽。

2

如果您使用2D绘图,则不想使用透视投影。如果你用gluOrtho2D(0, window_width, window_height, 0);设置你的相机,那么你应该得到你想要的。

+0

在我的代码中应该更改它的位置,我试过了,但它什么都没做 – jmasterx 2010-05-01 22:03:28

+0

您需要更换'gluPerspective()'调用。 – tzaman 2010-05-01 22:51:44