Fixing OpenGL App: Black Background & White Triangle

  • Thread starter Thread starter cscott
  • Start date Start date
  • Tags Tags
    App Triangle
Click For Summary
The OpenGL application initially displayed a white window instead of the intended black background with a white triangle. The issue was resolved by adding a glFlush() call, which ensured that the rendering commands were executed. The code included proper initialization of OpenGL settings, such as setting the clear color and enabling depth testing. The drawScene function was responsible for rendering a triangle, but without glFlush(), the output was not displayed correctly. The addition of this function call allowed the application to function as expected, displaying the correct colors and shapes.
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
 
Thread 'ChatGPT Examples, Good and Bad'
I've been experimenting with ChatGPT. Some results are good, some very very bad. I think examples can help expose the properties of this AI. Maybe you can post some of your favorite examples and tell us what they reveal about the properties of this AI. (I had problems with copy/paste of text and formatting, so I'm posting my examples as screen shots. That is a promising start. :smile: But then I provided values V=1, R1=1, R2=2, R3=3 and asked for the value of I. At first, it said...

Similar threads

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