Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Issues with lighting in OpenGL 4 (edited shaders)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 Syerjchep   Members   -  Reputation: 180

Like
0Likes
Like

Posted 03 January 2014 - 05:10 PM

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

Attached Files



Sponsor:

#2 Syerjchep   Members   -  Reputation: 180

Like
0Likes
Like

Posted 05 January 2014 - 02:47 PM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS