Converted ring to 1D texture.

This commit is contained in:
Quantum 2018-08-28 14:39:15 -04:00
parent 9d73059ad0
commit 91fce520b9
3 changed files with 49 additions and 20 deletions

View file

@ -8,7 +8,7 @@ from six.moves import range
from punyverse.glgeom import compile, glRestore, belt, Disk, OrbitVBO, Matrix4f, SimpleSphere, TangentSphere, Cube from punyverse.glgeom import compile, glRestore, belt, Disk, OrbitVBO, Matrix4f, SimpleSphere, TangentSphere, Cube
from punyverse.model import load_model, WavefrontVBO from punyverse.model import load_model, WavefrontVBO
from punyverse.orbit import KeplerOrbit 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 from punyverse.utils import cached_property
G = 6.67384e-11 # Gravitation Constant G = 6.67384e-11 # Gravitation Constant
@ -343,7 +343,7 @@ class SphericalBody(Body):
roll = world.evaluate(info['ring'].get('roll', roll)) roll = world.evaluate(info['ring'].get('roll', roll))
self.ring_rotation = pitch, yaw, 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) self.ring = Disk(distance, distance + size, 30)
def _draw_planet(self): def _draw_planet(self):
@ -482,7 +482,7 @@ class SphericalBody(Body):
shader.uniform_float('u_ambient', 0.1) shader.uniform_float('u_ambient', 0.1)
glActiveTexture(GL_TEXTURE0) glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, self.ring_texture) glBindTexture(GL_TEXTURE_1D, self.ring_texture)
shader.uniform_texture('u_texture', 0) shader.uniform_texture('u_texture', 0)
glBindBuffer(GL_ARRAY_BUFFER, self.ring.vbo) glBindBuffer(GL_ARRAY_BUFFER, self.ring.vbo)

View file

@ -9,13 +9,13 @@ uniform vec3 u_sun;
uniform vec3 u_planet; uniform vec3 u_planet;
uniform float u_planetRadius; uniform float u_planetRadius;
uniform float u_ambient; uniform float u_ambient;
uniform sampler2D u_texture; uniform sampler1D u_texture;
void main() { void main() {
vec3 incident = v_position - u_sun; vec3 incident = v_position - u_sun;
vec3 plane_normal = u_planet - u_sun; vec3 plane_normal = u_planet - u_sun;
vec3 plane_intersect = dot(plane_normal, plane_normal) / dot(incident, plane_normal) * incident; 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) && if (length(plane_intersect) < length(incident) &&
distance(plane_intersect, plane_normal) <= u_planetRadius) distance(plane_intersect, plane_normal) <= u_planetRadius)
o_fragColor.rgb *= u_ambient; o_fragColor.rgb *= u_ambient;

View file

@ -186,6 +186,27 @@ def get_file_path(file):
return 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): def load_texture(file, clamp=False):
path, file = get_file_path(file) path, file = get_file_path(file)
if path in cache: if path in cache:
@ -193,10 +214,7 @@ def load_texture(file, clamp=False):
path, width, height, depth, mode, texture = load_image(file, path) path, width, height, depth, mode, texture = load_image(file, path)
buffer = GLuint() id = create_texture()
glGenTextures(1, byref(buffer))
id = buffer.value
glBindTexture(GL_TEXTURE_2D, id) glBindTexture(GL_TEXTURE_2D, id)
if gl_info.have_version(3) or gl_info.have_extension('GL_ARB_framebuffer_object'): 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 return id
def get_internal_mode(mode): def load_texture_1d(file, clamp=False):
return { path, file = get_file_path(file)
GL_RGB: GL_RGB8, path, width, height, depth, mode, texture = load_image(file, path)
GL_BGR: GL_RGB8,
GL_RGBA: GL_RGBA8, id = create_texture()
GL_BGRA: GL_RGBA8, glBindTexture(GL_TEXTURE_1D, id)
}[mode]
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): def load_clouds(file):
@ -264,9 +295,7 @@ def get_cube_map(files, callback=None):
assert len(files) == 6 assert len(files) == 6
callback = callback or (lambda index, file: None) callback = callback or (lambda index, file: None)
buffer = GLuint() id = create_texture()
glGenTextures(1, byref(buffer))
id = buffer.value
glBindTexture(GL_TEXTURE_CUBE_MAP, id) glBindTexture(GL_TEXTURE_CUBE_MAP, id)
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BASE_LEVEL, 0) glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BASE_LEVEL, 0)
@ -284,7 +313,7 @@ def get_cube_map(files, callback=None):
callback(index, file) callback(index, file)
path, width, height, depth, mode, texture = load_image(file, path) path, width, height, depth, mode, texture = load_image(file, path)
except Exception: except Exception:
glDeleteTextures(1, byref(buffer)) delete_texture(id)
raise raise
glTexImage2D(part, 0, get_internal_mode(mode), width, height, 0, mode, GL_UNSIGNED_BYTE, texture) glTexImage2D(part, 0, get_internal_mode(mode), width, height, 0, mode, GL_UNSIGNED_BYTE, texture)