Troubleshooting OpenGL+GLUT on OSX

  • Thread starter Thread starter cscott
  • Start date Start date
  • Tags Tags
    Troubleshooting
Click For Summary

Discussion Overview

The discussion revolves around troubleshooting issues with OpenGL and GLUT on macOS, specifically regarding rendering a window that displays a white square and triangle on a black background. Participants explore code snippets and suggest modifications to achieve the desired output.

Discussion Character

  • Technical explanation
  • Exploratory
  • Debate/contested

Main Points Raised

  • The original poster describes their issue with a window displaying only a white background, despite following a tutorial.
  • One participant suggests adding a color command (glColor3f(1.0, 1.0, 1.0)) before the drawing commands to set the color of the shapes.
  • Another participant proposes adding glutSwapBuffers() at the end of the DrawGLScene function to properly display the rendered content.
  • A later reply mentions that a missing translate command was also contributing to the issue, as it caused the square and triangle to overlap.

Areas of Agreement / Disagreement

Participants generally agree on the need for specific OpenGL commands to resolve the rendering issue, but there is no consensus on the exact cause of the initial problem, as multiple suggestions are offered without a definitive resolution.

Contextual Notes

Participants reference external tutorials and code examples, indicating potential differences in implementation or compatibility with macOS that may affect the troubleshooting process.

cscott
Messages
778
Reaction score
1
I'm having some trouble on OS X following a tutorial that wasn't really meant for this language-OS combination.

I'm using Xcode and the GLUT+OpenGL frameworks to compile the following code. [it's supposed to open a window and display a white square and triangle on a black backdrop.]:
Code:
#include <OpenGL/gl.h>		
#include <OpenGL/glu.h>		
#include <GLUT/glut.h>		

#define kWindowWidth	800
#define kWindowHeight	300

GLvoid InitGL(GLvoid);
GLvoid DrawGLScene(GLvoid);
GLvoid ReSizeGLScene(int Width, int Height);

int main(int argc, char** argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize (kWindowWidth, kWindowHeight);
	glutInitWindowPosition (100, 100);
	glutCreateWindow (argv[0]);

	InitGL();

	glutDisplayFunc(DrawGLScene);
	glutReshapeFunc(ReSizeGLScene);

	glutMainLoop();

	return 0;
}

GLvoid ReSizeGLScene(int width, int height)				// Resize And Initialize The GL Window
{
	if (height==0)								// Prevent A Divide By Zero By
	{
		height=1;							// Making Height Equal One
	}

	glViewport(0, 0, width, height);	
		glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix
	glLoadIdentity();							// Reset The Projection Matrix

	// Calculate The Aspect Ratio Of The Window
	gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

	glMatrixMode(GL_MODELVIEW);						// Select The Modelview Matrix
	glLoadIdentity();							// Reset The Modelview Matrix
}

GLvoid InitGL(GLvoid)								// All Setup For OpenGL Goes Here
{
	glShadeModel(GL_SMOOTH);						
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);		
	glClearDepth(1.0f);							// Depth Buffer Setup
	glEnable(GL_DEPTH_TEST);						// Enables Depth Testing
	glDepthFunc(GL_LEQUAL);	
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	
 }
 
GLvoid DrawGLScene(GLvoid)								// Here's Where We Do All The Drawing
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		// Clear The Screen And The Depth Buffer
	glLoadIdentity();	
	glTranslatef(-1.5f,0.0f,-6.0f);		
	
	glBegin(GL_TRIANGLES);						// Drawing Using Triangles
		glVertex3f( 0.0f, 1.0f, 0.0f);				// Top
		glVertex3f(-1.0f,-1.0f, 0.0f);				// Bottom Left
		glVertex3f( 1.0f,-1.0f, 0.0f);				// Bottom Right
	glEnd();							// Finished Drawing The Triangle

	glBegin(GL_QUADS);						// Draw A Quad
		glVertex3f(-1.0f, 1.0f, 0.0f);				// Top Left
		glVertex3f( 1.0f, 1.0f, 0.0f);				// Top Right
		glVertex3f( 1.0f,-1.0f, 0.0f);				// Bottom Right
		glVertex3f(-1.0f,-1.0f, 0.0f);				// Bottom Left
	glEnd();							// Done Drawing The Quad
}

...I get a window but it's white with nothing in it. When I resize the window to particular sizes I can see the black background and square only but obviously something is still wrong...

The only code I can find is written in Objective-C and uses some NextStep stuff.

NeHe tutotirlas:
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=02
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=Mac_OS_X
 
Last edited by a moderator:
Technology news on Phys.org
For the DrawGLScene(GLvoid) before the glBegin(...) commands put glColor3f(1.0, 1.0, 1.0). You might want to change the colors if you feel like.

However I didn't find a resonable sollution yet. I didn't find what may be wrong yet too. You can try that and see if it works though.
 
Haha I found it. I don't know why I didn't notice. At the end of the redraw function DrawGLScene(GLvoid) add glutSwapBuffers(). and you might also want the glColorf() too. Hope that works. I didn't look at the tutorial yet, I trust you copyed and pasted the code correctly.
 
Thanks a lot, it works now.

I was actually missing a translate command too, so that the square and triangle weren't on top of each other.
 
Last edited:

Similar threads

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