user1179321
user1179321

Reputation: 801

Changing projection in OpenGL

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

Upvotes: 0

Views: 1388

Answers (1)

Tim
Tim

Reputation: 35933

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

Upvotes: 3

Related Questions