-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWASM_forestDemo.c
178 lines (154 loc) · 6.32 KB
/
WASM_forestDemo.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
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
//emcc -o WASM_forestDemo.js WASM_forestDemo.c -s EXPORTED_FUNCTIONS='["_initialize", "_renderPass", "_getFrameBuffer", "_deleteWebContext", "_initializeFromObj", "_renderPassWireFrame", "_updateExplodeScalar", "_updateColorBuffer", "_updateLightVector", "_addMatrixValue", "_addMatrixSlot","_randomizeColorBuffer", "_misc"]' -s --preload-file forestPondFIXED.obj
#include <string.h>
#include <emscripten.h>
#include "GraphicsEngine/raster/rasterizer.c"
#include "GraphicsEngine/3Dmath/operations.c"
#include "GraphicsEngine/engineTypes.h"
#include "GraphicsEngine/graphicsEngineFunctions.c"
#include "WASM_transforms.c"
#include "commonCoords.c"
#include "utahTeapot.c"
#include "GraphicsEngine/OBJParser/parser.c"
EMSCRIPTEN_KEEPALIVE
webContext* initialize(int height, int width){
webContext* wc = malloc(sizeof(webContext));
wc->rc = createRenderContext_RGBA(height, width);
vertexBuffer* vb0 = createVertexBuffer(108);
memcpy(vb0->inputVertices, correctedCube, sizeof(float) * 108);
normalBuffer* nb0 = generateNormals(vb0);
colorBuffer* cb0 = createColorBuffer(108);
for(int i = 0; i < cb0->length; i++) cb0->inputColors[i] = 127;
mesh* mesh0 = meshify(vb0, cb0, nb0);
wc->sc = createScene(1);
wc->sc->meshes[0] = mesh0;
wc->sc->lightVector = malloc(sizeof(vec3));
wc->sc->lightVector->x = 0; wc->sc->lightVector->y = 0; wc->sc->lightVector->z = -1;
wc->ts = malloc(sizeof(transformSpec));
wc->ts->translateX = 0;
wc->ts->translateY = 0;
wc->ts->translateZ = 5;
wc->ts->rotateX = 45.0;
wc->ts->rotateY = 45.0;
wc->ts->rotateZ = 0.0;
wc->ts->explodeScalar = 0.0;
wc->ts->matrixArrayLength = 0;
wc->ts->matrixArray = NULL;
return wc;
}
EMSCRIPTEN_KEEPALIVE
webContext* initializeFromObj(int height, int width){
webContext* wc = malloc(sizeof(webContext));
wc->rc = createRenderContext_RGBA(height, width);
object* obj = parseNoMTL("forestPondFIXED.obj");
vertexBuffer* vb0 = createVertexBuffer(obj->faceCount * 9);
memcpy(vb0->inputVertices, obj->faces, sizeof(float) * obj->faceCount * 9);
normalBuffer* nb0 = generateNormals(vb0);
colorBuffer* cb0 = createColorBuffer(obj->faceCount * 9);
for(int i = 0; i < cb0->length; i++) cb0->inputColors[i] = 127;
mesh* mesh0 = meshify(vb0, cb0, nb0);
deleteObjNoMtl(obj);
wc->sc = createScene(1);
wc->sc->meshes[0] = mesh0;
wc->sc->lightVector = malloc(sizeof(vec3));
wc->sc->lightVector->x = 0; wc->sc->lightVector->y = 0; wc->sc->lightVector->z = -1;
wc->ts = malloc(sizeof(transformSpec));
wc->ts->translateX = 0;
wc->ts->translateY = 2;
wc->ts->translateZ = 10;
wc->ts->rotateX = 180.0;
wc->ts->rotateY = -90.0;
wc->ts->rotateZ = 0.0;
wc->ts->explodeScalar = 0.0;
wc->ts->matrixArrayLength = 0;
wc->ts->matrixArray = NULL;
return wc;
}
EMSCRIPTEN_KEEPALIVE
void renderPass(webContext* wc, float translateX, float translateY, float translateZ, float rotateX, float rotateY, float rotateZ){
cleanRenderContext_RGBA(wc->rc);
wc->ts->translateX += translateX;
wc->ts->translateY += translateY;
wc->ts->translateZ += translateZ;
wc->ts->rotateX += rotateX;
wc->ts->rotateY += rotateY;
wc->ts->rotateZ += rotateZ;
transform(wc->rc, wc->ts, wc->sc, wc->sc->meshes[0]->vb, wc->sc->meshes[0]->cb, wc->sc->meshes[0]->nb);
rasterize_RGBA(wc->rc, wc->sc->meshes[0]->vb, wc->sc->meshes[0]->cb);
}
EMSCRIPTEN_KEEPALIVE
void renderPassWireFrame(webContext* wc, float translateX, float translateY, float translateZ, float rotateX, float rotateY, float rotateZ){
cleanRenderContext_RGBA(wc->rc);
wc->ts->translateX += translateX;
wc->ts->translateY += translateY;
wc->ts->translateZ += translateZ;
wc->ts->rotateX += rotateX;
wc->ts->rotateY += rotateY;
wc->ts->rotateZ += rotateZ;
transform(wc->rc, wc->ts, wc->sc, wc->sc->meshes[0]->vb, wc->sc->meshes[0]->cb, wc->sc->meshes[0]->nb);
rasterizeNoScanline_RGBA(wc->rc, wc->sc->meshes[0]->vb, wc->sc->meshes[0]->cb);
}
EMSCRIPTEN_KEEPALIVE
void updateExplodeScalar(webContext* wc, float scalar){
wc->ts->explodeScalar = scalar;
}
EMSCRIPTEN_KEEPALIVE
void randomizeColorBuffer(webContext* wc){
for(int i = 0; i < wc->sc->meshes[0]->vb->length; i += 3){
wc->sc->meshes[0]->cb->inputColors[i] = rand() % 255;
wc->sc->meshes[0]->cb->inputColors[i + 1] = rand() % 255;
wc->sc->meshes[0]->cb->inputColors[i + 2] = rand() % 255;
}
}
EMSCRIPTEN_KEEPALIVE
void updateColorBuffer(webContext* wc, unsigned char r, unsigned char g, unsigned char b){
for(int i = 0; i < wc->sc->meshes[0]->vb->length; i += 3){
wc->sc->meshes[0]->cb->inputColors[i] = r;
wc->sc->meshes[0]->cb->inputColors[i + 1] = g;
wc->sc->meshes[0]->cb->inputColors[i + 2] = b;
}
}
EMSCRIPTEN_KEEPALIVE
void updateLightVector(webContext* wc, float x, float y, float z){
wc->sc->lightVector->x = x;
wc->sc->lightVector->y = y;
wc->sc->lightVector->z = z;
normalizeVector(wc->sc->lightVector);
}
EMSCRIPTEN_KEEPALIVE
uint8_t* getFrameBuffer(webContext* wc){
return wc->rc->frameBuffer;
}
EMSCRIPTEN_KEEPALIVE
void deleteWebContext(webContext* wc){
deleteRenderContext(wc->rc);
deleteScene(wc->sc);
free(wc->ts);
free(wc);
}
EMSCRIPTEN_KEEPALIVE
void addMatrixSlot(webContext* wc){
float* new = realloc(wc->ts->matrixArray, sizeof(float) * (wc->ts->matrixArrayLength + 16));
wc->ts->matrixArray = new;
}
EMSCRIPTEN_KEEPALIVE
void addMatrixValue(webContext* wc, float newVal){
wc->ts->matrixArray[wc->ts->matrixArrayLength] = newVal;
wc->ts->matrixArrayLength++;
}
//for debugging purposes
EMSCRIPTEN_KEEPALIVE
int misc(webContext* wc){
return wc->sc->meshes[0]->vb->length;
}
//for debugging purposes
// int main(){
// webContext* wc = initializeFromObj(800, 600);
// renderPass(wc, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
// renderPass(wc, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
// // unsigned char* ptr = getFrameBuffer(wc);
// renderPass(wc, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
// for(int i = 0; i < wc->rc->height * wc->rc->width * 4; i+=4){
// if(wc->rc->frameBuffer[i] != 0 && wc->rc->frameBuffer[i] != 254)
// printf(" %d, %d, %d, %d : %p\n ", wc->rc->frameBuffer[i], wc->rc->frameBuffer[i + 1], wc->rc->frameBuffer[i + 2], wc->rc->frameBuffer[i + 3], wc->rc->frameBuffer + i);
// }
// }