首页 > 解决方案 > 调整窗口大小时使用 GLUT 重塑回调来更改对象的大小?

问题描述

我一直在摆弄这个,这似乎是我如何使用 gluOrtho2d 的问题。使用原样的重塑功能,我可以毫无问题地重塑窗口,但顶点的相对大小保持不变。如果在制作顶点之前我没有使用 gluOrtho2D,我可能可以通过在重塑 glLoadIdentity 之后添加一个 glFrustum 来相当容易地做到这一点?

void initWindow1(const char* windowLabel) {

    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowPosition(100, 100);   
    glutInitWindowSize(800, 600);       
    glutCreateWindow(windowLabel); 
    glClearColor(1.0, 1.0, 1.0, 0.0);   

}

void house() {

    // set point vertices for the rectangle
    GLint rectP1[] = { 0, 0 }; // bottom center
    GLint rectP2[] = { 25,0 }; // bottom right
    GLint rectP3[] = { 25, 50 }; // top right
    GLint rectP4[] = { -25, 50 }; // top left
    GLint rectP5[] = { -25, 0 }; // bottom left

    // set point vertices for the triangle using the rectangle points
    GLint triP1[] = { rectP3[0] + 10, rectP3[1] }; // right
    GLint triP2[] = { (rectP3[0] + rectP4[0]) / 2, rectP3[1] + 15 }; // top
    GLint triP3[] = { (rectP4[0] - 10), rectP4[1] }; // left

    // Rectangle 
    glBegin(GL_LINE_LOOP);
      glVertex2iv(rectP1);
      glVertex2iv(rectP2);
      glVertex2iv(rectP3);
      glVertex2iv(rectP4);
      glVertex2iv(rectP5);
    glEnd();

    //Triangle
    glBegin(GL_LINE_LOOP);
      glVertex2iv(triP1);
      glVertex2iv(triP2);
      glVertex2iv(triP3);
    glEnd();
}

void displayFiveHouses() {

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity(); // Reset back to id matrix

    // Make the lines thicker and the line color black
    glLineWidth(2.0);
    glColor3f(0.0, 0.0, 0.0);

    // Create a circle object in order to allocate the starting locations for each house.
    // This will simulate polar coordinates on the cartesian graph
    Circle myCircle(350, 200, 200.0);
    GLfloat xOffset = 0;
    GLfloat yOffset = 0;
    const GLfloat PI = 3.14159265358979323846;

    // Loops through the five instances of the houses at different angles based on the circle   
    GLfloat radians = 1.0 / 6.0;
    GLfloat angle[] = { -45.0, -30.0, 0.0, 30.0, 45.0 };
    for (int i = 0; i < 5; i++) {

        xOffset = myCircle.radius * cos(PI * radians);
        yOffset = myCircle.radius * sin(PI * radians);

        glPushMatrix();
            glTranslatef(myCircle.x + xOffset, myCircle.y + yOffset, 0.0);
            glRotatef( angle[i], 0.0, 0.0, 1.0);
            house();
        glPopMatrix();
    
        radians += 1.0 / 6.0;
    }

    glFlush();
    glutSwapBuffers();

}

void reshape(int w, int h) {

    float wf = w;
    float hf = h;

    glViewport(0, 0, (GLsizei)w, (GLsizei)h);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0, wf, 0.0, hf);
    //glFrustum(-1.0, 1.0, -1.0, 1.0, 0.0, 40);
    glMatrixMode(GL_MODELVIEW);

}

int main(int argc, char** argv) {

    // Initialize openGL and set up the display window
    glutInit(&argc, argv);
    initWindow1("Comp 390: TME 1, Program 2");
    gluOrtho2D(0.0, 800, 0.0, 600);

    // display the houses
    glutDisplayFunc(displayFiveHouses);
    glutReshapeFunc(reshape);
    glutMainLoop();

    return 0;
}

标签: c++openglreshapeglut

解决方案


推荐阅读