Error message here!

Hide Error message here!

Error message here!

Hide Error message here!

Error message here!

Close

# 【Ray Tracing The Next Week 超详解】 光线追踪2-5

Chapter 5：Image Texture Mapping

u = i / (nx - 1)

v = j / (ny - 1)

∠θ 为xOz平面与线段OP之间的夹角，θ∈0~π

∠φ 为线段OP在xOz平面的映射线与x轴正方向之间的夹角，φ∈0~2π

x = cosφ· cosθ

z = sinφ· cosθ
y = sinθ

u = φ/（2π）

v = θ/π

过程：

/// image_tex.hpp
// -----------------------------------------------------
// [author] lv
// [begin ] 2019.1
// [brief ] the image_texture-class for the ray-tracing project
// from the 《ray tracing the next week》
// -----------------------------------------------------
#pragma once
namespace rt
{
class image_texture: public texture
{
public:
image_texture() { }
image_texture(unsigned char* image, size_t a, size_t b);
inline unsigned char* image()const { return _image; }
inline size_t sizeX()const { return _sizeX; }
inline size_t sizeY()const { return _sizeY; }
public:
virtual rtvec value(rtvar u, rtvar v, const rtvec& p)const override;
private:
unsigned char* _image;
size_t _sizeX;
size_t _sizeY;
};
image_texture::image_texture(unsigned char* image, size_t a, size_t b)
:_image(image)
,_sizeX(a)
,_sizeY(b)
{
}
rtvec image_texture::value(rtvar u, rtvar v, const rtvec& p)const
{
int i = u*_sizeX;
int j = (1 - v)*_sizeY - 0.001;
if (i < 0)i = 0;
if (j < 0)j = 0;
if (i > _sizeX - 1)i = _sizeX - 1;
if (j > _sizeY - 1)j = _sizeY - 1;
rtvar r = int(_image[3 * i + 3 * _sizeX*j]) / 255.0;
rtvar g = int(_image[3 * i + 3 * _sizeX*j + 1]) / 255.0;
rtvar b = int(_image[3 * i + 3 * _sizeX*j + 2]) / 255.0;
return rtvec(r, g, b);
}
}
image_texture.hpp

#define STB_IMAGE_IMPLEMENTATION

然后就可以得到第一张图

https://www.cnblogs.com/lv-anchoret/p/10295137.html