Lighting problems

Hi,

I’v load a object from an object file,
I used to work on wire frame mode and now I want it to have smooth shading.
But, I can’t manage to do it.My object now still looks like its having flat shading.Do not know where the problem lies.
Heres a portion the code.


// Set smooth shading mode for drawing the object
      gl.glPolygonMode (gl.GL_FRONT, gl.GL_SMOOTH);
      
      // lighting settings
      float mat_specular[]={1.0f, 1.0f, 1.0f, 1.0f};
      float mat_shininess[]={50.0f};
      float light_position[]={-2.0f, -3.0f, 4.0f, 0.0f};
      float white_light[]={1.0f, 1.0f, 1.0f, 1.0f};
      float lmodel_ambient[]={0.1f, 0.1f, 0.1f, 1.0f};
      gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
      gl.glShadeModel(gl.GL_SMOOTH);
      gl.glMaterialfv(gl.GL_FRONT, gl.GL_SPECULAR, mat_specular,0);
      gl.glMaterialfv(gl.GL_FRONT, gl.GL_SHININESS,mat_shininess,0);

      gl.glLightfv(gl.GL_LIGHT0, gl.GL_POSITION, light_position, 0);
      gl.glLightfv(gl.GL_LIGHT0, gl.GL_DIFFUSE, white_light, 1);
      gl.glLightfv(gl.GL_LIGHT0, gl.GL_SPECULAR, white_light, 1);
      gl.glLightModelfv(gl.GL_LIGHT_MODEL_AMBIENT, lmodel_ambient,2);
      
      gl.glEnable(gl.GL_LIGHTING);
      gl.glEnable(gl.GL_LIGHT0);
      gl.glEnable(gl.GL_DEPTH_TEST);
      gl.glEnable(gl.GL_COLOR_MATERIAL);

      //import the model
      model=new OBJModel(modelname,2.0f,gl,true);


I then draw the model is the display() method.


//draw the model 
drawModel(gl,drawable,gl.GL_RENDER);

Thanks for the help. :wink:

The line

gl.glPolygonMode(gl.GL_FRONT, gl.Gl_SMOOTH);

is invalid. glPolygonMode is a function that controls whether or not polygons are rendered as points, lines or solid polygons (and can then only take the mode as GL_POINT, GL_LINE, or GL_FILL).

To enable smooth vs. flat shading, you must call:

gl.glShadeModel(gl.GL_SMOOTH);

To switch back to flat shading, use GL_FLAT instead of GL_SMOOTH. When using smooth shading, rendered lines will also be smoothed shaded, so you can see gradients along the edges. It is not specific to just solid polygons.

Enabling DebugGL can be a good way of catching mistakes like this, I find.