void SkyboxObject::load ( trObjectParserBlock &parserBlock ) { float skyboxSize = 500; skybox.SetTexturePath("data/skyboxes"); // scan elements tvStringPairList::iterator itr = parserBlock.elements.begin(); while ( itr != parserBlock.elements.end() ) { if ( !basicElement( *itr ) ) { std::string tag = string_util::toupper(itr->first); if ( tag == "BOUNDS" ) skyboxSize = (float)atof(itr->second.c_str()); else if ( tag == "ZSHIFT" ) skybox.SetZShift((float)atof(itr->second.c_str())); } itr++; } skybox.Create(skyboxSize,name.c_str()); MapBaseObject::load(parserBlock); }
//-------------------------------------------------------------------------------------- // This callback function will be called at the end of every frame to perform all the // rendering calls for the scene, and it will also be called if the window needs to be // repainted. After this function has returned, DXUT will call // IDirect3DDevice9::Present to display the contents of the next buffer in the swap chain //-------------------------------------------------------------------------------------- void CALLBACK OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext ) { // If the settings dialog is being shown, then // render it instead of rendering the app's scene if( g_SettingsDlg.IsActive() ) { g_SettingsDlg.OnRender( fElapsedTime ); return; } HRESULT hr; // Clear the render target and the zbuffer V( pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB( 0, 50, 50, 50 ), 1.0f, 0 ) ); // Render the scene if( SUCCEEDED( pd3dDevice->BeginScene() ) ) { // Get the projection & view matrix from the camera class D3DXMATRIXA16 mViewProjection = ( *g_Camera.GetViewMatrix() ) * ( *g_Camera.GetProjMatrix() ); g_Skybox.SetDrawSH( false ); g_Skybox.Render( &mViewProjection, 1.0f, 1.0f ); V( g_pEffect->SetMatrix( "g_mViewProjection", &mViewProjection ) ); V( g_pEffect->SetFloat( "g_fTime", ( float )fTime ) ); // Set the amount of transmitted light per color channel D3DXVECTOR3 vColorTransmit; vColorTransmit.x = g_SampleUI.GetSlider( IDC_RED_TRANSMIT_SLIDER )->GetValue() / 1000.0f; vColorTransmit.y = g_SampleUI.GetSlider( IDC_GREEN_TRANSMIT_SLIDER )->GetValue() / 1000.0f; vColorTransmit.z = g_SampleUI.GetSlider( IDC_BLUE_TRANSMIT_SLIDER )->GetValue() / 1000.0f; V( g_pEffect->SetFloatArray( "g_vColorTransmit", vColorTransmit, 3 ) ); // for Cubic degree rendering V( g_pEffect->SetFloat( "g_fLightIntensity", g_fLightIntensity ) ); V( g_pEffect->SetFloatArray( "g_vLightDirection", g_vLightDirection, 3 * sizeof( float ) ) ); V( g_pEffect->SetFloatArray( "g_vLightCoeffsR", m_fRLC, 4 * sizeof( float ) ) ); V( g_pEffect->SetFloatArray( "g_vLightCoeffsG", m_fGLC, 4 * sizeof( float ) ) ); V( g_pEffect->SetFloatArray( "g_vLightCoeffsB", m_fBLC, 4 * sizeof( float ) ) ); pd3dDevice->SetRenderState( D3DRS_FILLMODE, g_bWireFrame ? D3DFILL_WIREFRAME : D3DFILL_SOLID ); DrawFrame( pd3dDevice, g_pFrameRoot ); DXUT_BeginPerfEvent( DXUT_PERFEVENTCOLOR, L"HUD / Stats" ); // These events are to help PIX identify what the code is doing RenderText(); V( g_HUD.OnRender( fElapsedTime ) ); V( g_SampleUI.OnRender( fElapsedTime ) ); V( g_LightControl.OnRender9( D3DXCOLOR( 1, 1, 1, 1 ), ( D3DXMATRIX* )g_Camera.GetViewMatrix(), ( D3DXMATRIX* )g_Camera.GetProjMatrix(), g_Camera.GetEyePt() ) ); DXUT_EndPerfEvent(); V( pd3dDevice->EndScene() ); } }
//-------------------------------------------------------------------------------------- // This callback function will be called immediately after the Direct3D device has been // reset, which will happen after a lost device scenario. This is the best location to // create D3DPOOL_DEFAULT resources since these resources need to be reloaded whenever // the device is lost. Resources created here should be released in the OnLostDevice // callback. //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { HRESULT hr; V_RETURN( g_DialogResourceManager.OnD3D9ResetDevice() ); V_RETURN( g_SettingsDlg.OnD3D9ResetDevice() ); if( g_pFont ) V_RETURN( g_pFont->OnResetDevice() ); if( g_pEffect ) V_RETURN( g_pEffect->OnResetDevice() ); g_LightControl.OnD3D9ResetDevice( pBackBufferSurfaceDesc ); g_Skybox.OnResetDevice( pBackBufferSurfaceDesc ); // Create a sprite to help batch calls when drawing many lines of text V_RETURN( D3DXCreateSprite( pd3dDevice, &g_pTextSprite ) ); // Setup the camera's projection parameters float fAspectRatio = pBackBufferSurfaceDesc->Width / ( FLOAT )pBackBufferSurfaceDesc->Height; g_Camera.SetProjParams( D3DX_PI / 4, fAspectRatio, 0.001f, 1000.0f ); g_Camera.SetWindow( pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height ); g_Camera.SetButtonMasks( MOUSE_LEFT_BUTTON, MOUSE_WHEEL, MOUSE_RIGHT_BUTTON ); g_Camera.SetAttachCameraToModel( true ); g_Camera.SetRadius( 5.0f, 0.1f, 20.0f ); g_HUD.SetLocation( pBackBufferSurfaceDesc->Width - 170, 0 ); g_HUD.SetSize( 170, 170 ); g_SampleUI.SetLocation( pBackBufferSurfaceDesc->Width - 300, pBackBufferSurfaceDesc->Height - 245 ); g_SampleUI.SetSize( 300, 300 ); return S_OK; }
void DrawSkybox(void) { // 关闭深度测试 glDisable(GL_DEPTH_TEST); // 关闭光照 glDisable(GL_LIGHTING); glColor3f(1.0, 1.0, 1.0); // 保存投影变换矩阵 glMatrixMode(GL_PROJECTION); glPushMatrix(); // 增大透视投影变换远平面距离 glLoadIdentity(); int viewport[4]; glGetIntegerv(GL_VIEWPORT, viewport); gluPerspective(60.0, (float)viewport[2]/(float)viewport[3], sky_box.size*0.01, sky_box.size*3.0); // 绘制天空盒 sky_box.Draw(); // 恢复投影变换矩阵 glPopMatrix(); glMatrixMode(GL_MODELVIEW); // 重新打开光照 glEnable(GL_LIGHTING); // 重新打开深度测试 glEnable(GL_DEPTH_TEST); }
//-------------------------------------------------------------------------------------- // This callback function will be called immediately after the Direct3D device has // entered a lost state and before IDirect3DDevice9::Reset is called. Resources created // in the OnResetDevice callback should be released here, which generally includes all // D3DPOOL_DEFAULT resources. See the "Lost Devices" section of the documentation for // information about lost devices. //-------------------------------------------------------------------------------------- void CALLBACK OnLostDevice( void* pUserContext ) { g_DialogResourceManager.OnD3D9LostDevice(); g_SettingsDlg.OnD3D9LostDevice(); if( g_pFont ) g_pFont->OnLostDevice(); if( g_pEffect ) g_pEffect->OnLostDevice(); g_LightControl.StaticOnD3D9LostDevice(); g_Skybox.OnLostDevice(); SAFE_RELEASE( g_pTextSprite ); }
//-------------------------------------------------------------------------------------- // This callback function will be called immediately after the Direct3D device has // been destroyed, which generally happens as a result of application termination or // windowed/full screen toggles. Resources created in the OnCreateDevice callback // should be released here, which generally includes all D3DPOOL_MANAGED resources. //-------------------------------------------------------------------------------------- void CALLBACK OnDestroyDevice( void* pUserContext ) { g_DialogResourceManager.OnD3D9DestroyDevice(); g_SettingsDlg.OnD3D9DestroyDevice(); SAFE_RELEASE( g_pEffect ); SAFE_RELEASE( g_pFont ); g_LightControl.StaticOnD3D9DestroyDevice(); g_Skybox.OnDestroyDevice(); if( g_pFrameRoot ) { CAllocateHierarchy Alloc; D3DXFrameDestroy( g_pFrameRoot, &Alloc ); g_pFrameRoot = NULL; } SAFE_RELEASE( g_pAnimController ); }
//-------------------------------------------------------------------------------------- // This callback function will be called immediately after the Direct3D device has been // created, which will happen during application initialization and windowed/full screen // toggles. This is the best location to create D3DPOOL_MANAGED resources since these // resources need to be reloaded whenever the device is destroyed. Resources created // here should be released in the OnDestroyDevice callback. //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { HRESULT hr; V_RETURN( g_DialogResourceManager.OnD3D9CreateDevice( pd3dDevice ) ); V_RETURN( g_SettingsDlg.OnD3D9CreateDevice( pd3dDevice ) ); // Initialize the font V_RETURN( D3DXCreateFont( pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, L"Arial", &g_pFont ) ); // Define DEBUG_VS and/or DEBUG_PS to debug vertex and/or pixel shaders with the // shader debugger. Debugging vertex shaders requires either REF or software vertex // processing, and debugging pixel shaders requires REF. The // D3DXSHADER_FORCE_*_SOFTWARE_NOOPT flag improves the debug experience in the // shader debugger. It enables source level debugging, prevents instruction // reordering, prevents dead code elimination, and forces the compiler to compile // against the next higher available software target, which ensures that the // unoptimized shaders do not exceed the shader model limitations. Setting these // flags will cause slower rendering since the shaders will be unoptimized and // forced into software. See the DirectX documentation for more information about // using the shader debugger. DWORD dwShaderFlags = D3DXFX_NOT_CLONEABLE; #if defined( DEBUG ) || defined( _DEBUG ) // Set the D3DXSHADER_DEBUG flag to embed debug information in the shaders. // Setting this flag improves the shader debugging experience, but still allows // the shaders to be optimized and to run exactly the way they will run in // the release configuration of this program. dwShaderFlags |= D3DXSHADER_DEBUG; #endif #ifdef DEBUG_VS dwShaderFlags |= D3DXSHADER_FORCE_VS_SOFTWARE_NOOPT; #endif #ifdef DEBUG_PS dwShaderFlags |= D3DXSHADER_FORCE_PS_SOFTWARE_NOOPT; #endif // Determine which LDPRT texture and SH coefficient cubemap formats are supported IDirect3D9* pD3D = DXUTGetD3D9Object(); D3DCAPS9 Caps; pd3dDevice->GetDeviceCaps( &Caps ); D3DDISPLAYMODE DisplayMode; pd3dDevice->GetDisplayMode( 0, &DisplayMode ); GetSupportedTextureFormat( pD3D, &Caps, DisplayMode.Format, &g_fmtTexture, &g_fmtCubeMap ); if( D3DFMT_UNKNOWN == g_fmtTexture || D3DFMT_UNKNOWN == g_fmtCubeMap ) return E_FAIL; // Create the skybox g_Skybox.OnCreateDevice( pd3dDevice, 50, L"Light Probes\\rnl_cross.dds", L"SkyBox.fx" ); V( D3DXSHProjectCubeMap( 6, g_Skybox.GetEnvironmentMap(), g_fSkyBoxLightSH[0], g_fSkyBoxLightSH[1], g_fSkyBoxLightSH[2] ) ); // Now compute the SH projection of the skybox... LPDIRECT3DCUBETEXTURE9 pSHCubeTex = NULL; V( D3DXCreateCubeTexture( pd3dDevice, 256, 1, 0, D3DFMT_A16B16G16R16F, D3DPOOL_MANAGED, &pSHCubeTex ) ); SHCubeProj projData; projData.Init( g_fSkyBoxLightSH[0], g_fSkyBoxLightSH[1], g_fSkyBoxLightSH[2] ); V( D3DXFillCubeTexture( pSHCubeTex, SHCubeFill, &projData ) ); g_Skybox.InitSH( pSHCubeTex ); // Read the D3DX effect file WCHAR str[MAX_PATH]; V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, TEXT( "LocalDeformablePRT.fx" ) ) ); // If this fails, there should be debug output as to they the .fx file failed to compile V_RETURN( D3DXCreateEffectFromFile( pd3dDevice, str, NULL, NULL, dwShaderFlags, NULL, &g_pEffect, NULL ) ); V_RETURN( LoadTechniqueObjects( "bat" ) ); V_RETURN( g_LightControl.StaticOnD3D9CreateDevice( pd3dDevice ) ); g_LightControl.SetRadius( 2.0f ); // Setup the camera's view parameters D3DXVECTOR3 vecEye( 0.0f, 0.0f, -5.0f ); D3DXVECTOR3 vecAt ( 0.0f, 0.0f, 0.0f ); g_Camera.SetViewParams( &vecEye, &vecAt ); // Set the model's initial orientation D3DXQUATERNION quatRotation; D3DXQuaternionRotationYawPitchRoll( &quatRotation, -0.5f, 0.7f, 0.0f ); g_Camera.SetWorldQuat( quatRotation ); return hr; }
void SkyboxObject::DrawItem ( int item, int param ) { glDisable(GL_LIGHTING); skybox.Draw(); glEnable(GL_LIGHTING); }
void GL_init(void) { glClearColor (1.0, 1.0, 1.0, 0.0); glEnable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); static GLfloat light_ambient[] = { 0.01, 0.01, 0.01, 1.0 }; static GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; static GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 }; glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1); models[0].CreateBlock(2.0, 1.6, 1.0, 4.0, 2.0, 2.0); models[1].CreateSphere(1.0, 80, 80, 1.0, 1.0); models[2].CreateCylinder(0.5, 2.0, 40, 20, 3.0, 2.0, 1.0); models[3].CreateTorus(0.2, 0.8, 40, 20, 8.0, 2.0); ground_model.CreateTerrainFromHeightMap(140.0, 140.0, 32.0, 32.0, "../textures/terrain_height-01.jpg", -21.0f, 0.3f); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glPixelStorei(GL_PACK_ALIGNMENT, 1); model_textures[0]=Load2DTexture("../textures/wood01.jpg"); model_textures[1]=Load2DTexture("../textures/earth.jpg"); model_textures[2]=Load2DTexture("../textures/metal01.jpg"); model_textures[3]=Load2DTexture("../textures/glass01.jpg"); ground_texture=Load2DMipmapTexture("../textures/rock01.jpg"); glEnable(GL_TEXTURE_2D); glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR); //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); camera.Init(CVector3D(15.0, 0.0, 1.0), CVector3D(0.0, 0.0, 1.0), CVector3D(1.0, 0.0, 0.0)); // Sky Box sky_box.size=1000.0f; sky_box.Create(); env_texture=LoadCubemapTexture( "../textures/Skybox01_left.jpg", "../textures/Skybox01_right.jpg", "../textures/Skybox01_up.jpg", "../textures/Skybox01_down.jpg", "../textures/Skybox01_front.jpg", "../textures/Skybox01_back.jpg" ); water_surface.Create(140.0, 140.0, 64, 64); water_surface.plane_waves[0].A=0.4f; water_surface.plane_waves[0].f=0.5f; water_surface.plane_waves[0].Lx_rcp=4.0f/100.0f; water_surface.plane_waves[0].Ly_rcp=0.0f/100.0f; water_surface.plane_waves[1].A=0.1f; water_surface.plane_waves[1].f=1.0f; water_surface.plane_waves[1].Lx_rcp=10.0f/100.0f; water_surface.plane_waves[1].Ly_rcp=10.0f/100.0f; current_time=timeGetTime(); }