1

我正在加载 Collada(.dae 文件)并且模型有 2 个对象。第一个是带有很多顶点的布料,带有浅蓝色材料。第二个是布料应该在它掉下来时折叠起来的盒子。

这应该是一个 250 帧的动画,但我不确定它是否真的是。当我将它加载到 Assimp 中aiScene*时,它会说HasAnimation() == 0...它还说布料的网格没有颜色,HasVertexColors() == 0这让我担心我必须再看看导出它。我不知道,也许你能说出来?我会将它链接到外部,因为它对于这篇文章来说太大了。(对此感到抱歉)

落布(科拉达动画.dae): http: //pastebin.com/54LkKq8k

我的问题是看不到浅蓝色的布,盒子是黑色的,如(0, 0, 0)...

初始化 VBO:

void AssimpMesh::initMesh(aiMesh *mesh, MeshData *data) {
    //Buffer for temporary storage of new ids
    GLuint id;

    //Make vertex array
    glGenVertexArrays(1, &id);
    data->meshArray = id;

    //Tell OpenGL to use this array
    glBindVertexArray(id);

    //Assign vertices
    if (mesh->HasPositions()) {
        //Make buffer
        glGenBuffers(1, &id);
        data->buffers.push_back(id);
        data->bufferNames.push_back("Positions");

        //Set buffer data
        glBindBuffer(GL_ARRAY_BUFFER, id);
        glBufferData(GL_ARRAY_BUFFER, sizeof(aiVector3D) * mesh->mNumVertices, &mesh->mVertices[0], GL_STATIC_DRAW);

        //Set shader attribute data
        glEnableVertexAttribArray(VBO_VERTEX);
        glVertexAttribPointer(VBO_VERTEX, 3, GL_FLOAT, GL_FALSE, NULL, NULL);
    }

    //Assign colors
    if (mesh->HasVertexColors(0)) {
        //Make buffer
        glGenBuffers(1, &id);
        data->buffers.push_back(id);
        data->bufferNames.push_back("Colors");

        //Set buffer data
        glBindBuffer(GL_ARRAY_BUFFER, id);
        glBufferData(GL_ARRAY_BUFFER, sizeof(aiColor4D) * mesh->mNumVertices, &mesh->mColors[0], GL_STATIC_DRAW);

        //Set shader attribute data
        glEnableVertexAttribArray(VBO_COLOR);
        glVertexAttribPointer(VBO_COLOR, 4, GL_FLOAT, GL_FALSE, NULL, NULL);
    }

    //Assign texture coords
    if (mesh->HasTextureCoords(0)) {
        //Make buffer
        glGenBuffers(1, &id);
        data->buffers.push_back(id);
        data->bufferNames.push_back("TextureCoords");

        //Set buffer data
        glBindBuffer(GL_ARRAY_BUFFER, id);
        glBufferData(GL_ARRAY_BUFFER, sizeof(aiVector3D) * mesh->mNumVertices, &mesh->mTextureCoords[0], GL_STATIC_DRAW);

        //Set shader attribute data
        glEnableVertexAttribArray(VBO_TEXCORD);
        glVertexAttribPointer(VBO_TEXCORD, 3, GL_FLOAT, GL_FALSE, NULL, NULL);
    }

    //Assign colors
    if (mesh->HasNormals()) {
        //Make buffer
        glGenBuffers(1, &id);
        data->buffers.push_back(id);
        data->bufferNames.push_back("Normals");

        //Set buffer data
        glBindBuffer(GL_ARRAY_BUFFER, id);
        glBufferData(GL_ARRAY_BUFFER, sizeof(aiVector3D) * mesh->mNumVertices, &mesh->mNormals[0], GL_STATIC_DRAW);

        //Set shader attribute data
        glEnableVertexAttribArray(VBO_NORMAL);
        glVertexAttribPointer(VBO_NORMAL, 3, GL_FLOAT, GL_FALSE, NULL, NULL);
    }

    if (mesh->HasFaces()) {
        vector <unsigned int> indices;
        aiFace face;
        for (int i = 0; i < mesh->mNumFaces; i++) {
            face = mesh->mFaces[i];
            for (int j = 0; j < face.mNumIndices; j++) {
                indices.push_back(face.mIndices[j]);
            }
        }

        //Make buffer
        glGenBuffers(1, &id);
        data->buffers.push_back(id);
        data->bufferNames.push_back("Faces");

        //Set buffer data
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * indices.size(), &indices.front(), GL_STATIC_DRAW);
    }

    //Unbind vertex array
    glBindVertexArray(NULL);
}

绘制模型:

void AssimpMesh::draw() {
    //Draw all vertex arrays
    aiMesh *mesh;
    aiFace face;
    MeshData *data;
    for (int i = 0; i < meshes.size(); i++) {
        mesh = scene->mMeshes[i];
        face = mesh->mFaces[0];
        data = meshes[i];

        //Tell OpenGL to use this array
        glBindVertexArray(data->meshArray);

        //Tell OpenGL which shader to use
        glUseProgram(data->program);

        //Draw the elements of the array
        glDrawElements(GL_TRIANGLES, face.mNumIndices * mesh->mNumFaces, GL_UNSIGNED_INT, 0);
    }

    //Unbind vertex array
    glBindVertexArray(NULL);

    //Unbind shader
    glUseProgram(NULL);
}

