Nothing is drawing in my simple openGL program.

I’m trying to just gain simple experience with openGL after reverse engineering a snake program using GLUT. I’m essentially using the same main body as the author of the code I’m using, however it does not behave as I would expect…

Here is my code,


#include <GL/glut.h>

void drawSquare(int x1, int y1, int x2, int y2) {
	glColor3f(0, 1, 0);
	glBegin(GL_QUADS);
	glVertex2f(x1, y1);
	glVertex2f(x2, y1);
	glVertex2f(x1, y2);
	glVertex2f(x2, y2);
	glEnd();
}

void display() {
	glClear(GL_COLOR_BUFFER_BIT);
	drawSquare(250, 350, 250, 350);
	glutSwapBuffers();
}

void timer(int = 0) {
	display();
	glutTimerFunc(300, timer, 0);
}

int main(int argc, char **argv) {
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowSize(800, 600);
	glutInitWindowPosition(300, 300);
	glutCreateWindow("Tester!");
	glClearColor(0, 0, 0, 1.0);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0, 800, 600, 0, -1.0, 1.0);
	glutDisplayFunc(display);
	timer();

	glutMainLoop();
}

All I want it to so is draw a simple square inside of the window it generates. I have no clue what I’m doing wrong. My code as far as I can tell is doing the exact same thing as the code found at speedprogramming/snake at master · mika314/speedprogramming · GitHub

If anyone could enlighten me on what I’m doing wrong, I’d appreciate it (also any good openGL tutorials to check out for absolute beginnings with the API, I’m not completely new to C++ [~2 years experience with it]).

Hi,
I can see two reasons.

  1. You are not giving the correct coordinates. That is x1, y1, x2, y2 values should be atleast enough to render a rectangle. You are giving it 250, 350, 250, 350 that means that your x1=x2 and y1=y2 which as far as I can see would not render anything. Instead give it 250, 250, 260, 360 which should render a 10x10 rectangle.
  2. You winding order in the drawSquare function is wrong. Remember to give coordinates in an anticlockwise order like this assuming that x1<x2 and y1<y2

void drawSquare(int x1, int y1, int x2, int y2) {
    glColor3f(0, 1, 0);
    glBegin(GL_QUADS);
    glVertex2f(x1, y1);
    glVertex2f(x1, y2);
    glVertex2f(x2, y2);
    glVertex2f(x2, y1);
    glEnd();
}

See if this helps.

[QUOTE=mobeen;1278559]Hi,
I can see two reasons.

  1. You are not giving the correct coordinates. That is x1, y1, x2, y2 values should be atleast enough to render a rectangle. You are giving it 250, 350, 250, 350 that means that your x1=x2 and y1=y2 which as far as I can see would not render anything. Instead give it 250, 250, 260, 360 which should render a 10x10 rectangle.
  2. You winding order in the drawSquare function is wrong. Remember to give coordinates in an anticlockwise order like this assuming that x1<x2 and y1<y2

void drawSquare(int x1, int y1, int x2, int y2) {
    glColor3f(0, 1, 0);
    glBegin(GL_QUADS);
    glVertex2f(x1, y1);
    glVertex2f(x1, y2);
    glVertex2f(x2, y2);
    glVertex2f(x2, y1);
    glEnd();
}

See if this helps.[/QUOTE]

Yup, that did the trick… Didn’t realize I was messing it up like that. Makes more sense now. Thanks!