diff options
author | rom <romangraef@gmail.com> | 2021-04-23 23:29:43 +0200 |
---|---|---|
committer | rom <romangraef@gmail.com> | 2021-04-23 23:59:12 +0200 |
commit | e5b6ba623aeb28ad9408358c58a3da12b1126396 (patch) | |
tree | 70b3474196f28e4a2059c245888a6a4ac91a677b | |
parent | 99ab218d9a0964a9de10cf621ff82d4fbaaf50b9 (diff) | |
download | txtgameengine-e5b6ba623aeb28ad9408358c58a3da12b1126396.tar.gz txtgameengine-e5b6ba623aeb28ad9408358c58a3da12b1126396.tar.bz2 txtgameengine-e5b6ba623aeb28ad9408358c58a3da12b1126396.zip |
textures
-rw-r--r-- | MANIFEST.in | 2 | ||||
-rw-r--r-- | requirements.txt | 1 | ||||
-rw-r--r-- | test_image.png | bin | 0 -> 3133555 bytes | |||
-rw-r--r-- | txtgameengine/__main__.py | 49 | ||||
-rw-r--r-- | txtgameengine/base_shaders/vertex.glsl | 8 | ||||
-rw-r--r-- | txtgameengine/builtin_shaders/basic/fragment.glsl (renamed from txtgameengine/base_shaders/fragment.glsl) | 0 | ||||
-rw-r--r-- | txtgameengine/builtin_shaders/basic/vertex.glsl | 7 | ||||
-rw-r--r-- | txtgameengine/builtin_shaders/texture/fragment.glsl | 12 | ||||
-rw-r--r-- | txtgameengine/builtin_shaders/texture/vertex.glsl | 12 | ||||
-rw-r--r-- | txtgameengine/fonts.py | 26 | ||||
-rw-r--r-- | txtgameengine/platform.py | 37 | ||||
-rw-r--r-- | txtgameengine/twod/__init__.py | 1 | ||||
-rw-r--r-- | txtgameengine/twod/textures.py | 11 |
13 files changed, 143 insertions, 23 deletions
diff --git a/MANIFEST.in b/MANIFEST.in index e23b1aa..a96ea88 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1 +1 @@ -recursive-include txtgameengine/base_shaders *
\ No newline at end of file +recursive-include txtgameengine/builtin_shaders *
\ No newline at end of file diff --git a/requirements.txt b/requirements.txt index c750e50..48fc8ac 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ glfw~=2.1.0 pyopengl~=3.1.5 numpy~=1.20.0 +pillow~=8.2.0 diff --git a/test_image.png b/test_image.png Binary files differnew file mode 100644 index 0000000..6264a7a --- /dev/null +++ b/test_image.png diff --git a/txtgameengine/__main__.py b/txtgameengine/__main__.py index 38e9815..1ebc309 100644 --- a/txtgameengine/__main__.py +++ b/txtgameengine/__main__.py @@ -1,24 +1,25 @@ import numpy as np +from PIL import Image from .scenes import SceneTxtGameApp, Scene from OpenGL.GL import * from pathlib import Path -shader_path = Path(__file__).parent / 'base_shaders' +shader_path = Path(__file__).parent / 'builtin_shaders' class TriangleScene(Scene): TRIANGLE_DATA = [ - -1.0, -1.0, 0.0, - 1.0, -1.0, 0.0, - 0.0, 1.0, 0.0, + -1.0, -1.0, + 1.0, -1.0, + 0.0, 1.0, ] def on_enter(self): self.default_shaders = self.app.shaders.load_shaders( - str(shader_path / 'vertex.glsl'), str(shader_path / 'fragment.glsl')) + str(shader_path / 'basic/vertex.glsl'), str(shader_path / 'basic/fragment.glsl')) - self.tri_buffer = self.app.render.setup_triangle( + self.tri_buffer = self.app.render.setup_buffer( np.array(self.TRIANGLE_DATA, np.float32)) self.t = 0 @@ -33,14 +34,42 @@ class TriangleScene(Scene): class EvilTriangleScene(TriangleScene): TRIANGLE_DATA = [ - -1.0, 1.0, 0.0, - 1.0, 1.0, 0.0, - 0.0, -1.0, 0.0, + -1.0, 1.0, + 1.0, 1.0, + 0.0, -1.0, ] + def update(self, delta): + super().update(delta) + self.t = 0 + + +class TextureScene(Scene): + def on_enter(self): + self.texture_shaders = self.app.shaders.load_shaders(str(shader_path / 'texture/vertex.glsl'), + str(shader_path / 'texture/fragment.glsl')) + self.sampler_location = self.app.shaders.get_uniform_location(self.texture_shaders, 'textureSampler') + self.texture = self.app.render.setup_texture_from_pil(Image.open('test_image.png')) + self.triangle = self.app.render.setup_buffer( + np.array([ + -1.0, 1.0, + 1.0, 1.0, + -1.0, -1.0, + ], np.float32)) + self.uvs = self.app.render.setup_buffer( + np.array([ + 0, 0, + 1, 0, + 0, 1, + ], np.float32)) + + def update(self, delta: float): + with self.texture_shaders: + self.app.render.textured_triangle(self.sampler_location, self.texture, self.triangle, self.uvs) + class TestApp(SceneTxtGameApp): - MAIN_SCENE_T = TriangleScene + MAIN_SCENE_T = TextureScene def init(self): super().init() diff --git a/txtgameengine/base_shaders/vertex.glsl b/txtgameengine/base_shaders/vertex.glsl deleted file mode 100644 index 1e02417..0000000 --- a/txtgameengine/base_shaders/vertex.glsl +++ /dev/null @@ -1,8 +0,0 @@ -#version 330 core - -layout(location = 0) in vec3 vertexPosition_modelspace; - -void main() { - gl_Position.xyz = vertexPosition_modelspace; - gl_Position.w = 1.0; -} diff --git a/txtgameengine/base_shaders/fragment.glsl b/txtgameengine/builtin_shaders/basic/fragment.glsl index a9dfa6f..a9dfa6f 100644 --- a/txtgameengine/base_shaders/fragment.glsl +++ b/txtgameengine/builtin_shaders/basic/fragment.glsl diff --git a/txtgameengine/builtin_shaders/basic/vertex.glsl b/txtgameengine/builtin_shaders/basic/vertex.glsl new file mode 100644 index 0000000..92f7c97 --- /dev/null +++ b/txtgameengine/builtin_shaders/basic/vertex.glsl @@ -0,0 +1,7 @@ +#version 330 core + +layout(location = 0) in vec2 vertexPosition_modelspace; + +void main() { + gl_Position = vec4(vertexPosition_modelspace, 0, 1); +} diff --git a/txtgameengine/builtin_shaders/texture/fragment.glsl b/txtgameengine/builtin_shaders/texture/fragment.glsl new file mode 100644 index 0000000..d9d36fd --- /dev/null +++ b/txtgameengine/builtin_shaders/texture/fragment.glsl @@ -0,0 +1,12 @@ +#version 330 core + +in vec2 UV; + +out vec3 color; + +uniform sampler2D textureSampler; + +void main() { + color = texture(textureSampler, UV).rgb; +} + diff --git a/txtgameengine/builtin_shaders/texture/vertex.glsl b/txtgameengine/builtin_shaders/texture/vertex.glsl new file mode 100644 index 0000000..dd93afb --- /dev/null +++ b/txtgameengine/builtin_shaders/texture/vertex.glsl @@ -0,0 +1,12 @@ +#version 330 core + +layout(location=0) in vec3 vertexPosition_modelspace; +layout(location=1) in vec2 vertexUV; + +out vec2 UV; + +void main() { + gl_Position = vec4(vertexPosition_modelspace, 1); + UV = vertexUV; +} + diff --git a/txtgameengine/fonts.py b/txtgameengine/fonts.py new file mode 100644 index 0000000..60660d6 --- /dev/null +++ b/txtgameengine/fonts.py @@ -0,0 +1,26 @@ +from abc import ABC +from dataclasses import dataclass +from typing import Optional + + +class Font(ABC): + def get_glyph(self, char: str) -> Optional['Glyph']: + raise NotImplementedError() + + @property + def font(self): + raise NotImplementedError() + + +@dataclass +class Glyph: + font: 'Font' + x_texture_offset: int + y_texture_offset: int + x_width: int + y_width: int + + +class MonospacedFont(Font): + def get_glyph(self, char: str): + pass 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) diff --git a/txtgameengine/twod/__init__.py b/txtgameengine/twod/__init__.py new file mode 100644 index 0000000..ec5e8aa --- /dev/null +++ b/txtgameengine/twod/__init__.py @@ -0,0 +1 @@ +from .textures import Texture diff --git a/txtgameengine/twod/textures.py b/txtgameengine/twod/textures.py new file mode 100644 index 0000000..01dde52 --- /dev/null +++ b/txtgameengine/twod/textures.py @@ -0,0 +1,11 @@ +import typing + +if typing.TYPE_CHECKING: + from ..app import TxtGameApp + + +class Texture: + def __init__(self, app: 'TxtGameApp', gl_texid: int): + self.app = app + self.gl_texid = gl_texid + |