]> Dogcows Code - chaz/rasterize/blobdiff - scene.c
make animate script work for modern luas
[chaz/rasterize] / scene.c
diff --git a/scene.c b/scene.c
index 85f15e6c02f093b61c7e7ddd1ac384faa80e0385..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);
 
@@ -39,10 +40,12 @@ scene_t* scene_alloc(FILE* file)
     int w, h;
     double eyeX, eyeY, eyeZ, spotX, spotY, spotZ, upX, upY, upZ;
     double fovy, aspect, near, far;
-    if (fscanf(file, "U3 %d %d %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",
+    double aR, aG, aB;
+    if (fscanf(file, "U4 %d %d %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",
                &w, &h,
                &eyeX, &eyeY, &eyeZ, &spotX, &spotY, &spotZ, &upX, &upY, &upZ,
-               &fovy, &aspect, &near, &far) != 15) {
+               &fovy, &aspect, &near, &far,
+               &aR, &aG, &aB) != 18) {
         fprintf(stderr, "Cannot read scene header.\n");
         return NULL;
     }
@@ -57,7 +60,7 @@ scene_t* scene_alloc(FILE* file)
     s->eye = vec_new(eyeX, eyeY, eyeZ);
     s->projection = MAT_PERSPECTIVE((scal_t)fovy, (scal_t)aspect, (scal_t)near, (scal_t)far);
     s->lights = NULL;
-    s->ambient = color_new(S(0.05), S(0.05), S(0.05), S(1.0));
+    s->ambient = color_new((scal_t)aR, (scal_t)aG, (scal_t)aB, S(1.0));
 
     char filename[4096];
     model_t* m = NULL;
@@ -109,21 +112,22 @@ if (m == NULL) { \
                 }
                 break;
 
-            case 'A':
-                if (_scene_set_ambient(s, file) != 0) {
+            case 'l':
+                if (_scene_add_light(s, file) != 0) {
                     goto fail;
                 }
                 break;
 
-            case 'L':
-                if (_scene_add_light(s, file) != 0) {
+            case 'p':
+                _ASSERT_G;
+                if (_model_set_material(m, file) != 0) {
                     goto fail;
                 }
                 break;
 
-            case 'M':
+            case 'm':
                 _ASSERT_G;
-                if (_model_set_material(m, file) != 0) {
+                if (_model_set_texture(m, file) != 0) {
                     goto fail;
                 }
                 break;
@@ -138,6 +142,7 @@ if (m == NULL) { \
 #undef _ASSERT_G
 
 done:
+    list_reverse(&s->models);
     return s;
 
 fail:
@@ -158,18 +163,18 @@ void scene_destroy(scene_t* s)
  */
 static int _model_set_colors(model_t* m, FILE* file)
 {
-    double r1, g1, b1, r2, g2, b2, r3, g3, b3;
-    if (fscanf(file, " %lf %lf %lf %lf %lf %lf %lf %lf %lf",
-               &r1, &g1, &b1, &r2, &g2, &b2, &r3, &g3, &b3) != 9) {
+    double a, r, g, b;
+    if (fscanf(file, " %lf %lf %lf %lf", &a, &r, &g, &b) != 4) {
         fprintf(stderr, "Cannot read color values from scene.\n");
         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)r1, (colorchan_t)g1, (colorchan_t)b1, S(1.0));
-        t->b.c = color_new((colorchan_t)r2, (colorchan_t)g2, (colorchan_t)b2, S(1.0));
-        t->c.c = color_new((colorchan_t)r3, (colorchan_t)g3, (colorchan_t)b3, S(1.0));
+        t->a.c = color;
+        t->b.c = color;
+        t->c.c = color;
     }
     return 0;
 }
@@ -234,16 +239,18 @@ static int _model_set_material(model_t* m, FILE* file)
 }
 
 /*
- * Set the ambient light properties of a scene.
+ * Set the texture to be used while drawing the current model.
  */
-static int _scene_set_ambient(scene_t* s, FILE* file)
+static int _model_set_texture(model_t* m, FILE* file)
 {
-    double r, g, b;
-    if (fscanf(file, " %lf %lf %lf", &r, &g, &b) != 3) {
-        fprintf(stderr, "Cannot read ambient light from scene.\n");
+    char filename[4096];
+    if (fgets(filename, 4096, file) == NULL) {
+        fprintf(stderr, "Cannot read texture filename from scene.\n");
         return -1;
     }
-    s->ambient = color_new((scal_t)r, (scal_t)g, (scal_t)b, S(1.0));
+#if TEXTURING
+    model_texture(m, (const void*)raster_import(trim(filename)));
+#endif
     return 0;
 }
 
@@ -252,16 +259,16 @@ static int _scene_set_ambient(scene_t* s, FILE* file)
  */
 static int _scene_add_light(scene_t* s, FILE* file)
 {
-    double lx, ly, lz, dr, dg, db, sr, sg, sb;
-    if (fscanf(file, " %lf %lf %lf %lf %lf %lf %lf %lf %lf",
-               &lx, &ly, &lz, &dr, &dg, &db, &sr, &sg, &sb) != 9) {
+    double lx, ly, lz, r, g, b;
+    if (fscanf(file, " %lf %lf %lf %lf %lf %lf",
+               &lx, &ly, &lz, &r, &g, &b) != 6) {
         fprintf(stderr, "Cannot read light values from scene.\n");
         return -1;
     }
     light_t* l = light_alloc(
             vec_new(lx, ly, lz),
-            color_new(dr, dg, db, S(1.0)),
-            color_new(sr, sg, sb, S(1.0))
+            color_new((scal_t)r, (scal_t)g, (scal_t)b, S(1.0)),
+            color_new((scal_t)r, (scal_t)g, (scal_t)b, S(1.0))
     );
     list_push2(&s->lights, l, mem_free);
     return 0;
This page took 0.019857 seconds and 4 git commands to generate.