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
|
//-----------------------------------------------------------------------------
#define STRICT
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "geometry.h"
#include "resource.h"
//-----------------------------------------------------------------------------
// GLOBALS
//-----------------------------------------------------------------------------
HWND g_hWnd = NULL;
HDC g_hDC = NULL;
HGLRC g_hRC = NULL;
// Spin control for view
float g_fSpinX_L = 0.0f;
float g_fSpinY_L = -10.0f;
// Spin control for shadow casting quad
float g_fSpinX_R = 0.0f;
float g_fSpinY_R = 0.0f;
bool g_bRenderShadowVolume = false;
float g_fAmountOfExtrusion = 5.0f;
float g_lightPosition[] = { 2.0f, 6.0f, 0.0f, 1.0f }; // World position of light source
// GL_C3F_V3F
struct Vertex
{
float r, g, b;
float x, y, z;
};
Vertex g_shadowCasterVerts[] =
{
{ 1.0, 1.0, 1.0, -1.0f, 2.5f, -1.0f },
{ 1.0, 1.0, 1.0, -1.0f, 2.5f, 1.0f },
{ 1.0, 1.0, 1.0, 1.0f, 2.5f, 1.0f },
{ 1.0, 1.0, 1.0, 1.0f, 2.5f, -1.0f },
};
float g_shadowCasterNormal[] = { 0.0f, 1.0f, 0.0f };
struct ShadowCaster
{
Vertex *verts; // Vertices of the actual shadow casting object
float *normal; // A surface normal for lighting
int numVerts; // Total number of vertices
int shadowVolume; // Display list for holding the shadow volume
};
ShadowCaster g_shadowCaster;
//-----------------------------------------------------------------------------
// PROTOTYPES
//-----------------------------------------------------------------------------
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow);
LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
void loadTexture(void);
void init(void);
void render(void);
void shutDown(void);
void extendVertex(float newVert[3], float lightPosit[3], Vertex vert, float ext);
void buildShadowVolume(ShadowCaster *caster, float lightPosit[3], float ext);
//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow )
{
WNDCLASSEX winClass;
MSG uMsg;
memset(&uMsg,0,sizeof(uMsg));
winClass.lpszClassName = "MY_WINDOWS_CLASS";
winClass.cbSize = sizeof(WNDCLASSEX);
winClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
winClass.lpfnWndProc = WindowProc;
winClass.hInstance = hInstance;
winClass.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_OPENGL_ICON);
winClass.hIconSm = LoadIcon(hInstance, (LPCTSTR)IDI_OPENGL_ICON);
winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
winClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winClass.lpszMenuName = NULL;
winClass.cbClsExtra = 0;
winClass.cbWndExtra = 0;
if( !RegisterClassEx(&winClass) )
return E_FAIL;
g_hWnd = CreateWindowEx( NULL, "MY_WINDOWS_CLASS",
"OpenGL - Shadow Volume",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
0, 0, 640, 480, NULL, NULL, hInstance, NULL );
if( g_hWnd == NULL )
return E_FAIL;
ShowWindow( g_hWnd, nCmdShow );
UpdateWindow( g_hWnd );
init();
while( uMsg.message != WM_QUIT )
{
if( PeekMessage( &uMsg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &uMsg );
DispatchMessage( &uMsg );
}
else
render();
}
shutDown();
UnregisterClass( "MY_WINDOWS_CLASS", winClass.hInstance );
return uMsg.wParam;
}
//-----------------------------------------------------------------------------
// Name: WindowProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT CALLBACK WindowProc( HWND hWnd,
UINT msg,
WPARAM wParam,
LPARAM lParam )
{
static POINT ptLastMousePosit_L;
static POINT ptCurrentMousePosit_L;
static bool bMousing_L;
static POINT ptLastMousePosit_R;
static POINT ptCurrentMousePosit_R;
static bool bMousing_R;
switch( msg )
{
case WM_KEYDOWN:
{
switch( wParam )
{
case VK_ESCAPE:
PostQuitMessage(0);
break;
case VK_F1:
g_bRenderShadowVolume = !g_bRenderShadowVolume;
break;
case VK_F2:
g_fAmountOfExtrusion += 0.1f;
break;
case VK_F3:
g_fAmountOfExtrusion -= 0.1f;
break;
case 38: // Up Arrow Key
g_lightPosition[1] += 0.1f;
break;
case 40: // Down Arrow Key
g_lightPosition[1] -= 0.1f;
break;
case 37: // Left Arrow Key
g_lightPosition[0] -= 0.1f;
break;
case 39: // Right Arrow Key
g_lightPosition[0] += 0.1f;
break;
}
}
break;
case WM_LBUTTONDOWN:
{
ptLastMousePosit_L.x = ptCurrentMousePosit_L.x = LOWORD (lParam);
ptLastMousePosit_L.y = ptCurrentMousePosit_L.y = HIWORD (lParam);
bMousing_L = true;
}
break;
case WM_LBUTTONUP:
{
bMousing_L = false;
}
break;
case WM_RBUTTONDOWN:
{
ptLastMousePosit_R.x = ptCurrentMousePosit_R.x = LOWORD (lParam);
ptLastMousePosit_R.y = ptCurrentMousePosit_R.y = HIWORD (lParam);
bMousing_R = true;
}
break;
case WM_RBUTTONUP:
{
bMousing_R = false;
}
break;
case WM_MOUSEMOVE:
{
ptCurrentMousePosit_L.x = LOWORD (lParam);
ptCurrentMousePosit_L.y = HIWORD (lParam);
ptCurrentMousePosit_R.x = LOWORD (lParam);
ptCurrentMousePosit_R.y = HIWORD (lParam);
if( bMousing_L )
{
g_fSpinX_L -= (ptCurrentMousePosit_L.x - ptLastMousePosit_L.x);
g_fSpinY_L -= (ptCurrentMousePosit_L.y - ptLastMousePosit_L.y);
}
if( bMousing_R )
{
g_fSpinX_R -= (ptCurrentMousePosit_R.x - ptLastMousePosit_R.x);
g_fSpinY_R -= (ptCurrentMousePosit_R.y - ptLastMousePosit_R.y);
}
ptLastMousePosit_L.x = ptCurrentMousePosit_L.x;
ptLastMousePosit_L.y = ptCurrentMousePosit_L.y;
ptLastMousePosit_R.x = ptCurrentMousePosit_R.x;
ptLastMousePosit_R.y = ptCurrentMousePosit_R.y;
}
break;
case WM_SIZE:
{
int nWidth = LOWORD(lParam);
int nHeight = HIWORD(lParam);
glViewport(0, 0, nWidth, nHeight);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 45.0, (GLdouble)nWidth / (GLdouble)nHeight, 0.1, 100.0);
}
break;
case WM_CLOSE:
{
PostQuitMessage(0);
}
break;
case WM_DESTROY:
{
PostQuitMessage(0);
}
break;
default:
{
return DefWindowProc( hWnd, msg, wParam, lParam );
}
break;
}
return 0;
}
}
|