00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00038 #include <math.h>
00039 #include <stdlib.h>
00040 #include <stdio.h>
00041 #include <string.h>
00042
00043 #if defined(__linux)
00044 #include <unistd.h>
00045 #endif
00046
00047 #include "Inform.h"
00048 #include "ImageIO.h"
00049 #include "OSPRayRenderer.h"
00050
00051 #include "Matrix4.h"
00052 #include "utilities.h"
00053 #include "WKFUtils.h"
00054
00055 #if !(OSPRAY_VERSION_MAJOR >= 1 && OSPRAY_VERSION_MINOR >= 2)
00056 #error VMD requires OSPRay >= 1.2.0 for correct transparent AO shading
00057
00058 #endif
00059
00060
00061 #if defined(VMDOSPRAY_INTERACTIVE_OPENGL)
00062 #if (defined(WIN32) || defined(_WIN64)) && defined(_MSC_VER)
00063 #include <windows.h>
00064 #endif
00065
00066 #include <GL/gl.h>
00067 #endif
00068
00069 #if 1
00070 #define DBG()
00071 #else
00072 #define DBG() printf("OSPRayRenderer) %s\n", __func__);
00073 #endif
00074
00075 static void vmd_ospray_error_callback(OSPError err, const char *detailstr) {
00076 printf("OSPRayRenderer) ERROR: '%s'\n", detailstr);
00077 }
00078
00079 static void vmd_ospray_status_callback(const char *detailstr) {
00080 printf("OSPRayRenderer) STATUS: %s\n", detailstr);
00081 }
00082
00083
00084
00085
00086 int OSPRayRenderer::OSPRay_Global_Init(void) {
00087 DBG();
00088
00089
00090 const char *ospraynormalargs[] = {"vmd", "--osp:mpi"};
00091 const char *ospraydebugargs[] = {"vmd", "--osp:debug", "--osp:mpi"};
00092 const char **osprayargs = ospraynormalargs;
00093 int argcount = 1;
00094
00095 if (getenv("VMDOSPRAYDEBUG") != NULL) {
00096 osprayargs = ospraydebugargs;
00097 argcount=2;
00098 }
00099
00100
00101
00102
00103 if (getenv("VMDOSPRAYMPI") || getenv("VMD_OSPRAY_MPI")) {
00104 msgInfo << "OSPRayRenderer) Initializing OSPRay in MPI mode" << sendmsg;
00105 argcount++;
00106 }
00107
00108 OSPError osprc = ospInit(&argcount, osprayargs);
00109
00110 if (osprc != OSP_NO_ERROR)
00111 return -1;
00112
00113 return 0;
00114 }
00115
00116
00117
00118 void OSPRayRenderer::OSPRay_Global_Shutdown(void) {
00119 ospShutdown();
00120 }
00121
00123 OSPRayRenderer::OSPRayRenderer(void) {
00124 DBG();
00125
00126 osp_timer = wkf_timer_create();
00127 wkf_timer_start(osp_timer);
00128
00129
00130 ospRenderer = NULL;
00131 ospFrameBuffer = NULL;
00132 ospCamera = NULL;
00133 ospModel = NULL;
00134 ospLightData = NULL;
00135
00136 lasterror = 0;
00137 context_created = 0;
00138 buffers_allocated = 0;
00139 scene_created = 0;
00140
00141 destroy_scene();
00142
00143
00144 time_ctx_create = 0.0;
00145 time_ctx_setup = 0.0;
00146 time_ctx_validate = 0.0;
00147 time_ctx_AS_build = 0.0;
00148 time_ray_tracing = 0.0;
00149 time_image_io = 0.0;
00150
00151
00152 scene_background_mode = RT_BACKGROUND_TEXTURE_SOLID;
00153 memset(scene_bg_color, 0, sizeof(scene_bg_color));
00154 memset(scene_bg_grad_top, 0, sizeof(scene_bg_grad_top));
00155 memset(scene_bg_grad_bot, 0, sizeof(scene_bg_grad_bot));
00156 memset(scene_gradient, 0, sizeof(scene_gradient));
00157 scene_gradient_topval = 1.0f;
00158 scene_gradient_botval = 0.0f;
00159
00160 scene_gradient_invrange = 1.0f / (scene_gradient_topval - scene_gradient_botval);
00161
00162 cam_zoom = 1.0f;
00163 cam_stereo_eyesep = 0.06f;
00164 cam_stereo_convergence_dist = 2.0f;
00165
00166 headlight_enabled = 0;
00167
00168 shadows_enabled = RT_SHADOWS_OFF;
00169 aa_samples = 0;
00170
00171 ao_samples = 0;
00172 ao_direct = 0.3f;
00173 ao_ambient = 0.7f;
00174
00175 dof_enabled = 0;
00176 cam_dof_focal_dist = 2.0f;
00177 cam_dof_fnumber = 64.0f;
00178
00179 fog_mode = RT_FOG_NONE;
00180 fog_start = 0.0f;
00181 fog_end = 10.0f;
00182 fog_density = 0.32f;
00183
00184 verbose = RT_VERB_MIN;
00185 check_verbose_env();
00186
00187
00188 trimesh_v3f_n3f_c3f.clear();
00189 spheres.clear();
00190 spheres_color.clear();
00191 cylinders_color.clear();
00192
00193 if (verbose == RT_VERB_DEBUG) printf("OSPRayRenderer) creating context...\n");
00194
00195 if (verbose == RT_VERB_DEBUG) printf("OSPRayRenderer) setting error / status callbacks...\n");
00196 OSPDevice dev = ospGetCurrentDevice();
00197 ospDeviceSetErrorFunc(dev, vmd_ospray_error_callback);
00198 ospDeviceSetStatusFunc(dev, vmd_ospray_status_callback);
00199 int loglevel = 0;
00200
00201 ospDeviceSet1i(dev, "logLevel", loglevel);
00202 ospDeviceCommit(dev);
00203
00204 double starttime = wkf_timer_timenow(osp_timer);
00205
00206 if (verbose == RT_VERB_DEBUG) printf("OSPRayRenderer) creating context...\n");
00207
00208
00209
00210
00211
00212 if ((ospRenderer = ospNewRenderer("scivis")) == NULL) {
00213 printf("OSPRayRenderer) Failed to load OSPRay renderer 'scivis'!\n");
00214 }
00215
00216
00217 init_materials();
00218
00219 time_ctx_create = wkf_timer_timenow(osp_timer) - starttime;
00220
00221 if (verbose == RT_VERB_TIMING || verbose == RT_VERB_DEBUG) {
00222 printf("OSPRayRenderer) context creation time: %.2f\n", time_ctx_create);
00223 }
00224
00225 context_created = 1;
00226 }
00227
00228
00230 OSPRayRenderer::~OSPRayRenderer(void) {
00231 DBG();
00232
00233 int lcnt = ospLights.size();
00234 for (int i = 0; i < lcnt; ++i) {
00235 ospRelease(ospLights[i]);
00236 }
00237 ospLights.clear();
00238
00239 framebuffer_destroy();
00240
00241 if (context_created && (ospRenderer != NULL))
00242 ospRelease(ospRenderer);
00243
00244 if (context_created && (ospModel != NULL))
00245 ospRelease(ospModel);
00246
00247 wkf_timer_destroy(osp_timer);
00248 }
00249
00250
00251 void OSPRayRenderer::check_verbose_env() {
00252 DBG();
00253
00254 char *verbstr = getenv("VMDOSPRAYVERBOSE");
00255 if (verbstr != NULL) {
00256
00257 if (!strupcmp(verbstr, "MIN")) {
00258 verbose = RT_VERB_MIN;
00259 printf("OSPRayRenderer) verbose setting: minimum\n");
00260 } else if (!strupcmp(verbstr, "TIMING")) {
00261 verbose = RT_VERB_TIMING;
00262 printf("OSPRayRenderer) verbose setting: timing data\n");
00263 } else if (!strupcmp(verbstr, "DEBUG")) {
00264 verbose = RT_VERB_DEBUG;
00265 printf("OSPRayRenderer) verbose setting: full debugging data\n");
00266 }
00267 }
00268 }
00269
00270
00271 void OSPRayRenderer::setup_context(int w, int h) {
00272 DBG();
00273 double starttime = wkf_timer_timenow(osp_timer);
00274 time_ctx_setup = 0;
00275
00276 lasterror = 0;
00277 width = w;
00278 height = h;
00279
00280 if (!context_created)
00281 return;
00282
00283 check_verbose_env();
00284
00285 if (getenv("VMDOSPRAYMAXDEPTH")) {
00286 int maxdepth = atoi(getenv("VMDOSPRAYMAXDEPTH"));
00287 if (maxdepth > 0 && maxdepth <= 20) {
00288 printf("OSPRayRenderer) Setting maxdepth to %d...\n", maxdepth);
00289 ospSet1i(ospRenderer, "maxDepth", maxdepth);
00290 } else {
00291 printf("OSPRayRenderer) ignoring out-of-range maxdepth: %d...\n", maxdepth);
00292 }
00293 } else {
00294 ospSet1i(ospRenderer, "maxDepth", 20);
00295 }
00296
00297 float scene_epsilon = 5.e-5f;
00298 if (getenv("VMDOSPRAYSCENEEPSILON") != NULL) {
00299 scene_epsilon = float(atof(getenv("VMDOSPRAYSCENEEPSILON")));
00300 printf("OSPRayRenderer) user override of scene epsilon: %g\n", scene_epsilon);
00301 }
00302 ospSet1f(ospRenderer, "epsilon", scene_epsilon);
00303
00304
00305
00306 ospSet1i(ospRenderer, "aoTransparencyEnabled", 1);
00307
00308
00309
00310
00311
00312 time_ctx_setup = wkf_timer_timenow(osp_timer) - starttime;
00313 }
00314
00315
00316 void OSPRayRenderer::destroy_scene() {
00317 DBG();
00318
00319 double starttime = wkf_timer_timenow(osp_timer);
00320 time_ctx_destroy_scene = 0;
00321
00322
00323 cylinder_array_cnt = 0;
00324 cylinder_array_color_cnt = 0;
00325 ring_array_color_cnt = 0;
00326 sphere_array_cnt = 0;
00327 sphere_array_color_cnt = 0;
00328 tricolor_cnt = 0;
00329 trimesh_c4u_n3b_v3f_cnt = 0;
00330 trimesh_n3b_v3f_cnt = 0;
00331 trimesh_n3f_v3f_cnt = 0;
00332 trimesh_v3f_cnt = 0;
00333
00334
00335 trimesh_v3f_n3f_c3f.clear();
00336 spheres.clear();
00337 spheres_color.clear();
00338 cylinders_color.clear();
00339
00340 int i;
00341 for (i=0; i<materialcache.num(); i++) {
00342 ospRelease(materialcache[i].mat);
00343 }
00344 materialcache.clear();
00345
00346 int lcnt = ospLights.size();
00347 for (i = 0; i < lcnt; ++i) {
00348 ospRelease(ospLights[i]);
00349 }
00350 ospLights.clear();
00351
00352 ospRelease(ospCamera);
00353 ospCamera = NULL;
00354
00355 double endtime = wkf_timer_timenow(osp_timer);
00356 time_ctx_destroy_scene = endtime - starttime;
00357
00358 scene_created = 0;
00359 }
00360
00361
00362 void OSPRayRenderer::update_rendering_state(int interactive) {
00363 DBG();
00364 if (!context_created)
00365 return;
00366
00367 wkf_timer_start(osp_timer);
00368
00369
00370
00371
00372
00373
00374 interactive_renderer = interactive;
00375
00376
00377
00378 long totaltris = tricolor_cnt + trimesh_c4u_n3b_v3f_cnt +
00379 trimesh_n3b_v3f_cnt + trimesh_n3f_v3f_cnt + trimesh_v3f_cnt;
00380
00381 if (verbose == RT_VERB_TIMING || verbose == RT_VERB_DEBUG) {
00382 printf("OSPRayRenderer) cyl %ld, ring %ld, sph %ld, tri %ld, tot: %ld lt %ld\n",
00383 cylinder_array_cnt + cylinder_array_color_cnt,
00384 ring_array_color_cnt,
00385 sphere_array_cnt + sphere_array_color_cnt,
00386 totaltris,
00387 cylinder_array_cnt + cylinder_array_color_cnt + ring_array_color_cnt + sphere_array_cnt + sphere_array_color_cnt + totaltris,
00388 directional_lights.num() + positional_lights.num());
00389 }
00390
00391 if (verbose == RT_VERB_DEBUG) {
00392 printf("OSPRayRenderer) using fully general shader and materials.\n");
00393 }
00394
00395
00396
00397 if (verbose == RT_VERB_DEBUG) {
00398 printf("OSPRayRenderer) scene bg mode: %d\n", scene_background_mode);
00399
00400 printf("OSPRayRenderer) scene bgsolid: %.2f %.2f %.2f\n",
00401 scene_bg_color[0], scene_bg_color[1], scene_bg_color[2]);
00402
00403 printf("OSPRayRenderer) scene bggradT: %.2f %.2f %.2f\n",
00404 scene_bg_grad_top[0], scene_bg_grad_top[1], scene_bg_grad_top[2]);
00405
00406 printf("OSPRayRenderer) scene bggradB: %.2f %.2f %.2f\n",
00407 scene_bg_grad_bot[0], scene_bg_grad_bot[1], scene_bg_grad_bot[2]);
00408
00409 printf("OSPRayRenderer) bg gradient: %f %f %f top: %f bot: %f\n",
00410 scene_gradient[0], scene_gradient[1], scene_gradient[2],
00411 scene_gradient_topval, scene_gradient_botval);
00412 }
00413
00414
00415 scene_gradient_invrange = 1.0f / (scene_gradient_topval - scene_gradient_botval);
00416
00417
00418
00419
00420 if (verbose == RT_VERB_DEBUG) {
00421 printf("OSPRayRenderer) adding lights: dir: %ld pos: %ld\n",
00422 directional_lights.num(), positional_lights.num());
00423 }
00424
00425
00426
00427 if (verbose == RT_VERB_DEBUG)
00428 printf("OSPRayRenderer) Finalizing OSPRay scene graph...\n");
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440 if (verbose == RT_VERB_DEBUG) {
00441 printf("OSPRayRenderer) cam zoom factor %f\n", cam_zoom);
00442 printf("OSPRayRenderer) cam stereo eye separation %f\n", cam_stereo_eyesep);
00443 printf("OSPRayRenderer) cam stereo convergence distance %f\n",
00444 cam_stereo_convergence_dist);
00445 printf("OSPRayRenderer) cam DoF focal distance %f\n", cam_dof_focal_dist);
00446 printf("OSPRayRenderer) cam DoF f/stop %f\n", cam_dof_fnumber);
00447 }
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460 if (verbose == RT_VERB_DEBUG) {
00461 printf("OSPRayRenderer) setting sample counts: AA %d AO %d\n", aa_samples, ao_samples);
00462 printf("OSPRayRenderer) setting AO factors: AOA %f AOD %f\n", ao_ambient, ao_direct);
00463 }
00464
00465
00466
00467
00468
00469
00470
00471 #if 1
00472 ext_aa_loops = 1;
00473 #else
00474 ext_aa_loops = 1;
00475 if (ao_samples > 0 || (aa_samples > 4)) {
00476
00477
00478 ext_aa_loops = 1 + aa_samples;
00479
00480 } else {
00481
00482
00483
00484 }
00485
00486 #endif
00487
00488 if (verbose == RT_VERB_DEBUG) {
00489 if (ext_aa_loops > 1)
00490 printf("OSPRayRenderer) Running OSPRay multi-pass: %d loops\n", ext_aa_loops);
00491 else
00492 printf("OSPRayRenderer) Running OSPRay single-pass: %d total samples\n", 1+aa_samples);
00493 }
00494
00495
00496
00497
00498
00499
00500
00501
00502 }
00503
00504
00505 void OSPRayRenderer::framebuffer_config(int fbwidth, int fbheight) {
00506 if (!context_created)
00507 return;
00508
00509 width = fbwidth;
00510 height = fbheight;
00511
00512
00513 if (buffers_allocated) {
00514
00515
00516 if (verbose == RT_VERB_DEBUG) {
00517 printf("OSPRayRenderer) resizing framebuffer\n");
00518 }
00519 framebuffer_resize(width, height);
00520 } else {
00521
00522
00523
00524 if (verbose == RT_VERB_DEBUG) {
00525 printf("OSPRayRenderer) creating framebuffer and accum. buffer\n");
00526 }
00527
00528
00529 osp::vec2i fbsize = { width, height };
00530 ospFrameBuffer = ospNewFrameBuffer(fbsize, OSP_FB_RGBA8, OSP_FB_COLOR | OSP_FB_ACCUM);
00531 ospFrameBufferClear(ospFrameBuffer, OSP_FB_COLOR | OSP_FB_ACCUM);
00532
00533 buffers_allocated = 1;
00534 }
00535 }
00536
00537
00538 void OSPRayRenderer::framebuffer_resize(int fbwidth, int fbheight) {
00539 if (!context_created)
00540 return;
00541
00542 width = fbwidth;
00543 height = fbheight;
00544
00545 if (buffers_allocated) {
00546 if (verbose == RT_VERB_DEBUG)
00547 printf("OSPRayRenderer) framebuffer_resize(%d x %d)\n", width, height);
00548 framebuffer_destroy();
00549 }
00550
00551 osp::vec2i fbsize = { width, height };
00552 ospFrameBuffer = ospNewFrameBuffer(fbsize, OSP_FB_RGBA8, OSP_FB_COLOR | OSP_FB_ACCUM);
00553
00554 ospFrameBufferClear(ospFrameBuffer, OSP_FB_COLOR | OSP_FB_ACCUM);
00555 buffers_allocated = 1;
00556 }
00557
00558
00559 void OSPRayRenderer::framebuffer_destroy() {
00560 if (!context_created)
00561 return;
00562
00563 if (buffers_allocated) {
00564 if (ospFrameBuffer)
00565 #if OSPRAY_VERSION_MAJOR >= 1 && OSPRAY_VERSION_MINOR >= 6
00566 ospRelease(ospFrameBuffer);
00567 #else
00568 ospFreeFrameBuffer(ospFrameBuffer);
00569 #endif
00570 }
00571 buffers_allocated = 0;
00572 }
00573
00574
00575 void OSPRayRenderer::render_compile_and_validate(void) {
00576 int i;
00577
00578 DBG();
00579 if (!context_created)
00580 return;
00581
00582
00583
00584
00585 double startctxtime = wkf_timer_timenow(osp_timer);
00586
00587
00588
00589 if ((ospModel = ospNewModel()) == NULL) {
00590 printf("OSPRayRenderer) Failed to create new model!\n");
00591 }
00592
00593
00594
00595
00596
00597 if (spheres.size() > 0) {
00598 OSPData data = ospNewData(5 * spheres.size(), OSP_FLOAT, &spheres[0]);
00599 OSPGeometry sphere_geom = ospNewGeometry("spheres");
00600 ospSet1i(sphere_geom, "bytes_per_sphere", sizeof(Sphere));
00601 ospSet1i(sphere_geom, "offset_center", 0);
00602 ospSet1i(sphere_geom, "offset_radius", sizeof(vec3));
00603 ospSet1i(sphere_geom, "offset_materialID", sizeof(vec3) + sizeof(float));
00604 ospSetData(sphere_geom, "spheres", data);
00605 ospCommit(sphere_geom);
00606 ospAddGeometry(ospModel, sphere_geom);
00607 ospRelease(data);
00608 ospRelease(sphere_geom);
00609 }
00610
00611 if (verbose == RT_VERB_DEBUG)
00612 printf("OSPRayReenderer) num spheres = %ld\n", spheres.size());
00613
00614
00615
00616
00617
00618 float cam_pos_orig[3] = {0.0f, 0.0f, 2.0f};
00619 float cam_U_orig[3] = {1.0f, 0.0f, 0.0f};
00620 float cam_V_orig[3] = {0.0f, 1.0f, 0.0f};
00621 float cam_W_orig[3] = {0.0f, 0.0f, -1.0f};
00622
00623 float cam_pos[3], cam_U[3], cam_V[3], cam_W[3];
00624 vec_copy(cam_pos, cam_pos_orig);
00625 vec_copy(cam_U, cam_U_orig);
00626 vec_copy(cam_V, cam_V_orig);
00627 vec_copy(cam_W, cam_W_orig);
00628
00629 if (camera_projection == OSPRayRenderer::RT_ORTHOGRAPHIC) {
00630 if(!ospCamera) ospCamera = ospNewCamera("orthographic");
00631
00632 ospSet1f(ospCamera, "aspect", width / ((float) height));
00633 float orthoheight = 2.0f * cam_zoom;
00634 ospSet1f(ospCamera, "height", orthoheight);
00635
00636 if (dof_enabled) {
00637 msgWarn << "OSPRayRenderer) DoF not implemented for orthographic camera!" << sendmsg;
00638 }
00639 } else {
00640 if(!ospCamera) ospCamera = ospNewCamera("perspective");
00641
00642 ospSet1f(ospCamera, "aspect", width / ((float) height));
00643 ospSet1f(ospCamera, "fovy", 2.0f*180.0f*(atanf(cam_zoom)/float(VMD_PI)));
00644
00645 if (dof_enabled) {
00646 ospSet1f(ospCamera, "focusDistance", cam_dof_focal_dist);
00647 ospSet1f(ospCamera, "apertureRadius", cam_dof_focal_dist / (2.0f * cam_zoom * cam_dof_fnumber));
00648 } else {
00649 ospSet1f(ospCamera, "apertureRadius", 0.0f);
00650 }
00651 }
00652
00653 if (ospCamera) {
00654 ospSet3fv(ospCamera, "pos", cam_pos);
00655 ospSet3fv(ospCamera, "dir", cam_W);
00656 ospSet3fv(ospCamera, "up", cam_V);
00657 ospCommit(ospCamera);
00658
00659
00660 }
00661
00662
00663
00664
00665 framebuffer_config(width, height);
00666
00667
00668
00669
00670
00671
00672 float lightscale = 1.0f;
00673 if (ao_samples != 0)
00674 lightscale = ao_direct;
00675
00676 for (i = 0; i < directional_lights.num(); ++i) {
00677 #if OSPRAY_VERSION_MAJOR >= 1 && OSPRAY_VERSION_MINOR >= 7
00678 OSPLight light = ospNewLight3("distant");
00679 #elif OSPRAY_VERSION_MAJOR >= 1 && OSPRAY_VERSION_MINOR >= 6
00680 OSPLight light = ospNewLight2("scivis", "distant");
00681 #elif 1
00682 OSPLight light = ospNewLight(ospRenderer, "distant");
00683 #else
00684 OSPLight light = ospNewLight(ospRenderer, "DirectionalLight");
00685 #endif
00686
00687
00688 ospSet1f(light, "intensity", lightscale);
00689 ospSet3fv(light, "color", directional_lights[i].color);
00690
00691 float lightDir[3];
00692 vec_copy(lightDir, directional_lights[i].dir);
00693 vec_normalize(lightDir);
00694
00695
00696 ospSet3f(light, "direction", -lightDir[0], -lightDir[1], -lightDir[2]);
00697
00698 ospCommit(light);
00699 ospLights.push_back(light);
00700 }
00701
00702
00703 if (ao_samples != 0) {
00704 #if OSPRAY_VERSION_MAJOR >= 1 && OSPRAY_VERSION_MINOR >= 7
00705 OSPLight light = ospNewLight3("ambient");
00706 #elif OSPRAY_VERSION_MAJOR >= 1 && OSPRAY_VERSION_MINOR >= 6
00707 OSPLight light = ospNewLight2("scivis", "ambient");
00708 #else
00709 OSPLight light = ospNewLight(ospRenderer, "ambient");
00710 #endif
00711
00712
00713 ospSet1f(light, "intensity", ao_ambient);
00714 ospSet3f(light, "color", 1.0f, 1.0f, 1.0f);
00715
00716 ospCommit(light);
00717 ospLights.push_back(light);
00718 }
00719
00720
00721
00722
00723 if (ao_samples && interactive_renderer) {
00724 ospSet1i(ospRenderer, "aoSamples", 1);
00725 ospSet1i(ospRenderer, "spp", 1);
00726 } else {
00727 ospSet1i(ospRenderer, "spp", aa_samples);
00728 ospSet1i(ospRenderer, "aoSamples", ao_samples);
00729 }
00730
00731 if (getenv("VMDOSPRAYAOMAXDIST")) {
00732 float tmp = float(atof(getenv("VMDOSPRAYAOMAXDIST")));
00733 if (verbose == RT_VERB_DEBUG) {
00734 printf("OSPRayRenderer) setting AO maxdist: %f\n", tmp);
00735 }
00736 ospSet1f(ospRenderer, "aoOcclusionDistance", tmp);
00737 }
00738
00739 float scene_epsilon = 5.e-5f;
00740 if (getenv("VMDOSPRAYSCENEEPSILON") != NULL) {
00741 scene_epsilon = float(atof(getenv("VMDOSPRAYSCENEEPSILON")));
00742 printf("OSPRayRenderer) User override of scene epsilon: %g\n", scene_epsilon);
00743 }
00744 ospSet1f(ospRenderer, "epsilon", scene_epsilon);
00745
00746
00747 if (shadows_enabled || ao_samples) {
00748 if (shadows_enabled && !ao_samples)
00749 msgInfo << "Shadow rendering enabled." << sendmsg;
00750
00751 ospSet1i(ospRenderer, "shadowsEnabled", 1);
00752 } else {
00753 ospSet1i(ospRenderer, "shadowsEnabled", 0);
00754 }
00755
00756
00757 if (ao_samples) {
00758 msgInfo << "Ambient occlusion enabled." << sendmsg;
00759 msgInfo << "Shadow rendering enabled." << sendmsg;
00760 }
00761
00762
00763 for (i=0; i<trimesh_v3f_n3f_c3f.num(); i++) {
00764 if (verbose == RT_VERB_DEBUG)
00765 printf("OSPRayRenderer) Adding triangle mesh[%d]: %d tris ...\n",
00766 i, trimesh_v3f_n3f_c3f[i].numtris);
00767 ospAddGeometry(ospModel, trimesh_v3f_n3f_c3f[i].geom);
00768 ospRelease(trimesh_v3f_n3f_c3f[i].geom);
00769 }
00770
00771
00772 for (i=0; i<spheres_color.num(); i++) {
00773 if (verbose == RT_VERB_DEBUG)
00774 printf("OSPRayRenderer) Adding sphere_color array [%d]: %d spheres ...\n",
00775 i, spheres_color[i].num);
00776 ospAddGeometry(ospModel, spheres_color[i].geom);
00777 ospRelease(spheres_color[i].geom);
00778 }
00779
00780
00781 for (i=0; i<cylinders_color.num(); i++) {
00782 if (verbose == RT_VERB_DEBUG)
00783 printf("OSPRayRenderer) Adding cylinders_color array [%d]: %d cylinders...\n",
00784 i, cylinders_color[i].num);
00785 ospAddGeometry(ospModel, cylinders_color[i].geom);
00786 ospRelease(cylinders_color[i].geom);
00787 }
00788
00789
00790 ospCommit(ospModel);
00791
00792
00793
00794
00795 ospLightData = ospNewData(ospLights.size(), OSP_OBJECT, &ospLights[0], 0);
00796 ospSetData(ospRenderer, "lights", ospLightData);
00797 ospSet3fv(ospRenderer, "bgColor", scene_bg_color);
00798 ospSetObject(ospRenderer, "camera", ospCamera);
00799 ospSetObject(ospRenderer, "world", ospModel);
00800 ospCommit(ospRenderer);
00801 ospRelease(ospLightData);
00802
00803 if (verbose == RT_VERB_DEBUG) printf("OSPRayRenderer) Finalizing OSPRay rendering kernels...\n");
00804
00805
00806 double contextinittime = wkf_timer_timenow(osp_timer);
00807 time_ctx_validate = contextinittime - startctxtime;
00808
00809
00810
00811
00812
00813
00814 time_ctx_AS_build = wkf_timer_timenow(osp_timer) - contextinittime;
00815 if (verbose == RT_VERB_DEBUG) {
00816 printf("OSPRayRenderer) launching render: %d x %d\n", width, height);
00817 }
00818 }
00819
00820
00821 #if defined(VMDOSPRAY_INTERACTIVE_OPENGL)
00822
00823 static void *createospraywindow(const char *wintitle, int width, int height) {
00824 printf("OSPRayRenderer) Creating OSPRay window: %d x %d...\n", width, height);
00825
00826 void *win = glwin_create(wintitle, width, height);
00827 while (glwin_handle_events(win, GLWIN_EV_POLL_NONBLOCK) != 0);
00828
00829 glDrawBuffer(GL_BACK);
00830 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
00831 glClearColor(0.0, 0.0, 0.0, 1.0);
00832 glViewport(0, 0, width, height);
00833 glClear(GL_COLOR_BUFFER_BIT);
00834
00835 glShadeModel(GL_FLAT);
00836 glMatrixMode(GL_PROJECTION);
00837 glLoadIdentity();
00838 glOrtho(0.0, width, height, 0.0, -1.0, 1.0);
00839 glMatrixMode(GL_MODELVIEW);
00840 glLoadIdentity();
00841
00842 glDrawBuffer(GL_BACK);
00843 glClear(GL_COLOR_BUFFER_BIT);
00844
00845 glwin_swap_buffers(win);
00846
00847 return win;
00848 }
00849
00850
00851 static void interactive_viewer_usage(void *win) {
00852 printf("OSPRayRenderer) VMD TachyonL-OSPRay Interactive Ray Tracer help:\n");
00853 printf("OSPRayRenderer) ================================================\n");
00854
00855
00856 int havespaceball = ((glwin_spaceball_available(win)) && (getenv("VMDDISABLESPACEBALLXDRV") == NULL));
00857 printf("OSPRayRenderer) Spaceball/SpaceNavigator/Magellan: %s\n",
00858 (havespaceball) ? "Available" : "Not available");
00859
00860
00861 int havestereo, havestencil;
00862 glwin_get_wininfo(win, &havestereo, &havestencil);
00863 printf("OSPRayRenderer) Stereoscopic display: %s\n",
00864 (havestereo) ? "Available" : "Not available");
00865
00866
00867 int vsync=0, rc=0;
00868 if ((rc = glwin_query_vsync(win, &vsync)) == GLWIN_SUCCESS) {
00869 printf("OSPRayRenderer) Vert retrace sync: %s\n", (vsync) ? "On" : "Off");
00870 } else {
00871 printf("OSPRayRenderer) Vert retrace sync: indeterminate\n");
00872 }
00873
00874 printf("OSPRayRenderer)\n");
00875 printf("OSPRayRenderer) General controls:\n");
00876 printf("OSPRayRenderer) space: save numbered snapshot image\n");
00877 printf("OSPRayRenderer) =: reset to initial view\n");
00878 printf("OSPRayRenderer) h: print this help info\n");
00879 printf("OSPRayRenderer) p: print current rendering parameters\n");
00880 printf("OSPRayRenderer) ESC,q: quit viewer\n");
00881 printf("OSPRayRenderer)\n");
00882 printf("OSPRayRenderer) Display controls\n");
00883 printf("OSPRayRenderer) F1: override shadows on/off (off=AO off too)\n");
00884 printf("OSPRayRenderer) F2: override AO on/off\n");
00885 printf("OSPRayRenderer) F3: override DoF on/off\n");
00886 printf("OSPRayRenderer) F4: override Depth cueing on/off\n");
00887
00888
00889
00890
00891 printf("OSPRayRenderer) F12: toggle full-screen display on/off\n");
00892 printf("OSPRayRenderer) 1-9,0: override samples per update auto-FPS off\n");
00893 printf("OSPRayRenderer) Up: increase DoF focal distance\n");
00894 printf("OSPRayRenderer) Down: decrease DoF focal distance\n");
00895 printf("OSPRayRenderer) Left: decrease DoF f/stop\n");
00896 printf("OSPRayRenderer) Right: increase DoF f/stop\n");
00897 printf("OSPRayRenderer) S: toggle stereoscopic display on/off (if avail)\n");
00898 printf("OSPRayRenderer) a: toggle AA/AO auto-FPS tuning on/off (on)\n");
00899 printf("OSPRayRenderer) g: toggle gradient sky xforms on/off (on)\n");
00900 printf("OSPRayRenderer) l: toggle light xforms on/off (on)\n");
00901 printf("OSPRayRenderer)\n");
00902 printf("OSPRayRenderer) Mouse controls:\n");
00903 printf("OSPRayRenderer) f: mouse depth-of-field mode\n");
00904 printf("OSPRayRenderer) r: mouse rotation mode\n");
00905 printf("OSPRayRenderer) s: mouse scaling mode\n");
00906 printf("OSPRayRenderer) t: mouse translation mode\n");
00907
00908 int movie_recording_enabled = (getenv("VMDOSPRAYLIVEMOVIECAPTURE") != NULL);
00909 if (movie_recording_enabled) {
00910 printf("OSPRayRenderer)\n");
00911 printf("OSPRayRenderer) Movie recording controls:\n");
00912 printf("OSPRayRenderer) R: start/stop movie recording\n");
00913 printf("OSPRayRenderer) F: toggle movie FPS (24, 30, 60)\n");
00914 }
00915 }
00916
00917
00918 void OSPRayRenderer::render_to_glwin(const char *filename) {
00919 DBG();
00920 int i;
00921
00922 if (!context_created)
00923 return;
00924
00925 enum RtMouseMode { RTMM_ROT=0, RTMM_TRANS=1, RTMM_SCALE=2, RTMM_DOF=3 };
00926 enum RtMouseDown { RTMD_NONE=0, RTMD_LEFT=1, RTMD_MIDDLE=2, RTMD_RIGHT=3 };
00927 RtMouseMode mm = RTMM_ROT;
00928 RtMouseDown mousedown = RTMD_NONE;
00929
00930
00931 int gl_shadows_on=(shadows_enabled) ? RT_SHADOWS_ON : RT_SHADOWS_OFF;
00932
00933 int gl_fs_on=0;
00934 int owsx=0, owsy=0;
00935 int gl_ao_on=(ao_samples > 0);
00936 int gl_dof_on, gl_dof_on_old;
00937 gl_dof_on=gl_dof_on_old=dof_enabled;
00938 int gl_fog_on=(fog_mode != RT_FOG_NONE);
00939
00940
00941
00942
00943
00944
00945
00946 int movie_recording_enabled = (getenv("VMDOSPRAYLIVEMOVIECAPTURE") != NULL);
00947 int movie_recording_on = 0;
00948 double movie_recording_start_time = 0.0;
00949 int movie_recording_fps = 30;
00950 int movie_framecount = 0;
00951 int movie_lastframeindex = 0;
00952 const char *movie_recording_filebase = "vmdlivemovie.%05d.tga";
00953 if (getenv("VMDOSPRAYLIVEMOVIECAPTUREFILEBASE"))
00954 movie_recording_filebase = getenv("VMDOSPRAYLIVEMOVIECAPTUREFILEBASE");
00955
00956
00957 int spaceballenabled=(getenv("VMDDISABLESPACEBALLXDRV") == NULL) ? 1 : 0;
00958 int spaceballmode=0;
00959 int spaceballflightmode=0;
00960 if (getenv("VMDOSPRAYSPACEBALLFLIGHT"))
00961 spaceballflightmode=1;
00962
00963
00964
00965 int totalsamplecount=0;
00966
00967
00968 int snapshotcount=0;
00969
00970
00971 int autosamplecount=1;
00972
00973
00974
00975 int xformlights=1, xformgradientsphere=1;
00976
00977
00978
00979
00980
00981
00982
00983
00984
00985
00986
00987 int wsx=width, wsy=height;
00988 const char *imageszstr = getenv("VMDOSPRAYIMAGESIZE");
00989 if (imageszstr) {
00990 if (sscanf(imageszstr, "%d %d", &width, &height) != 2) {
00991 width=wsx;
00992 height=wsy;
00993 }
00994 }
00995 framebuffer_config(width, height);
00996
00997
00998
00999 update_rendering_state(1);
01000 render_compile_and_validate();
01001
01002
01003
01004 int samples_per_pass = 1;
01005 int cur_aa_samples = aa_samples;
01006 int cur_ao_samples = ao_samples;
01007 float cam_zoom_orig = cam_zoom;
01008 float scene_gradient_orig[3] = {0.0f, 1.0f, 0.0f};
01009 vec_copy(scene_gradient_orig, scene_gradient);
01010
01011 float cam_pos_orig[3] = {0.0f, 0.0f, 2.0f};
01012 float cam_U_orig[3] = {1.0f, 0.0f, 0.0f};
01013 float cam_V_orig[3] = {0.0f, 1.0f, 0.0f};
01014 float cam_W_orig[3] = {0.0f, 0.0f, -1.0f};
01015 float cam_pos[3], cam_U[3], cam_V[3], cam_W[3];
01016 float hmd_U[3], hmd_V[3], hmd_W[3];
01017
01018 vec_copy(cam_pos, cam_pos_orig);
01019 vec_copy(cam_U, cam_U_orig);
01020 vec_copy(cam_V, cam_V_orig);
01021 vec_copy(cam_W, cam_W_orig);
01022
01023
01024 osp_directional_light *cur_dlights = (osp_directional_light *) calloc(1, directional_lights.num() * sizeof(osp_directional_light));
01025 for (i=0; i<directional_lights.num(); i++) {
01026 vec_copy((float*)&cur_dlights[i].color, directional_lights[i].color);
01027 vec_copy((float*)&cur_dlights[i].dir, directional_lights[i].dir);
01028 vec_normalize((float*)&cur_dlights[i].dir);
01029 }
01030
01031
01032 void *win = createospraywindow("VMD TachyonL-OSPRay Interactive Ray Tracer", width, height);
01033 interactive_viewer_usage(win);
01034
01035
01036 int havestereo=0, havestencil=0;
01037 int stereoon=0, stereoon_old=0;
01038 glwin_get_wininfo(win, &havestereo, &havestencil);
01039
01040
01041
01042
01043
01044
01045
01046 cur_aa_samples = samples_per_pass;
01047 if (cur_ao_samples > 0) {
01048 cur_aa_samples = 1;
01049 cur_ao_samples = samples_per_pass;
01050 }
01051
01052 const char *statestr = "|/-\\.";
01053 int done=0, winredraw=1, accum_count=0;
01054 int state=0, mousedownx=0, mousedowny=0;
01055 float cur_cam_zoom = cam_zoom_orig;
01056
01057 double fpsexpave=0.0;
01058 double mapbuftotaltime=0.0;
01059
01060 double oldtime = wkf_timer_timenow(osp_timer);
01061 while (!done) {
01062 int winevent=0;
01063
01064 while ((winevent = glwin_handle_events(win, GLWIN_EV_POLL_NONBLOCK)) != 0) {
01065 int evdev, evval;
01066 char evkey;
01067
01068 glwin_get_lastevent(win, &evdev, &evval, &evkey);
01069 glwin_get_winsize(win, &wsx, &wsy);
01070
01071 if (evdev == GLWIN_EV_WINDOW_CLOSE) {
01072 printf("OSPRayRenderer) display window closed, exiting...\n");
01073 done = 1;
01074 winredraw = 0;
01075 } else if (evdev == GLWIN_EV_KBD) {
01076 switch (evkey) {
01077 case '1': autosamplecount=0; samples_per_pass=1; winredraw=1; break;
01078 case '2': autosamplecount=0; samples_per_pass=2; winredraw=1; break;
01079 case '3': autosamplecount=0; samples_per_pass=3; winredraw=1; break;
01080 case '4': autosamplecount=0; samples_per_pass=4; winredraw=1; break;
01081 case '5': autosamplecount=0; samples_per_pass=5; winredraw=1; break;
01082 case '6': autosamplecount=0; samples_per_pass=6; winredraw=1; break;
01083 case '7': autosamplecount=0; samples_per_pass=7; winredraw=1; break;
01084 case '8': autosamplecount=0; samples_per_pass=8; winredraw=1; break;
01085 case '9': autosamplecount=0; samples_per_pass=9; winredraw=1; break;
01086 case '0': autosamplecount=0; samples_per_pass=10; winredraw=1; break;
01087
01088 case '=':
01089 vec_copy(scene_gradient, scene_gradient_orig);
01090 cam_zoom = cam_zoom_orig;
01091 vec_copy(cam_pos, cam_pos_orig);
01092 vec_copy(cam_U, cam_U_orig);
01093 vec_copy(cam_V, cam_V_orig);
01094 vec_copy(cam_W, cam_W_orig);
01095
01096
01097 for (i=0; i<directional_lights.num(); i++) {
01098 vec_copy((float*)&cur_dlights[i].dir, directional_lights[i].dir);
01099 vec_normalize((float*)&cur_dlights[i].dir);
01100 }
01101 winredraw = 1;
01102 break;
01103
01104 case ' ':
01105 {
01106 char snapfilename[256];
01107 sprintf(snapfilename, "vmdsnapshot.%04d.tga", snapshotcount);
01108 const unsigned char *FB = (const unsigned char*)ospMapFrameBuffer(ospFrameBuffer, OSP_FB_COLOR);
01109 if (write_image_file_rgb4u(snapfilename, FB, width, height)) {
01110 printf("OSPRayRenderer) Failed to write output image!\n");
01111 } else {
01112 printf("OSPRayRenderer) Saved snapshot to '%s' \n",
01113 snapfilename);
01114 }
01115 ospUnmapFrameBuffer(FB, ospFrameBuffer);
01116 snapshotcount++;
01117 }
01118 break;
01119
01120 case 'a':
01121 autosamplecount = !(autosamplecount);
01122 printf("\nOSPRayRenderer) Automatic AO sample count FPS tuning %s\n",
01123 (autosamplecount) ? "enabled" : "disabled");
01124 break;
01125
01126 case 'f':
01127 mm = RTMM_DOF;
01128 printf("\nOSPRayRenderer) Mouse DoF aperture and focal dist. mode\n");
01129 break;
01130
01131 case 'g':
01132 xformgradientsphere = !(xformgradientsphere);
01133 printf("\nOSPRayRenderer) Gradient sky sphere transformations %s\n",
01134 (xformgradientsphere) ? "enabled" : "disabled");
01135 break;
01136
01137 case 'h':
01138 printf("\n");
01139 interactive_viewer_usage(win);
01140 break;
01141
01142 case 'l':
01143 xformlights = !(xformlights);
01144 printf("\nOSPRayRenderer) Light transformations %s\n",
01145 (xformlights) ? "enabled" : "disabled");
01146 break;
01147
01148 case 'p':
01149 printf("\nOSPRayRenderer) Current Ray Tracing Parameters:\n");
01150 printf("OSPRayRenderer) -------------------------------\n");
01151 printf("OSPRayRenderer) Camera zoom: %f\n", cur_cam_zoom);
01152 printf("OSPRayRenderer) Shadows: %s Ambient occlusion: %s\n",
01153 (gl_shadows_on) ? "on" : "off",
01154 (gl_ao_on) ? "on" : "off");
01155 printf("OSPRayRenderer) Antialiasing samples per-pass: %d\n",
01156 cur_aa_samples);
01157 printf("OSPRayRenderer) Ambient occlusion samples per-pass: %d\n",
01158 cur_ao_samples);
01159 printf("OSPRayRenderer) Depth-of-Field: %s f/num: %.1f Foc. Dist: %.2f\n",
01160 (gl_dof_on) ? "on" : "off",
01161 cam_dof_fnumber, cam_dof_focal_dist);
01162 printf("OSPRayRenderer) Image size: %d x %d\n", width, height);
01163 break;
01164
01165 case 'r':
01166 mm = RTMM_ROT;
01167 printf("\nOSPRayRenderer) Mouse rotation mode\n");
01168 break;
01169
01170 case 's':
01171 mm = RTMM_SCALE;
01172 printf("\nOSPRayRenderer) Mouse scaling mode\n");
01173 break;
01174
01175 case 'F':
01176 if (movie_recording_enabled) {
01177 switch (movie_recording_fps) {
01178 case 24: movie_recording_fps = 30; break;
01179 case 30: movie_recording_fps = 60; break;
01180 case 60:
01181 default: movie_recording_fps = 24; break;
01182 }
01183 printf("\nOSPRayRenderer) Movie recording FPS rate: %d\n",
01184 movie_recording_fps);
01185 } else {
01186 printf("\nOSPRayRenderer) Movie recording not available.\n");
01187 }
01188 break;
01189
01190 case 'R':
01191 if (movie_recording_enabled) {
01192 movie_recording_on = !(movie_recording_on);
01193 printf("\nOSPRayRenderer) Movie recording %s\n",
01194 (movie_recording_on) ? "STARTED" : "STOPPED");
01195 if (movie_recording_on) {
01196 movie_recording_start_time = wkf_timer_timenow(osp_timer);
01197 movie_framecount = 0;
01198 movie_lastframeindex = 0;
01199 } else {
01200 printf("OSPRayRenderer) Encode movie with:\n");
01201 printf("OSPRayRenderer) ffmpeg -f image2 -i vmdlivemovie.%%05d.tga -c:v libx264 -profile:v baseline -level 3.0 -pix_fmt yuv420p -b:v 15000000 output.mp4\n");
01202 }
01203 } else {
01204 printf("\nOSPRayRenderer) Movie recording not available.\n");
01205 }
01206 break;
01207
01208 case 'S':
01209 if (havestereo) {
01210 stereoon = (!stereoon);
01211 printf("\nOSPRayRenderer) Stereoscopic display %s\n",
01212 (stereoon) ? "enabled" : "disabled");
01213 winredraw = 1;
01214 } else {
01215 printf("\nOSPRayRenderer) Stereoscopic display unavailable\n");
01216 }
01217 break;
01218
01219 case 't':
01220 mm = RTMM_TRANS;
01221 printf("\nOSPRayRenderer) Mouse translation mode\n");
01222 break;
01223
01224 case 'q':
01225 case 'Q':
01226 case 0x1b:
01227 printf("\nOSPRayRenderer) Exiting on user input. \n");
01228 done=1;
01229 break;
01230 }
01231 } else if (evdev != GLWIN_EV_NONE) {
01232 switch (evdev) {
01233 case GLWIN_EV_KBD_F1:
01234 gl_shadows_on=(!gl_shadows_on) ? RT_SHADOWS_ON : RT_SHADOWS_OFF;
01235
01236 printf("\n");
01237 printf("OSPRayRenderer) Shadows %s\n",
01238 (gl_shadows_on) ? "enabled" : "disabled");
01239 winredraw = 1;
01240 break;
01241
01242 case GLWIN_EV_KBD_F2:
01243 gl_ao_on = (!gl_ao_on);
01244 printf("\n");
01245 printf("OSPRayRenderer) Ambient occlusion %s\n",
01246 (gl_ao_on) ? "enabled" : "disabled");
01247 winredraw = 1;
01248 break;
01249
01250 case GLWIN_EV_KBD_F3:
01251 gl_dof_on = (!gl_dof_on);
01252 printf("\n");
01253 if ((camera_projection == RT_ORTHOGRAPHIC) && gl_dof_on) {
01254 gl_dof_on=0;
01255 printf("OSPRayRenderer) Depth-of-field not available in orthographic mode\n");
01256 }
01257 printf("OSPRayRenderer) Depth-of-field %s\n",
01258 (gl_dof_on) ? "enabled" : "disabled");
01259 winredraw = 1;
01260 break;
01261
01262 case GLWIN_EV_KBD_F4:
01263 gl_fog_on = (!gl_fog_on);
01264 printf("\n");
01265 printf("OSPRayRenderer) Depth cueing %s\n",
01266 (gl_fog_on) ? "enabled" : "disabled");
01267 winredraw = 1;
01268 break;
01269
01270 case GLWIN_EV_KBD_F12:
01271 gl_fs_on = (!gl_fs_on);
01272 printf("\nOSPRayRenderer) Toggling fullscreen window %s\n",
01273 (gl_fs_on) ? "on" : "off");
01274 if (gl_fs_on) {
01275 if (glwin_fullscreen(win, gl_fs_on, 0) == 0) {
01276 owsx = wsx;
01277 owsy = wsy;
01278 glwin_get_winsize(win, &wsx, &wsy);
01279 } else {
01280 printf("OSPRayRenderer) Fullscreen mode note available\n");
01281 }
01282 } else {
01283 glwin_fullscreen(win, gl_fs_on, 0);
01284 glwin_resize(win, owsx, owsy);
01285 }
01286 winredraw = 1;
01287 break;
01288
01289 case GLWIN_EV_KBD_UP:
01290 cam_dof_focal_dist *= 1.02f;
01291 printf("\nOSPRayRenderer) DoF focal dist: %f\n", cam_dof_focal_dist);
01292 winredraw = 1;
01293 break;
01294
01295 case GLWIN_EV_KBD_DOWN:
01296 cam_dof_focal_dist *= 0.96f;
01297 if (cam_dof_focal_dist < 0.02f) cam_dof_focal_dist = 0.02f;
01298 printf("\nOSPRayRenderer) DoF focal dist: %f\n", cam_dof_focal_dist);
01299 winredraw = 1;
01300 break;
01301
01302 case GLWIN_EV_KBD_RIGHT:
01303 cam_dof_fnumber += 1.0f;
01304 printf("\nOSPRayRenderer) DoF f/stop: %f\n", cam_dof_fnumber);
01305 winredraw = 1;
01306 break;
01307
01308 case GLWIN_EV_KBD_LEFT:
01309 cam_dof_fnumber -= 1.0f;
01310 if (cam_dof_fnumber < 1.0f) cam_dof_fnumber = 1.0f;
01311 printf("\nOSPRayRenderer) DoF f/stop: %f\n", cam_dof_fnumber);
01312 winredraw = 1;
01313 break;
01314
01315 case GLWIN_EV_MOUSE_MOVE:
01316 if (mousedown != RTMD_NONE) {
01317 int x, y;
01318 glwin_get_mousepointer(win, &x, &y);
01319
01320 float zoommod = 2.0f*cur_cam_zoom/cam_zoom_orig;
01321 float txdx = (x - mousedownx) * zoommod / wsx;
01322 float txdy = (y - mousedowny) * zoommod / wsy;
01323 if (mm != RTMM_SCALE) {
01324 mousedownx = x;
01325 mousedowny = y;
01326 }
01327
01328 if (mm == RTMM_ROT) {
01329 Matrix4 rm;
01330 if (mousedown == RTMD_LEFT) {
01331
01332
01333 rm.rotate_axis(cam_V, -txdx);
01334 rm.rotate_axis(cam_U, -txdy);
01335 } else if (mousedown == RTMD_MIDDLE ||
01336 mousedown == RTMD_RIGHT) {
01337 rm.rotate_axis(cam_W, txdx);
01338 }
01339 rm.multpoint3d(cam_pos, cam_pos);
01340 rm.multnorm3d(cam_U, cam_U);
01341 rm.multnorm3d(cam_V, cam_V);
01342 rm.multnorm3d(cam_W, cam_W);
01343
01344 if (xformgradientsphere) {
01345 rm.multnorm3d(scene_gradient, scene_gradient);
01346 }
01347
01348 if (xformlights) {
01349
01350 for (i=0; i<directional_lights.num(); i++) {
01351 rm.multnorm3d((float*)&cur_dlights[i].dir, (float*)&cur_dlights[i].dir);
01352 }
01353 }
01354
01355 winredraw = 1;
01356 } else if (mm == RTMM_TRANS) {
01357 if (mousedown == RTMD_LEFT) {
01358 float dU[3], dV[3];
01359 vec_scale(dU, -txdx, cam_U);
01360 vec_scale(dV, txdy, cam_V);
01361 vec_add(cam_pos, cam_pos, dU);
01362 vec_add(cam_pos, cam_pos, dV);
01363 } else if (mousedown == RTMD_MIDDLE ||
01364 mousedown == RTMD_RIGHT) {
01365 float dW[3];
01366 vec_scale(dW, txdx, cam_W);
01367 vec_add(cam_pos, cam_pos, dW);
01368 }
01369 winredraw = 1;
01370 } else if (mm == RTMM_SCALE) {
01371 float txdx = (x - mousedownx) * 2.0f / wsx;
01372 float zoominc = 1.0f - txdx;
01373 if (zoominc < 0.01f) zoominc = 0.01f;
01374 cam_zoom = cur_cam_zoom * zoominc;
01375 winredraw = 1;
01376 } else if (mm == RTMM_DOF) {
01377 cam_dof_fnumber += txdx * 20.0f;
01378 if (cam_dof_fnumber < 1.0f) cam_dof_fnumber = 1.0f;
01379 cam_dof_focal_dist += -txdy;
01380 if (cam_dof_focal_dist < 0.01f) cam_dof_focal_dist = 0.01f;
01381 winredraw = 1;
01382 }
01383 }
01384 break;
01385
01386 case GLWIN_EV_MOUSE_LEFT:
01387 case GLWIN_EV_MOUSE_MIDDLE:
01388 case GLWIN_EV_MOUSE_RIGHT:
01389 if (evval) {
01390 glwin_get_mousepointer(win, &mousedownx, &mousedowny);
01391 cur_cam_zoom = cam_zoom;
01392
01393 if (evdev == GLWIN_EV_MOUSE_LEFT) mousedown = RTMD_LEFT;
01394 else if (evdev == GLWIN_EV_MOUSE_MIDDLE) mousedown = RTMD_MIDDLE;
01395 else if (evdev == GLWIN_EV_MOUSE_RIGHT) mousedown = RTMD_RIGHT;
01396 } else {
01397 mousedown = RTMD_NONE;
01398 }
01399 break;
01400
01401 case GLWIN_EV_MOUSE_WHEELUP:
01402 cam_zoom /= 1.1f; winredraw = 1; break;
01403
01404 case GLWIN_EV_MOUSE_WHEELDOWN:
01405 cam_zoom *= 1.1f; winredraw = 1; break;
01406 }
01407 }
01408 }
01409
01410
01411
01412
01413
01414
01415
01416
01417
01418
01419 if (spaceballenabled) {
01420
01421 int tx=0, ty=0, tz=0, rx=0, ry=0, rz=0, buttons=0;
01422 if (glwin_get_spaceball(win, &rx, &ry, &rz, &tx, &ty, &tz, &buttons)) {
01423
01424 if (spaceballflightmode) {
01425 rx= -rx;
01426 ry= -ry;
01427 rz= -rz;
01428
01429 tx= -tx;
01430 ty= -ty;
01431 tz= -tz;
01432 }
01433
01434
01435 if (buttons & 1) {
01436 printf("OSPRayRenderer) spaceball button 1 pressed: reset view\n");
01437 vec_copy(scene_gradient, scene_gradient_orig);
01438 cam_zoom = cam_zoom_orig;
01439 vec_copy(cam_pos, cam_pos_orig);
01440 vec_copy(cam_U, cam_U_orig);
01441 vec_copy(cam_V, cam_V_orig);
01442 vec_copy(cam_W, cam_W_orig);
01443
01444
01445 for (i=0; i<directional_lights.num(); i++) {
01446 vec_copy((float*)&cur_dlights[i].dir, directional_lights[i].dir);
01447 vec_normalize((float*)&cur_dlights[i].dir);
01448 }
01449 winredraw = 1;
01450 }
01451
01452
01453 if (buttons & 2) {
01454 spaceballmode = !(spaceballmode);
01455 printf("OSPRayRenderer) spaceball mode: %s \n",
01456 (spaceballmode) ? "scaling" : "rotation/translation");
01457 }
01458
01459
01460 if (spaceballmode == 0) {
01461 float zoommod = 2.0f*cam_zoom/cam_zoom_orig;
01462 float divlen = sqrtf(wsx*wsx + wsy*wsy) * 50.0f;
01463
01464
01465 if (rx != 0 || ry !=0 || rz !=0) {
01466 Matrix4 rm;
01467 rm.rotate_axis(cam_U, -rx * zoommod / divlen);
01468 rm.rotate_axis(cam_V, -ry * zoommod / divlen);
01469 rm.rotate_axis(cam_W, -rz * zoommod / divlen);
01470
01471 rm.multpoint3d(cam_pos, cam_pos);
01472 rm.multnorm3d(cam_U, cam_U);
01473 rm.multnorm3d(cam_V, cam_V);
01474 rm.multnorm3d(cam_W, cam_W);
01475
01476 if (xformgradientsphere) {
01477 rm.multnorm3d(scene_gradient, scene_gradient);
01478 }
01479
01480 if (xformlights) {
01481
01482 for (i=0; i<directional_lights.num(); i++) {
01483 rm.multnorm3d((float*)&cur_dlights[i].dir, (float*)&cur_dlights[i].dir);
01484 }
01485 }
01486 winredraw = 1;
01487 }
01488
01489
01490 if (tx != 0 || ty !=0 || tz !=0) {
01491 float dU[3], dV[3], dW[3];
01492 vec_scale(dU, -tx * zoommod / divlen, cam_U);
01493 vec_scale(dV, -ty * zoommod / divlen, cam_V);
01494 vec_scale(dW, -tz * zoommod / divlen, cam_W);
01495 vec_add(cam_pos, cam_pos, dU);
01496 vec_add(cam_pos, cam_pos, dV);
01497 vec_add(cam_pos, cam_pos, dW);
01498 winredraw = 1;
01499 }
01500 }
01501
01502
01503 if (spaceballmode == 1) {
01504 const float sbscale = 1.0f / (1024.0f * 8.0f);
01505 float zoominc = 1.0f - (rz * sbscale);
01506 if (zoominc < 0.01f) zoominc = 0.01f;
01507 cam_zoom *= zoominc;
01508 winredraw = 1;
01509 }
01510
01511 }
01512 }
01513
01514
01515
01516 vec_copy(hmd_U, cam_U);
01517 vec_copy(hmd_V, cam_V);
01518 vec_copy(hmd_W, cam_W);
01519
01520
01521
01522
01523
01524
01525
01526 int resize_buffers=0;
01527
01528 if (wsx != width) {
01529 width = wsx;
01530 resize_buffers=1;
01531 }
01532
01533 if (wsy != height || (stereoon != stereoon_old)) {
01534 if (stereoon) {
01535 if (height != wsy * 2) {
01536 height = wsy * 2;
01537 resize_buffers=1;
01538 }
01539 } else {
01540 height = wsy;
01541 resize_buffers=1;
01542 }
01543 }
01544
01545
01546
01547
01548 if ((stereoon != stereoon_old) || (gl_dof_on != gl_dof_on_old)) {
01549
01550
01551 if (stereoon != stereoon_old) {
01552 resize_buffers=1;
01553 }
01554
01555
01556 stereoon_old = stereoon;
01557 gl_dof_on_old = gl_dof_on;
01558
01559
01560
01561
01562 winredraw=1;
01563 }
01564
01565 if (resize_buffers) {
01566 framebuffer_resize(width, height);
01567
01568
01569
01570
01571 if (movie_recording_enabled) {
01572 printf("\rOSPRayRenderer) Window resize: %d x %d \n", width, height);
01573 }
01574
01575 winredraw=1;
01576 }
01577
01578 int frame_ready = 1;
01579 unsigned int subframe_count = 1;
01580 if (!done) {
01581
01582
01583
01584
01585
01586 if (winredraw) {
01587
01588 ospSet3fv(ospCamera, "pos", cam_pos);
01589 ospSet3fv(ospCamera, "dir", hmd_W);
01590 ospSet3fv(ospCamera, "up", hmd_V);
01591 ospSet1f(ospCamera, "aspect", width / ((float) height));
01592 ospSet1f(ospCamera, "fovy", 2.0f*180.0f*(atanf(cam_zoom)/float(VMD_PI)));
01593
01594
01595 ospSet1i(ospRenderer, "shadowsEnabled", gl_shadows_on);
01596
01597
01598 if (gl_shadows_on && gl_ao_on) {
01599 ospSet1i(ospRenderer, "aoSamples", 1);
01600 } else {
01601 ospSet1i(ospRenderer, "aoSamples", 0);
01602 }
01603
01604
01605
01606
01607
01608
01609
01610 if (camera_projection == OSPRayRenderer::RT_PERSPECTIVE) {
01611 if (gl_dof_on) {
01612 ospSet1f(ospCamera, "focusDistance", cam_dof_focal_dist);
01613 ospSet1f(ospCamera, "apertureRadius", cam_dof_focal_dist / (2.0f * cam_zoom * cam_dof_fnumber));
01614 } else {
01615 ospSet1f(ospCamera, "apertureRadius", 0.0f);
01616 }
01617 }
01618
01619
01620 ospCommit(ospCamera);
01621
01622
01623
01624
01625 if (xformlights) {
01626
01627
01628 float lightscale = 1.0f;
01629 if (ao_samples != 0)
01630 lightscale = ao_direct;
01631
01632
01633
01634
01635
01636 for (i=0; i<directional_lights.num(); i++) {
01637 ospSet1f(ospLights[i], "intensity", lightscale);
01638
01639 ospSet3fv(ospLights[i], "color", cur_dlights[i].color);
01640
01641 ospSet3f(ospLights[i], "direction",
01642 -cur_dlights[i].dir[0],
01643 -cur_dlights[i].dir[1],
01644 -cur_dlights[i].dir[2]);
01645
01646 ospCommit(ospLights[i]);
01647 }
01648 }
01649
01650
01651 ospCommit(ospRenderer);
01652
01653
01654 accum_count=0;
01655 totalsamplecount=0;
01656 if (ospFrameBuffer != NULL) {
01657 ospFrameBufferClear(ospFrameBuffer, OSP_FB_COLOR | OSP_FB_ACCUM);
01658 }
01659
01660
01661
01662
01663
01664
01665
01666
01667
01668 if (autosamplecount) {
01669 if (fpsexpave > 37)
01670 samples_per_pass++;
01671 else if (fpsexpave < 30)
01672 samples_per_pass--;
01673
01674
01675 if (samples_per_pass > 14)
01676 samples_per_pass=14;
01677 if (samples_per_pass < 1)
01678 samples_per_pass=1;
01679 }
01680
01681
01682
01683 if (gl_shadows_on && gl_ao_on) {
01684 if (gl_dof_on) {
01685 if (samples_per_pass < 4) {
01686 cur_aa_samples=samples_per_pass;
01687 cur_ao_samples=1;
01688 } else {
01689 int s = (int) sqrtf(samples_per_pass);
01690 cur_aa_samples=s;
01691 cur_ao_samples=s;
01692 }
01693 } else {
01694 cur_aa_samples=1;
01695 cur_ao_samples=samples_per_pass;
01696 }
01697 } else {
01698 cur_aa_samples=samples_per_pass;
01699 cur_ao_samples=0;
01700 }
01701
01702
01703
01704
01705
01706
01707 if (gl_shadows_on && gl_ao_on) {
01708
01709 } else {
01710 cur_ao_samples = 0;
01711
01712 }
01713 }
01714
01715
01716
01717
01718
01719
01720
01721
01722
01723
01724
01725
01726
01727
01728
01729
01730
01731 frame_ready = 1;
01732 subframe_count = 1;
01733 if (lasterror == 0 ) {
01734 if (winredraw) {
01735 ospFrameBufferClear(ospFrameBuffer, OSP_FB_COLOR | OSP_FB_ACCUM);
01736 winredraw=0;
01737 }
01738
01739
01740
01741 ospRenderFrame(ospFrameBuffer, ospRenderer, OSP_FB_COLOR | OSP_FB_ACCUM);
01742 subframe_count++;
01743 totalsamplecount += samples_per_pass;
01744 accum_count += cur_aa_samples;
01745
01746
01747
01748
01749
01750 if (lasterror == 0 ) {
01751 if (frame_ready) {
01752 double bufnewtime = wkf_timer_timenow(osp_timer);
01753
01754
01755 const unsigned char * img;
01756 img = (const unsigned char*)ospMapFrameBuffer(ospFrameBuffer, OSP_FB_COLOR);
01757
01758 #if 0
01759 glwin_draw_image_tex_rgb3u(win, (stereoon!=0)*GLWIN_STEREO_OVERUNDER, width, height, img);
01760 #else
01761 glwin_draw_image_rgb3u(win, (stereoon!=0)*GLWIN_STEREO_OVERUNDER, width, height, img);
01762 #endif
01763 ospUnmapFrameBuffer(img, ospFrameBuffer);
01764 mapbuftotaltime = wkf_timer_timenow(osp_timer) - bufnewtime;
01765
01766
01767
01768
01769
01770
01771 if (movie_recording_enabled && movie_recording_on) {
01772 char moviefilename[2048];
01773
01774
01775
01776 double now = wkf_timer_timenow(osp_timer);
01777 double frametime = now - movie_recording_start_time;
01778 int fidx = frametime * movie_recording_fps;
01779
01780
01781 if (movie_framecount==0)
01782 fidx=0;
01783 movie_framecount++;
01784
01785 #if defined(__linux)
01786
01787
01788
01789
01790 sprintf(moviefilename, movie_recording_filebase,
01791 movie_lastframeindex);
01792 int symidx;
01793 for (symidx=movie_lastframeindex; symidx<fidx; symidx++) {
01794 char symlinkfilename[2048];
01795 sprintf(symlinkfilename, movie_recording_filebase, symidx);
01796 symlink(moviefilename, symlinkfilename);
01797 }
01798 #endif
01799
01800
01801 sprintf(moviefilename, movie_recording_filebase, fidx);
01802 const unsigned char *FB = (const unsigned char*)ospMapFrameBuffer(ospFrameBuffer, OSP_FB_COLOR);
01803 if (write_image_file_rgb4u(moviefilename, FB, width, height)) {
01804 movie_recording_on = 0;
01805 printf("\n");
01806 printf("OSPRayRenderer) ERROR during writing image during movie recording!\n");
01807 printf("OSPRayRenderer) Movie recording STOPPED\n");
01808 }
01809 ospUnmapFrameBuffer(FB, ospFrameBuffer);
01810
01811 movie_lastframeindex = fidx;
01812 }
01813 }
01814 } else {
01815 printf("OSPRayRenderer) An error occured during rendering. Rendering is aborted.\n");
01816 done=1;
01817 break;
01818 }
01819 } else {
01820 printf("OSPRayRenderer) An error occured in AS generation. Rendering is aborted.\n");
01821 done=1;
01822 break;
01823 }
01824 }
01825
01826 if (!done && frame_ready) {
01827 double newtime = wkf_timer_timenow(osp_timer);
01828 double frametime = (newtime-oldtime) + 0.00001f;
01829 oldtime=newtime;
01830
01831
01832 double framefps = 1.0f/frametime;
01833 fpsexpave = (fpsexpave * 0.90) + (framefps * 0.10);
01834
01835 printf("OSPRayRenderer) %c AA:%2d AO:%2d, %4d tot RT FPS: %.1f %.4f s/frame sf: %d \r",
01836 statestr[state], cur_aa_samples, cur_ao_samples,
01837 totalsamplecount, fpsexpave, frametime, subframe_count);
01838
01839 fflush(stdout);
01840 state = (state+1) & 3;
01841 }
01842
01843 }
01844
01845 printf("\n");
01846
01847
01848 if (lasterror == 0 ) {
01849 wkf_timer_start(osp_timer);
01850
01851 const unsigned char *FB = (const unsigned char*)ospMapFrameBuffer(ospFrameBuffer, OSP_FB_COLOR);
01852 if (write_image_file_rgb4u(filename, FB, width, height)) {
01853 printf("OSPRayRenderer) Failed to write output image!\n");
01854 }
01855 ospUnmapFrameBuffer(FB, ospFrameBuffer);
01856 wkf_timer_stop(osp_timer);
01857
01858 if (verbose == RT_VERB_TIMING || verbose == RT_VERB_DEBUG) {
01859 printf("OSPRayRenderer) image file I/O time: %f secs\n", wkf_timer_time(osp_timer));
01860 }
01861 }
01862
01863 glwin_destroy(win);
01864 }
01865
01866 #endif
01867
01868
01869 void OSPRayRenderer::render_to_file(const char *filename) {
01870 DBG();
01871 if (!context_created)
01872 return;
01873
01874
01875
01876 int wsx=width, wsy=height;
01877 const char *imageszstr = getenv("VMDOSPRAYIMAGESIZE");
01878 if (imageszstr) {
01879 if (sscanf(imageszstr, "%d %d", &width, &height) != 2) {
01880 width=wsx;
01881 height=wsy;
01882 }
01883 }
01884
01885
01886 framebuffer_config(width, height);
01887
01888 update_rendering_state(0);
01889 render_compile_and_validate();
01890 double starttime = wkf_timer_timenow(osp_timer);
01891
01892
01893
01894
01895 if (lasterror == 0 ) {
01896
01897 ospFrameBufferClear(ospFrameBuffer, OSP_FB_COLOR | OSP_FB_ACCUM);
01898
01899
01900 if (getenv("VMDOSPRAYNORENDER") == NULL) {
01901 int accum_sample;
01902 for (accum_sample=0; accum_sample<ext_aa_loops; accum_sample++) {
01903
01904
01905 ospRenderFrame(ospFrameBuffer, ospRenderer, OSP_FB_COLOR | OSP_FB_ACCUM);
01906 }
01907 }
01908
01909
01910
01911 double rtendtime = wkf_timer_timenow(osp_timer);
01912 time_ray_tracing = rtendtime - starttime;
01913
01914 if (lasterror == 0 ) {
01915
01916 if (getenv("VMDOSPRAYNOSAVE") == NULL) {
01917 const unsigned char *FB = (const unsigned char*)ospMapFrameBuffer(ospFrameBuffer, OSP_FB_COLOR);
01918 if (write_image_file_rgb4u(filename, FB, width, height)) {
01919 printf("OSPRayRenderer) Failed to write output image!\n");
01920 }
01921 ospUnmapFrameBuffer(FB, ospFrameBuffer);
01922 }
01923 time_image_io = wkf_timer_timenow(osp_timer) - rtendtime;
01924 } else {
01925 printf("OSPRayRenderer) Error during rendering. Rendering aborted.\n");
01926 }
01927
01928 if (verbose == RT_VERB_TIMING || verbose == RT_VERB_DEBUG) {
01929 printf("OSPRayRenderer) ctx setup %.2f valid %.2f AS %.2f RT %.2f io %.2f\n", time_ctx_setup, time_ctx_validate, time_ctx_AS_build, time_ray_tracing, time_image_io);
01930 }
01931 } else {
01932 printf("OSPRayRenderer) Error during AS generation. Rendering aborted.\n");
01933 }
01934 }
01935
01936
01937 void OSPRayRenderer::add_material(int matindex,
01938 float ambient, float diffuse, float specular,
01939 float shininess, float reflectivity,
01940 float opacity,
01941 float outline, float outlinewidth,
01942 int transmode) {
01943 int oldmatcount = materialcache.num();
01944 if (oldmatcount <= matindex) {
01945 osp_material m;
01946 memset(&m, 0, sizeof(m));
01947
01948
01949 m.ambient = 0.5f;
01950 m.diffuse = 0.7f;
01951 m.specular = 0.0f;
01952 m.shininess = 10.0f;
01953 m.reflectivity = 0.0f;
01954 m.opacity = 1.0f;
01955 m.transmode = 0;
01956
01957 materialcache.appendN(m, matindex - oldmatcount + 1);
01958 }
01959
01960 if (materialcache[matindex].isvalid) {
01961 return;
01962 } else {
01963 if (verbose == RT_VERB_DEBUG) printf("OSPRayRenderer) Adding material[%d]\n", matindex);
01964
01965 materialcache[matindex].ambient = ambient;
01966 materialcache[matindex].diffuse = diffuse;
01967 materialcache[matindex].specular = specular;
01968 materialcache[matindex].shininess = shininess;
01969 materialcache[matindex].reflectivity = reflectivity;
01970 materialcache[matindex].opacity = opacity;
01971 materialcache[matindex].outline = outline;
01972 materialcache[matindex].outlinewidth = outlinewidth;
01973 materialcache[matindex].transmode = transmode;
01974
01975
01976 #if OSPRAY_VERSION_MAJOR >= 1 && OSPRAY_VERSION_MINOR >= 5
01977 OSPMaterial ospMat = ospNewMaterial2("scivis", "RaytraceMaterial");
01978 #else
01979 OSPMaterial ospMat = ospNewMaterial(ospRenderer, "RaytraceMaterial");
01980 #endif
01981 ospSet3f(ospMat, "Ka", materialcache[matindex].ambient, materialcache[matindex].ambient, materialcache[matindex].ambient);
01982 ospSet3f(ospMat, "Kd", materialcache[matindex].diffuse, materialcache[matindex].diffuse, materialcache[matindex].diffuse);
01983 ospSet3f(ospMat, "Ks", materialcache[matindex].specular, materialcache[matindex].specular, materialcache[matindex].specular);
01984 ospSet1f(ospMat, "d", materialcache[matindex].opacity);
01985 ospSet1f(ospMat, "Ns", materialcache[matindex].shininess);
01986
01991
01992 ospCommit(ospMat);
01993 materialcache[matindex].mat = ospMat;
01994
01995 materialcache[matindex].isvalid = 1;
01996 }
01997 }
01998
01999
02000 void OSPRayRenderer::init_materials() {
02001 DBG();
02002 if (verbose == RT_VERB_DEBUG) printf("OSPRayRenderer: init_materials()\n");
02003
02004 }
02005
02006
02007 void OSPRayRenderer::set_material(OSPGeometry &geom, int matindex, float *uniform_color) {
02008 if (!context_created)
02009 return;
02010
02011 if (verbose == RT_VERB_DEBUG) printf("OSPRay: setting material\n");
02012 ospSetMaterial(geom, materialcache[matindex].mat);
02013 }
02014
02015
02016 void OSPRayRenderer::add_directional_light(const float *dir, const float *color) {
02017 DBG();
02018 osp_directional_light l;
02019 vec_copy(l.dir, dir);
02020 vec_copy(l.color, color);
02021
02022 directional_lights.append(l);
02023 }
02024
02025
02026 void OSPRayRenderer::add_positional_light(const float *pos, const float *color) {
02027 DBG();
02028 osp_positional_light l;
02029 vec_copy(l.pos, pos);
02030 vec_copy(l.color, color);
02031
02032 positional_lights.append(l);
02033 }
02034
02035
02036 void OSPRayRenderer::cylinder_array(Matrix4 *wtrans, float radius,
02037 float *uniform_color,
02038 int cylnum, float *points, int matindex) {
02039 DBG();
02040 if (!context_created) return;
02041 if (verbose == RT_VERB_DEBUG) printf("OSPRayRenderer) creating cylinder array: %d...\n", cylnum);
02042 cylinder_array_cnt += cylnum;
02043
02044 osp_cylinder_array_color ca;
02045 memset(&ca, 0, sizeof(ca));
02046 ca.num = cylnum;
02047
02048
02049
02050 int bytes_per_cylinder = 3*sizeof(float) + 3*sizeof(float) + sizeof(float);
02051 ca.cylinders = (float *) calloc(1, cylnum * bytes_per_cylinder);
02052 ca.colors = (float *) calloc(1, cylnum * 4 * sizeof(float));
02053
02054 int i,ind4,ind6,ind7;
02055 const int rOffset = 6;
02056 if (wtrans == NULL) {
02057 for (i=0,ind4=0,ind6=0,ind7=0; i<cylnum; i++,ind4+=4,ind6+=6,ind7+=7) {
02058 vec_copy(&ca.cylinders[ind7 ], &points[ind6 ]);
02059 vec_copy(&ca.cylinders[ind7+3], &points[ind6+3]);
02060 ca.cylinders[ind7+rOffset] = radius;
02061 vec_copy(&ca.colors[ind4], &uniform_color[0]);
02062 ca.colors[ind4 + 3] = 1.0f;
02063 }
02064 } else {
02065 for (i=0,ind4=0,ind6=0,ind7=0; i<cylnum; i++,ind4+=4,ind6+=6,ind7+=7) {
02066
02067 wtrans->multpoint3d(&points[ind6 ], &ca.cylinders[ind7 ]);
02068 wtrans->multpoint3d(&points[ind6+3], &ca.cylinders[ind7+3]);
02069 ca.cylinders[ind7+rOffset] = radius;
02070 vec_copy(&ca.colors[ind4], &uniform_color[0]);
02071 ca.colors[ind4 + 3] = 1.0f;
02072 }
02073 }
02074
02075 ca.matindex = matindex;
02076 ca.geom = ospNewGeometry("cylinders");
02077 ca.cyls = ospNewData(cylnum*bytes_per_cylinder, OSP_CHAR, ca.cylinders, 0);
02078 ca.cols = ospNewData(cylnum, OSP_FLOAT4, ca.colors, 0);
02079 ospSetData(ca.geom, "cylinders", ca.cyls);
02080 ospSet1i(ca.geom, "bytes_per_cylinder", bytes_per_cylinder);
02081 ospSet1i(ca.geom, "offset_v0", 0);
02082 ospSet1i(ca.geom, "offset_v1", 3*sizeof(float));
02083 ospSet1i(ca.geom, "offset_radius", rOffset*sizeof(float));
02084 ospSetData(ca.geom, "color", ca.cols);
02085 set_material(ca.geom, matindex, NULL);
02086 ospCommit(ca.geom);
02087 ospRelease(ca.cyls);
02088 ospRelease(ca.cols);
02089
02090 free(ca.cylinders);
02091 ca.cylinders = NULL;
02092 free(ca.colors);
02093 ca.colors = NULL;
02094
02095 cylinders_color.append(ca);
02096 }
02097
02098
02099 void OSPRayRenderer::cylinder_array_color(Matrix4 & wtrans, float rscale,
02100 int cylnum, float *points,
02101 float *radii, float *colors,
02102 int matindex) {
02103 DBG();
02104 if (!context_created) return;
02105 if (verbose == RT_VERB_DEBUG) printf("OSPRayRenderer) creating color cylinder array: %d...\n", cylnum);
02106 cylinder_array_color_cnt += cylnum;
02107
02108 osp_cylinder_array_color cac;
02109 memset(&cac, 0, sizeof(cac));
02110 cac.num = cylnum;
02111
02112
02113
02114 int bytes_per_cylinder = 3*sizeof(float) + 3*sizeof(float) + sizeof(float);
02115 cac.cylinders = (float *) calloc(1, cylnum * bytes_per_cylinder);
02116 cac.colors = (float *) calloc(1, cylnum * 4 * sizeof(float));
02117
02118 int i, ind3, ind4, ind6, ind7;
02119 const int rOffset = 6;
02120 for (i=0,ind3=0,ind4=0,ind6=0,ind7=0; i<cylnum; i++,ind3+=3,ind4+=4,ind6+=6,ind7+=7) {
02121
02122 wtrans.multpoint3d(&points[ind6 ], &cac.cylinders[ind7 ]);
02123 wtrans.multpoint3d(&points[ind6+3], &cac.cylinders[ind7+3]);
02124 cac.cylinders[ind7+rOffset] = radii[i] * rscale;
02125 vec_copy(&cac.colors[ind4], &colors[ind3]);
02126 cac.colors[ind4 + 3] = 1.0f;
02127 }
02128
02129 cac.matindex = matindex;
02130 cac.geom = ospNewGeometry("cylinders");
02131 cac.cyls = ospNewData(cylnum*bytes_per_cylinder, OSP_CHAR, cac.cylinders, 0);
02132 cac.cols = ospNewData(cylnum, OSP_FLOAT4, cac.colors, 0);
02133 ospSetData(cac.geom, "cylinders", cac.cyls);
02134 ospSet1i(cac.geom, "bytes_per_cylinder", bytes_per_cylinder);
02135 ospSet1i(cac.geom, "offset_v0", 0);
02136 ospSet1i(cac.geom, "offset_v1", 3*sizeof(float));
02137 ospSet1i(cac.geom, "offset_radius", rOffset*sizeof(float));
02138 ospSetData(cac.geom, "color", cac.cols);
02139 set_material(cac.geom, matindex, NULL);
02140 ospCommit(cac.geom);
02141 ospRelease(cac.cyls);
02142 ospRelease(cac.cols);
02143
02144 free(cac.cylinders);
02145 cac.cylinders = NULL;
02146 free(cac.colors);
02147 cac.colors = NULL;
02148
02149 cylinders_color.append(cac);
02150 }
02151
02152 #if 0
02153 void OSPRayRenderer::ring_array_color(Matrix4 & wtrans, float rscale,
02154 int rnum, float *centers,
02155 float *norms, float *radii,
02156 float *colors, int matindex) {
02157 }
02158 #endif
02159
02160
02161 void OSPRayRenderer::sphere_array(Matrix4 *wtrans, float rscale,
02162 float *uniform_color,
02163 int numsp, float *centers,
02164 float *radii,
02165 int matindex) {
02166 DBG();
02167 if (!context_created) return;
02168 if (verbose == RT_VERB_DEBUG) printf("OSPRayRenderer) creating sphere array: %d...\n", numsp);
02169 sphere_array_cnt += numsp;
02170
02171 const rgba c = { uniform_color[0], uniform_color[1], uniform_color[2], 1.0f};
02172
02173 osp_sphere_array_color sp;
02174 memset(&sp, 0, sizeof(sp));
02175 sp.num = numsp;
02176 sp.xyzr = (float *) calloc(1, numsp * 4*sizeof(float));
02177 sp.colors = (float *) calloc(1, numsp * 4*sizeof(float));
02178
02179 int i, ind3, ind4;
02180 if (wtrans == NULL) {
02181 if (radii == NULL) {
02182 for (i=0,ind3=0,ind4=0; i<numsp; i++,ind3+=3,ind4+=4) {
02183
02184 vec_copy((float*) &sp.xyzr[ind4], ¢ers[ind3]);
02185 sp.xyzr[ind4+3] = rscale;
02186 memcpy((float*) &sp.colors[ind4], &c, 4*sizeof(float));
02187 }
02188 } else {
02189 for (i=0,ind3=0,ind4=0; i<numsp; i++,ind3+=3,ind4+=4) {
02190
02191 vec_copy((float*) &sp.xyzr[ind4], ¢ers[ind3]);
02192 sp.xyzr[ind4+3] = radii[i] * rscale;
02193 memcpy((float*) &sp.colors[ind4], &c, 4*sizeof(float));
02194 }
02195 }
02196 } else {
02197 if (radii == NULL) {
02198 for (i=0,ind3=0,ind4=0; i<numsp; i++,ind3+=3,ind4+=4) {
02199 wtrans->multpoint3d(¢ers[ind3], &sp.xyzr[ind4]);
02200 sp.xyzr[ind4+3] = rscale;
02201 memcpy((float*) &sp.colors[ind4], &c, 4*sizeof(float));
02202 }
02203 } else {
02204 for (i=0,ind3=0,ind4=0; i<numsp; i++,ind3+=3,ind4+=4) {
02205
02206 wtrans->multpoint3d(¢ers[ind3], &sp.xyzr[ind4]);
02207 sp.xyzr[ind4+3] = radii[i] * rscale;
02208 memcpy((float*) &sp.colors[ind4], &c, 4*sizeof(float));
02209 }
02210 }
02211 }
02212
02213 sp.matindex = matindex;
02214 sp.geom = ospNewGeometry("spheres");
02215 sp.cents = ospNewData(numsp, OSP_FLOAT4, sp.xyzr, 0);
02216 sp.cols = ospNewData(numsp, OSP_FLOAT4, sp.colors, 0);
02217 ospSetData(sp.geom, "spheres", sp.cents);
02218 ospSet1i(sp.geom, "bytes_per_sphere", 4*sizeof(float));
02219 ospSet1i(sp.geom, "offset_center", 0);
02220 ospSet1i(sp.geom, "offset_radius", 3*sizeof(float));
02221 ospSetData(sp.geom, "color", sp.cols);
02222 set_material(sp.geom, matindex, NULL);
02223 ospCommit(sp.geom);
02224 ospRelease(sp.cents);
02225 ospRelease(sp.cols);
02226
02227 free(sp.xyzr);
02228 sp.xyzr = NULL;
02229 free(sp.colors);
02230 sp.colors = NULL;
02231
02232 spheres_color.append(sp);
02233 }
02234
02235
02236 void OSPRayRenderer::sphere_array_color(Matrix4 & wtrans, float rscale,
02237 int numsp, float *centers,
02238 float *radii, float *colors,
02239 int matindex) {
02240 DBG();
02241 if (!context_created) return;
02242 if (verbose == RT_VERB_DEBUG) printf("OSPRayRenderer) creating sphere array color: %d...\n", numsp);
02243 sphere_array_color_cnt += numsp;
02244
02245 osp_sphere_array_color sp;
02246 memset(&sp, 0, sizeof(sp));
02247 sp.num = numsp;
02248 sp.xyzr = (float *) calloc(1, numsp * 4*sizeof(float));
02249 sp.colors = (float *) calloc(1, numsp * 4*sizeof(float));
02250
02251 int i, ind3, ind4;
02252 for (i=0,ind3=0,ind4=0; i<numsp; i++,ind3+=3,ind4+=4) {
02253 wtrans.multpoint3d(¢ers[ind3], &sp.xyzr[ind4]);
02254 sp.xyzr[ind4+3] = radii[i] * rscale;
02255 vec_copy((float*) &sp.colors[ind4], &colors[ind3]);
02256 sp.colors[ind4 + 3] = 1.0f;
02257 }
02258
02259 sp.matindex = matindex;
02260 sp.geom = ospNewGeometry("spheres");
02261 sp.cents = ospNewData(numsp, OSP_FLOAT4, sp.xyzr, 0);
02262 sp.cols = ospNewData(numsp, OSP_FLOAT4, sp.colors, 0);
02263 ospSetData(sp.geom, "spheres", sp.cents);
02264 ospSet1i(sp.geom, "bytes_per_sphere", 4*sizeof(float));
02265 ospSet1i(sp.geom, "offset_center", 0);
02266 ospSet1i(sp.geom, "offset_radius", 3*sizeof(float));
02267 ospSetData(sp.geom, "color", sp.cols);
02268 set_material(sp.geom, matindex, NULL);
02269 ospCommit(sp.geom);
02270 ospRelease(sp.cents);
02271 ospRelease(sp.cols);
02272
02273 free(sp.xyzr);
02274 sp.xyzr = NULL;
02275 free(sp.colors);
02276 sp.colors = NULL;
02277
02278 spheres_color.append(sp);
02279 }
02280
02281
02282 void OSPRayRenderer::append_trimesh_v3f_n3f_c3f(osp_trimesh_v3f_n3f_c3f &mesh) {
02283 mesh.geom = ospNewGeometry("triangles");
02284 mesh.verts = ospNewData(mesh.numverts, OSP_FLOAT3, mesh.v, 0);
02285 mesh.norms = ospNewData(mesh.numverts, OSP_FLOAT3, mesh.n, 0);
02286 mesh.cols = ospNewData(mesh.numverts, OSP_FLOAT4, mesh.c, 0);
02287 mesh.ind = ospNewData(mesh.numtris, OSP_INT3, mesh.f, 0);
02288
02289 ospSetData(mesh.geom, "vertex", mesh.verts);
02290 ospSetData(mesh.geom, "vertex.normal", mesh.norms);
02291 ospSetData(mesh.geom, "index", mesh.ind);
02292 ospSetData(mesh.geom, "vertex.color", mesh.cols);
02293 set_material(mesh.geom, mesh.matindex, NULL);
02294
02295 ospCommit(mesh.geom);
02296 ospRelease(mesh.verts);
02297 ospRelease(mesh.norms);
02298 ospRelease(mesh.cols);
02299 ospRelease(mesh.ind);
02300
02301 free(mesh.v);
02302 mesh.v = NULL;
02303 free(mesh.n);
02304 mesh.n = NULL;
02305 free(mesh.c);
02306 mesh.c = NULL;
02307 free(mesh.f);
02308 mesh.f = NULL;
02309
02310 trimesh_v3f_n3f_c3f.append(mesh);
02311 }
02312
02313
02314
02315 void OSPRayRenderer::tricolor_list(Matrix4 & wtrans, int numtris, float *vnc,
02316 int matindex) {
02317 if (!context_created) return;
02318
02319 tricolor_cnt += numtris;
02320
02321
02322 osp_trimesh_v3f_n3f_c3f mesh;
02323 memset(&mesh, 0, sizeof(mesh));
02324 mesh.numtris = numtris;
02325 mesh.numverts = 3L * numtris;
02326 mesh.matindex = matindex;
02327 mesh.v = (float *) calloc(1, numtris * 9*sizeof(float));
02328 mesh.n = (float *) calloc(1, numtris * 9*sizeof(float));
02329 mesh.c = (float *) calloc(1, numtris * 12*sizeof(float));
02330 mesh.f = (int *) calloc(1, numtris * 3*sizeof(int));
02331
02332 float alpha = 1.0f;
02333
02334
02335 int i, ind, ind9, ind12;
02336 for (i=0,ind=0,ind9=0,ind12=0; i<numtris; i++,ind+=27,ind9+=9,ind12+=12) {
02337
02338 wtrans.multpoint3d(&vnc[ind ], (float*) &mesh.v[ind9 ]);
02339 wtrans.multpoint3d(&vnc[ind + 3], (float*) &mesh.v[ind9 + 3]);
02340 wtrans.multpoint3d(&vnc[ind + 6], (float*) &mesh.v[ind9 + 6]);
02341
02342 wtrans.multnorm3d(&vnc[ind + 9], (float*) &mesh.n[ind9 ]);
02343 wtrans.multnorm3d(&vnc[ind + 12], (float*) &mesh.n[ind9 + 3]);
02344 wtrans.multnorm3d(&vnc[ind + 15], (float*) &mesh.n[ind9 + 6]);
02345
02346 vec_copy(&mesh.c[ind12 ], &vnc[ind + 18]);
02347 mesh.c[ind12 + 3] = alpha;
02348 vec_copy(&mesh.c[ind12 + 4], &vnc[ind + 21]);
02349 mesh.c[ind12 + 7] = alpha;
02350 vec_copy(&mesh.c[ind12 + 8], &vnc[ind + 24]);
02351 mesh.c[ind12 + 11] = alpha;
02352
02353 mesh.f[i*3 ] = i*3;
02354 mesh.f[i*3+1] = i*3 + 1;
02355 mesh.f[i*3+2] = i*3 + 2;
02356 }
02357
02358 append_trimesh_v3f_n3f_c3f(mesh);
02359 }
02360
02361
02362 void OSPRayRenderer::trimesh_c4n3v3(Matrix4 & wtrans, int numverts,
02363 float *cnv, int numfacets, int * facets,
02364 int matindex) {
02365 if (!context_created) return;
02366 if (verbose == RT_VERB_DEBUG) printf("OSPRayRenderer) creating trimesh_c4n3v3: %d...\n", numfacets);
02367 trimesh_c4u_n3b_v3f_cnt += numfacets;
02368
02369
02370 osp_trimesh_v3f_n3f_c3f mesh;
02371 memset(&mesh, 0, sizeof(mesh));
02372 mesh.numtris = numfacets;
02373 mesh.numverts = 3L * numfacets;
02374 mesh.matindex = matindex;
02375 mesh.v = (float *) calloc(1, numfacets * 9*sizeof(float));
02376 mesh.n = (float *) calloc(1, numfacets * 9*sizeof(float));
02377 mesh.c = (float *) calloc(1, numfacets * 12*sizeof(float));
02378 mesh.f = (int *) calloc(1, numfacets * 3*sizeof(int));
02379
02380 float alpha = 1.0f;
02381
02382
02383
02384
02385 int i, ind, ind9, ind12;
02386 for (i=0,ind=0,ind9=0,ind12=0; i<numfacets; i++,ind+=3,ind9+=9,ind12+=12) {
02387 int v0 = facets[ind ] * 10;
02388 int v1 = facets[ind + 1] * 10;
02389 int v2 = facets[ind + 2] * 10;
02390
02391
02392 wtrans.multpoint3d(cnv + v0 + 7, (float*) &mesh.v[ind9 ]);
02393 wtrans.multpoint3d(cnv + v1 + 7, (float*) &mesh.v[ind9 + 3]);
02394 wtrans.multpoint3d(cnv + v2 + 7, (float*) &mesh.v[ind9 + 6]);
02395
02396 wtrans.multnorm3d(cnv + v0 + 4, (float*) &mesh.n[ind9 ]);
02397 wtrans.multnorm3d(cnv + v1 + 4, (float*) &mesh.n[ind9 + 3]);
02398 wtrans.multnorm3d(cnv + v2 + 4, (float*) &mesh.n[ind9 + 6]);
02399
02400 vec_copy(&mesh.c[ind12 ], cnv + v0);
02401 mesh.c[ind12 + 3] = alpha;
02402 vec_copy(&mesh.c[ind12 + 4], cnv + v1);
02403 mesh.c[ind12 + 7] = alpha;
02404 vec_copy(&mesh.c[ind12 + 8], cnv + v2);
02405 mesh.c[ind12 + 11] = alpha;
02406
02407 mesh.f[i*3 ] = i*3;
02408 mesh.f[i*3+1] = i*3 + 1;
02409 mesh.f[i*3+2] = i*3 + 2;
02410 }
02411
02412 append_trimesh_v3f_n3f_c3f(mesh);
02413 }
02414
02415
02416
02417
02418
02419
02420
02421 void OSPRayRenderer::trimesh_c4u_n3b_v3f(Matrix4 & wtrans, unsigned char *c,
02422 signed char *n, float *v,
02423 int numfacets, int matindex) {
02424 if (!context_created) return;
02425 if (verbose == RT_VERB_DEBUG) printf("OSPRayRenderer) creating trimesh_c4u_n3b_v3f: %d...\n", numfacets);
02426 trimesh_n3b_v3f_cnt += numfacets;
02427
02428
02429 osp_trimesh_v3f_n3f_c3f mesh;
02430 memset(&mesh, 0, sizeof(mesh));
02431 mesh.numtris = numfacets;
02432 mesh.numverts = 3L * numfacets;
02433 mesh.matindex = matindex;
02434 mesh.v = (float *) calloc(1, numfacets * 9*sizeof(float));
02435 mesh.n = (float *) calloc(1, numfacets * 9*sizeof(float));
02436 mesh.c = (float *) calloc(1, numfacets * 12*sizeof(float));
02437 mesh.f = (int *) calloc(1, numfacets * 3*sizeof(int));
02438
02439 float alpha = 1.0f;
02440
02441
02442
02443
02444 int i, ind, ind9, ind12;
02445 const float ci2f = 1.0f / 255.0f;
02446 const float cn2f = 1.0f / 127.5f;
02447 for (i=0,ind=0,ind9=0,ind12=0; i<numfacets; i++,ind+=3,ind9+=9,ind12+=12) {
02448 float norm[9];
02449
02450
02451
02452 norm[0] = n[ind9 ] * cn2f + ci2f;
02453 norm[1] = n[ind9 + 1] * cn2f + ci2f;
02454 norm[2] = n[ind9 + 2] * cn2f + ci2f;
02455 norm[3] = n[ind9 + 3] * cn2f + ci2f;
02456 norm[4] = n[ind9 + 4] * cn2f + ci2f;
02457 norm[5] = n[ind9 + 5] * cn2f + ci2f;
02458 norm[6] = n[ind9 + 6] * cn2f + ci2f;
02459 norm[7] = n[ind9 + 7] * cn2f + ci2f;
02460 norm[8] = n[ind9 + 8] * cn2f + ci2f;
02461
02462
02463 wtrans.multpoint3d(v + ind9 , (float*) &mesh.v[ind9 ]);
02464 wtrans.multpoint3d(v + ind9 + 3, (float*) &mesh.v[ind9 + 3]);
02465 wtrans.multpoint3d(v + ind9 + 6, (float*) &mesh.v[ind9 + 6]);
02466
02467 wtrans.multnorm3d(norm , (float*) &mesh.n[ind9 ]);
02468 wtrans.multnorm3d(norm + 3, (float*) &mesh.n[ind9 + 3]);
02469 wtrans.multnorm3d(norm + 6, (float*) &mesh.n[ind9 + 6]);
02470
02471 float col[9];
02472
02473
02474
02475 col[0] = c[ind12 ] * ci2f;
02476 col[1] = c[ind12 + 1] * ci2f;
02477 col[2] = c[ind12 + 2] * ci2f;
02478 col[3] = c[ind12 + 4] * ci2f;
02479 col[4] = c[ind12 + 5] * ci2f;
02480 col[5] = c[ind12 + 6] * ci2f;
02481 col[6] = c[ind12 + 8] * ci2f;
02482 col[7] = c[ind12 + 9] * ci2f;
02483 col[8] = c[ind12 + 10] * ci2f;
02484
02485 vec_copy(&mesh.c[ind12 ], col );
02486 mesh.c[ind12 + 3] = alpha;
02487 vec_copy(&mesh.c[ind12 + 4], col + 3);
02488 mesh.c[ind12 + 7] = alpha;
02489 vec_copy(&mesh.c[ind12 + 8], col + 6);
02490 mesh.c[ind12 + 11] = alpha;
02491
02492 mesh.f[i*3 ] = i*3;
02493 mesh.f[i*3+1] = i*3 + 1;
02494 mesh.f[i*3+2] = i*3 + 2;
02495 }
02496
02497 append_trimesh_v3f_n3f_c3f(mesh);
02498 }
02499
02500
02501
02502 void OSPRayRenderer::trimesh_c4u_n3f_v3f(Matrix4 & wtrans, unsigned char *c,
02503 float *n, float *v,
02504 int numfacets, int matindex) {
02505 if (!context_created) return;
02506 if (verbose == RT_VERB_DEBUG) printf("OSPRayRenderer) creating trimesh_c4u_n3f_v3f: %d...\n", numfacets);
02507 tricolor_cnt += numfacets;
02508
02509
02510 osp_trimesh_v3f_n3f_c3f mesh;
02511 memset(&mesh, 0, sizeof(mesh));
02512 mesh.numtris = numfacets;
02513 mesh.numverts = 3L * numfacets;
02514 mesh.matindex = matindex;
02515 mesh.v = (float *) calloc(1, numfacets * 9*sizeof(float));
02516 mesh.n = (float *) calloc(1, numfacets * 9*sizeof(float));
02517 mesh.c = (float *) calloc(1, numfacets * 12*sizeof(float));
02518 mesh.f = (int *) calloc(1, numfacets * 3*sizeof(int));
02519
02520 float alpha = 1.0f;
02521
02522
02523
02524
02525 int i, ind, ind9, ind12;
02526 const float ci2f = 1.0f / 255.0f;
02527 for (i=0,ind=0,ind9=0,ind12=0; i<numfacets; i++,ind+=3,ind9+=9,ind12+=12) {
02528
02529 wtrans.multpoint3d(v + ind9 , (float*) &mesh.v[ind9 ]);
02530 wtrans.multpoint3d(v + ind9 + 3, (float*) &mesh.v[ind9 + 3]);
02531 wtrans.multpoint3d(v + ind9 + 6, (float*) &mesh.v[ind9 + 6]);
02532
02533 wtrans.multnorm3d(n + ind9 , &mesh.n[ind9 ]);
02534 wtrans.multnorm3d(n + ind9 + 3, &mesh.n[ind9 + 3]);
02535 wtrans.multnorm3d(n + ind9 + 6, &mesh.n[ind9 + 3]);
02536
02537
02538
02539 float col[9];
02540 col[0] = c[ind12 ] * ci2f;
02541 col[1] = c[ind12 + 1] * ci2f;
02542 col[2] = c[ind12 + 2] * ci2f;
02543 col[3] = c[ind12 + 4] * ci2f;
02544 col[4] = c[ind12 + 5] * ci2f;
02545 col[5] = c[ind12 + 6] * ci2f;
02546 col[6] = c[ind12 + 8] * ci2f;
02547 col[7] = c[ind12 + 9] * ci2f;
02548 col[8] = c[ind12 + 10] * ci2f;
02549
02550 vec_copy(&mesh.c[ind12 ], col );
02551 mesh.c[ind12 + 3] = alpha;
02552 vec_copy(&mesh.c[ind12 + 4], col + 3);
02553 mesh.c[ind12 + 7] = alpha;
02554 vec_copy(&mesh.c[ind12 + 8], col + 6);
02555 mesh.c[ind12 + 11] = alpha;
02556
02557 mesh.f[i*3 ] = i*3;
02558 mesh.f[i*3+1] = i*3 + 1;
02559 mesh.f[i*3+2] = i*3 + 2;
02560 }
02561
02562 append_trimesh_v3f_n3f_c3f(mesh);
02563 }
02564
02565
02566 void OSPRayRenderer::trimesh_n3b_v3f(Matrix4 & wtrans, float *uniform_color,
02567 signed char *n, float *v,
02568 int numfacets, int matindex) {
02569 if (!context_created) return;
02570 if (verbose == RT_VERB_DEBUG) printf("OSPRayRenderer) creating trimesh_n3b_v3f: %d...\n", numfacets);
02571 trimesh_n3b_v3f_cnt += numfacets;
02572
02573
02574 osp_trimesh_v3f_n3f_c3f mesh;
02575 memset(&mesh, 0, sizeof(mesh));
02576 mesh.numtris = numfacets;
02577 mesh.numverts = 3L * numfacets;
02578 mesh.matindex = matindex;
02579 mesh.v = (float *) calloc(1, numfacets * 9*sizeof(float));
02580 mesh.n = (float *) calloc(1, numfacets * 9*sizeof(float));
02581 mesh.c = (float *) calloc(1, numfacets * 12*sizeof(float));
02582 mesh.f = (int *) calloc(1, numfacets * 3*sizeof(int));
02583
02584 float alpha = 1.0f;
02585
02586
02587
02588 int i, ind, ind9, ind12;
02589
02590 const float ci2f = 1.0f / 255.0f;
02591 const float cn2f = 1.0f / 127.5f;
02592 for (i=0,ind=0,ind9=0,ind12=0; i<numfacets; i++,ind+=3,ind9+=9,ind12+=12) {
02593 float norm[9];
02594
02595
02596
02597 norm[0] = n[ind9 ] * cn2f + ci2f;
02598 norm[1] = n[ind9 + 1] * cn2f + ci2f;
02599 norm[2] = n[ind9 + 2] * cn2f + ci2f;
02600 norm[3] = n[ind9 + 3] * cn2f + ci2f;
02601 norm[4] = n[ind9 + 4] * cn2f + ci2f;
02602 norm[5] = n[ind9 + 5] * cn2f + ci2f;
02603 norm[6] = n[ind9 + 6] * cn2f + ci2f;
02604 norm[7] = n[ind9 + 7] * cn2f + ci2f;
02605 norm[8] = n[ind9 + 8] * cn2f + ci2f;
02606
02607
02608 wtrans.multpoint3d(v + ind9 , (float*) &mesh.v[ind9 ]);
02609 wtrans.multpoint3d(v + ind9 + 3, (float*) &mesh.v[ind9 + 3]);
02610 wtrans.multpoint3d(v + ind9 + 6, (float*) &mesh.v[ind9 + 6]);
02611
02612 wtrans.multnorm3d(norm , (float*) &mesh.n[ind9 ]);
02613 wtrans.multnorm3d(norm + 3, (float*) &mesh.n[ind9 + 3]);
02614 wtrans.multnorm3d(norm + 6, (float*) &mesh.n[ind9 + 6]);
02615
02616 vec_copy(&mesh.c[ind12 ], uniform_color);
02617 mesh.c[ind12 + 3] = alpha;
02618 vec_copy(&mesh.c[ind12 + 4], uniform_color);
02619 mesh.c[ind12 + 7] = alpha;
02620 vec_copy(&mesh.c[ind12 + 8], uniform_color);
02621 mesh.c[ind12 + 11] = alpha;
02622
02623 mesh.f[i*3 ] = i*3;
02624 mesh.f[i*3+1] = i*3 + 1;
02625 mesh.f[i*3+2] = i*3 + 2;
02626 }
02627
02628 append_trimesh_v3f_n3f_c3f(mesh);
02629 }
02630
02631
02632
02633
02634 void OSPRayRenderer::trimesh_n3f_v3f(Matrix4 & wtrans, float *uniform_color,
02635 float *n, float *v, int numfacets,
02636 int matindex) {
02637 DBG();
02638 if (!context_created) return;
02639 if (verbose == RT_VERB_DEBUG) printf("OSPRayRenderer) creating trimesh_n3f_v3f: %d...\n", numfacets);
02640 trimesh_n3f_v3f_cnt += numfacets;
02641
02642
02643 osp_trimesh_v3f_n3f_c3f mesh;
02644 memset(&mesh, 0, sizeof(mesh));
02645 mesh.numtris = numfacets;
02646 mesh.numverts = 3L * numfacets;
02647 mesh.matindex = matindex;
02648 mesh.v = (float *) calloc(1, numfacets * 9*sizeof(float));
02649 mesh.n = (float *) calloc(1, numfacets * 9*sizeof(float));
02650 mesh.c = (float *) calloc(1, numfacets * 12*sizeof(float));
02651 mesh.f = (int *) calloc(1, numfacets * 3*sizeof(int));
02652
02653 float alpha = 1.0f;
02654
02655
02656 int i, ind, ind9, ind12;
02657 for (i=0,ind=0,ind9=0,ind12=0; i<numfacets; i++,ind+=3,ind9+=9,ind12+=12) {
02658
02659 wtrans.multpoint3d(v + ind9 , (float*) &mesh.v[ind9 ]);
02660 wtrans.multpoint3d(v + ind9 + 3, (float*) &mesh.v[ind9 + 3]);
02661 wtrans.multpoint3d(v + ind9 + 6, (float*) &mesh.v[ind9 + 6]);
02662
02663 wtrans.multnorm3d(n + ind9 , (float*) &mesh.n[ind9 ]);
02664 wtrans.multnorm3d(n + ind9 + 3, (float*) &mesh.n[ind9 + 3]);
02665 wtrans.multnorm3d(n + ind9 + 6, (float*) &mesh.n[ind9 + 6]);
02666
02667 vec_copy(&mesh.c[ind12 ], uniform_color);
02668 mesh.c[ind12 + 3] = alpha;
02669 vec_copy(&mesh.c[ind12 + 4], uniform_color);
02670 mesh.c[ind12 + 7] = alpha;
02671 vec_copy(&mesh.c[ind12 + 8], uniform_color);
02672 mesh.c[ind12 + 11] = alpha;
02673
02674 mesh.f[i*3 ] = i*3;
02675 mesh.f[i*3+1] = i*3 + 1;
02676 mesh.f[i*3+2] = i*3 + 2;
02677 }
02678
02679 append_trimesh_v3f_n3f_c3f(mesh);
02680 }
02681
02682
02683 #if 0
02684 void OSPRayRenderer::trimesh_v3f(Matrix4 & wtrans, float *uniform_color,
02685 float *v, int numfacets, int matindex) {
02686 if (!context_created) return;
02687 if (verbose == RT_VERB_DEBUG) printf("OSPRayRenderer) creating trimesh_v3f: %d...\n", numfacets);
02688 trimesh_v3f_cnt += numfacets;
02689
02690 set_material(geom, matindex, NULL);
02691 append_objects(buf, geom, instance);
02692 }
02693
02694 #endif
02695
02696 void OSPRayRenderer::tristrip(Matrix4 & wtrans, int numverts, const float * cnv,
02697 int numstrips, const int *vertsperstrip,
02698 const int *facets, int matindex) {
02699 if (!context_created) return;
02700 int i;
02701 int numfacets = 0;
02702 for (i=0; i<numstrips; i++)
02703 numfacets += (vertsperstrip[i] - 2);
02704
02705 if (verbose == RT_VERB_DEBUG) printf("OSPRayRenderer) creating tristrip: %d...\n", numfacets);
02706 tricolor_cnt += numfacets;
02707
02708
02709 osp_trimesh_v3f_n3f_c3f mesh;
02710 memset(&mesh, 0, sizeof(mesh));
02711 mesh.numtris = numfacets;
02712 mesh.numverts = 3L * numfacets;
02713 mesh.matindex = matindex;
02714 mesh.v = (float *) calloc(1, numfacets * 9*sizeof(float));
02715 mesh.n = (float *) calloc(1, numfacets * 9*sizeof(float));
02716 mesh.c = (float *) calloc(1, numfacets * 12*sizeof(float));
02717 mesh.f = (int *) calloc(1, numfacets * 3*sizeof(int));
02718
02719 float alpha = 1.0f;
02720
02721
02722
02723
02724
02725
02726
02727
02728 int strip, t, v = 0;
02729 int stripaddr[2][3] = { {0, 1, 2}, {1, 0, 2} };
02730
02731
02732 i=0;
02733 int ind9, ind12;
02734 for (strip=0,ind9=0,ind12=0; strip < numstrips; strip++) {
02735
02736 for (t = 0; t < (vertsperstrip[strip] - 2); t++) {
02737
02738 int v0 = facets[v + (stripaddr[t & 0x01][0])] * 10;
02739 int v1 = facets[v + (stripaddr[t & 0x01][1])] * 10;
02740 int v2 = facets[v + (stripaddr[t & 0x01][2])] * 10;
02741
02742
02743 wtrans.multpoint3d(cnv + v0 + 7, (float*) &mesh.v[ind9 ]);
02744 wtrans.multpoint3d(cnv + v1 + 7, (float*) &mesh.v[ind9 + 3]);
02745 wtrans.multpoint3d(cnv + v2 + 7, (float*) &mesh.v[ind9 + 6]);
02746
02747 wtrans.multnorm3d(cnv + v0 + 4, (float*) &mesh.n[ind9 ]);
02748 wtrans.multnorm3d(cnv + v1 + 4, (float*) &mesh.n[ind9 + 3]);
02749 wtrans.multnorm3d(cnv + v2 + 4, (float*) &mesh.n[ind9 + 6]);
02750
02751 vec_copy(&mesh.c[ind12 ], cnv + v0);
02752 mesh.c[ind12 + 3] = alpha;
02753 vec_copy(&mesh.c[ind12 + 4], cnv + v1);
02754 mesh.c[ind12 + 7] = alpha;
02755 vec_copy(&mesh.c[ind12 + 8], cnv + v2);
02756 mesh.c[ind12 + 11] = alpha;
02757
02758 mesh.f[i*3 ] = i*3;
02759 mesh.f[i*3+1] = i*3 + 1;
02760 mesh.f[i*3+2] = i*3 + 2;
02761
02762 v++;
02763 i++;
02764 ind9+=9;
02765 ind12+=12;
02766 }
02767 v+=2;
02768 }
02769
02770 append_trimesh_v3f_n3f_c3f(mesh);
02771 }
02772
02773
02774