Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Very preliminary texturing

  • Loading branch information...
commit 197b60a4de41467502b6f1da570ee1b254ea2075 1 parent a06296e
Mikko Perttunen cyndis authored
7 backend/Makefile
@@ -23,7 +23,7 @@ src/task.cpp src/taskmanager.cpp src/ray.cpp src/renderer.cpp src/camera.cpp \
23 23 src/backendapplication.cpp src/octreescene.cpp src/aabb.cpp src/box.cpp \
24 24 src/sphere.cpp src/octree.cpp src/raycastresult.cpp \
25 25 src/threadparallelrenderer.cpp src/kdtreescene.cpp src/random.cpp \
26   -src/settings.cpp src/util.cpp
  26 +src/settings.cpp src/util.cpp src/texture.cpp
27 27 CC_SOURCES =
28 28
29 29 # Source files that belong explicitly to normal operation (disregard from testing)
@@ -145,7 +145,7 @@ obj/src/taskmanager.o: src/taskmanager.h src/itasklistener.h src/task.h
145 145 obj/src/ray.o: src/ray.h src/vector.h
146 146 obj/src/renderer.o: src/util.h src/renderer.h src/random.h src/vector.h
147 147 obj/src/renderer.o: src/scene.h src/camera.h src/ray.h src/raycastresult.h
148   -obj/src/renderer.o: src/sceneobject.h src/unique_ptr
  148 +obj/src/renderer.o: src/sceneobject.h src/unique_ptr src/texture.h
149 149 obj/src/camera.o: src/camera.h src/vector.h src/ray.h
150 150 obj/src/backendapplication.o: src/kdtreescene.h src/vector.h
151 151 obj/src/backendapplication.o: src/sceneobject.h src/raycastresult.h
@@ -178,6 +178,9 @@ obj/src/kdtreescene.o: src/unique_ptr src/aabb.h
178 178 obj/src/random.o: src/random.h src/vector.h
179 179 obj/src/settings.o: src/util.h src/settings.h
180 180 obj/src/util.o: src/util.h
  181 +obj/src/texture.o: src/texture.h src/renderer.h src/random.h src/vector.h
  182 +obj/src/texture.o: src/scene.h src/camera.h src/ray.h src/raycastresult.h
  183 +obj/src/texture.o: src/sceneobject.h src/unique_ptr
181 184 obj/src/main.o: src/backendapplication.h src/itasklistener.h src/socket.h
182 185 obj/src/main.o: src/dmcr_protocol.pb.h src/itaskprovider.h src/renderer.h
183 186 obj/src/main.o: src/random.h src/vector.h src/scene.h src/camera.h src/ray.h
3  backend/src/aabb.cpp
@@ -77,6 +77,9 @@ dmcr::IntersectionResult dmcr::AABB::intersects(const dmcr::Ray &ray) const
77 77 normal = dmcr::Vector3f(0, 0, 1);
78 78
79 79 result.normal = normal;
  80 +
  81 + result.uv = dmcr::Vector3f(c1.x() / (m_max-m_min).x(),
  82 + c1.y() / (m_max-m_min).y(), 0.0);
80 83
81 84 return result;
82 85 }
4 backend/src/raycastresult.h
@@ -53,11 +53,15 @@ class RaycastResult
53 53
54 54 void setNormal(const dmcr::Vector3f& normal) { m_normal = normal; }
55 55 dmcr::Vector3f normal() const { return m_normal; }
  56 +
  57 + dmcr::Vector3f uv() const { return m_uv; }
  58 + void setUV(const dmcr::Vector3f& uv) { m_uv = uv; }
56 59
57 60 private:
58 61 dmcr::SceneObjectPtr m_object;
59 62 dmcr::Vector3f m_intersection_point;
60 63 dmcr::Vector3f m_normal;
  64 + dmcr::Vector3f m_uv;
61 65 };
62 66
63 67 } // namespace dmcr
4 backend/src/renderer.cpp
@@ -119,6 +119,8 @@ dmcr::Color dmcr::Renderer::midfunc(dmcr::Ray ray) const
119 119 return iterator(ray);
120 120 }
121 121
  122 +#include "texture.h"
  123 +
