PDA

View Full Version : OpenGL Diffuse Lighting bug/question



kuronie
11-09-2016, 06:29 AM
I am currently doing a flashlight/torchlight using diffuse lighting.
my lighting on chair and table is working. [can't post url somehow]


But when i shift away and faces my skybox, it turns pitch black with no lighting.

my lighting code:

//Torchlight
glLightfv(GL_LIGHT7, GL_DIFFUSE, gTorchlightColor);
glLightfv(GL_LIGHT7, GL_SPECULAR, gSpecularMaterial);
glLightf(GL_LIGHT7, GL_SPOT_CUTOFF, 10.0f);
glLightf(GL_LIGHT7, GL_SPOT_EXPONENT, 50.0f);


skybox code:

void skybox2()
{
glPushMatrix();
GLfloat properties[] = { 0.2f, 0.2f, 0.2f, 1.0f };
//
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, properties);
glBindTexture(GL_TEXTURE_2D, skyboxtexture[FRONT].id);
//FRONT
glBegin(GL_QUADS);
glNormal3f(1, 0, 0);
glTexCoord2f(0, 0); glVertex3f(100, -25, -75); //bottom left
glTexCoord2f(0, 1); glVertex3f(100, 75, -75); //upper left
glTexCoord2f(1, 1); glVertex3f(100, 75, 75); // upper right
glTexCoord2f(1, 0); glVertex3f(100, -25, 75); //bottom right
glEnd();

glBindTexture(GL_TEXTURE_2D, skyboxtexture[RIGHT].id);
//RIGHT
glBegin(GL_QUADS);
glNormal3f(0, 0, 1);
glTexCoord2f(0, 0); glVertex3f(100, -25, 75); //bottom left
glTexCoord2f(0, 1); glVertex3f(100, 75, 75); //upper left
glTexCoord2f(1, 1); glVertex3f(-100, 75, 75); //upper right
glTexCoord2f(1, 0); glVertex3f(-100, -25, 75); // bottom right

glEnd();

glBindTexture(GL_TEXTURE_2D, skyboxtexture[BACK].id);
//BACK
glBegin(GL_QUADS);
glNormal3f(-1, 0, 0);
glTexCoord2f(0, 0); glVertex3f(-100, -25, 75); //bottom left
glTexCoord2f(0, 1); glVertex3f(-100, 75, 75); //upper left
glTexCoord2f(1, 1); glVertex3f(-100, 75, -75); //upper right
glTexCoord2f(1, 0); glVertex3f(-100, -25, -75); //bottom right
glEnd();

glBindTexture(GL_TEXTURE_2D, skyboxtexture[LEFT].id);
//LEFT
glBegin(GL_QUADS);
glNormal3f(0, 0, -1);
glTexCoord2f(0, 0); glVertex3f(-100, -25, -75); //bottom left
glTexCoord2f(0, 1); glVertex3f(-100, 75, -75); //upper left
glTexCoord2f(1, 1); glVertex3f(100, 75, -75); //upper right
glTexCoord2f(1, 0); glVertex3f(100, -25, -75); //bottom right
glEnd();

glBindTexture(GL_TEXTURE_2D, skyboxtexture[BOTTOM].id);
//BOTTOM
glBegin(GL_QUADS);
glNormal3f(0, -1, 0);
glTexCoord2f(0, 0); glVertex3f(-100, -25, -75); //bottom left
glTexCoord2f(0, 1); glVertex3f(100, -25, -75); //upper left
glTexCoord2f(1, 1); glVertex3f(100, -25, 75); //upper right
glTexCoord2f(1, 0); glVertex3f(-100, -25, 75); //bottom right
glEnd();

glBindTexture(GL_TEXTURE_2D, skyboxtexture[TOP].id);
//TOP
glBegin(GL_QUADS);
glNormal3f(0, -1, 0);
glTexCoord2f(0, 0); glVertex3f(-100, 75, -75); //bottom left
glTexCoord2f(0, 1); glVertex3f(100, 75, -75); //upper left
glTexCoord2f(1, 1); glVertex3f(100, 75, 75); //upper right
glTexCoord2f(1, 0); glVertex3f(-100, 75, 75); //bottom right
glEnd();

glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, gNone);
glPopMatrix();

glBindTexture( GL_TEXTURE_2D, 0 );
}


I am out of ideas for this situation, can anyone provide me guidance? Thanks a lot!

Silence
11-09-2016, 10:50 PM
If the chair and the table are lit correctly, then I would suggest you to use smaller triangles for your skybox.

