-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviewer.py
executable file
·137 lines (114 loc) · 4.27 KB
/
viewer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# CENG 487 Assignment# by
# Kaan Çağlan
# StudentId: 230201047
# Month Year: 18.05.2020
from OpenGL.GL import *
from OpenGL.GLUT import *
from matrix import *
import numpy
from os import walk
from texture_loader import *
class Viewer:
def __init__(self, cam, width=1280, height=720):
self.prev_time = 0
self.change_image = False
self.show_shadows = True
self.lighters = [True, True]
self.percentage = 0.5
self.blinn_status = False
self.blendOn = 0
self.BLENDSTYLES = [
(None, None),
(GL_SRC_ALPHA, GL_ONE),
(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA),
(GL_SRC_ALPHA, GL_DST_ALPHA),
]
self.show_animation = False
self.cam = cam
glutInit()
glutInitDisplayMode(GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)
glutInitWindowSize (width, height)
glutInitWindowPosition (300, 200)
self.width = width
self.height = height
self.window = glutCreateWindow("CENG488")
aspect_ratio = width/height
self.projection = Matrix.create_perspective_projection(45.0, aspect_ratio, 0.1, 100.0)
glutDisplayFunc(self.run)
glutKeyboardFunc(self.keyboard)
self.drawables = []
self.isSun = []
def run(self):
glClearColor(0.0, 0.0, 0.0, 0.0)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
err = glGetError()
if err != GL_NO_ERROR:
print("err is: ", err)
exit(1)
glEnable(GL_DEPTH_TEST)
glEnable(GL_CULL_FACE)
view = self.cam.getViewMatrix().asNumpy()
for drawable in self.drawables:
if(drawable[1]):
drawable[2].draw(self.projection, view, None, self.show_animation, self.percentage, self.blinn_status,
drawable[4] if self.lighters[0] else None,
drawable[5] if self.lighters[1] else None,
self.show_shadows
)
self.change_image = False
glutSwapBuffers()
def add(self, *drawables):
self.drawables.extend(drawables)
def show_objects(self, name):
for i in self.drawables:
if(i[0] == "Lighter1"):
i[1] = name[0]
elif(i[0] == "Lighter2"):
i[1] = name[1]
def change_percentage(self, count):
if count > 0:
if self.percentage < 1:
self.percentage += count
else:
if self.percentage > 0:
self.percentage += count
def keyboard(self, key, x, y):
mkey = glutGetModifiers()
if mkey == GLUT_ACTIVE_SHIFT:
if(ord(key) == 88): #X
self.cam.processMovement("LEFT", 1.0)
elif(ord(key) == 89): #Y
self.cam.processMovement("DOWN", 1.0)
elif(ord(key) == 90): #Z
self.cam.processMovement("BACKWARD", 1.0)
else:
if ord(key) == 27: # ord() is needed to get the keycode
glutLeaveMainLoop()
return
elif(ord(key) == 120): #x
self.cam.processMovement("RIGHT", 1.0)
elif(ord(key) == 121): #y
self.cam.processMovement("UP", 1.0)
elif(ord(key) == 122): #z
self.cam.processMovement("FORWARD", 1.0)
elif(ord(key) == 97): #a
self.show_animation = not self.show_animation
#self.blinn_status = not self.blinn_status
elif(ord(key) == 98): #b
self.blinn_status = not self.blinn_status
elif(ord(key) == 49):
self.lighters = [not self.lighters[0], self.lighters[1]]
self.show_objects(self.lighters)
#self.show_objects(["Cube","Sphere"])
elif(ord(key) == 50):
self.lighters = [self.lighters[0], not self.lighters[1]]
self.show_objects(self.lighters)
elif(ord(key) == 51):
self.show_shadows = not self.show_shadows
"""
elif(ord(key) == 43): #+
self.change_percentage(+0.05)
elif(ord(key) == 45): #-
self.change_percentage(-0.05)
"""
glutPostRedisplay()