PDA

View Full Version : OpenGL - Ambient light working in simulator, not on device.




jshmrsn
Sep 30, 2008, 07:32 AM
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:


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);



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...

http://img213.imageshack.us/img213/1002/96233945kc6.th.jpg (http://img213.imageshack.us/my.php?image=96233945kc6.jpg)http://img213.imageshack.us/images/thpix.gif (http://g.imageshack.us/thpix.php)

Not working...

http://img510.imageshack.us/img510/5461/20435449it6.th.jpg (http://img510.imageshack.us/my.php?image=20435449it6.jpg)http://img510.imageshack.us/images/thpix.gif (http://g.imageshack.us/thpix.php)


Thanks for any help/ideas.

-Josh Rosen



SqueegyX
Sep 30, 2008, 10:45 AM
I have seen this exact same thing. Is the iphone incapable of ambient light in lighting rendering? That seems like it would be crazy.

jshmrsn
Sep 30, 2008, 04:08 PM
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:


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);

SqueegyX
Sep 30, 2008, 05:45 PM
I think its the GL_FRONT_AND_BACK that makes it work. I used to just have GL_FRONT.

jshmrsn
Sep 30, 2008, 06:46 PM
Ah, good catch.

So problem solved I guess, cool.

Frogblast
Oct 1, 2008, 01:01 AM
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.

SqueegyX
Oct 1, 2008, 11:07 AM
Yeah but its weird because it works great because it works great in the simulator. Is the simulator OpenGL, not OpenGLES?

robbieduncan
Oct 1, 2008, 11:20 AM
Yeah but its weird because it works great because it works great in the simulator. Is the simulator OpenGL, not OpenGLES?

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...