summaryrefslogtreecommitdiff
path: root/stern.cpp
blob: c1d68d900a0cbc234cf4cacdc819e62d99bb0af4 (plain)
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
/*	$Id$	*/
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <process.h>
#include <math.h>

void Calc3D(void);
void DrawObject(void);
void InitProgram(void);
void MainLoop(void);
void Rotation(void);
int key(void);
void Legende(void);

#define COPYRIGHT "Dark Alliance (c) 1996-2000 DreamDemon"
#define DATUM "13.02.2000"
#define PI 3.14
#define Lines 72

static int World[Lines][3] = {
	 90,  0,  0,   10, 10, 10,     90,  0,  0,   10, 10,-10,
	 90,  0,  0,   10,-10,-10,     90,  0,  0,   10,-10, 10,
	-90,  0,  0,  -10, 10, 10,    -90,  0,  0,  -10, 10,-10,
	-90,  0,  0,  -10,-10,-10,    -90,  0,  0,  -10,-10, 10,
	  0, 90,  0,   10, 10, 10,      0, 90,  0,  -10, 10, 10,
	  0, 90,  0,  -10, 10,-10,      0, 90,  0,   10, 10,-10,
	  0,-90,  0,   10,-10, 10,      0,-90,  0,  -10,-10, 10,
	  0,-90,  0,  -10,-10,-10,      0,-90,  0,   10,-10,-10,
	  0,  0, 90,   10, 10, 10,      0,  0, 90,   10,-10, 10,
	  0,  0, 90,  -10,-10, 10,      0,  0, 90,  -10, 10, 10,
	  0,  0,-90,   10, 10,-10,      0,  0,-90,   10,-10,-10,
	  0,  0,-90,  -10,-10,-10,      0,  0,-90,  -10, 10,-10,
	 10, 10, 10,  -10, 10, 10,     10,-10, 10,  -10,-10, 10,
	 10, 10,-10,  -10, 10,-10,     10,-10,-10,  -10,-10,-10,
	 10, 10, 10,   10,-10, 10,    -10, 10, 10,  -10,-10, 10,
	 10, 10,-10,   10,-10,-10,    -10, 10,-10,  -10,-10,-10,
	 10, 10, 10,   10, 10,-10,     10,-10, 10,   10,-10,-10,
	-10,-10, 10,  -10,-10,-10,    -10, 10, 10,  -10, 10,-10};

float X, Y, Z, sX, sY, Xa, Ya, Za, sXs, sYs, D, G, Page, Xmid, Ymid;
float R, R1, R2, R3, Sr1, Sr2, Sr3, Cr1, Cr2, Cr3, mX, mY, mZ;

void main(void)
{
	InitProgram();
	MainLoop();
	closegraph();
	printf("%s\n",COPYRIGHT);
//	getch();
}

void Calc3D(void)
{
	X  =  -1 * X;
	Xa = Cr1 * X  - Sr1 * Z;
	Za = Sr1 * X  + Cr1 * Z;
	X  = Cr2 * Xa + Sr2 * Y;
	Ya = Cr2 * Y  - Sr2 * Xa;
	Z  = Cr3 * Za - Sr3 * Ya;
	Y  = Sr3 * Za + Cr3 * Ya;
	X  = X + mX;
	Y  = Y + mY;
	Z  = Z + mZ;
	sX = Xmid + D * X / Z;
	sY = Ymid + D * Y / Z;
}

void DrawObject(void)
{
	int i;

	Rotation();
	for(i=0; i<Lines; i+=2)
	{
		X = World[i][0];
		Y = World[i][1];
		Z = World[i][2];
		Calc3D();
		sXs = sX;
		sYs = sY;

		X = World[i+1][0];
		Y = World[i+1][1];
		Z = World[i+1][2];
		Calc3D();

//		line(abs(sXs), abs(sYs), abs(sX), abs(sY));
		line(sXs, sYs, sX, sY);
	}
}

void InitProgram(void)
{
	int gdriver = EGA, gmode = EGAHI, errorcode;
	initgraph(&gdriver, &gmode, "");
	errorcode = graphresult();
	if (errorcode != grOk)
	{
		printf("%s\n", grapherrormsg(errorcode));
		exit(1);
	}
	Xmid = getmaxx() / 2;
	Ymid = getmaxy() / 2;

//	D  = 1200;      // View point and rotation values
	D  = 10000;
//	mZ = -850;
	mZ = -10000;
//	mX = -5;
	R1 = 0.3;
	R2 = 0.0;
	R3 = 0.3;
	G  = 0.3;
}

void MainLoop(void)
{
	int k, Demo=0;
	while((k=key()) != 27)
	{
		switch(k) {
		case 115: case 83: // S
			R1 += G;
			if(R1 > (2 * PI)) R1 = 0;
			break;
		case 101: case 69: // E
			R2 += G;
			if(R2 > (2 * PI)) R2 = 0;
			break;
		case 113: case 81: // Q
			R3 += G;
			if(R3 > (2 * PI)) R3 = 0;
			break;
		case 119: case 87: // W
			R1 -= G;
			if(R1 < 0) R1 = 2 * PI;
			break;
		case 100: case 68: // D
			R2 -= G;
			if(R2 < 0) R2 = 2 * PI;
			break;
		case 97: case 65:  // A
			R3 -= G;
			if(R3 < 0) R3 = 2 * PI;
			break;
		case 43:           // +
			if(D < 30000) D += 1000;
			break;
		case 45:           // -
			if(D > 1000) D -= 1000;
			break;
		case 13:           // Enter
			R1 = 0.3;
			R2 = 0.0;
			R3 = 0.3;
			D = 10000;
			break;
		case 32:           // Space
			Demo = 1 - Demo;
			break;
		default: break;
		}
		if(Demo == 1) {
			R += 0.1;
			if(R > (2 * PI)) R = 0;
			R1 = R2 = R3 = R;
		}

		clearviewport();
		DrawObject();
		Legende();
		setvisualpage(Page);
		Page = 1 - Page;
		setactivepage(Page);
	}
}

void Rotation(void)
{
	Sr1 = sin(R1);
	Sr2 = sin(R2);
	Sr3 = sin(R3);
	Cr1 = cos(R1);
	Cr2 = cos(R2);
	Cr3 = cos(R3);
}

extern int key(void)
{
	int k=0;
	if(kbhit()) {
		k = getch();
		if (k == 0)
			k = key();
	}
	return k;
}

void Legende(void)
{
	outtextxy( 10, 10,"Space: Run/Stop Demo");
	outtextxy( 10, 20,"Enter: Set to Zero");
	outtextxy( 10, 30,"+/-  : Zoom");
	outtextxy( 10, 40,"Esc  : Quit");
	outtextxy(470, 10,"Rotation:");
	outtextxy(550, 10,"Q  W  E");
	outtextxy(550, 20,"A  S  D");
	outtextxy( 10,330,COPYRIGHT);
	outtextxy(540,330,DATUM);
}