-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRi.cpp
More file actions
313 lines (259 loc) · 10.1 KB
/
Copy pathRi.cpp
File metadata and controls
313 lines (259 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include "Ri.h"
#include "Primitives.h"
#include "States.h"
#include "Renderer.h"
#include <iostream>
#define PI 3.141592653589793238462643383279502884197
ImageState image_state;
CameraState camera_state;
RenderState render_state;
TransformationState transformation_state;
WorldState world_state;
Eigen::Matrix4f get_ortho_projection_matrix(float width, float height, float zFar, float zNear)
{
Eigen::Matrix4f ortho_projection = Eigen::Matrix4f::Identity();
ortho_projection << 1 / width, 0, 0, 0,
0, 1 / height, 0, 0,
0, 0, -(2 / zFar - zNear), 0,
0, 0, 0, 1;
//std::cout << "Ortho Projection: " << ortho_projection;
return ortho_projection;
}
Eigen::Matrix4f get_perspective_projection_matrix(float eye_fov, float aspect_ratio, float zNear, float zFar)
{
const float ar = aspect_ratio;
const float zRange = zNear - zFar;
const float eye_fov_in_rads = eye_fov * PI / 180;
const float tanHalfFOV = tanf(eye_fov_in_rads / 2.0);
Eigen::Matrix4f m = Eigen::Matrix4f::Identity();
m(0, 0) = 1.0f / (tanHalfFOV * ar);
m(0, 1) = 0.0f;
m(0, 2) = 0.0f;
m(0, 3) = 0.0f;
m(1, 0) = 0.0f;
m(1, 1) = 1.0f / tanHalfFOV;
m(1, 2) = 0.0f;
m(1, 3) = 0.0f;
m(2, 0) = 0.0f;
m(2, 1) = 0.0f;
m(2, 2) = (-zNear - zFar) / zRange;
m(2, 3) = 2.0f * zFar * zNear / zRange;
m(3, 0) = 0.0f;
m(3, 1) = 0.0f;
m(3, 2) = 1.0f;
m(3, 3) = 0.0f;
return m;
}
void RiBegin(RtToken token) {
// TODO add reyes defaults
render_state = RenderState();
camera_state = CameraState();
}
void RiEnd() {
}
void RiDisplay(char* fname, RtToken type, RtToken mode, ...) {
image_state.filename = std::string(fname);
}
void RiFormat(int x_resolution, int y_resolution, float pixelaspectratio) {
image_state.x_resolution = x_resolution;
image_state.y_resolution = y_resolution;
image_state.pixelaspectratio = pixelaspectratio;
//set default frame aspect ratio as image aspect ratio
render_state.frame_aspect_ratio = (x_resolution * pixelaspectratio) / y_resolution;
}
void RiPixelSamples(float xsamples, float ysamples) {
render_state.xsamples = xsamples;
render_state.ysamples = ysamples;
}
void RiShutter(float shutter_min, float shutter_max) {
camera_state.shutter_min = shutter_min;
camera_state.shutter_max = shutter_max;
}
void RiFrameBegin(int i) {
render_state.f_no = i;
//render_state.projection_type = RI_ORTHOGRAPHIC;
}
void RiColor(RtColor color) {
float r = color[0];
float g = color[1];
float b = color[2];
/*r *= 255.0f;
g *= 255.0f;
b *= 255.0f;*/
Color c;
c.set(r, g, b);
render_state.current_color = c;
}
void RiFrameAspectRatio(float aspect_ratio) {
render_state.frame_aspect_ratio = aspect_ratio;
}
/*
template<typename T, typename... Args>
void RiProjection(T projetion_type, Args... args) {
//https://renderman.pixar.com/resources/RenderMan_20/options.html#riprojection
}
*/
void RiWorldBegin() {
// Set the transformation matrix in render state according to the defined projection
//auto perspective = render_state.projection_type;
}
void RiWorldEnd() {
}
void RiFrameEnd() {
render_frame(world_state, render_state, image_state);
}
void RiTransformBegin() {
transformation_state.current_transformation = Eigen::Matrix4f::Identity();
transformation_state.is_in_transform_block = 1;
transformation_state.geometric_shade = nullptr;
transformation_state.surface_shader = nullptr;
}
void RiTransformEnd() {
transformation_state.current_transformation = Eigen::Matrix4f::Identity();
transformation_state.is_in_transform_block = 0;
transformation_state.geometric_shade = nullptr;
transformation_state.surface_shader = nullptr;
}
void RiTranslate(RtFloat dx, RtFloat dy, RtFloat dz) {
// Use actual matrix here
Eigen::Matrix4f model;
model <<
1, 0, 0, dx,
0, 1, 0, dy,
0, 0, 1, dz,
0, 0, 0, 1;
if (transformation_state.is_in_transform_block) {
transformation_state.current_transformation = model * transformation_state.current_transformation;
}
else {
render_state.transformation = model * render_state.transformation;
}
}
void RiRotate(float rotation_angle, float dx, float dy, float dz) {
Eigen::Matrix4f model = Eigen::Matrix4f::Zero();
float angle = rotation_angle * PI / 180.0f;
float x = dx;
float y = dy;
float z = dz;
model(0, 0) = x * x * (1 - cos(angle)) + cos(angle);
model(1, 0) = x * y * (1 - cos(angle)) + z * sin(angle);
model(2, 0) = x * z * (1 - cos(angle)) - y * sin(angle);
model(0, 1) = y * x * (1 - cos(angle)) - z * sin(angle);
model(1, 1) = y * y * (1 - cos(angle)) + cos(angle);
model(2, 1) = y * z * (1 - cos(angle)) + x * sin(angle);
model(0, 2) = z * x * (1 - cos(angle)) + y * sin(angle);
model(1, 2) = z * y * (1 - cos(angle)) - x * sin(angle);
model(2, 2) = z * z * (1 - cos(angle)) + cos(angle);
model(3, 3) = 1.0f;
if (transformation_state.is_in_transform_block) {
transformation_state.current_transformation = model * transformation_state.current_transformation;
}
else {
render_state.transformation = model * render_state.transformation;
}
}
void Ri_GeometricShader(void (*geometric_shade)(GeometricShaderPayload& p)) {
transformation_state.geometric_shade = geometric_shade;
}
void Ri_Texture(void (*surface_shader)(FragmentShaderPayload& p)) {
transformation_state.surface_shader = surface_shader;
}
template<typename T, typename... Args>
void _generate(Args ... args) {
auto model_to_world_matrix = transformation_state.current_transformation;
auto world_to_view_transformation = render_state.transformation;
auto view_to_frame_transformation = get_perspective_projection_matrix(45.0, 1.0, 1, 100);
//auto view_to_frame_transformation = get_ortho_projection_matrix(50, 50, 1, 100);
std::unique_ptr<Primitive> ptr = std::make_unique<T>(std::forward<Args>(args)...);
ptr->primitive_color = render_state.current_color;
ptr->m = model_to_world_matrix;
ptr->v = world_to_view_transformation;
ptr->p = view_to_frame_transformation;
ptr->mvp = view_to_frame_transformation * world_to_view_transformation * model_to_world_matrix;
ptr->geometric_shader = transformation_state.geometric_shade;
ptr->surface_shader = transformation_state.surface_shader;
//ptr->surface_shade = checkboard;
world_state.object_ptrs.push_back(std::move(ptr));
}
void RiSphere(float radius, float zmin, float zmax, float tmax) {
_generate<Sphere>(radius, -1 * radius, radius, 360.0f);
}
void RiCylinder(float radius, float zmin, float zmax, float tmax) {
_generate<Cylinder>(radius, zmin, zmax, tmax);
}
void RiTorus(float majorRadius, float minorRadius, float phimin, float phimax, float tmax) {
_generate<Torus>(majorRadius, minorRadius, phimin, phimax, tmax);
}
void Ri_Patch(std::vector<Eigen::Vector3f> cp) {
_generate<Patch>(cp, 5.0f);
}
//void RiSphere(float radius, float zmin, float zmax, float tmax) {
// auto model_to_world_matrix = transformation_state.current_transformation;
// auto world_to_view_transformation = render_state.transformation;
// auto view_to_frame_transformation = get_perspective_projection_matrix(45.0, 1.0, 1, 100);
// //auto view_to_frame_transformation = get_ortho_projection_matrix(50, 50, 1, 100);
//
// std::unique_ptr<Primitive> ptr = std::make_unique<Sphere>(radius, -1* radius, radius, 360.0f);
// ptr->primitive_color = render_state.current_color;
// ptr->m = model_to_world_matrix;
// ptr->v = world_to_view_transformation;
// ptr->p = view_to_frame_transformation;
// ptr->mvp = view_to_frame_transformation * world_to_view_transformation * model_to_world_matrix;
// ptr->geometric_shader = transformation_state.geometric_shade;
// //ptr->surface_shade = checkboard;
// world_state.object_ptrs.push_back(std::move(ptr));
//
//}
//
//
//void RiCylinder(float radius, float zmin, float zmax, float tmax) {
// auto model_to_world_matrix = transformation_state.current_transformation;
// auto world_to_view_transformation = render_state.transformation;
// auto view_to_frame_transformation = get_perspective_projection_matrix(90.0f, 1.0, 1, 100);
// //auto view_to_frame_transformation = get_ortho_projection_matrix(50, 50, 1, 100);
//
// std::unique_ptr<Primitive> ptr = std::make_unique<Cylinder>(radius, zmin, zmax, tmax);
// ptr->primitive_color = render_state.current_color;
// ptr->m = model_to_world_matrix;
// ptr->v = world_to_view_transformation;
// ptr->p = view_to_frame_transformation;
// ptr->mvp = view_to_frame_transformation * world_to_view_transformation * model_to_world_matrix;
// ptr->geometric_shader = transformation_state.geometric_shade;
// //ptr->surface_shade = checkboard;
// world_state.object_ptrs.push_back(std::move(ptr));
//}
//
//
//void RiTorus(float majorRadius, float minorRadius, float phimin, float phimax, float tmax) {
// auto model_to_world_matrix = transformation_state.current_transformation;
// auto world_to_view_transformation = render_state.transformation;
// auto view_to_frame_transformation = get_perspective_projection_matrix(90.0f, 1.0, 1, 100);
// //auto view_to_frame_transformation = get_ortho_projection_matrix(50, 50, 1, 100);
//
// std::unique_ptr<Primitive> ptr = std::make_unique<Torus>(majorRadius, minorRadius, phimin, phimax, tmax);
// ptr->primitive_color = render_state.current_color;
// ptr->m = model_to_world_matrix;
// ptr->v = world_to_view_transformation;
// ptr->p = view_to_frame_transformation;
// ptr->mvp = view_to_frame_transformation * world_to_view_transformation * model_to_world_matrix;
// ptr->geometric_shader = transformation_state.geometric_shade;
// //ptr->surface_shade = checkboard;
// world_state.object_ptrs.push_back(std::move(ptr));
//}
//
//
//void Ri_Patch(std::vector<Eigen::Vector3f> cp) {
// auto model_to_world_matrix = transformation_state.current_transformation;
// auto world_to_view_transformation = render_state.transformation;
// //auto view_to_frame_transformation = get_perspective_projection_matrix(90.0f, 1.0, 1, 100);
// auto view_to_frame_transformation = get_ortho_projection_matrix(50, 50, 0.01, 100);
//
// std::unique_ptr<Primitive> ptr = std::make_unique<Patch>(cp, 5.0f);
// ptr->primitive_color = render_state.current_color;
// ptr->m = model_to_world_matrix;
// ptr->v = world_to_view_transformation;
// ptr->p = view_to_frame_transformation;
// ptr->mvp = view_to_frame_transformation * world_to_view_transformation * model_to_world_matrix;
// ptr->geometric_shader = transformation_state.geometric_shade;
// world_state.object_ptrs.push_back(std::move(ptr));
//}