diff options
Diffstat (limited to 'Vector.c')
-rwxr-xr-x | Vector.c | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/Vector.c b/Vector.c new file mode 100755 index 0000000..b29d01e --- /dev/null +++ b/Vector.c | |||
@@ -0,0 +1,112 @@ | |||
1 | #include "Gfx.h" | ||
2 | #include "Vector.h" | ||
3 | |||
4 | static double rx, ry, rz; | ||
5 | static int numpts; | ||
6 | static int border; | ||
7 | |||
8 | static int startx, starty; | ||
9 | |||
10 | typedef struct{ | ||
11 | signed int x; signed int y; signed int z; | ||
12 | unsigned char r; unsigned char g; unsigned char b; | ||
13 | } point_3d ; | ||
14 | |||
15 | static point_3d *poinz = (void*)0; | ||
16 | static point_3d *poinz_rot = (void*)0; | ||
17 | |||
18 | void Vector_init( int points, int borderlen ) { | ||
19 | int i, j, k; | ||
20 | rx = ry = rz = 0; | ||
21 | numpts = points; | ||
22 | border = borderlen; | ||
23 | |||
24 | poinz = (void*)malloc( points * points * points * sizeof( point_3d) ); | ||
25 | poinz_rot = (void*)malloc( points * points * points * sizeof( point_3d) ); | ||
26 | |||
27 | for( i = 0; i<points; i++) | ||
28 | for( j = 0; j<points; j++) | ||
29 | for( k = 0; k<points; k++) { | ||
30 | poinz[i+j*points+k*points*points].x = ( i * borderlen ) / ( points - 1 ) - ( borderlen >> 1); | ||
31 | poinz[i+j*points+k*points*points].y = ( j * borderlen ) / ( points - 1 ) - ( borderlen >> 1); | ||
32 | poinz[i+j*points+k*points*points].z = ( k * borderlen ) / ( points - 1 ) - ( borderlen >> 1); | ||
33 | poinz[i+j*points+k*points*points].r = 255; | ||
34 | poinz[i+j*points+k*points*points].g = 255; | ||
35 | poinz[i+j*points+k*points*points].b = 0; | ||
36 | } | ||
37 | } | ||
38 | |||
39 | void Vector_destroy( ) { | ||
40 | if( poinz ) | ||
41 | free( poinz ); | ||
42 | if( poinz_rot ) | ||
43 | free( poinz_rot ); | ||
44 | } | ||
45 | |||
46 | void Vector_reset( void ) { | ||
47 | rx = 0; ry = 0; rz = 0; | ||
48 | } | ||
49 | |||
50 | void Vector_angle( double dx, double dy, double dz ) { | ||
51 | rx += dx, ry += dy, rz += dz; | ||
52 | } | ||
53 | |||
54 | void Vector_move( signed int dx, signed int dy ) { | ||
55 | double rotation[3][3] = { | ||
56 | { cos(-ry)*cos(-rz), -cos(-rx)*sin(-rz)+sin(-rx)*sin(-ry)*cos(-rz), sin(-rx)*sin(-rz)+cos(-rx)*sin(-ry)*cos(-rz) }, | ||
57 | { cos(-ry)*sin(-rz), cos(-rx)*cos(-rz)+sin(-rx)*sin(-ry)*sin(-rz), -sin(-rx)*cos(-rz)+cos(-rx)*sin(-ry)*sin(-rz) }, | ||
58 | {-sin(-ry), sin(-rx)*cos(-ry) , cos(-rx)*cos(-ry) } | ||
59 | }; | ||
60 | |||
61 | int xold = rotation[0][2] * border; | ||
62 | int yold = rotation[1][2] * border; | ||
63 | int zold = rotation[2][2] * border; | ||
64 | |||
65 | int xnew = rotation[0][0] * dx + rotation[0][1] * dy + rotation[0][2] * border; | ||
66 | int ynew = rotation[1][0] * dx + rotation[1][1] * dy + rotation[1][2] * border; | ||
67 | int znew = rotation[2][0] * dx + rotation[2][1] * dy + rotation[2][2] * border; | ||
68 | |||
69 | rx += 0.01 * (xold - xnew); | ||
70 | ry += 0.01 * (yold - ynew); | ||
71 | rz += 0.01 * (zold - znew); | ||
72 | } | ||
73 | |||
74 | static int compare_points( const void *a, const void *b) { | ||
75 | return ((point_3d*)b)->z - ((point_3d*)a)->z ; | ||
76 | } | ||
77 | |||
78 | void Vector_paint( void ) { | ||
79 | double rotation[3][3] = { | ||
80 | { cos(ry)*cos(rz) , -cos(ry)*sin(rz) , sin(ry) }, | ||
81 | { sin(rx)*sin(ry)*cos(rz)+cos(rx)*sin(rz) , -sin(rx)*sin(ry)*sin(rz)+cos(rx)*cos(rz) , -sin(rx)*cos(ry) }, | ||
82 | {-cos(rx)*sin(ry)*cos(rz)+sin(rx)*sin(rz) , cos(rx)*sin(ry)*sin(rz)+sin(rx)*cos(rz) , cos(rx)*cos(ry) } | ||
83 | }; | ||
84 | int i; | ||
85 | |||
86 | for( i = 0; i < numpts*numpts*numpts; i++) { | ||
87 | poinz_rot[i].x = (rotation[0][0] * poinz[i].x + rotation[0][1] * poinz[i].y + rotation[0][2] * poinz[i].z)*65536; | ||
88 | poinz_rot[i].y = (rotation[1][0] * poinz[i].x + rotation[1][1] * poinz[i].y + rotation[1][2] * poinz[i].z)*65536; | ||
89 | poinz_rot[i].z = rotation[2][0] * poinz[i].x + rotation[2][1] * poinz[i].y + rotation[2][2] * poinz[i].z; | ||
90 | // poinz_rot[i].r = poinz[i].r; poinz_rot[i].g = poinz[i].g; poinz_rot[i].b = poinz[i].b; | ||
91 | poinz_rot[i].r = random(); poinz_rot[i].g = random(); poinz_rot[i].b = random(); | ||
92 | } | ||
93 | |||
94 | /* Sort by z-order */ | ||
95 | qsort( poinz_rot, numpts*numpts*numpts, sizeof(point_3d), compare_points); | ||
96 | |||
97 | Gfx_clear(); | ||
98 | |||
99 | for( i = 0; i < numpts*numpts*numpts; i++) { | ||
100 | int size; | ||
101 | poinz_rot[i].x /= poinz_rot[i].z + 1.5*border; | ||
102 | poinz_rot[i].y /= poinz_rot[i].z + 1.5*border; | ||
103 | poinz_rot[i].x >>= 8; | ||
104 | poinz_rot[i].y >>= 8; | ||
105 | poinz_rot[i].x += g_width>>1; | ||
106 | poinz_rot[i].y += g_height>>1; | ||
107 | |||
108 | size = 10 * border / ( (poinz_rot[i].z) + border ); | ||
109 | |||
110 | Gfx_kuller( poinz_rot[i].x-(size>>1), poinz_rot[i].y-(size>>1), size, poinz_rot[i].r, poinz_rot[i].g, poinz_rot[i].b); | ||
111 | } | ||
112 | } | ||