Search code examples
c++copenglglut

Visualization Issue in OpenGL (glOrtho & projection)


I don't know if I did something wrong in my functions (maybe wrong parameters to glOrtho) and so the result is just wrong or if it's all normal as it would be.

In particular I have this situation:

link to image

I would like to have the green rect and all its inner content to take all the space of the window and not just a part of it (if you notice there are some empty spaces all around the main content: "the green rect").

Here there are my main functions:

#define HEXAGONAL_SHRINK 0.8655f

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(640, 480);
    glutInitWindowPosition(100, 100);
    glutCreateWindow(argv[0]);
    glClearColor (0.0, 0.0, 0.0, 0.0);
    glShadeModel (GL_FLAT);
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMouseFunc(mouse);
    glutMainLoop();
    return 0;
}

void reshape(int w, int h)
{
    GLfloat aspect, dim;

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

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if (s3hex->rows > s3hex->columns)
          dim = s3hex->rows * 0.5f; 
        else 
          dim = s3hex->columns * 0.5f;
    aspect = (GLfloat)w / (GLfloat)h;
    if (w <= h)
        glOrtho (-dim, dim, -dim/aspect, dim/aspect, 1.0, -1.0);
    else
        glOrtho (-dim*aspect, dim*aspect, -dim, dim, 1.0, -1.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void display(void)
{
    CALint i, j;
    CALreal z, h, color;

    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();

    glTranslatef(-(s3hex->columns*HEXAGONAL_SHRINK)/2.0f, s3hex->rows/2.0f, 0);
    glScalef(HEXAGONAL_SHRINK, -1, 1);

    for (i=0; i<s3hex->rows; i++)
        for (j=0; j<s3hex->columns; j++)
        {
            z = calGet2Dr(s3hex,Q.z,i,j);
            h = calGet2Dr(s3hex,Q.h,i,j);

            if (h > 0)
            {
                color = (h - h_min) / (h_Max - h_min);
                glColor3d(1,color,0);
                glRecti(j, i, j+1, i+1);
            }
            else
                if (z > 0)
                {
                    color = (z - z_min) / (z_Max - z_min);
                    glColor3d(color,color,color);
                    glRecti(j, i, j+1, i+1);
                }
        }

    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    glColor3d(0,1,0);
    glRectd(0,0,s3hex->columns, s3hex->rows);
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

    glPopMatrix();

    glutSwapBuffers();
}

P.S: If you need to know what's s3hex is, then you can see it as a normal matrix in which every cell contains a set of substates. Then in according to the value of these substates I set the colors for the rendering in the display func.


Solution

  • Everything seems normal to me. Nothing guaranties that s3hex->columns and s3hex->rows will match your viewport size.

    What you can do is to scale up the modelview to make the drawing fill your viewport.

    Something along the lines of:

    glScalef(viewportWidth / s3hex->columns, viewportHeight / s3hex->rows, 1);
    

    Don't do this if s3hex->columns or s3hex->rows is zero.