Directional light
I need help with directional light in OpenGL. It doesn´t work to change the direction of a GL_SPOT_DIRECTION light. My question is maybe easy but it dosn´t hurt to be over-explicity. I do it like this:
1. First I create the light.
GLfloat LightDiffuse[] = { 0.7f, 0.7f, 0.7f, 0.7f };
GLfloat LightDirection[] = { 0.0f, 0.0f, 1.0f, 1.0f };
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, LightDirection);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
2. Then I render the scene.
glClear(GL_COLOR_BUFFER_BIT / GL_DEPTH_BUFFER_BIT);
m_fRotate++;
glMatrixMode(GL_MODELVIEW);
// Place objects in a circle around the "camera". The "camera" is located // at origin (0,0,0).
for(int i=0; i{
// Set matrix to identity
glLoadIdentity();
// Translate object to its new positon
glTranslatef(g_App.m_Object.fXPos,
g_App.m_Object.fYPos,
-g_App.m_Object.fZPos);
// Rotate around Y (m_fRotate = degree number)
if(g_App.m_Object.ucRotAx == 0)
glRotatef(m_fRotate, -1.0, 0.0, 0.0);
else if(g_App.m_Object.ucRotAx == 1)
glRotatef(m_fRotate, 0.0, -1.0, 0.0);
else if(g_App.m_Object.ucRotAx == 2)
glRotatef(m_fRotate, 0.0, 0.0, -1.0);
// Set cube position to (0,0,0) in the new matrix pos
// In this function I call glVertex3d, glBegin etc.
DrawCube(0,0,0);
}
3. Then the view is rotated. It´s here were I try to the change the direction of the light.
static float fRot = 90;
static bool bLeft = true;
static bool bPause = false;
glMatrixMode(GL_PROJECTION);
if(!bPause)
{
if(bLeft)
{
fRot++;
glRotatef( 1.0f, 0.0, 1.0, 0.0);
}
else
{
fRot–;
glRotatef( 1.0f, 0.0, -1.0, 0.0);
}
}
// Change speed every 3-5:e second
static unsigned int iLastTimeValue;
if(GetTickCount() - iLastTimeValue >=
(3000 + (unsigned int) rand() % 2000))
{
int iPauseChance = rand() % 75;
if(iPauseChance > 50)
bPause = true;
else
bPause = false;
iLastTimeValue = GetTickCount();
}
// Change from left to right if rotation angle
if(fRot >= 359.0f // fRot <= 0.0f) bLeft=!bLeft;
// This have no effect! Very strange indeed.
GLfloat LightDirection[4];
LightDirection[0] = -sinf(fRot*g_DEGTORAD); // x direction
LightDirection[2] = cosf(fRot*g_DEGTORAD); // z direction
LightDirection[1] = 0.0f;
LightDirection[3] = 1.0f;
GLfloat LightDiffuse[] = { 0.7f, 0.7f, 0.7f, 0.7f };
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, LightDirection);
glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
SwapBuffers(m_hDC);
Or maybe it´s the objects normal vectors. I rotate the objects with calls to glRotatef (see section 2) but don´t change the normal vectors.
Example:
// Front face
glBegin(GL_POLYGON);
glNormal3d( 0.0, 0.0, 1.0); // do not change!
glTexCoord2f( 1.0f, 1.0f); glVertex3d( 1.0, 1.0, 1.0+pz); // top right
glTexCoord2f( 0.0f, 1.0f); glVertex3d( -1.0, 1.0, 1.0+pz); // top left
glTexCoord2f( 0.0f, 0.0f); glVertex3d( -1.0, -1.0, 1.0+pz); // bottom left
glTexCoord2f( 1.0f, 0.0f); glVertex3d( 1.0, -1.0, 1.0+pz); // bottom right
glEnd();
I don´t know if OpenGL change the normals automaticly in light calculations.
My intention is to get the light to follow the "cameras" direction (the view is also rotating).
By the way, is GL_SPOT_DIRECTION a ordinary directional light? Or must this lightsource as well have a position?
Grateful for help.
Gandalf the White
</i>
Gandalf the White<BR>
Gandalf the Black
Hey! I need help here!
Directional Light dosn´t work on my computer!!!
Gandalf the White
Directional Light dosn´t work on my computer!!!
Gandalf the White
Gandalf the Black
The trick is to set the w-component of your light direction vector to 0.0, which creates directional light (the vector is then placed at an "infinite" direction). Instead of using GL_SPOT_DIRECTION, use ordinary GL_DIFFUSE.
There, that''s it !
Cheers
Mustard
Hey i''m also a big tolkien fan !
There, that''s it !
Cheers
Mustard
Hey i''m also a big tolkien fan !
Hey, btw, go download the redbook, it''s a VERY valuable source of information.
You''ll find the link to the downloadable pdf file in the opengl section of the developper resources.
Have fun reading it :-)
Cheers
Mustard
You''ll find the link to the downloadable pdf file in the opengl section of the developper resources.
Have fun reading it :-)
Cheers
Mustard
Colonel Mustard: could you provide me with a link where I can download the red book (in pdf format) instead of having to read it online all the time.
Thanks.
Quo
Thanks.
Quo
ColonelMustard, I dont understand what you mean. GL_DIFFUSE isn´t directional light. But I agree with you about the w-thing.
Gandalf the White
Gandalf the White
Gandalf the Black
explicit explanation
Its very simple to create directional light
what you do is this:
in your initialization routine :
// place your ambient lighting init call here
glLightfv(GL_LIGHTn, GL_DIFFUSE, &LightColor);
glLightfv(GL_LIGHTn, GL_POSITION, &LightPosition);
glEnable(GL_LIGHTn);
glEnable(GL_LIGHTING);
where LightPosition points to an array of 4 floats, the first three components being the normalized direction of your light, and the fourth set to 0 (it being the w-coordinate of your vector)
I hope i made myself clear enough
else download the pdf of the redbook
Good Luck
Mustard
PS : the link for the pdf you shall find by these means :
go to http://gamedev.net/gamedev.asp
click on the developer resources link, underneath the GDNet logo
on the right, you will have a small menu
click on OpenGL
click on the link pointing to the downloadable redbook
there ! (7 megs in size)
Its very simple to create directional light
what you do is this:
in your initialization routine :
// place your ambient lighting init call here
glLightfv(GL_LIGHTn, GL_DIFFUSE, &LightColor);
glLightfv(GL_LIGHTn, GL_POSITION, &LightPosition);
glEnable(GL_LIGHTn);
glEnable(GL_LIGHTING);
where LightPosition points to an array of 4 floats, the first three components being the normalized direction of your light, and the fourth set to 0 (it being the w-coordinate of your vector)
I hope i made myself clear enough
else download the pdf of the redbook
Good Luck
Mustard
PS : the link for the pdf you shall find by these means :
go to http://gamedev.net/gamedev.asp
click on the developer resources link, underneath the GDNet logo
on the right, you will have a small menu
click on OpenGL
click on the link pointing to the downloadable redbook
there ! (7 megs in size)
Oh sorry for the link
you go to developer resouces, THEN to the ''programming'' section, THEN you click on the opengl link and do as I told in my last post
Cheers
Mustard
you go to developer resouces, THEN to the ''programming'' section, THEN you click on the opengl link and do as I told in my last post
Cheers
Mustard
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement