OpenGL:在 python 中加载时模型看起来不正确

发布于 2024-10-02 18:18:20 字数 2127 浏览 1 评论 0原文

我有一个搅拌机模型,下面是我的模型在将其加载到 python 中时如何呈现的图像。看起来法线都乱了。我为每个顶点使用正确的法线。我正在以正确的顺序导出它们。我在搅拌机控制台中测试了实际导出文件是否具有正确的数据。

我知道我必须在 python 中旋转模型,因为 z 轴不同,所以我不确定法线 z 是否指向错误的方向。

我正在使用 pyglet。以前有人遇到过这个问题吗? 我能做些什么来尝试修复它有什么想法吗?

我不确定这是 OpenGL 还是 python 问题。

opengl 设置代码:

glMatrixMode(GL_PROJECTION)
    zNear = 0.01
    zFar = 1000.0
    fieldOfView = 45.0
    size = zNear * math.tan(math.radians(fieldOfView) / 2.0)
    glFrustum(-size, size, -size / (w / h), size / 
           (w / h), zNear, zFar); 
    glViewport(0, 0, w, h);  
    # Set-up projection matrix
    # TODO


    glMatrixMode(GL_MODELVIEW)
    glShadeModel(GL_SMOOTH)
    glEnable(GL_LIGHTING)
    glEnable(GL_LIGHT0)


    light0Ambient = (GLfloat * 4)(*[])
    light0Ambient[0] = 0.2
    light0Ambient[1] = 0.2
    light0Ambient[2] = 0.2
    light0Ambient[3] = 1.0
    glLightfv(GL_LIGHT0, GL_AMBIENT, light0Ambient);


    lightpos = (GLfloat * 3)(*[])
    lightpos[0] = 5.0
    lightpos[1] = 5.0
    lightpos[2] = 5.0
    glLightfv(GL_LIGHT0, GL_POSITION, lightpos)


    tempLV = self.kjgCreateVectorWithStartandEndPoints((5.0,5.0,5.0), (0.0,0.0,-3.0))
    lightVector = (GLfloat * 3)(*[])
    lightVector[0] = tempLV[0]
    lightVector[1] = tempLV[1]
    lightVector[2] = tempLV[2]
    glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION,lightVector);

    glLoadIdentity( )
    glTranslatef(0.0, 2.0, -18.0)
    #glScalef(0.4, 0.4, 0.4)
    glRotatef(-90, 1.0, 0.0, 0.0)

绘制代码:

    for face in self.faces:
        #print group
        if len(face) == 3:
                glBegin(GL_TRIANGLES)
        elif len(face) == 4:
                glBegin(GL_QUADS)
        else:
                glBegin(GL_POLYGON)
        for i in face:
            if i in (104,16,18,102):
                    glVertex3f(*self.vertices[i])
                    color = self.calculateVertexIntensity((.5,.5,.5),self.normals[i],self.vertices[i])
                    glColor3f(*color)
                    glNormal3f(*self.normals[i])
        glEnd()

alt text

I have a blender model and below is a image of how my model renders when I load it into python. It looks like the normals are all messed up. I am using the correct normal for each vertex. I'm exporting them in the correct order. I test this in the blender console that the actual export file had the right data.

I know that I had to rotate the model in python because z axis is different, so I'm not sure if the normals z is pointing in the wrong direction.

I'm using pyglet. Has anyone ever had this problem before?
Any ideas of what I can do to try to fix it?

Im not sure if this a OpenGL or python problem.

opengl setup code:

