OpenGL 3.3照明给出了非常奇怪的分类

时间:2015-05-09 10:30:21

标签: c++ opengl 3d shader lighting

上个月,我已经在各个页面(learnopengl.com)上关注了教程。
我到了灯光部分(我创建了一个装载器,首先用Assimp加载网格物体),我的碎片编码与教程灯光(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);
}

例如,当点亮飞机时,它会给我这些结果:

OpenGLplane_light_distortions

当我没有点亮飞机时,它是浅灰色的。

这里是.obj文件或飞机:

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

它被正确导入,每个顶点法线为{0,1,0}且索引也正确。

搜索类似" OpenGL灯光故事"或者只是" OpenGL照明"我找不到这样的东西。所以我真的不知道出了什么问题。

哦,是的,我使用openGL 3.3,我有一个集成的英特尔GPU。 如果硬件出现问题,CPU是intel核心i3-2367M。

感谢您的时间:D

编辑:

所以我只是试图在飞机上显示我的法线的颜色,它们应该是纯绿色的,但它们是令人厌恶的:

OpenGL disortion Normals

我也会在这里转储我的vertexshadercode:

#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);
}

以下是我设置网格的方法:

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

打印功能用于调试,它输出正确的值,所以我真的不知道发生了什么。 有一件事不是问题,模型中的加载是我设法收集的,法线表现得非常苛刻。

1 个答案:

答案 0 :(得分:2)

您没有将正常数据从顶点着色器正确传递到片段着色器。你在VS中使用out vec3 Normal,但在FS中使用in vec3 normal(注意小写),所以你的输入值是未定义的。