Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: Quick lighting question

Hybrid View

  1. #1
    Junior Member Newbie
    Join Date
    May 2004
    Posts
    9

    Quick lighting question

    Just learning about lighting and wondered if I was doing something wrong with my setup. I create three spotlights that shine onto a large gluSphere. Using gluLookAt I view the scene from up the Z axis, essentially from the front, and everything looks ok. I change the eye view point by decreasing the Z axis and essentialy "flip" the view when I hit the destination (origin in this case) of gluLookAt so I am viewing the rear of the sphere. Odd thing is that the spot lights are still shining on the back side of the sphere. It's as though the spehere is opaque.

    Let me know if code/screenshots are required. Thanks.

  2. #2
    Intern Contributor
    Join Date
    Mar 2004
    Location
    springfield, va [usa]
    Posts
    61

    Re: Quick lighting question

    yeah - could you please post some code [and screenies]?

    we hope to help
    :regards:

  3. #3
    Junior Member Newbie
    Join Date
    May 2004
    Posts
    9

    Re: Quick lighting question

    Here is my init and draw scene routines:

    Code :
    int DrawGLScene()                                                                   
    { 
    	// clear buffers
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    	glLoadIdentity();
     
    	// setup camera view point
    	gluLookAt(0.0f,2.0f,zoom,  0.0f,2.0f,0.0f,  0.0f,1.0f,0.0f);
     
    	// update light positions
    	glLightfv(GL_LIGHT1, GL_POSITION, lightPositionR);
    	glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, lightDirectionR);
    	glLightfv(GL_LIGHT2, GL_POSITION, lightPositionG);
    	glLightfv(GL_LIGHT2, GL_SPOT_DIRECTION, lightDirectionG);
    	glLightfv(GL_LIGHT3, GL_POSITION, lightPositionB);
    	glLightfv(GL_LIGHT3, GL_SPOT_DIRECTION, lightDirectionB);
     
    	// check key presses
    	CheckKeyPress();
     
    	// Draw ground at 0.0 Y axis
    	DrawGround();
     
    	// draw spot light positions and direction indicators
    	DrawCrossHairs(lightPositionR[0],...
    	DrawCrossHairs(lightPositionG[0],...
    	DrawCrossHairs(lightPositionB[0],...
     
    	// render and update particle fountain
    	pMgr->Render();
    	pMgr->Update(g_timer.timeStep);
     
    	// draw sphere
    	DrawSphere();
     
    	// update fps in window title bar
    	DisplayText();
     
    	// update counters
    	yrot += 0.05f;
    	if (yrot > 359.0)
    		yrot = 0.0f;
     
    	return true;
    } 
     
    int InitGL(void)                                                                        
    {
     
    	yrot = 0.0f;
     
    	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    	glClearDepth(1.0f);
     
    	glShadeModel(GL_SMOOTH);
    	glEnable(GL_DEPTH_TEST);
    	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
     
    	// load up our textures
    	if (!LoadGLTexture(PARTICLE_TEXTURE1_FILE,...
     
    	// Setup Particle Manager
    	pMgr = new ParticleManager();
     
    	// Create and setup new particle fountain
    	fount[0] = new Fountain1;
    	CreateFount();
     
    	// enable and setp lighting and materials
    	glEnable(GL_LIGHTING);
    	// meterial properties
    	glMaterialfv(GL_FRONT, GL_AMBIENT, matAmbient);
    	glMaterialfv(GL_FRONT, GL_DIFFUSE, matDiffuse);
    	glMaterialf(GL_FRONT, GL_SHININESS, 100.0f);
    	// global ambient light
    	glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
    //	glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
    //	glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
    	glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
     
    	// red spot light
    	glLightfv(GL_LIGHT1, GL_AMBIENT, ambientLightR);
    	glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuseLightR);
    	glLightfv(GL_LIGHT1, GL_SPECULAR, specularLightR);
    	glLightfv(GL_LIGHT1, GL_POSITION, lightPositionR);
    	glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 5.0f);
    	glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 100.0f);
    		FVector p1, p2, p3;
    		p1.Set(-1.0f, 2.0f, 20.0f);
    		p2.Set(-5.0f, 2.0f, -2.0f);
    		p3 = p2 - p1; p3.Normalize();
    		lightDirectionR[0] = p3.x; lightDirectionR[1] = p3.y; lightDirectionR[2] = p3.z;
    	glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, lightDirectionR);
     
    	// green spot light
    	glLightfv(GL_LIGHT2, GL_AMBIENT, ambientLightG);
    	glLightfv(GL_LIGHT2, GL_DIFFUSE, diffuseLightG);
    	glLightfv(GL_LIGHT2, GL_SPECULAR, specularLightG);
    	glLightfv(GL_LIGHT2, GL_POSITION, lightPositionG);
    	glLightf(GL_LIGHT2, GL_SPOT_CUTOFF, 5.0f);
    	glLightf(GL_LIGHT2, GL_SPOT_EXPONENT, 100.0f);
    		p1.Set(1.0f, 2.0f, 20.2f);
    		p2.Set(5.0f, 2.0f, -2.0f);
    		p3 = p2 - p1; p3.Normalize();
    		lightDirectionG[0] = p3.x; lightDirectionG[1] = p3.y; lightDirectionG[2] = p3.z;
    	glLightfv(GL_LIGHT2, GL_SPOT_DIRECTION, lightDirectionG);
     
    	// blue spot light
    	glLightfv(GL_LIGHT3, GL_AMBIENT, ambientLightB);
    	glLightfv(GL_LIGHT3, GL_DIFFUSE, diffuseLightB);
    	glLightfv(GL_LIGHT3, GL_SPECULAR, specularLightB);
    	glLightfv(GL_LIGHT3, GL_POSITION, lightPositionB);
    	glLightf(GL_LIGHT3, GL_SPOT_CUTOFF, 20.0f);
    	glLightf(GL_LIGHT3, GL_SPOT_EXPONENT, 100.0f);
    		p1.Set(0.0f, 0.0f, 6.0f);
    		p2.Set(0.0f, 5.0f, -2.0f);
    		p3 = p2 - p1; p3.Normalize();
    		lightDirectionB[0] = p3.x; lightDirectionB[1] = p3.y; lightDirectionB[2] = p3.z;
    	glLightfv(GL_LIGHT3, GL_SPOT_DIRECTION, lightDirectionB);
     
    	glEnable(GL_COLOR_MATERIAL);
    	glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
     
    	// switch on lights
    	glEnable(GL_LIGHT0);
    	glEnable(GL_LIGHT1);
    	glEnable(GL_LIGHT2);
    	glEnable(GL_LIGHT3);
     
    	return true; 
    }
    screenshots here
    http://hw001.gate01.com/sean451/tmp/f1.jpg
    http://hw001.gate01.com/sean451/tmp/b1.jpg

    Thanks a lot

  4. #4
    Member Regular Contributor
    Join Date
    Oct 2001
    Location
    Princeton, NJ
    Posts
    391

    Re: Quick lighting question

    try positioning the light before positioning the camera

  5. #5
    Junior Member Newbie
    Join Date
    May 2004
    Posts
    9

    Re: Quick lighting question

    Thanks. That seems to have fixed the spots appearing on the back of the sphere, but now my spots are all messed up. Not shining in the direction I calculate, but thats probably an issue with my direction calculations. I will work on it. Thanks again. btw. I realise I still have a bit more work to do on understanding the basics when it comes to viewing/transformations, but could you give me a brief explanation why placing the lights before the camera position worked better?

  6. #6
    Advanced Member Frequent Contributor plasmonster's Avatar
    Join Date
    Mar 2004
    Posts
    750

    Re: Quick lighting question

    Fahrenheit451,

    whenever you specify light positions, the gl assumes that you want to transform them with the model matrix. If you want your light position unaltered by he model matrix, simple load an identity matrix before setting them:

    glMatrixMode( GL_MODELVIEW );
    glPushMatrix();
    glLoadIdentity();

    // now set the lights...
    //...

    glPopMatrix();

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •