lighting - working with openGL -


im trying little 3d scene opengl , include bit of lighting, im fine scene (although isnt special) , im trying add lighting give effect. can add material podium (which isnt textured) , gives me light , textured not apply material defies point of having lights. heres of code.

// setup gl_light0 gllightfv(gl_light0, gl_ambient, lightambient);     // setup ambient light gllightfv(gl_light0, gl_diffuse, lightdiffuse);     // setup diffuse light gllightfv(gl_light0, gl_specular, lightspecular);   // setup specular light  gllightf(gl_light0, gl_constant_attenuation, ca); gllightf(gl_light0, gl_linear_attenuation, la); gllightf(gl_light0, gl_quadratic_attenuation, qa);  gllightf(gl_light0, gl_spot_cutoff, 180.0f); gllightf(gl_light0, gl_spot_exponent, 0.0);  // setup gl_light1 gllightfv(gl_light1, gl_ambient, lightambient);     // setup ambient light gllightfv(gl_light1, gl_diffuse, lightdiffuse);     // setup diffuse light gllightfv(gl_light1, gl_specular, lightspecular);   // setup specular light  gllightf(gl_light1, gl_constant_attenuation, ca); gllightf(gl_light1, gl_linear_attenuation, la); gllightf(gl_light1, gl_quadratic_attenuation, qa);  gllightf(gl_light1, gl_spot_cutoff, 180.0f); gllightf(gl_light1, gl_spot_exponent, 0.0); // setup gl_light2 gllightfv(gl_light2, gl_ambient, lightambient);     // setup ambient light gllightfv(gl_light2, gl_diffuse, lightdiffuse);     // setup diffuse light gllightfv(gl_light2, gl_specular, lightspecular);   // setup specular light  gllightf(gl_light2, gl_constant_attenuation, ca); gllightf(gl_light2, gl_linear_attenuation, la); gllightf(gl_light2, gl_quadratic_attenuation, qa);  gllightf(gl_light2, gl_spot_cutoff, 180.0f); gllightf(gl_light2, gl_spot_exponent, 0.0); // setup gl_light3 gllightfv(gl_light3, gl_ambient, lightambient);     // setup ambient light gllightfv(gl_light3, gl_diffuse, lightdiffuse);     // setup diffuse light gllightfv(gl_light3, gl_specular, lightspecular);   // setup specular light  gllightf(gl_light3, gl_constant_attenuation, ca); gllightf(gl_light3, gl_linear_attenuation, la); gllightf(gl_light3, gl_quadratic_attenuation, qa);  gllightf(gl_light3, gl_spot_cutoff, 180.0f); gllightf(gl_light3, gl_spot_exponent, 0.0);  // opengl provides global ambient light component - don't want set 0 glfloat global_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f }; gllightmodelfv(gl_light_model_ambient, global_ambient); 

and render room + 3d object.

glenable(gl_lighting);     glenable(gl_light0);     glenable(gl_light1);     glenable(gl_light2);     glenable(gl_light3);      // define position , direction     // note: placing these commands after above viewing (camera) transformations means light position , direction appear have set point / direction in 3d environment.  if these commands placed before above viewing transformations, light appears move camera (as if attached camera!)     gllightfv(gl_light0, gl_spot_direction, lightdirection);     gllightfv(gl_light0, gl_position, lightposition);      gllightfv(gl_light1, gl_spot_direction, lightdirection1);     gllightfv(gl_light1, gl_position, lightposition1);      gllightfv(gl_light2, gl_spot_direction, lightdirection2);     gllightfv(gl_light2, gl_position, lightposition2);      gllightfv(gl_light3, gl_spot_direction, lightdirection3);     gllightfv(gl_light3, gl_position, lightposition3);       // setup materials objects draw     glmaterialfv(gl_front, gl_ambient, ambientmaterial);     glmaterialfv(gl_front, gl_diffuse, diffusematerial);     glmaterialfv(gl_front, gl_specular, specularmaterial);       // 2. draw scene     glpushmatrix();         drawroom();         gltranslatef(0.0,-0.75,0.0);         glrotatef(90.0, 1.0,0.0, 0.0);         glrotatef(-spin, 0.0,0.0, 1.0);         glcolor3f(1.0, 1.0, 1.0);         drawpodium();     glpopmatrix();      // save transformations prior rendering scene     glpushmatrix();          glcolor3f(0.0, 0.0, 1.0);             // setup texture state         glenable(gl_texture_2d);         gltexenvi(gl_texture_env, gl_texture_env_mode, gl_replace);          glrotatef(spin, 0.0,1.0, 0.0);         // render model          glmaterialfv(gl_front, gl_diffuse, diffusematerial2);          gltranslatef(0.0,0.8,0.0);         glrotatef(1.0,1.0,0.0,0.0);         mymodel->rendertexturedmodel();          // reset texture state         gldisable(gl_texture_2d);      // restore transformations after rendering scene     glpopmatrix(); 

and current outcome 4 lights, in square shape above helicopter looking down towards 0.0 is:

my scene

if can make out white dotted lines direction lights looking. surface of podium lit , sides of podium lit spins. rest of room , helicopter not respond light , act lighting isnt enabled hence why think because except podium textured.

you need setup gltexenv specify how lighting should work textured objects

see:

21.030 why doesn't lighting work when turn on texture mapping? http://www.opengl.org/resources/faq/technical/texture.htm

or address problem more directly, line

gltexenvi(gl_texture_env, gl_texture_env_mode, gl_replace); 

is causes behaviour. faq explains, when specify gl_replace, replace primitive lighting colour texture colour, overwrites lighting calculations. can remove line gl_modulate default behaviour.


Comments

Popular posts from this blog

jasper reports - Fixed header in Excel using JasperReports -

media player - Android: mediaplayer went away with unhandled events -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -