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

Osc*_*car 2 python opengl pyglet

我有一个搅拌器模型,下面是我将模型加载到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)
Run Code Online (Sandbox Code Playgroud)

绘制代码:

    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()
Run Code Online (Sandbox Code Playgroud)

替代文字

Kos*_*Kos 6

现在你指明正常顶点,而不是之前,所以基本上你指定正常顶点X为顶点X + 1.这是当前代码中最重要的缺陷.

那是calculateVertexIntensity什么?首先,在你的代码中启用了光照,所以glColor无论如何都会被忽略,但是看起来你似乎在这里做了一些不必要的事情 - OpenGL固定函数渲染已经根据glLight*设置和glMaterial*设置来计算顶点强度.

另外,你可能想要规范化你lightVector,我不确定OpenGL是否会为你规范化它.

(还要确保检查最近的OpenGL功能;你现在正在使用已弃用的功能,因此你很快就会遇到性能障碍.首先要看的是vertex arrays或者VBO,接下来是shaders.)