Troubleshooting OpenGL+GLUT on OSX

  • Thread starter Thread starter cscott
  • Start date Start date
  • Tags Tags
    Troubleshooting
AI Thread Summary
The discussion revolves around troubleshooting OpenGL code on OS X using Xcode and the GLUT framework. The user initially encounters a problem where the window displays a white screen instead of the expected shapes. Key solutions proposed include adding a color specification with glColor3f(1.0, 1.0, 1.0) before the drawing commands in the DrawGLScene function and including glutSwapBuffers() at the end of the redraw function to properly display the rendered content. Additionally, it was noted that a translation command was necessary to position the square and triangle correctly, preventing them from overlapping. After implementing these changes, the user successfully resolved the issues and achieved the desired output.
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:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top