Issues with lighting in OpenGL 4 (edited shaders)

Started by
0 comments, last by Syerjchep 10 years, 3 months ago
So I've recently started working on OpenGL again.
This time I figured I'd try working with OpenGL 4 and shaders instead of the old OpenGL.
I'm learning, but I can't seem to get my lights working quite right.
Interestingly I've made a model loader (not pictured) and lights seem to work better on them than the terrain.
However the issue with my position changing the way the light appears (see below) is still a bit of a problem.

This is how I put my terrain into vertex buffer objects for rendering:
struct tmpVec3
{
    float x,y,z;
};
 
tmpVec3 cross(float x1,float y1,float z1,float x2,float y2,float z2)
{
    tmpVec3 ret;
    ret.x = (y1*z2) - (z1*y2);
    ret.y = (z1*x2) - (x1*z2);
    ret.z = (x1*y2) - (y1*x2);
    return ret;
}
 
tmpVec3 normalize(tmpVec3 in)
{
    float length;
    length = sqrt(pow(in.x,2)+pow(in.y,2)+pow(in.z,2));
    in.x /= length;
    in.y /= length;
    in.z /= length;
    return in;
}
 
void recompileHeightmap()
{
    terrainVerts.empty();
    terrainUVs.empty();
    terrainNormals.empty();
 
    for(int x = 0; x<heightMapSize-1; x++)
    {
        for(int z = 0; z<heightMapSize-1; z++)
        {
 
            glm::vec3 temp1(x,(float)heightMap[x][z]/10,z);
            glm::vec3 temp2(x,(float)heightMap[x][z+1]/10,z+1);
            glm::vec3 temp3(x+1,(float)heightMap[x+1][z]/10,z);
 
            glm::vec3 temp4(x+1,(float)heightMap[x+1][z+1]/10,z+1);
            glm::vec3 temp5(x+1,(float)heightMap[x+1][z]/10,z);
            glm::vec3 temp6(x,(float)heightMap[x][z+1]/10,z+1);
 
            terrainVerts.push_back(temp1);
            terrainVerts.push_back(temp2);
            terrainVerts.push_back(temp3);
            terrainVerts.push_back(temp4);
            terrainVerts.push_back(temp5);
            terrainVerts.push_back(temp6);
            int randNum = rand() % 4;
 
            if(randNum == 0)
            {
                glm::vec2 uv1(0,0);
                glm::vec2 uv2(0,1);
                glm::vec2 uv3(1,0);
 
                glm::vec2 uv4(1,1);
                glm::vec2 uv5(1,0);
                glm::vec2 uv6(0,1);
 
                terrainUVs.push_back(uv1);
                terrainUVs.push_back(uv2);
                terrainUVs.push_back(uv3);
                terrainUVs.push_back(uv4);
                terrainUVs.push_back(uv5);
                terrainUVs.push_back(uv6);
            }
            else if(randNum == 1)
            {
                glm::vec2 uv1(1,1);
                glm::vec2 uv2(1,0);
                glm::vec2 uv3(0,1);
 
                glm::vec2 uv4(1,1);
                glm::vec2 uv5(1,0);
                glm::vec2 uv6(0,1);
 
                terrainUVs.push_back(uv1);
                terrainUVs.push_back(uv2);
                terrainUVs.push_back(uv3);
                terrainUVs.push_back(uv4);
                terrainUVs.push_back(uv5);
                terrainUVs.push_back(uv6);
            }
            else if(randNum == 2)
            {
                glm::vec2 uv1(0,0);
                glm::vec2 uv2(0,1);
                glm::vec2 uv3(1,0);
 
                glm::vec2 uv4(1,0);
                glm::vec2 uv5(0,1);
                glm::vec2 uv6(1,1);
 
                terrainUVs.push_back(uv1);
                terrainUVs.push_back(uv2);
                terrainUVs.push_back(uv3);
                terrainUVs.push_back(uv4);
                terrainUVs.push_back(uv5);
                terrainUVs.push_back(uv6);
            }
            else
            {
                glm::vec2 uv1(1,1);
                glm::vec2 uv2(1,0);
                glm::vec2 uv3(0,1);
 
                glm::vec2 uv4(1,1);
                glm::vec2 uv5(1,0);
                glm::vec2 uv6(0,1);
 
                terrainUVs.push_back(uv1);
                terrainUVs.push_back(uv2);
                terrainUVs.push_back(uv3);
                terrainUVs.push_back(uv4);
                terrainUVs.push_back(uv5);
                terrainUVs.push_back(uv6);
            }
        }
    }
 
    for(int i = 0; i<terrainVerts.size(); i+=3)
    {
        glm::vec3 first = terrainVerts[i];
        glm::vec3 second = terrainVerts[i+1];
        glm::vec3 third = terrainVerts[i+2];
 
        float v1x = second.x - first.x;
        float v1y = second.y - first.y;
        float v1z = second.z - first.z;
        float v2x = third.x - first.x;
        float v2y = third.y - first.y;
        float v2z = third.z - first.z;
 
        tmpVec3 ret = normalize(cross(v1x,v1y,v1z,v2x,v2y,v2z));
 
        glm::vec3 normal(ret.x,ret.y,ret.z);
 
        terrainNormals.push_back(normal);
        terrainNormals.push_back(normal);
        terrainNormals.push_back(normal);
    }
 
    glGenVertexArrays(1,&terrainVAO);
    glBindVertexArray(terrainVAO);
 
    glBindTexture(GL_TEXTURE_2D, terrainTextureSand);
 
    glGenBuffers(1, &terrainVertBuffer);
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, terrainVertBuffer);
    glBufferData(GL_ARRAY_BUFFER, terrainVerts.size() * sizeof(glm::vec3), &terrainVerts[0], GL_STATIC_DRAW);
    glVertexAttribPointer(
            0,                  // attribute
            3,                  // size
            GL_FLOAT,           // type
            GL_FALSE,           // normalized?
            0,                  // stride
            (void*)0            // array buffer offset
    );
 
    glGenBuffers(1, &terrainUVBuffer);
    glEnableVertexAttribArray(1);
    glBindBuffer(GL_ARRAY_BUFFER, terrainUVBuffer);
    glBufferData(GL_ARRAY_BUFFER, terrainUVs.size() * sizeof(glm::vec2), &terrainUVs[0], GL_STATIC_DRAW);
    glVertexAttribPointer(
            1,                  // attribute
            2,                  // size
            GL_FLOAT,           // type
            GL_FALSE,           // normalized?
            0,                  // stride
            (void*)0            // array buffer offset
    );
 
    glGenBuffers(1, &terrainNormalBuffer);
    glEnableVertexAttribArray(2);
    glBindBuffer(GL_ARRAY_BUFFER, terrainNormalBuffer);
    glBufferData(GL_ARRAY_BUFFER, terrainNormals.size() * sizeof(glm::vec3), &terrainNormals[0], GL_STATIC_DRAW);
    glVertexAttribPointer(
            2,                                // attribute
            3,                                // size
            GL_FLOAT,                         // type
            GL_FALSE,                         // normalized?
            0,                                // stride
            (void*)0                          // array buffer offset
    );
 
}
Here's the result:
Blue_Light_One.png
Yellow_Light1.png
Both_Light1.png
Even if I set all the heights to 0 and make a completely flat landscape, it still doesn't work right.
flat_Yellow1.png
If I back up a bit, it gets brighter.
flat_Yellow2.png
For some reason my position has an effect. I was wondering if anyone could help look in my shaders and figure out where I went wrong.

tl;dr what did I do wrong in my shaders that makes the light get brighter the further away I am from it
and why is the terrain being lit up so odd
Advertisement

I edited the shaders a bit.

Removed specular lighting and added a transform variable rather than just moving the view matrix around for each model.

Still really need help though.

This topic is closed to new replies.

Advertisement