Fixing OpenGL App: Black Background & White Triangle

In summary, the conversation discusses difficulties with getting an OpenGL app to run properly, with a white background instead of a black one and a white triangle instead of a colored square. The speaker explains that adding some code from a book and including a glFlush(); resolved the issue.
  • #1
cscott
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...
 
Computer science news on Phys.org
  • #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 :-p
 
  • #3


Thank you for providing the code and describing the issue you are having. It seems like the main issue is with the colors and background not being displayed correctly. Let's take a look at the code and see where the problem might be.

First, in the initGL() function, you are setting the clear color to red (1.0, 0.0, 0.0). This means that the background will be red instead of black. To fix this, change the values to (0.0, 0.0, 0.0) to set the background to black.

Next, in the resizeScene() function, you are setting the viewport to the width and height of the window, but you are not setting the projection matrix properly. Instead of using gluPerspective(), you should use glOrtho() to set up an orthographic projection. This will allow you to see the triangle in the center of the screen. You can use the same values that are currently being used in glOrtho() in the main() function.

Finally, in the drawScene() function, you are setting the color to red again (1.0, 0.0, 0.0) before drawing the triangle. This will make the triangle red instead of white. Change the color to (1.0, 1.0, 1.0) to make the triangle white.

With these changes, the background should be black and the triangle should be white. I hope this helps you get your OpenGL app running properly. If you continue to have issues, feel free to reach out for further assistance.
 

Related to Fixing OpenGL App: Black Background & White Triangle

1. What could be causing a black background in my OpenGL app?

There are several possible reasons for a black background in an OpenGL app. One common cause is that the background color is not being properly set in the code. Another possibility is that the camera's view is not properly set up, resulting in all objects appearing behind the camera. Additionally, there may be issues with lighting or shaders that are causing the background to appear black.

2. Why is there only a white triangle visible in my OpenGL app?

If you are only seeing a white triangle in your app, it is likely that there is an issue with the rendering of your objects. Check to make sure that the vertices and indices for your objects are correctly defined, and that the correct shaders are being used. It is also possible that the camera's view is not properly set, resulting in only one small object being visible.

3. How can I fix the lighting in my OpenGL app?

The lighting in an OpenGL app can be fixed by adjusting the lighting parameters in your code. Make sure that the light position, color, and intensity are set correctly and that the objects have the necessary materials and textures for the desired lighting effect. If you are using shaders, check for any errors in the shader code that may be affecting the lighting.

4. Why is my OpenGL app running slowly?

There are a few possible reasons for slow performance in an OpenGL app. One common issue is that the code is not optimized, resulting in inefficient rendering processes. Another possible cause is that the graphics card or driver may not be compatible with your app. Additionally, if the app is rendering complex objects or using a lot of textures, it may require more processing power and cause slower performance.

5. How can I prevent objects from disappearing in my OpenGL app?

If objects are disappearing in your app, it is likely that the camera's view is not properly set up. Make sure that the camera's near and far clipping planes are set appropriately and that the objects are within the camera's view frustum. It is also possible that objects are being rendered behind the camera, so adjusting the camera's position or the objects' positions may solve the issue.

Similar threads

  • Programming and Computer Science
Replies
3
Views
4K
Replies
11
Views
4K
Replies
1
Views
7K
Back
Top