diff --git a/punyverse/assets/textures.txt b/punyverse/assets/textures.txt index 5b915b5..d7e7b70 100644 --- a/punyverse/assets/textures.txt +++ b/punyverse/assets/textures.txt @@ -5,6 +5,7 @@ mercury.jpg earth.jpg earth_normal.jpg earth_emission.jpg +earth_specular.jpg cloudmap.jpg moon.jpg mars.jpg diff --git a/punyverse/assets/textures/earth_specular.jpg b/punyverse/assets/textures/earth_specular.jpg new file mode 100644 index 0000000..baa34ff Binary files /dev/null and b/punyverse/assets/textures/earth_specular.jpg differ diff --git a/punyverse/entity.py b/punyverse/entity.py index 9339590..331b1a4 100644 --- a/punyverse/entity.py +++ b/punyverse/entity.py @@ -293,6 +293,7 @@ class SphericalBody(Body): self.texture = get_best_texture(info['texture']) self.normal_texture = None + self.specular_texture = None self.emission_texture = None self.sphere = self._get_sphere(division, tangent=self.type == 'planet') @@ -303,6 +304,9 @@ class SphericalBody(Body): if 'normal_map' in info: self.normal_texture = get_best_texture(info['normal_map']) + if 'specular_map' in info: + self.specular_texture = get_best_texture(info['specular_map']) + if 'emission_map' in info: self.emission_texture = get_best_texture(info['emission_map']) @@ -349,7 +353,16 @@ class SphericalBody(Body): glBindTexture(GL_TEXTURE_2D, self.normal_texture) shader.uniform_texture('u_planet.normalMap', 1) - shader.uniform_bool('u_planet.hasSpecular', False) + shader.uniform_bool('u_planet.hasSpecular', self.specular_texture) + if self.specular_texture: + glActiveTexture(GL_TEXTURE2) + glBindTexture(GL_TEXTURE_2D, self.specular_texture) + shader.uniform_texture('u_planet.specularMap', 2) + shader.uniform_vec3('u_planet.specular', 1, 1, 1) + shader.uniform_float('u_planet.shininess', 10) + else: + shader.uniform_vec3('u_planet.specular', 0, 0, 0) + shader.uniform_float('u_planet.shininess', 0) shader.uniform_bool('u_planet.hasEmission', self.emission_texture) if self.emission_texture: @@ -363,9 +376,6 @@ class SphericalBody(Body): shader.uniform_vec3('u_planet.emission', 0, 0, 0) shader.uniform_vec3('u_planet.diffuse', 1, 1, 1) - shader.uniform_vec3('u_planet.specular', 0, 0, 0) - - shader.uniform_float('u_planet.shininess', 0) glBindBuffer(GL_ARRAY_BUFFER, self.sphere.vbo) shader.vertex_attribute('a_normal', self.sphere.direction_size, self.sphere.type, GL_FALSE, diff --git a/punyverse/shaders/planet.fragment.glsl b/punyverse/shaders/planet.fragment.glsl index 5a2dc79..e03b903 100644 --- a/punyverse/shaders/planet.fragment.glsl +++ b/punyverse/shaders/planet.fragment.glsl @@ -39,18 +39,15 @@ void main() { vec3 emission = u_planet.hasEmission ? texture2D(u_planet.emissionMap, v_uv).rgb : vec3(1); vec3 incident = normalize(u_sun.position - v_position); - vec3 reflected = normalize(-reflect(incident, normal)); + vec3 reflected = normalize(reflect(-incident, normal)); float diffuseIntensity = max(dot(normal, incident), 0.0); - float shininess = pow(clamp(dot(normalize(v_camDirection), reflected), 0.0, 1.0), u_planet.shininess); + float shininess = pow(max(dot(normalize(v_camDirection), reflected), 0), u_planet.shininess); vec3 ambient = u_planet.ambient * u_sun.ambient * diffuse; diffuse *= u_planet.diffuse * u_sun.diffuse * diffuseIntensity; emission *= u_planet.emission * (1 - min(diffuseIntensity * 2, 1)); - if (u_planet.shininess > 0) - specular *= u_planet.specular * u_sun.specular * clamp(shininess, 0, 1); - else - specular = vec3(0); + specular *= u_planet.specular * u_sun.specular * max(shininess, 0) * diffuseIntensity; gl_FragColor = vec4((ambient + diffuse + emission + specular) * u_sun.intensity, 1); } diff --git a/punyverse/world.json b/punyverse/world.json index cb9936f..1bbd145 100644 --- a/punyverse/world.json +++ b/punyverse/world.json @@ -57,6 +57,7 @@ "rotation": 86400, "division": 90, "normal_map": ["earth_normal.jpg", "earth_normal_small.jpg"], + "specular_map": ["earth_specular.jpg", "earth_specular_small.jpg"], "emission_map": ["earth_emission.jpg", "earth_emission_medium.jpg", "earth_emission_small.jpg"], "atmosphere": { "cloud_texture": ["cloudmap.jpg", "cloudmap_small.jpg"],