-
Notifications
You must be signed in to change notification settings - Fork 18
/
minimal_example_shader_mesh.py
143 lines (118 loc) · 4.17 KB
/
minimal_example_shader_mesh.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
132
133
134
135
136
137
138
139
140
141
142
143
from OpenGL.GLUT import *
from OpenGL.GLU import *
from OpenGL.GL import *
import OpenGL.GL.shaders
import numpy as np
from ctypes import c_void_p
import glm
import math
sh_vert = """
#version 460 core
layout (location = 0) in vec4 a_position;
layout (location = 1) in vec3 a_normal;
layout (location = 2) in vec3 a_uvw;
out vec3 v_pos;
out vec3 v_nv;
out vec3 v_uvw;
layout (location = 0) uniform mat4 u_projection;
layout (location = 1) uniform mat4 u_view;
layout (location = 2) uniform mat4 u_model;
void main()
{
v_pos = vec3(u_model * a_position);
v_nv = inverse(transpose(mat3(u_model))) * a_normal;
v_uvw = a_uvw;
gl_Position = u_projection * u_view * u_model * a_position;
}
"""
sh_frag = """
#version 460 core
out vec4 frag_color;
in vec3 v_pos;
in vec3 v_nv;
in vec3 v_uvw;
layout (location = 1) uniform mat4 u_view;
vec3 HUEtoRGB(in float H)
{
float R = abs(H * 6.0 - 3.0) - 1.0;
float G = 2.0 - abs(H * 6.0 - 2.0);
float B = 2.0 - abs(H * 6.0 - 4.0);
return clamp(vec3(R, G, B), 0.0, 1.0);
}
void main()
{
vec4 color = vec4(HUEtoRGB(v_uvw.z), 1.0);
vec3 L = normalize(vec3(1.0, -1.0, 1.0));
vec3 eye = inverse(u_view)[3].xyz;
vec3 V = normalize(eye - v_pos);
float face = sign(dot(v_nv, V));
vec3 N = normalize(v_nv) * face;
vec3 H = normalize(V + L);
float ka = 0.5;
float kd = max(0.0, dot(N, L)) * 0.5;
float NdotH = max(0.0, dot(N, H));
float sh = 100.0;
float ks = pow(NdotH, sh) * 0.1;
frag_color = vec4(color.rgb * (ka + kd + ks), color.a);
}
"""
def display():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
elapsed_ms = glutGet(GLUT_ELAPSED_TIME)
projection = glm.perspective(glm.radians(60), aspect, 0.1, 10.0)
view = glm.lookAt(glm.vec3(0, 0, 4), glm.vec3(0, 0, 0), glm.vec3(0, 1, 0))
angle = elapsed_ms * math.pi * 2 / 5000.0
model = glm.rotate(glm.mat4(1), angle * 0.5, glm.vec3(0, 1, 0))
model = glm.rotate(model, angle, glm.vec3(1, 0, 0))
glUniformMatrix4fv(0, 1, GL_FALSE, glm.value_ptr(projection))
glUniformMatrix4fv(1, 1, GL_FALSE, glm.value_ptr(view))
glUniformMatrix4fv(2, 1, GL_FALSE, glm.value_ptr(model))
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, None)
glutSwapBuffers()
glutPostRedisplay()
def reshape(width, height):
global aspect
glViewport(0, 0, width, height)
aspect = width / height
glutInit(sys.argv)
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_MULTISAMPLE)
glutSetOption(GLUT_MULTISAMPLE, 8)
glutInitWindowSize(640, 480)
glutCreateWindow(b"OpenGL Window")
glutDisplayFunc(display)
glutReshapeFunc(reshape)
vertices = [(-1,-1,-1), (1,-1,-1), (1, 1,-1), (-1, 1,-1), (-1,-1, 1), (1,-1, 1), (1, 1, 1), (-1, 1, 1)]
uv = [(0,0), (1,0), (1, 1), (0,1)]
faces = [[0,1,2,3], [1,5,6,2], [5,4,7,6], [4,0,3,7], [3,2,6,7], [1,0,4,5]]
normals = [(0,0,-1), (1,0,0), (0,0,1), (-1,0,0), (0,1,0), (0,-1,0)]
attributes = []
indices = []
for si, f in enumerate(faces):
for qi, i in enumerate(f):
attributes.append(list(vertices[i]) + list(normals[si]) + [*uv[qi], si/len(faces)])
indices.append([4*si, 4*si+1, 4*si+2, 4*si, 4*si+2, 4*si+3])
attributes = np.array(attributes, dtype=np.float32)
indices = np.array(indices, dtype=np.uint32)
vao = glGenVertexArrays(1)
vbo = glGenBuffers(1)
ebo = glGenBuffers(1)
glBindVertexArray(vao)
glBindBuffer(GL_ARRAY_BUFFER, vbo)
glBufferData(GL_ARRAY_BUFFER, attributes, GL_STATIC_DRAW)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo)
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices, GL_STATIC_DRAW)
glVertexAttribPointer(0, 3, GL_FLOAT, False, 9 * attributes.itemsize, None)
glVertexAttribPointer(1, 3, GL_FLOAT, False, 9 * attributes.itemsize, c_void_p(3 * attributes.itemsize))
glVertexAttribPointer(2, 3, GL_FLOAT, False, 9 * attributes.itemsize, c_void_p(6 * attributes.itemsize))
glEnableVertexAttribArray(0)
glEnableVertexAttribArray(1)
glEnableVertexAttribArray(2)
program = OpenGL.GL.shaders.compileProgram(
OpenGL.GL.shaders.compileShader(sh_vert, GL_VERTEX_SHADER),
OpenGL.GL.shaders.compileShader(sh_frag, GL_FRAGMENT_SHADER)
)
glUseProgram(program)
glEnable(GL_MULTISAMPLE) # default
glEnable(GL_DEPTH_TEST)
glClearColor(0.0, 0.0, 0.0, 0.0)
glutMainLoop()