OpenGL - Ambient light working in simulator, not on device.

Discussion in 'iPhone/iPad Programming' started by jshmrsn, Sep 30, 2008.

  1. macrumors member

    Joined:
    Jul 26, 2008
    #1
    My implementation of OpenGL displays ambient light normally while running in the iPhone OS simulator, but acts as if ambient light color was 0,0,0 on the device.

    I activate lighting like so:
    Code:
    
            glEnable(GL_LIGHTING);
    	glEnable(GL_LIGHT0);     	     
    
    	GLfloat ambient[] = {0.6, 0.6, 0.6, 1.0 };
    	GLfloat diffuse[] = { 1, 1, 1, 1 };
    	GLfloat specular[] = { 0,0, 0, 1.0 };
    
    	glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
    	glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
    	glLightfv( GL_LIGHT0, GL_SPECULAR, specular);
    
    	GLfloat position[] = { direction.x, direction.y, direction.z, 0.0 };
    	glLightfv(GL_LIGHT0, GL_POSITION, position);
    
    Code:
    		GLfloat amb[] = { 1,1, 1, 1.0 };
    		GLfloat diffuse[] = { 1, 1, 1, 1.0 }; 	
    		glMaterialfv(GL_FRONT, GL_AMBIENT, amb);
    		glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse);
    
    Working...

    [​IMG][​IMG]

    Not working...

    [​IMG][​IMG]


    Thanks for any help/ideas.

    -Josh Rosen
     
  2. macrumors regular

    Joined:
    Mar 24, 2008
    #2
    I have seen this exact same thing. Is the iphone incapable of ambient light in lighting rendering? That seems like it would be crazy.
     
  3. thread starter macrumors member

    Joined:
    Jul 26, 2008
    #3
    Well I'm not exactly sure what fixed it, but by strictly following this tutorial I was able to get ambient lighting to work.

    http://www.zeuscmd.com/tutorials/opengles/15-Lighting.php

    my code is now:

    Code:
    	float lightAmbient[] = { 0.3f, 0.3f, 0.3f, 1.0f };
    	float lightDiffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
    
    	float matAmbient[] = { 0.6f, 0.6f, 0.6f, 1.0f };
    	float matDiffuse[] = { 0.6f, 0.6f, 0.6f, 1.0f };
    
    	glEnable(GL_LIGHTING);
    	glEnable(GL_LIGHT0);     	     
    
    	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, matAmbient);
    	glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, matDiffuse);
    
    	glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
    	glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse);
    
    
    	//using my vector class
    	direction.unit();	
    	GLfloat position[] = { direction.x, direction.y, direction.z, 0.0 };
    	glLightfv(GL_LIGHT0, GL_POSITION, position);
    
     
  4. macrumors regular

    Joined:
    Mar 24, 2008
    #4
    I think its the GL_FRONT_AND_BACK that makes it work. I used to just have GL_FRONT.
     
  5. thread starter macrumors member

    Joined:
    Jul 26, 2008
    #5
    Ah, good catch.

    So problem solved I guess, cool.
     
  6. macrumors newbie

    Joined:
    Sep 17, 2008
    #6
    If you had been checking for errors, an OpenGL ES implementation should reject the GL_FRONT value with a GL_INVALID_ENUM error.

    Always use glGetError() to debug these kind of problems.
     
  7. macrumors regular

    Joined:
    Mar 24, 2008
    #7
    Yeah but its weird because it works great because it works great in the simulator. Is the simulator OpenGL, not OpenGLES?
     
  8. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    Lots of things work in the simulator that don't work on a real device. This is because the simulator has access to the full-on Mac OSX libraries rather than the limited Mobile OSX ones...
     

Share This Page