diff options
author | rgraef <romangraef@gmail.com> | 2021-04-22 13:26:56 +0200 |
---|---|---|
committer | rgraef <romangraef@gmail.com> | 2021-04-22 13:26:56 +0200 |
commit | 2b597ca9617fd424d0785afae15b9f7adc78ef70 (patch) | |
tree | 74bf5a12d86517641c95e1c720649be35102635f /txtgameengine/platform.py | |
parent | a165c0da2484776124bf895a810076c8acf94368 (diff) | |
download | txtgameengine-2b597ca9617fd424d0785afae15b9f7adc78ef70.tar.gz txtgameengine-2b597ca9617fd424d0785afae15b9f7adc78ef70.tar.bz2 txtgameengine-2b597ca9617fd424d0785afae15b9f7adc78ef70.zip |
working triangles
Diffstat (limited to 'txtgameengine/platform.py')
-rw-r--r-- | txtgameengine/platform.py | 63 |
1 files changed, 60 insertions, 3 deletions
diff --git a/txtgameengine/platform.py b/txtgameengine/platform.py index 0d89213..f5b8ded 100644 --- a/txtgameengine/platform.py +++ b/txtgameengine/platform.py @@ -1,7 +1,8 @@ import glfw import typing -from OpenGL import GL +from OpenGL.GL import * +import OpenGL.GL.shaders as shaders if typing.TYPE_CHECKING: from .app import TxtGameApp @@ -20,6 +21,7 @@ class PlatformComponent: glfw.init() self.init_window() glfw.make_context_current(self.window) + glViewport(0, 0, *self.app.size) @staticmethod def monotonic_time(): @@ -32,6 +34,9 @@ class PlatformComponent: def init_window(self): glfw.window_hint(glfw.CLIENT_API, glfw.OPENGL_API) glfw.window_hint(glfw.RESIZABLE, glfw.FALSE) + glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, glfw.TRUE) + glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) + glfw.window_hint(glfw.OPENGL_DEBUG_CONTEXT, glfw.TRUE) glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3) self.window = glfw.create_window( @@ -60,8 +65,60 @@ class PlatformComponent: @staticmethod def set_clear_color(r, g, b, a): - GL.glClearColor(r, g, b, a) + glClearColor(r, g, b, a) + + @staticmethod + def print_debug_message(source, msg_type, msg_id, severity, length, raw, user): + print('OPENGL ERROR', source, msg_type, msg_id, severity) + + def check_debug(self): + assert (glGetIntegerv(GL_CONTEXT_FLAGS) + & GL_CONTEXT_FLAG_DEBUG_BIT) != 0 + glDebugMessageCallback(GLDEBUGPROC(self.print_debug_message), None) @staticmethod def clear_background(depth_buffer=False): - GL.glClear(GL.GL_COLOR_BUFFER_BIT | (depth_buffer and GL.GL_DEPTH_BUFFER_BIT or 0)) + glClear(GL_COLOR_BUFFER_BIT | ( + depth_buffer and GL_DEPTH_BUFFER_BIT or 0)) + + +class ShaderComponent: + def __init__(self, app: 'TxtGameApp'): + self.app = app + + @staticmethod + def load_shaders(vertex_file, fragment_file): + with open(vertex_file) as fp: + vertex_source = fp.read() + with open(fragment_file) as fp: + fragment_source = fp.read() + vertex_shader = shaders.compileShader(vertex_source, GL_VERTEX_SHADER) + fragment_shader = shaders.compileShader( + fragment_source, GL_FRAGMENT_SHADER) + return shaders.compileProgram(vertex_shader, fragment_shader) + + +class RenderComponent: + def __init__(self, app: 'TxtGameApp'): + self.app = app + + @staticmethod + def setup_vertex_arrays(): + arr = glGenVertexArrays(1) + glBindVertexArray(arr) + + @staticmethod + def setup_triangle(arr): + buf = glGenBuffers(1) + glBindBuffer(GL_ARRAY_BUFFER, buf) + glBufferData(GL_ARRAY_BUFFER, arr.itemsize * + arr.size, arr, GL_STATIC_DRAW) + return buf + + @staticmethod + def triangle(buf): + glEnableVertexAttribArray(0) + glBindBuffer(GL_ARRAY_BUFFER, buf) + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, None) + glDrawArrays(GL_TRIANGLES, 0, 3) + glDisableVertexAttribArray(0) |