[2242] | 1 | //--------------------------------------------------------------------------------------
|
---|
| 2 | // File: MultipleReflections.cpp
|
---|
| 3 | //
|
---|
| 4 | // Empty starting point for new Direct3D applications
|
---|
| 5 | //
|
---|
| 6 | // Copyright (c) Microsoft Corporation. All rights reserved.
|
---|
| 7 | //--------------------------------------------------------------------------------------
|
---|
| 8 | #include "dxstdafx.h"
|
---|
| 9 | #include "resource.h"
|
---|
| 10 | #include "Mesh.h"
|
---|
| 11 | #include "utilFunctions.h"
|
---|
| 12 |
|
---|
| 13 | //-------------------------------------------------------------------------
|
---|
| 14 | // D3dDevice and UI elements
|
---|
| 15 | IDirect3DDevice9* g_pd3dDevice; ///< THE D3D DEVICE
|
---|
| 16 | CDXUTDialogResourceManager g_DialogResourceManager; // manager for shared resources of dialogs
|
---|
| 17 | CDXUTDialog g_HUD; // dialog for standard controls
|
---|
| 18 | CDXUTComboBox* g_objectMaterialComboBox;
|
---|
| 19 | CDXUTComboBox* g_objectModelComboBox;
|
---|
| 20 | ID3DXFont* g_pFont; ///< Font for drawing text
|
---|
| 21 | ID3DXSprite* g_pTextSprite; ///< Sprite for batching draw text calls
|
---|
[2244] | 22 | bool g_showWelcomeScreen = true;
|
---|
[2242] | 23 | //slider ids
|
---|
| 24 | #define FIRST_SLIDER 10
|
---|
| 25 | #define LINEAR_MIN_ITERATION_SLIDER 10
|
---|
| 26 | #define LINEAR_MAX_ITERATION_SLIDER 11
|
---|
| 27 | #define SECANT_ITERATION_SLIDER 12
|
---|
| 28 | #define MAX_RAY_DEPTH_SLIDER 13
|
---|
| 29 | #define CUBEMAP_UPDATE_INTERVAL 14
|
---|
| 30 | #define OBJECT_SIZE 15
|
---|
| 31 | #define REFRACT_FACTOR_SLIDER 16
|
---|
| 32 | #define FRESHEL_RED_SLIDER 17
|
---|
| 33 | #define FRESHEL_GREEN_SLIDER 18
|
---|
| 34 | #define FRESHEL_BLUE_SLIDER 19
|
---|
| 35 | #define LAST_SLIDER 19
|
---|
| 36 |
|
---|
| 37 | //combo box ids
|
---|
| 38 | #define OBJECT_MATERIAL_COMBO 20
|
---|
| 39 | #define OBJECT_MODEL_COMBO 21
|
---|
| 40 | //slider values
|
---|
| 41 | int g_linearMinIter = 5;
|
---|
| 42 | int g_linearMaxIter = 30;
|
---|
| 43 | int g_secantIter = 1;
|
---|
| 44 | int g_rayDepth = 2;
|
---|
| 45 | int g_iSelectedMenuItem = 0;
|
---|
| 46 | int g_iObjectSize = 30;
|
---|
| 47 | float g_objectSize = 0.3;
|
---|
| 48 | bool g_showHelp = false;
|
---|
| 49 | bool g_showGUI = true;
|
---|
| 50 | unsigned int g_updateInterval = 0;
|
---|
| 51 | unsigned int g_currentFrame = 0;
|
---|
| 52 | int g_currentMaterial = 0;
|
---|
| 53 | int g_iRefractFactor = 0;
|
---|
| 54 | int g_iFreshnelR = 80;
|
---|
| 55 | int g_iFreshnelG = 80;
|
---|
| 56 | int g_iFreshnelB = 80;
|
---|
| 57 | float g_refractFactor = -0.01f;
|
---|
| 58 | float g_freshnelR = 0.8f;
|
---|
| 59 | float g_freshnelG = 0.8f;
|
---|
| 60 | float g_freshnelB = 0.8f;
|
---|
| 61 |
|
---|
| 62 | //---------------------------------------------------------------
|
---|
| 63 | // Mesh data
|
---|
| 64 | #define ENVIRONMENT_MESH L"Media\\Objects\\cube.x"
|
---|
| 65 | #define ENVIRONMENT_MESH_TEXTURE L"Media\\Textures\\cubecolor.png"
|
---|
| 66 | #define MESH_TEXTURE L"Media\\Textures\\white.png"
|
---|
| 67 | LPCWSTR MESHNAMES[] = { L"Media\\Objects\\sphere.x",
|
---|
| 68 | L"Media\\Objects\\teapot.x",
|
---|
| 69 | L"Media\\Objects\\column.x"};
|
---|
| 70 | Mesh* g_environmentMesh;
|
---|
| 71 | Mesh* g_objectMesh;
|
---|
| 72 |
|
---|
| 73 | //----------------------------------------------------------------
|
---|
| 74 | // shader program data
|
---|
| 75 | ID3DXEffect* g_basicShaders;
|
---|
| 76 | ID3DXEffect* g_reflectionShaders;
|
---|
| 77 |
|
---|
| 78 | //---------------------------------------------------------------
|
---|
| 79 | //Camera settings
|
---|
| 80 | CModelViewerCamera camera; ///< the camera
|
---|
| 81 | D3DXVECTOR3 cameraPosition(0,0,-2);
|
---|
| 82 | D3DXVECTOR3 cameraLookat(0,0,0);
|
---|
| 83 | unsigned int g_width = 800;
|
---|
| 84 | unsigned int g_height = 600;
|
---|
| 85 |
|
---|
| 86 | //-----------------------------------------------------------------
|
---|
| 87 | //RenderTargets
|
---|
| 88 | unsigned int g_cubeMapSize = 512;
|
---|
| 89 | //normal and distance information of the center object's front facing polygons
|
---|
| 90 | IDirect3DCubeTexture9* g_CubeTexture1 = NULL;
|
---|
| 91 | //normal and distance information of the center object's back facing polygons
|
---|
| 92 | IDirect3DCubeTexture9* g_CubeTexture2 = NULL;
|
---|
| 93 | //color and distance information of the environment (distant non reflective objects)
|
---|
| 94 | IDirect3DCubeTexture9* g_CubeTexture3 = NULL;
|
---|
| 95 |
|
---|
| 96 | //-----------------------------------------------------------------
|
---|
| 97 | //function forward declarations
|
---|
| 98 | void CALLBACK OnGUIEvent( UINT nEvent, int nControlID, CDXUTControl* pControl, void* pUserContext );
|
---|
| 99 | void UpdateParamFromSlider();
|
---|
| 100 |
|
---|
| 101 | //--------------------------------------------------------------------------------------
|
---|
| 102 | // Rejects any devices that aren't acceptable by returning false
|
---|
| 103 | //--------------------------------------------------------------------------------------
|
---|
| 104 | bool CALLBACK IsDeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat,
|
---|
| 105 | D3DFORMAT BackBufferFormat, bool bWindowed, void* pUserContext )
|
---|
| 106 | {
|
---|
| 107 | // Typically want to skip backbuffer formats that don't support alpha blending
|
---|
| 108 | IDirect3D9* pD3D = DXUTGetD3DObject();
|
---|
| 109 | if( FAILED( pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal, pCaps->DeviceType,
|
---|
| 110 | AdapterFormat, D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING,
|
---|
| 111 | D3DRTYPE_TEXTURE, BackBufferFormat ) ) )
|
---|
| 112 | return false;
|
---|
| 113 |
|
---|
| 114 | return true;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 |
|
---|
| 118 | //--------------------------------------------------------------------------------------
|
---|
| 119 | // Before a device is created, modify the device settings as needed
|
---|
| 120 | //--------------------------------------------------------------------------------------
|
---|
| 121 | bool CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, const D3DCAPS9* pCaps, void* pUserContext )
|
---|
| 122 | {
|
---|
| 123 | //VSync Off
|
---|
| 124 | pDeviceSettings->pp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
|
---|
| 125 | return true;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[2244] | 128 | void compileShaders()
|
---|
[2242] | 129 | {
|
---|
| 130 | HRESULT hr;
|
---|
| 131 | //load effect files
|
---|
| 132 | DWORD dwShaderFlags = 0;
|
---|
| 133 | ID3DXBuffer* errBuff; // buffer for error message
|
---|
| 134 | if (FAILED(hr = D3DXCreateEffectFromFile( g_pd3dDevice, L"Media\\Shaders\\basicShaders.fx", NULL, NULL, dwShaderFlags,
|
---|
| 135 | NULL, &g_basicShaders, &errBuff ))) // if compilation error occurs
|
---|
| 136 | {
|
---|
| 137 | int BufSize = errBuff->GetBufferSize();
|
---|
| 138 |
|
---|
| 139 | wchar_t* wbuf = new wchar_t[BufSize];
|
---|
| 140 | mbstowcs( wbuf, (const char*)errBuff->GetBufferPointer(), BufSize );
|
---|
| 141 | MessageBox(NULL, wbuf, L".fx Compilation Error", MB_ICONERROR); // error message
|
---|
| 142 |
|
---|
| 143 | delete wbuf;
|
---|
| 144 | exit(-1);
|
---|
| 145 | }
|
---|
| 146 | dwShaderFlags += D3DXSHADER_PREFER_FLOW_CONTROL;
|
---|
| 147 | if (FAILED(hr = D3DXCreateEffectFromFile( g_pd3dDevice, L"Media\\Shaders\\MultipleReflection.fx", NULL, NULL, dwShaderFlags,
|
---|
| 148 | NULL, &g_reflectionShaders, &errBuff ))) // if compilation error occurs
|
---|
| 149 | {
|
---|
| 150 | int BufSize = errBuff->GetBufferSize();
|
---|
| 151 |
|
---|
| 152 | wchar_t* wbuf = new wchar_t[BufSize];
|
---|
| 153 | mbstowcs( wbuf, (const char*)errBuff->GetBufferPointer(), BufSize );
|
---|
| 154 | MessageBox(NULL, wbuf, L".fx Compilation Error", MB_ICONERROR); // error message
|
---|
| 155 |
|
---|
| 156 | delete wbuf;
|
---|
| 157 | exit(-1);
|
---|
| 158 | }
|
---|
[2244] | 159 | }
|
---|
| 160 | //--------------------------------------------------------------------------------------
|
---|
| 161 | // Create any D3DPOOL_MANAGED resources here
|
---|
| 162 | //--------------------------------------------------------------------------------------
|
---|
| 163 | HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
|
---|
| 164 | {
|
---|
| 165 | HRESULT hr;
|
---|
[2242] | 166 |
|
---|
[2244] | 167 | g_pd3dDevice = pd3dDevice;
|
---|
| 168 | g_DialogResourceManager.OnCreateDevice( pd3dDevice );
|
---|
| 169 |
|
---|
| 170 | //load meshes
|
---|
| 171 | g_environmentMesh = new Mesh(ENVIRONMENT_MESH, ENVIRONMENT_MESH_TEXTURE, 1, D3DXVECTOR3(0,0,0));
|
---|
| 172 | D3DXVECTOR3 environmentSize = g_environmentMesh->GetMeshSize();
|
---|
| 173 | g_objectMesh = new Mesh(MESHNAMES[0], MESH_TEXTURE, g_objectSize, D3DXVECTOR3(0,0,0));
|
---|
| 174 | g_objectMesh->SetContainerSize(environmentSize);
|
---|
| 175 |
|
---|
[2242] | 176 | // Initialize the font
|
---|
| 177 | V_RETURN( D3DXCreateFont( g_pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET,
|
---|
| 178 | OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
|
---|
| 179 | L"Arial", &g_pFont ) );
|
---|
| 180 | return S_OK;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 |
|
---|
| 184 | //--------------------------------------------------------------------------------------
|
---|
| 185 | // Create any D3DPOOL_DEFAULT resources here
|
---|
| 186 | //--------------------------------------------------------------------------------------
|
---|
| 187 | HRESULT CALLBACK OnResetDevice( IDirect3DDevice9* pd3dDevice,
|
---|
| 188 | const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
|
---|
| 189 | {
|
---|
| 190 | HRESULT hr;
|
---|
| 191 | //create cubemap rendertargets
|
---|
| 192 | g_CubeTexture1 = CreateCubeTexture( g_cubeMapSize, D3DFMT_A16B16G16R16F, g_pd3dDevice);
|
---|
| 193 | g_CubeTexture2 = CreateCubeTexture( g_cubeMapSize, D3DFMT_A16B16G16R16F, g_pd3dDevice);
|
---|
| 194 | g_CubeTexture3 = CreateCubeTexture( g_cubeMapSize, D3DFMT_A16B16G16R16F, g_pd3dDevice);
|
---|
| 195 |
|
---|
| 196 | g_DialogResourceManager.OnResetDevice();
|
---|
| 197 | if( g_basicShaders ) V_RETURN( g_basicShaders->OnResetDevice() );
|
---|
| 198 | if( g_reflectionShaders ) V_RETURN( g_reflectionShaders->OnResetDevice() );
|
---|
| 199 | if( g_pFont ) g_pFont->OnResetDevice();
|
---|
| 200 | camera.SetProjParams(3.14 / 4.0, (float)g_width / (float)g_height, 0.01, 20);
|
---|
| 201 | g_currentFrame = 0;
|
---|
| 202 | return S_OK;
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 |
|
---|
| 206 | //--------------------------------------------------------------------------------------
|
---|
| 207 | // Handle updates to the scene
|
---|
| 208 | //--------------------------------------------------------------------------------------
|
---|
| 209 | void CALLBACK OnFrameMove( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
|
---|
| 210 | {
|
---|
| 211 | camera.FrameMove( fElapsedTime );
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | //--------------------------------------------------------------------------------------
|
---|
| 215 | // Sets matrices and renders a mesh
|
---|
| 216 | //--------------------------------------------------------------------------------------
|
---|
| 217 | void renderMesh(Mesh* mesh, D3DXMATRIXA16& mView, D3DXMATRIXA16& mProj, ID3DXEffect* effect)
|
---|
| 218 | {
|
---|
| 219 | D3DXMATRIXA16 rotationMatrix = mesh->getRotation();
|
---|
| 220 | D3DXMATRIXA16 scaleOffsetMatrix = ScaleAndOffset(mesh->GetMeshScale(), mesh->GetMeshPosition());
|
---|
| 221 | D3DXMATRIXA16 worldMatrix;
|
---|
| 222 | D3DXMatrixMultiply(&worldMatrix, &rotationMatrix, &scaleOffsetMatrix);
|
---|
| 223 | setWorldViewProj( worldMatrix, mView, mProj, effect);
|
---|
| 224 | mesh->Draw();
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | //--------------------------------------------------------------------------------------
|
---|
| 228 | // Renders a mesh to all 6 faces of a cubemap
|
---|
| 229 | //--------------------------------------------------------------------------------------
|
---|
| 230 | void renderEnvMap(Mesh* mesh, IDirect3DCubeTexture9* target, ID3DXEffect* effect)
|
---|
| 231 | {
|
---|
| 232 | IDirect3DSurface9* oldRenderTarget;
|
---|
| 233 | IDirect3DSurface9* oldDepthStencil;
|
---|
| 234 | IDirect3DSurface9* newDepthStencil;
|
---|
| 235 | // create a CUBEMAP_SIZE x CUBEMAP_SIZE size depth buffer
|
---|
| 236 | g_pd3dDevice->CreateDepthStencilSurface(g_cubeMapSize, g_cubeMapSize, D3DFMT_D16,
|
---|
| 237 | D3DMULTISAMPLE_NONE, 0, true, &newDepthStencil, NULL);
|
---|
| 238 | // replace old depth buffer
|
---|
| 239 | g_pd3dDevice->GetDepthStencilSurface(&oldDepthStencil);
|
---|
| 240 | g_pd3dDevice->SetDepthStencilSurface(newDepthStencil);
|
---|
| 241 | g_pd3dDevice->GetRenderTarget(0,&oldRenderTarget);
|
---|
| 242 |
|
---|
| 243 | HRESULT hr;
|
---|
| 244 |
|
---|
| 245 | for(int i = 0; i < 6; i++)
|
---|
| 246 | {
|
---|
| 247 | IDirect3DSurface9* pFace;
|
---|
| 248 | V( target->GetCubeMapSurface((D3DCUBEMAP_FACES)i, 0, &pFace) );
|
---|
| 249 | V( g_pd3dDevice->SetRenderTarget(0, pFace) );
|
---|
| 250 | V( g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
|
---|
| 251 | D3DCOLOR_ARGB(0,0,0,0), 1.0f, 0) );
|
---|
| 252 |
|
---|
| 253 | D3DXMATRIXA16 viewMatrix = *camera.GetViewMatrix();
|
---|
| 254 | D3DXMATRIXA16 projMatrix = *camera.GetProjMatrix();
|
---|
| 255 | getViewProjForCubeFace(&viewMatrix, &projMatrix, i, g_objectMesh->GetMeshPosition());
|
---|
| 256 | renderMesh(mesh, viewMatrix, projMatrix, effect);
|
---|
| 257 |
|
---|
| 258 | SAFE_RELEASE( pFace );
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | // restore old rendertarget & depth buffer
|
---|
| 262 | g_pd3dDevice->SetRenderTarget(0, oldRenderTarget);
|
---|
| 263 | g_pd3dDevice->SetDepthStencilSurface(oldDepthStencil);
|
---|
| 264 | SAFE_RELEASE( oldDepthStencil );
|
---|
| 265 | SAFE_RELEASE( oldRenderTarget );
|
---|
| 266 | SAFE_RELEASE( newDepthStencil );
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | //--------------------------------------------------------------------------------------
|
---|
| 270 | // Cubemap updates
|
---|
| 271 | //--------------------------------------------------------------------------------------
|
---|
| 272 | void renderEnvMaps()
|
---|
| 273 | {
|
---|
| 274 | HRESULT hr;
|
---|
| 275 | //check if update is needed
|
---|
| 276 | static bool first = true;
|
---|
| 277 | if(first)//skip firs frame (why?)
|
---|
| 278 | {
|
---|
| 279 | first = false;
|
---|
| 280 | return;
|
---|
| 281 | }
|
---|
| 282 | if(g_currentFrame == g_updateInterval)
|
---|
| 283 | {
|
---|
| 284 | g_currentFrame = 0;
|
---|
| 285 | //update color and distance envmap for distant objects
|
---|
| 286 | V( g_basicShaders->SetTechnique( "ColorDistance" ) );
|
---|
| 287 | UINT uPasses;
|
---|
| 288 | V( g_basicShaders->Begin( &uPasses, 0 ) );
|
---|
| 289 | V( g_basicShaders->BeginPass( 0 ) ); // only one pass exists
|
---|
| 290 | renderEnvMap(g_environmentMesh, g_CubeTexture3, g_basicShaders);
|
---|
| 291 | V( g_basicShaders->EndPass() );
|
---|
| 292 | V( g_basicShaders->End() );
|
---|
| 293 | //update normal and distance envmap for near objects
|
---|
| 294 | V( g_basicShaders->SetTechnique( "NormalDistance" ) );
|
---|
| 295 | V( g_basicShaders->Begin( &uPasses, 0 ) );
|
---|
| 296 | V( g_basicShaders->BeginPass( 0 ) ); // only one pass exists
|
---|
| 297 | //front facing polygons
|
---|
| 298 | renderEnvMap(g_objectMesh, g_CubeTexture1, g_basicShaders);
|
---|
| 299 | //back facing polygons
|
---|
| 300 | g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
|
---|
| 301 | renderEnvMap(g_objectMesh, g_CubeTexture2, g_basicShaders);
|
---|
| 302 | g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
|
---|
| 303 | V( g_basicShaders->EndPass() );
|
---|
| 304 | V( g_basicShaders->End() );
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 | if(g_updateInterval == 0)
|
---|
| 308 | g_currentFrame = 1;
|
---|
| 309 | else
|
---|
| 310 | g_currentFrame++;
|
---|
| 311 | }
|
---|
| 312 |
|
---|
| 313 | //--------------------------------------------------------------------------------------
|
---|
| 314 | // Final rendering to the screen
|
---|
| 315 | //--------------------------------------------------------------------------------------
|
---|
| 316 | void renderToScreen()
|
---|
| 317 | {
|
---|
| 318 | HRESULT hr;
|
---|
| 319 | D3DXMATRIXA16 viewMatrix = *camera.GetViewMatrix();
|
---|
| 320 | D3DXMATRIXA16 projMatrix = *camera.GetProjMatrix();
|
---|
| 321 | UINT uPasses;
|
---|
| 322 |
|
---|
| 323 | V( g_basicShaders->SetTechnique( "Textured" ) );
|
---|
| 324 | V( g_basicShaders->Begin( &uPasses, 0 ) );
|
---|
| 325 | V( g_basicShaders->BeginPass( 0 ) ); // only one pass exists
|
---|
| 326 | V( g_basicShaders->SetTexture("colorMap", g_environmentMesh->GetTexture()));
|
---|
| 327 | renderMesh(g_environmentMesh, viewMatrix, projMatrix, g_basicShaders);
|
---|
| 328 | V( g_basicShaders->EndPass() );
|
---|
| 329 | V( g_basicShaders->End() );
|
---|
| 330 |
|
---|
| 331 | ID3DXEffect* currentEffect;
|
---|
| 332 | if(g_currentMaterial == 0)
|
---|
| 333 | currentEffect = g_basicShaders;
|
---|
| 334 | else
|
---|
| 335 | currentEffect = g_reflectionShaders;
|
---|
| 336 |
|
---|
| 337 | switch(g_currentMaterial)
|
---|
| 338 | {
|
---|
| 339 | case 0:
|
---|
| 340 | V( currentEffect->SetTechnique( "EnvMapped" ) ); break;
|
---|
| 341 | case 1:
|
---|
| 342 | V( currentEffect->SetTechnique( "SingleReflection" ) ); break;
|
---|
| 343 | case 2:
|
---|
| 344 | V( currentEffect->SetTechnique( "MultipleReflection" ) ); break;
|
---|
| 345 | }
|
---|
| 346 |
|
---|
| 347 | V( currentEffect->Begin( &uPasses, 0 ) );
|
---|
| 348 | V( currentEffect->BeginPass( 0 ) ); // only one pass exists
|
---|
| 349 | D3DXVECTOR3 eye = *camera.GetEyePt();
|
---|
| 350 | D3DXVECTOR4 eyePos(eye.x, eye.y, eye.z, 1);
|
---|
| 351 | D3DXVECTOR3 refpos = g_objectMesh->GetMeshPosition();
|
---|
| 352 | D3DXVECTOR4 refPos(refpos.x, refpos.y, refpos.z, 1);
|
---|
| 353 | D3DXVECTOR4 freshnel(g_freshnelR, g_freshnelG, g_freshnelB, 1);
|
---|
| 354 | V( currentEffect->SetVector( "eyePos", &eyePos ) );
|
---|
| 355 | V( currentEffect->SetVector( "referencePos", &refPos ) );
|
---|
| 356 | V( currentEffect->SetVector( "F0", &freshnel));
|
---|
| 357 | V( currentEffect->SetFloat( "N0", g_refractFactor));
|
---|
| 358 |
|
---|
| 359 | if(g_currentMaterial > 0)
|
---|
| 360 | {
|
---|
| 361 | V( currentEffect->SetInt("MIN_LIN_ITERATIONCOUNT", g_linearMinIter));
|
---|
| 362 | V( currentEffect->SetInt("MAX_LIN_ITERATIONCOUNT", g_linearMaxIter));
|
---|
| 363 | V( currentEffect->SetInt("SECANT_ITERATIONCOUNT", g_secantIter));
|
---|
| 364 | V( currentEffect->SetInt("MAX_RAY_DEPTH", g_rayDepth));
|
---|
| 365 | V( currentEffect->SetTexture("envCube1", g_CubeTexture1));
|
---|
| 366 | V( currentEffect->SetTexture("envCube2", g_CubeTexture2));
|
---|
| 367 | V( currentEffect->SetTexture("envCube3", g_CubeTexture3));
|
---|
| 368 | }
|
---|
| 369 | else
|
---|
| 370 | V( currentEffect->SetTexture("envCube", g_CubeTexture3));
|
---|
| 371 | renderMesh(g_objectMesh, viewMatrix, projMatrix, currentEffect);
|
---|
| 372 | V( currentEffect->EndPass() );
|
---|
| 373 | V( currentEffect->End() );
|
---|
| 374 |
|
---|
| 375 | }
|
---|
| 376 |
|
---|
| 377 |
|
---|
| 378 | //----------------------------------------------------------------------------------
|
---|
| 379 | // Render text to screen
|
---|
| 380 | //----------------------------------------------------------------------------------
|
---|
| 381 | void renderText()
|
---|
| 382 | {
|
---|
| 383 | const D3DSURFACE_DESC* backBufferDesc = DXUTGetBackBufferSurfaceDesc();
|
---|
| 384 |
|
---|
| 385 | CDXUTTextHelper txtHelper( g_pFont, g_pTextSprite, 15 );
|
---|
| 386 | txtHelper.Begin();
|
---|
| 387 |
|
---|
| 388 | txtHelper.SetInsertionPos( 5, 5 );
|
---|
| 389 | txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 0.0f, 1.0f ) );
|
---|
| 390 |
|
---|
| 391 | txtHelper.DrawFormattedTextLine( L"%.2f fps @ %i x %i",
|
---|
| 392 | DXUTGetFPS(), backBufferDesc->Width, backBufferDesc->Height );
|
---|
| 393 |
|
---|
| 394 | if(g_showGUI)
|
---|
| 395 | {
|
---|
| 396 | int xpos = 135;
|
---|
| 397 | int ypos = 30;
|
---|
| 398 | txtHelper.SetInsertionPos( xpos, ypos );
|
---|
| 399 | txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ) );
|
---|
| 400 |
|
---|
| 401 | txtHelper.DrawFormattedTextLine( L"linear iteration (min) : %d", g_linearMinIter);
|
---|
| 402 | txtHelper.DrawFormattedTextLine( L"linear iteration (max) : %d", g_linearMaxIter);
|
---|
| 403 | txtHelper.DrawFormattedTextLine( L"secant iteration : %d", g_secantIter);
|
---|
| 404 | txtHelper.DrawFormattedTextLine( L"max ray depth : %d", g_rayDepth);
|
---|
| 405 | txtHelper.DrawFormattedTextLine( L"cubemap update interal : %d", g_updateInterval);
|
---|
| 406 | txtHelper.DrawFormattedTextLine( L"object scale : %0.2f", g_objectSize);
|
---|
| 407 | txtHelper.DrawFormattedTextLine( L"refraction coefficient : %0.2f", g_refractFactor);
|
---|
| 408 | txtHelper.DrawFormattedTextLine( L"Freshnel factor(RED) : %0.2f", g_freshnelR);
|
---|
| 409 | txtHelper.DrawFormattedTextLine( L"Freshnel factor(GREEN) : %0.2f", g_freshnelG);
|
---|
| 410 | txtHelper.DrawFormattedTextLine( L"Freshnel factor(BLUE) : %0.2f", g_freshnelB);
|
---|
| 411 | }
|
---|
| 412 |
|
---|
| 413 | if ( !g_showHelp )
|
---|
| 414 | {
|
---|
| 415 | txtHelper.SetInsertionPos( backBufferDesc->Width / 2 -50, 5 );
|
---|
| 416 | txtHelper.DrawTextLine( L"Press F1 for help" );
|
---|
| 417 | }
|
---|
| 418 |
|
---|
| 419 | if(g_showHelp)
|
---|
| 420 | {
|
---|
| 421 | txtHelper.SetInsertionPos( backBufferDesc->Width - 260, backBufferDesc->Height-24*22 );
|
---|
| 422 |
|
---|
| 423 | txtHelper.DrawTextLine(
|
---|
| 424 | L"Controls (F1 to hide):\n"
|
---|
| 425 | L"___________________________________\n"
|
---|
| 426 | L" APPLICATION CONTROLS\n"
|
---|
| 427 | L"\n"
|
---|
| 428 | L"Right click+drag: Rotate mesh\n"
|
---|
| 429 | L"Left click+drag: Move camera\n"
|
---|
| 430 | L"Mouse wheel: Zoom\n"
|
---|
| 431 | L"Arrow keys: Move object\n"
|
---|
| 432 | L"F1: Show/Hide Help\n"
|
---|
| 433 | L"TAB: Show/Hide User Interface\n"
|
---|
| 434 | L"\n"
|
---|
| 435 | L"___________________________________\n"
|
---|
| 436 | L" Quit: ESC");
|
---|
| 437 | }
|
---|
| 438 |
|
---|
| 439 | txtHelper.End();
|
---|
| 440 | }
|
---|
| 441 |
|
---|
[2244] | 442 | void renderWelcomeText()
|
---|
| 443 | {
|
---|
| 444 | const D3DSURFACE_DESC* backBufferDesc = DXUTGetBackBufferSurfaceDesc();
|
---|
| 445 |
|
---|
| 446 | CDXUTTextHelper txtHelper( g_pFont, g_pTextSprite, 20 );
|
---|
| 447 | txtHelper.Begin();
|
---|
| 448 |
|
---|
| 449 | txtHelper.SetInsertionPos( backBufferDesc->Width / 2 - 100, backBufferDesc->Height /2 );
|
---|
| 450 | txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 0.0f, 1.0f ) );
|
---|
| 451 |
|
---|
| 452 | txtHelper.DrawFormattedTextLine( L"Compiling shaders. Please wait..." );
|
---|
| 453 | }
|
---|
[2242] | 454 | //--------------------------------------------------------------------------------------
|
---|
| 455 | // Render the scene
|
---|
| 456 | //--------------------------------------------------------------------------------------
|
---|
| 457 | void CALLBACK OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
|
---|
| 458 | {
|
---|
| 459 | HRESULT hr;
|
---|
| 460 |
|
---|
| 461 | // Clear the render target and the zbuffer
|
---|
| 462 | V( pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0, 45, 50, 170), 1.0f, 0) );
|
---|
| 463 |
|
---|
| 464 | // Render the scene
|
---|
| 465 | if( SUCCEEDED( pd3dDevice->BeginScene() ) )
|
---|
| 466 | {
|
---|
[2244] | 467 | if(g_showWelcomeScreen)
|
---|
| 468 | {
|
---|
| 469 | renderWelcomeText();
|
---|
| 470 | g_showWelcomeScreen = false;
|
---|
| 471 | }
|
---|
| 472 | else
|
---|
| 473 | {
|
---|
| 474 | if(g_basicShaders == 0)
|
---|
| 475 | compileShaders();
|
---|
| 476 | renderEnvMaps();
|
---|
| 477 | renderToScreen();
|
---|
| 478 | renderText();
|
---|
| 479 | if(g_showGUI)
|
---|
| 480 | g_HUD.OnRender( fElapsedTime );
|
---|
| 481 | }
|
---|
[2242] | 482 | V( pd3dDevice->EndScene() );
|
---|
[2244] | 483 | }
|
---|
[2242] | 484 | }
|
---|
| 485 |
|
---|
| 486 | //--------------------------------------------------------------------------------------
|
---|
| 487 | // Handle messages to the application
|
---|
| 488 | //--------------------------------------------------------------------------------------
|
---|
| 489 | LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam,
|
---|
| 490 | bool* pbNoFurtherProcessing, void* pUserContext )
|
---|
| 491 | {
|
---|
| 492 | *pbNoFurtherProcessing = g_DialogResourceManager.MsgProc( hWnd, uMsg, wParam, lParam );
|
---|
| 493 | if( *pbNoFurtherProcessing )
|
---|
| 494 | return 0;
|
---|
| 495 |
|
---|
| 496 | *pbNoFurtherProcessing = g_HUD.MsgProc( hWnd, uMsg, wParam, lParam );
|
---|
| 497 | if( *pbNoFurtherProcessing )
|
---|
| 498 | return 0;
|
---|
| 499 |
|
---|
| 500 | camera.HandleMessages( hWnd, uMsg, wParam, lParam );
|
---|
| 501 | D3DXMATRIXA16 rotation = *camera.GetWorldMatrix();
|
---|
| 502 | if(g_objectMesh)
|
---|
| 503 | g_objectMesh->setRotation(rotation);
|
---|
| 504 |
|
---|
| 505 | return 0;
|
---|
| 506 | }
|
---|
| 507 |
|
---|
| 508 | //--------------------------------------------------------------------------------------
|
---|
| 509 | // Keyboard message handler
|
---|
| 510 | //--------------------------------------------------------------------------------------
|
---|
| 511 | void CALLBACK KeyboardProc( UINT nChar, bool bKeyDown, bool bAltDown, void* pUserContext )
|
---|
| 512 | {
|
---|
| 513 | if( bKeyDown )
|
---|
| 514 | {
|
---|
| 515 | float step = 0.01f;
|
---|
| 516 |
|
---|
| 517 | switch( nChar )
|
---|
| 518 | {
|
---|
| 519 | case VK_RIGHT: g_objectMesh->Move( D3DXVECTOR3( step,0,0), true ); break;
|
---|
| 520 | case VK_LEFT: g_objectMesh->Move( D3DXVECTOR3(-step,0,0), true ); break;
|
---|
| 521 | case VK_UP: g_objectMesh->Move( D3DXVECTOR3(0, step,0), true ); break;
|
---|
| 522 | case VK_DOWN: g_objectMesh->Move( D3DXVECTOR3(0,-step,0), true ); break;
|
---|
| 523 | case VK_PRIOR: g_objectMesh->Move( D3DXVECTOR3(0,0, step), true ); break;
|
---|
| 524 | case VK_NEXT: g_objectMesh->Move( D3DXVECTOR3(0,0,-step), true ); break;
|
---|
| 525 | case VK_TAB: g_showGUI = !g_showGUI; break;
|
---|
| 526 | case VK_F1: g_showHelp = !g_showHelp; break;
|
---|
| 527 | }
|
---|
| 528 | }
|
---|
| 529 | }
|
---|
| 530 |
|
---|
| 531 |
|
---|
| 532 | //--------------------------------------------------------------------------------------
|
---|
| 533 | // Release resources created in the OnResetDevice callback here
|
---|
| 534 | //--------------------------------------------------------------------------------------
|
---|
| 535 | void CALLBACK OnLostDevice( void* pUserContext )
|
---|
| 536 | {
|
---|
| 537 | g_DialogResourceManager.OnLostDevice();
|
---|
| 538 | if( g_basicShaders ) g_basicShaders->OnLostDevice();
|
---|
| 539 | if( g_reflectionShaders ) g_reflectionShaders->OnLostDevice();
|
---|
| 540 | if( g_pFont ) g_pFont->OnLostDevice();
|
---|
| 541 | SAFE_RELEASE( g_CubeTexture1 );
|
---|
| 542 | SAFE_RELEASE( g_CubeTexture2 );
|
---|
| 543 | SAFE_RELEASE( g_CubeTexture3 );
|
---|
| 544 | }
|
---|
| 545 |
|
---|
| 546 | //--------------------------------------------------------------------------------------
|
---|
| 547 | // Release resources created in the OnCreateDevice callback here
|
---|
| 548 | //--------------------------------------------------------------------------------------
|
---|
| 549 | void CALLBACK OnDestroyDevice( void* pUserContext )
|
---|
| 550 | {
|
---|
| 551 | g_DialogResourceManager.OnDestroyDevice();
|
---|
| 552 | delete g_environmentMesh;
|
---|
| 553 | delete g_objectMesh;
|
---|
| 554 | SAFE_RELEASE(g_basicShaders);
|
---|
| 555 | SAFE_RELEASE(g_reflectionShaders);
|
---|
| 556 | SAFE_RELEASE(g_pFont);
|
---|
| 557 | }
|
---|
| 558 |
|
---|
| 559 | //--------------------------------------------------------------------------------------
|
---|
| 560 | // GUI initialization
|
---|
| 561 | //--------------------------------------------------------------------------------------
|
---|
| 562 | void InitializeDialogs()
|
---|
| 563 | {
|
---|
| 564 | g_HUD.Init( &g_DialogResourceManager );
|
---|
| 565 | g_HUD.SetCallback( OnGUIEvent );
|
---|
| 566 |
|
---|
| 567 | // Initialize dialog
|
---|
| 568 | int posX = 10;
|
---|
| 569 | int posY = 30;
|
---|
| 570 | int sliderheight = 14;
|
---|
| 571 | int sliderYoffset = 15;
|
---|
| 572 | // Setup for sliders
|
---|
| 573 | g_HUD.AddSlider( LINEAR_MIN_ITERATION_SLIDER, posX, posY, 120, sliderheight, 1, 100, g_linearMinIter );
|
---|
| 574 | g_HUD.AddSlider( LINEAR_MAX_ITERATION_SLIDER, posX, posY += sliderYoffset, 120, sliderheight, 1, 100, g_linearMaxIter);
|
---|
| 575 | g_HUD.AddSlider( SECANT_ITERATION_SLIDER, posX, posY += sliderYoffset, 120, sliderheight, 0, 20, g_secantIter );
|
---|
| 576 | g_HUD.AddSlider( MAX_RAY_DEPTH_SLIDER, posX, posY += sliderYoffset, 120, sliderheight, 1, 10, g_rayDepth );
|
---|
| 577 | g_HUD.AddSlider( CUBEMAP_UPDATE_INTERVAL, posX, posY += sliderYoffset, 120, sliderheight, 0, 10, g_updateInterval );
|
---|
| 578 | g_HUD.AddSlider( OBJECT_SIZE, posX, posY += sliderYoffset, 120, sliderheight, 1, 100, g_iObjectSize );
|
---|
| 579 | g_HUD.AddSlider( REFRACT_FACTOR_SLIDER, posX, posY += sliderYoffset, 120, sliderheight, 0, 101, g_iRefractFactor );
|
---|
| 580 | g_HUD.AddSlider( FRESHEL_RED_SLIDER, posX, posY += sliderYoffset, 120, sliderheight, 0, 100, g_iFreshnelR );
|
---|
| 581 | g_HUD.AddSlider( FRESHEL_GREEN_SLIDER, posX, posY += sliderYoffset, 120, sliderheight, 0, 100, g_iFreshnelG );
|
---|
| 582 | g_HUD.AddSlider( FRESHEL_BLUE_SLIDER, posX, posY += sliderYoffset, 120, sliderheight, 0, 100, g_iFreshnelB );
|
---|
| 583 | // Setup fo combo boxes
|
---|
| 584 | g_HUD.AddComboBox( OBJECT_MATERIAL_COMBO, 10, posY += 20, 160, 20, 0, false, &g_objectMaterialComboBox );
|
---|
| 585 | g_HUD.AddComboBox( OBJECT_MODEL_COMBO, 10, posY += 20, 160, 20, 0, false, &g_objectModelComboBox );
|
---|
| 586 |
|
---|
| 587 | g_objectMaterialComboBox->AddItem( L"classical envmapping", NULL );
|
---|
| 588 | g_objectMaterialComboBox->AddItem( L"single reflection", NULL );
|
---|
| 589 | g_objectMaterialComboBox->AddItem( L"multiple reflection", NULL );
|
---|
| 590 |
|
---|
| 591 | g_objectModelComboBox->AddItem( L"sphere", NULL );
|
---|
| 592 | g_objectModelComboBox->AddItem( L"teapot", NULL );
|
---|
| 593 | g_objectModelComboBox->AddItem( L"column", NULL );
|
---|
| 594 | }
|
---|
| 595 |
|
---|
| 596 | //--------------------------------------------------------------------------------------
|
---|
| 597 | // GUI message handler
|
---|
| 598 | //--------------------------------------------------------------------------------------
|
---|
| 599 | void CALLBACK OnGUIEvent( UINT nEvent, int nControlID, CDXUTControl* pControl, void* pUserContext )
|
---|
| 600 | {
|
---|
| 601 | if( nControlID >= FIRST_SLIDER && nControlID <= LAST_SLIDER )
|
---|
| 602 | {
|
---|
| 603 | g_iSelectedMenuItem = nControlID;
|
---|
| 604 | UpdateParamFromSlider();
|
---|
| 605 | }
|
---|
| 606 | else
|
---|
| 607 | {
|
---|
| 608 | if(nControlID == OBJECT_MODEL_COMBO)
|
---|
| 609 | {
|
---|
| 610 | D3DXVECTOR3 lastposition = g_objectMesh->GetMeshPosition();
|
---|
| 611 | int currentmodel = g_objectModelComboBox->FindItem(g_objectModelComboBox->GetSelectedItem()->strText);
|
---|
| 612 | delete g_objectMesh;
|
---|
| 613 | g_objectMesh = new Mesh(MESHNAMES[currentmodel], MESH_TEXTURE, g_objectSize, lastposition);
|
---|
| 614 | D3DXVECTOR3 environmentSize = g_environmentMesh->GetMeshSize();
|
---|
| 615 | g_objectMesh->SetContainerSize(environmentSize);
|
---|
| 616 | g_currentFrame = 0;
|
---|
| 617 | }
|
---|
| 618 |
|
---|
| 619 | if(nControlID == OBJECT_MATERIAL_COMBO)
|
---|
| 620 | {
|
---|
| 621 | g_currentMaterial = g_objectMaterialComboBox->FindItem(g_objectMaterialComboBox->GetSelectedItem()->strText);
|
---|
| 622 | }
|
---|
| 623 | }
|
---|
| 624 | }
|
---|
| 625 |
|
---|
| 626 | //--------------------------------------------------------------------------------------
|
---|
| 627 | // Update slider parameters
|
---|
| 628 | //--------------------------------------------------------------------------------------
|
---|
| 629 | void UpdateParamFromSlider()
|
---|
| 630 | {
|
---|
| 631 | int iSelectedValue = g_HUD.GetSlider( g_iSelectedMenuItem )->GetValue();
|
---|
| 632 |
|
---|
| 633 | switch (g_iSelectedMenuItem)
|
---|
| 634 | {
|
---|
| 635 | case LINEAR_MIN_ITERATION_SLIDER: g_linearMinIter = min(iSelectedValue, g_linearMaxIter); break;
|
---|
| 636 | case LINEAR_MAX_ITERATION_SLIDER: g_linearMaxIter = max(iSelectedValue, g_linearMinIter); break;
|
---|
| 637 | case SECANT_ITERATION_SLIDER: g_secantIter = iSelectedValue; break;
|
---|
| 638 | case CUBEMAP_UPDATE_INTERVAL: g_updateInterval = iSelectedValue; g_currentFrame = 0; break;
|
---|
| 639 | case MAX_RAY_DEPTH_SLIDER: g_rayDepth = iSelectedValue; break;
|
---|
| 640 | case OBJECT_SIZE: g_iObjectSize = iSelectedValue;
|
---|
| 641 | g_objectSize = (float) g_iObjectSize / 100.0f;
|
---|
| 642 | g_objectMesh->SetPreferredDiameter(g_objectSize);
|
---|
| 643 | g_currentFrame = 0;
|
---|
| 644 | break;
|
---|
| 645 | case REFRACT_FACTOR_SLIDER: g_iRefractFactor = iSelectedValue; g_refractFactor = (g_iRefractFactor - 1) / 100.0; break;
|
---|
| 646 | case FRESHEL_RED_SLIDER: g_iFreshnelR = iSelectedValue; g_freshnelR = g_iFreshnelR / 100.0; break;
|
---|
| 647 | case FRESHEL_GREEN_SLIDER: g_iFreshnelG = iSelectedValue; g_freshnelG = g_iFreshnelG / 100.0; break;
|
---|
| 648 | case FRESHEL_BLUE_SLIDER: g_iFreshnelB = iSelectedValue; g_freshnelB = g_iFreshnelB / 100.0; break;
|
---|
| 649 | }
|
---|
| 650 | }
|
---|
| 651 |
|
---|
| 652 | //--------------------------------------------------------------------------------------
|
---|
| 653 | // Initialize everything and go into a render loop
|
---|
| 654 | //--------------------------------------------------------------------------------------
|
---|
| 655 | INT WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR, int )
|
---|
| 656 | {
|
---|
| 657 | // Enable run-time memory check for debug builds.
|
---|
| 658 | #if defined(DEBUG) | defined(_DEBUG)
|
---|
| 659 | _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
|
---|
| 660 | #endif
|
---|
| 661 |
|
---|
| 662 | // Set the callback functions
|
---|
| 663 | DXUTSetCallbackDeviceCreated( OnCreateDevice );
|
---|
| 664 | DXUTSetCallbackDeviceReset( OnResetDevice );
|
---|
| 665 | DXUTSetCallbackDeviceLost( OnLostDevice );
|
---|
| 666 | DXUTSetCallbackDeviceDestroyed( OnDestroyDevice );
|
---|
| 667 | DXUTSetCallbackMsgProc( MsgProc );
|
---|
| 668 | DXUTSetCallbackKeyboard( KeyboardProc );
|
---|
| 669 | DXUTSetCallbackFrameRender( OnFrameRender );
|
---|
| 670 | DXUTSetCallbackFrameMove( OnFrameMove );
|
---|
| 671 |
|
---|
| 672 | // TODO: Perform any application-level initialization here
|
---|
| 673 | InitializeDialogs();
|
---|
| 674 | camera.SetViewParams(&cameraPosition, &cameraLookat);
|
---|
| 675 | camera.SetButtonMasks( MOUSE_RIGHT_BUTTON, MOUSE_WHEEL, MOUSE_LEFT_BUTTON );
|
---|
| 676 | // Initialize DXUT and create the desired Win32 window and Direct3D device for the application
|
---|
| 677 | DXUTInit( true, true, true ); // Parse the command line, handle the default hotkeys, and show msgboxes
|
---|
| 678 | DXUTSetCursorSettings( true, true ); // Show the cursor and clip it when in full screen
|
---|
| 679 | DXUTCreateWindow( L"MultipleReflections" );
|
---|
| 680 | DXUTCreateDevice( D3DADAPTER_DEFAULT, true, g_width, g_height, IsDeviceAcceptable, ModifyDeviceSettings );
|
---|
| 681 |
|
---|
| 682 | // Start the render loop
|
---|
| 683 | DXUTMainLoop();
|
---|
| 684 | // TODO: Perform any application-level cleanup here
|
---|
| 685 | return DXUTGetExitCode();
|
---|
| 686 | }
|
---|
| 687 |
|
---|
| 688 |
|
---|