-
Notifications
You must be signed in to change notification settings - Fork 0
/
shader.py
39 lines (28 loc) · 1.21 KB
/
shader.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
"""This module represents a vertex+fragment shader pair.
This is a simple abstraction to make loading them from disk and using
them easier.
"""
from OpenGL.GL import * #type:ignore
def readFile(path):
with open(path, 'r') as f:
return f.read()
class ShaderProgram:
shaderProgram: int
def __init__(self, vertexPath: str, fragmentPath: str):
# From https://learnopengl.com
vertexShader = glCreateShader(GL_VERTEX_SHADER)
glShaderSource(vertexShader, readFile(vertexPath)) #type:ignore
glCompileShader(vertexShader)
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER)
glShaderSource(fragmentShader, readFile(fragmentPath)) #type:ignore
glCompileShader(fragmentShader)
self.shaderProgram = glCreateProgram() #type:ignore
glAttachShader(self.shaderProgram, vertexShader)
glAttachShader(self.shaderProgram, fragmentShader)
glLinkProgram(self.shaderProgram)
glDeleteShader(vertexShader)
glDeleteShader(fragmentShader)
def useProgram(self):
glUseProgram(self.shaderProgram)
def getUniformLocation(self, name: str) -> int:
return glGetUniformLocation(self.shaderProgram, name)