forked from dantros/grafica
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex_triangle.py
109 lines (82 loc) · 3.01 KB
/
ex_triangle.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
# coding=utf-8
"""Hello OpenGL!"""
import glfw
from OpenGL.GL import *
import OpenGL.GL.shaders
import numpy
import sys
__author__ = "Daniel Calderon"
__license__ = "MIT"
# We will use 32 bits data, so an integer has 4 bytes
# 1 byte = 8 bits
SIZE_IN_BYTES = 4
if __name__ == "__main__":
# Initialize glfw
if not glfw.init():
glfw.set_window_should_close(window, True)
width = 600
height = 600
window = glfw.create_window(width, height, "Simple Triangle - Modern OpenGL", None, None)
if not window:
glfw.terminate()
glfw.set_window_should_close(window, True)
glfw.make_context_current(window)
# Defining the triangle and its colors at each vertex
# positions colors
triangle = [-0.5, -0.5, 0.0, 1.0, 0.0, 0.0,
0.5, -0.5, 0.0, 0.0, 1.0, 0.0,
0.0, 0.5, 0.0, 0.0, 0.0, 1.0]
triangle = numpy.array(triangle, dtype = numpy.float32)
# Defining shaders for our pipeline
vertex_shader = """
#version 130
in vec3 position;
in vec3 color;
out vec3 newColor;
void main()
{
gl_Position = vec4(position, 1.0f);
newColor = color;
}
"""
fragment_shader = """
#version 130
in vec3 newColor;
out vec4 outColor;
void main()
{
outColor = vec4(newColor, 1.0f);
}
"""
# Assembling the shader program (pipeline) with both shaders
shaderProgram = OpenGL.GL.shaders.compileProgram(
OpenGL.GL.shaders.compileShader(vertex_shader, GL_VERTEX_SHADER),
OpenGL.GL.shaders.compileShader(fragment_shader, GL_FRAGMENT_SHADER))
# Each shape must be attached to a Vertex Buffer Object (VBO)
VBO = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, VBO)
glBufferData(GL_ARRAY_BUFFER, len(triangle) * SIZE_IN_BYTES, triangle, GL_STATIC_DRAW)
# Setting up the location of the attributes position and color from the VBO
# A vertex attribute has 3 integers for the position (each is 4 bytes),
# and 3 numbers to represent the color (each is 4 bytes),
# Henceforth, we have 3*4 + 3*4 = 24 bytes
position = glGetAttribLocation(shaderProgram, "position")
glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(0))
glEnableVertexAttribArray(position)
color = glGetAttribLocation(shaderProgram, "color")
glVertexAttribPointer(color, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(12))
glEnableVertexAttribArray(color)
# Telling OpenGL to use our shader program
glUseProgram(shaderProgram)
# Setting up the clear screen color
glClearColor(0.15, 0.15, 0.15, 1.0)
glClear(GL_COLOR_BUFFER_BIT)
# It renders a scene using the active shader program (pipeline) and the active VAO (shapes)
glDrawArrays(GL_TRIANGLES, 0, 3)
# Moving our draw to the active color buffer
glfw.swap_buffers(window)
# Waiting to close the window
while not glfw.window_should_close(window):
# Getting events from GLFW
glfw.poll_events()
glfw.terminate()