Skip to content

Commit c5d3d4b

Browse files
committed
Fold image_texture class into texture.h header
Resolves #295
1 parent 6c326b5 commit c5d3d4b

File tree

3 files changed

+29
-45
lines changed

3 files changed

+29
-45
lines changed

src/TheNextWeek/main.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include "common/rtweekend.h"
1313
#include "common/camera.h"
1414
#include "common/rtw_stb_image.h"
15-
#include "common/surface_texture.h"
1615
#include "common/texture.h"
1716
#include "box.h"
1817
#include "bvh.h"
@@ -44,7 +43,7 @@ hittable *earth() {
4443
//unsigned char *tex_data = stbi_load("tiled.jpg", &nx, &ny, &nn, 0);
4544
unsigned char *tex_data = stbi_load("earthmap.jpg", &nx, &ny, &nn, 0);
4645
material *mat = new lambertian(new image_texture(tex_data, nx, ny));
47-
return new sphere(vec3(0,0, 0), 2, mat);
46+
return new sphere(vec3(0, 0, 0), 2, mat);
4847
}
4948

5049
hittable *two_spheres() {

src/common/surface_texture.h

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/common/texture.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class texture {
2020
virtual vec3 value(double u, double v, const vec3& p) const = 0;
2121
};
2222

23+
2324
class constant_texture : public texture {
2425
public:
2526
constant_texture() {}
@@ -32,6 +33,7 @@ class constant_texture : public texture {
3233
vec3 color;
3334
};
3435

36+
3537
class checker_texture : public texture {
3638
public:
3739
checker_texture() {}
@@ -65,4 +67,30 @@ class noise_texture : public texture {
6567
double scale;
6668
};
6769

70+
71+
class image_texture : public texture {
72+
public:
73+
image_texture() {}
74+
image_texture(unsigned char *pixels, int A, int B) : data(pixels), nx(A), ny(B) {}
75+
76+
virtual vec3 value(double u, double v, const vec3& p) const {
77+
auto i = static_cast<int>(( u)*nx);
78+
auto j = static_cast<int>((1-v)*ny-0.001);
79+
80+
if (i < 0) i = 0;
81+
if (j < 0) j = 0;
82+
if (i > nx-1) i = nx-1;
83+
if (j > ny-1) j = ny-1;
84+
85+
auto r = static_cast<int>(data[3*i + 3*nx*j+0]) / 255.0;
86+
auto g = static_cast<int>(data[3*i + 3*nx*j+1]) / 255.0;
87+
auto b = static_cast<int>(data[3*i + 3*nx*j+2]) / 255.0;
88+
return vec3(r, g, b);
89+
}
90+
91+
unsigned char *data;
92+
int nx, ny;
93+
};
94+
95+
6896
#endif

0 commit comments

Comments
 (0)