-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9e9498c
commit e61c897
Showing
32 changed files
with
396,625 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import glm | ||
import pygame as pg | ||
|
||
FOV = 50 # deg | ||
Near = 0.1 | ||
Far = 100 | ||
SPEED = 0.01 | ||
SENSITIVITY = 0.05 | ||
|
||
|
||
class Camera: | ||
def __init__(self, app, position=(0, 0, 4), yaw=-90, pitch=0): | ||
self.app = app | ||
self.aspect_ratio = app.WIN_SIZE[0] / app.WIN_SIZE[1] | ||
self.position = glm.vec3(position) | ||
self.up = glm.vec3(0, 1, 0) | ||
self.right = glm.vec3(1, 0, 0) | ||
self.forward = glm.vec3(0, 0, -1) | ||
self.yaw = yaw | ||
self.pitch = pitch | ||
# view matrix | ||
self.m_view = self.get_view_matrix() | ||
# projection matrix | ||
self.m_proj = self.get_projection_matrix() | ||
|
||
def rotate(self): | ||
rel_x, rel_y = pg.mouse.get_rel() | ||
self.yaw += rel_x * SENSITIVITY | ||
self.pitch -= rel_y * SENSITIVITY | ||
self.pitch = max(-89, min(89, self.pitch)) | ||
|
||
def update_camera_vectors(self): | ||
yaw, pitch = glm.radians(self.yaw), glm.radians(self.pitch) | ||
|
||
self.forward.x = glm.cos(yaw) * glm.cos(pitch) | ||
self.forward.y = glm.sin(pitch) | ||
self.forward.z = glm.sin(yaw) * glm.cos(pitch) | ||
|
||
self.forward = glm.normalize(self.forward) | ||
self.right = glm.normalize(glm.cross(self.forward, glm.vec3(0, 1, 0))) | ||
self.up = glm.normalize(glm.cross(self.right, self.forward)) | ||
|
||
def update(self): | ||
self.move() | ||
self.rotate() | ||
self.update_camera_vectors() | ||
self.m_view = self.get_view_matrix() | ||
|
||
def move(self): | ||
velocity = SPEED * self.app.delta_time | ||
keys = pg.key.get_pressed() | ||
if keys[pg.K_w]: | ||
self.position += self.forward * velocity | ||
if keys[pg.K_s]: | ||
self.position -= self.forward * velocity | ||
if keys[pg.K_d]: | ||
self.position += self.right * velocity | ||
if keys[pg.K_a]: | ||
self.position -= self.right * velocity | ||
if keys[pg.K_SPACE]: | ||
self.position += self.up * velocity | ||
if keys[pg.K_LSHIFT]: | ||
self.position -= self.up * velocity | ||
|
||
def get_view_matrix(self): | ||
return glm.lookAt(self.position, self.position + self.forward, self.up) | ||
|
||
def get_projection_matrix(self): | ||
return glm.perspective(glm.radians(FOV), self.aspect_ratio, Near, Far) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import glm | ||
|
||
|
||
class Light: | ||
def __init__(self, position=(3, 3, -3), color=(1, 1, 1), intensity=1): | ||
self.position = glm.vec3(position) | ||
self.color = glm.vec3(color) | ||
|
||
self.intensity = intensity * self.color |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import pygame as pg | ||
import moderngl as mgl | ||
import sys | ||
import model | ||
from camera import Camera | ||
from light import Light | ||
from mesh import Mesh | ||
from scene import Scene | ||
|
||
|
||
class GraphicsEngin: | ||
def __init__(self, win_size=(1600, 900)): | ||
# init pygame modules | ||
pg.init() | ||
# window size | ||
self.WIN_SIZE = win_size | ||
# set opengl attr | ||
pg.display.gl_set_attribute(pg.GL_CONTEXT_MAJOR_VERSION, 4) | ||
pg.display.gl_set_attribute(pg.GL_CONTEXT_MINOR_VERSION, 3) | ||
pg.display.gl_set_attribute(pg.GL_CONTEXT_PROFILE_MASK, pg.GL_CONTEXT_PROFILE_CORE) | ||
# create opengl context | ||
pg.display.set_mode(self.WIN_SIZE, flags=pg.OPENGL | pg.DOUBLEBUF) | ||
# mouse settings | ||
pg.event.set_grab(True) | ||
pg.mouse.set_visible(False) | ||
# detect and use existing opengl context | ||
self.ctx = mgl.create_context() | ||
self.ctx.enable(flags=mgl.DEPTH_TEST | mgl.CULL_FACE) | ||
# create an object to help track time | ||
self.clock = pg.time.Clock() | ||
self.time = 0 | ||
self.delta_time = 0 | ||
# light | ||
self.light = Light(intensity=2) | ||
# camera | ||
self.camera = Camera(self) | ||
# mesh | ||
self.mesh = Mesh(self) | ||
# scene | ||
self.scene = Scene(self) | ||
|
||
def check_events(self): | ||
for event in pg.event.get(): | ||
if event.type == pg.QUIT or event.type == pg.KEYDOWN and event.key == pg.K_ESCAPE: | ||
self.mesh.destroy() | ||
pg.quit() | ||
sys.exit() | ||
|
||
def render(self): | ||
self.ctx.clear(color=(0.08, 0.16, 0.18, 1.0)) | ||
|
||
self.scene.render() | ||
|
||
pg.display.flip() | ||
|
||
@staticmethod | ||
def get_time(): | ||
return pg.time.get_ticks()*0.001 | ||
|
||
def run(self): | ||
while True: | ||
self.time = self.get_time() | ||
self.check_events() | ||
self.camera.update() | ||
pg.mouse.set_pos(pg.Vector2(pg.display.get_surface().get_size())/2) | ||
self.render() | ||
self.delta_time = self.clock.tick(60) | ||
|
||
|
||
if __name__ == "__main__": | ||
app = GraphicsEngin() | ||
app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from vao import VAO | ||
from texture import Texture | ||
|
||
|
||
class Mesh: | ||
def __init__(self, app): | ||
self.app = app | ||
self.vao = VAO(app.ctx) | ||
self.texture = Texture(app.ctx) | ||
|
||
def destroy(self): | ||
self.vao.destroy() | ||
self.texture.destroy() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import glm | ||
|
||
|
||
class BaseModel: | ||
def __init__(self, app, tex_path, normal_tex_path, vao_name, pos = glm.vec3(0, 0, 0), rot = glm.vec3(0, 0, 0), scale=glm.vec3(1, 1, 1)): | ||
self.app = app | ||
self.pos = pos | ||
self.rot = glm.vec3([glm.radians(a) for a in rot]) | ||
self.scale = scale | ||
self.m_model = self.get_model_matrix() | ||
self.tex_path = tex_path | ||
self.normal_tex_path = normal_tex_path | ||
self.vao = app.mesh.vao.vaos[vao_name] | ||
self.program = self.vao.program | ||
self.camera = self.app.camera | ||
|
||
def update(self): ... | ||
|
||
def get_model_matrix(self): | ||
m_model = glm.mat4() | ||
# translate | ||
m_model = glm.translate(m_model, self.pos) | ||
# rotate | ||
m_model = glm.rotate(m_model, self.rot.x, glm.vec3(1, 0, 0)) | ||
m_model = glm.rotate(m_model, self.rot.y, glm.vec3(0, 1, 0)) | ||
m_model = glm.rotate(m_model, self.rot.z, glm.vec3(0, 0, 1)) | ||
# scale | ||
m_model = glm.scale(m_model, self.scale) | ||
return m_model | ||
|
||
def render(self): | ||
self.update() | ||
self.vao.render() | ||
|
||
|
||
class Cube(BaseModel): | ||
def __init__(self, app, tex_path, normal_tex_path=None, vao_name='cube', pos = glm.vec3(0, 0, 0), rot = glm.vec3(0, 0, 0), scale = glm.vec3(1, 1, 1)): | ||
super().__init__(app, tex_path, normal_tex_path, vao_name, pos, rot, scale) | ||
self.texture = None | ||
self.normal_texure = None | ||
self.on_init() | ||
|
||
def update(self): | ||
self.program['u_texture_0'] = 0 | ||
self.texture.use(0) | ||
self.program['n_texture_0'] = 1 | ||
self.normal_texure.use(1) | ||
self.program['m_model'].write(self.m_model) | ||
self.program['m_view'].write(self.app.camera.m_view) | ||
self.program['camPos'].write(self.app.camera.position) | ||
|
||
def on_init(self): | ||
# texture | ||
self.texture = self.app.mesh.texture.get_texture(self.tex_path) | ||
|
||
if self.normal_tex_path is not None: | ||
self.normal_texure = self.app.mesh.texture.get_texture(self.normal_tex_path) | ||
|
||
# mvp | ||
self.program['m_proj'].write(self.app.camera.m_proj) | ||
self.program['m_view'].write(self.app.camera.m_view) | ||
self.program['m_model'].write(self.m_model) | ||
# light | ||
self.program['light.position'].write(self.app.light.position) | ||
self.program['light.intensity'].write(self.app.light.intensity) | ||
# material | ||
self.program['material.Ka'].write(glm.vec3(1.0)) | ||
self.program['material.Kd'].write(glm.vec3(1.0)) | ||
self.program['material.Ks'].write(glm.vec3(1.0)) | ||
|
||
|
||
class ObjModel(BaseModel): | ||
def __init__(self, app, vao_name: str, tex_path, normal_tex_path, path: str, pos = glm.vec3(0, 0, 0), rot = glm.vec3(0, 0, 0), scale=glm.vec3(1, 1, 1)): | ||
super().__init__(app, tex_path, normal_tex_path, vao_name, pos, rot, scale) | ||
self.texture = None | ||
self.normal_texure = None | ||
self.on_init() | ||
|
||
def on_init(self): | ||
# texture | ||
self.texture = self.app.mesh.texture.get_texture(self.tex_path) | ||
|
||
if self.normal_tex_path is not None: | ||
self.normal_texure = self.app.mesh.texture.get_texture(self.normal_tex_path) | ||
|
||
# mvp | ||
self.program['m_proj'].write(self.app.camera.m_proj) | ||
self.program['m_view'].write(self.app.camera.m_view) | ||
self.program['m_model'].write(self.m_model) | ||
# light | ||
self.program['light.position'].write(self.app.light.position) | ||
self.program['light.intensity'].write(self.app.light.intensity) | ||
# material | ||
self.program['material.Ka'].write(glm.vec3(1.0)) | ||
self.program['material.Kd'].write(glm.vec3(1.0)) | ||
self.program['material.Ks'].write(glm.vec3(1.0)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# 3ds Max Wavefront OBJ Exporter v0.97b - (c)2007 guruware | ||
# File Created: 06.05.2013 13:43:53 | ||
|
||
newmtl 20430_Cat | ||
Ns 51.0000 | ||
Ni 1.5000 | ||
d 1.0000 | ||
Tr 0.0000 | ||
Tf 1.0000 1.0000 1.0000 | ||
illum 2 | ||
Ka 1.0000 1.0000 1.0000 | ||
Kd 1.0000 1.0000 1.0000 | ||
Ks 0.2070 0.2070 0.2070 | ||
Ke 0.7000 0.7000 0.7000 | ||
map_Ka 20430_cat_diff_v1.jpg | ||
map_Kd 20430_cat_diff_v1.jpg |
Oops, something went wrong.