-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwireframe2.ino
333 lines (263 loc) · 7.25 KB
/
wireframe2.ino
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// from GameDuino Project
// http://excamera.com/sphinx/gameduino/samples/wireframe/index.html
////////////////////////////////////////////////////////////////////////////////
// 3D Projection
////////////////////////////////////////////////////////////////////////////////
#if defined(WIREFRAME)
struct ship
{
const char *name;
byte nvertices;
prog_char *vertices;
byte nedges;
prog_uchar *edges;
};
#include "eliteships.h"
#define NSHIPS (sizeof(eliteships) / sizeof(eliteships[0]))
static float mat[9];
// Taken from glRotate()
static void rotation(float phi)
{
float x = 0.57735026918962573;
float y = 0.57735026918962573;
float z = 0.57735026918962573;
float s = sin(phi);
float c = cos(phi);
mat[0] = x*x*(1-c)+c;
mat[1] = x*y*(1-c)-z*s;
mat[2] = x*z*(1-c)+y*s;
mat[3] = y*x*(1-c)+z*s;
mat[4] = y*y*(1-c)+c;
mat[5] = y*z*(1-c)-x*s;
mat[6] = x*z*(1-c)-y*s;
mat[7] = y*z*(1-c)+x*s;
mat[8] = z*z*(1-c)+c;
}
//static byte projected[40 * 2];
static byte projected[40 * 3]; // add z in the projected truc ... vinz 24/02/2013
static byte sn; // Ship number, 0-NSHIPS
static float phi; // Current rotation angle
void project(struct ship *s, float distance)
{
byte vx;
prog_char *pm = s->vertices;
prog_char *pm_e = pm + (s->nvertices * 3);
byte *dst = projected;
char x, y, z;
while (pm < pm_e) {
x = pgm_read_byte_near(pm++);
y = pgm_read_byte_near(pm++);
z = pgm_read_byte_near(pm++);
float xx = x * mat[0] + y * mat[3] + z * mat[6];
float yy = x * mat[1] + y * mat[4] + z * mat[7];
float zz = x * mat[2] + y * mat[5] + z * mat[8] + distance;
//float q = 140 / (140 + zz);
//*dst++ = byte(128 + xx * q);
//*dst++ = byte(128 + yy * q);
float q = 100 / (100 + zz);
*dst++ = byte((MIDX + xx * q )*1.01); // correction perspective sur le grand ecran
*dst++ = byte(MIDY + yy * q);
*dst++ = byte( 128 + zz ); // add z in the projected truc ... vinz 24/02/2013
}
}
// added for outside call without include ... vince 24/02/2013
void drawship(int shipnb, float distance, float rot) {
//rotation(rot);
rotation(phi);
phi += 0.02;
//if( phi > 3.14 ) phi = 0;
draw(&eliteships[shipnb], distance);
}
void draw(struct ship *s, float distance)
{
project(s, distance);
prog_uchar *pe = s->edges;
prog_uchar *pe_e = pe + (s->nedges * 2);
while (pe < pe_e) {
//byte *v0 = &projected[pgm_read_byte_near(pe++) *3 ];
//byte *v1 = &projected[pgm_read_byte_near(pe++) *3 ];
byte *v0 = &projected[ pgm_read_byte_near(pe++) *3 ]; // add z in the projected truc ... vinz 24/02/2013
byte *v1 = &projected[ pgm_read_byte_near(pe++) *3 ]; // add z in the projected truc ... vinz 24/02/2013
int ax = v0[0];
int ay = v0[1];
int bx = v1[0];
int by = v1[1];
int az = v0[2];
int bz = v1[2];
line( ax, ay, bx, by, abs( ((az+bz)/2+10) / (distance/100) )+ 5 );
//line( ax, ay, bx, by, map( (az+bz), 0, 200, 1, 255) );
//line( ax, ay, bx, by, 255);
#ifdef DEBUG
Serial.print("->");
Serial.print("\t");
Serial.print(ax);
Serial.print("\t");
Serial.print(ay);
Serial.print("\t");
Serial.print(bx);
Serial.print("\t");
Serial.print(by);
Serial.print("\t\t");
Serial.print(bz);
Serial.print("\t");
Serial.print(bz);
Serial.println();
#endif
}
}
// Draw one frame of ship
void cycle(float distance)
{
rotation(phi);
phi += 0.15;
//setvirtualdisplay( 0 );
draw(&eliteships[sn], distance);
//render();
}
void rendercycle(float distance)
{
rotation(phi);
//phi += 0.15;
phi += 0.05;
setvirtualdisplay( 0 );
draw(&eliteships[sn], distance);
//#ifdef WIREFRAME_NUMBER
int diz = int( sn/10);
dispcarascii( MIDX - OFFSET, 1, '0' + diz ,3 );
int unit = int(sn - diz*10);
dispcarascii( MIDX - 2*OFFSET, 1, '0' + unit ,3 );
//#endif
render();
}
void wireframeloop()
{
const char *name = eliteships[sn].name;
int d;
for (d = 0; d < 100; d=d+2)
rendercycle(1000 - 8.5 * d);
for (d = 0; d < 100; d++)
rendercycle( 1000 - 8.5*100 );
for (d = 100; d > 0; d--)
rendercycle(1000 - 8.5 * d);
//for (d = 0; d < 100; d++)
//cycle(10 * d);
sn = (sn + 1) % NSHIPS;
}
#endif
/*
// from GameDuino Project
// http://excamera.com/sphinx/gameduino/samples/wireframe/index.html
////////////////////////////////////////////////////////////////////////////////
// 3D Projection
////////////////////////////////////////////////////////////////////////////////
// ORIGINAL VERSION
struct ship
{
const char *name;
byte nvertices;
prog_char *vertices;
byte nedges;
prog_uchar *edges;
};
#include "eliteships.h"
#define NSHIPS (sizeof(eliteships) / sizeof(eliteships[0]))
static float mat[9];
// Taken from glRotate()
static void rotation(float phi)
{
float x = 0.57735026918962573;
float y = 0.57735026918962573;
float z = 0.57735026918962573;
float s = sin(phi);
float c = cos(phi);
mat[0] = x*x*(1-c)+c;
mat[1] = x*y*(1-c)-z*s;
mat[2] = x*z*(1-c)+y*s;
mat[3] = y*x*(1-c)+z*s;
mat[4] = y*y*(1-c)+c;
mat[5] = y*z*(1-c)-x*s;
mat[6] = x*z*(1-c)-y*s;
mat[7] = y*z*(1-c)+x*s;
mat[8] = z*z*(1-c)+c;
}
static byte projected[40 * 2];
void project(struct ship *s, float distance)
{
byte vx;
prog_char *pm = s->vertices;
prog_char *pm_e = pm + (s->nvertices * 3);
byte *dst = projected;
char x, y, z;
while (pm < pm_e) {
x = pgm_read_byte_near(pm++);
y = pgm_read_byte_near(pm++);
z = pgm_read_byte_near(pm++);
float xx = x * mat[0] + y * mat[3] + z * mat[6];
float yy = x * mat[1] + y * mat[4] + z * mat[7];
float zz = x * mat[2] + y * mat[5] + z * mat[8] + distance;
//float q = 140 / (140 + zz);
//*dst++ = byte(128 + xx * q);
//*dst++ = byte(128 + yy * q);
float q = 100 / (100 + zz);
*dst++ = byte(MIDX + xx * q);
*dst++ = byte(MIDY + yy * q);
}
}
// added for outside call without include ... vince 24/02/2013
void drawship(int shipnb, float distance, float phi) {
rotation(phi);
draw(&eliteships[shipnb], distance);
}
void draw(struct ship *s, float distance)
{
project(s, distance);
prog_uchar *pe = s->edges;
prog_uchar *pe_e = pe + (s->nedges * 2);
while (pe < pe_e) {
//byte *v0 = &projected[pgm_read_byte_near(pe++) << 1];
//byte *v1 = &projected[pgm_read_byte_near(pe++) << 1];
byte *v0 = &projected[pgm_read_byte_near(pe++) ];
byte *v1 = &projected[pgm_read_byte_near(pe++) ];
int ax = v0[0];
int ay = v0[1];
int bx = v1[0];
int by = v1[1];
line2(ax,bx,ay,by, MAX_INTENSITY );
if(DEBUG) {
Serial.print("->");
Serial.print("\t");
Serial.print(ax);
Serial.print("\t");
Serial.print(ay);
Serial.print("\t");
Serial.print(bx);
Serial.print("\t");
Serial.print(by);
Serial.println();
}
}
}
static byte sn; // Ship number, 0-NSHIPS
static float phi; // Current rotation angle
// Draw one frame of ship
void cycle(float distance)
{
rotation(phi);
phi += 0.02;
setvirtualdisplay( 0 );
draw(&eliteships[sn], distance);
render(20);
}
void wireframeloop()
{
const char *name = eliteships[sn].name;
int d;
for (d = 0; d < 100; d++)
cycle(1000 - 10 * d);
for (d = 0; d < 72*6; d++)
cycle(0.0);
for (d = 0; d < 100; d++)
cycle(10 * d);
sn = (sn + 1) % NSHIPS;
}
*/