122 124 // getting pixel color
123 125 dmcr::Color dmcr::Renderer::iterator(dmcr::Ray ray, int iterations) const
124 126 {
@@ -166,7 +168,7 @@ dmcr::Color dmcr::Renderer::iterator(dmcr::Ray ray, int iterations) const
166 168 }
167 169
168 170 c += opacity * iterator(new_ray, iterations + 1) *
169   - rr.object()->color();
  171 + rr.object()->color();
170 172 }
171 173 }
172 174
4 backend/src/scene.cpp
@@ -176,13 +176,14 @@ dmcr::RaycastResult dmcr::Scene::shootRay(const dmcr::Ray& ray) const
176 176 std::list<SceneObjectPtr> objects = intersectionCandidates(ray);
177 177 double min = 1.0 / 0.0;
178 178 SceneObjectPtr min_obj = nullptr;
179   - dmcr::Vector3f min_normal;
  179 + dmcr::Vector3f min_normal, min_uv;
180 180
181 181 for (auto obj : objects) {
182 182 dmcr::IntersectionResult ir = obj->intersects(ray);
183 183 if (ir.intersects && ir.t > 0.0 && ir.t < min) {
184 184 min_obj = obj;
185 185 min_normal = ir.normal;
  186 + min_uv = ir.uv;
186 187 min = ir.t;
187 188 }
188 189 }
@@ -192,6 +193,7 @@ dmcr::RaycastResult dmcr::Scene::shootRay(const dmcr::Ray& ray) const
192 193 result.setObject(min_obj);
193 194 result.setIntersectionPoint(ray.origin() + ray.direction() * min);
194 195 result.setNormal(min_normal);
  196 + result.setUV(min_uv);
195 197 }
196 198
197 199 return result;
1  backend/src/sceneobject.h
@@ -20,6 +20,7 @@ struct IntersectionResult
20 20 bool intersects;
21 21 double t;
22 22 dmcr::Vector3f normal;
  23 + dmcr::Vector3f uv;
23 24 };
24 25
25 26 class AABB;
3  backend/src/socket.cpp
@@ -15,9 +15,10 @@
15 15 #include <sstream>
16 16 #include <vector>
17 17
18   -#include "dmcr_protocol.pb.h"
19 18 #include "png++/image.hpp"
20 19 #include "png++/rgb_pixel.hpp"
  20 +
  21 +#include "dmcr_protocol.pb.h"
21 22 #include "settings.h"
22 23 #include "socket.h"
23 24 #include "vector.h"
18 backend/src/texture.cpp
... ... @@ -0,0 +1,18 @@
  1 +#include "texture.h"
  2 +#include <cmath>
  3 +#include <iostream>
  4 +
  5 +dmcr::ImageTexture::ImageTexture(std::string path)
  6 +: Texture2D()
  7 +, m_image(path)
  8 +{
  9 +}
  10 +
  11 +dmcr::Color dmcr::ImageTexture::queryTexel(double u, double v)
  12 +{
  13 + auto w = m_image.get_width()-1;
  14 + auto h = m_image.get_height()-1;
  15 + auto px = m_image.get_pixel(round(u * w), round(v * h));
  16 +
  17 + return dmcr::Color(px.red, px.green, px.blue);
  18 +}
28 backend/src/texture.h
... ... @@ -0,0 +1,28 @@
  1 +#ifndef DMCR_TEXTURE_H
  2 +#define DMCR_TEXTURE_H
  3 +
  4 +#include "png++/image.hpp"
  5 +#include "png++/rgb_pixel.hpp"
  6 +
  7 +#include "renderer.h"
  8 +
  9 +#include <string>
  10 +
  11 +namespace dmcr {
  12 + class Texture2D {
  13 + public:
  14 + virtual Color queryTexel(double u, double v) = 0;
  15 + };
  16 +
  17 + class ImageTexture : public Texture2D {
  18 + public:
  19 + ImageTexture(std::string path);
  20 +
  21 + virtual Color queryTexel(double u, double v);
  22 +
  23 + private:
  24 + png::image<png::rgb_pixel> m_image;
  25 + };
  26 +}
  27 +
  28 +#endif

0 comments on commit 197b60a

Please sign in to comment.
Something went wrong with that request. Please try again.