forked from Jerdak/opengl_tutorials_python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tutorial2.py
131 lines (99 loc) · 3.91 KB
/
tutorial2.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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
""" Tutorial 2: Drawing the triangle
"""
from __future__ import print_function
from OpenGL.GL import *
from OpenGL.GL.ARB import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from OpenGL.GLUT.special import *
from OpenGL.GL.shaders import *
from glew_wish import *
import common
import glfw
import sys
import os
# Global window
window = None
null = c_void_p(0)
def opengl_init():
global window
# Initialize the library
if not glfw.init():
print("Failed to initialize GLFW\n",file=sys.stderr)
return False
glfw.window_hint(glfw.SAMPLES, 4)
glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, GL_TRUE)
glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
# Open Window and create its OpenGL context
window = glfw.create_window(1024, 768, "Tutorial 02", None, None) #(in the accompanying source code this variable will be global)
if not window:
print("Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n",file=sys.stderr)
glfw.terminate()
return False
# Initialize GLEW
glfw.make_context_current(window)
glewExperimental = True
# GLEW is a framework for testing extension availability. Please see tutorial notes for
# more information including why can remove this code.
if glewInit() != GLEW_OK:
print("Failed to initialize GLEW\n",file=sys.stderr);
return False
return True
def main():
if not opengl_init():
return
glfw.set_input_mode(window,glfw.STICKY_KEYS,GL_TRUE)
# Set opengl clear color to something other than red (color used by the fragment shader)
glClearColor(0,0,0.4,0)
vertex_array_id = glGenVertexArrays(1)
glBindVertexArray( vertex_array_id )
program_id = common.LoadShaders( "Shaders/Tutorial2/SimpleVertexShader.vertexshader",
"Shaders/Tutorial2/SimpleFragmentShader.fragmentshader" )
vertex_data = [-1.0, -1.0, 0.0,
1.0, -1.0, 0.0,
0.0, 1.0, 0.0]
vertex_buffer = glGenBuffers(1);
# GLFloat = c_types.c_float
array_type = GLfloat * len(vertex_data)
# array_type = c_types.c_float_array_9 so unpack values of vertex array
x = array_type(*vertex_data)
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer)
glBufferData(GL_ARRAY_BUFFER, len(vertex_data) * 4, array_type(*vertex_data), GL_STATIC_DRAW)
while glfw.get_key(window,glfw.KEY_ESCAPE) != glfw.PRESS and not glfw.window_should_close(window):
glClear(GL_COLOR_BUFFER_BIT)
glUseProgram(program_id)
# Bind vertex buffer data to the attribute 0 in our shader.
# Note: This can also be done in the VAO itself (see vao_test.py)
# Enable the vertex attribute at element[0], in this case that's the triangle's vertices
# this could also be color, normals, etc. It isn't necessary to disable these
#
glEnableVertexAttribArray(0)
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
glVertexAttribPointer(
0, # attribute 0. No particular reason for 0, but must match the layout in the shader.
3, # len(vertex_data)
GL_FLOAT, # type
GL_FALSE, # ormalized?
0, # stride
null # array buffer offset (c_type == void*)
)
# Draw the triangle !
glDrawArrays(GL_TRIANGLES, 0, 3) #3 indices starting at 0 -> 1 triangle
# Not strictly necessary because we only have
glDisableVertexAttribArray(0)
# Swap front and back buffers
glfw.swap_buffers(window)
# Poll for and process events
glfw.poll_events()
# note braces around vertex_buffer and vertex_array_id.
# These 2 functions expect arrays of values
glDeleteBuffers(1, [vertex_buffer])
glDeleteProgram(program_id)
glDeleteVertexArrays(1, [vertex_array_id])
glfw.terminate()
if __name__ == "__main__":
main()