-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_csg.c
66 lines (51 loc) · 2.04 KB
/
example_csg.c
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
#include <stdio.h>
#include "csg.h"
#include "world.h"
int main() {
// create CSG primitive
CSGSets *csgSets = CSGPrimitiveSetsCreate();
// CSGPrimitiveSetsAppend(csgSets, (CSGPrimitive *)CSGPrimitiveCubeCreate(2));
// CSGPrimitiveSetsAppend(csgSets, (CSGPrimitive *)CSGPrimitiveCylinderCreate(1, 2, 128));
// CSGPrimitiveSetsAppend(csgSets, (CSGPrimitive *)CSGPrimitiveTriangularCreate(1, 3, 128));
CSGPrimitiveSetsAppend(csgSets, (CSGPrimitive *)CSGPrimitiveBallCreate(1, 8));
CSGPrimitiveSetsReduce(csgSets);
// convert CSG primitive set to polygon
Polygon *polygon = CSGPrimitiveSetsPolygon(csgSets);
PolygonCalculateVertexNormals(polygon);
// now, CSG primitive set can be destroyed
CSGPrimitiveSetsDestroy(csgSets);
for (int i = 0; i < 360; ++i) {
const int w = 1000, h = 1000;
Bitmap *bmp = BitmapNewImage(w, h);
// create camera
Camera *camera = CameraPerspectiveProjection(V(3, -3, 3), V(1, -2, 1), V(0, 1, 0), w, h, 0.1, 1000, 60);
// create empty scene
Scene *scene = SceneCreateEmpty();
// set camera
SceneSetCamera(scene, camera);
// create Z-buffer
ZBuffer *zbuffer = ZBufferCreate(w, h);
// append light to scene
Light light = LightCreatePointLight(V(1, 1, 1), V(1, 1, 1), V(0, -6, 3));
SceneAppendLight(scene, &light);
// append CSG primitive to scene
Transformer *transformer = TransformerCreate(V0, V(0, RADIAN(i), 0), V1);
const Material redMaterial = (Material){V(0.8274, 0.2196, 0.1098), 1, 1, 1, 30};
Thing *thing = ThingCreate(polygon, transformer, &redMaterial);
SceneAppendThing(scene, thing);
// render scene to Bitmap
SceneRender(scene, bmp, zbuffer, WorldRender, GouraudShading, PhongReflectionModel);
// save image to Bitmap file
char buf[100];
sprintf(buf, "csg_%03d.bmp", i);
BitmapWriteFile(bmp, buf);
// clean-up
ThingDestroy(thing);
TransformerDestroy(transformer);
ZBufferDestroy(zbuffer);
SceneDestroy(scene);
CameraDestroy(camera);
BitmapDestroy(bmp);
}
PolygonDestroy(polygon);
}