forked from totex/PyOpenGL_tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
video_18_simple_light.py
106 lines (79 loc) · 3.58 KB
/
video_18_simple_light.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
import glfw
from OpenGL.GL import *
import ShaderLoader
import numpy
import pyrr
from PIL import Image
from ObjLoader import *
def window_resize(window, width, height):
glViewport(0, 0, width, height)
def main():
# initialize glfw
if not glfw.init():
return
w_width, w_height = 800, 600
#glfw.window_hint(glfw.RESIZABLE, GL_FALSE)
window = glfw.create_window(w_width, w_height, "My OpenGL window", None, None)
if not window:
glfw.terminate()
return
glfw.make_context_current(window)
glfw.set_window_size_callback(window, window_resize)
obj = ObjLoader()
obj.load_model("res/monkey/monkey_smooth.obj")
texture_offset = len(obj.vertex_index)*12
normal_offset = (texture_offset + len(obj.texture_index)*8)
shader = ShaderLoader.compile_shader("shaders/video_18_vert.vs", "shaders/video_18_frag.fs")
VBO = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, VBO)
glBufferData(GL_ARRAY_BUFFER, obj.model.itemsize * len(obj.model), obj.model, GL_STATIC_DRAW)
#positions
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, obj.model.itemsize * 3, ctypes.c_void_p(0))
glEnableVertexAttribArray(0)
#textures
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, obj.model.itemsize * 2, ctypes.c_void_p(texture_offset))
glEnableVertexAttribArray(1)
#normals
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, obj.model.itemsize * 3, ctypes.c_void_p(normal_offset))
glEnableVertexAttribArray(2)
texture = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, texture)
# Set the texture wrapping parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
# Set texture filtering parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
# load image
image = Image.open("res/monkey/monkey.jpg")
flipped_image = image.transpose(Image.FLIP_TOP_BOTTOM)
img_data = numpy.array(list(flipped_image.getdata()), numpy.uint8)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.width, image.height, 0, GL_RGB, GL_UNSIGNED_BYTE, img_data)
glEnable(GL_TEXTURE_2D)
glUseProgram(shader)
glClearColor(0.2, 0.3, 0.2, 1.0)
glEnable(GL_DEPTH_TEST)
#glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
view = pyrr.matrix44.create_from_translation(pyrr.Vector3([0.0, 0.0, -3.0]))
projection = pyrr.matrix44.create_perspective_projection_matrix(65.0, w_width / w_height, 0.1, 100.0)
model = pyrr.matrix44.create_from_translation(pyrr.Vector3([0.0, 0.0, 0.0]))
view_loc = glGetUniformLocation(shader, "view")
proj_loc = glGetUniformLocation(shader, "projection")
model_loc = glGetUniformLocation(shader, "model")
transform_loc = glGetUniformLocation(shader, "transform")
light_loc = glGetUniformLocation(shader, "light")
glUniformMatrix4fv(view_loc, 1, GL_FALSE, view)
glUniformMatrix4fv(proj_loc, 1, GL_FALSE, projection)
glUniformMatrix4fv(model_loc, 1, GL_FALSE, model)
while not glfw.window_should_close(window):
glfw.poll_events()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
#rot_x = pyrr.Matrix44.from_x_rotation(0.5 * glfw.get_time() )
rot_y = pyrr.Matrix44.from_y_rotation(0.8 * glfw.get_time() )
glUniformMatrix4fv(transform_loc, 1, GL_FALSE, rot_y)
glUniformMatrix4fv(light_loc, 1, GL_FALSE, rot_y)
glDrawArrays(GL_TRIANGLES, 0, len(obj.vertex_index))
glfw.swap_buffers(window)
glfw.terminate()
if __name__ == "__main__":
main()