PDA

View Full Version : Moving SpotLights Around



IssamTP
12-11-2014, 09:41 AM
Hi there, I'm doing a small application where the user inputs the space coordinates to move a spotlight around a sphere to get a sort of preview on how it gets lighted. The application also draws axes to have an idea about the position of the sphere in the space.


// Main drawing function
void CLuciRendGLCtrl::DrawGLScene( void )
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
// look all around my sphere to see how's the lighting
glRotated( m_vRotCamera[0], 1.0, 0.0, 0.0 );
glRotated( m_vRotCamera[1], 0.0, 1.0, 0.0 );
glRotated( m_vRotCamera[2], 0.0, 0.0, 1.0 );
DrawAxes(); // Draw the Axes in the origin.
glTranslatef( m_fTraslation_X, m_fTraslation_Y, m_fTraslation_Z );
// I want my sphere place in the same position where something much more complicated will be placed later.
DrawSphere();
// Fiat-lux
PlaceLights();
SwapBuffers( m_pDC->m_hDC );
}

void CLuciRendGLCtrl::DrawSphere( void ) const
{
GLfloat vParam[3];

// Drawing the sphere with "fancy" emerald look
vParam[0] = 0.0215f;
vParam[1] = 0.1745f;
vParam[2] = 0.0215f;
glMaterialfv( GL_FRONT, GL_AMBIENT, vParam );
vParam[0] = 0.07568f;
vParam[1] = 0.61424f;
vParam[2] = 0.07568f;
glMaterialfv( GL_FRONT, GL_DIFFUSE, vParam );
vParam[0] = 0.633f;
vParam[1] = 0.727811f;
vParam[2] = 0.633f;
glMaterialfv( GL_FRONT, GL_SPECULAR, vParam );
glMaterialf( GL_FRONT, GL_SHININESS, 0.6f * 128 );
gluSphere( m_pSphere, m_fDimSfera, 100, 100 );
}

void CLuciRendGLCtrl::PosizionaLuci( void ) const
{
// placing lights
glPushMatrix();
glLoadIdentity();
glLightfv( GL_LIGHT0, GL_DIFFUSE, m_vColLuci );
glLightfv( GL_LIGHT0, GL_POSITION, m_vPosLuci );
glLightfv( GL_LIGHT0, GL_SPOT_DIRECTION, m_vDirLuci );
//glLightf( GL_LIGHT0, GL_SPOT_CUTOFF, m_fSpotCutoff );
glLightf( GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.0 );
glPopMatrix();
}

Now, my code works gracefully (less or more) when I move the origin of my spotlight (keeping the last element of m_vPosLuci to 1.0f) but, things go bad when:


I change the direction of my lights; meaning that my light points relentlessy on the same location.
I change the color of my light, according to this (www.glprogramming.com/red/chapter05.html) book; meaning that my light is not colored at all.
I change the spot cutoff of my light; meaning that if I specify it, I get no light. That's why you see the relative line commented out.


Now given these points, I guess that I've not fully understand lights in Open GL, so: what am I doing wrong?