我想在OpenGL / GLSL中使用Assimp加载Crisis Nanosuit model。该模型具有几个网格,如assimp中的节点树所描述的。每个网格与一个或多个纹理(漫反射或镜面等)相关联。如何在模型上渲染纹理并仍然使用单个绘制调用进行渲染?
到目前为止,我已经能够加载没有纹理的模型。这就是我如何做到的:我使用节点树来找出模型中存在多少个网格,并使用结构数组堆叠它们。这包含位置,法线,Texcoords,Color_Ambient,Color_diffuse,Color_specular和Shininess的浮点值到缓冲区VBO中。由于网格是堆叠的,因此索引数组相应地为每个网格偏移。最后,通过单次绘制调用,模型成功呈现。这是整个code和相关部分如下
struct Vertex
{
glm::vec3 position;
glm::vec3 normal;
glm::vec2 texcoord;
glm::vec3 colorambient;
glm::vec3 colordiffuse;
glm::vec3 colorspecular;
float shininess;
};
// Creating a nodestack of all the meshes
void modelloader::NodeTreeTraversal(aiNode *node)
{
if(node->mNumChildren==0)
nodestack.push_back(node);
else
for(unsigned int i=0; i<node->mNumChildren; i++)
this->NodeTreeTraversal(node->mChildren[i]);
}
// Look into assimp data structures for data and populate them into opengl's vbo's and ebo.
void modelloader::ProcessMeshes()
{
// currently this method loads vertex positions, normals, textures;
// also loads material info such as ambient, diffuse and specular colors with shininess as 16.0f
Vertex vertex;
unsigned int offset_faces=0;
for(unsigned int i=0; i<this->nodestack.size(); i++)
{
aiNode *node = nodestack[i];
for(unsigned int j=0; j<node->mNumMeshes; j++)
{
aiMesh *mesh = this->scene->mMeshes[node->mMeshes[j]];
aiColor4D ambient;
aiColor4D diffuse;
aiColor4D specular;
if(this->scene->HasMaterials()) {
aiMaterial *mtl = scene->mMaterials[mesh->mMaterialIndex];
aiGetMaterialColor(mtl, AI_MATKEY_COLOR_AMBIENT, &ambient);
aiGetMaterialColor(mtl, AI_MATKEY_COLOR_DIFFUSE, &diffuse);
aiGetMaterialColor(mtl, AI_MATKEY_COLOR_SPECULAR, &specular);
}
// load all mesh data
for(unsigned int k=0; k<mesh->mNumVertices; k++)
{
// positions and normals
vertex.position = glm::vec3(mesh->mVertices[k].x, mesh->mVertices[k].y, mesh->mVertices[k].z); // load positions
vertex.normal = glm::vec3(mesh->mNormals[k].x, mesh->mNormals[k].y, mesh->mNormals[k].z); // load normals
// load textures
if(this->scene->HasTextures())
vertex.texcoord = glm::vec2(mesh->mTextureCoords[0][k].x, mesh->mTextureCoords[0][k].y);
else vertex.texcoord = glm::vec2(0.0f, 0.0f);
// load materials
vertex.colorambient = glm::vec3(ambient.r, ambient.g, ambient.b);
vertex.colordiffuse = glm::vec3(diffuse.r, diffuse.g, diffuse.b);
vertex.colorspecular = glm::vec3(specular.r, specular.g, specular.b);
vertex.shininess = 16.0f;
// push back all the data for each vertex
meshdata.push_back(vertex);
}
// create index data
for(unsigned int l=0; l<mesh->mNumFaces; l++) {
this->indices.push_back(mesh->mFaces[l].mIndices[0]+offset_faces);
this->indices.push_back(mesh->mFaces[l].mIndices[1]+offset_faces);
this->indices.push_back(mesh->mFaces[l].mIndices[2]+offset_faces);
}
offset_faces = offset_faces+mesh->mNumVertices;
}
}
this->MeshData = &meshdata[0].position.x;
this->MeshDataSize = meshdata.size() * 18 * sizeof(float);
this->Indices = indices.data();
this->IndicesSize = indices.size()*sizeof(unsigned int);
}
// draw call
void modelloader::RenderModel()
{
glBindVertexArray(this->VAO);
glDrawElements(GL_TRIANGLES, this->IndicesSize/sizeof(unsigned int), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
这是output image。
现在,当加载纹理(身体的每个部分的单独图像文件)时,当我激活所有纹理atonce时,它会在整个身体上拉伸每个纹理文件。我该怎么做呢?
我的初步想法是:激活所有纹理文件。在VBO和片段着色器中添加名为“mesh_number”的属性,使用与“mesh_number”对应的适当纹理。我不知道这是否有效。通常怎么做?你有代码样本吗?
当绘制调用应用于模型中的每个网格时,如here完成,此问题就会得到解决。 1)但不是拉电话贵吗?我不应该一次性绘制整个网格吗? 2)我应该创建一个拼凑成一个的所有身体部位的单个图像文件;很像sprite sheet?
您需要通过以下方式激活每个纹理:
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, <cour texture id>);
glActiveTexture(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, <cour texture id>);
因此,可以为绘制调用渲染所有纹理。