c++ - 法线在 openGL 中表现怪异

标签 c++ objective-c opengl normals

我一直在为 openGl 编写 .obj 加载器。几何体加载正常,但法线总是乱七八糟。我尝试在 2 个不同的程序中导出模型,但似乎没有任何效果。据我所知,这就是你如何将法线放入 GL_TRIANGLES

     glNormal3fv(norm1);   
     glVertex3fv(vert1);
     glNormal3fv(norm2);
     glVertex3fv(vert2);
     glNormal3fv(norm3);
     glVertex3fv(vert3);

(法线在其余代码中引用 GLfloats。)

编辑:这是法线破损的等面体图片 enter image description here

这是完整的 obj 加载器代码和文件:

    void loadOBJFromFile(NSString *path,float movex,float movey)
{
    NSString *contentns = [[NSString alloc]initWithContentsOfFile:path encoding:NSASCIIStringEncoding error:NULL];
    NSArray *pieces = [contentns componentsSeparatedByString:@"#"];
    //creating the arrays to read the vertecies and normals from.
    NSArray *normals = [[pieces objectAtIndex:3]componentsSeparatedByString:@"\n"];
    NSArray *vertecies = [[pieces objectAtIndex:2]componentsSeparatedByString:@"\n"];
    //The +1 is to make sure we ignore the texture/ material definition that vomes before the faces.
    int normalCount = [[normals objectAtIndex:0]intValue]+2;
    int faceCount = [[normals objectAtIndex:normalCount]intValue];
    //glTranslatef(movex, 0, movey);

glBegin(GL_TRIANGLES);
{
for (int i = 0; i < faceCount;i++)
{
    //aquires all the numbers in thye current face.
    NSArray *currentFace = [[normals objectAtIndex:normalCount+i+1]componentsSeparatedByString:@" "];
    NSArray *v1 = [[currentFace objectAtIndex:1]componentsSeparatedByString:@"//"];
    NSArray *v2 = [[currentFace objectAtIndex:2]componentsSeparatedByString:@"//"];
    NSArray *v3 = [[currentFace objectAtIndex:3]componentsSeparatedByString:@"//"];

    //crewatres the arrays to contain the vertecies
    NSArray *vertex1 = [[vertecies objectAtIndex:[[v1 objectAtIndex:1]intValue]]componentsSeparatedByString:@" "];
    NSArray *vertex2 = [[vertecies objectAtIndex:[[v2 objectAtIndex:1]intValue]]componentsSeparatedByString:@" "];
    NSArray *vertex3 = [[vertecies objectAtIndex:[[v3 objectAtIndex:1]intValue]]componentsSeparatedByString:@" "];
    //creates all the arrays for the normals
    NSArray *normal1 = [[normals objectAtIndex:[[v1 objectAtIndex:1]intValue]]componentsSeparatedByString:@" "];
    NSArray *normal2 = [[normals objectAtIndex:[[v2 objectAtIndex:1]intValue]]componentsSeparatedByString:@" "];
    NSArray *normal3 = [[normals objectAtIndex:[[v3 objectAtIndex:1]intValue]]componentsSeparatedByString:@" "];

    //creates the vertecies coordinates
    GLfloat vert1[] = {[[vertex1 objectAtIndex:1]floatValue],[[vertex1 objectAtIndex:2]floatValue],[[vertex1 objectAtIndex:3]floatValue]};
    GLfloat vert2[] = {[[vertex2 objectAtIndex:1]floatValue],[[vertex2 objectAtIndex:2]floatValue],[[vertex2 objectAtIndex:3]floatValue]};
    GLfloat vert3[] = {[[vertex3 objectAtIndex:1]floatValue],[[vertex3 objectAtIndex:2]floatValue],[[vertex3 objectAtIndex:3]floatValue]};
    //creates the normals coordinates

    GLfloat norm1[] = {[[normal1 objectAtIndex:1]floatValue],[[normal1 objectAtIndex:2]floatValue],[[normal1 objectAtIndex:3]floatValue]};
    GLfloat norm2[] = {[[normal2 objectAtIndex:1]floatValue],[[normal2 objectAtIndex:2]floatValue],[[normal2 objectAtIndex:3]floatValue]};
    GLfloat norm3[] = {[[normal3 objectAtIndex:1]floatValue],[[normal3 objectAtIndex:2]floatValue],[[normal3 objectAtIndex:3]floatValue]};

     glNormal3fv(norm1);   
     glVertex3fv(vert1);
     glNormal3fv(norm2);
     glVertex3fv(vert2);
     glNormal3fv(norm3);
     glVertex3fv(vert3);
}}
glEnd();

目标文件:

#Wavefront OBJ file created by Hexagon 2 
mtllib object.mtl
g Form0
usemtl def_surf_mat
# 12 
v -12.533 4.78719 0 
v -12.533 20.2788 0 
v -7.74583 12.533 -12.533 
v -7.74583 12.533 12.533 
v 0 0 -7.74583 
v 0 0 7.74583 
v 0 25.066 -7.74583 
v 0 25.066 7.74583 
v 7.74583 12.533 -12.533 
v 7.74583 12.533 12.533 
v 12.533 4.78719 0 
v 12.533 20.2788 0 
# 12 
vn -0.850651 -0.525731 0 
vn -0.850651 0.525731 1.50014e-08 
vn -0.525731 -1.50014e-08 -0.850651 
vn -0.525731 0 0.850651 
vn 1.50014e-08 -0.850651 -0.525731 
vn -1.50014e-08 -0.850651 0.525731 
vn -1.50014e-08 0.850651 -0.525731 
vn 1.50014e-08 0.850651 0.525731 
vn 0.525731 0 -0.850651 
vn 0.525731 7.5007e-09 0.850651 
vn 0.850651 -0.525731 1.50014e-08 
vn 0.850651 0.525731 -1.50014e-08 
usemtl def_surf_mat
20
f 7//7 8//8 12//12 
f 7//7 2//2 8//8 
f 8//8 4//4 10//10 
f 6//6 10//10 4//4 
f 7//7 9//9 3//3 
f 5//5 3//3 9//9 
f 6//6 5//5 11//11 
f 6//6 1//1 5//5 
f 1//1 4//4 2//2 
f 1//1 2//2 3//3 
f 12//12 11//11 9//9 
f 12//12 10//10 11//11 
f 2//2 4//4 8//8 
f 10//10 12//12 8//8 
f 12//12 9//9 7//7 
f 3//3 2//2 7//7 
f 3//3 5//5 1//1 
f 4//4 1//1 6//6 
f 9//9 11//11 5//5 
f 11//11 10//10 6//6 

最佳答案

有了您添加到问题中的额外信息,我的想法已经用完了。我也不能真正阅读 Objective-C,但我建议仔细检查所有数组索引和行计数代码(也许打印出所有内容以查看它们是否真的符合您的期望),因为基于信任确切布局和注释的解析单个导出商创建的外观非常毛茸茸。

原始答案:

在没有看到您的加载程序代码和 .obj 文件的情况下,请检查以下几点:

  • 怎么搞砸了?完全错误,或者只是例如平坦阴影而不是平滑阴影?
  • 那些 norm1、norm2 等是三个 GLfloat 的数组吗?
  • 普通数组是否包含合理的值(因此可以从文件中读取它们)?
  • 您的法线是否标准化为长度 1?
  • 法线索引是否正确,即您是否拥有正确顶点的正确法线?

关于c++ - 法线在 openGL 中表现怪异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11229110/

相关文章:

ios - 检测音量按钮的长按和短按 - iOS

objective-c - Mac OS X : start on launch while in app store?

c++ - OpenGL:纹理渲染卡住

java - JOGL中如何接受用户键盘输入?

c++ - 为什么 fstream.read 和 fstream.write 使用 char 而不是 unsigned char?

c++ - 如何制作过滤掉非整数的 C++ 程序?

ios - 使用 [self endEditing :YES] 隐藏 UITextView 后键盘不显示

c - 纹理映射圆

c++ - 非虚推导: what do I really get from the compiler?

c++ - 字符位移有问题