-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmode7.c
More file actions
210 lines (170 loc) · 4.63 KB
/
mode7.c
File metadata and controls
210 lines (170 loc) · 4.63 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
#include <emscripten.h>
#include <math.h>
#include <malloc.h>
struct m7_perspective
{
float fov;
float near;
float far;
};
struct m7_view
{
float angle;
float x;
float y;
};
struct m7_plane
{
int width;
int height;
char *data;
};
struct m7_view view;
struct m7_perspective perspective;
struct m7_plane plane;
float linear(float p, float a, float b) {
return (b - a) * p + a;
}
int clamp(int value, int min, int max) {
if (value < min) return min;
if (value > max) return max;
return value;
}
float clampf(float value, float min, float max) {
if (value < min) return min;
if (value > max) return max;
return value;
}
EMSCRIPTEN_KEEPALIVE
char* get_safe_memory(unsigned int size) {
return (char*)malloc(size);
}
EMSCRIPTEN_KEEPALIVE
void delete_safe_memory(char* pointer) {
free((void*)pointer);
}
EMSCRIPTEN_KEEPALIVE
void fill(int color, int width, int height, char* data) {
char r = (color >> 24) & 0xFF;
char g = (color >> 16) & 0xFF;
char b = (color >> 8) & 0xFF;
char a = color & 0xFF;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int offset = (y * width + x) * 4;
data[offset + 0] = r;
data[offset + 1] = g;
data[offset + 2] = b;
data[offset + 3] = a;
}
}
}
EMSCRIPTEN_KEEPALIVE
void fill_rect(
int color,
int rx,
int ry,
int rw,
int rh,
int width,
int height,
char* data
) {
char r = (color >> 24) & 0xFF;
char g = (color >> 16) & 0xFF;
char b = (color >> 8) & 0xFF;
char a = color & 0xFF;
int rex = rx + rw;
int rey = ry + rh;
int ex = rex > width ? width : rex;
int ey = rey > height ? height : rey;
int y = ry;
int x = rx;
for (int y = ry; y < ey; y++) {
for (int x = rx; x < ex; x++) {
int offset = ((y * width) + x) * 4;
data[offset + 0] = r;
data[offset + 1] = g;
data[offset + 2] = b;
data[offset + 3] = a;
}
}
}
EMSCRIPTEN_KEEPALIVE
void set_plane(int width, int height, char *data)
{
plane.width = width;
plane.height = height;
plane.data = data;
}
EMSCRIPTEN_KEEPALIVE
void set_perspective(float fov, float near, float far)
{
perspective.fov = fov;
perspective.near = near;
perspective.far = far;
}
EMSCRIPTEN_KEEPALIVE
void set_view(float angle, float x, float y)
{
view.angle = angle;
view.x = x;
view.y = y;
}
EMSCRIPTEN_KEEPALIVE
void render(int width, int height, char *data)
{
float halfFov = perspective.fov * 0.5f;
float fovStartX = cosf(view.angle - halfFov);
float fovStartY = sinf(view.angle - halfFov);
float fovEndX = cosf(view.angle + halfFov);
float fovEndY = sinf(view.angle + halfFov);
float farStartX = view.x + fovStartX * perspective.far;
float farStartY = view.y + fovStartY * perspective.far;
float farEndX = view.x + fovEndX * perspective.far;
float farEndY = view.y + fovEndY * perspective.far;
float nearStartX = view.x + fovStartX * perspective.near;
float nearStartY = view.y + fovStartY * perspective.near;
float nearEndX = view.x + fovEndX * perspective.near;
float nearEndY = view.y + fovEndY * perspective.near;
int horizonHeight = height >> 1;
for (int y = 1; y < horizonHeight; y++) {
float ny = (float)(y) / (float)(horizonHeight);
int dy = (horizonHeight + (y - 1));
float nz = 1.0f / ny;
float textureStartX = linear(nz, nearStartX, farStartX);
float textureStartY = linear(nz, nearStartY, farStartY);
float textureEndX = linear(nz, nearEndX, farEndX);
float textureEndY = linear(nz, nearEndY, farEndY);
for (int x = 0; x < width; x++) {
float nx = (float)(x) / (float)(width);
int offset = (dy * width + x) * 4;
float textureX = linear(nx, textureStartX, textureEndX);
float textureY = linear(nx, textureStartY, textureEndY);
if (textureX > 1.0f || textureX < 0.0f
|| textureY > 1.0f || textureY < 0.0f) {
data[offset + 0] = 0x00;
data[offset + 1] = 0x00;
data[offset + 2] = 0x00;
data[offset + 3] = 0x00;
continue;
}
float px = roundf(fabsf(textureX * plane.width));
float py = roundf(fabsf(textureY * plane.height));
int sampleX = (int)(px);
int sampleY = (int)(py);
int planeOffset = (sampleY * plane.width + sampleX) * 4;
if (planeOffset < 0 || planeOffset > plane.width * plane.height * 4) {
data[offset + 0] = 0x00;
data[offset + 1] = 0x00;
data[offset + 2] = 0x00;
data[offset + 3] = 0x00;
continue;
}
data[offset + 0] = plane.data[planeOffset + 0];
data[offset + 1] = plane.data[planeOffset + 1];
data[offset + 2] = plane.data[planeOffset + 2];
data[offset + 3] = 0xFF;
}
}
}