Jump to content
  • Advertisement
Sign in to follow this  
juanmanuelsanchez

OpenGL Lighting = black window?

This topic is 4252 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi all ! Im trying to enable lighting in my scene in opengl. The thing is that when I enable it all I get is a black window (if I disable the lightning then I can see my model). Here is the code that Im usingç
void CGL_Light3D::Init ( int light ) {
   LIGHT = light;
   // set all members to OpenGL defaults
   // all values comply with the OpenGL 1.2.1 Specification
   ambient[0] = 0.0f;
   ambient[1] = 0.0f;
   ambient[2] = 0.0f;
   ambient[3] = 1.0f;

   if ( light == GL_LIGHT0 ) {
      diffuse[0] = 5.0f;
      diffuse[1] = 1.0f;
      diffuse[2] = 2.0f;
      diffuse[3] = 1.0f;
   } else {
      diffuse[0] = 0.0f;
      diffuse[1] = 0.0f;
      diffuse[2] = 0.0f;
      diffuse[3] = 1.0f;
   }

   if ( light == GL_LIGHT0 ) {
      specular[0] = 1.0f;
      specular[1] = 1.0f;
      specular[2] = 1.0f;
      specular[3] = 1.0f;
   } else {
      specular[0] = 0.0f;
      specular[1] = 0.0f;
      specular[2] = 0.0f;
      specular[3] = 1.0f;
   }

   position[0] = 0.0f;
   position[1] = 1.0f;
   position[2] = 0.0f;
   position[3] = 0.0f;

   spot_direction[0] =  0.0f;
   spot_direction[1] =  0.0f;
   spot_direction[2] = -1.0f;

   spot_exponent = 0.0f;
   spot_cutoff = 180.0f;

   constant_attenuation  = 1.0f;
   linear_attenuation    = 0.0f;
   quadratic_attenuation = 0.0f;

   TurnOn(LIGHT);

}

void CGL_Light3D::TurnOn ( int light ) {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	LIGHT=light;
	glEnable(LIGHT);
	glLightfv(LIGHT, GL_AMBIENT, ambient);
    glLightfv(LIGHT, GL_DIFFUSE, diffuse);
    glLightfv(LIGHT, GL_SPECULAR, specular);
	

}

void CGL_Light3D::TurnOff ( int light ) {
	LIGHT=light;
	glDisable(LIGHT);

}

void CGL_Light3D::TurnOffAllLights () {
	
	glDisable(GL_LIGHTING);

}

And yes Im enabling GL_LIGHTING when I Initiate the engine. THanks for the help!

Share this post


Link to post
Share on other sites
Advertisement
1) did u send the light position?. if yes is the light facing the polygons
correctly ?.

2) did u calculate the normals for every polygon in the scene?. if yes are the normals recalculating after Scaling/... etc?.

not sure but if opengl normalize the lighting color but i guess
it should be between [0,1]. =\

take care.


Share this post


Link to post
Share on other sites
Well I use the light position that I wrote in last entry in this case
position[0] = 0.0f;
position[1] = 1.0f;
position[2] = 0.0f;
position[3] = 0.0f;

Dont know if thats correct tho...

And yes the polygons are normalized, but Im not rescaling or moving anything, I just have a model loaded into a space and a moving camera, now I want to setup the lights...

Sorry but Im very new into this

Thanks!

Share this post


Link to post
Share on other sites
did u send the light-position to opengl-context by doing this:

glLightfv(LIGHT, GL_POSITION, position);?

and try this
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,1);

and see if it works .

Share this post


Link to post
Share on other sites
try

ambient[0] = 1.0f;
ambient[1] = 1.0f;
ambient[2] = 1.0f;
ambient[3] = 1.0f;


and

diffuse[0] = 1.0f;
diffuse[1] = 1.0f;
diffuse[2] = 1.0f;
diffuse[3] = 1.0f;

for all lights GL_LIGHT0 ~ GL_LIGHTx

and tell me .

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
did you assign material properties to the objects in the scene so that they are capable of reflecting light?
as well as normals to their polygons?

Share this post


Link to post
Share on other sites
if I dont use lighting I can see the model with the texture, and yes I do normalize the faces, here is the code that does the trick.

void ASE::draw()
{
outs("ASE::draw",0);

if (loaded) {
if (currentScale != desiredScale)
setScale(desiredScale);

// Since we know how many objects our model has, go through each of them.

for (int i=0; i < ASEModel.numOfObjects; i++) {

// Make sure we have valid objects just in case. (size() is in the vector class)
if (ASEModel.pObject.size() <= 0) break;

// Get the current object that we are displaying
tASEObject *pObject = &ASEModel.pObject;

// Check to see if this object has a texture map, if so bind the texture to it.
if (pObject->bHasTexture) {

// Turn on texture mapping and turn off color
glEnable(GL_TEXTURE_2D);

// Reset the color to normal again
glColor3ub(255, 255, 255);

// Bind the texture map to the object by it's materialID
bindTexture2D(ASEModel.pMaterials[pObject->materialID].texureId);
}
else {
// Turn off texture mapping and turn on color
glDisable(GL_TEXTURE_2D);
glEnable(GL_COLOR);
glEnable(GL_COLOR_MATERIAL);

// Reset the color to normal again

glColor3ub(255, 255, 255);
}
// glTranslatef(10.0, -1.0, -80.0f);
glBegin(GL_TRIANGLES); // Begin drawing with our selected mode (triangles or lines)

// Go through all of the faces (polygons) of the object and draw them
for (int j=0; j < pObject->numOfFaces; j++) {

// Go through each corner of the triangle and draw it.
for (int whichVertex=0; whichVertex < 3; whichVertex++) {
// Get the vertex index for each point of the face
int vertIndex = pObject->pFaces[j].vertIndex[whichVertex];

// Give OpenGL the normal for this vertex.
glNormal3f(pObject->pNormals[ vertIndex ].x, pObject->pNormals[ vertIndex ].y, pObject->pNormals[ vertIndex ].z);

// If the object has a texture associated with it, give it a texture coordinate.
if (pObject->bHasTexture) {
// Make sure there was a UVW map applied to the object or else it won't have tex coords.
if (pObject->pTexVerts) {
// Get the texture coordinate index
int coordIndex = pObject->pFaces[j].coordIndex[whichVertex];

// Assign the UV coordinates to the current vertex being rendered
glTexCoord2f(pObject->pTexVerts[ coordIndex ].x, pObject->pTexVerts[ coordIndex ].y);
}
}
else {
// Make sure there is a valid material/color assigned to this object
if (ASEModel.pMaterials.size() && pObject->materialID >= 0) {
// Get and set the color that the object is, since it must not have a texture
float *pColor = ASEModel.pMaterials[pObject->materialID].fColor;
// Assign the current color to this model
glColor3f(pColor[0], pColor[1], pColor[2]);

}
}

// Pass in the current vertex of the object (Corner of current face)
glVertex3f(pObject->pVerts[ vertIndex ].x, pObject->pVerts[ vertIndex ].y, pObject->pVerts[ vertIndex ].z);
}
}
glEnd(); // End the drawing
glColor3f(0,0,0);
glDisable(GL_TEXTURE_2D);
glDisable(GL_COLOR_MATERIAL);
}
}
outs("OUT OF ASE::draw",0);
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!