繁体   English   中英

如何将 obj 文件加载到 PyOpenGL

[英]How to load obj file to PyOpenGL

I'm trying to load.obj file to PyOpenGL and pygame referring to http://www.pygame.org/wiki/OBJFileLoader and https://github.com/yarolig/OBJFileLoader

我试图改变视角并进行翻译,但看不到 object

这是我到目前为止所做的

import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
from objloader import *


if __name__ == "__main__":
    pygame.init()
    display = (1000,600)
    pygame.display.set_mode(display, DOUBLEBUF|OPENGL)

    gluPerspective(90, (display[0]/display[1]), 1, 100)

    glTranslatef(0.0,0.0, -10)

    # import file
    model = OBJ('model.obj')

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
        
        # draw model
        glPushMatrix()
        glTranslatef(10, 10, 10)
        model.render()
        glPopMatrix()
        
        pygame.display.flip()
        pygame.time.wait(10)

OpenGL 有不同的电流矩阵,见glMatrixMode 每个顶点坐标由 model 视图矩阵和投影矩阵变换。
我建议将投影矩阵设置为当前的GL_PROJECTION并将视图矩阵设置为当前的GL_MODELVIEW

if __name__ == "__main__":
    # [...]

    glMatrixMode(GL_PROJECTION) # <---- specify projection matrix
    gluPerspective(90, (display[0]/display[1]), 0.1, 100)

    glMatrixMode(GL_MODELVIEW)  # <---- specify model view matrix
    glTranslatef(0.0, 0.0, -5)

Anyway you have to remove the model transformation, because the model transformation moves the object out of the Viewing frustum and causes that the model is clipped:

if __name__ == "__main__":
    # [...]

    while True:
        # [...]

        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
        
        # draw model
        glPushMatrix()
        #glTranslatef(10, 10, 10) <--- DELETE
        model.render()
        glPopMatrix()

请注意, Wavefront OBJ加载器需要.obj.mtl文件才能正确加载 model。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM