Fixing OpenGL App: Black Background & White Triangle

  • #1
782
1
I'm having trouble getting the basic OpenGL app running properly. When it opens the window is entirely white when it should have a black background and white triangle.

I will explain the code in bold bellow

Code:
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>

int initGL(GLvoid);
GLvoid drawScene(GLvoid);
GLvoid resizeScene(int width, int height);

int
main(int argc, char *argv[])
{
	const int WINDOW_HEIGHT = 300;
	const int WINDOW_WIDTH = 400;
	
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
	glutInitWindowPosition(100, 100);
	glutCreateWindow(argv[0]);
	
	initGL();
	
	glutDisplayFunc(drawScene);
	glutReshapeFunc(resizeScene);
	[b]
   glClearColor(0.0, 0.0, 0.0, 0.0);
   glClear(GL_COLOR_BUFFER_BIT);
   glColor3f(0.0, 1.0, 1.0);
   glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); 
   glBegin(GL_POLYGON);
      glVertex2f(-0.5, -0.5);
      glVertex2f(-0.5, 0.5);
      glVertex2f(0.5, 0.5);
      glVertex2f(0.5, -0.5);
   glEnd();
   glFlush();
	[/b]
	glutMainLoop();
	
	return 0;
}

int 
initGL(GLvoid)
{
	glShadeModel(GL_SMOOTH);
	glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
	
	glClearDepth(1.0f);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);
	
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
	
	return 0;
}

GLvoid 
resizeScene(int width, int height)
{
	if (height == 0)
		height = 1;
		
	glViewport(0, 0, width, height);
	
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f);
	
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	
	return;
}

GLvoid 
drawScene(GLvoid)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	
	glTranslatef(-1.5f, 0.0f, -6.0f);
	glColor3f(1.0,0.0,0.0);
	glBegin(GL_TRIANGLES);
		glVertex3f(0.0f, 1.0f, 0.0f);
		glVertex3f(-1.0f, -1.0f, 0.0f);
		glVertex3f(1.0f, -1.0f, 0.0f);
	glEnd();
	
	return;
}

The code in bold, when added to the rest, produces a black background and white square like it's suppose to. I don't know if that tells you anything about the rest of my code or not...
 
  • #2
Argh! It seems that a silly glFlush(); fixed my problems. The code in bold above was from a book and gave me the hint :tongue:
 

Suggested for: Fixing OpenGL App: Black Background & White Triangle

Replies
3
Views
703
Replies
6
Views
792
Replies
3
Views
3K
Replies
3
Views
786
Replies
3
Views
424
Replies
10
Views
861
  • Poll
Replies
9
Views
1K
Back
Top