法线在 openGL 中表现得很奇怪

2024-04-30

我一直在为 openGl 编写一个 .obj 加载器。几何体加载得很好,但法线总是混乱的。我尝试在两个不同的程序中导出模型,但似乎没有任何效果。据我所知,这就是将法线放入 GL_TRIANGLES 的方法

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

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

EDIT: Here is a picture of a isohedron with the broken normals 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();

}

.obj 文件:

#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等三个GLfloats的数组吗?
  • 普通数组是否包含正常值(因此从文件中读取它们是可行的)?
  • 你的法线是否标准化为长度 1?
  • 法线索引是否正确,即是否具有正确顶点的正确法线?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

法线在 openGL 中表现得很奇怪 的相关文章

随机推荐

  • Angular 指令和 Jquery slipToggle 函数实现

    使用 Angular 和 jquery 我实现了 slipToggle 函数 为了仅将此函数应用于一个特定的 HTML 元素 我在 ng click 函数中使用一个参数 我的代码工作正常 但是 我想知道是否存在另一种更好的方法来实现角度中的
  • 使用默认值递归填写对象属性

    我想用一些虚拟数据填充对象的属性 这是我的代码 但它总是返回 null private static object InsertDummyValues object obj if obj null var properties obj Ge
  • 使用 django-cas-ng 在管理站点上进行身份验证

    我在用着Django Cas NG https github com mingchen django cas ng用于验证用户身份的框架 主要问题是管理页面仍然使用默认的登录视图 到目前为止使用的方法 1 使用环境变量 来自文档 CAS A
  • 如何修复此错误:[WinError 267] 目录名称无效:

    我有一个 python 脚本 它创建一个 GUI 应用程序 在用户选择路径后显示现有文件的列表 系统显示在QlistWidget文件名及其父文件夹 我想从列表中获取所选文件的完整路径 例子 C Users test Desktop test
  • R闪亮:使用fileInput重新加载数据

    有没有办法重新加载使用 fileInput 加载的输入文件 我想允许用户通过简单修改和重新加载 csv 文件来更新他的输入 似乎重新加载文件并没有更新它 一种有用的解决方法是使用不同的文件名保存修改后的数据 我已经尝试过使用 actionB
  • 其中哪些在 Python 中是不可变的?

    我试图弄清楚以下内容在 Sage 中是否是不可变的 它是基于 Python 构建的 所以我相信如果它在 python 中是不可变的 我相信在大多数情况下它在 Sage 中也是不可变的 下面是对象 e f g i class e pass f
  • Observable.of 转异步

    我即将模拟一个封装在 observable 中的 http 调用 我最初的想法是简单地使用Observable of如同Promise resolve 但它似乎没有按我的预期工作 Rx Observable of of1 subscribe
  • 单元测试 Typescript 装饰器

    我有一个基于带有装饰器的打字稿构建的应用程序 用于一些方便的属性分配 并想知道如何为它们编写单元测试 export function APIUrl return function target any key string let valu
  • 代码未在我的 PDF 中绘制水平线

    I m trying to add a horizontal line on top to divide the header text from the actual values in my pdf file 这是我的代码 public
  • 带有 TF 后端的 Keras:获取输出相对于输入的梯度

    我有一个非常简单的 Keras MLP 并且我正在尝试获取输出相对于输入的梯度 我正在使用以下代码 regressor Sequential Dense 32 input shape n features activation relu D
  • 有没有办法在 Chrome 开发者工具中评估源映射中的变量?

    我正在 Chrome 中使用 SuperDevMode 调试 GWT 应用程序 它让我能够访问 devtools 中的 Java 源映射 我的问题是 当我调试代码时 我想评估一些变量 我的问题是 是否可以从源映射中评估变量 如果没有 是否可
  • 将 ng-model 与标签元素一起使用。

    我正在阅读代码 并进入了将标签元素与 data ng model 一起使用的部分 这可能吗
  • Python中“函数之外返回”语法错误的原因?

    我在这段代码中遇到问题 return outside function
  • 如何更改项目符号或非项目符号列表中除第一行之外的所有行的缩进?

    所以 我有这样的简单项目符号列表 ul li Apple Lorem ipsum dolor sit amet consectetur adipiscing elit Vivamus venenatis elit turpis vel fa
  • 正则表达式与换行符不匹配

    有一天 我又要怀疑自己的理智了 我不明白为什么我的正则表达式拒绝匹配可选的换行符 代码 string this is some template code nice varinnewline if bla 1 else 2 if echo
  • 获取Spring bean的真实Class对象

    我正在使用 Spring 注入 Bean 我正在使用一些注释来注释 bean 方法 Security TransactionManagement ExceptionHanling Logging 问题是 我想创建 JUnit 测试来检查我是
  • 无法在 Zsh 中找到 Bash 的替代命令

    我将最新的 git completion bash 放入我的 zshrc 中 然后我得到 Users Masi bin shells git git completion bash 2116 command not found comple
  • 如何使用 Linq 查询 Azure 存储表?

    我不确定具体在哪里 但我在某个地方有错误的想法 我首先尝试使用 linq 查询 azure 存储表 但我无法弄清楚它是如何完成的 通过查看各种来源 我得到以下信息 List
  • 出现错误 /usr/bin/env: 节点:权限被拒绝

    我已经在我的服务器 Centos 上完成了 ODOO v9 安装 一切都已安装成功 登录页面也可以正常工作 但登录后我收到一个包含以下错误的页面 usr bin env node Permission Denied 我尝试更改权限 但我的问
  • 法线在 openGL 中表现得很奇怪

    我一直在为 openGl 编写一个 obj 加载器 几何体加载得很好 但法线总是混乱的 我尝试在两个不同的程序中导出模型 但似乎没有任何效果 据我所知 这就是将法线放入 GL TRIANGLES 的方法 glNormal3fv norm1