Zi0P4tch0
Zi0P4tch0

Reputation: 21

OpenGL black screen/nothing drawn?

Why does this piece of code produce a black screen (nothing is drawn...)?

I'm creating a Pong clone but I can't proceed without getting this to work.

#include <GL/glut.h>

struct Rectangle {
    int x;
    int y;
    int width;
    int height;
};

struct Ball {
    int x;
    int y;
    int radius;
};

typedef struct Rectangle Rectangle;
typedef struct Ball Ball;

Rectangle rOne, rTwo;
Ball ball;

void display(void);
void reshape(int w, int h);
void drawRectangle(Rectangle *r);

int main(int argc, char* argv[]) {

    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
    glutInitWindowSize(800,600);
    glutCreateWindow("Pong");
    gluOrtho2D(0,0,800.0,600.0);

    rOne.x = 100;
    rOne.y = 100;
    rOne.width = 100;
    rOne.height = 50;   

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();

    return 0;

}

void display(void) {
    glClear(GL_COLOR_BUFFER_BIT);

    drawRectangle(&rOne);

    glFlush();
    glutSwapBuffers();
}

void reshape(int w, int h) {
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0,0,(GLfloat)w,(GLfloat)h);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void drawRectangle(Rectangle *r) {
    glBegin(GL_QUADS);
        glVertex2i(r->x,r->y);
    glVertex2i(r->x+(r->width-1),r->y);
    glVertex2i(r->x+(r->width-1),r->y+(r->height-1));
    glVertex2i(r->x,r->y+(r->height-1));
    glEnd();
}

Upvotes: 2

Views: 3169

Answers (2)

datenwolf
datenwolf

Reputation: 162164

Your problem lies here:

gluOrtho2D(0,0,800.0,600.0);

gluOrtho2D is not like glViewport. The parameter of gluOrtho2D are :

gluOrtho2D(left, right, bottom, top);

So you must call it like

gluOrtho(0, w, 0, h);

Upvotes: 3

genpfault
genpfault

Reputation: 52084

You're requesting an ortho projection with zero width:

gluOrtho2D(0,0,(GLfloat)w,(GLfloat)h);

This is probably what you meant:

gluOrtho2D(0,(GLfloat)w,0,(GLfloat)h);

Example:

#include <GL/glut.h>

typedef struct {
    int x;
    int y;
    int width;
    int height;
} Rect;

typedef struct {
    int x;
    int y;
    int radius;
} Ball;

Rect rOne, rTwo;
Ball ball;

void display(void);
void reshape(int w, int h);
void drawRectangle(Rect *r);

int main(int argc, char* argv[]) 
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(800,600);
    glutCreateWindow("Pong");

    rOne.x = 100;
    rOne.y = 100;
    rOne.width = 100;
    rOne.height = 50;   

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();

    return 0;
}

void display(void) 
{
    glClear(GL_COLOR_BUFFER_BIT);

    drawRectangle(&rOne);

    glFlush();
    glutSwapBuffers();
}

void reshape(int w, int h)
{
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0,(GLfloat)w,0,(GLfloat)h);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void drawRectangle(Rect *r) 
{
    glBegin(GL_QUADS);
    glVertex2i(r->x,r->y);
    glVertex2i(r->x+(r->width-1),r->y);
    glVertex2i(r->x+(r->width-1),r->y+(r->height-1));
    glVertex2i(r->x,r->y+(r->height-1));
    glEnd();
}

Upvotes: 5

Related Questions