NSOpenGLView code simple problem

Discussion in 'Mac Programming' started by Soulstorm, Aug 13, 2008.

  1. Soulstorm macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #1
    I have this code in a carbon application using Xcode and OpenGL with GLUT:

    Code:
    #include <Carbon/Carbon.h>
    #include <GLUT/glut.h>
    #include <OpenGL/OpenGL.h>
    
    float xRot;
    float yRot;
    
    ///////////////////////////////////////////////////////////
    // Called to draw scene
    void RenderScene(void)
    {
    	// Clear the window with current clearing color
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    	glPushMatrix();
    	
    	glColor3f(0, 1.0, 0);
    	glRotatef(xRot, 1.0f, 0.0f, 0.0f);
    	glRotatef(yRot, 0.0f, 1.0f, 0.0f);
    	
    	glBegin(GL_TRIANGLES);					// Start Drawing The Pyramid
    	
    	glColor3f(1.0f,0.0f,0.0f);			// Red
    	glVertex3f( 0.0f, 1.0f, 0.0f);			// Top Of Triangle (Front)
    	glColor3f(0.0f,1.0f,0.0f);			// Green
    	glVertex3f(-1.0f,-1.0f, 1.0f);			// Left Of Triangle (Front)
    	glColor3f(0.0f,0.0f,1.0f);			// Blue
    	glVertex3f( 1.0f,-1.0f, 1.0f);			// Right Of Triangle (Front)
    	
    	
    	glColor3f(1.0f,0.0f,0.0f);			// Red
    	glVertex3f( 0.0f, 1.0f, 0.0f);			// Top Of Triangle (Right)
    	glColor3f(0.0f,0.0f,1.0f);			// Blue
    	glVertex3f( 1.0f,-1.0f, 1.0f);			// Left Of Triangle (Right)
    	glColor3f(0.0f,1.0f,0.0f);			// Green
    	glVertex3f( 1.0f,-1.0f, -1.0f);			// Right Of Triangle (Right)
    	
    	glColor3f(1.0f,0.0f,0.0f);			// Red
    	glVertex3f( 0.0f, 1.0f, 0.0f);			// Top Of Triangle (Back)
    	glColor3f(0.0f,1.0f,0.0f);			// Green
    	glVertex3f( 1.0f,-1.0f, -1.0f);			// Left Of Triangle (Back)
    	glColor3f(0.0f,0.0f,1.0f);			// Blue
    	glVertex3f(-1.0f,-1.0f, -1.0f);			// Right Of Triangle (Back)
    	
    	glColor3f(1.0f,0.0f,0.0f);			// Red
    	glVertex3f( 0.0f, 1.0f, 0.0f);			// Top Of Triangle (Left)
    	glColor3f(0.0f,0.0f,1.0f);			// Blue
    	glVertex3f(-1.0f,-1.0f,-1.0f);			// Left Of Triangle (Left)
    	glColor3f(0.0f,1.0f,0.0f);			// Green
    	glVertex3f(-1.0f,-1.0f, 1.0f);			// Right Of Triangle (Left)
    	glEnd();
    	
    	
    
    	glPopMatrix();
        
    	glFlush();
    }
    
    
    ///////////////////////////////////////////////////////////
    // Setup the rendering state
    void SetupRC(void)
    {
    	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    	//cout << "hello! :";
    	//cout << tgaFile.width;
    	
    	glShadeModel(GL_SMOOTH);
    	glEnable(GL_DEPTH_TEST);
    	
    }
    
    
    
    ///////////////////////////////////////////////////////////
    // Called by GLUT library when the window has chanaged size
    void ChangeSize(int w, int h)
    {
        GLfloat nRange = 2.0f;
        // Prevent a divide by zero
        if(h == 0)
            h = 1;
    	
        // Set Viewport to window dimensions
        glViewport(0, 0, w, h);
    	
        // Reset coordinate system
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
    	
        // Establish clipping volume (left, right, bottom, top, near, far)
        if (w <= h) 
            glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange);
        else 
            glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange);
    	
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    }
    //void TimerFunction(int value){
    //	xRot += 0.5;
    //	yRot += 0.5;
    //	glutPostRedisplay();
    //	glutTimerFunc(30,TimerFunction,4);
    //}
    //
    void SpecialKeys(int key, int x, int y)
    {
    	//cout << "special key hit\n";
    	if(key == GLUT_KEY_UP)
    		xRot-= 5.0f;
    	
    	if(key == GLUT_KEY_DOWN)
    		xRot += 5.0f;
    	
    	if(key == GLUT_KEY_LEFT)
    		yRot -= 5.0f;
    	
    	if(key == GLUT_KEY_RIGHT)
    		yRot += 5.0f;
    	
    	if(key > 356.0f)
    		xRot = 0.0f;
    	
    	if(key < -1.0f)
    		xRot = 355.0f;
    	
    	if(key > 356.0f)
    		yRot = 0.0f;
    	
    	if(key < -1.0f)
    		yRot = 355.0f;
    	
    	// Refresh the Window
    	glutPostRedisplay();
    }
    
    
    // Program entry point
    int main(int argc, char* argv[])
    {
    	glutInit(&argc, argv);
    	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH);
    	glutInitWindowSize(800,600);
    	glutCreateWindow("OpenGL Test");
    	
    	glutReshapeFunc(ChangeSize);
    	glutDisplayFunc(RenderScene);
    	glutSpecialFunc(SpecialKeys);
    	//glutTimerFunc(33, TimerFunction, 1);
    	SetupRC();
    	
    	glutMainLoop();
    	
    	SetupRC();
    	return 0;
    }
    
    While porting this code to Cocoa OpenGL using an NSOpenGLView, my code looks something like this:

    Code:
    #import "MyOpenGLView.h"
    
    @implementation MyOpenGLView
    
    - (void)prepareOpenGL
    {
    	yRot = 0.0;
    	xRot = 0.0;
    	
    	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    	//cout << "hello! :";
    	//cout << tgaFile.width;
    	
    	glShadeModel(GL_SMOOTH);
    	glEnable(GL_DEPTH_TEST);
    }
    
    - (void)reshape
    {
    	//NSLog(@"reshaping...");
    	[[self openGLContext]makeCurrentContext];
    	NSRect bounds = [self bounds];
    	//NSLog(@"reshaping: %f, %f", NSWidth(bounds), NSHeight(bounds));
    	glViewport(0, 0, NSWidth(bounds), NSHeight(bounds));
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
    	glOrtho(-2, 2, -2, 2, -2, 2);
    	glMatrixMode(GL_MODELVIEW);
    	glLoadIdentity();
    	
    	[NSOpenGLContext clearCurrentContext];
    }
    
    - (void)drawRect:(NSRect)rect
    {
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    	glPushMatrix();
    	
    	glColor3f(0, 1.0, 0);
    	glRotatef(xRot, 1.0f, 0.0f, 0.0f);
    	glRotatef(yRot, 0.0f, 1.0f, 0.0f);
    	
    	glBegin(GL_TRIANGLES);					// Start Drawing The Pyramid
    		glColor3f(1.0f,0.0f,0.0f);			// Red
    		glVertex3f( 0.0f, 1.0f, 0.0f);			// Top Of Triangle (Front)
    		glColor3f(0.0f,1.0f,0.0f);			// Green
    		glVertex3f(-1.0f,-1.0f, 1.0f);			// Left Of Triangle (Front)
    		glColor3f(0.0f,0.0f,1.0f);			// Blue
    		glVertex3f( 1.0f,-1.0f, 1.0f);			// Right Of Triangle (Front)
    		
    		
    		glColor3f(1.0f,0.0f,0.0f);			// Red
    		glVertex3f( 0.0f, 1.0f, 0.0f);			// Top Of Triangle (Right)
    		glColor3f(0.0f,0.0f,1.0f);			// Blue
    		glVertex3f( 1.0f,-1.0f, 1.0f);			// Left Of Triangle (Right)
    		glColor3f(0.0f,1.0f,0.0f);			// Green
    		glVertex3f( 1.0f,-1.0f, -1.0f);			// Right Of Triangle (Right)
    		
    		glColor3f(1.0f,0.0f,0.0f);			// Red
    		glVertex3f( 0.0f, 1.0f, 0.0f);			// Top Of Triangle (Back)
    		glColor3f(0.0f,1.0f,0.0f);			// Green
    		glVertex3f( 1.0f,-1.0f, -1.0f);			// Left Of Triangle (Back)
    		glColor3f(0.0f,0.0f,1.0f);			// Blue
    		glVertex3f(-1.0f,-1.0f, -1.0f);			// Right Of Triangle (Back)
    		
    		glColor3f(1.0f,0.0f,0.0f);			// Red
    		glVertex3f( 0.0f, 1.0f, 0.0f);			// Top Of Triangle (Left)
    		glColor3f(0.0f,0.0f,1.0f);			// Blue
    		glVertex3f(-1.0f,-1.0f,-1.0f);			// Left Of Triangle (Left)
    		glColor3f(0.0f,1.0f,0.0f);			// Green
    		glVertex3f(-1.0f,-1.0f, 1.0f);			// Right Of Triangle (Left)
    	glEnd();
    	
    	glPopMatrix();
    	
    	[[self openGLContext]flushBuffer];
    }
    
    - (IBAction)rotateUp:(id)sender
    {
    	xRot-= 5.0f;
    	if(xRot < 1.0f)
    		xRot = 355.0f;
    	[self setNeedsDisplay:YES];
    }
    - (IBAction)rotateDown:(id)sender
    {
    	xRot += 5.0f;
    	if(xRot > 356.0f)
    		yRot = 0.0f;
    	[self setNeedsDisplay:YES];
    }
    - (IBAction)rotateLeft:(id)sender
    {
    	yRot -= 5.0f;
    	if(yRot < 1.0f)
    		yRot = 355.0f;
    	[self setNeedsDisplay:YES];
    }
    - (IBAction)rotateRight:(id)sender
    {
    	yRot += 5.0f;
    	if(yRot > 356.0f)
    		yRot = 0.0f;
    	[self setNeedsDisplay:YES];
    }
    
    - (void) dealloc
    {
    	[texture1 release];
    	[super dealloc];
    }
    
    @end
    However, using GLUT I get the expected result, which is a multicolored pyramid ,like the left one. But on the right, I get the same pyramid, only that you can see through some of the faces. I don't know why this is happening... Wasn't it supposed to look the same when I rotate the images? Note that the fault into the second project can only be seen when you rotate the object.
     

    Attached Files:

  2. lazydog macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #2
    Looks like you haven't got a depth buffer assigned to your view.

    b e n
     
  3. Soulstorm thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #3
    Damn. It was so easy! I went into the interface builder and set a depth buffer (16 bits). That solved the problem. Thanks a lot.
     

Share This Page