-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab5-3-solution.py
More file actions
51 lines (35 loc) · 1.44 KB
/
lab5-3-solution.py
File metadata and controls
51 lines (35 loc) · 1.44 KB
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
from OpenGL.GL import *
from meshes import load
from app import run, readFile
from glslprogram import Program
from matrix import Matrix
from math import pi, cos, sin
vsCode = readFile('./shaders/lab5-3-solution.vert')
fsCode = readFile('./shaders/lab5-3-solution.frag')
def init():
global program
global displayCount
global mView
program = Program(vsCode, fsCode)
program.use()
displayCount = load(program.programId, "./models/monkey")
program.setUniformMat4('mProj', Matrix.makePerspective())
program.setUniformFloat('gloss', 150.0)
glEnable(GL_CULL_FACE)
glEnable(GL_DEPTH_TEST)
def update(dt, time):
mView = Matrix.makeTranslation(0, 0, -4)
# Rotate light position in the Z=5 plane with a radius of 4 units ("in a circle in front of the model")
angle = -time*4
lightDistanceFromOrigin = 4
lightPosWorld = [lightDistanceFromOrigin *
cos(angle), lightDistanceFromOrigin * sin(angle), 5, 1]
# calculate light position in camera space and pass to shader
lightPosCamera = mView @ lightPosWorld
program.setUniformVec4('lightPos', lightPosCamera)
mModelView = mView @ Matrix.makeRotationY(
-pi*3.0/4) @ Matrix.makeRotationX(time*pi/8)
program.setUniformMat4('mModelView', mModelView)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glDrawElements(GL_TRIANGLES, displayCount, GL_UNSIGNED_SHORT, None)
run("2DT904 - Illumination", init, update)