Question

I am having some trouble running a simple program:(code below text)

i would like to print as an output a square with [50,50],[-50,-50],[50,-50],[-50,50] coordinates.

I'm setting up a glOrtho matrix in init() and then in my display func i print the square with the above coordinates as glVertex2f (for example glVertex2f(50.0,-50.0)) Is it the correct way or does glVertex2f not take real coordinates as values?

Anyway here is the code: (it compiles nicely, however it doesnt show anything in graphics window)

#include <iostream>
#include <stdlib.h>

#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <windows.h>
#include <GL/glut.h>
#endif

#define SCR_WID 640
#define SCR_HEI 480

#include "imageloader.h"

using namespace std;
//*****************************CUSTOM FUNCS***********************************//
void init()
{
    glViewport(0,0,SCR_WID,SCR_HEI);
    glPushMatrix();
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho((double)SCR_WID,0.0,(double)SCR_HEI,0.0,-5.0,-20.0);
    glMatrixMode(GL_MODELVIEW);
}

//*****************************CALLBACKS (win 1)******************************//
void resizeFunc(int w, int h)
{
    glutReshapeWindow(SCR_WID,SCR_HEI);
}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);

    glColor3f(1.0,1.0,1.0);
    glBegin(GL_QUADS);
        glVertex2f(1.0,1.0);
        glVertex2f(-1.0,1.0);
        glVertex2f(1.0,-1.0);
        glVertex2f(-1.0,-1.0);
    glEnd();
    glutSwapBuffers();
}

void key(unsigned char key, int x, int y)
{
    if (key == 'q' || key == 27 )
        exit(0);

    glutPostRedisplay();
}
//**********************************main func*********************************//
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitWindowSize(SCR_WID,SCR_HEI);
    glutInitWindowPosition(10,10);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("TestGlut");

    glutReshapeFunc(resizeFunc);
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutKeyboardFunc(key);

    init();

    glutMainLoop();

    return EXIT_SUCCESS;
}
Was it helpful?

Solution

Your init function is a little odd

    glViewport(0,0,SCR_WID,SCR_HEI);

The default viewport is already the full window, so this is redundant.

    glPushMatrix();

You never call glPopMatrix or manipulate the matrix stack in any other way elsewhere, so why are you doing this?

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho((double)SCR_WID,0.0,(double)SCR_HEI,0.0,-5.0,-20.0);

Calling glLoadIdentity immediately before glOrtho is a waste. glOrtho overwrite the current matrix, so it doesn't matter what it was before. Also glLoadIdentity resets the matrix value back to it's default of an identity matrix, which is what it would be anyway at program start, so it's doubly redundant.

Also, the glOrtho params are 'left right bottom top near far'. You've reversed the X and Y axes by putting a larger number for left and bottom than for right and top. At best this will give you a projection that will put the (0,0) coordinate at the lower right of the screen, which is pretty unusual. Finally, your near and far planes lie completely in negative Z space. That means that vertices that are specified with only two values (glVertex2f as you're using) will always be excluded since they have an implicit 0 for their z coordinate.

void resizeFunc(int w, int h)
{
    glutReshapeWindow(SCR_WID,SCR_HEI);
}

Why bother with a resize function if you're going to ignore the input width and height?

    glBegin(GL_QUADS);
        glVertex2f(1.0,1.0);
        glVertex2f(-1.0,1.0);
        glVertex2f(1.0,-1.0);
        glVertex2f(-1.0,-1.0);
    glEnd();

Aside from the fact that these coordinates will produce clipped vertices (because their Z value is implicitly 0, and thus outside of your near/far clip region), you're specifying them in what appear to be in normalized clip coordinates. If you corrected the Z shape problem that genpfault mentioned (by swapping vertex 3 and 4) then these would take up the whole screen if you were working with the default projection matrix. By setting an ortho matrix using the screen pixel dimensions, you've created a square that would be 2 pixels wide and two pixels tall, located in the lower right hand corner of the screen.

void key(unsigned char key, int x, int y)
{
    ...
    glutPostRedisplay();
}

Why? If you want your program to render continuously, and not just when someone hits a key, then you should have glutPostRedisplay() in an idle function, not in the keypress function.

int main(int argc, char *argv[])
{
    ...
    glutIdleFunc(display);
    ...
}

Don't set your idle function to the display function. If you want your program to animate, make an actual idle function that calls glutPostRedisplay.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top