2
votes

I've, the last month, been following tutorials on a various pages (learnopengl.com) amongst others.
I got to the lighting section (I created a loader to load meshes with Assimp first), my fragmentshader code is pretty much the same as the tutorials lighting ( learnopengl.com/basic_lighting).

Frag code:

#version 130

out vec4 outColor;

in vec3 fragPos;
in vec3 normal;
in vec2 texcoord;

uniform sampler2D tex;
uniform vec3 lightPos;
uniform vec3 lightColor;

void main(){
    float ambientStrength = 0.1f;
    vec3 ambient = ambientStrength * lightColor;

    //Diffuse
    vec3 norm = normalize(normal);
    vec3 lightDir = normalize(lightPos - fragPos);
    float diff = max(dot(norm, lightDir), 0.0f);
    vec3 diffuse = diff * lightColor;
    float objectColor = 0.5f;
    //texture(tex, texcoord)
    vec3 result = (ambient + diffuse) * objectColor;
    outColor = vec4(result, 1.0f);
}

When, for example, lighting a plane it gives me these results:

OpenGLplane_light_distortions

When I'm not lighting the plane it's plain grey.

Here's the .obj file or the plane:

Blender v2.69 (sub 0) OBJ File: 'plane.blend'
# www.blender.org
mtllib plane.mtl
o Plane
v 10.000000 0.000000 10.000000
v -10.000000 0.000000 10.000000
v 10.000000 0.000000 -10.000000
v -10.000000 0.000000 -10.000000
vn 0.000000 1.000000 0.000000
usemtl None
s off
f 2//1 1//1 3//1
f 4//1 2//1 3//1

It get's imported correctly, every vertex normal is { 0, 1, 0} and the indices are also correct.

When searching for things like "OpenGL light disortions" or just "OpenGL lighting" I can't find anything like this. So I really have no idea what's wrong.

Oh yeah, I use openGL 3.3, and I have an integrated intel GPU. CPU is intel core i3-2367M in case there is a problem with the hardware.

Thank you for your time :D

EDIT:

So I just tried to show the color of my normals on the plane, they should be solid green but they are this abomination:

OpenGL disortion Normals

I'll also dump my vertexshadercode in here:

#version 130
in vec3 position;
in vec3 normal;
in vec2 texCoord;


out vec3 fragPos;
out vec3 Normal;
out vec2 texcoord;

uniform mat4 transform;
uniform mat4 view;
uniform mat4 projection;

void main(){
    texcoord = texCoord;
    Normal = normal;
    fragPos = vec3(transform * vec4(position, 1.0f));
    gl_Position = projection * view * transform * vec4(position, 1.0);
}

Here's how I set up my mesh:

void mesh::bindVertices(){
    glBindVertexArray(vertexArrayObject);

    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, this->vertices.size() * sizeof(Vertex), &vertices[0], GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBuffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, this->indices.size() * sizeof(GLuint), &indices[0], GL_STATIC_DRAW);



    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)0); // Position

    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, normal)); //Normal

    glEnableVertexAttribArray(2);
    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, texCoord)); //Texcoord


    glBindVertexArray(0);

}

Vertex.h

#ifndef VERTEX_H
#define VERTEX_H

#include <vector>
#include <glm/glm.hpp>

class Vertex
{
    public:
        Vertex(){
            position = glm::vec3(0.0f, 0.0f, 0.0f);
            normal = glm::vec3(0.0f, 0.0f, 0.0f);
            texCoord = glm::vec2(0.0f, 0.0f);
        };

        Vertex(glm::vec3 pos, glm::vec3 nor, glm::vec2 tex){
            setUpVertex(pos, nor, tex);
        };
        ~Vertex(){}

        void setUpVertex(glm::vec3 pos, glm::vec3 nor, glm::vec2 tex){
            this->position = pos;
            this->normal = nor;
            this->texCoord = tex;
        };

        void print(){
             std::cout << "Position: { " << position.x << ", " << position.y << ", " << position.z << " }\n";
            std::cout << "Normals: { " << normal.x << ", " << normal.y << ", " << normal.z << " }\n";
            std::cout << "Texture Coordinates: { " << texCoord.x << ", " << texCoord.y << " }" << std::endl;
        }
        glm::vec3 position;
        glm::vec3 normal;
        glm::vec2 texCoord;

    protected:
    private:

};

#endif // VERTEX_H

The print function is there for debugging, and it outputs the correct values, so I really have no idea what is going on.
One thing that is not a problem is loading in the model is what I've managed to gather and the normals are behaving very strangley.

1
It is hard to tell what is going on, you should post more code. - derhass
@derhass Think those will be enought? Are there any specific samples of code you want or do you want me to just dump the whole project in here? - v_johan

1 Answers

2
votes

You don't correctly pass your normal data from the vertex shader to the fragment shader. You use out vec3 Normal in the VS, but in vec3 normal (note the lowercase) in the FS, so your input value is just undefined.