gamnit: remove `Gamnit` from `GamnitRootTexture` and `GamnitSubtexture`
[nit.git] / lib / gamnit / depth / more_materials.nit
index 27bdd9a..6374dea 100644 (file)
@@ -18,24 +18,26 @@ module more_materials
 intrude import depth_core
 intrude import flat
 
-# Simple material with static colors used for debugging or display abstract objects
-class SmoothMaterial
-       super Material
-
+redef class Material
        # Get the default blueish material
-       init default do init(
+       new do return new SmoothMaterial(
                [0.0, 0.0, 0.3, 1.0],
                [0.0, 0.0, 0.6, 1.0],
                [1.0, 1.0, 1.0, 1.0])
+end
+
+# Simple material with static colors used for debugging or display abstract objects
+class SmoothMaterial
+       super Material
 
        # Ambient color, always visible
-       var ambient_color: Array[Float]
+       var ambient_color: Array[Float] is writable
 
        # Diffuse color when covered by a light source
-       var diffuse_color: Array[Float]
+       var diffuse_color: Array[Float] is writable
 
        # Specular color affecting reflections
-       var specular_color: Array[Float]
+       var specular_color: Array[Float] is writable
 
        redef fun draw(actor, model)
        do
@@ -75,9 +77,9 @@ class SmoothMaterial
 
                # Execute draw
                if mesh.indices.is_empty then
-                       glDrawArrays(gl_TRIANGLES, 0, mesh.vertices.length/3)
+                       glDrawArrays(mesh.draw_mode, 0, mesh.vertices.length/3)
                else
-                       glDrawElements(gl_TRIANGLES, mesh.indices.length, gl_UNSIGNED_SHORT, mesh.indices_c.native_array)
+                       glDrawElements(mesh.draw_mode, mesh.indices.length, gl_UNSIGNED_SHORT, mesh.indices_c.native_array)
                end
        end
 end
@@ -144,7 +146,7 @@ class TexturedMaterial
                # If using a texture, set `texture_coords`
                program.tex_coord.array_enabled = sample_used_texture != null
                if sample_used_texture != null then
-                       if sample_used_texture isa GamnitRootTexture then
+                       if sample_used_texture isa RootTexture then
                                # Coordinates are directly valid
                                program.tex_coord.array(mesh.texture_coords, 2)
                        else
@@ -154,11 +156,13 @@ class TexturedMaterial
                                var xd = sample_used_texture.offset_right - xa
                                var ya = sample_used_texture.offset_top
                                var yd = sample_used_texture.offset_bottom - ya
+                               xd *= 0.999
+                               yd *= 0.999
 
                                var tex_coords = new Array[Float].with_capacity(mesh.texture_coords.length)
                                for i in [0..mesh.texture_coords.length/2[ do
                                        tex_coords[i*2]   = xa + xd * mesh.texture_coords[i*2]
-                                       tex_coords[i*2+1] = ya + yd * mesh.texture_coords[i*2+1]
+                                       tex_coords[i*2+1] = 1.0 - (ya + yd * mesh.texture_coords[i*2+1])
                                end
 
                                program.tex_coord.array(tex_coords, 2)
@@ -180,9 +184,9 @@ class TexturedMaterial
                program.camera.uniform(app.world_camera.position.x, app.world_camera.position.y, app.world_camera.position.z)
 
                if mesh.indices.is_empty then
-                       glDrawArrays(gl_TRIANGLES, 0, mesh.vertices.length/3)
+                       glDrawArrays(mesh.draw_mode, 0, mesh.vertices.length/3)
                else
-                       glDrawElements(gl_TRIANGLES, mesh.indices.length, gl_UNSIGNED_SHORT, mesh.indices_c.native_array)
+                       glDrawElements(mesh.draw_mode, mesh.indices.length, gl_UNSIGNED_SHORT, mesh.indices_c.native_array)
                end
        end
 end
@@ -218,9 +222,9 @@ class NormalsMaterial
                program.normal.array(mesh.normals, 3)
 
                if mesh.indices.is_empty then
-                       glDrawArrays(gl_TRIANGLES, 0, mesh.vertices.length/3)
+                       glDrawArrays(mesh.draw_mode, 0, mesh.vertices.length/3)
                else
-                       glDrawElements(gl_TRIANGLES, mesh.indices.length, gl_UNSIGNED_SHORT, mesh.indices_c.native_array)
+                       glDrawElements(mesh.draw_mode, mesh.indices.length, gl_UNSIGNED_SHORT, mesh.indices_c.native_array)
                end
        end
 end
@@ -317,7 +321,7 @@ class LambertProgram
                        float lambert = max(dot(light_dir, v_normal), 0.0);
 
                        if (use_map_ambient)
-                               gl_FragColor = ambient_color + texture2D(map_ambient, v_tex_coord);
+                               gl_FragColor = ambient_color * texture2D(map_ambient, v_tex_coord);
                        else
                                gl_FragColor = ambient_color;
 
@@ -325,6 +329,8 @@ class LambertProgram
                                gl_FragColor += lambert * diffuse_color * texture2D(map_diffuse, v_tex_coord);
                        else
                                gl_FragColor += lambert * diffuse_color;
+
+                       if (gl_FragColor.a < 0.01) discard;
                }
                """ @ glsl_fragment_shader