Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
OpenGL:模型不';在python中加载时,外观不正确_Python_Opengl_Pyglet - Fatal编程技术网

OpenGL:模型不';在python中加载时,外观不正确

OpenGL:模型不';在python中加载时,外观不正确,python,opengl,pyglet,Python,Opengl,Pyglet,我有一个blender模型,下面是我的模型加载到python中时如何渲染的图像。看起来法线都搞乱了。我为每个顶点使用正确的法线。我正在按正确的顺序导出它们。我在blender控制台中测试这一点,以确保实际导出文件具有正确的数据 我知道我必须在python中旋转模型,因为z轴不同,所以我不确定法线z是否指向错误的方向 我用的是pyglet。以前有人有过这个问题吗? 我能做些什么来修复它 我不确定这是OpenGL还是python的问题 opengl设置代码: glMatrixMode(GL_PROJ

我有一个blender模型,下面是我的模型加载到python中时如何渲染的图像。看起来法线都搞乱了。我为每个顶点使用正确的法线。我正在按正确的顺序导出它们。我在blender控制台中测试这一点,以确保实际导出文件具有正确的数据

我知道我必须在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()

这看起来像是法线的问题

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

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


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

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

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

另外,您可能想要规范化您的
光向量,我不确定OpenGL是否会为您规范化它


(还要确保查看最新的OpenGL功能;您现在正在使用不推荐的功能,因此很快就会遇到性能障碍。首先要查找的是
顶点数组
VBO
,其次是
着色器

设置每个顶点的颜色是怎么回事?就是这样。非常感谢。新手失误!