我正在加载 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;
}