Browse Source

Trying to implement image mapping.

Something not working yet.
Godzil 4 years ago
parent
commit
6a2c5a77ae
5 changed files with 132 additions and 0 deletions
  1. 7 0
      source/canvas.cpp
  2. 2 0
      source/include/canvas.h
  3. 43 0
      source/uvpattern/uv_image.h
  4. 4 0
      tests/CMakeLists.txt
  5. 76 0
      tests/uvmap_earth.cpp

+ 7 - 0
source/canvas.cpp

@@ -68,3 +68,10 @@ bool Canvas::SaveAsPNG(const char *filename)
 
     return ret == 0;
 }
+
+Canvas::Canvas(const char *pngfile)
+{
+    uint32_t ret = lodepng_decode24_file(&this->bitmap, &this->width, &this->height, pngfile);
+    if(ret){ printf("error %u: %s\n", ret, lodepng_error_text(ret));}
+    printf("%p - %d - %d\n", this->bitmap, this->width, this->height);
+}

+ 2 - 0
source/include/canvas.h

@@ -23,6 +23,8 @@ public:
     Canvas(uint32_t width, uint32_t height);
     Canvas(const Canvas *c);
     Canvas(const Canvas &c);
+    Canvas(const char *pngfile);
+
     ~Canvas();
 
     void putPixel(uint32_t x, uint32_t y, Tuple c);

+ 43 - 0
source/uvpattern/uv_image.h

@@ -0,0 +1,43 @@
+/*
+ *  DoRayMe - a quick and dirty Raytracer
+ *  UV Image pattern header
+ *
+ *  Created by Manoël Trapier
+ *  Copyright (c) 2020 986-Studio.
+ *
+ */
+
+#ifndef DORAYME_UV_IMAGE_H
+#define DORAYME_UV_IMAGE_H
+
+#include <stdint.h>
+
+#include <uv_pattern.h>
+#include <canvas.h>
+#include <tuple.h>
+
+class UVImage : public UVPattern
+{
+public:
+    Canvas *image;
+
+    UVImage(Canvas *image) : UVPattern(1, 1, Colour(0, 0, 0), Colour(0, 0, 0)),
+            image(image) {};
+
+    UVImage(const char *filepath) : UVPattern(1, 1, Colour(0, 0, 0), Colour(0, 0, 0)) {
+        this->image = new Canvas(filepath);
+        this->width = this->image->width;
+        this->height = this->image->height;
+    };
+
+    Colour uvPatternAt(double u, double v) {
+        v = 1 - v;
+        double x = u * (this->image->width/2.0 - 1);
+        double y = v * (this->image->height/2.0 - 1);
+
+        Colour ret = this->image->getPixel(round(x), round(y));
+        return ret;
+    };
+};
+
+#endif /* DORAYME_UV_IMAGE_H */

+ 4 - 0
tests/CMakeLists.txt

@@ -89,6 +89,9 @@ target_sources(uvmap_checkeredcube PRIVATE uvmap_checkeredcube.cpp)
 add_executable(uvmap_aligncheckplane)
 target_sources(uvmap_aligncheckplane PRIVATE uvmap_aligncheckplane.cpp)
 
+add_executable(uvmap_earth)
+target_sources(uvmap_earth PRIVATE uvmap_earth.cpp)
+
 add_test(NAME Chapter05_Test COMMAND $<TARGET_FILE:ch5_test>)
 add_test(NAME Chapter06_Test COMMAND $<TARGET_FILE:ch6_test>)
 add_test(NAME Chapter07_Test COMMAND $<TARGET_FILE:ch7_test>)
@@ -107,6 +110,7 @@ add_test(NAME UVMap_CheckeredPlane COMMAND $<TARGET_FILE:uvmap_checkeredplane>)
 add_test(NAME UVMap_CheckeredCylinder COMMAND $<TARGET_FILE:uvmap_checkeredcylinder>)
 add_test(NAME UVMap_AlignCheckPlane COMMAND $<TARGET_FILE:uvmap_aligncheckplane>)
 add_test(NAME UVMap_CheckeredCube COMMAND $<TARGET_FILE:uvmap_checkeredcube>)
+add_test(NAME UVMap_Earth COMMAND $<TARGET_FILE:uvmap_earth>)
 add_test(NAME Test_Rendering COMMAND $<TARGET_FILE:test_render>)
 add_test(NAME Triangle_RenderTest COMMAND $<TARGET_FILE:triangle_rendertest>)
 add_test(NAME ChristmasBall_Rendering COMMAND $<TARGET_FILE:christmasball_render>)

+ 76 - 0
tests/uvmap_earth.cpp

@@ -0,0 +1,76 @@
+/*
+ *  DoRayMe - a quick and dirty Raytracer
+ *  Render test for chapter 10
+ *
+ *  Created by Manoël Trapier
+ *  Copyright (c) 2020 986-Studio.
+ *
+ */
+#include <world.h>
+#include <light.h>
+#include <sphere.h>
+#include <plane.h>
+#include <cylinder.h>
+
+#include <material.h>
+#include <colour.h>
+#include <canvas.h>
+#include <camera.h>
+
+#include <pattern.h>
+#include <texturemap.h>
+#include <uv_image.h>
+
+#include <transformation.h>
+
+int main()
+{
+    World w = World();
+
+    Light light = Light(POINT_LIGHT, Point(-100, 100, -100), Colour(1, 1, 1));
+    w.addLight(&light);
+
+    Plane p = Plane();
+    p.material.colour = Colour(1, 1, 1);
+    p.material.diffuse = 0.1;
+    p.material.specular = 0;
+    p.material.ambient = 0;
+    p.material.reflective = 0.4;
+    w.addObject(&p);
+
+    Cylinder cyl = Cylinder();
+    cyl.minCap = 0;
+    cyl.maxCap = 0.1;
+    cyl.isClosed = true;
+    cyl.material.colour = Colour(1, 1, 1);
+    cyl.material.diffuse = 0.2;
+    cyl.material.specular = 0;
+    cyl.material.ambient = 0;
+    cyl.material.reflective = 0.1;
+    w.addObject(&cyl);
+
+    Sphere sp = Sphere();
+
+    UVImage sphereTexture = UVImage("earthmap1k.png");
+    TextureMap tm = TextureMap(SPHERICAL_MAP, &sphereTexture);
+    sp.setTransform(translation(0, 1.1, 0) * rotationY(1.9));
+    sp.material.pattern = &tm;
+    sp.material.ambient = 0.1;
+    sp.material.specular = 0.1;
+    sp.material.shininess = 10;
+    sp.material.diffuse = 0.9;
+    w.addObject(&sp);
+
+    /* Set the camera */
+    Camera camera = Camera(800, 400, 0.8);
+    camera.setTransform(viewTransform(Point(1, 2, -10),
+                                      Point(0, 1.1, 0),
+                                      Vector(0, 1, 0)));
+
+    /* Now render it */
+    Canvas image = camera.render(w);
+
+    image.SaveAsPNG("uvmap_earth.png");
+
+    return 0;
+}