From 6a2c5a77ae06f31a7eac3afed89d8b3e3b5b88bd Mon Sep 17 00:00:00 2001 From: Godzil Date: Thu, 5 Mar 2020 09:48:37 +0000 Subject: [PATCH] Trying to implement image mapping. Something not working yet. --- source/canvas.cpp | 7 ++++ source/include/canvas.h | 2 + source/uvpattern/uv_image.h | 43 +++++++++++++++++++++ tests/CMakeLists.txt | 4 ++ tests/uvmap_earth.cpp | 76 +++++++++++++++++++++++++++++++++++++ 5 files changed, 132 insertions(+) create mode 100644 source/uvpattern/uv_image.h create mode 100644 tests/uvmap_earth.cpp diff --git a/source/canvas.cpp b/source/canvas.cpp index 13bda04..0816cc4 100644 --- a/source/canvas.cpp +++ b/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); +} \ No newline at end of file diff --git a/source/include/canvas.h b/source/include/canvas.h index aef7300..1817f2e 100644 --- a/source/include/canvas.h +++ b/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); diff --git a/source/uvpattern/uv_image.h b/source/uvpattern/uv_image.h new file mode 100644 index 0000000..3b3da6e --- /dev/null +++ b/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 + +#include +#include +#include + +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 */ diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 57775cd..eed05c4 100644 --- a/tests/CMakeLists.txt +++ b/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 $) add_test(NAME Chapter06_Test COMMAND $) add_test(NAME Chapter07_Test COMMAND $) @@ -107,6 +110,7 @@ add_test(NAME UVMap_CheckeredPlane COMMAND $) add_test(NAME UVMap_CheckeredCylinder COMMAND $) add_test(NAME UVMap_AlignCheckPlane COMMAND $) add_test(NAME UVMap_CheckeredCube COMMAND $) +add_test(NAME UVMap_Earth COMMAND $) add_test(NAME Test_Rendering COMMAND $) add_test(NAME Triangle_RenderTest COMMAND $) add_test(NAME ChristmasBall_Rendering COMMAND $) diff --git a/tests/uvmap_earth.cpp b/tests/uvmap_earth.cpp new file mode 100644 index 0000000..f605224 --- /dev/null +++ b/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 +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include + +#include + +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; +} \ No newline at end of file