【问题标题】:OpenGL Coordinate system confusionOpenGL坐标系混乱
【发布时间】:2010-05-01 21:54:22
【问题描述】:

也许我设置了 GLUT 错误。我希望顶点与它们的像素大小相关。现在如果我创建一个六边形,即使单位是 6,它也会占据整个屏幕。

#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像素?

【问题讨论】:

    标签: opengl polygon glut


    【解决方案1】:

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

    现在,通过透视,事物不仅按大小缩放,还按 Z 轴位置缩放。所以用这个函数代替gluPerspective

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

    那个函数基本上定义了你能看到的空间,就像一个大的矩形棱镜。这使得远处的物体看起来与近处的物体大小相同。

    至于精确的缩放,它也会相对于视口大小而变化。要使像素完全正确,您必须不断更改投影,或保持视口大小固定。

    如果您的视口为 x 像素宽,则要实现 1:1,则正交投影也应为 x 像素宽。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 我应该在代码中的哪个位置更改它,我试过了,但它什么也没做
      • 你需要替换gluPerspective()调用。
      猜你喜欢
      • 1970-01-01
      • 2016-12-24
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多