views:

63

answers:

2

Why can't I get anything to display with this code?

#include <iostream>
#include "GL/glfw.h"
#ifndef MAIN
#define MAIN
#include "GL/gl.h"
#include "GL/glu.h"
#endif
using namespace std;

void display();

int main()
{
    int running = GL_TRUE;
    glfwInit();

    if( !glfwOpenWindow( 640,480, 0,0,0,0,0,0, GLFW_WINDOW ) )
    {
        glfwTerminate();
        return 0;
    }

    while( running )
    {
        //GL Code here
        display();

        glfwSwapBuffers();
        // Check if ESC key was pressed or window was closed
        running = !glfwGetKey( GLFW_KEY_ESC ) &&
        glfwGetWindowParam( GLFW_OPENED );
    }

    glfwTerminate();

    return 0;
}

void display()
{
    glClearColor(0,0,0,0);
    glClear(GL_COLOR_BUFFER_BIT);
    glViewport(0,0,640,480);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    //glTranslatef(0, 0, -2);
    glBegin(GL_POLYGON);
        glColor3f(1.0, 0.2, 0.2);
        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();
}
A: 

You haven't set the viewport with glViewport nor the projection matrix. If you change the clear color to something other than black do you get this color in your window?

Try changing the code to:

void display()
{
    glClearColor(0,0,0,0);
    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-1, 1, -1, 1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    //glTranslatef(0, 0, -2);
    glBegin(GL_POLYGON);
        glColor3f(1.0, 0.2, 0.2);
        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();
}

(The viewport may have been set correctly when you created your OpenGL window, otherwise initialise it with the window width and height)

Also disable backface culling incase your polygon has incorrect winding, glDisable(GL_CULLFACE)

Andreas Brinck
Yes; I was under the impression that viewport is taken care of automatically.
I think your impression is correct ;) I'm not sure if it's part of the OpenGL spec but at least on windows when you create your rendering context the viewport will be set to match the window.
Andreas Brinck
That code does not work with glTranslate.
How do you mean? Also posted a small edit about back face culling.
Andreas Brinck
When I used glTranslate nothing appears on the screen except the clear color. Basically I want to adjust "zoom".
`gluOrtho2D` sets the far and near clip plane to -1 and 1 respectively, if you translate your polygon with -2 in z it gets behind the far clip plane.
Andreas Brinck
If you wan't to use zoom you can't use an orthographic projection, change `gluOrtho` to `gluPerspective`
Andreas Brinck
I think I get what you are saying; I'll adjust the code to see.
+1  A: 

I think the issue is that when you don't specify a coordinate system eg

void glOrtho(GLdouble   left,
             GLdouble   right,
             GLdouble   bottom,
             GLdouble   top,
             GLdouble   nearVal,
             GLdouble   farVal);

or

void glFrustum(GLdouble  left,
               GLdouble  right,
               GLdouble  bottom,
               GLdouble  top,
               GLdouble  nearVal,
               GLdouble  farVal);

the default coordinate system is:

-1, 1, -1, 1, -1, 1

so the glTranslate moves the object outside the coordinate system and openGL clips the object.

If you change the glTranslate() call to translate by 1.0 the object is visible.

JRT