Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ OpenGl 4,GL\u数组\u缓冲区无法正确发送法线数据_C++_Opengl_Glsl - Fatal编程技术网

C++ OpenGl 4,GL\u数组\u缓冲区无法正确发送法线数据

C++ OpenGl 4,GL\u数组\u缓冲区无法正确发送法线数据,c++,opengl,glsl,C++,Opengl,Glsl,我有一个使用此方法创建的平面: 我将这些三角形条纹分开,这样每个三角形都可以有唯一的颜色,而且我仍然可以使用顶点索引 我的问题是whit normals缓冲区。我像这样创建法线(这是在计算顶点的算法中): 在GLVertexAttribute中,指针inedex设置为2,因为顶点为0,颜色为1 下面是我的着色器: #shader vertex #version 330 core layout(location = 0) in vec3 position; layout(location = 1

我有一个使用此方法创建的平面:

我将这些三角形条纹分开,这样每个三角形都可以有唯一的颜色,而且我仍然可以使用顶点索引

我的问题是whit normals缓冲区。我像这样创建法线(这是在计算顶点的算法中):

在GLVertexAttribute中,指针inedex设置为2,因为顶点为0,颜色为1

下面是我的着色器:

#shader vertex
#version 330 core

layout(location = 0) in vec3 position;
layout(location = 1) in vec3 color_in;
layout(location = 2) in vec3 normals_in;

uniform mat4 u_MVP;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

out vec3 FragPos;
out vec3 Normal;
out vec3 color_f;

void main()
{
    color_f = color_in;
    FragPos = vec3(model * vec4(position, 1.0));
    Normal = normals_in;
    Normal = mat3(transpose(inverse(model))) * normals_in;

    gl_Position = projection * view * vec4(FragPos, 1.0);
};

#shader fragment
#version 330 core
out vec4 color;

in flat vec3 Normal;
in vec3 FragPos;

in flat vec3 color_f;

uniform vec4 u_Color;

uniform vec3 lightPos;
uniform vec3 viewPos;
uniform vec3 lightColor;


void main()
{
    vec3 objectColor = color_f;

    // ambient
    float ambientStrength = 0.1;
    vec3 ambient = ambientStrength * lightColor;

    // diffuse 
    vec3 norm = normalize(Normal);
    vec3 lightDir = normalize(lightPos - FragPos);
    float diff = max(dot(norm, lightDir), 0.0);
    vec3 diffuse = diff * lightColor;

    // specular
    float specularStrength = 0.5;
    vec3 viewDir = normalize(viewPos - FragPos);
    vec3 reflectDir = reflect(-lightDir, norm);
    float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
    vec3 specular = specularStrength * spec * lightColor;

    vec3 result = (ambient + diffuse + specular) * objectColor;
    color = vec4(result, 1.0);
}

如果我转到片段着色器并设置vec3 norm=vec3(0.0,1.0,0.0),一切正常,但这不是我想要的方式

因此,我以同样的方式发送颜色数据,这很好,但发送法线数据似乎不起作用

因此,可以为颜色(1)启用VertexAttributeArray 应激活法线(2)的VertexAttributeArray,并且 确保
(void*)0
正确无误如果您使用的是结构,请确保
使用offset更可靠,尤其是当您使用具有优化功能的编译器时

这是一个简单的印刷错误。法向量的属性位置为2。所以它必须是
GlenableVertexAttributeArray(2)
而不是
GlenableVertexAttributeArray(1)
。哇,谢谢。从昨天开始,我就对它失去了理智,它太简单了。我已经知道了,但无论如何,谢谢你,希望下一个程序员从你的答案中获得知识,我愚蠢的排版错误是离题的。本报告无意回答这些问题。问题和答案对任何用户都没有用处。
glGenBuffers(1, &normalsBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, normalsBuffer);
    glBufferData(GL_ARRAY_BUFFER, Normals.size() * sizeof(float) * 3, &Normals[0].x, GL_STATIC_DRAW);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, (void*)0); 
#shader vertex
#version 330 core

layout(location = 0) in vec3 position;
layout(location = 1) in vec3 color_in;
layout(location = 2) in vec3 normals_in;

uniform mat4 u_MVP;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

out vec3 FragPos;
out vec3 Normal;
out vec3 color_f;

void main()
{
    color_f = color_in;
    FragPos = vec3(model * vec4(position, 1.0));
    Normal = normals_in;
    Normal = mat3(transpose(inverse(model))) * normals_in;

    gl_Position = projection * view * vec4(FragPos, 1.0);
};

#shader fragment
#version 330 core
out vec4 color;

in flat vec3 Normal;
in vec3 FragPos;

in flat vec3 color_f;

uniform vec4 u_Color;

uniform vec3 lightPos;
uniform vec3 viewPos;
uniform vec3 lightColor;


void main()
{
    vec3 objectColor = color_f;

    // ambient
    float ambientStrength = 0.1;
    vec3 ambient = ambientStrength * lightColor;

    // diffuse 
    vec3 norm = normalize(Normal);
    vec3 lightDir = normalize(lightPos - FragPos);
    float diff = max(dot(norm, lightDir), 0.0);
    vec3 diffuse = diff * lightColor;

    // specular
    float specularStrength = 0.5;
    vec3 viewDir = normalize(viewPos - FragPos);
    vec3 reflectDir = reflect(-lightDir, norm);
    float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
    vec3 specular = specularStrength * spec * lightColor;

    vec3 result = (ambient + diffuse + specular) * objectColor;
    color = vec4(result, 1.0);
}
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);