-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscene.cpp
100 lines (81 loc) · 2.6 KB
/
scene.cpp
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
#include "scene.h"
#include "scene_types.h"
#include <string.h>
#include <algorithm>
Object *initSphere(point3 center, float radius, Material mat) {
Object *ret;
ret = (Object *)malloc(sizeof(Object));
ret->geom.type = SPHERE;
ret->geom.sphere.center = center;
ret->geom.sphere.radius = radius;
memcpy(&(ret->mat), &mat, sizeof(Material));
return ret;
}
Object *initPlane(vec3 normal, float d, Material mat) {
Object *ret;
ret = (Object *)malloc(sizeof(Object));
ret->geom.type = PLANE;
ret->geom.plane.normal = normalize(normal);
ret->geom.plane.dist = d;
memcpy(&(ret->mat), &mat, sizeof(Material));
return ret;
}
#include "stdio.h"
Object *initTriangle(point3 v[3], vec3 vt[3], vec3 vn[3], Material mat) {
Object *ret;
ret = (Object *)malloc(sizeof(Object));
ret->geom.type = TRIANGLE;
memcpy(ret->geom.triangle.v, v, sizeof(point3) * 3);
memcpy(ret->geom.triangle.vt, vt, sizeof(vec3) * 3);
memcpy(ret->geom.triangle.vn, vn, sizeof(vec3) * 3);
memcpy(&(ret->mat), &mat, sizeof(Material));
return ret;
}
Object *initCone(point3 C, vec3 V, float teta, Material mat) {
Object *ret;
ret = (Object *)malloc(sizeof(Object));
ret->geom.type = CONE;
ret->geom.cone.C = C;
ret->geom.cone.V = V;
ret->geom.cone.teta = teta;
memcpy(&(ret->mat), &mat, sizeof(Material));
return ret;
}
void freeObject(Object *obj) {
free(obj);
}
Light *initLight(point3 position, color3 color) {
Light *light = (Light*)malloc(sizeof(Light));
light->position = position;
light->color = color;
return light;
}
void freeLight(Light *light) {
free(light);
}
Scene * initScene() {
return new Scene;
}
void freeScene(Scene *scene) {
std::for_each(scene->objects.begin(), scene->objects.end(), freeObject);
std::for_each(scene->lights.begin(), scene->lights.end(), freeLight);
delete scene;
}
void setCamera(Scene *scene, point3 position, point3 at, vec3 up, float fov, float aspect) {
scene->cam.fov = fov;
scene->cam.aspect = aspect;
scene->cam.position = position;
scene->cam.zdir = normalize(at-position);
scene->cam.xdir = normalize(cross(up, scene->cam.zdir));
scene->cam.ydir = normalize(cross(scene->cam.zdir, scene->cam.xdir));
scene->cam.center = 1.f / tanf ((scene->cam.fov * glm::pi<float>() / 180.f) * 0.5f) * scene->cam.zdir;
}
void addObject(Scene *scene, Object *obj) {
scene->objects.push_back(obj);
}
void addLight(Scene *scene, Light *light) {
scene->lights.push_back(light);
}
void setSkyColor(Scene *scene, color3 c) {
scene->skyColor = c;
}