00001 /*************************************************************************** 00002 *cr 00003 *cr (C) Copyright 1995-2019 The Board of Trustees of the 00004 *cr University of Illinois 00005 *cr All Rights Reserved 00006 *cr 00007 ***************************************************************************/ 00008 00009 /*************************************************************************** 00010 * RCS INFORMATION: 00011 * 00012 * $RCSfile: OSPRay2Renderer.h,v $ 00013 * $Author: johns $ $Locker: $ $State: Exp $ 00014 * $Revision: 1.12 $ $Date: 2021年12月13日 07:54:00 $ 00015 * 00016 ***************************************************************************/ 00038 #ifndef LIBOSPRAY2RENDERER 00039 #define LIBOSPRAY2RENDERER 00040 00041 #include <stdio.h> 00042 #include <stdlib.h> 00043 #include <ospray/ospray.h> 00044 #include "Matrix4.h" 00045 #include "ResizeArray.h" 00046 #include "WKFUtils.h" 00047 #if !defined(OSPRAY_VERSION_MAJOR) 00048 #include <ospray/version.h> 00049 #endif 00050 00051 // Prevent interactive RT window code from being compiled when 00052 // VMD isn't compiled with an interactive GUI 00053 #if defined(VMDOPTIX_INTERACTIVE_OPENGL) && !defined(VMDOPENGL) 00054 #undef VMDOPTIX_INTERACTIVE_OPENGL 00055 #endif 00056 00057 #if defined(VMDOSPRAY_INTERACTIVE_OPENGL) 00058 #include "glwin.h" 00059 #endif 00060 00062 typedef struct { 00063 OSPMaterial mat; 00064 int isvalid; 00065 float ambient; 00066 float diffuse; 00067 float specular; 00068 float shininess; 00069 float reflectivity; 00070 float opacity; 00071 float outline; 00072 float outlinewidth; 00073 int transmode; 00074 int ind; 00075 } osp_material; 00076 00077 typedef struct { 00078 float dir[3]; 00079 float color[3]; 00080 } osp_directional_light; 00081 00082 typedef struct { 00083 float pos[3]; 00084 float color[3]; 00085 } osp_positional_light; 00086 00087 00088 typedef struct { 00089 int num; 00090 float *v; 00091 float *n; 00092 float *c; 00093 int *f; 00094 OSPGeometry geom; 00095 OSPData verts; 00096 OSPData norms; 00097 OSPData cols; 00098 OSPData ind; 00099 OSPGeometricModel model; 00100 int matindex; 00101 } osp_trimesh_v3f_n3f_c3f; 00102 00103 00104 typedef struct { 00105 int num; 00106 float *xyz; // xyzr vec3 00107 float *radii; // float 00108 float *colors; // RGBA vec4 00109 OSPGeometry geom; 00110 OSPData cents; 00111 OSPData rads; 00112 OSPData cols; 00113 OSPGeometricModel model; 00114 int matindex; 00115 } osp_sphere_array_color; 00116 00117 00118 typedef struct { 00119 int num; // number of cylinders 00120 float *vertsrads; // point a, rad a, point b, rad b 00121 float *colors; // rgba 00122 unsigned int *indices; 00123 OSPGeometry geom; 00124 OSPData cyls; 00125 OSPData cols; 00126 OSPData ind; 00127 OSPGeometricModel model; 00128 int matindex; 00129 } osp_cylinder_array_color; 00130 00131 00132 class OSPRay2Renderer { 00133 public: 00134 // Use reverse rays by default rather than only when enabled interactively 00135 enum RtShadowMode { RT_SHADOWS_OFF=0, 00136 RT_SHADOWS_ON=1, 00137 }; 00138 enum FogMode { RT_FOG_NONE=0, RT_FOG_LINEAR=1, RT_FOG_EXP=2, RT_FOG_EXP2=3 }; 00139 enum CameraProjection { RT_PERSPECTIVE=0, 00140 RT_ORTHOGRAPHIC=1 00141 // , 00142 // RT_CUBEMAP=2, 00143 // RT_DOME_MASTER=3, 00144 // RT_EQUIRECTANGULAR=4, 00145 // RT_OCULUS_RIFT 00146 }; 00147 enum Verbosity { RT_VERB_MIN=0, RT_VERB_TIMING=1, RT_VERB_DEBUG=2 }; 00148 enum BGMode { RT_BACKGROUND_TEXTURE_SOLID=0, 00149 RT_BACKGROUND_TEXTURE_SKY_SPHERE=1, 00150 RT_BACKGROUND_TEXTURE_SKY_ORTHO_PLANE=2 }; 00151 enum RenderMode { RT_SCIVIS=0, RT_PATHTRACER=1 }; 00152 00153 private: 00154 struct vec3 { float x, y, z; }; 00155 struct rgba { float r, g, b, a; }; 00156 00157 struct Sphere { 00158 vec3 center; 00159 float radius; 00160 rgba color; 00161 int type; //Material ID 00162 }; 00163 00164 Verbosity verbose; 00165 int width; 00166 int height; 00167 00168 wkf_timerhandle osp_timer; 00169 double time_ctx_create; 00170 double time_ctx_setup; 00171 double time_ctx_validate; 00172 double time_ctx_AS_build; 00173 double time_ctx_destroy_scene; 00174 double time_ray_tracing; 00175 double time_image_io; 00176 00177 // OSPRay objects managed by VMD 00178 RenderMode osp_rendermode; 00179 int context_created; 00180 OSPRenderer ospRenderer; 00181 OSPWorld ospWorld; 00182 ResizeArray<OSPInstance> ospInstances; 00183 00184 ResizeArray<OSPLight> ospLights; 00185 OSPData ospLightData; 00186 OSPCamera ospCamera; 00187 OSPFrameBuffer ospFrameBuffer; 00188 00189 int interactive_renderer; 00190 00191 int lasterror; 00192 int buffers_allocated; 00193 int headlight_enabled; 00194 00195 float ao_ambient; 00196 float ao_direct; 00197 00198 // cylinder array primitive 00199 long cylinder_array_cnt; 00200 00201 // color-per-cylinder array primitive 00202 long cylinder_array_color_cnt; 00203 00204 // color-per-ring array primitive 00205 long ring_array_color_cnt; 00206 00207 // sphere array primitive 00208 long sphere_array_cnt; 00209 00210 // color-per-sphere array primitive 00211 long sphere_array_color_cnt; 00212 00213 00214 // triangle mesh primitives of various types 00215 long tricolor_cnt; 00216 long trimesh_c4u_n3b_v3f_cnt; 00217 long trimesh_n3b_v3f_cnt; 00218 long trimesh_n3f_v3f_cnt; 00219 long trimesh_v3f_cnt; 00220 00221 // state variables to hold scene geometry 00222 int scene_created; 00223 00224 // 00225 // OSPRay shader state variables and the like 00226 // 00227 00228 // shadow rendering mode 00229 int shadows_enabled; 00230 float cam_zoom; 00231 00232 float cam_stereo_eyesep; 00233 float cam_stereo_convergence_dist; 00234 00235 int dof_enabled; 00236 float cam_dof_focal_dist; 00237 float cam_dof_fnumber; 00238 00239 CameraProjection camera_projection; 00240 00241 int ext_aa_loops; 00242 int aa_samples; 00243 int ao_samples; 00244 00245 // background color and/or gradient parameters 00246 BGMode scene_background_mode; 00247 float scene_bg_color[3]; 00248 float scene_bg_grad_top[3]; 00249 float scene_bg_grad_bot[3]; 00250 float scene_gradient[3]; 00251 float scene_gradient_topval; 00252 float scene_gradient_botval; 00253 float scene_gradient_invrange; 00254 00255 // fog / depth cueing parameters 00256 int fog_mode; 00257 float fog_start; 00258 float fog_end; 00259 float fog_density; 00260 00261 ResizeArray<osp_material> materialcache; 00262 00263 ResizeArray<osp_directional_light> directional_lights; 00264 ResizeArray<osp_positional_light> positional_lights; 00265 00266 // keep track of all of the OSPRay objects we create on-the-fly... 00267 ResizeArray<osp_trimesh_v3f_n3f_c3f> trimesh_v3f_n3f_c3f; 00268 ResizeArray<osp_sphere_array_color> spheres_color; 00269 ResizeArray<osp_cylinder_array_color> cylinders_color; 00270 00271 public: 00272 static int OSPRay_Global_Init(void); 00273 static void OSPRay_Global_Shutdown(void); 00274 00276 OSPRay2Renderer(void); 00277 ~OSPRay2Renderer(void); 00278 00280 void check_verbose_env(); 00281 00283 void setup_context(int width, int height); 00284 00286 void reposp_context_stats(void); 00287 00289 void shadows_on(int onoff) { shadows_enabled = (onoff != 0); } 00290 00292 void set_aa_samples(int cnt) { aa_samples = cnt; } 00293 00295 void set_camera_projection(CameraProjection m) { camera_projection = m; } 00296 00298 void set_camera_zoom(float zoomfactor) { cam_zoom = zoomfactor; } 00299 00301 void set_camera_stereo_eyesep(float eyesep) { cam_stereo_eyesep = eyesep; } 00302 00304 void set_camera_stereo_convergence_dist(float dist) { 00305 cam_stereo_convergence_dist = dist; 00306 } 00307 00309 void dof_on(int onoff) { dof_enabled = (onoff != 0); } 00310 00312 void set_camera_dof_focal_dist(float d) { cam_dof_focal_dist = d; } 00313 00315 void set_camera_dof_fnumber(float n) { cam_dof_fnumber = n; } 00316 00318 void set_ao_samples(int cnt) { ao_samples = cnt; } 00319 00321 void set_ao_ambient(float aoa) { ao_ambient = aoa; } 00322 00324 void set_ao_direct(float aod) { ao_direct = aod; } 00325 00326 void set_bg_mode(BGMode m) { scene_background_mode = m; } 00327 void set_bg_color(float *rgb) { memcpy(scene_bg_color, rgb, sizeof(scene_bg_color)); } 00328 void set_bg_color_grad_top(float *rgb) { memcpy(scene_bg_grad_top, rgb, sizeof(scene_bg_grad_top)); } 00329 void set_bg_color_grad_bot(float *rgb) { memcpy(scene_bg_grad_bot, rgb, sizeof(scene_bg_grad_bot)); } 00330 void set_bg_gradient(float *vec) { memcpy(scene_gradient, vec, sizeof(scene_gradient)); } 00331 void set_bg_gradient_topval(float v) { scene_gradient_topval = v; } 00332 void set_bg_gradient_botval(float v) { scene_gradient_botval = v; } 00333 00334 void set_cue_mode(FogMode mode, float start, float end, float density) { 00335 fog_mode = mode; 00336 fog_start = start; 00337 fog_end = end; 00338 fog_density = density; 00339 } 00340 00341 void init_materials(); 00342 void add_material(int matindex, float ambient, float diffuse, 00343 float specular, float shininess, float reflectivity, 00344 float opacity, float outline, float outlinewidth, 00345 int transmode); 00346 void set_material(OSPGeometricModel &model, int matindex, float *uniform_color); 00347 void attach_sphere_array(int numsp, int matindex, 00348 osp_sphere_array_color &sparray); 00349 void attach_cylinder_array(int numcyl, int matindex, 00350 osp_cylinder_array_color &cylarray); 00351 void attach_mesh(int numverts, int numfacets, int matindex, 00352 osp_trimesh_v3f_n3f_c3f &mesh); 00353 00354 00355 void clear_all_lights() { 00356 headlight_enabled = 0; 00357 directional_lights.clear(); 00358 positional_lights.clear(); 00359 } 00360 void headlight_onoff(int onoff) { headlight_enabled = (onoff==1); }; 00361 void add_directional_light(const float *dir, const float *color); 00362 void add_positional_light(const float *pos, const float *color); 00363 00364 void update_rendering_state(int interactive); 00365 00366 void framebuffer_config(int fbwidth, int fbheight); 00367 void framebuffer_resize(int fbwidth, int fbheight); 00368 void framebuffer_destroy(void); 00369 00370 void render_compile_and_validate(void); 00371 void render_to_file(const char *filename); 00372 #if defined(VMDOSPRAY_INTERACTIVE_OPENGL) 00373 void render_to_glwin(const char *filename); 00374 #endif 00375 00376 void destroy_scene(void); 00377 00378 #if 1 00379 void cylinder_array(Matrix4 *wtrans, float rscale, float *uniform_color, 00380 int cylnum, float *points, int matindex); 00381 #endif 00382 00383 void cylinder_array_color(Matrix4 & wtrans, float rscale, int cylnum, 00384 float *points, float *radii, float *colors, 00385 int matindex); 00386 00387 #if 0 00388 void ring_array_color(Matrix4 & wtrans, float rscale, int rnum, 00389 float *centers, float *norms, float *radii, 00390 float *colors, int matindex); 00391 #endif 00392 00393 void sphere_array(Matrix4 *wtrans, float rscale, float *uniform_color, 00394 int spnum, float *centers, float *radii, int matindex); 00395 00396 void sphere_array_color(Matrix4 & wtrans, float rscale, int spnum, 00397 float *centers, float *radii, float *colors, 00398 int matindex); 00399 00400 void tricolor_list(Matrix4 & wtrans, int numtris, float *vnc, int matindex); 00401 00402 void trimesh_c4n3v3(Matrix4 & wtrans, int numverts, 00403 float *cnv, int numfacets, int * facets, int matindex); 00404 00405 void trimesh_c4u_n3b_v3f(Matrix4 & wtrans, unsigned char *c, signed char *n, 00406 float *v, int numfacets, int matindex); 00407 00408 void trimesh_c4u_n3f_v3f(Matrix4 & wtrans, unsigned char *c, 00409 float *n, float *v, int numfacets, int matindex); 00410 00411 void trimesh_n3b_v3f(Matrix4 & wtrans, float *uniform_color, 00412 signed char *n, float *v, int numfacets, int matindex); 00413 00414 void trimesh_n3f_v3f(Matrix4 & wtrans, float *uniform_color, 00415 float *n, float *v, int numfacets, int matindex); 00416 00417 #if 0 00418 void trimesh_v3f(Matrix4 & wtrans, float *uniform_color, 00419 float *v, int numfacets, int matindex); 00420 #endif 00421 00422 void tristrip(Matrix4 & wtrans, int numverts, const float * cnv, 00423 int numstrips, const int *vertsperstrip, 00424 const int *facets, int matindex); 00425 00426 }; 00427 00428 #endif 00429