我正在加载Collada(.dae文件),模型有2个对象。第一个是布料,有很多顶点和浅蓝色材料。 第二个布料应该在它倒下时折叠的盒子。
这应该是一个250帧动画,但我不确定它是否真的存在。当我把它加载到Assimp' aiScene*
时它说HasAnimation() == 0
...它还说布料的网格没有颜色,HasVertexColors() == 0
这让我很担心我必须再看看出口它。我不知道,也许你可以告诉我?
我会在外部链接它,因为它对于这个帖子来说太大了。 (对不起)
坠落的布料(Collada animation .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;
}
答案 0 :(得分:2)
在查看您正在使用的库的文档后,我在这里看到了一些问题:
glVertexAttribPointer()
。glVertexAttribPointer()
。glBufferData()
作为你的指数。实际上,您传递的是一系列索引计数和指针,而不是一系列索引。glDrawElements()
的第二个参数是索引的数量,而不是三角形的数量。让我们知道一旦修复它们会如何发生。另一个常见的错误来源是顶点属性如何与顶点着色器相关联,这是我们在这里看不到的代码。
答案 1 :(得分:1)
你测试过吗?它看起来应该正确呈现,尽管你的释放看起来并不正确。
glDelete*
获取第二个参数的指针;你正在传递对前元素的引用。
将其更改为:
for (int i = 0; i < buffers.size(); i++) {
glDeleteBuffers(buffers[i].size(), &buffers[i].front());
}
glDeleteVertexArrays(meshes.size(), &meshes.front());
它是一个非常标准的渲染循环,它的速度几乎完全取决于着色器的复杂程度。虽然我会更改在循环外声明一个指针然后重新分配它,而不是为每个网格声明一个新指针:
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);
}
它有一个可怕的小数学库,我永远不会推荐,但你应该知道以后处理顶点和事物(例如,当你稍后映射缓冲区时)
因此,如果您要映射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);
答案 2 :(得分:0)
我查看了文档,发现HasVertexColors()
实际上并不是我想要的材料的漫反射值。所以我通过使用网格中索引的材质并将漫反射颜色提取到大小为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);
}