]> Dogcows Code - chaz/rasterize/blobdiff - scene.c
add texture mapping with perspective correction
[chaz/rasterize] / scene.c
diff --git a/scene.c b/scene.c
index d079e17ac7c4788617beb42d6c1ff4665ddb428a..c7c4a3274b2e17db0cd89a7313d566dd30c3b818 100644 (file)
--- a/scene.c
+++ b/scene.c
@@ -18,6 +18,7 @@ static int _model_add_translate(model_t* m, FILE* file);
 static int _model_add_rotate(model_t* m, FILE* file);
 static int _model_add_scale(model_t* m, FILE* file);
 static int _model_set_material(model_t* m, FILE* file);
+static int _model_set_texture(model_t* m, FILE* file);
 static int _scene_set_ambient(scene_t* s, FILE* file);
 static int _scene_add_light(scene_t* s, FILE* file);
 
@@ -124,6 +125,13 @@ if (m == NULL) { \
                 }
                 break;
 
+            case 'm':
+                _ASSERT_G;
+                if (_model_set_texture(m, file) != 0) {
+                    goto fail;
+                }
+                break;
+
             case 'X':
                 goto done;
 
@@ -161,11 +169,12 @@ static int _model_set_colors(model_t* m, FILE* file)
         return -1;
     }
 
+    color_t color = color_new((colorchan_t)r, (colorchan_t)g, (colorchan_t)b, (colorchan_t)a);
     for (const list_t* i = model_geometry(m); i; i = i->link) {
         tri_t* t = (tri_t*)i->val;
-        t->a.c = color_new((colorchan_t)r, (colorchan_t)g, (colorchan_t)b, (colorchan_t)a);
-        t->b.c = color_new((colorchan_t)r, (colorchan_t)g, (colorchan_t)b, (colorchan_t)a);
-        t->c.c = color_new((colorchan_t)r, (colorchan_t)g, (colorchan_t)b, (colorchan_t)a);
+        t->a.c = color;
+        t->b.c = color;
+        t->c.c = color;
     }
     return 0;
 }
@@ -229,6 +238,22 @@ static int _model_set_material(model_t* m, FILE* file)
     return 0;
 }
 
+/*
+ * Set the texture to be used while drawing the current model.
+ */
+static int _model_set_texture(model_t* m, FILE* file)
+{
+    char filename[4096];
+    if (fgets(filename, 4096, file) == NULL) {
+        fprintf(stderr, "Cannot read texture filename from scene.\n");
+        return -1;
+    }
+#if TEXTURING
+    model_texture(m, (const void*)raster_import(trim(filename)));
+#endif
+    return 0;
+}
+
 /*
  * Add a light to the scene.
  */
This page took 0.019589 seconds and 4 git commands to generate.