-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgl_lighting.py
More file actions
85 lines (61 loc) · 2.8 KB
/
gl_lighting.py
File metadata and controls
85 lines (61 loc) · 2.8 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
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
from OpenGL.GL import *
class Lighting(object):
""" @author Preetham Chalasani
@brief Lighting for the scene. Used to enable single/multiple Light sources.
This class helps to setup a light source at some position on the screen from which light will be emitted"""
def __init__(self):
self.__lights = {}
glEnable(GL_LIGHTING)
def addLight(self, id):
"""Adds a Light with the given id to the scene
@param id : GL_LIGHTi where i = 0, 1, 2 .. GL_MAX_LIGHTS
"""
newLight = Light(id)
self.__lights[id] = newLight
def setLight(self, id, position, diffuse, specular, ambient):
"""Set the light properties for the id
@param id : GL_LIGHTi where i = 0, 1, 2 .. GL_MAX_LIGHTS
@param position : A list containing four fixed-point or floating-point values that specify the position of the light in homogeneous object coordinates.
@param diffuse : A list containing four fixed-point or floating-point values that specify the diffuse RGBA intensity of the light.
@param specular : A list containing four fixed-point or floating-point values that specify the specular RGBA intensity of the light.
@param ambient : A list containing four fixed-point or floating-point values that specify the ambient RGBA intensity of the light.
"""
self.__lights[id].set(position, diffuse, specular, ambient)
def render(self):
"""
Render all light sources on to the scene.
"""
for light in self.__lights.values():
light.render()
class Light(object):
""" @author Preetham Chalasani
@brief Individual Light source properties"""
def __init__(self, id):
"""
Enables the light source with the specified id
@param id : GL_LIGHTi where i = 0, 1, 2 .. GL_MAX_LIGHTS
"""
self.__id = id
glEnable(id)
self.__position = []
self.__diffuse = []
self.__specular = []
self.__ambient = []
def set(self, position, diffuse, specular, ambient):
"""Set the light parameteres
@param position : A list containing four fixed-point or floating-point values that specify the position of the light in homogeneous object coordinates.
@param diffuse : A list containing four fixed-point or floating-point values that specify the diffuse RGBA intensity of the light.
@param specular : A list containing four fixed-point or floating-point values that specify the specular RGBA intensity of the light.
@param ambient : A list containing four fixed-point or floating-point values that specify the ambient RGBA intensity of the light.
"""
self.__position = position
self.__diffuse = diffuse
self.__specular = specular
self.__ambient = ambient
def render(self):
"""Render the light source.
"""
glLight(self.__id, GL_POSITION, self.__position)
glLight(self.__id, GL_DIFFUSE, self.__diffuse)
glLight(self.__id, GL_SPECULAR, self.__specular)
glLight(self.__id, GL_AMBIENT, self.__ambient)