1

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 ピクセルの多角形を定義するようにするにはどうすればよい ですか?

4

2 に答える 2

3

オブジェクトをそのようにスケーリングする場合は、正投影を使用する必要があります。

現在、遠近法を使用すると、サイズだけでなく、Z 軸の位置によってもスケーリングされます。したがって、代わりにこの関数を使用しますgluPerspective

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

その機能は基本的に、大きな四角柱のような、見える空間を定義します。これにより、遠くのものは近くのものと同じサイズに見えます。

正確なスケーリングに関しては、ビューポート サイズに対しても変化します。ピクセルを正確に正しく表示するには、投影を常に変更するか、ビューポート サイズを固定しておく必要があります。

1:1 として機能するには、ビューポートの幅がピクセルの場合x、正投影もxピクセル幅にする必要があります。

于 2010-05-01T21:56:57.410 に答える
2

2D で描画している場合は、透視投影を使用したくありません。カメラをセットアップするとgluOrtho2D(0, window_width, window_height, 0);、探しているものが得られるはずです。

于 2010-05-01T22:00:12.353 に答える