C++ OpenGL 3.3照明提供了非常奇怪的混乱

C++ OpenGL 3.3照明提供了非常奇怪的混乱,c++,opengl,3d,shader,lighting,C++,Opengl,3d,Shader,Lighting,上个月,我一直在各种页面()上学习教程。 我进入了照明部分(我首先创建了一个加载程序来加载带有Assimp的网格),我的fragmentshader代码与照明教程()基本相同 Frag代码: #version 130 out vec4 outColor; in vec3 fragPos; in vec3 normal; in vec2 texcoord; uniform sampler2D tex; uniform vec3 lightPos; uniform vec3 lightColo

上个月,我一直在各种页面()上学习教程。
我进入了照明部分(我首先创建了一个加载程序来加载带有Assimp的网格),我的fragmentshader代码与照明教程()基本相同

Frag代码:

#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);
}
例如,对平面进行照明时,会得到以下结果:

我不点灯的时候,飞机是纯灰色的

以下是.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 light disortions”或只是“OpenGL lighting”之类的东西时,我找不到类似的东西。所以我真的不知道怎么了

哦,是的,我使用openGL 3.3,我有一个集成的英特尔GPU。 CPU采用intel core i3-2367M,以防硬件出现问题

谢谢你抽出时间:D

编辑:

所以我试着在平面上显示我法线的颜色,它们应该是纯绿色,但它们是令人讨厌的:

我还将在此处转储我的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);

}
顶点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
#如果没有顶点#
#定义顶点
#包括
#包括
类顶点
{
公众:
顶点(){
位置=glm::vec3(0.0f,0.0f,0.0f);
正常=glm::vec3(0.0f,0.0f,0.0f);
texCoord=glm::vec2(0.0f,0.0f);
};
顶点(glm::vec3位置,glm::vec3 nor,glm::vec2 tex){
设置顶点(pos、nor、tex);
};
~Vertex(){}
无效设置顶点(glm::vec3位置、glm::vec3 nor、glm::vec2 tex){
此->位置=位置;
这->正常=正常;
这->texCoord=tex;
};
作废打印(){

std::cout您没有正确地将法线数据从顶点着色器传递到片段着色器。您在VS中使用了
out vec3 normal
,但在vec3 normal中使用了
(注意小写)在FS中,您的输入值是未定义的。

很难说到底发生了什么,您应该发布更多的代码。@Derhas认为这些代码足够了吗?是否有您需要的特定代码示例,或者您希望我将整个项目转储到这里?因此,每当我在vertexshader和fragmentshader之间传递值时,输出变量VS上的e必须与FS上的in值具有相同的名称?
我不知道,非常感谢。它现在可以工作了!:在这种情况下,是的。(还有一些更高级的事情,例如,也可以使用一些用户指定的位置进行匹配,但您应该忽略这一点)。