45 #define STB_IMAGE_WRITE_IMPLEMENTATION 50 #if defined(TACHYON_USEPINNEDMEMORY) 51 #include <cuda_runtime.h> 58 void HSItoRGB(
float h,
float s,
float i,
float &r,
float &g,
float &b) {
59 float t=2.0f *
M_PI * h;
60 float scale = i / 2.0f;
61 r=(1.0f + s*sin(t - 2.0f*
M_PI/3.0f)) * scale;
62 g=(1.0f + s*sin(t)) * scale;
63 b=(1.0f + s*sin(t + 2.0f*
M_PI/3.0f)) * scale;
67 float invmaxval = 1.0f / float(maxval);
68 for (
int c=0; c<3; c++) {
69 srgbf[c] = srgbi[c] * invmaxval;
74 for (
int c=0; c<3; c++) {
75 if (srgb[c] <= 0.0404482362771082f) {
76 lin[c] = srgb[c] / 12.92f;
78 lin[c] = powf(((srgb[c] + 0.055f)/1.055f), 2.4f);
84 for (
int c=0; c<3; c++) {
85 if (lin[c] > 0.0031308f) {
86 srgb[c] = 1.055f * (powf(lin[c], (1.0f / 2.4f))) - 0.055f;
88 srgb[c] = 12.92f * lin[c];
102 unsigned char *rgba4ub,
103 int imgxs,
int imgys,
unsigned char *img) {
104 if (bxs < 0) bxs = 0;
105 if (bxs >= imgxs) bxs = imgxs - 1;
106 if (bys < 0) bys = 0;
107 if (bys >= imgys) bys = imgys - 1;
109 if (bxe < 0) bxe = 0;
110 if (bxe >= imgxs) bxe = imgxs - 1;
111 if (bye < 0) bye = 0;
112 if (bye >= imgys) bye = imgys - 1;
114 for (
int y=bys; y<bye; y++) {
115 unsigned char *rowaddr = img + y*imgxs*4;
116 for (
int x4=bxs*4; x4<bxe*4; x4+=4) {
117 unsigned char *px = rowaddr + x4;
134 int imgxs,
int imgys,
float *img) {
135 if (bxs < 0) bxs = 0;
136 if (bxs >= imgxs) bxs = imgxs - 1;
137 if (bys < 0) bys = 0;
138 if (bys >= imgys) bys = imgys - 1;
140 if (bxe < 0) bxe = 0;
141 if (bxe >= imgxs) bxe = imgxs - 1;
142 if (bye < 0) bye = 0;
143 if (bye >= imgys) bye = imgys - 1;
145 for (
int y=bys; y<bye; y++) {
146 float *rowaddr = img + y*imgxs*4;
147 for (
int x4=bxs*4; x4<bxe*4; x4+=4) {
148 float *px = rowaddr + x4;
163 int & imgszx,
int & imgszy) {
164 int bsz = tilesz + 2*pad;
165 imgszx = 6 * bsz + 2*pad;
166 imgszy = 4 * bsz + 2*pad;
167 unsigned char *img =
new unsigned char[imgszx * imgszy * 4];
169 unsigned char background[4] = {0, 0, 0, 255};
171 imgszx, imgszy, img);
173 for (
int y=0; y<4; y++) {
174 for (
int x=0; x<6; x++) {
175 int idx3 = (y*6 + x) * 3;
179 unsigned char srgba4ub[4];
180 srgba4ub[0] = srgbi[idx3 ];
181 srgba4ub[1] = srgbi[idx3 + 1];
182 srgba4ub[2] = srgbi[idx3 + 2];
185 int bxs = pad + x*bsz + pad;
186 int bxe = pad + (x+1)*bsz - pad;
187 int bys = pad + y*bsz + pad;
188 int bye = pad + (y+1)*bsz - pad;
203 int maxval,
int convertsrgbtolinear,
204 int & imgszx,
int & imgszy) {
205 int bsz = tilesz + 2*pad;
206 imgszx = 6 * bsz + 2*pad;
207 imgszy = 4 * bsz + 2*pad;
208 float *img =
new float[imgszx * imgszy * 4];
210 float background[4] = {0.0f, 0.0f, 0.0f, 1.0f};
212 imgszx, imgszy, img);
214 for (
int y=0; y<4; y++) {
215 for (
int x=0; x<6; x++) {
216 int idx3 = (y*6 + x) * 3;
221 tmp[0] = srgbi[idx3 ] / float(maxval);
222 tmp[1] = srgbi[idx3 + 1] / float(maxval);
223 tmp[2] = srgbi[idx3 + 2] / float(maxval);
227 if (convertsrgbtolinear) {
230 memcpy(rgbaf32, tmp,
sizeof(tmp));
233 int bxs = pad + x*bsz + pad;
234 int bxe = pad + (x+1)*bsz - pad;
235 int bys = pad + y*bsz + pad;
236 int bye = pad + (y+1)*bsz - pad;
252 float *coords =
new float[numpts * 3];
254 float psz = tilesz + 2.0f*pad;
255 float szx = 6.0f * psz + 2.0f*pad;
256 float szy = 4.0f * psz + 2.0f*pad;
258 float szxh = szx * 0.5f;
259 float szyh = szy * 0.5f;
260 float htsz = tilesz * 0.5f;
263 for (
int y=0; y<4; y++) {
264 for (
int x=0; x<6; x++) {
265 float px = pad + x*psz + pad + htsz;
266 float py = pad + y*psz + pad + htsz;
268 coords[idx ] = px - szxh;
269 coords[idx + 1] = height;
270 coords[idx + 2] = -py + szyh;
288 float *colors =
new float[numpts * 4];
289 for (
int y=0; y<4; y++) {
290 for (
int x=0; x<6; x++) {
293 int idx3 = (y*6 + x) * 3;
294 int idx4 = (y*6 + x) * 4;
297 colors[idx4 + 3] = 1.0f;
311 float *verts =
new float[numpts * 4 * 3];
313 float psz = tilesz + 2.0f*pad;
314 float szx = 6.0f * psz + 2.0f*pad;
315 float szy = 4.0f * psz + 2.0f*pad;
317 float szxh = szx * 0.5f;
318 float szyh = szy * 0.5f;
321 for (
int y=0; y<4; y++) {
322 for (
int x=0; x<6; x++) {
324 float pxs = pad + x*psz + pad;
325 float pxe = pad + (x+1)*psz - pad;
326 float pys = pad + y*psz + pad;
327 float pye = pad + (y+1)*psz - pad;
329 verts[idx ] = pxs - szxh;
330 verts[idx + 1] = height;
331 verts[idx + 2] = -pys + szyh;
334 verts[idx ] = pxe - szxh;
335 verts[idx + 1] = height;
336 verts[idx + 2] = -pys + szyh;
339 verts[idx ] = pxs - szxh;
340 verts[idx + 1] = height;
341 verts[idx + 2] = -pye + szyh;
344 verts[idx ] = pxe - szxh;
345 verts[idx + 1] = height;
346 verts[idx + 2] = -pye + szyh;
360 numtris = numpts * 2;
361 meshindices = numtris * 3;
362 int *indices =
new int[meshindices];
363 memset(indices, 0, meshindices *
sizeof(
int));
366 for (
int y=0; y<4; y++) {
367 for (
int x=0; x<6; x++,idx4+=4,idx6+=6) {
368 indices[idx6 ] = idx4;
369 indices[idx6 + 1] = idx4 + 1;
370 indices[idx6 + 2] = idx4 + 2;
372 indices[idx6 + 3] = idx4 + 2;
373 indices[idx6 + 4] = idx4 + 1;
374 indices[idx6 + 5] = idx4 + 3;
387 int numverts = numpts * 4;
388 float *patchcols =
new float[numverts * 4];
391 for (
int i=0; i<numpts; i++) {
394 for (
int v=0; v<4; v++,vidx+=4) {
395 patchcols[vidx ] = colors[pidx ];
396 patchcols[vidx + 1] = colors[pidx + 1];
397 patchcols[vidx + 2] = colors[pidx + 2];
398 patchcols[vidx + 3] = colors[pidx + 3];
410 for (
int i=0; i<num; i++) {
413 col3f[i3 ] = col4f[i4 ];
414 col3f[i3 + 1] = col4f[i4 + 1];
415 col3f[i3 + 2] = col4f[i4 + 2];
424 float *coords,
float radius,
425 float *colors,
int mat) {
427 spheres.
center.resize(numpts);
428 spheres.
radius.resize(numpts);
431 float3 *verts = spheres.
center.data();
432 float *radii = spheres.
radius.data();
435 memcpy(verts, coords, numpts * 3 *
sizeof(
float));
437 for (
int i=0; i<numpts; i++)
448 float *coords,
int *trimesh_indices,
449 float *colors,
int mat) {
453 mesh.
indices.resize(numpts * 2);
455 float3 *verts = mesh.
vertices.data();
457 int3 *indices = mesh.
indices.data();
459 memcpy(verts, coords, numpts * 4 * 3 *
sizeof(
float));
461 memcpy(indices, trimesh_indices, numpts * 2 * 3 *
sizeof(
int));
471 float width,
float height,
float length,
int mat) {
473 -100.0f, 4.0f, -100.0f,
474 -100.0f, 4.0f, 100.0f,
475 100.0f, 4.0f, -100.0f,
489 vertex[ 0] = -width / 2.0f;
490 vertex[ 3] = -width / 2.0f;
491 vertex[ 6] = width / 2.0f;
492 vertex[ 9] = width / 2.0f;
499 vertex[ 2] = -
length / 2.0f;
500 vertex[ 5] =
length / 2.0f;
501 vertex[ 8] = -
length / 2.0f;
502 vertex[11] =
length / 2.0f;
506 mesh.
tex2d.resize(4);
509 float3 *verts = mesh.
vertices.data();
510 float2 *tex2d = mesh.
tex2d.data();
511 int3 *indices = mesh.
indices.data();
513 memcpy(verts, vertex, 4 * 3 *
sizeof(
float));
514 memcpy(tex2d, texcoord, 4 * 2 *
sizeof(
float));
515 memcpy(indices, index, 2 * 3 *
sizeof(
int));
526 -100.0f, 4.0f, -100.0f,
527 -100.0f, 4.0f, 100.0f,
528 100.0f, 4.0f, -100.0f,
532 1.0f, 1.0f, 1.0f, 1.0f,
533 1.0f, 1.0f, 1.0f, 1.0f,
534 1.0f, 1.0f, 1.0f, 1.0f,
535 1.0f, 1.0f, 1.0f, 1.0f
542 vertex[ 0] = -width / 2.0f;
543 vertex[ 3] = -width / 2.0f;
544 vertex[ 6] = width / 2.0f;
545 vertex[ 9] = width / 2.0f;
552 vertex[ 2] = -
length / 2.0f;
553 vertex[ 5] =
length / 2.0f;
554 vertex[ 8] = -
length / 2.0f;
555 vertex[11] =
length / 2.0f;
562 float3 *verts = mesh.
vertices.data();
564 int3 *indices = mesh.
indices.data();
566 memcpy(verts, vertex, 4 * 3 *
sizeof(
float));
568 memcpy(indices, index, 2 * 3 *
sizeof(
int));
574 int main(
int argc,
const char **argv) {
576 int imgSize[2] = {1024, 1024 };
581 int ambientocclusion = 1;
604 printf(
"Usage help: %s [optional flags]\n", argv[0]);
605 printf(
" options: -nofloor: don't draw floor\n");
606 printf(
" -nospheres: don't draw spheres over patches\n");
607 printf(
" -drawtex4ub: draw patches w/ sRGB uchar4 texture\n");
609 printf(
" -ao: add renderer-specific AO lighting\n");
610 printf(
" -res XXXX YYYY: override default image res\n");
616 for (
int i=1; i<argc; i++) {
618 if (!strcmp(
"-quads", argv[i])) {
620 printf(
"Drawing surface using quads.\n");
625 if (!strcmp(
"-nofloor", argv[i])) {
627 printf(
"Drawing surface without floor.\n");
631 if (!strcmp(
"-drawtex4ub", argv[i])) {
633 printf(
"Drawing surface sRGB unsigned byte texture map.\n");
637 if (!strcmp(
"-nospheres", argv[i])) {
639 printf(
"Drawing surface without sphere points.\n");
643 if (!strcmp(
"-ao", argv[i])) {
644 ambientocclusion = 1;
645 printf(
"Enabling renderer-specific AO lighting.\n");
649 if (!strcmp(
"-res", argv[i])) {
650 if ((argc - i) >= 2) {
651 imgSize[0] = atoi(argv[++i]);
652 imgSize[1] = atoi(argv[++i]);
653 printf(
"Image resolution set to: %d x %d\n", imgSize[0], imgSize[1]);
658 if (!strcmp(
"-pause", argv[i])) {
662 printf(
"Unrecognized flag: '%s'.\n", argv[i]);
673 int imgszx=0, imgszy=0;
675 printf(
"Writing %dx%d sRGB reference chart to 'colorchecker-refchart.png'\n",
677 stbi_write_png(
"colorchecker-refchart.png", imgszx, imgszy, 4, chartimg4ub, imgszx *
sizeof(
int));
683 int numtris=0, numindices=0;
690 printf(
"Initializing TachyonOptiX...");
697 printf(
"Found %u OptiX devices\n", devcount);
698 printf(
"OptiX version used for build: %d.%d.%d (%u)\n",
700 (optixversion%10000)/100,
717 float rtbgcolor[] = { 1.0, 1.0, 1.0 };
718 float rtbggradtopcolor[] = { 0.6, 0.0, 0.0 };
719 float rtbggradbotcolor[] = { 0.0, 0.0, 0.6 };
725 float bggradient[] = { 0.0f, 1.0f, 0.0f };
737 if (ambientocclusion) {
747 float lightdir0[] = { -0.5f, 0.5f, -1.0f };
748 float lightcolor0[] = { 1.0f, 1.0f, 1.0f };
755 float campos[3] = {0.0f, 10.0f, -8.0f};
757 float camV[3] = {0.0f, 1.0f, 0.0f};
759 float camat[3] = {0.0f, 0.0f, 0.0f};
775 float density = 0.33f;
782 float ambient = 0.0f;
783 float diffuse = 0.9f;
784 float specular = 0.0f;
785 float shininess = 0.0f;
786 float reflectivity = 0.0f;
787 float opacity = 1.0f;
788 float outline = 0.0f;
789 float outlinewidth = 0.0f;
792 rt->
add_material(ambient, diffuse, specular, shininess, reflectivity,
793 opacity, outline, outlinewidth, transmode, mat);
814 if (chartimg4ub != NULL) {
819 shininess, reflectivity, opacity,
820 outline, outlinewidth, transmode,
823 rt->
add_material(ambient, diffuse, specular, shininess, reflectivity,
824 opacity, outline, outlinewidth, transmode, mat2);
827 gen_teximg(rt, imgszx, imgszy, 11.0f, 0.5f, 7.3333333f, mat2);
830 gen_chart(rt, numpts, patchverts, patchindices, patchcols, mat);
835 gen_floor(rt, 200.0f, -3.0f, 200.0f, mat);
841 printf(
"Rendering frames w/ accumulation buffer...\n");
852 const char *filename =
"colorchecker.png";
853 printf(
"Writing accumulated frames to '%s'...\n", filename);
854 if (filename != NULL) {
856 size_t bufsz = imgSize[0] * imgSize[1] *
sizeof(int);
857 unsigned char *rgb4u = (
unsigned char *) calloc(1, bufsz);
862 printf(
"Writing rgba4u alpha channel output image 2\n");
863 if (write_image_file_rgba4u(filename, rgb4u, imgSize[0], imgSize[1]))
864 printf(
"Failed to write image '%s'!!\n", filename);
866 if (write_image_file_rgb4u(filename, rgb4u, imgSize[0], imgSize[1]))
867 printf(
"Failed to write image '%s'!!\n", filename);
872 stbi_write_png(filename, imgSize[0], imgSize[1], 4, rgb4u, imgSize[0] *
sizeof(
int));
878 delete [] chartimg4ub;
881 delete [] patchverts;
882 delete [] patchindices;
STBIWDEF void stbi_flip_vertically_on_write(int flip_boolean)
void color4f_to_color3f(float *col3f, const float *col4f, int num)
void set_ao_maxdist(float dist)
set AO maximum occlusion distance
std::vector< float3 PINALLOCS(float3)> center
static const int colorchecker_maxval
void gen_chart(TachyonOptiX *rt, int numpts, float *coords, int *trimesh_indices, float *colors, int mat)
void set_bg_gradient_topval(float v)
set background gradient "top" value (view direction dot product)
void img_draw_box_rgba4ub(int bxs, int bys, int bxe, int bye, unsigned char *rgba4ub, int imgxs, int imgys, unsigned char *img)
void set_ao_samples(int cnt)
ambient occlusion (samples > 1 == on)
int add_tex2d_rgba4u(const unsigned char *img, int xres, int yres, int texflags, int userindex)
define image to be used in a texture map
int add_material_textured(float ambient, float diffuse, float specular, float shininess, float reflectivity, float opacity, float outline, float outlinewidth, int transmode, int textureindex, int userindex)
void framebuffer_get_size(int &fbwidth, int &fbheight)
void add_directional_light(const float *dir, const float *color)
float * img_gen_colorchecker_f32(int tilesz, int pad, const int *srgbi, int maxval, int convertsrgbtolinear, int &imgszx, int &imgszy)
int add_material(float ambient, float diffuse, float specular, float shininess, float reflectivity, float opacity, float outline, float outlinewidth, int transmode, int userindex)
add a material with an associated user-provided index
CPU and GPU profiling utility macros/routines.
void img_draw_box_rgbaf32(int bxs, int bys, int bxe, int bye, float *rgbaf32, int imgxs, int imgys, float *img)
#define PROFILE_PUSH_RANGE(name, cid)
Pushes a time range annotation onto the profiler's trace stack, beginning at the time of submission...
std::vector< float PINALLOCS(float)> radius
void framebuffer_resize(int fbwidth, int fbheight)
static const int colorchecker_srgbi[]
void gen_colorchecker_spheres(TachyonOptiX *rt, int numpts, float *coords, float radius, float *colors, int mat)
void camera_dof_enable(int onoff)
depth of field on/off
static int device_count(void)
static GPU device query
void set_camera_lookat(const float *at, const float *V)
set camera orientation to look "at" a point in space, with a given "up" direction (camera ONB "V" vec...
Table of sRGB colors that match the classic pre-2014 ColorChecker charts by Gretag-Macbeth / X-Rite /...
void print_raystats_info(void)
report performance statistics
std::vector< float3 PINALLOCS(float3) > vertices
float * calc_colorchecker_patch_verts(float tilesz, float pad, float height)
float * calc_colorchecker_patch_colors(const float *colors)
Output timing/perf data only.
void set_ao_direct(float aod)
set AO direct lighting rescale factor
void set_bg_gradient(float *vec)
set world "up" direction for background gradient
void add_spherearray(SphereArray &model, int matidx)
std::vector< float3 PINALLOCS(float3) > vertcolors3f
#define PROFILE_POP_RANGE()
Pops the innermost time range off of the profiler's trace stack, at the time of execution.
void set_camera_zoom(float zoomfactor)
set camera zoom factor
void int_to_normalized_float(const int *srgbi, int maxval, float *srgbf)
std::vector< float2 PINALLOCS(float2) > tex2d
void set_camera_dof_fnumber(float n)
set depth of field f/stop number
float * calc_colorchecker_gridpts(float tilesz, float pad, float height)
void HSItoRGB(float h, float s, float i, float &r, float &g, float &b)
void set_bg_color_grad_bot(float *rgb)
set color for "bottom" of background gradient
void set_camera_type(CameraType m)
set the camera projection mode
void framebuffer_download_rgb4u(unsigned char *imgrgb4u)
void set_camera_stereo_convergence_dist(float dist)
set stereo convergence distance
void shadows_enable(int onoff)
enable/disable shadows
#define COLORCHECKER_NUM_PATCHES
void set_bg_color(float *rgb)
set solid background color
void set_ao_ambient(float aoa)
set AO ambient lighting factor
void set_aa_samples(int cnt)
antialiasing (samples > 1 == on)
#define PROFILE_START()
Trigger the beginning of profiler trace capture, for those that support it.
void set_cue_mode(FogMode mode, float start, float end, float density)
set depth cueing mode and parameters
void set_bg_gradient_botval(float v)
set background gradient "bottom" value (view direction dot product)
STBIWDEF int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes)
void set_verbose_mode(TachyonOptiX::Verbosity mode)
programmatically set verbosity
void set_bg_color_grad_top(float *rgb)
set color for "top" of background gradient
__host__ __device__ float length(const float3 &v)
void set_camera_pos(const float *pos)
set the camera position
static unsigned int optix_version(void)
static OptiX version query
void linear_to_srgb(const float *lin, float *srgb)
std::vector< int3 PINALLOCS(int3) > indices
int main(int argc, const char **argv)
void set_camera_stereo_eyesep(float eyesep)
set stereo eye separation
int * calc_colorchecker_patch_indices(int &numtris, int &meshindices)
unsigned char * img_gen_colorchecker_4ub(int tilesz, int pad, const int *srgbi, int &imgszx, int &imgszy)
void framebuffer_colorspace(int colspace)
Tachyon ray tracing host side routines and internal APIs that provide the core ray OptiX-based RTX-ac...
float * calc_colorchecker_linear_rgba(const int *srgbi, int maxval)
void gen_teximg(TachyonOptiX *rt, int imszx, int imszy, float width, float height, float length, int mat)
void set_bg_mode(BGMode m)
set background rendering mode
void set_camera_dof_focal_dist(float d)
set depth of field focal plane distance
std::vector< float3 PINALLOCS(float3)> primcolors3f
void add_trimesh(TriangleMesh &model, int matidx)
void srgb_to_linear(const float *srgb, float *lin)
void gen_floor(TachyonOptiX *rt, float width, float height, float length, int mat)