-
Notifications
You must be signed in to change notification settings - Fork 0
/
assignment4.py
147 lines (110 loc) · 3.21 KB
/
assignment4.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
138
139
140
141
142
143
144
145
146
147
# CENG 487 Assignment#7 by
# Hakan Alp
# StudentId: 250201056
# January 2022
import sys
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
from src.app import App
from src import Matrix
from src.shape.winged_object import WingedObject3D
from src.utils.fileIO import *
app = App()
def InitGL(Width, Height):
glClearColor(0.0, 0.0, 0.0, 0.0)
glClearDepth(1.0)
glDepthFunc(GL_LESS)
glEnable(GL_DEPTH_TEST)
glShadeModel(GL_SMOOTH)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
glMatrixMode(GL_MODELVIEW)
def ReSizeGLScene(Width, Height):
global app
app.scene.set_size(Width, Height)
def drawObjects():
global app
app.draw_instructions()
app.scene.draw()
def DrawGLScene():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glColor3f(1.0, 0.0, 3.0)
glTranslatef(0.0, 0.0, -4.0)
drawObjects()
glutSwapBuffers()
def keyPressed(key, x, y):
if ord(key) == 27:
glutLeaveMainLoop()
return
elif key == b'+':
app.scene.increase_subdivision()
elif key == b'-':
app.scene.decrease_subdivision()
glutPostRedisplay()
def specialKeyPressed(key, x, y):
if key == GLUT_KEY_LEFT:
app.scene.translate_camera(Matrix.rotateY(-0.06))
elif key == GLUT_KEY_RIGHT:
app.scene.translate_camera(Matrix.rotateY(0.06))
elif key == GLUT_KEY_UP:
app.scene.translate_camera(Matrix.rotateX(-0.06))
elif key == GLUT_KEY_DOWN:
app.scene.translate_camera(Matrix.rotateX(0.06))
glutPostRedisplay()
def mouseMoved(key, x, y, z):
global app
if key != 3 and key != 4:
return
if key == 3:
app.scene.translate_camera(Matrix.scale(1.05, 1.05, 1.05))
elif key == 4:
app.scene.translate_camera(Matrix.scale(0.95, 0.95, 0.95))
glutPostRedisplay()
m = []
def drag(x, y):
global app
m.append((x, y))
if(len(m) == 2):
xVec = m[1][0] - m[0][0]
yVec = m[1][1] - m[0][1]
if(xVec > 10):
xVec = 10
elif(xVec < -10):
xVec = -10
if(yVec > 10):
yVec = 10
if(yVec < -10):
yVec = -10
app.scene.translate_camera(Matrix.rotateX(
yVec/250) @ Matrix.rotateY(xVec/250))
m.pop(0)
glutPostRedisplay()
def initGlut():
glutInit(sys.argv)
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
glutInitWindowSize(640, 480)
glutInitWindowPosition(0, 0)
glutCreateWindow("Hakan Alp - Assignment 4")
glutDisplayFunc(DrawGLScene)
glutSpecialFunc(specialKeyPressed)
glutReshapeFunc(ReSizeGLScene)
glutIdleFunc(drawObjects)
glutKeyboardFunc(keyPressed)
glutMouseFunc(mouseMoved)
glutMotionFunc(drag)
InitGL(640, 480)
glutMainLoop()
def main():
global app
if(len(sys.argv) < 2):
print("Please enter an object like below:")
print("python assignment4.py <objectname.obj>")
return
app.scene.add_object(WingedObject3D(generate_winged_edge(
"{file}".format(file=sys.argv[1]))))
app.scene.translate_camera(Matrix.scale(0.5, 0.5, 0.5))
initGlut()
main()