Yattabyte
Yattabyte

Reputation: 1480

Lighting not appearing in OpenGL

I've been trying to implement lighting for my OpenGL project, but so far I haven't been able to get any lighting effects.

I know that many things could go wrong, so I've tried hardcoding my normals (and using only a few triangles), but still I don't get anything.

In my scene, I have a few objects laying down along the "ground" at 0,0,0, hoping to see light coming down from above on them. They have a very basic texture on them which would easily show if they were lit up or not.

I initialize my scene like this:

glEnable(GL_CULL_FACE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glShadeModel(GL_SMOOTH);
glEnable(GL_COLOR_MATERIAL);

glTranslatef(0,0,0);
GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 0.2f };
GLfloat diffuseLight[] = { 0.8f, 0.8f, 0.8, 1.0f };
GLfloat specularLight[] = { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat position[] = {0, 100.0f, 0, 1.0f };

glLightModelfv(GL_AMBIENT, ambientLight);
glLightModelfv(GL_DIFFUSE, ambientLight);
glLightModelfv(GL_SPECULAR, specularLight);
glLightModelfv(GL_POSITION, position);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
glLightfv(GL_LIGHT0, GL_POSITION, position);

glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE );

GLfloat shine [] = {50};
GLfloat diffuse[] = {1.0f, 0.f, 0.f, 1.0f};
GLfloat spec[] = {1.0f, 0.f, 0.f, 1.0f};
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE,diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR,spec);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS,shine);

And when I go to render:

    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, tiles[x]->image.width(), tiles[x]->image.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, tiles[x]->image.bits());
    glNormal3d(0,1,0); //Will eventually calculate       
    glDrawArrays(GL_TRIANGLES, 0, 3);
    glDrawArrays(GL_TRIANGLES, 3, 3);

"Tiles" is just a class of mine that holds an image and corresponding vectors for my objects. The "vertices" that are called is just a list of vectors (so each object has 6 vectors = 2 triangles.

My fragment shader:

uniform sampler2D texture;
in vec4 gl_Color;
in vec2 varyingTextureCoordinate;   

out vec4 fragColor;

void main(void)
{
    fragColor = texture2D(texture, varyingTextureCoordinate);
}

And my vertex shader:

uniform mat4 mvpMatrix;

in vec4 vertex;
in vec2 textureCoordinate;

out vec2 varyingTextureCoordinate;

void main(void)
{
    varyingTextureCoordinate = textureCoordinate;
    gl_Position = mvpMatrix * vertex;
}

Is my lack of lighting due to a conflict with the texture? No matter how I change that normal, I don't get any lighting.

Upvotes: 0

Views: 584

Answers (1)

hhanesand
hhanesand

Reputation: 1000

I think you're trying to combine pre-programmable shader opengl and shader open gl here. I recommend learning how to implement lighting by utilizing as much shader code as possible

BennyBox has a great youtube series on OpenGL covering shaders and engine design

Here is his git repo and the java version.

Upvotes: 3

Related Questions