Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.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++ 顶点着色器属性位置中损坏的数据_C++_Opengl_Glsl_Shader_Vertex Array - Fatal编程技术网

C++ 顶点着色器属性位置中损坏的数据

C++ 顶点着色器属性位置中损坏的数据,c++,opengl,glsl,shader,vertex-array,C++,Opengl,Glsl,Shader,Vertex Array,由于顶点着色器属性位置中的数据损坏,我的模型看起来拉伸了 以下是顶点着色器代码: #version 330 core layout (location = 0) in vec3 vertPos; layout (location = 1) in vec3 vertNormal; layout (location = 2) in vec2 texCoord; layout (location = 3) in vec4 boneWeigths; layout (location = 4) in i

由于顶点着色器属性位置中的数据损坏,我的模型看起来拉伸了

以下是顶点着色器代码:

#version 330 core

layout (location = 0) in vec3 vertPos;
layout (location = 1) in vec3 vertNormal;
layout (location = 2) in vec2 texCoord;
layout (location = 3) in vec4 boneWeigths;
layout (location = 4) in ivec4 boneIDs;

out vec3 vNormal;
out vec3 fragPos;
out vec2 fragTexCoord;

const int MAX_BONES = 100;

uniform mat4 MVP;
uniform mat4 M;
uniform mat4 boneTransforms[MAX_BONES];

void main()
{
    mat4 boneTx = boneTransforms[boneIDs[0]] * boneWeigths[0]
                + boneTransforms[boneIDs[1]] * boneWeigths[1]
                + boneTransforms[boneIDs[2]] * boneWeigths[2]
                + boneTransforms[boneIDs[3]] * boneWeigths[3];
    vec4 pos = boneTx * vec4(vertPos, 1.0f);
    gl_Position = MVP * pos;
    vec4 normal = boneTx * vec4(vertNormal, 0.0f);
    vNormal = normalize(vec3(M * normal));
    fragPos = vec3(M * pos);
    fragTexCoord = vec2(texCoord.x, texCoord.y);
}
问题似乎是boneIDs中的损坏数据boneIDs数据在CPU上没有问题,但在着色器中获取损坏的数据。我尝试在着色器中对boneIDs数据进行编码,效果很好

以下是VAO的代码:

    // create buffers/arrays
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    glGenBuffers(1, &EBO);

    glBindVertexArray(VAO);
    // load data into vertex buffers
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    unsigned int sz = sizeof(BoneVertex);
    glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(BoneVertex), &vertices[0], GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);

    // set the vertex attribute pointers
    // vertex Positions
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(BoneVertex), (void*)0);
    // vertex normals
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(BoneVertex), (void*)(3 * sizeof(float)));
    // vertex texture coords
    glEnableVertexAttribArray(2);
    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(BoneVertex), (void*)(6 * sizeof(float)));
    // bone weights
    glEnableVertexAttribArray(3);
    glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, sizeof(BoneVertex), (void*)(8 * sizeof(float)));
    // bone ids
    glEnableVertexAttribArray(4);
    glVertexAttribPointer(4, 4, GL_INT, GL_FALSE, sizeof(BoneVertex), (void*)(12 * sizeof(float)));

    glBindVertexArray(0);
BoneVertex结构:

struct BoneVertex
{
    glm::vec3 position;
    glm::vec3 normal;
    glm::vec2 textureCoords;
    glm::vec4 boneWeights;
    glm::ivec4 boneIDs;
}
这很奇怪,因为前3个属性数据看起来很好。问题在于boneIDs和boneWeights 这是否与填充以及数据在结构中的排列方式有关?还是我错过了什么

感谢您,boneIDs是一种具有积分数据类型的顶点着色器输入:

布局位置=ivec4 boneIDs中的4; 如果要为整型属性指定常规顶点属性数据,则必须使用glVertexAttribIPointer focus on I而不是glVertexAttribIPointer see。 注意,type参数没有指定目标属性的类型,它指定了源数据数组的元素类型。GLVertexAttributePointer将源数据数组转换为浮点值,但GLVertexAttributePointer为整数目标属性指定数组

glvertexattributepointer4,4,GL_INT,GL_FALSE,sizeofBoneVertex,void*12*sizeoffloat

glvertexattributepointer4,4,GL_INT,sizeofBoneVertex,void*12*sizeoflot;