PDA

View Full Version : Lighting problem



stefkos
05-08-2012, 01:30 PM
Hello

Thats my first post here:) Hello again then:)
Ok now my problem.
I started to work on collada object loader and i stopped on lighting/materials, do you know where I can have problems? what i should fix? look on?

opengl preview: http://stefkos.amigazeux.net/car_gl.png

opengl disabled depth test preview: http://stefkos.amigazeux.net/car.png

renderer preview: http://stefkos.amigazeux.net/car_ray.png

as you see everything is ok when I'm using renderer.

Part of my code responsible for lighting:

glutInitDisplayMode (GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );

glutCreateWindow ("Collada");

glShadeModel(GL_SMOOTH);
glEnable( GL_NORMALIZE );
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glFrontFace( GL_CW ); // set front face

glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glBlendFunc(GL_ONE,GL_SRC_ALPHA);

.............


void display (void)
{
// Clear the window or more specifically the frame buffer...
// This happens by replacing all the contents of the frame
// buffer by the clear color (black in our case)

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer

#ifdef OPENGL_LIGHT
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

GLfloat light_ambient[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };

glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1.5);
glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.5);
glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.2);

GLfloat lmodel_ambient[] = { 0.9, 0.9, 0.9, 1.0 };
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
#endif





....................

void displayGLMesh( ColladaMesh *cm )
{
vector <ColladaPolygonGroup *>cp = cm->getPolygonGroups();

//mprintf("Display mesh\n");

for( uint j=0 ; j < cp.size() ; j++ )
{
ColladaPolygonGroup *cpolyg = cp[ j ];
ColladaMaterial *cmat = cpolyg->getMaterial();
ColladaEffect *ceff = NULL;

if( cmat != NULL ){ ceff = cmat->getEffect(); }
if( ceff )
{
//mprintf("Draw group with effect %s\n", ceff->getName() );
ColladaShading *cshade = ceff->getShading( 0 );//ceff->getShading( "phong" );
if( cshade )
{
#ifdef OPENGL_LIGHT
glMaterialfv( GL_FRONT, GL_AMBIENT, cshade->getAmbient() );
glMaterialfv( GL_FRONT, GL_DIFFUSE, cshade->getDiffuse() );
glMaterialfv( GL_FRONT, GL_EMISSION, cshade->getEmission() );
glMaterialfv( GL_FRONT, GL_SPECULAR, cshade->getSpecular() );
float s = cshade->getShininess();
glMaterialfv( GL_FRONT, GL_SHININESS, &s );
#else
float *x = cshade->getDiffuse();
glColor3f( x[0],x[1],x[2] );
#endif
}
}

switch( cpolyg->type )
{
case COLLADA_PTS_TRIANGLES:

Best Regards
Stefkos

thokra
05-09-2012, 10:21 AM
First of all, there is no need to disable the depth test in your case and it does not help to show your problem so the exampe is irrelevant. To really verify your model is rendered properly the depth is kind of essential. Second, your light position is a direction:



// w-component is 0 for directions
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };

// a valid point in space has a non-zero w-component - 1.0 most of the time
GLfloat light_position[] = { 1.0, 1.0, 1.0, 1.0 };


Third, how do you know your problem is in lighting? The black wholes in the model look more like missing faces or incorrect normals. You should check if your vertex and normal data is correct as well. Other than that on first sight I don't see anthing wrong with your code. Then again it's fixed function and I haven't done that in years. Maybe the others will see more.

One thing I find curious is that your vertex winding is actually set to clock-wise:


glFrontFace( GL_CW );

Why is that?

stefkos
05-09-2012, 10:36 AM
1) ok i will leave it and test other cases
2) will fix that
3) well, i probably set subject incorrect, I just meet this problem when I started to play with lighting
4)Some code left for testing.

thx for check, I will look again


Ok I found problem:

gluPerspective( 45, 1.3333, 1 , 2500 );

I had 0 as znear argument

http://stefkos.amigazeux.net/car.png -> and here how everything looks after change