home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Encyclopedia of History
/
DKMM_CEOH.iso
/
support
/
wing10
/
disk1
/
cube.cp_
/
cube.cp
Wrap
Text File
|
1997-04-01
|
19KB
|
679 lines
/**************************************************************************
CUBE.CPP - A spinning cube demo for WinG
**************************************************************************/
/**************************************************************************
(C) Copyright 1994 Microsoft Corp. All rights reserved.
You have a royalty-free right to use, modify, reproduce and
distribute the Sample Files (and/or any modified version) in
any way you find useful, provided that you agree that
Microsoft has no warranty obligations or liability for any
Sample Application Files which are modified.
**************************************************************************/
#include <windows.h>
#include <windowsx.h>
#include <wing.h>
#include "cube.hpp"
#include "dumb3d.hpp"
#include "..\utils\utils.h"
#if defined(WIN32)
#define _export
#endif
/**************************************************************************
Global Variables
**************************************************************************/
static char szAppName[]="Spinning Cube";
//*** Global Windows needs
static HINSTANCE hInstApp;
static BOOL fAppActive;
static HWND hwndApp;
static HPALETTE hpalApp = 0;
static HDC hdcWinG;
static HBITMAP OldBitmap;
static HBITMAP WinGBitmap;
struct
{
BITMAPINFOHEADER Header;
RGBQUAD aColorTable[256];
} HeaderAndPalette =
{
sizeof(BITMAPINFOHEADER),
50, 50,
1, 8,
BI_RGB,
0, 0, 0, 0, 0
};
static int DibWidth, DibHeight;
//*** Cube vertices, normals, shades, and modeling transform
static point_4 CubeVertices[8] =
{
point_4( -10, 10, -10 ),
point_4( -10, 10, 10 ),
point_4( 10, 10, 10 ),
point_4( 10, 10, -10 ),
point_4( 10, -10, -10 ),
point_4( 10, -10, 10 ),
point_4( -10, -10, 10 ),
point_4( -10, -10, -10 )
};
static vector_4 CubeSurfaceNormals[6];
static real CubeSurfaceShades[6];
static matrix_4x4 CubeTransform;
//*** Cube edges - ordered indices into the vertex array
const int CubeFaces[6][4] =
{
0, 1, 2, 3,
2, 1, 6, 5,
3, 2, 5, 4,
0, 3, 4, 7,
1, 0, 7, 6,
4, 5, 6, 7
};
//*** Cube colors - one RGB color per surface
const unsigned char CubeColors[6][3] =
{
240, 20, 20, // Unsaturated Red
20, 240, 20, // Unsaturated Green
20, 20, 240, // Unsaturated Blue
128, 64, 0, // Brown
240, 20, 240, // Unsaturated Magenta
240, 240, 20 // Unsaturated Yellow
};
//*** Lighting
vector_4 LightSourceDirection;
const real AmbientLight = 0.2;
//*** Viewing and perspective
static matrix_4x4 ViewPerspective;
static point_4 Viewpoint(60, 60, 60);
static vector_4 Up(0, 1, 0);
static point_4 Origin;
//*** Interaction
static real XMove,YMove;
static short gSpinFlag = 1;
//*** Dithering
static int DitherType = 0;
static int Monochrome = 0;
/**************************************************************************
Internal function declarations
**************************************************************************/
LONG FAR PASCAL _export AppWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
void AppExit(void);
BOOL AppIdle(void);
void AppPaint(HWND hwnd, HDC hdc);
void TransformCube(matrix_4x4 const &Transform);
void ProjectAndDrawCube(HDC hdc, int XOffset, int YOffset);
/**************************************************************************
AppAbout
Description:
This function handles messages belonging to the "About" dialog box.
The only message that it looks for is WM_COMMAND, indicating the user
has pressed the "OK" button.
**************************************************************************/
BOOL FAR PASCAL _export 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
Description:
This is called when the application is first loaded. It initializes
all variables, registers the window class, and creates the main app
window.
**************************************************************************/
BOOL AppInit(HINSTANCE hInst,HINSTANCE hPrev,int sw,LPSTR szCmdLine)
{
WNDCLASS cls;
/* Save instance handle for DialogBoxes */
hInstApp = hInst;
// Clear the System Palette so that WinG blting runs at full speed.
ClearSystemPalette();
if (!hPrev)
{
//*** Register a class for the main application window
cls.hCursor = LoadCursor(0,IDC_ARROW);
//*** Just for fun, we'll draw our own spinning cube icon.
cls.hIcon = 0;
cls.lpszMenuName = "AppMenu";
cls.lpszClassName = szAppName;
cls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
cls.hInstance = hInst;
cls.style = CS_BYTEALIGNCLIENT | CS_VREDRAW | CS_HREDRAW;
cls.lpfnWndProc = (WNDPROC)AppWndProc;
cls.cbClsExtra = 0;
cls.cbWndExtra = 0;
if (!RegisterClass(&cls))
return FALSE;
}
//*** Set and normalize the light source
LightSourceDirection = vector_4(50, 30, -15);
LightSourceDirection.Normalize();
//*** Distance to view plane:
ViewPerspective.SetElement(3, 2, 1/300.0);
ViewPerspective.SetElement(3, 3, 0);
//*** Viewport scaling - some arbitrary number like 3.5 will do
ViewPerspective.SetElement(0, 0, 3.5);
ViewPerspective.SetElement(1, 1, 3.5);
//*** Calculate the initial normals and shades
TransformCube(CubeTransform);
//*** Then generate an interesting rotation for the spin
CubeTransform.ConcatenateYRotation(6.0);
CubeTransform.ConcatenateXRotation(3.5);
CubeTransform.ConcatenateZRotation(2.0);
hwndApp = CreateWindow (szAppName, // Class name
szAppName, // Caption
WS_OVERLAPPED |
WS_CAPTION |
WS_SYSMENU |
WS_MINIMIZEBOX, // Style bits
CW_USEDEFAULT, 0, // Position
350,350, // Size
0, // Parent window (no parent)
0, // use class menu
hInst, // handle to window instance
0 // no params to pass on
);
hdcWinG = WinGCreateDC();
ShowWindow(hwndApp,sw);
//*** Check the default dither selection
HMENU hMenu = GetMenu(hwndApp);
CheckMenuItem(hMenu, MENU_DISPERSED8x8, MF_CHECKED);
CheckMenuItem(hMenu, MENU_SPIN, MF_CHECKED);
return TRUE;
}
/**************************************************************************
WinMain
Description:
The main procedure for the App. After initializing, it just goes
into a message-processing loop until it gets a WM_QUIT message.
**************************************************************************/
int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
{
MSG msg;
//*** Call initialization procedure
if (!AppInit(hInst,hPrev,sw,szCmdLine))
return FALSE;
//*** Polling messages from event queue until quit
for (;;)
{
if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
if (AppIdle())
WaitMessage();
}
}
return msg.wParam;
}
/**************************************************************************
AppIdle
Description:
**************************************************************************/
BOOL AppIdle()
{
//*** Spin while the app is active, lbutton is up, and spinning is on.
//*** Spin while the app is iconized.
if ( (gSpinFlag && fAppActive && GetKe