Skip to content

Commit 1ac3887

Browse files
committed
load obj
1 parent db58d21 commit 1ac3887

File tree

3 files changed

+64
-19
lines changed

3 files changed

+64
-19
lines changed

poetry.lock

Lines changed: 15 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

py3d/main.py

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pygame
22
import pygame.gfxdraw
33
import glm
4+
import pywavefront
45

56

67
class Camera:
@@ -22,6 +23,18 @@ def __init__(self) -> None:
2223
self.vertices = []
2324
self.faces = []
2425

26+
def load_obj(self, path):
27+
o = pywavefront.Wavefront(path)
28+
vert_arr = []
29+
for v in o.vertices:
30+
self.vertices.append(glm.vec3(*v))
31+
for mat_name, mat in o.materials.items():
32+
for i in range(0, len(mat.vertices), 9):
33+
v1 = (mat.vertices[i], mat.vertices[i+1], mat.vertices[i+2])
34+
v2 = (mat.vertices[i+3], mat.vertices[i+4], mat.vertices[i+5])
35+
v3 = (mat.vertices[i+6], mat.vertices[i+7], mat.vertices[i+8])
36+
self.faces.append((o.vertices.index(v1), o.vertices.index(v2), o.vertices.index(v3)))
37+
2538

2639
class Cube(Mesh):
2740
def __init__(self) -> None:
@@ -84,16 +97,34 @@ def draw_mesh_lines(surface, mesh, vp_matrix):
8497
pygame.gfxdraw.line(surface, int(c.x), int(c.y), int(b.x), int(b.y), (255, 255, 255))
8598
pygame.gfxdraw.line(surface, int(a.x), int(a.y), int(c.x), int(c.y), (255, 255, 255))
8699

100+
def draw_mesh_triangles(surface, mesh, vp_matrix):
101+
width, height = surface.get_size()
102+
103+
world_matrix = glm.mat4(1)
104+
world_matrix = glm.translate(world_matrix, mesh.pos)
105+
world_matrix = glm.rotate(world_matrix, mesh.rot.x, glm.vec3(1, 0, 0))
106+
world_matrix = glm.rotate(world_matrix, mesh.rot.y, glm.vec3(0, 1, 0))
107+
world_matrix = glm.rotate(world_matrix, mesh.rot.z, glm.vec3(0, 0, 1))
108+
world_matrix = glm.scale(world_matrix, mesh.scale)
109+
110+
for face in mesh.faces:
111+
a,b,c = (glm.project(mesh.vertices[i], world_matrix, vp_matrix, glm.vec4(0, 0, width, height)) for i in face)
112+
pygame.gfxdraw.filled_trigon(surface, int(a.x), int(a.y), int(b.x), int(b.y), int(c.x), int(c.y), (255, 255, 255, 40))
113+
114+
87115
def main():
88116
pygame.init()
117+
clock = pygame.time.Clock()
89118
pygame.display.set_caption("minimal program")
90119
size = width, height = (800, 600)
91120
screen = pygame.display.set_mode(size)
92-
mesh = Cube()
93-
mesh2 = Cube()
94-
mesh2.pos = glm.vec3(1, 1, 1)
121+
#mesh = Cube()
122+
#mesh2 = Cube()
123+
#mesh2.pos = glm.vec3(1, 1, 1)
124+
mesh = Mesh()
125+
mesh.load_obj("data/teapot.obj")
126+
mesh.scale = glm.vec3(.02, .02, .02)
95127
camera = Camera()
96-
camera.pos = glm.vec3(0, 0, -10)
97128
running = True
98129

99130
while running:
@@ -109,32 +140,32 @@ def main():
109140
keys = pygame.key.get_pressed()
110141
movement = glm.vec3(0, 0, 0)
111142
if keys[pygame.K_w]:
112-
movement += camera.dir * .01
143+
movement += camera.dir * .1
113144
if keys[pygame.K_s]:
114-
movement -= camera.dir * .01
145+
movement -= camera.dir * .1
115146
if keys[pygame.K_a]:
116-
movement += glm.normalize(glm.cross(camera.dir, camera.up)) * .01
147+
movement += glm.normalize(glm.cross(camera.dir, camera.up)) * .1
117148
if keys[pygame.K_d]:
118-
movement -= glm.normalize(glm.cross(camera.dir, camera.up)) * .01
149+
movement -= glm.normalize(glm.cross(camera.dir, camera.up)) * .1
119150
if keys[pygame.K_SPACE]:
120-
movement += camera.up * .01
151+
movement += camera.up * .1
121152
if keys[pygame.K_LSHIFT]:
122-
movement -= camera.up * .01
153+
movement -= camera.up * .1
123154

124155
camera.pos += movement
125156

126-
mesh.rot.x += 0.001
127-
mesh.rot.y += 0.002
128-
mesh.rot.z += 0.003
157+
mesh.rot.x += 0.01
158+
mesh.rot.y += 0.02
159+
mesh.rot.z += 0.03
129160

130161
screen.fill((0, 0, 0))
131162

132163
vp_matrix = glm.mat4(1)
133164
vp_matrix = vp_matrix * glm.perspectiveFovLH(glm.radians(45), width, height, .1, 100)
134165
vp_matrix = vp_matrix * glm.lookAt(camera.pos, camera.target, camera.up)
135166

136-
for m in mesh, mesh2:
137-
draw_mesh_lines(screen, m, vp_matrix)
167+
for m in mesh,:
168+
draw_mesh_triangles(screen, m, vp_matrix)
138169

139170
origin = glm.project(glm.vec3(0, 0, 0), glm.mat4(1), vp_matrix, glm.vec4(0, 0, width, height))
140171
for v in glm.vec3(1, 0, 0), glm.vec3(0, 1, 0), glm.vec3(0, 0, 1):
@@ -146,10 +177,9 @@ def main():
146177
pass
147178

148179
font = pygame.font.Font(pygame.font.get_default_font(), 20)
149-
screen.blit(font.render(f"camera pos: f{camera.pos}", True, (255, 255, 0)), (5, 5))
150-
screen.blit(font.render(f"camera target: f{camera.target}", True, (255, 255, 0)), (5, 20))
151-
180+
screen.blit(font.render(str(int(clock.get_fps())), True, (255, 255, 0)), (5, 5))
152181
pygame.display.flip()
182+
clock.tick(60)
153183

154184

155185
if __name__ == "__main__":

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pygame = "^2.1.2"
1010
numpy = "^1.23.0"
1111
pyrr = "^0.10.3"
1212
PyGLM = "^2.5.7"
13+
PyWavefront = "^1.3.3"
1314

1415
[tool.poetry.dev-dependencies]
1516
pytest = "^5.2"

0 commit comments

Comments
 (0)