C++ OpenGL绘制了大量缺少的面
我正在尝试制作一个OBJ loder程序,但无论我怎么努力,它都无法正常工作。有几张脸不见了。我打开了三角形面,并在混合器中保持顶点顺序,但我无法使其工作 这是我的OBJLoader代码:C++ OpenGL绘制了大量缺少的面,c++,opengl,C++,Opengl,我正在尝试制作一个OBJ loder程序,但无论我怎么努力,它都无法正常工作。有几张脸不见了。我打开了三角形面,并在混合器中保持顶点顺序,但我无法使其工作 这是我的OBJLoader代码: #include "OBJLoader.h" void OBJLoader::Load(string file){ int lines = 0; int indexsize = 0; int offset = 0; vector<string> datalines; vector<ve
#include "OBJLoader.h"
void OBJLoader::Load(string file){
int lines = 0;
int indexsize = 0;
int offset = 0;
vector<string> datalines;
vector<vec3> position;
vector<vec3> normal;
vector<vec2> texturecoord;
bool progress[] = { false, false, false, false};
bool succes = IOManager::readFiletoLines(file, datalines, lines);
if (!succes){
char* tmp = "";
cin >> tmp;
exit(-1);
}
int facecount = 0;
string::size_type sz;
for (int i = 0; i < lines-1; i++){
string line = datalines[i];
if (line.at(0) == 'v'){
if (line.at(1) == ' '){
if (!progress[0]){
cout << "Loading positions..." << endl;
progress[0] = true;
}
offset = 2;
float x = stof(line.substr(offset), &sz);
offset += sz+1;
float y = stof(line.substr(offset), &sz);
offset += sz+1;
float z = stof(line.substr(offset), &sz);
position.push_back(vec3(x, y, z));
continue;
}
else if (line.at(1) == 't'){
if (!progress[1]){
cout << "Loading texture coordinates..." << endl;
progress[1] = true;
}
offset = 3;
float u = stof(line.substr(offset), &sz);
offset += sz+1;
float v = stof(line.substr(offset), &sz);
texturecoord.push_back(vec2(u, v));
continue;
}else if (line.at(1) == 'n'){
if (!progress[2]){
cout << "Loading normals..." << endl;
progress[2] = true;
}
offset = 3;
float x = stof(line.substr(offset), &sz);
offset += sz+1;
float y = stof(line.substr(offset), &sz);
offset += sz+1;
float z = stof(line.substr(offset), &sz);
normal.push_back(vec3(x, y, z));
cout << "a";
continue;
}
}else if (line.at(0) == 'f'){
if (!progress[3]){
cout << "Loading faces..." << endl;
data.resize(position.size());
progress[3] = true;
}
facecount++;
cout << facecount << endl;
Vertex vertex;
Vertex vertex2;
Vertex vertex3;
//First corner
offset = 2;
int pos1=stoi(line.substr(offset), &sz);
offset += sz+1;
int tex1=stoi(line.substr(offset), &sz);
offset += sz+1;
int norm1=stoi(line.substr(offset), &sz);
//Second corner
offset += sz+1;
int pos2 = stoi(line.substr(offset), &sz);
offset += sz + 1;
int tex2 = stoi(line.substr(offset), &sz);
offset += sz + 1;
int norm2 = stoi(line.substr(offset), &sz);
//Third corner
offset += sz+1;
int pos3 = stoi(line.substr(offset), &sz);
offset += sz + 1;
int tex3 = stoi(line.substr(offset), &sz);
offset += sz + 1;
int norm3 = stoi(line.substr(offset), &sz);
//put the values into the data vector
vertex.postion(position[pos1 - 1].x, position[pos1 - 1].y, position[pos1 - 1].z);
vertex.textureUV(texturecoord[tex1 - 1].x, texturecoord[tex1 - 1].y);
vertex.normal(normal[norm1 - 1].x, normal[norm1 - 1].y, normal[norm1 - 1].z);
vertex.colorRGBA(1, 1, 1, 1);
data[pos1 - 1] = vertex;
vertex2.postion(position[pos2 - 1].x, position[pos2 - 1].y, position[pos2 - 1].z);
vertex2.textureUV(texturecoord[tex2 - 1].x, texturecoord[tex2 - 1].y);
vertex2.normal(normal[norm2 - 1].x, normal[norm2 - 1].y, normal[norm2 - 1].z);
vertex2.colorRGBA(1, 1, 1, 1);
data[pos2 - 1] = vertex2;
vertex3.postion(position[pos3 - 1].x, position[pos3 - 1].y, position[pos3 - 1].z);
vertex3.textureUV(texturecoord[tex3 - 1].x, texturecoord[tex3 - 1].y);
vertex3.normal(normal[norm3 - 1].x, normal[norm3 - 1].y, normal[norm3 - 1].z);
vertex3.colorRGBA(1, 1, 1, 1);
data[pos3 - 1] = vertex3;
indexdata.push_back(pos1-1);
indexdata.push_back(pos2-1);
indexdata.push_back(pos3-1);
}
}
}
#包括“OBJLoader.h”
void OBJLoader::Load(字符串文件){
int行=0;
int indexsize=0;
整数偏移=0;
矢量数据线;
矢量位置;
向量正态分布;
矢量纹理命令;
bool progress[]={false,false,false,false};
bool succes=IOManager::readFiletoLines(文件、数据行、行);
如果(!成功){
char*tmp=“”;
cin>>tmp;
出口(-1);
}
int facecount=0;
字符串::size_类型sz;
对于(int i=0;i cout如果我不得不猜测的话,我会说“保持顶点顺序”标志可能是问题所在。OpenGL通常会剔除非逆时针方向的面,并基于被剔除的三角形(在曲面上,这些三角形显示为受影响对象的每隔一个面),很可能这些三角形的顶点是按顺时针顺序绘制的
为了简单起见,您可以简单地告诉OpenGL不要剔除面:
glDisable(GL_CULL_FACE);
但更好的解决方案可能是确保对象的所有面上的缠绕顺序一致。如果我不得不猜测,我会说“保持顶点顺序”标志可能是问题所在。OpenGL通常会根据被剔除的三角形来剔除非逆时针方向的面(在曲面上显示为受影响对象的每隔一个面),很可能这些三角形的顶点是按顺时针顺序绘制的
为了简单起见,您可以简单地告诉OpenGL不要剔除面:
glDisable(GL_CULL_FACE);
但更好的解决方案可能是确保对象的所有面上的缠绕顺序一致。对问题进行更好的描述可能会有所帮助。对问题进行更好的描述可能会有所帮助。面剔除已禁用。结果可以在图片上看到。面剔除已禁用结果可以在图片上看到。