File: tex.c

package info (click to toggle)
bb 1.3rc1-11
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 3,728 kB
  • sloc: ansic: 36,697; sh: 329; makefile: 23
file content (340 lines) | stat: -rw-r--r-- 8,512 bytes parent folder | download | duplicates (7)
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
329
330
331
332
333
334
335
336
337
338
339
340
/*
 * BB: The portable demo
 *
 * (C) 1997 by AA-group (e-mail: aa@horac.ta.jcu.cz)
 *
 * 3rd August 1997
 * version: 1.2 [final3]
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public Licences as by published
 * by the Free Software Foundation; either version 2; or (at your option)
 * any later version
 *
 * This program is distributed in the hope that it will entertaining,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILTY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Publis License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.
 * 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <time.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <aalib.h>
#include "bb.h"

#define DEG (M_PI/180)
#define X_s aa_imgwidth(context)
#define Y_s aa_imgheight(context)
#define data context->imagebuffer
#define X_S (X_s/2)
#define Y_S (Y_s/2)
#define Tx 127
#define Ty 127
#define XRATIO X_s*zoom/320
#define YRATIO Y_s*zoom*aa_mmwidth(context)/aa_mmheight(context)/320
#define Bufflen 768
#define VERT 3
#define ArC 12000
#define MaxFaces 500

typedef unsigned char byte;
typedef unsigned short int word;
typedef unsigned long dword;
typedef struct {
    unsigned short int x, y;
} INFO;
typedef long TBL[361];
typedef struct POL {
    long x, y, z, n_x, n_y, n_z;
} POLYS[3];
typedef struct {
    long x, y, z, nx, ny, nz;
} TRANSFORMED;
typedef TRANSFORMED TRANS[MaxFaces][3];

static TBL sinn, coss;
static int nFaces;
dword alfa, beta, gama;
int centerx, centery, centerz;
float zoom = 2.0;
static byte *envmapa;
static POLYS *obj, rot[MaxFaces];
static TRANS polyg;
static int *zbuff;

#include "patnik.h"
#include "torus.h"

static void precalculate(void)
{
    unsigned i;
    for (i = 0; i <= 360; i++) {
	sinn[i] = (long) (sin(i * DEG) * 65536);
	coss[i] = (long) (cos(i * DEG) * 65536);
    }
}
void set_zbuff()
{
    zbuff = (int *) malloc(X_s * Y_s * sizeof(int));
}

void unset_zbuff()
{
    free(zbuff);
}

static inline void clear_zbuff()
{
    memset(zbuff, 0x55, (X_s * Y_s * sizeof(int)));
}


static inline void makej(long x1, long x2, long n1z, long n2z, long ii, long n1x, long n1y, long n2x, long n2y)
{
    register byte *off, *off1;
    register int *zbptr = zbuff + (ii * X_s) + x1;
    long n3x;
    long n3y;
    long n3z;
    long n3Dx;
    long n3Dy;
    long n3Dz;
    long e, rx, ry;

    if (ii >= Y_s || ii < 0 || x2 < 0 || x1 >= X_s)
	return;
    if (x1 < 0)
	x1 = 0;
    if (x2 >= X_s)
	x2 = X_s - 1;
    e = x2 - x1;
    if (!e)
	return;
    n3Dx = (n2x - n1x) / e;
    n3x = n1x;
    n3Dy = (n2y - n1y) / e;
    n3y = n1y;
    n3Dz = (n2z - n1z) / e;
    n3z = n1z;

    off = data + (ii * X_s) + x1;
    off1 = off + x2 - x1;
    for (; off <= off1;) {

	ry = (abs(n3y) >> 7) + 64;
	rx = (abs(n3x) >> 7) + 64;
	if (((n3z >> 8) - *zbptr) < 500) {
	    *off = *(envmapa + (((unsigned int) ((ry << 7) + rx)) & (128 * 128 - 1)));
	    *zbptr = n3z >> 8;
	}
	off++;
	zbptr++;
	n3x += n3Dx;
	n3y += n3Dy;
	n3z += n3Dz;
    }
}

static void optfsp(unsigned wh)
{
    byte pivot, maxy, a, b, c;
    long ii, aa, bb;
    long x1, x2, d1, d2, m1, m2, m3;

    long n1x, n1y, n1z;
    long n2x, n2y, n2z;
    long n1Dx, n1Dy, n1Dz;
    long n2Dx, n2Dy, n2Dz;

    pivot = maxy = 0;
    if (polyg[wh][pivot].y > polyg[wh][1].y)
	pivot = 1;
    if (polyg[wh][pivot].y > polyg[wh][2].y)
	pivot = 2;
    if (polyg[wh][maxy].y < polyg[wh][1].y)
	maxy = 1;
    if (polyg[wh][maxy].y < polyg[wh][2].y)
	maxy = 2;

    a = b = pivot;
    c = maxy;
  a01:
    if (b == 2)
	b = 0;
    else
	b++;
    if ((b == pivot) || (b == maxy))
	goto a01;

    m1 = polyg[wh][c].y - polyg[wh][a].y;
    m2 = polyg[wh][b].y - polyg[wh][a].y;
    m3 = polyg[wh][c].y - polyg[wh][b].y;

    if (m1) {
	d1 = ((polyg[wh][c].x - polyg[wh][a].x) << 16) / m1;
	x1 = polyg[wh][a].x << 16;
	n1Dx = (rot[wh][c].n_x - rot[wh][a].n_x) / m1;
	n1x = rot[wh][a].n_x;
	n1Dy = (rot[wh][c].n_y - rot[wh][a].n_y) / m1;
	n1y = rot[wh][a].n_y;
	n1Dz = (polyg[wh][c].z - polyg[wh][a].z) / m1;
	n1z = polyg[wh][a].z;


	ii = 0;
	if (m2) {
	    d2 = ((polyg[wh][b].x - polyg[wh][a].x) << 16) / m2;
	    x2 = polyg[wh][a].x << 16;
	    n2Dx = (rot[wh][b].n_x - rot[wh][a].n_x) / m2;
	    n2x = rot[wh][a].n_x;
	    n2Dy = (rot[wh][b].n_y - rot[wh][a].n_y) / m2;
	    n2y = rot[wh][a].n_y;
	    n2Dz = (polyg[wh][b].z - polyg[wh][a].z) / m2;
	    n2z = polyg[wh][a].z;
	    for (ii = polyg[wh][a].y; ii <= polyg[wh][b].y; ii++) {
		aa = x1 >> 16;
		bb = x2 >> 16;
		if (aa > bb)
		    makej(bb, aa, n2z, n1z, ii, n2x, n2y, n1x, n1y);
		if (aa < bb)
		    makej(aa, bb, n1z, n2z, ii, n1x, n1y, n2x, n2y);
		x1 += d1;
		x2 += d2;
		n1x += n1Dx;
		n1y += n1Dy;
		n1z += n1Dz;
		n2x += n2Dx;
		n2y += n2Dy;
		n2z += n2Dz;
	    }
	}
	if (m3) {
	    d2 = ((polyg[wh][c].x - polyg[wh][b].x) << 16) / m3;
	    x2 = polyg[wh][b].x << 16;
	    n2Dx = (rot[wh][c].n_x - rot[wh][b].n_x) / m3;
	    n2x = rot[wh][b].n_x;
	    n2Dy = (rot[wh][c].n_y - rot[wh][b].n_y) / m3;
	    n2y = rot[wh][b].n_y;
	    n2Dz = (polyg[wh][c].z - polyg[wh][b].z) / m3;
	    n2z = polyg[wh][b].z;
	    for (ii = polyg[wh][b].y + 1; ii <= polyg[wh][c].y; ii++) {
		aa = x1 >> 16;
		bb = x2 >> 16;
		if (aa > bb)
		    makej(bb, aa, n2z, n1z, ii, n2x, n2y, n1x, n1y);
		if (aa < bb)
		    makej(aa, bb, n1z, n2z, ii, n1x, n1y, n2x, n2y);
		x1 += d1;
		x2 += d2;
		n1x += n1Dx;
		n1y += n1Dy;
		n1z += n1Dz;
		n2x += n2Dx;
		n2y += n2Dy;
		n2z += n2Dz;
	    }
	}
    }
}

static void show(void)
{
    double ZZ;
    unsigned i, j;
    for (i = 0; i < nFaces; i++)
	for (j = 0; j < 3; j++) {
	    polyg[i][j].x = ((rot[i][j].x << 8) / (256 + rot[i][j].z)) * XRATIO + X_S;
	    polyg[i][j].y = ((rot[i][j].y << 8) / (256 + rot[i][j].z)) * YRATIO + Y_S;
	    polyg[i][j].z = rot[i][j].z << 16;
	}
    for (i = 0; i < nFaces; i++) {
	ZZ = (polyg[i][2].x - polyg[i][0].x) * (polyg[i][1].y - polyg[i][0].y) -
	    (polyg[i][1].x - polyg[i][0].x) * (polyg[i][2].y - polyg[i][0].y);
	if (ZZ <= 0)
	    optfsp(i);
    }
}

void disp3d(void)
{
    int i, j;
    for (i = 0; i < nFaces; i++)
	for (j = 0; j < 3; j++) {
	    int x = obj[i][j].x + centerx, y = obj[i][j].y + centery,
	     z = obj[i][j].z + centerz;
	    rot[i][j].x = (z * coss[alfa] - x * sinn[alfa]) >> 16;
	    rot[i][j].z = (z * sinn[alfa] + x * coss[alfa]) >> 16;
	    x = (y * coss[beta] - rot[i][j].z * sinn[beta]) >> 16;
	    rot[i][j].z = (y * sinn[beta] + rot[i][j].z * coss[beta]) >> 16;
	    rot[i][j].y = x;
	    x = (rot[i][j].y * coss[gama] - rot[i][j].x * sinn[gama]) >> 16;
	    rot[i][j].x = (rot[i][j].y * sinn[gama] + rot[i][j].x * coss[gama]) >> 16;
	    rot[i][j].y = x;


	    x = obj[i][j].n_x + centerx, y = obj[i][j].n_y + centery, z = obj[i][j].n_z + centerz;
	    rot[i][j].n_x = (z * coss[alfa] - x * sinn[alfa]) >> 16;
	    rot[i][j].n_z = (z * sinn[alfa] + x * coss[alfa]) >> 16;
	    x = (y * coss[beta] - rot[i][j].z * sinn[beta]) >> 16;
	    rot[i][j].n_z = (y * sinn[beta] + rot[i][j].z * coss[beta]) >> 16;
	    rot[i][j].n_y = x;
	    x = (rot[i][j].n_y * coss[gama] - rot[i][j].n_x * sinn[gama]) >> 16;
	    rot[i][j].n_x = (rot[i][j].n_y * sinn[gama] + rot[i][j].n_x * coss[gama]) >> 16;
	    rot[i][j].n_y = x;
	}
    clear_zbuff();
    memset(data, 0, X_s * Y_s);
    show();
}

void patnikconstructor(void)
{
    long i, j;
    unsigned char table[256];
    for (i = 0; i < 256; i++) {
	table[i] = pow(i / 256.0, 2.0) * 256.0;
    }

    precalculate();
    nFaces = patniknFaces;
    obj = patnikobj;
    envmapa = (byte *) malloc(128 * 128);

    for (i = 0; i <= 127; i++)
	for (j = 0; j <= 127; j++)
	    *(envmapa + i * 128 + j) = table[(unsigned char) (0x0007A120 / ((i - 64) * (i - 64) + (j - 64) * (j - 64) + 2100))];
    memcpy(&rot, &obj, sizeof(obj));

}

void torusconstructor(void)
{
    long i, j;
    unsigned char table[256];
    for (i = 0; i < 256; i++) {
	table[i] = pow(i / 256.0, 3.0) * 200.0;
    }

    precalculate();
    nFaces = torusnFaces;
    obj = torusobj;
    envmapa = (byte *) malloc(128 * 128);

    for (i = 0; i <= 127; i++)
	for (j = 0; j <= 127; j++)
	    *(envmapa + i * 128 + j) = table[(unsigned char) (0x0007A120 / ((i - 64) * (i - 64) + (j - 64) * (j - 64) + 2100))];
    memcpy(&rot, &obj, sizeof(obj));

}

void destructor()
{
    free(envmapa);
}