Changing projection in OpenGL

删除回忆录丶 提交于 2019-12-04 05:40:16

问题


I'm having trouble with resizing a GLUT window. I want to print a rectangle but when i use the resize function i cant seem to draw and shape in the window. I have the following code and i want to change with width of the square when i resize the window but if the height is resized i dont want the height of it to change.

#include <GLUT/glut.h>
#include <stdio.h>
#include <stdlib.h>
/* globals */
GLsizei wh = 500, ww = 500; 
/* initial window size */ 
GLfloat size = 3.0; 
/* half side length of square */

void myinit(void)
{
    glClearColor(0.0,0.0,1.0,1.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0,1,0,1.0,-1.0,1.0);
    glViewport(0,0,ww,wh);
}

/* display callback -- required by GLUT 3.0 */
void renderScene(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1.0,.5,.7);
    glBegin(GL_POLYGON);
        glVertex3f (0.25, 0.25, 0.0);
        glVertex3f (0.75, 0.25, 0.0);
        glVertex3f (0.75, 0.75, 0.0);
        glVertex3f (0.25, 0.75, 0.0);
    glEnd();

    glFlush();
}

void mouse(int button, int state, int x, int y) {
    if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) {
       glMatrixMode(GL_PROJECTION);
       glLoadIdentity();
       glOrtho(0.0,500,0.0,500,0.0,1.0);
   }
}
/*app quits if q is pressed*/
void keyboard(unsigned char key, int x, int y) {
     if(key == 'Q' | key == 'q')
        exit(0);
}

/* The main program -- note that there is no "flow of control" -- the program merely calls          functions to handle interactions */
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitWindowSize(ww,wh);
    glutInitWindowPosition(100,100);
    glutCreateWindow("Square");

    myinit();

    glutMouseFunc(mouse);
    glutKeyboardFunc(keyboard);
    glutDisplayFunc(renderScene);
    //glutReshapeFunc(changeSize);
    glutMainLoop();
    return 0;
}

i made a test changeSize function and when i used that it would just show an empty blue window without the rectangle in there


回答1:


Your problem lies in the use of a fixed value for the orthographic matrix:

glOrtho(0.0,500,0.0,500,0.0,1.0);

What this says is, however big my window is, make vertex position of 0 map to the bottom of the window, and 500 map to the top of the window.

So if you have a 500 pixel high window, and you draw a square that covers 100 units, this will map to 100 pixels in the window.

However if you stretch the window to be 1000 pixels high, now you have a region from 0 to 500 mapping to a window of height 1000 pixels. So your same square that used to cover 100 pixels in height now covers 200 pixels in height.

If you want it to always be the same size when you resize, you need to update the orthographic matrix to map a larger area to the new larger window. So if you change your orthographic matrix to map region 0 to windowHeight to the window, a 100 unit square will always fill the exact same amount of height.

glOrtho( 0.0, //left
         500, //right
         0.0, //bottom
         height_in_pixels, //top
         0.0,   //near 
         1.0);  //far


来源:https://stackoverflow.com/questions/12518603/changing-projection-in-opengl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!