python中obj文件的渲染

2024-06-28 20:07:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个obj文件,我想用光渲染它,但我没有任何关于图形的知识

请看输出

enter image description here

下面是obj文件ex.obj

这是我在网上找到的代码

我想要的是光线(我想是漫射光),形状的纹理看起来不像面具

import ctypes
import sys

sys.path.append('..')
import pyglet
from pyglet.gl import *

from pywavefront import visualization
import pywavefront
import pywavefront.material

rotation = 0
#meshes = pywavefront.Wavefront('ex.obj')

meshes = pywavefront.Wavefront('ex.obj')


print '==================='
window = pyglet.window.Window()
lightfv = ctypes.c_float * 4

@window.event
def on_resize(width, height):
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(60., float(width)/height, 0.01, 100.)
    glMatrixMode(GL_MODELVIEW)
    return True

global rotatex, rotatey, zoom, outName
rotatex = 0
rotatey = 0
zoom=0
outName = "test.png"

@window.event
def on_draw():
    global rotatex, rotatey, outName
    lightfv = ctypes.c_float * 4
    window.clear()
    glLoadIdentity()
    #glLightfv(GL_LIGHT0, GL_POSITION, lightfv(-40.0, 200.0, 100.0, 0.0))
    glLightfv(GL_LIGHT0, GL_AMBIENT, lightfv(0.2, 0.2, 0.2, 1.0))
    glLightfv(GL_LIGHT0, GL_DIFFUSE, lightfv(0.5, 0.5, 0.5, 1.0))
    glEnable(GL_LIGHT0)
    glEnable(GL_LIGHTING)

    glEnable(GL_COLOR_MATERIAL)
    glEnable(GL_DEPTH_TEST)
    glShadeModel(GL_SMOOTH)

    glTranslated(0, -1, -6)
    glRotatef(-66.5, 0, 0, 1)
    glRotatef(rotation, 1, 0, 0)
    glRotatef(90, 0, 0, 1)
    glRotatef(0, 0, 1, 0)

    visualization.draw(meshes)

    pyglet.image.get_buffer_manager().get_color_buffer().save(outName)

    #exit()

pyglet.app.run()

Tags: importobjfloatwindowctypesexpygletgl
1条回答
网友
1楼 · 发布于 2024-06-28 20:07:51

您的模型文件没有包含法线信息,需要近似曲面照明。你知道吗

从模型的外观来看,它似乎不是模型而是三维扫描的,而且表面是用marching cubes算法从体积数据集重建的,所以导出“真实”法线不是一个选择。你知道吗

现在,您可以找到波前对象导入器,它可以为您重新计算法线(pywavefront没有),或者将其导入3D建模应用程序(如Blender),将面标记为平滑或锐利,然后重新导出包含法线的面。结果将不太完美,因为原始曲面将丢失,并且将为手上的块状体素网格构建法线。你知道吗

模型文件将变成这样:ex_normals.obj

渲染将如下所示: normals recalculated

相关问题 更多 >