77
88#include " ../programs/vec.h"
99
10+ // Image I/O - disregard lib warnings
11+ #pragma warning(push, 0)
12+ #define STBI_MSC_SECURE_CRT
13+ #define STB_IMAGE_IMPLEMENTATION
14+ #include " ../lib/stb_image.h"
15+ #define STB_IMAGE_WRITE_IMPLEMENTATION
16+ #include " ../lib/stb_image_write.h"
17+ #pragma warning(pop)
18+
1019float rnd () {
1120 // static std::random_device rd; //Will be used to obtain a seed for the random number engine
1221 static std::mt19937 gen (0 ); // Standard mersenne_twister_engine seeded with rd()
@@ -22,6 +31,7 @@ float rnd() {
2231extern " C" const char embedded_constant_texture_programs[];
2332extern " C" const char embedded_checker_texture_programs[];
2433extern " C" const char embedded_noise_texture_programs[];
34+ extern " C" const char embedded_image_texture_programs[];
2535
2636struct Texture {
2737 virtual optix::Program assignTo (optix::GeometryInstance gi, optix::Context &g_context) const = 0;
@@ -123,4 +133,59 @@ struct Noise_Texture : public Texture{
123133 const float scale;
124134};
125135
136+ struct Image_Texture : public Texture {
137+ Image_Texture (const std::string f) : fileName(f) {}
138+
139+ optix::TextureSampler loadTexture (optix::Context context, const std::string fileName) const {
140+ int nx, ny, nn;
141+ unsigned char *tex_data = stbi_load ((char *)fileName.c_str (), &nx, &ny, &nn, 0 );
142+
143+ optix::TextureSampler sampler = context->createTextureSampler ();
144+ sampler->setWrapMode (0 , RT_WRAP_REPEAT);
145+ sampler->setWrapMode (1 , RT_WRAP_REPEAT);
146+ sampler->setWrapMode (2 , RT_WRAP_REPEAT);
147+ sampler->setIndexingMode (RT_TEXTURE_INDEX_NORMALIZED_COORDINATES);
148+ sampler->setReadMode (RT_TEXTURE_READ_NORMALIZED_FLOAT);
149+ sampler->setMaxAnisotropy (1 .0f );
150+ sampler->setMipLevelCount (1u );
151+ sampler->setArraySize (1u );
152+
153+ optix::Buffer buffer = context->createBuffer (RT_BUFFER_INPUT, RT_FORMAT_UNSIGNED_BYTE4, nx, ny);
154+ unsigned char * data = static_cast <unsigned char *>(buffer->map ());
155+
156+ for (int i = 0 ; i < nx; ++i) {
157+ for (int j = 0 ; j < ny; ++j) {
158+ int bindex = (j * nx + i) * 4 ;
159+ int iindex = ((ny - j - 1 ) * nx + i) * nn;
160+
161+ data[bindex + 0 ] = tex_data[iindex + 0 ];
162+ data[bindex + 1 ] = tex_data[iindex + 1 ];
163+ data[bindex + 2 ] = tex_data[iindex + 2 ];
164+
165+ if (nn == 4 )
166+ data[bindex + 3 ] = tex_data[iindex + 3 ];
167+ else // 3-channel images
168+ data[bindex + 3 ] = (unsigned char )255 ;
169+ }
170+ }
171+
172+ buffer->unmap ();
173+ sampler->setBuffer (buffer);
174+ sampler->setFilteringModes (RT_FILTER_LINEAR, RT_FILTER_LINEAR, RT_FILTER_NONE);
175+ return sampler;
176+ }
177+
178+ virtual optix::Program assignTo (optix::GeometryInstance gi, optix::Context &g_context) const override {
179+ optix::Program textProg = g_context->createProgramFromPTXString (embedded_image_texture_programs, " sample_texture" );
180+
181+ textProg[" data" ]->setTextureSampler (loadTexture (g_context, fileName));
182+
183+ gi[" sample_texture" ]->setProgramId (textProg);
184+
185+ return textProg;
186+ }
187+
188+ const std::string fileName;
189+ };
190+
126191#endif
0 commit comments