OpenGL(核心配置文件)模型&x27;s三角形渲染错误
这是我项目的一个问题,请看截图: 我的目标是以错误的方式构建。顶点未正确连接。 我怀疑这与模型的指数有关。无论如何,以下是为我构建网格的代码:OpenGL(核心配置文件)模型&x27;s三角形渲染错误,opengl,rendering,Opengl,Rendering,这是我项目的一个问题,请看截图: 我的目标是以错误的方式构建。顶点未正确连接。 我怀疑这与模型的指数有关。无论如何,以下是为我构建网格的代码: 网格模型::processMesh(aiMesh*网格,const aiScene*场景) { 向量顶点; std::向量指数; 矢量纹理; //顶点 对于(GLuint i=0;imNumVertices;i++) { 顶点顶点; glm::vec3载体; //职位 vector.x=网格坐标->垂直坐标[i].x; vector.y=网格->mV
网格模型::processMesh(aiMesh*网格,const aiScene*场景)
{
向量顶点;
std::向量指数;
矢量纹理;
//顶点
对于(GLuint i=0;imNumVertices;i++)
{
顶点顶点;
glm::vec3载体;
//职位
vector.x=网格坐标->垂直坐标[i].x;
vector.y=网格->mVertices[i].y;
vector.z=网格->mVertices[i].z;
垂直位置=向量;
//常态
vector.x=网格\法线[i].x;
vector.y=mesh_uu->mNormals[i].y;
vector.z=mesh_uu->mNormals[i].z;
垂直法线=向量;
//纹理坐标
if(网格->mTextureCoords[0])
{
glm::vec2向量;
向量_ux=mesh_ux->mTextureCoords[0][i].x;
向量_u0.y=mesh_0->mTextureCoords[0][i].y;
vert.texCoords=向量_u3;;
}
else vert.texCoords=glm::vec2(0.0f,0.0f);
顶点。向后推(顶点);
}
//指数
对于(GLuint i=0;imNumFaces;i++)
{
aiFace face=mesh_->MFACE[i];
对于(GLuint j=0;jmNumFaces;j++)索引;
}
//质地
如果(网格->材质索引>=0)
{
AimMaterial*材质=场景->材质[网格->材质索引];
//漫
std::vector diffuseMaps=this->loadMaterialTextures(材质、aiTextureType\u DIFFUSE、TEX\u DIFF\u名称);
插入(textures.end(),diffuseMaps.begin(),diffuseMaps.end());
//镜面反射
std::vector specularMaps=this->loadMaterialTextures(材质、AitTextureType\u SPECULAR、TEX\u SPEC\u名称);
插入(textures.end(),specularMaps.begin(),specularMaps.end());
}
返回网格(顶点、索引、纹理);
}
在此函数中,我设置了所有对象:
void mesh::setupMesh()
{
//缓冲区
glgenvertexarray(1,&this->vao);
glGenBuffers(1,&this->vbo);
glGenBuffers(1,&this->ebo);
glBindVertexArray(此->vao);
glBindBuffer(GL_数组_BUFFER,this->vbo);
glBufferData(GL\u数组\u BUFFER,this->vertices.size()*sizeof(vertex),&this->vertices[0],GL\u STATIC\u DRAW);
glBindBuffer(GL\u元素\u数组\u缓冲区,this->ebo);
glBufferData(GL\u ELEMENT\u ARRAY\u BUFFER,this->index.size()*sizeof(GLuint),&this->index[0],GL\u STATIC\u DRAW);
//属性
GlenableVertexAttributeArray(0);
glvertexattributepointer(0,3,GL_FLOAT,GL_FALSE,sizeof(顶点),(GLvoid*)0);
GlenableVertexAttributeArray(1);
glvertexattributepointer(1,3,GL_FLOAT,GL_FALSE,sizeof(顶点),(GLvoid*)偏移(顶点,法线));
GlenableVertexAttributeArray(2);
glvertexattributepointer(2,2,GL_FLOAT,GL_FALSE,sizeof(顶点),(GLvoid*)offsetof(顶点,texCoords));
//解开
glBindVertexArray(0);
}
这是渲染
void mesh::draw(着色器*着色器)
{
GLuint-tex_-diffNumber=1;
GLuint tex_specNumber=1;
对于(GLuint i=0;itextures.size();i++)
{
//加载目标纹理
玻璃结构(GL_纹理0+i);
std::stringstream和ssstream;
std::字符串编号;
std::string name=this->textures[i]。键入;
if(name==TEX\u DIFF\u name)
sstream shaderProgID,(名称+编号).c_str()),i);
}
//设置光泽度
//glUniform1f(glGetUniformLocation(着色器->着色器程序,“材质光泽”),16.0f;
//画
glBindVertexArray(此->vao);
glpaurements(GL_三角形,this->index.size(),GL_UNSIGNED_INT,0);
glBindVertexArray(0);
//释放
对于(GLuint i=0;itextures.size();i++)
{
玻璃结构(GL_纹理0+i);
glBindTexture(GL_TEXTURE_2D,0);
}
}
问题是为什么我的模型有错误的三角形?
询问任何其他信息。
提前谢谢你们的帮助,伙计们 for(GLuint i=0;imNumFaces;i++)
for (GLuint i = 0; i < mesh_->mNumFaces; i++)
{
aiFace face = mesh_->mFaces[i];
for (GLuint j = 0; j < mesh_->mNumFaces; j++)
indices.push_back(face.mIndices[j]);
}
{
aiFace face=mesh_->MFACE[i];
对于(GLuint j=0;jmNumFaces;j++)
指数.推回(脸.心[j]);
}
我觉得这是不对的。我不认为内部循环应该再次覆盖所有面。相反,它应该迭代面中的所有索引:
for (GLuint i = 0; i < mesh_->mNumFaces; i++)
{
aiFace face = mesh_->mFaces[i];
for (GLuint j = 0; j < face->mNumIndices; j++)
indices.push_back(face.mIndices[j]);
}
for(GLuint i=0;imNumFaces;i++)
{
aiFace face=mesh_->MFACE[i];
对于(GLuint j=0;jmnumindice;j++)
指数.推回(脸.心[j]);
}
请注意,原始版本在可用内存之外读取,因为循环很可能一直持续到j=5(六个面),但只有最多四个索引的面。如果你附加了一个调试器并通过它,这应该是很容易看到的。for(GLuint i=0;imNumFaces;i++)
{
aiFace face=mesh_->MFACE[i];
对于(GLuint j=0;jmNumFaces;j++)
指数.推回(脸.心[j]);
}
我觉得这是不对的。我不认为内部循环应该再次覆盖所有面。相反,它应该迭代面中的所有索引:
for (GLuint i = 0; i < mesh_->mNumFaces; i++)
{
aiFace face = mesh_->mFaces[i];
for (GLuint j = 0; j < face->mNumIndices; j++)
indices.push_back(face.mIndices[j]);
}
for(GLuint i=0;imNumFaces;i++)
{
aiFace face=mesh_->MFACE[i];
对于(GLuint j=0;jmnumindice;j++)
指数.推回(脸.心[j]);
}
请注意,原始版本在可用内存之外读取,因为循环很可能一直持续到j=5(六个面),但只有最多四个索引的面。如果你附加了一个调试器并通过它,这应该很容易看到。谢谢你,大师!它解决了这个问题。这很有道理。我经常为循环复制粘贴