From 91fce520b9aa57f29301c646aa947df7c99348b5 Mon Sep 17 00:00:00 2001 From: Quantum Date: Tue, 28 Aug 2018 14:39:15 -0400 Subject: [PATCH] Converted ring to 1D texture. --- punyverse/entity.py | 6 +-- punyverse/shaders/ring.fragment.glsl | 4 +- punyverse/texture.py | 59 +++++++++++++++++++++------- 3 files changed, 49 insertions(+), 20 deletions(-) diff --git a/punyverse/entity.py b/punyverse/entity.py index 98e44b1..dd93d82 100644 --- a/punyverse/entity.py +++ b/punyverse/entity.py @@ -8,7 +8,7 @@ from six.moves import range from punyverse.glgeom import compile, glRestore, belt, Disk, OrbitVBO, Matrix4f, SimpleSphere, TangentSphere, Cube from punyverse.model import load_model, WavefrontVBO from punyverse.orbit import KeplerOrbit -from punyverse.texture import get_best_texture, load_clouds, get_cube_map +from punyverse.texture import get_best_texture, load_clouds, get_cube_map, load_texture_1d from punyverse.utils import cached_property G = 6.67384e-11 # Gravitation Constant @@ -343,7 +343,7 @@ class SphericalBody(Body): roll = world.evaluate(info['ring'].get('roll', roll)) self.ring_rotation = pitch, yaw, roll - self.ring_texture = get_best_texture(info['ring'].get('texture', None), clamp=True) + self.ring_texture = load_texture_1d(info['ring'].get('texture'), clamp=True) self.ring = Disk(distance, distance + size, 30) def _draw_planet(self): @@ -482,7 +482,7 @@ class SphericalBody(Body): shader.uniform_float('u_ambient', 0.1) glActiveTexture(GL_TEXTURE0) - glBindTexture(GL_TEXTURE_2D, self.ring_texture) + glBindTexture(GL_TEXTURE_1D, self.ring_texture) shader.uniform_texture('u_texture', 0) glBindBuffer(GL_ARRAY_BUFFER, self.ring.vbo) diff --git a/punyverse/shaders/ring.fragment.glsl b/punyverse/shaders/ring.fragment.glsl index af07020..5807b69 100644 --- a/punyverse/shaders/ring.fragment.glsl +++ b/punyverse/shaders/ring.fragment.glsl @@ -9,13 +9,13 @@ uniform vec3 u_sun; uniform vec3 u_planet; uniform float u_planetRadius; uniform float u_ambient; -uniform sampler2D u_texture; +uniform sampler1D u_texture; void main() { vec3 incident = v_position - u_sun; vec3 plane_normal = u_planet - u_sun; vec3 plane_intersect = dot(plane_normal, plane_normal) / dot(incident, plane_normal) * incident; - o_fragColor = texture(u_texture, vec2(v_u, 0)); + o_fragColor = texture(u_texture, v_u); if (length(plane_intersect) < length(incident) && distance(plane_intersect, plane_normal) <= u_planetRadius) o_fragColor.rgb *= u_ambient; diff --git a/punyverse/texture.py b/punyverse/texture.py index 2f106c6..36c3f7b 100644 --- a/punyverse/texture.py +++ b/punyverse/texture.py @@ -186,6 +186,27 @@ def get_file_path(file): return path, file +def create_texture(): + buffer = GLuint() + glGenTextures(1, byref(buffer)) + id = buffer.value + return id + + +def delete_texture(id): + buffer = GLuint(id) + glDeleteTextures(1, byref(buffer)) + + +def get_internal_mode(mode): + return { + GL_RGB: GL_RGB8, + GL_BGR: GL_RGB8, + GL_RGBA: GL_RGBA8, + GL_BGRA: GL_RGBA8, + }[mode] + + def load_texture(file, clamp=False): path, file = get_file_path(file) if path in cache: @@ -193,10 +214,7 @@ def load_texture(file, clamp=False): path, width, height, depth, mode, texture = load_image(file, path) - buffer = GLuint() - glGenTextures(1, byref(buffer)) - id = buffer.value - + id = create_texture() glBindTexture(GL_TEXTURE_2D, id) if gl_info.have_version(3) or gl_info.have_extension('GL_ARB_framebuffer_object'): @@ -219,13 +237,26 @@ def load_texture(file, clamp=False): return id -def get_internal_mode(mode): - return { - GL_RGB: GL_RGB8, - GL_BGR: GL_RGB8, - GL_RGBA: GL_RGBA8, - GL_BGRA: GL_RGBA8, - }[mode] +def load_texture_1d(file, clamp=False): + path, file = get_file_path(file) + path, width, height, depth, mode, texture = load_image(file, path) + + id = create_texture() + glBindTexture(GL_TEXTURE_1D, id) + + if gl_info.have_version(3) or gl_info.have_extension('GL_ARB_framebuffer_object'): + glTexImage1D(GL_TEXTURE_1D, 0, get_internal_mode(mode), width, 0, mode, GL_UNSIGNED_BYTE, texture) + glGenerateMipmap(GL_TEXTURE_1D) + else: + gluBuild1DMipmaps(GL_TEXTURE_1D, depth, width, mode, GL_UNSIGNED_BYTE, texture) + + if clamp: + glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) + + if gl_info.have_extension('GL_EXT_texture_filter_anisotropic'): + glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAX_ANISOTROPY_EXT, glGetInteger(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT)) + + return id def load_clouds(file): @@ -264,9 +295,7 @@ def get_cube_map(files, callback=None): assert len(files) == 6 callback = callback or (lambda index, file: None) - buffer = GLuint() - glGenTextures(1, byref(buffer)) - id = buffer.value + id = create_texture() glBindTexture(GL_TEXTURE_CUBE_MAP, id) glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BASE_LEVEL, 0) @@ -284,7 +313,7 @@ def get_cube_map(files, callback=None): callback(index, file) path, width, height, depth, mode, texture = load_image(file, path) except Exception: - glDeleteTextures(1, byref(buffer)) + delete_texture(id) raise glTexImage2D(part, 0, get_internal_mode(mode), width, height, 0, mode, GL_UNSIGNED_BYTE, texture)