Fixing OpenGL App: Black Background & White Triangle

  • Thread starter Thread starter cscott
  • Start date Start date
  • Tags Tags
    App Triangle
Click For Summary
SUMMARY

The discussion centers on resolving an issue in an OpenGL application where the window displays a white background instead of the intended black background with a white triangle. The solution involved adding a glFlush(); call to ensure that the rendering commands are executed. The user also noted that the initialization function initGL() was incorrectly setting the clear color to red instead of black, which contributed to the initial problem. By correcting these elements, the application now functions as expected.

PREREQUISITES
  • Understanding of OpenGL 1.1 rendering pipeline
  • Familiarity with GLUT for window management
  • Basic knowledge of C/C++ programming
  • Experience with OpenGL context and state management
NEXT STEPS
  • Explore OpenGL 4.0 features and enhancements
  • Learn about OpenGL context creation and management with GLFW
  • Investigate advanced rendering techniques using shaders
  • Study the differences between immediate mode and modern OpenGL practices
USEFUL FOR

OpenGL developers, graphics programmers, and anyone troubleshooting rendering issues in OpenGL applications.

cscott
Messages
778
Reaction score
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...
 
Computer science news on Phys.org
Argh! It seems that a silly glFlush(); fixed my problems. The code in bold above was from a book and gave me the hint :-p
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
5K
Replies
11
Views
4K
Replies
1
Views
7K