kuronie
11-09-2016, 11:23 PM
If the chair and the table are lit correctly, then I would suggest you to use smaller triangles for your skybox.

Hmm, i have no idea why my ambient light is able to work, but diffuse lighting on it doesn't.

kuronie
11-09-2016, 11:39 PM
What does it mean when only I point towards corner of each quads, then it lights up with the forchlight effect? If I shift away from the corners, the light is gone.

Silence
11-09-2016, 11:56 PM
As I just said, split your quads into smaller ones (or into small triangles). This will work far better.

Silence
11-09-2016, 11:57 PM
Hmm, i have no idea why my ambient light is able to work, but diffuse lighting on it doesn't.

Ambient light applies the same everywhere. It does not depend on distance neither any angles.

kuronie
11-10-2016, 12:57 AM
As I just said, split your quads into smaller ones (or into small triangles). This will work far better.

So I will need to subdivide my quads? Can I get any tips/idea on it? Can't really seem to understand subdividing a quad.

Silence
11-10-2016, 01:10 AM
Can't really seem to understand subdividing a quad.

Well, that's just basic mathematics / logics.

Just have a function that draw a 1*1 quad from a given position, and call this function as much as you need, with incrementing your x and y position. Do the same for the other dimensions.

kuronie
11-10-2016, 01:58 AM
Well, that's just basic mathematics / logics.

Just have a function that draw a 1*1 quad from a given position, and call this function as much as you need, with incrementing your x and y position. Do the same for the other dimensions.

Stuck doing this approach right now. Can you give me an example?

Silence
11-10-2016, 05:06 AM
Come on. It's just a matter of doing some loops over what you did.

kuronie
11-10-2016, 05:35 AM
Come on. It's just a matter of doing some loops over what you did.

I am sorry for being a nuisance . Managed to do it, but the texcoord loop is giving me problem now.

Silence
11-10-2016, 05:56 AM
Managed to do it, but the texcoord loop is giving me problem now.

Perfect !

For the texture coordinates, it should vary between 0 and 1 over the whole quad. You would generally do something like:



tex_coord = relative_current_position_of_vertex / overall_size


And for both directions.

You can post your code so that people can check.

kuronie
11-10-2016, 06:21 AM
Perfect !

For the texture coordinates, it should vary between 0 and 1 over the whole quad. You would generally do something like:



tex_coord = relative_current_position_of_vertex / overall_size


And for both directions.



I am super lost at this right now.

how do I go about mapping to it, i am lost. Tried many ways but it just mapping all over.

for (int z = 0; z < 75; ++z)
{
for (int y = 0; y < 25; ++y)
{
glTexCoord2f(1, 0);
glNormal3f(-1, 0, 0); glVertex3f(100, -y, z); //bottom right
glTexCoord2f(1, 1);
glNormal3f(-1, 0, 0); glVertex3f(100, z, z); // upper right
glTexCoord2f(0, 1);
glNormal3f(-1, 0, 0); glVertex3f(100, z, -z); //upper left
glTexCoord2f(0, 0);
glNormal3f(-1, 0, 0); glVertex3f(100, -y, -z); //bottom left
}
}

kuronie
11-12-2016, 01:29 AM
Perfect !

For the texture coordinates, it should vary between 0 and 1 over the whole quad. You would generally do something like:



tex_coord = relative_current_position_of_vertex / overall_size


And for both directions.

You can post your code so that people can check.

Okay. New update, after postponing the quad for awhile, went to do other small stuff like blending, I MANAGED TO DO IT TODAY!


for (int i = 0; i < subDiv; ++i)
{
for (int j = 0; j < subDiv; ++j)
{
glTexCoord2f((i*unitUV) + unitUV, j * unitUV);
glNormal3f(-1, 0, 0); glVertex3f(100, -25 + (j * testY), -75 + (i + 1) * testZ); //bottom right
glTexCoord2f((i*unitUV) + unitUV, (j*unitUV) + unitUV);
glNormal3f(-1, 0, 0); glVertex3f(100, -25 + ((j + 1) * testY), -75 + (i + 1) * testZ); // upper right
glTexCoord2f(i * unitUV, (j*unitUV) + unitUV);
glNormal3f(-1, 0, 0); glVertex3f(100, -25 + ((j + 1) * testY), -75 + (i)*testZ); //upper left
glTexCoord2f(i*unitUV, j*unitUV);
glNormal3f(-1, 0, 0); glVertex3f(100, -25 + (j * testY), -75 + (i)*testZ); //bottom left
}
}

Now the texture is mapped in one piece! I want to thank you for helping me, really appreciate it!