glMatrixMode(GL_PROJECTION)
    zNear = 0.01
    zFar = 1000.0
    fieldOfView = 45.0
    size = zNear * math.tan(math.radians(fieldOfView) / 2.0)
    glFrustum(-size, size, -size / (w / h), size / 
           (w / h), zNear, zFar); 
    glViewport(0, 0, w, h);  
    # Set-up projection matrix
    # TODO


    glMatrixMode(GL_MODELVIEW)
    glShadeModel(GL_SMOOTH)
    glEnable(GL_LIGHTING)
    glEnable(GL_LIGHT0)


    light0Ambient = (GLfloat * 4)(*[])
    light0Ambient[0] = 0.2
    light0Ambient[1] = 0.2
    light0Ambient[2] = 0.2
    light0Ambient[3] = 1.0
    glLightfv(GL_LIGHT0, GL_AMBIENT, light0Ambient);


    lightpos = (GLfloat * 3)(*[])
    lightpos[0] = 5.0
    lightpos[1] = 5.0
    lightpos[2] = 5.0
    glLightfv(GL_LIGHT0, GL_POSITION, lightpos)


    tempLV = self.kjgCreateVectorWithStartandEndPoints((5.0,5.0,5.0), (0.0,0.0,-3.0))
    lightVector = (GLfloat * 3)(*[])
    lightVector[0] = tempLV[0]
    lightVector[1] = tempLV[1]
    lightVector[2] = tempLV[2]
    glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION,lightVector);

    glLoadIdentity( )
    glTranslatef(0.0, 2.0, -18.0)
    #glScalef(0.4, 0.4, 0.4)
    glRotatef(-90, 1.0, 0.0, 0.0)

Draw Code:

    for face in self.faces:
        #print group
        if len(face) == 3:
                glBegin(GL_TRIANGLES)
        elif len(face) == 4:
                glBegin(GL_QUADS)
        else:
                glBegin(GL_POLYGON)
        for i in face:
            if i in (104,16,18,102):
                    glVertex3f(*self.vertices[i])
                    color = self.calculateVertexIntensity((.5,.5,.5),self.normals[i],self.vertices[i])
                    glColor3f(*color)
                    glNormal3f(*self.normals[i])
        glEnd()

alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

宁愿没拥抱 2024-10-09 18:18:20

现在,您正在指定顶点之后的法线,而不是之前,因此基本上您是在为顶点 X+1 指定顶点 X 的法线。这是当前代码中最重要的缺陷。

另外,calculateVertexIntensity 是什么?首先,您的代码中启用了光照,因此 glColor 无论如何都会被忽略,但看起来您仍然试图在这里做一些不必要的事情 - OpenGL 固定函数渲染已经会计算顶点强度基于 glLight* 设置和 glMaterial* 设置。

另外,您可能想要标准化您的 lightVector,我不确定 OpenGL 是否会为您标准化它。

(还要确保检查最新的 OpenGL 功能;您现在正在使用已弃用的函数,因此您很快就会遇到性能障碍。首先要查找的是顶点数组VBO,下一个是 着色器。)

Right now you are specifying the normal after the vertex instead of before, so basically you're specifying the normal of vertex X for the vertex X+1. This is the most important flaw in the current code.

Also, what's that calculateVertexIntensity? First of all, lighting is enabled in your code so glColor is going to be ignored anyway, but still it looks like you tried to do something unnecessary here - the OpenGL fixed function rendering already will calculate the vertex intensity basing on the glLight* settings and glMaterial* settings.

Also, you might want to normalize your lightVector, I'm not sure whether OpenGL's going to normalize it for you.

(Also make sure to check on the recent OpenGL features; you're using deprecated functions right now and because of that you'll soon hit a performance barrier. First thing too look for is vertex arrays or VBO, the next is shaders.)

寄离 2024-10-09 18:18:20

这看起来像是你的法线有问题。

self.normals 中的数据可能是错误的:您应该重新计算法线,确保始终使用面周围逆时针方向的一系列顶点来计算每个法线。

(另外,您应该在绘制每个顶点/面之前调用 glNormal)

(另外,我不知道当您计算每个顶点的颜色时发生了什么:但请检查这是否会导致问题)

This looks like a problem with your normals.

The data in self.normals is probably wrong: you should recalculate normals making sure that you always use a sequence of vertices in the anticlockwise direction around the face to calculate each normal.

(also, you should be calling glNormal before drawing each vertex / face)

(also also, I don't know what's going on when you calculate the colour for each vertex: but check that that isn't causing problems)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文