diff options
Diffstat (limited to 'txtgameengine/platform.py')
-rw-r--r-- | txtgameengine/platform.py | 37 |
1 files changed, 33 insertions, 4 deletions
diff --git a/txtgameengine/platform.py b/txtgameengine/platform.py index f5b8ded..17d85ab 100644 --- a/txtgameengine/platform.py +++ b/txtgameengine/platform.py @@ -1,8 +1,11 @@ import glfw import typing +import numpy as np from OpenGL.GL import * import OpenGL.GL.shaders as shaders +from PIL import Image +from .twod import Texture if typing.TYPE_CHECKING: from .app import TxtGameApp @@ -79,7 +82,7 @@ class PlatformComponent: @staticmethod def clear_background(depth_buffer=False): glClear(GL_COLOR_BUFFER_BIT | ( - depth_buffer and GL_DEPTH_BUFFER_BIT or 0)) + depth_buffer and GL_DEPTH_BUFFER_BIT or 0)) class ShaderComponent: @@ -97,6 +100,10 @@ class ShaderComponent: fragment_source, GL_FRAGMENT_SHADER) return shaders.compileProgram(vertex_shader, fragment_shader) + @staticmethod + def get_uniform_location(shader, name): + return glGetUniformLocation(shader, name) + class RenderComponent: def __init__(self, app: 'TxtGameApp'): @@ -108,17 +115,39 @@ class RenderComponent: glBindVertexArray(arr) @staticmethod - def setup_triangle(arr): + def setup_buffer(arr, mode=GL_STATIC_DRAW): buf = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, buf) glBufferData(GL_ARRAY_BUFFER, arr.itemsize * - arr.size, arr, GL_STATIC_DRAW) + arr.size, arr, mode) return buf @staticmethod def triangle(buf): glEnableVertexAttribArray(0) glBindBuffer(GL_ARRAY_BUFFER, buf) - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, None) + glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, None) glDrawArrays(GL_TRIANGLES, 0, 3) glDisableVertexAttribArray(0) + + def textured_triangle(self, shader_location, texture, triangle, uvs): + glActiveTexture(GL_TEXTURE0) + glBindTexture(GL_TEXTURE_2D, texture.gl_texid) + glUniform1i(shader_location, 0) + glEnableVertexAttribArray(1) + glBindBuffer(GL_ARRAY_BUFFER, uvs) + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, None) + self.triangle(triangle) + glDisableVertexAttribArray(1) + + def setup_texture_from_pil(self, img: Image.Image): + return self.setup_texture(*img.size, np.array(list(img.getdata()), np.uint8)) + + def setup_texture(self, width: int, height: int, data: np.ndarray): + tex_id = glGenTextures(1) + glPixelStorei(GL_UNPACK_ALIGNMENT, 4) + glBindTexture(GL_TEXTURE_2D, tex_id) + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0) + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0) + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data) + return Texture(self.app, tex_id) |