summaryrefslogtreecommitdiff
path: root/txtgameengine
diff options
context:
space:
mode:
authorrom <romangraef@gmail.com>2021-04-23 23:29:43 +0200
committerrom <romangraef@gmail.com>2021-04-23 23:59:12 +0200
commite5b6ba623aeb28ad9408358c58a3da12b1126396 (patch)
tree70b3474196f28e4a2059c245888a6a4ac91a677b /txtgameengine
parent99ab218d9a0964a9de10cf621ff82d4fbaaf50b9 (diff)
downloadtxtgameengine-e5b6ba623aeb28ad9408358c58a3da12b1126396.tar.gz
txtgameengine-e5b6ba623aeb28ad9408358c58a3da12b1126396.tar.bz2
txtgameengine-e5b6ba623aeb28ad9408358c58a3da12b1126396.zip
textures
Diffstat (limited to 'txtgameengine')
-rw-r--r--txtgameengine/__main__.py49
-rw-r--r--txtgameengine/base_shaders/vertex.glsl8
-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.glsl7
-rw-r--r--txtgameengine/builtin_shaders/texture/fragment.glsl12
-rw-r--r--txtgameengine/builtin_shaders/texture/vertex.glsl12
-rw-r--r--txtgameengine/fonts.py26
-rw-r--r--txtgameengine/platform.py37
-rw-r--r--txtgameengine/twod/__init__.py1
-rw-r--r--txtgameengine/twod/textures.py11
10 files changed, 141 insertions, 22 deletions
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
+