顶点着色器:

#version 120

attribute vec3 vertex;
attribute vec4 color;
attribute vec3 texCoord;
attribute vec3 normal;

uniform mat4 transform;

varying vec3 shared_color;
varying vec2 shared_texCoord;
varying vec3 shared_normal;

void main() {
    gl_Position = transform * vec4(vertex, 1.0);

    //Send data to fragment shader
    shared_color = color.xyz;
    shared_texCoord = texCoord.xy;
    shared_normal = (transform * vec4(normal, 0.0)).xyz;
}

片段着色器:

#version 120

uniform sampler2D diffuse;
uniform int flagTexture;

varying vec3 shared_color;
varying vec2 shared_texCoord;
varying vec3 shared_normal;

void main() {
    vec4 color = vec4(shared_color, 1);
    vec4 texture = texture2D(diffuse, shared_texCoord);
    vec4 finalColor = color;

    if (flagTexture >= 1) {
        finalColor = vec4(mix(color.rgb, texture.bgr, texture.a), 1);
        //finalColor = color * texture;
    }

    float shade = 0;
    if (shade >= 1) {
        vec3 lightPosition = vec3(0, 0, -1);
        float shadowDarkness = 0.8;
        vec3 actualLightPos = vec3(-lightPosition.x, lightPosition.y, lightPosition.z);
        float lightStrength = clamp(dot(actualLightPos, shared_normal), 1 - shadowDarkness, 1.0);
        vec4 litColor = finalColor * lightStrength;
        finalColor = litColor;
    }

    gl_FragColor = finalColor;
}
4

3 回答 3

2

查看您正在使用的库的文档后,我在这里看到了几个问题:

  • 对于颜色,您获得的类型是 aiColor4D。顾名思义,这是一个包含 4 个浮点数的类。但是您将 3 的大小传递给对glVertexAttribPointer().
  • 对于纹理坐标,类型为 aiVector3D,包含 3 个浮点数。但是您将 2 的大小传递给相应的glVertexAttribPointer().
  • aiFace是一个包含索引计数和指向面部索引的指针的类。您不能只将这些人的数组传递给glBufferData()您的索引。实际上,您传递的是一系列索引计数和指针,而不是一系列索引。
  • 的第二个参数glDrawElements()是索引的数量,而不是三角形的数量。

让我们知道一旦这些问题得到解决,情况如何。另一个常见的错误来源是顶点属性如何绑定到顶点着色器,这是我们在这里看不到的代码。

于 2014-04-24T04:16:28.073 回答
1

1)它有效吗?

你测试了吗?看起来它应该正确呈现,尽管您的释放看起来不正确。 glDelete*为其第二个参数接受一个指针;您正在传递对前面元素的引用。

将其更改为:

for (int i = 0; i < buffers.size(); i++) {
    glDeleteBuffers(buffers[i].size(), &buffers[i].front());
}

glDeleteVertexArrays(meshes.size(), &meshes.front());

2)有什么好处吗?

这是一个非常标准的渲染循环,它的速度几乎完全取决于着色器的复杂程度。尽管我会改变在循环之外声明一个指针然后重新分配它,而不是为每个网格声明一个新指针:

aiMesh *mesh;
for (int i = 0; i < meshes.size(); i++) {
    mesh = scene->mMeshes[i];

    //Tell OpenGL to use this array
    glBindVertexArray(meshes[i]);

    //Draw the elements of the array
    glDrawElements(GL_TRIANGLES, mesh->mNumFaces, GL_UNSIGNED_INT, 0);
}

3) 额外信息

它有一个可怕的小数学库,我永远不会推荐,但无论如何你应该知道以后处理顶点和事物(例如,当你稍后映射缓冲区时)

所以如果你想映射你GL_ARRAY_BUFFER的修改存储的顶点,你必须使用这样的东西:

glBindVertexArray(the_vao);
aiVector3D *data = reinterpret_cast<aiVector3D*>(glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE));

// do all the changes!

glUnmapBuffer(GL_ARRAY_BUFFER);
于 2014-04-24T00:21:56.373 回答
0

我查看了文档,发现这HasVertexColors()实际上并不是我想要的材料的漫反射值。所以我通过使用网格中索引的材料解决了这个问题,并将漫反射颜色提取到一个 size 数组中mNumVertices

unsigned int matId = mesh->mMaterialIndex;
aiMaterial *material = scene->mMaterials[matId];
vector <aiColor3D> colors;
aiColor3D diffuse(0, 0, 0);
material->Get(AI_MATKEY_COLOR_DIFFUSE, diffuse);

for (int i = 0; i < mesh->mNumVertices; i++) {
    colors.push_back(diffuse);
}
于 2014-04-24T06:11:50.710 回答