-
Notifications
You must be signed in to change notification settings - Fork 0
/
glutils.py
66 lines (55 loc) · 2.55 KB
/
glutils.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
"""
Copyright (c) 2023, UrocyonF
All rights reserved.
This source code is licensed under the BSD-style license found in the
LICENSE file in the root directory of this source tree.
Author: UrocyonF
Date: 2022 - 2023
"""
#!/usr/bin/env python3
from PIL import Image
import OpenGL.GL as GL
import os
def compile_shader(shader_content, shader_type):
shader_id = GL.glCreateShader(shader_type)
GL.glShaderSource(shader_id, shader_content)
GL.glCompileShader(shader_id)
success = GL.glGetShaderiv(shader_id, GL.GL_COMPILE_STATUS)
if not success:
log = GL.glGetShaderInfoLog(shader_id).decode('ascii')
print(f'{25*"-"}\nError compiling shader: \n\
{shader_content}\n{5*"-"}\n{log}\n{25*"-"}')
return shader_id
def create_program( vertex_source, fragment_source):
vs_id = compile_shader(vertex_source, GL.GL_VERTEX_SHADER)
fs_id = compile_shader(fragment_source, GL.GL_FRAGMENT_SHADER)
if vs_id and fs_id:
program_id = GL.glCreateProgram()
GL.glAttachShader(program_id, vs_id)
GL.glAttachShader(program_id, fs_id)
GL.glLinkProgram(program_id)
success = GL.glGetProgramiv(program_id, GL.GL_LINK_STATUS)
if not success:
log = GL.glGetProgramInfoLog(program_id).decode('ascii')
print(f'{25*"-"}\nError linking program:\n{log}\n{25*"-"}')
GL.glDeleteShader(vs_id)
GL.glDeleteShader(fs_id)
return program_id
def create_program_from_file(vs_file, fs_file):
vs_content = open(vs_file, 'r').read() if os.path.exists(vs_file)\
else print(f'{25*"-"}\nError reading file:\n{vs_file}\n{25*"-"}')
fs_content = open(fs_file, 'r').read() if os.path.exists(fs_file)\
else print(f'{25*"-"}\nError reading file:\n{fs_file}\n{25*"-"}')
return create_program(vs_content, fs_content)
def load_texture(filename):
if not os.path.exists(filename):
print(f'{25*"-"}\nError reading file:\n{filename}\n{25*"-"}')
im = Image.open(filename).transpose(Image.FLIP_TOP_BOTTOM).convert('RGBA')
texture_id = GL.glGenTextures(1)
GL.glBindTexture(GL.GL_TEXTURE_2D, texture_id)
GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_REPEAT)
GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_REPEAT)
GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR)
GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR)
GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, im.width, im.height, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, im.tobytes())
return texture_id