BRL-CAD
liboslrend.h
Go to the documentation of this file.
1 /* L I B O S L R E N D . H
2  * BRL-CAD
3  *
4  * Copyright (c) 2011-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7 
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * version 2.1 as published by the Free Software Foundation.
11  *
12  * This library is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this file; see the file named COPYING for more
19  * information.
20  */
21 /** @file osl-renderer.h
22  *
23  * This code represents an interface to OSL system. Through it, one can
24  * add OSL shaders and query colors, given some global parameters
25  *
26  */
27 
28 #ifndef LIBOPTICAL_LIBOSLREND_H
29 #define LIBOPTICAL_LIBOSLREND_H
30 
31 #include "common.h"
32 
33 #include <stdio.h>
34 #include <string>
35 #include <vector>
36 
37 #include "vmath.h"
38 
39 # include "oslclosure.h"
40 # include "render_svc.h"
41 /* FIXME -- Add my own ShaderSystem? */
42 # include "./oslexec_pvt.h"
43 
44 using namespace OSL;
45 
46 typedef struct Ray {
47  point_t dir;
48  point_t origin;
49 } Ray;
50 
51 enum RayType {
54 };
55 
56 /* Shared struct by which the C shader and the C++ render system may
57  exchange information
58 */
59 struct RenderInfo {
60 
61  /* -- input -- */
62  void *thread_info; /* Thread specific information */
63  fastf_t screen_x; /* Coordinates of the screen (if applicable) */
65  point_t P; /* Query point */
66  point_t N; /* Object normal */
67  point_t I; /* Incident ray direction */
68  fastf_t u, v; /* uv coordinates */
69  point_t dPdu, dPdv; /* uv tangents */
70  int depth; /* How many times the ray hit an object */
71  fastf_t surfacearea; /* FIXME */
72  ShadingAttribStateRef shader_ref; /* Reference for the shader we're querying */
73  std::vector< Vec3 > light_dirs; /* List of directions of lights that are visible from
74  this query point */
75 
76  /* -- output -- */
77  point_t pc; /* Color of the point (or multiplier) */
78  int doreflection; /* 1 if there will be reflection 0, otherwise */
79  int out_ray_type; /* bitflag describing output ray type (bit 0: reflection; 1: refraction) */
80  Ray out_ray; /* output ray (in case of reflection) */
81 
82  /* Experimental! Don't use yet */
83  Color3 reflect_weight; /* Color that will be multiplied by the
84  color returned by the reflected ray */
85  Color3 transmit_weight; /* Color that will be multiplied by the
86  color returned by the transmitted ray */
87 };
88 
89 /* Required structure to initialize an OSL shader */
90 struct ShaderInfo {
91 
92  typedef std::pair< TypeDesc, Vec3 > TypeVec;
93 
94  std::string shadername; // Name of the shader (type of shader)
95  std::string layername; // Name of the layer (name of this particular instance)
96  std::vector< std::pair<std::string, int> > iparam; // int parameters
97  std::vector< std::pair<std::string, float> > fparam; // float parameters
98  std::vector< std::pair<std::string, Color3> > cparam; // color parameters
99  std::vector< std::pair<std::string, TypeVec > > vparam; // normal/vector/point parameters
100  std::vector< std::pair<std::string, std::string> > sparam; // string parameters
101  std::vector< std::pair<std::string, Matrix44 > > mparam; // matrix parameters
102 };
103 /* Represents a parameter a shader */
104 struct ShaderParam {
105  std::string layername;
106  std::string paramname;
107 };
108 /* Represents an edge from first to second */
109 typedef std::pair < ShaderParam, ShaderParam > ShaderEdge;
110 
111 /* Required structure to initialize an OSL shader group */
113  std::vector< ShaderInfo > shader_layers;
114  std::vector< ShaderEdge > shader_edges;
115 };
116 
117 
118 /* Class 'OSLRenderer' holds global information about OSL shader system.
119  These information are hidden from the calling C code */
120 class OSLRenderer {
121 
122  ErrorHandler errhandler;
123 
124  ShadingSystem *shadingsys;
125  ShadingSystemImpl *ssi;
126  SimpleRenderer rend;
127  void *handle;
128 
129  const ClosureColor
130  *ExecuteShaders(ShaderGlobals &globals, RenderInfo *info) const;
131 
132  /* Sample a primitive from the shaders group */
133  const ClosurePrimitive* SamplePrimitive(Color3& weight,
134  const ClosureColor *closure,
135  float r) const;
136 
137  /* Helper function for SamplePrimitive */
138  void SamplePrimitiveRecurse(const ClosurePrimitive*& r_prim,
139  Color3& r_weight,
140  const ClosureColor *closure,
141  const Color3& weight, float& totw, float& r) const;
142 
143 public:
144 
145  OSLRenderer();
146  ~OSLRenderer();
147 
148  ShadingAttribStateRef AddShader(ShaderGroupInfo &group_info);
149 
150  /* Query a color */
151  Color3 QueryColor(RenderInfo *info) const;
152 
153  /* Return thread specific information */
154  void * CreateThreadInfo();
155 
156  static void Vec3toPoint_t(Vec3 s, point_t t) {
157  t[0] = s[0];
158  t[1] = s[1];
159  t[2] = s[2];
160  }
161 
162 };
163 
164 #endif
165 
166 /*
167  * Local Variables:
168  * tab-width: 8
169  * mode: C++
170  * c-basic-offset: 4
171  * indent-tabs-mode: t
172  * c-file-style: "stroustrup"
173  * End:
174  * ex: shiftwidth=4 tabstop=8
175  */
struct Ray Ray
point_t dir
Definition: liboslrend.h:47
Color3 transmit_weight
Definition: liboslrend.h:85
void * thread_info
Definition: liboslrend.h:62
if lu s
Definition: nmg_mod.c:3860
std::string paramname
Definition: liboslrend.h:106
std::vector< Vec3 > light_dirs
Definition: liboslrend.h:73
std::vector< std::pair< std::string, TypeVec > > vparam
Definition: liboslrend.h:99
Header file for the BRL-CAD common definitions.
std::string layername
Definition: liboslrend.h:95
std::pair< TypeDesc, Vec3 > TypeVec
Definition: liboslrend.h:92
fastf_t surfacearea
Definition: liboslrend.h:71
ustring dPdu
std::vector< std::pair< std::string, Matrix44 > > mparam
Definition: liboslrend.h:101
int out_ray_type
Definition: liboslrend.h:79
point_t N
Definition: liboslrend.h:66
fastf_t screen_x
Definition: liboslrend.h:63
std::vector< ShaderEdge > shader_edges
Definition: liboslrend.h:114
static void Vec3toPoint_t(Vec3 s, point_t t)
Definition: liboslrend.h:156
point_t P
Definition: liboslrend.h:65
ShadingAttribStateRef shader_ref
Definition: liboslrend.h:72
std::vector< ShaderInfo > shader_layers
Definition: liboslrend.h:113
std::vector< std::pair< std::string, float > > fparam
Definition: liboslrend.h:97
point_t pc
Definition: liboslrend.h:77
point_t I
Definition: liboslrend.h:67
std::pair< ShaderParam, ShaderParam > ShaderEdge
Definition: liboslrend.h:109
std::string shadername
Definition: liboslrend.h:94
fastf_t v
Definition: liboslrend.h:68
fastf_t screen_y
Definition: liboslrend.h:64
Color3 reflect_weight
Definition: liboslrend.h:83
std::vector< std::pair< std::string, int > > iparam
Definition: liboslrend.h:96
Definition: liboslrend.h:46
point_t dPdv
Definition: liboslrend.h:69
std::vector< std::pair< std::string, std::string > > sparam
Definition: liboslrend.h:100
RayType
Definition: liboslrend.h:51
std::string layername
Definition: liboslrend.h:105
Ray out_ray
Definition: liboslrend.h:80
double fastf_t
Definition: defines.h:300
point_t origin
Definition: liboslrend.h:48
int doreflection
Definition: liboslrend.h:78
std::vector< std::pair< std::string, Color3 > > cparam
Definition: liboslrend.h:98