我有一个obj文件,并且成功地将对象加载到opengl而不使用给定的法线
这就是它的样子:
该文件的格式为:
v x y z
vn x y z
f x//x' y//y' z//z'
网格的显示功能如下:
glBegin(GL_TRIANGLES);
for all faces
{
glVertex3f(.., .., ..);
glVertex3f(.., .., ..);
glVertex3f(.., .., ..);
}
glEnd();
结果如下:
我已经读过,由于OpenGL用于照明方程的默认法线向量,对象可能看起来很平坦 这可以用法线来解决。给出的法线是780,顶点是155.
我尝试在每次glVertex3f调用之前使用glNorm,但是obj看起来很奇怪(行等)。
我该怎么办?
编辑#1: 这是我阅读文件的方式:
void loader(class mesh &tree)
{
int vx1, vx2, vx3, vn1, vn2, vn3;
ifstream file;
string line;
point vec, norm;
face tempface;
file.open("tree.obj");
if (file.is_open() == true)
{
while(getline(file, line) )
{
if (line.substr(0,2) == "") continue;
else if (line.substr(0, 2) == "v ")
{
istringstream numbers(line.substr(2));
numbers >> vec.x >> vec.y >> vec.z;
tree.vectors.push_back(vec);
}
else if (line.substr(0,2) == "vn")
{
istringstream numbers(line.substr(2));
numbers >> norm.x >> norm.y >> norm.z;
tree.normals.push_back(norm);
}
else if (line.substr(0,2) == "f ")
{
face f;
line = line.substr(2,line.length());
for (string::iterator it = line.begin(); it != line.end(); ++it)
{
if (*it == '/')
{
//erase both of the "//"
line.erase(it);
line.erase(it);
//add a space between the numbers
line.insert(it, ' ');
}
}
istringstream inp(line);
inp >> f.vert_indices[0] >> f.norm_indices[0] >> f.vert_indices[1] >> f.norm_indices[1] >> f.vert_indices[2] >> f.norm_indices[2];
tree.faces.push_back(f);
}
else
continue;
}
file.close();
}
}
这就是我展示它的方式:
void mesh::displayMesh()
{
glPushMatrix();
glBegin(GL_TRIANGLES);
for(vector<face>::const_iterator it = faces.begin();
it != faces.end(); ++it)
{
//glVertex3f(normals[it->norm_indices[0] -1 ].x, normals[it->norm_indices[0] -1 ].y, normals[it->norm_indices[0] -1 ].z);
glVertex3f(vectors[it->vert_indices[0] -1 ].x, vectors[it->vert_indices[0] -1 ].y, vectors[it->vert_indices[0] -1 ].z);
//glVertex3f(normals[it->norm_indices[1] -1 ].x, normals[it->norm_indices[1] -1 ].y, normals[it->norm_indices[1] -1 ].z);
glVertex3f(vectors[it->vert_indices[1] -1 ].x, vectors[it->vert_indices[1] -1 ].y, vectors[it->vert_indices[1] -1 ].z);
//glVertex3f(normals[it->norm_indices[2] -1 ].x, normals[it->norm_indices[2] -1 ].y, normals[it->norm_indices[2] -1 ].z);
glVertex3f(vectors[it->vert_indices[2] -1 ].x, vectors[it->vert_indices[2] -1 ].y, vectors[it->vert_indices[2] -1 ].z);
}
glEnd();
glPopMatrix();
}
在openGL的Render函数中调用displayMesh函数,并将loader函数调用到Setup OpenGL函数中,并且mesh tree对象是一个全局变量。
编辑#2:
树如何看待法线:
也是OpenGL设置功能的this is the code。
答案 0 :(得分:0)
您的绘图代码中不应该有以下内容吗?
glNormal3f(normals[it->norm_indices[0] -1 ].x, normals[it->norm_indices[0] -1 ].y, normals[it->norm_indices[0] -1 ].z);
glVertex3f(vectors[it->vert_indices[0] -1 ].x, vectors[it->vert_indices[0] -1 ].y, vectors[it->vert_indices[0] -1 ].z);
glNormal3f(normals[it->norm_indices[1] -1 ].x, normals[it->norm_indices[1] -1 ].y, normals[it->norm_indices[1] -1 ].z);
glVertex3f(vectors[it->vert_indices[1] -1 ].x, vectors[it->vert_indices[1] -1 ].y, vectors[it->vert_indices[1] -1 ].z);
glNormal3f(normals[it->norm_indices[2] -1 ].x, normals[it->norm_indices[2] -1 ].y, normals[it->norm_indices[2] -1 ].z);
glVertex3f(vectors[it->vert_indices[2] -1 ].x, vectors[it->vert_indices[2] -1 ].y, vectors[it->vert_indices[2] -1 ].z);
编辑:哎呀,你意识到已经了
如果要检查法线是否正确,请根据它的正常颜色为每个顶点着色。使用glColor3f
但提供法线。
答案 1 :(得分:0)
使用erase时,它会使迭代器失效
//erase both of the "//"
it = line.erase(it);
it = line.erase(it);
//add a space between the numbers
it = line.insert(it, ' ');
但是你可以擦除一次然后替换
//erase both of the "//"
it = line.erase(it);
//add a space between the numbers
*it = ' ';