Troubleshooting OpenGL+GLUT on OSX

In summary, the user is having trouble following a tutorial on OS X that wasn't meant for this language-OS combination. They are using Xcode and the GLUT+OpenGL frameworks to compile a code which is supposed to open a window and display a white square and triangle on a black backdrop. The user finds the problem when they try to resize the window to particular sizes and the square and triangle are still on top of each other. They add a translate command to the DrawGLScene(GLvoid) function and the window works.
  • #1
cscott
782
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
  • #2
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.
 
  • #3
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.
 
  • #4
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:

1. How do I know if my Mac supports OpenGL and GLUT?

To check if your Mac supports OpenGL and GLUT, go to the Apple menu and select "About This Mac." Then click on the "System Report" button and select "Graphics/Displays" from the sidebar. If your graphics card is listed as supporting OpenGL and GLUT, then your Mac supports these technologies.

2. Why am I getting a black screen when running my OpenGL+GLUT program on my Mac?

This could be due to a few reasons. First, make sure you have set up the display function properly and that it is being called in your main function. Next, check your code for any errors or missing elements. Also, make sure you are using the correct version of OpenGL for your Mac. If these steps do not solve the issue, try updating your graphics drivers.

3. How do I fix "GLUT: Fatal Error in myprogram: redisplay needed for window 1, but no display callback?"

This error occurs when the display function is not properly set up or called. Make sure you have created a display function and registered it using "glutDisplayFunc." Also, check that you are calling the "glutPostRedisplay" function at the end of your display function to update the display.

4. What are some common ways to optimize my OpenGL+GLUT program on my Mac?

One way to optimize your program is to use vertex arrays instead of immediate mode drawing. This can significantly improve performance. You can also use the "glutSwapBuffers" function to reduce flickering and "glutTimerFunc" to control the frame rate. Additionally, avoid using expensive operations like lighting and texturing if they are not necessary for your program.

5. How can I troubleshoot compatibility issues when running my OpenGL+GLUT program on different versions of Mac OS?

To troubleshoot compatibility issues, make sure you are using the correct version of OpenGL for the Mac OS you are running. Also, check for any deprecated functions or features that may not be supported in the version you are using. If necessary, you can use conditional compilation to target specific versions of Mac OS and use alternative code for unsupported features.

Similar threads

  • Computing and Technology
Replies
2
Views
2K
  • Linear and Abstract Algebra
Replies
1
Views
3K
Replies
11
Views
4K
Replies
1
Views
7K
Back
Top