Add specular mapping.

This commit is contained in:
Quantum 2018-08-26 04:42:46 -04:00
parent 25e28c3c12
commit 04758850f5
5 changed files with 19 additions and 10 deletions

View file

@ -5,6 +5,7 @@ mercury.jpg
earth.jpg earth.jpg
earth_normal.jpg earth_normal.jpg
earth_emission.jpg earth_emission.jpg
earth_specular.jpg
cloudmap.jpg cloudmap.jpg
moon.jpg moon.jpg
mars.jpg mars.jpg

Binary file not shown.

After

Width:  |  Height:  |  Size: 90 KiB

View file

@ -293,6 +293,7 @@ class SphericalBody(Body):
self.texture = get_best_texture(info['texture']) self.texture = get_best_texture(info['texture'])
self.normal_texture = None self.normal_texture = None
self.specular_texture = None
self.emission_texture = None self.emission_texture = None
self.sphere = self._get_sphere(division, tangent=self.type == 'planet') self.sphere = self._get_sphere(division, tangent=self.type == 'planet')
@ -303,6 +304,9 @@ class SphericalBody(Body):
if 'normal_map' in info: if 'normal_map' in info:
self.normal_texture = get_best_texture(info['normal_map']) 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: if 'emission_map' in info:
self.emission_texture = get_best_texture(info['emission_map']) self.emission_texture = get_best_texture(info['emission_map'])
@ -349,7 +353,16 @@ class SphericalBody(Body):
glBindTexture(GL_TEXTURE_2D, self.normal_texture) glBindTexture(GL_TEXTURE_2D, self.normal_texture)
shader.uniform_texture('u_planet.normalMap', 1) 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) shader.uniform_bool('u_planet.hasEmission', self.emission_texture)
if 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.emission', 0, 0, 0)
shader.uniform_vec3('u_planet.diffuse', 1, 1, 1) 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) glBindBuffer(GL_ARRAY_BUFFER, self.sphere.vbo)
shader.vertex_attribute('a_normal', self.sphere.direction_size, self.sphere.type, GL_FALSE, shader.vertex_attribute('a_normal', self.sphere.direction_size, self.sphere.type, GL_FALSE,

View file

@ -39,18 +39,15 @@ void main() {
vec3 emission = u_planet.hasEmission ? texture2D(u_planet.emissionMap, v_uv).rgb : vec3(1); vec3 emission = u_planet.hasEmission ? texture2D(u_planet.emissionMap, v_uv).rgb : vec3(1);
vec3 incident = normalize(u_sun.position - v_position); 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 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; vec3 ambient = u_planet.ambient * u_sun.ambient * diffuse;
diffuse *= u_planet.diffuse * u_sun.diffuse * diffuseIntensity; diffuse *= u_planet.diffuse * u_sun.diffuse * diffuseIntensity;
emission *= u_planet.emission * (1 - min(diffuseIntensity * 2, 1)); emission *= u_planet.emission * (1 - min(diffuseIntensity * 2, 1));
if (u_planet.shininess > 0) specular *= u_planet.specular * u_sun.specular * max(shininess, 0) * diffuseIntensity;
specular *= u_planet.specular * u_sun.specular * clamp(shininess, 0, 1);
else
specular = vec3(0);
gl_FragColor = vec4((ambient + diffuse + emission + specular) * u_sun.intensity, 1); gl_FragColor = vec4((ambient + diffuse + emission + specular) * u_sun.intensity, 1);
} }

View file

@ -57,6 +57,7 @@
"rotation": 86400, "rotation": 86400,
"division": 90, "division": 90,
"normal_map": ["earth_normal.jpg", "earth_normal_small.jpg"], "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"], "emission_map": ["earth_emission.jpg", "earth_emission_medium.jpg", "earth_emission_small.jpg"],
"atmosphere": { "atmosphere": {
"cloud_texture": ["cloudmap.jpg", "cloudmap_small.jpg"], "cloud_texture": ["cloudmap.jpg", "cloudmap_small.jpg"],