-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTEXTURE.CPP
1235 lines (995 loc) · 32.4 KB
/
TEXTURE.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*----------------------------------------------------------------------------
Texture Test Program - a cheesy test harness for texture mapping
by Chris Hecker for my Game Developer Magazine articles. See my homepage
for more information.
NOTE: This is a hacked test program, not a nice example of Windows programming.
The texture mappers are the only part of this you should look at.
This material is Copyright 1997 Chris Hecker, All Rights Reserved.
It's for you to read and learn from, not to put in your own articles
or books or on your website, etc. Thank you.
Chris Hecker
http://www.d6.com/users/checker
*/
#define WIN32_LEAN_AND_MEAN
#define WIN32_EXTRA_LEAN
#include <windows.h>
#include <windowsx.h>
#include <commdlg.h>
#include <stdlib.h>
#include <strstream>
#include <fstream>
#include <math.h>
#include <assert.h>
#include "texture.h"
#include "dumb3d.hpp"
#include "mappers.h"
#pragma warning (disable:4244) // conversion from float to int
/*----------------------------------------------------------------------------
Globals and declarations
*/
char szAppName[200] = "Cheesy Texture Test - ";
char *pMapperType;
HINSTANCE hInstApp;
HWND hwndApp;
HPALETTE hpalApp;
LONG FAR PASCAL AppWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
LONG AppCommand (HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
void AppExit(void);
BOOL AppIdle(void);
void TestMapper( void );
void Draw5000PixelTriangle( void );
int Timing = 0;
real const MinimumViewLength = 280.0;
real ViewLength = MinimumViewLength + 40.0;
vector_4 View(0,0,-1);
vector_4 Up(0,1,0);
vector_4 Right(1,0,0);
point_4 aVertices[5] =
{
point_4(-100, 100,0),
point_4( 100, 100,0),
point_4( 100,-100,0),
point_4(-100,-100,0),
point_4(-100, 100,0),
};
point_4 aScreenVertices[5];
struct info
{
BITMAPINFOHEADER Header;
RGBQUAD aColors[256];
};
info BufferInfo =
{
sizeof(BITMAPINFOHEADER),
300,300,
1,8,
};
info TextureInfo;
char unsigned *pTextureBits;
HBITMAP TextureBitmap;
HBITMAP OldBitmap;
HDC BufferDC;
HBITMAP BufferBitmap;
char unsigned *pBufferBits;
dib_info Texture;
dib_info Buffer;
struct
{
WORD Version;
WORD NumberOfEntries;
PALETTEENTRY aEntries[256];
} LogicalPalette =
{
0x300,
256
};
struct
{
char unsigned Red, Green, Blue;
} aStaticColors[20] =
{
{ 0,0,0 },
{ 128,0,0 },
{ 0,128,0 },
{ 128,128,0 },
{ 0,0,128 },
{ 128,0,128 },
{ 0,128,128 },
{ 192,192,192 },
{ 192,220,192 },
{ 166,202,240 },
{ 255,251,240 },
{ 160,160,164 },
{ 128,128,128 },
{ 255,0,0 },
{ 0,255,0 },
{ 255,255,0 },
{ 0,0,255 },
{ 255,0,255 },
{ 0,255,255 },
{ 255,255,255 },
};
/*----------------------------------------------------------------------------
dynamic menu structures - here's where you can add your own mappers and views
*/
struct mapper_entry;
typedef void mapper( dib_info const &Dest,
POINT3D const *pVertices, dib_info const &Texture );
typedef void mapper_init( mapper_entry &Entry );
typedef void mapper_setup( mapper_entry &Entry );
mapper_init Init_fl_fl;
mapper_setup Setup_fl_fl;
mapper_init Init_i_fl;
mapper_setup Setup_i_fl;
mapper_init Init_fx_fl;
mapper_setup Setup_fx_fl;
struct mapper_entry
{
mapper *pMapper;
mapper_init *pInit;
mapper_setup *pSetup;
char const *pDescription;
POINT3D aVertices[5];
} aMappers[] =
{
/* 0 */ {TextureMapTriangle_div_fl_fl, Init_fl_fl, Setup_fl_fl, "Div, fl, fl"},
/* 1 */ {TextureMapTriangle_div_i_fl, Init_i_fl, Setup_i_fl, "Div, int, fl"},
/* 2 */ {TextureMapTriangle_div_fx_fl, Init_fx_fl, Setup_fx_fl, "Div, fix, fl"},
/* 3 */ {TextureMapTriangle_suba_fx_fl, Init_fx_fl, Setup_fx_fl, "SubAff, fix, fl"},
/* 4 */ {TextureMapTriangle_suba_fx_fl_asm, Init_fx_fl, Setup_fx_fl, "SubAff, fix, fl - Asm"},
};
int const NumberOfMappers = sizeof(aMappers) / sizeof(aMappers[0]);
int MapperIndex = 3;
struct view_entry
{
view_entry( char const *p, real L, vector_4 V, vector_4 U, vector_4 R ) :
pDescription(p),Length(L),View(V),Up(U),Right(R) { }
char const *pDescription;
real Length;
vector_4 View, Up, Right;
} aViews[] =
{
view_entry("&Wall",ViewLength,vector_4(0.939693,0,-0.34202),vector_4(0,1,0),vector_4(0.34202,0,0.939693)),
view_entry("&Floor",ViewLength,vector_4(0,0.939694,-0.34202),vector_4(0,0.342019,0.939693),vector_4(1,0,0)),
view_entry("Wol&berg",ViewLength,vector_4(0.655004,0.529593,-0.538986),vector_4(0.456389,0.291221,0.840775),vector_4(0.602231,-0.796697,-0.0509494)),
// you can add your own views here, like I did, just break in the
// debugger and dump out he ViewLength, View, Up, and Right variables
// and put them another field
// view_entry("&Casey",320.0,vector_4(0.0348996,6.99192e-007,-0.999393),vector_4(0.000000,1.00000,1.22685e-006),vector_4(0.999391,-4.28163e-008,0.0348995)),
// view_entry("&Bad",320.0,vector_4(0.883587,0.0532488,-0.465236),vector_4(0.250669,-0.892966,0.373867),vector_4(-0.395530,-0.446964,-0.802359)),
};
int const NumberOfViews = sizeof(aViews) / sizeof(aViews[0]);
/*----------------------------------------------------------------------------*\
| AppAbout( hDlg, uiMessage, wParam, lParam ) |
| |
| Description: |
| This function handles messages belonging to the "About" dialog box. |
| The only message that it looks for is WM_COMMAND, indicating the use |
| has pressed the "OK" button. When this happens, it takes down |
| the dialog box. |
| |
| Arguments: |
| hDlg window handle of about dialog window |
| uiMessage message number |
| wParam message-dependent |
| lParam message-dependent |
| |
| Returns: |
| TRUE if message has been processed, else FALSE |
| |
\*----------------------------------------------------------------------------*/
BOOL FAR PASCAL AppAbout(HWND hwnd,UINT msg,WPARAM wParam,
LPARAM /* lParam */ )
{
switch (msg)
{
case WM_COMMAND:
if (LOWORD(wParam) == IDOK)
{
EndDialog(hwnd,TRUE);
}
break;
case WM_INITDIALOG:
return TRUE;
}
return FALSE;
}
/*----------------------------------------------------------------------------*\
| AppInit( hInst, hPrev) |
| |
| Description: |
| This is called when the application is first loaded into |
| memory. It performs all initialization that doesn't need to be done |
| once per instance. |
| |
| Arguments: |
| hInstance instance handle of current instance |
| hPrev instance handle of previous instance |
| |
| Returns: |
| TRUE if successful, FALSE if not |
| |
\*----------------------------------------------------------------------------*/
BOOL AppInit(HINSTANCE hInst,HINSTANCE hPrev,int sw,LPSTR szCmdLine)
{
WNDCLASS cls;
/* Save instance handle for DialogBoxs */
hInstApp = hInst;
if (!hPrev)
{
/*
* Register a class for the main application window
*/
cls.hCursor = LoadCursor(NULL,IDC_ARROW);
cls.hIcon = LoadIcon(hInst,"AppIcon");
cls.lpszMenuName = "AppMenu";
cls.lpszClassName = szAppName;
cls.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
cls.hInstance = hInst;
cls.style = CS_BYTEALIGNCLIENT | CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS;
cls.lpfnWndProc = (WNDPROC)AppWndProc;
cls.cbWndExtra = 0;
cls.cbClsExtra = 0;
if (!RegisterClass(&cls))
return FALSE;
}
// init palette and color table
HDC Screen = GetDC(0);
if(GetDeviceCaps(Screen,RASTERCAPS) & RC_PALETTE)
{
GetSystemPaletteEntries(Screen,0,10,LogicalPalette.aEntries);
GetSystemPaletteEntries(Screen,246,10,LogicalPalette.aEntries+246);
}
else
{
for(int Counter = 0;Counter < 10;Counter++)
{
LogicalPalette.aEntries[Counter].peRed =
aStaticColors[Counter].Red;
LogicalPalette.aEntries[Counter].peGreen =
aStaticColors[Counter].Green;
LogicalPalette.aEntries[Counter].peBlue =
aStaticColors[Counter].Blue;
LogicalPalette.aEntries[Counter+246].peRed =
aStaticColors[Counter+10].Red;
LogicalPalette.aEntries[Counter+246].peGreen =
aStaticColors[Counter+10].Green;
LogicalPalette.aEntries[Counter+246].peBlue =
aStaticColors[Counter+10].Blue;
}
}
ReleaseDC(0,Screen);
for(int Counter = 0;Counter < 10;Counter++)
{
BufferInfo.aColors[Counter].rgbRed =
LogicalPalette.aEntries[Counter].peRed;
BufferInfo.aColors[Counter+246].rgbRed =
LogicalPalette.aEntries[Counter+246].peRed;
BufferInfo.aColors[Counter].rgbGreen =
LogicalPalette.aEntries[Counter].peGreen;
BufferInfo.aColors[Counter+246].rgbGreen =
LogicalPalette.aEntries[Counter+246].peGreen;
BufferInfo.aColors[Counter].rgbBlue =
LogicalPalette.aEntries[Counter].peBlue;
BufferInfo.aColors[Counter+246].rgbBlue =
LogicalPalette.aEntries[Counter+246].peBlue;
BufferInfo.aColors[Counter].rgbReserved =
LogicalPalette.aEntries[Counter].peFlags = 0;
BufferInfo.aColors[Counter+246].rgbReserved =
LogicalPalette.aEntries[Counter+246].peFlags = 0;
}
for(int Counter = 10;Counter < 246;Counter++)
{
BufferInfo.aColors[Counter].rgbRed =
LogicalPalette.aEntries[Counter].peRed = Counter;
BufferInfo.aColors[Counter].rgbGreen =
LogicalPalette.aEntries[Counter].peGreen = Counter;
BufferInfo.aColors[Counter].rgbBlue =
LogicalPalette.aEntries[Counter].peBlue = Counter;
BufferInfo.aColors[Counter].rgbReserved = 0;
LogicalPalette.aEntries[Counter].peFlags = PC_RESERVED;
}
hpalApp = CreatePalette((LOGPALETTE *)&LogicalPalette);
// create bitmaps
BufferDC = CreateCompatibleDC(0);
BufferBitmap = CreateDIBSection(BufferDC,(BITMAPINFO *)&BufferInfo,
DIB_RGB_COLORS,(void **)&pBufferBits,0,0);
TextureInfo = BufferInfo;
TextureInfo.Header.biWidth = 102;
TextureInfo.Header.biHeight = 102;
TextureBitmap = CreateDIBSection(BufferDC,(BITMAPINFO *)&TextureInfo,
DIB_RGB_COLORS,(void **)&pTextureBits,0,0);
assert((TextureInfo.Header.biHeight > 0)
&& (BufferInfo.Header.biHeight > 0));
// set up two bottom-up DIBs for rendering
Texture.Width = TextureInfo.Header.biWidth;
Texture.Height = TextureInfo.Header.biHeight;
Texture.DeltaScan = (TextureInfo.Header.biWidth + 3) & ~3;
Texture.pBits = pTextureBits + Texture.DeltaScan *
(TextureInfo.Header.biHeight - 1);
Texture.DeltaScan *= -1;
Buffer.Width = BufferInfo.Header.biWidth;
Buffer.Height = BufferInfo.Header.biHeight;
Buffer.DeltaScan = (BufferInfo.Header.biWidth + 3) & ~3;
Buffer.pBits = pBufferBits + Buffer.DeltaScan *
(BufferInfo.Header.biHeight - 1);
Buffer.DeltaScan *= -1;
// draw checker board texture
OldBitmap = SelectBitmap(BufferDC,TextureBitmap);
HBRUSH Red = CreateSolidBrush(RGB(255,0,0));
HBRUSH Blue = CreateSolidBrush(RGB(50,50,255));
for(int X = 0;X < 100;X += 10)
{
for(int Y = 0;Y < 100;Y += 10)
{
if(((X + Y)/10)%2)
{
SelectObject(BufferDC,Blue);
}
else
{
SelectObject(BufferDC,Red);
}
PatBlt(BufferDC,X+1,Y+1,10,10,PATCOPY);
}
}
// draw two outlines so we can see when we fetch invalid texture coordinates
HPEN Pen = CreatePen(PS_SOLID,0,RGB(0,255,0));
Pen = SelectPen(BufferDC,Pen);
MoveToEx(BufferDC,0,0,0);
LineTo(BufferDC,101,0);
LineTo(BufferDC,101,101);
LineTo(BufferDC,0,101);
LineTo(BufferDC,0,0);
DeleteObject(SelectObject(BufferDC,Pen));
MoveToEx(BufferDC,1,1,0);
LineTo(BufferDC,100,1);
LineTo(BufferDC,100,100);
LineTo(BufferDC,1,100);
LineTo(BufferDC,1,1);
SelectBitmap(BufferDC,BufferBitmap);
// setup window size
DWORD Style = WS_OVERLAPPEDWINDOW;
RECT WindowRect = { 0, 0 };
WindowRect.right = BufferInfo.Header.biWidth;
WindowRect.bottom = BufferInfo.Header.biHeight;
AdjustWindowRect(&WindowRect,Style,TRUE);
hwndApp = CreateWindow(szAppName,szAppName,Style,
CW_USEDEFAULT,0,
WindowRect.right-WindowRect.left,
WindowRect.bottom-WindowRect.top,
0,0,hInst,0);
// init dynamic menus
HMENU hmenu = GetSubMenu(GetMenu(hwndApp),1);
DeleteMenu(hmenu,MENU_MAPPER,MF_BYCOMMAND);
for(int i=0; i<NumberOfMappers; i++)
{
(*aMappers[i].pInit)(aMappers[i]);
char aBuffer[100];
std::ostrstream Out(aBuffer,sizeof(aBuffer));
Out<<aMappers[i].pDescription<<"\t"<<i<<std::ends;
AppendMenu(hmenu,0,MENU_MAPPER+i,aBuffer);
}
hmenu = GetSubMenu(GetMenu(hwndApp),2);
DeleteMenu(hmenu,MENU_VIEW,MF_BYCOMMAND);
for(int i=0; i<NumberOfViews; i++)
{
AppendMenu(hmenu,0,MENU_VIEW+i,aViews[i].pDescription);
}
pMapperType = szAppName + strlen(szAppName);
SendMessage(hwndApp,WM_COMMAND,MENU_MAPPER,0);
ShowWindow(hwndApp,SW_SHOWNORMAL);
return TRUE;
}
/*----------------------------------------------------------------------------*\
| AppExit() |
| |
| Description: |
| app is just about to exit, cleanup |
| |
\*----------------------------------------------------------------------------*/
void AppExit()
{
SelectObject(BufferDC,OldBitmap);
DeleteObject(TextureBitmap);
DeleteObject(BufferBitmap);
DeleteDC(BufferDC);
DeleteObject(hpalApp);
}
/*----------------------------------------------------------------------------*\
| WinMain( hInst, hPrev, lpszCmdLine, cmdShow ) |
| |
| Description: |
| The main procedure for the App. After initializing, it just goes |
| into a message-processing loop until it gets a WM_QUIT message |
| (meaning the app was closed). |
| |
| Arguments: |
| hInst instance handle of this instance of the app |
| hPrev instance handle of previous instance, NULL if first |
| szCmdLine ->null-terminated command line |
| cmdShow specifies how the window is initially displayed |
| |
| Returns: |
| The exit code as specified in the WM_QUIT message. |
| |
\*----------------------------------------------------------------------------*/
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev,
PSTR lpCmdLine, int nCmdShow)
{
MSG msg;
/* Call initialization procedure */
if (!AppInit(hInst,hPrev,nCmdShow,lpCmdLine))
return FALSE;
while(GetMessage(&msg,0,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
AppExit();
return msg.wParam;
}
/*----------------------------------------------------------------------------*\
| AppPaint(hwnd, hdc) |
| |
| Description: |
| The paint function. Right now this does nothing. |
| |
| Arguments: |
| hwnd window painting into |
| hdc display context to paint to |
| |
| Returns: |
| nothing |
| |
\*----------------------------------------------------------------------------*/
bool AppPaint (HWND hwnd, HDC hdc)
{
RECT rc;
GetClientRect(hwnd, &rc);
PatBlt(BufferDC,0,0,BufferInfo.Header.biWidth,BufferInfo.Header.biHeight,
WHITENESS);
int Counter;
real ViewDistance = 25.0;
matrix_4x4 ViewTransform(PointFrom(-View * ViewLength),Right,Up,View);
SetViewDistance(ViewTransform,ViewDistance);
ViewTransform.ConcatenateXScale(10);
ViewTransform.ConcatenateYScale(-10);
ViewTransform.ConcatenateXTranslation(BufferInfo.Header.biWidth/2.0);
ViewTransform.ConcatenateYTranslation(BufferInfo.Header.biHeight/2.0);
for(Counter = 0;Counter < 5;Counter++)
{
aScreenVertices[Counter] = ViewTransform * aVertices[Counter];
aScreenVertices[Counter].Homogenize();
}
MoveToEx(BufferDC,aScreenVertices[0].GetX(),aScreenVertices[0].GetY(),0);
// turn this on if you want to see which vertices are which
#if 0
for(Counter = 1;Counter < 5;Counter++)
{
// draw the edges
LineTo(BufferDC,aScreenVertices[Counter].GetX(),
aScreenVertices[Counter].GetY());
// label the vertices
TextOut(BufferDC,aScreenVertices[Counter].GetX(),
aScreenVertices[Counter].GetY(),"ABCDA"+Counter,1);
}
#endif
// call the mapper setup and the draw the polygon
(*aMappers[MapperIndex].pSetup)(aMappers[MapperIndex]);
(*aMappers[MapperIndex].pMapper)(Buffer,aMappers[MapperIndex].aVertices,
Texture);
(*aMappers[MapperIndex].pMapper)(Buffer,aMappers[MapperIndex].aVertices+2,
Texture);
#if 0
// XOR test with last mapper
(*aMappers[NumberOfMappers-1].pSetup)(aMappers[NumberOfMappers-1]);
(*aMappers[NumberOfMappers-1].pMapper)(Buffer,
aMappers[NumberOfMappers-1].aVertices,Texture);
(*aMappers[NumberOfMappers-1].pMapper)(Buffer,
aMappers[NumberOfMappers-1].aVertices+2,Texture);
#endif
// update the display
BitBlt(hdc,0,0,rc.right,rc.bottom,BufferDC,0,0,SRCCOPY);
return true;
}
/*----------------------------------------------------------------------------*\
| AppWndProc( hwnd, uiMessage, wParam, lParam ) |
| |
| Description: |
| The window proc for the app's main (tiled) window. This processes all |
| of the parent window's messages. |
| |
\*----------------------------------------------------------------------------*/
LONG FAR PASCAL AppWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
BOOL f;
static int LastX, LastY;
switch (msg)
{
case WM_CREATE:
break;
case WM_KEYDOWN:
{
real const Step = RadiansFrom(0.5); // 2.0);
switch(wParam)
{
case VK_PRIOR:
case VK_NEXT:
{
// rotate around the view vector
real Radians = (wParam == VK_PRIOR) ? -Step : Step;
matrix_4x4 Rotation(View,Radians);
Up = Rotation * Up;
Right = Rotation * Right;
break;
}
case VK_LEFT:
case VK_RIGHT:
{
// rotate around the up vector
real Radians = (wParam == VK_LEFT) ? -Step : Step;
matrix_4x4 Rotation(Up,Radians);
Right = Rotation * Right;
View = Rotation * View;
break;
}
case VK_UP:
case VK_DOWN:
{
// rotate around the right vector
real Radians = (wParam == VK_DOWN) ? -Step : Step;
matrix_4x4 Rotation(Right,Radians);
Up = Rotation * Up;
View = Rotation * View;
break;
}
case 'I':
{
ViewLength *= 0.9;
if(ViewLength < MinimumViewLength)
{
ViewLength = MinimumViewLength;
}
break;
}
case 'O':
{
ViewLength *= 1.1;
break;
}
case 'A':
{
SendMessage(hwnd,WM_COMMAND,MENU_ANIMATE,0);
break;
}
case 'P':
{
SendMessage(hwnd,WM_COMMAND,MENU_PIXELSPERSEC,0);
break;
}
case 'T':
{
if(GetKeyState(VK_CONTROL) & 0x80000000)
{
SendMessage(hwnd,WM_COMMAND,MENU_TEST,0);
return 0; // don't repaint
}
else
{
LARGE_INTEGER StartTime, EndTime, Frequency;
int const Iterations = 500;
QueryPerformanceFrequency(&Frequency);
QueryPerformanceCounter(&StartTime);
for(int Counter = 0;Counter < Iterations;Counter++)
{
(*aMappers[MapperIndex].pMapper)(Buffer,aMappers[MapperIndex].aVertices,
Texture);
}
QueryPerformanceCounter(&EndTime);
char aBuffer[100];
std::ostrstream Out(aBuffer,sizeof(aBuffer));
double SecondsPerIteration =
(double(EndTime.LowPart - StartTime.LowPart) /
double(Frequency.LowPart)) /
double(Iterations);
double CyclesPerSecond = 133000000.0;
Out<<"Microseconds: "<<SecondsPerIteration / 1000000.0
<<" Cycles: "<<SecondsPerIteration * CyclesPerSecond<<std::ends;
MessageBox(0,aBuffer,"Timing",MB_OK);
// SendMessage(hwnd,WM_COMMAND,MENU_TIME,0);
}
break;
}
default:
{
if((wParam >= '0') && (wParam < ('0' + NumberOfMappers)))
{
SendMessage(hwnd,WM_COMMAND,MENU_MAPPER+wParam-'0',0);
}
break;
}
}
InvalidateRect(hwnd,0,FALSE);
UpdateWindow(hwnd);
break;
}
case WM_LBUTTONDOWN:
{
LastX = (short)LOWORD(lParam);
LastY = (short)HIWORD(lParam);
SetCapture(hwnd);
break;
}
case WM_MOUSEMOVE:
{
if(wParam & MK_LBUTTON)
{
int X = (short)LOWORD(lParam);
int Y = (short)HIWORD(lParam);
int DeltaX = LastX - X;
int DeltaY = LastY - Y;
if(DeltaX)
{
// rotate around the up vector
real Radians = RadiansFrom(DeltaX);
matrix_4x4 Rotation(Up.Normalize(),Radians);
Right = Rotation * Right;
View = Rotation * View;
}
if(DeltaY)
{
// rotate around the right vector
real Radians = RadiansFrom(DeltaY);
matrix_4x4 Rotation(Right.Normalize(),-Radians);
Up = Rotation * Up;
View = Rotation * View;
}
LastX = X;
LastY = Y;
InvalidateRect(hwnd,0,FALSE);
UpdateWindow(hwnd);
}
break;
}
case WM_LBUTTONUP:
{
ReleaseCapture();
break;
}
case WM_ERASEBKGND:
break;
case WM_INITMENU:
break;
case WM_COMMAND:
return AppCommand(hwnd,msg,wParam,lParam);
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_CLOSE:
break;
case WM_PALETTECHANGED:
if ((HWND)wParam == hwnd)
break;
// fall through to WM_QUERYNEWPALETTE
case WM_QUERYNEWPALETTE:
hdc = GetDC(hwnd);
if (hpalApp)
SelectPalette(hdc, hpalApp, FALSE);
f = RealizePalette(hdc);
ReleaseDC(hwnd,hdc);
if (f)
InvalidateRect(hwnd,NULL,FALSE);
return f;
case WM_PAINT:
hdc = BeginPaint(hwnd,&ps);
if (hpalApp)
SelectPalette(hdc, hpalApp, FALSE);
f = RealizePalette(hdc);
AppPaint (hwnd,hdc);
EndPaint(hwnd,&ps);
return 0L;
case WM_SIZE:
break;
}
return DefWindowProc(hwnd,msg,wParam,lParam);
}
/*----------------------------------------------------------------------------*\
| AppCommand(hwnd, msg, wParam, lParam ) |
| |
| Description: |
| handles WM_COMMAND messages for the main window (hwndApp) |
| of the parent window's messages. |
| |
\*----------------------------------------------------------------------------*/
LONG AppCommand (HWND hwnd,UINT /* msg */,WPARAM wParam,LPARAM /* lParam */)
{
switch(wParam)
{
case MENU_ABOUT:
// DialogBox(hInstApp,"AppAbout",hwnd,AppAbout);
break;
case MENU_EXIT:
PostMessage(hwnd,WM_CLOSE,0,0L);
break;
case MENU_TEST:
TestMapper();
break;
case MENU_ANIMATE:
{
HDC Display = GetDC(hwnd);
SelectPalette(Display,hpalApp,FALSE);
RealizePalette(Display);
for(int Counter = 0;Counter < 100;Counter++)
{
matrix_4x4 Rotation(Up,RadiansFrom(0.5));
Right = Rotation * Right;
View = Rotation * View;
AppPaint(hwnd,Display);
}
ReleaseDC(hwnd,Display);
break;
}
case MENU_PIXELSPERSEC:
{
HDC Display = GetDC(hwnd);
SelectPalette(Display,hpalApp,FALSE);
RealizePalette(Display);
PatBlt(BufferDC,0,0,BufferInfo.Header.biWidth,
BufferInfo.Header.biHeight,
WHITENESS);
LARGE_INTEGER StartTime, EndTime, Frequency;
int const Iterations = 100;
int const Pixels = 5000;
QueryPerformanceFrequency(&Frequency);
QueryPerformanceCounter(&StartTime);
for(int Counter = 0;Counter < Iterations;Counter++)
{
Draw5000PixelTriangle();
}
QueryPerformanceCounter(&EndTime);
BitBlt(Display,0,0,Buffer.Width,Buffer.Height,BufferDC,0,0,SRCCOPY);
Timing = 0;
char aBuffer[100];
std::ostrstream Out(aBuffer,sizeof(aBuffer));
Out<<"Pixels/Sec ("<<Pixels<<" pixel triangles): "
<<(double(Iterations) * Pixels) /
(double(EndTime.LowPart - StartTime.LowPart) /
double(Frequency.LowPart))<<std::ends;
MessageBox(0,aBuffer,"Pixels Per Second",MB_OK);
ReleaseDC(hwnd,Display);
break;
}
case MENU_TIME:
{
HDC Display = GetDC(hwnd);
SelectPalette(Display,hpalApp,FALSE);
RealizePalette(Display);
LARGE_INTEGER StartTime, EndTime, Frequency;
int const Iterations = 25;
QueryPerformanceFrequency(&Frequency);
QueryPerformanceCounter(&StartTime);
for(int Counter = 0;Counter < Iterations;Counter++)
{
(*aMappers[MapperIndex].pMapper)(Buffer,
aMappers[MapperIndex].aVertices,Texture);
(*aMappers[MapperIndex].pMapper)(Buffer,
aMappers[MapperIndex].aVertices+2,Texture);
}
QueryPerformanceCounter(&EndTime);
Timing = 0;
char aBuffer[100];
std::ostrstream Out(aBuffer,sizeof(aBuffer));
Out<<"FPS: "<<double(Iterations) /
(double(EndTime.LowPart - StartTime.LowPart) /
double(Frequency.LowPart))<<std::ends;
MessageBox(0,aBuffer,"Frames Per Second",MB_OK);
ReleaseDC(hwnd,Display);
break;
}
default:
{
if((wParam >= MENU_MAPPER) &&
(wParam < (MENU_MAPPER + NumberOfMappers)))
{
std::ostrstream Out(pMapperType,
sizeof(szAppName) - (szAppName - pMapperType));
Out<<aMappers[wParam-MENU_MAPPER].pDescription<<std::ends;
SetWindowText(hwnd,szAppName);
CheckMenuItem(GetMenu(hwnd),MENU_MAPPER+MapperIndex,