Author Topic: Anyone decent with OpenGL? --- V3 Shaders uploaded 8:28 am est  (Read 1959 times)

I made this topic on gamedev.net, but if I do get a responce, it could take days:
http://www.gamedev.net/topic/651856-issues-with-lighting-in-opengl-4/

Basically I'm making a game that uses OpenGL 4 and I've made some stuff including a heightmap loader and a model loader that seem to work fine.
Now my next hurdle is making convincing lighting.

I'm prepared to give up whatever code could assist a potential helper, but for now I've just posted the two shaders themselves.
Also, on that link is my heightmap loading function.

Basically my problem is that the lights just aren't working on the terrain. (They do better with models, but problems are still present)

A light (the cube) close to terrain.

A further away look at the same. The light appears much brighter.

For comparison and contrast here is a blue and yellow light a bit above the terrain:

I have no idea what the forget is going on here. But the yellow light seems to be doing a bit better, though I doubt it has to do with its color.
« Last Edit: January 04, 2014, 07:28:27 AM by DrenDran »

your pics are broke dawg. use imgur

FlatYellow2 ftfy:
[img]http://s24.postimg.org/w3vw372zn/flat_Yellow2.png[ /img]
FlatYellow1 ftfy:
[img]http://s24.postimg.org/sbcdngnhf/flat_Yellow1.png[ /img]
Those are the correct links
http://s24.postimg.org/w3vw372zn/flat_Yellow2.png
http://s24.postimg.org/sbcdngnhf/flat_Yellow1.png
« Last Edit: January 03, 2014, 06:51:40 PM by LagoomSack »

Did you try restarting your computer.

Ah, they were working for me.
Replaced with imgur links.

Did you try restarting your computer.
This loving guy.

Anyway, idk why but I was hoping someone like port would come along and just be able to spot the problem in two seconds.
Assuming the problem is with the shaders.

are the normals set up properly

are the normals set up properly
The normals for the models (not shown) are taken from whatever Blender (or whatever model making program is being used) exports.
For the most part the effect of light on them seems alright, though it still has that odd changing as you move factor.

The code for the terrain is here:
Code: [Select]
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
    );
 
}


C++ right?
The code above is.
The shaders are in GLSL.
Based on the syntax of C, however.

Anyway, I ran some tests and a flat surface exported by blender did better at displaying lights at first, but ultimately showed the same issues.
Your position effects the position and intensity of the lights.

« Last Edit: January 03, 2014, 07:20:41 PM by Blockzillahead »

I'm jealous of the fact that you even comprehend OpenGL.
I tried it so many times and just hit dead ends while trying to understand it.

Port would probably help you here.

http://www.opengl.org/sdk/docs/man2/xhtml/glNormal.xml

You could look more into it because I'm not sure how to exactly use it but it's what ottosan was talking about.

http://gamedev.stackexchange.com/questions/50653/opengl-why-do-i-have-to-set-a-normal-with-glnormal
I know what a normal is and I know what they should be.
I mean, just look at the code I posted above and you should at least see it tries to get normals.
But thanks for trying anyway. Also those are functions from the deprecated opengl 1 and 2 fixed function pipeline but i wouldn't expect anyone here to know what that means

I guess I'll just try rewriting the shaders all over again.
One of the problems here is that I wrote them so long ago that I've got about as good idea about what they are as any of you do.

What exactly are you making. Is it a game?

What exactly are you making. Is it a game?
Was your last post on this topic serious?

What exactly are you making. Is it a game?
Blueblur at his finest
Basically I'm making a game that uses OpenGL 4