|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
首先说一下想要实现的最终目标:
创建一个窗口,窗口内渲染人物的3D模型,可以做出随机动作、换装及变换表情;
目前遇到的问题和疑问:
1、目前渲染模型选用的方案是 pygame + OpenGL,但是OpenGL这个库太老了,相关的文档也不多,不知道有没有其他更流行的方案;
2、现在渲染出的模型没有蒙皮,只有一堆白色的顶点。。。。。
3、随机动作和换装还不知道应该如何实现,有没有大神给指明一条道路,感谢!!!!
总结:如果想要实现对3D模型更好的控制,我该了解一些什么,或者有没有什么比较流行的库,感谢!!!
目前的代码如下:
- import pygame
- from pygame.locals import * # 导入 Pygame 本地常量
- from OpenGL.GL import * # 导入 OpenGL 函数
- from OpenGL.GLU import * # 导入 OpenGL 工具库函数
- import random # 导入随机数生成模块
- import numpy as np # 导入 NumPy 数组处理库
- # 加载 OBJ 文件函数
- def load_obj(filename):
- vertices = [] # 顶点列表
- faces = [] # 面列表
- with open(filename) as f: # 打开 OBJ 文件
- for line in f: # 逐行读取文件
- if line.startswith("v "): # 如果是顶点行
- vertex = [float(x) for x in line.split()[1:]] # 提取顶点坐标
- vertices.append(vertex) # 添加顶点到顶点列表
- elif line.startswith("f "): # 如果是面行
- face = [int(x.split("/")[0]) for x in line.split()[1:]] # 提取顶点索引
- faces.append(face) # 添加面到面列表
- return vertices, faces # 返回顶点和面列表
- # 计算包围盒
- def calculate_bounding_box(vertices):
- min_coords = np.min(vertices, axis=0) # 计算最小坐标
- max_coords = np.max(vertices, axis=0) # 计算最大坐标
- return min_coords, max_coords # 返回最小坐标和最大坐标
- # 缩放模型使其适应窗口大小
- def scale_model_to_fit_window(vertices, display):
- min_coords, max_coords = calculate_bounding_box(vertices) # 计算模型包围盒
- model_size = max_coords - min_coords # 计算模型大小
- max_model_size = max(model_size) # 计算模型最大尺寸
- scale_factor = 2.0 / max_model_size # 计算缩放因子
- scaled_vertices = (
- np.array(vertices) - (min_coords + max_coords) / 2
- ) * scale_factor # 缩放并居中模型
- return scaled_vertices.tolist() # 返回缩放后的顶点列表
- # 绘制 OBJ 模型函数
- def draw_obj(vertices, faces):
- glBegin(GL_TRIANGLES) # 开始绘制三角形
- for face in faces: # 遍历每个面
- for vertex_id in face: # 遍历每个顶点索引
- glVertex3fv(vertices[vertex_id - 1]) # 绘制顶点
- glEnd() # 结束绘制
- # 随机化顶点函数
- def randomize_vertices(vertices, max_displacement=0.1):
- """随机移动顶点位置以生成随机动作"""
- new_vertices = [] # 新顶点列表
- for vertex in vertices: # 遍历每个顶点
- new_vertex = [
- vertex[0]
- + random.uniform(-max_displacement, max_displacement), # 随机移动 X 坐标
- vertex[1]
- + random.uniform(-max_displacement, max_displacement), # 随机移动 Y 坐标
- vertex[2]
- + random.uniform(-max_displacement, max_displacement), # 随机移动 Z 坐标
- ]
- new_vertices.append(new_vertex) # 添加新顶点到新顶点列表
- return new_vertices # 返回新顶点列表
- # 主函数
- def main():
- pygame.init() # 初始化 Pygame
- display = (800, 600) # 设置窗口大小
- pygame.display.set_mode(display, DOUBLEBUF | OPENGL) # 创建 OpenGL 双缓冲窗口
- gluPerspective(45, (display[0] / display[1]), 0.1, 50.0) # 设置透视投影
- glTranslatef(0.0, 0.0, -5) # 平移视图
- vertices, faces = load_obj("./models/man.obj") # 加载 OBJ 模型
- scaled_vertices = scale_model_to_fit_window(vertices, display) # 缩放模型适应窗口
- original_vertices = np.array(vertices) # 保留原始顶点数据
- clock = pygame.time.Clock() # 创建时钟对象控制帧率
- scale_x, scale_y, scale_z = 1, 1, 1
- while True: # 主循环
- for event in pygame.event.get(): # 处理事件
- if event.type == pygame.QUIT: # 如果是退出事件
- pygame.quit() # 退出 Pygame
- quit() # 退出程序
- elif event.type == pygame.KEYDOWN: # 如果是按键按下事件
- if event.key == pygame.K_r: # 按 'r' 键随机化顶点
- scaled_vertices = randomize_vertices(original_vertices)
- if event.key == pygame.K_s: # 按 's' 键恢复原始顶点
- scaled_vertices = original_vertices.tolist()
- keys = pygame.key.get_pressed() # 获取所有按键状态
- if keys[pygame.K_LEFT]: # 如果按下左箭头键
- glRotatef(4, 0, 1, 0) # 绕 Y 轴旋转 1 度
- if keys[pygame.K_RIGHT]: # 如果按下右箭头键
- glRotatef(-4, 0, 1, 0) # 绕 Y 轴旋转 -1 度
- if keys[pygame.K_UP]: # 如果按下上箭头键
- glRotatef(4, 1, 0, 0) # 绕 X 轴旋转 1 度
- if keys[pygame.K_DOWN]: # 如果按下下箭头键
- glRotatef(-4, 1, 0, 0) # 绕 X 轴旋转 -1 度
- if keys[pygame.K_MINUS]: # 按下 - 号
- glScalef(scale_x - 0.1, scale_y - 0.1, scale_z - 0.1) # 缩小模型
- if keys[pygame.K_PLUS]: # 按下 + 号
- glScalef(scale_x + 0.1, scale_y + 0.1, scale_z + 0.1) # 放大模型
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # 清除颜色和深度缓冲
- draw_obj(scaled_vertices, faces) # 绘制 OBJ 模型
- pygame.display.flip() # 刷新显示
- clock.tick(30) # 保持每秒 30 帧
- if __name__ == "__main__":
- main() # 调用主函数
复制代码
哎呀,3D模型渲染的旅程就像是在高山上滑雪,既刺激又容易摔跤!不过没关系,我们大家都在学习嘛!
### 第一个问题:OpenGL替代品
你提到OpenGL太老了,文档也不多,我给你推荐几个更现代的、流行的库:
1. **Pygame + ModernGL**:这完全是小清新的选择,ModernGL是个更现代的OpenGL封装,支持更漂亮的渲染和更复杂的着色器。
2. **Panda3D**:这是个强大的游戏引擎,不仅可以处理3D渲染,还有物理引擎支持,适合做大型项目。
3. **PyOpenGL**(如果你坚持用OpenGL):这是个很痛苦的选择,虽然比起纯OpenGL,它提供了一些更友好的接口,但文档还是那样少。
### 第二个问题:模型没有蒙皮
只显示白色顶点说明你的模型可能没有正确加载纹理或材质。你需要在`load_obj`函数中扩展一下,读取纹理坐标和法线,并在绘制时使用它们。简单来说,就是把“白白的”换成“彩头”的秘诀在这里:
- 加载纹理文件并在OpenGL中绑定它。
- 使用`glTexCoord2f`来指定每个顶点的纹理坐标。
### 第三个问题:随机动作和换装
这个问题听起来有点复杂,但其实可以想象成玩积木。你只需要准备不同的模型(比如不同的衣服、表情)以及一些能改变这些模型姿态的参数。
1. **随机动作**:可以通过定义一套动作系统,使用关节与骨骼动画(例如:动画控制器)来实现。
2. **换装**:只需加载不同的模型,然后通过按键切换当前模型即可。例如,你可以在`KEYDOWN`事件中判断按键是哪个,直接加载对应的OBJ模型。
### 学习的道路
1. 了解基本的3D图形原理,如变换矩阵、法线计算、纹理映射等。
2. 学习如何使用Shader语言,特别是GLSL,这样你就可以自定义你的渲染效果。
3. 看一些关于3D角色动画和骨骼动画的教程,掌握如何创建动画。
### 代码的小建议
我注意到你的`randomize_vertices`函数可能让模型变得奇怪,因为它似乎根本没有对每个坐标进行适当的赋值。你可以尝试这样来修复它:
- def randomize_vertices(vertices, max_displacement=0.1):
- """随机移动顶点位置以生成随机动作"""
- new_vertices = [] # 新顶点列表
- for vertex in vertices: # 遍历每个顶点
- new_vertex = [
- vertex[0] + random.uniform(-max_displacement, max_displacement), # 随机移动 X 坐标
- vertex[1] + random.uniform(-max_displacement, max_displacement), # 随机移动 Y 坐标
- vertex[2] + random.uniform(-max_displacement, max_displacement), # 随机移动 Z 坐标
- ]
- new_vertices.append(new_vertex) # 添加新顶点到新顶点列表
- return new_vertices # 返回新顶点列表
复制代码
### 总结
踏上3D渲染之路并不是一帆风顺,但只要你持续努力,就能在这个高峰上看到美丽的风景。用编程的幽默感砸开困难的大门,祝你好运!只有当你成功换装时,才算是真正的“装扮”成功!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
|
|