-
Notifications
You must be signed in to change notification settings - Fork 0
/
NEW3D.CPP
394 lines (335 loc) · 9.43 KB
/
NEW3D.CPP
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/********************************************************************/
/* */
/* 3D Lambert, Grouraud Shading Demo */
/* --------------------------------- */
/* */
/* This code should be subject to copyright (c) 1995 Paul Carroll */
/* It should also be deemed a breach of copyright */
/* to reproduce any of this code in any form without the */
/* author's (Paul Carroll) expressed premission. */
/* */
/* */
/* Copyright (c) 1995-97 Black Marble Productions */
/* */
/********************************************************************/
#include "global.h"
#include "objects.h"
#include "menu.h"
//============== Global Definitions ===============
char keyflags[128]; // Keyflags for 3d interactive loop
char shadingModel; // Shading model being used (ie Lambert)
char far *VirtScr; // Pointer to virtual screen
int sort_array[5];
int sorted_array[5];
char currObj; // Object being rendered
convexType userObject;
VertexType LightSource; // Light source origin !(0,0,0)
//=============== Local Definitions ================
#define UpArrow 0x4800
#define DownArrow 0x5000
#define PageDown 0x5100
#define PageUp 0x4900
#define Escape 0x011b
#define Enter 0x1c0d
#define MAX_DISP_LIST 10
#define NUM_OBJECTS 11
#define CURSOR_WIDTH 37
static const unsigned long *timer=(unsigned long *)0x46c;// Point to system timer
static unsigned long startTime,stopTime;// Used to measure fps
static int cursorX=15,cursorY=11;
static int objectAtTop=0;
static char *menuList[NUM_OBJECTS]={
"CUBE ",
"PYRAMID ",
"24 SIDED OBJECT",
"ROOK ",
"THING ",
"EPCOT1 ",
"THING2 ",
"SOCCER BALL ",
"STICKS ",
"BANANA ",
"MUSHROOM ",
};
//================== Start Code ==================
//
// Updates any convexType object
//
void updateObjects(convexType *object)
{
if(shadingModel==LAMBERT || shadingModel==WIREFRAME)
rotateNormals(object);
xFormPoints(object);
drawObject(object);
if((object->xAn+=object->xinc)>=360) object->xAn=0;
if((object->yAn+=object->yinc)>=360) object->yAn=0;
if((object->zAn+=object->zinc)>=360) object->zAn=0;
if(object->Owz<=object->tolerance) object->Owz+=object->zoom;
}
int comp_points(const void *a,const void *b)
{
if(sort_array[*(int*)a]>sort_array[*(int*)b])
{
return sort_array[*(int*)a];
}
else
{
return sort_array[*(int*)b];
}
}
//
// Loads an object from a specified array. Could be easily adapted
// to load from a file.
//
void loadObject(float *array,convexType *object,char conv,float mag)
{
int k,i,l;
object->numVerts=array[0];
object->numPolys=array[1];
object->Verts=new VertexType[object->numVerts+1];
object->Polys=new PolygonType[object->numPolys+1];
for(i=2,k=0;k<object->numVerts;k++,i+=3) // Read vertices
{
object->Verts[k].Ox=Float2Fixed(mag*array[i]);
object->Verts[k].Oy=Float2Fixed(mag*array[i+1]);
object->Verts[k].Oz=Float2Fixed(mag*array[i+2]);
}
for(k=0,i=(object->numVerts*3)+2;k<object->numPolys;k++)
{ // Setup polygons
object->Polys[k].numVerts=array[i];
for(l=0,i++;l<object->Polys[k].numVerts;l++,i++)
{
if(conv)
object->Polys[k].Verts[l]=array[i]-1;
else object->Polys[k].Verts[l]=array[i];
sort_array[l]=object->Polys[k].Verts[l];
}
sort_array[0]=4;
sort_array[1]=3;
sort_array[2]=1;
sort_array[3]=2;
qsort((void*)sorted_array,4,sizeof(sorted_array[0]),comp_points);
delay(0);
}
object->xAn=0; // Set rotation angles to 0
object->yAn=0;
object->zAn=0;
calcNormals(object); // Pre-calc normals to planes
// for use in Lambert shading
}
void doDemo(void)
{
char lastkey=0;
long frames=0;
char done,k;
int i;
asm mov ax,0x13 // Linear mode 13h
asm int 0x10
userObject.xinc=1;
userObject.yinc=1;
userObject.zinc=1;
userObject.tolerance=Int2Fixed(-25);
userObject.zoom=Int2Fixed(10);
userObject.Owx=0;
userObject.Owy=0;
userObject.Owz=Int2Fixed(-25);
userObject.colOfs=CYAN;
switch(currObj)
{
case 0 : loadObject(Cube,&userObject,0,.1); break;
case 1 : loadObject(Pyramid,&userObject,0,.1); break;
case 2 : loadObject(_24_Sided,&userObject,0,.2); break;
case 3 : loadObject(Rook,&userObject,1,7); break;
case 4 : loadObject(Thing,&userObject,1,2); break;
case 5 : loadObject(Epcot1,&userObject,1,3); break;
case 6 : loadObject(Thing2,&userObject,1,2); break;
case 7 : loadObject(SoccerBall,&userObject,1,3); break;
case 8 : loadObject(Sticks,&userObject,1,7); break;
case 9 : loadObject(Banana,&userObject,1,7); break;
case 10: loadObject(Mushroom,&userObject,1,7); break;
}
LightSource.Ox=Int2Fixed(10);
LightSource.Oy=Int2Fixed(10);
LightSource.Oz=Int2Fixed(10);
loadPCX("3D_BKGR.PCX",VirtScr,LOADPAL); // Load background PCX picture
Init3D();
shadingModel=LAMBERT; // Default shading to start on
setColour(0,9,4,12); // Set purple bg
for(k=15,i=1;i<38;i++,k++) // Define colour ranges for shading
{
setColour(i+RED ,k,0,0);
setColour(i+GREEN ,0,k,0);
setColour(i+BLUE ,0,0,k);
setColour(i+MAGENTA,k,0,k);
setColour(i+YELLOW ,k,k,0);
setColour(i+CYAN ,0,k,k);
}
set_ikb();
memset(keyflags,0,128);
startTime=*timer; // Get start time for fps rating
done=0;
while(!done)
{
memset(VirtScr+STARTOFS,0,SCRBYTES);
updateObjects(&userObject);
if(keyflags[lastkey]==0) // Check for any user
{ // keypresses. Don't check
if(keyflags[escape]==1) // if the last key has not
{ // been released yet!
done=1;
}
else if(keyflags[comma])
{
if(shadingModel==LAMBERT) shadingModel=WIREFRAME;
else shadingModel=LAMBERT;
lastkey=comma;
}
else if(keyflags[stop])
{
if(shadingModel==LAMBERT) shadingModel=WIREFRAME;
else shadingModel=LAMBERT;
lastkey=stop;
}
else if(keyflags[bigPlus] || keyflags[smallPlus])
{
if(userObject.colOfs<CYAN)
userObject.colOfs+=38;
else userObject.colOfs=0;
if(keyflags[bigPlus]==0) lastkey=smallPlus;
else lastkey=bigPlus;
}
else if(keyflags[bigMinus] || keyflags[smallMinus])
{
if(userObject.colOfs>RED)
userObject.colOfs-=38;
else userObject.colOfs=CYAN;
if(keyflags[bigMinus]==0) lastkey=smallMinus;
else lastkey=bigMinus;
}
else if(keyflags[pause])
{
while(keyflags[pause]);
}
else lastkey=0;
}
frames++;
putbuffer(VirtScr); // Using this routine instead of memcpy saves
// about 8 frames per second on my pc
}
stopTime=*timer; // Get end time for fps rating
reset_ikb();
delete userObject.Polys;
delete userObject.Verts;
asm mov ax,0x03
asm int 0x10
}
void PutCursor(void)
{
int x;
PutChar(cursorX,cursorY,'¯',0x4B);
PutChar(cursorX+CURSOR_WIDTH+1,cursorY,'®',0x4B);
for(x=cursorX;x<CURSOR_WIDTH+cursorX+1;x++)
{
ChangeAttr(x,cursorY,0x4B);
}
}
void EraseCursor(void)
{
int x;
PutChar(cursorX,cursorY,' ',0x0B);
PutChar(cursorX+CURSOR_WIDTH+1,cursorY,' ',0x0B);
for(x=cursorX;x<=CURSOR_WIDTH+cursorX;x++)
{
ChangeAttr(x,cursorY,0x0B);
}
}
void UpdateScreen()
{
int i;
textattr(0x0B);
for(i=0;i<=MAX_DISP_LIST;i++)
{
gotoxy(18,12+i);
cprintf(menuList[objectAtTop+i]);
}
PutCursor();
}
//=================== Main =====================
void main(void)
{
int key;
char done;
VirtScr=new char[64000]; // Allocate virtual screen
_setcursortype(_NOCURSOR);
setColour(4,14,14,14);
putTextScreen(MENU_DATA,0);
PutCursor();
UpdateScreen();
currObj=0;
done=0;
while(!done)
{
if(_bios_keybrd(_KEYBRD_READY)!=0)
{
key=_bios_keybrd(_KEYBRD_READ);
switch(key)
{
case Escape :
{
done=1;
break;
}
case UpArrow :
{
if(cursorY>11)
{
EraseCursor();
currObj--;
cursorY--;
UpdateScreen();
}
else {
if(objectAtTop>0)
{
currObj--;
objectAtTop--;
UpdateScreen();
}
}
break;
}
case DownArrow :
{
if(cursorY<11+MAX_DISP_LIST && (currObj+(cursorY-19))<NUM_OBJECTS)
{
EraseCursor();
cursorY++;
currObj++;
UpdateScreen();
}
else {
if((objectAtTop+MAX_DISP_LIST)<NUM_OBJECTS-1)
{
currObj++;
objectAtTop++;
UpdateScreen();
}
}
break;
}
case Enter :
{
doDemo();
_setcursortype(_NOCURSOR);
setColour(4,14,14,14);
putTextScreen(MENU_DATA,0);
PutCursor();
UpdateScreen();
}
}
}
}
delete VirtScr;
asm mov ax,0x03
asm int 0x10
}