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