void Geometry::renderAlphaGeometry(unsigned int subsetId) { if( _numPrelights ) { _dxCR( dxSetRenderState( D3DRS_COLORVERTEX, TRUE ) ); _dxCR( dxSetRenderState( D3DRS_AMBIENTMATERIALSOURCE, D3DMCS_MATERIAL ) ); _dxCR( dxSetRenderState( D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_MATERIAL ) ); _dxCR( dxSetRenderState( D3DRS_SPECULARMATERIALSOURCE, D3DMCS_MATERIAL ) ); _dxCR( dxSetRenderState( D3DRS_EMISSIVEMATERIALSOURCE, D3DMCS_COLOR1 ) ); } else { _dxCR( dxSetRenderState( D3DRS_COLORVERTEX, FALSE ) ); _dxCR( dxSetRenderState( D3DRS_AMBIENTMATERIALSOURCE, D3DMCS_MATERIAL ) ); _dxCR( dxSetRenderState( D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_MATERIAL ) ); _dxCR( dxSetRenderState( D3DRS_SPECULARMATERIALSOURCE, D3DMCS_MATERIAL ) ); _dxCR( dxSetRenderState( D3DRS_EMISSIVEMATERIALSOURCE, D3DMCS_MATERIAL ) ); } unsigned int shaderId = _mesh->getAttributeId( subsetId ); if( _shaders[shaderId]->effect() ) { reinterpret_cast<Effect*>( _shaders[shaderId]->effect() )->render( _mesh, subsetId, _shaders[shaderId] ); dxSetVertexShader( NULL ); dxSetPixelShader( NULL ); } else { _shaders[shaderId]->apply(); _mesh->renderSubset( subsetId, _shaders[shaderId] ); } }
void ScreenCamera::endScene(void) { assert( Camera::_currentCamera == this ); Camera::_currentCamera = NULL; _dxCR( iDirect3DDevice->EndScene() ); }
void CameraEffect::endScene(void) { assert( Camera::_currentCamera == this ); Camera::_currentCamera = NULL; _dxCR( _rts->EndScene( D3DX_FILTER_LINEAR ) ); _effectBlocked = false; }
void dxRenderLines(int numLines, Line* lines, const Color* color, Matrix* ltm) { assert( numLines <= MAX_LINES ); if( !_lineVB ) createLineVB(); // make line vertex data void* lockedData; _dxCR( _lineVB->Lock( 0, sizeof(Vector)*2*numLines, &lockedData, D3DLOCK_DISCARD ) ); char* vertexStream = (char*)( lockedData ); int offset = 0; for( int i=0; i<numLines; i++ ) { *( (Vector*)( vertexStream + offset ) ) = ( lines + i )->start; offset += sizeof( Vector ); *( (Vector*)( vertexStream + offset ) ) = ( lines + i )->end; offset += sizeof( Vector ); } _dxCR( _lineVB->Unlock() ); // render line if( ltm ) _dxCR( iDirect3DDevice->SetTransform( D3DTS_WORLD, ltm ) ); else _dxCR( iDirect3DDevice->SetTransform( D3DTS_WORLD, &identity ) ); setWireRenderStates( color ); _dxCR( dxSetFVF( D3DFVF_XYZ ) ); _dxCR( dxSetStreamSource( 0, _lineVB, 0, sizeof(Vector) ) ); _dxCR( dxDrawPrimitive( D3DPT_LINELIST, 0, numLines ) ); }
static void createLineVB(void) { _dxCR( dxCreateVertexBuffer( 2 * MAX_LINES * sizeof(Vector), D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC, D3DFVF_XYZ, D3DPOOL_DEFAULT, &_lineVB, NULL ) ); }
void BSPSector::illuminate(unsigned int lightset) { int lightIndex = 0; for( LightI lightI = _lightsInSector.begin(); lightI != _lightsInSector.end(); lightI++ ) { if( (*lightI)->lightset() == lightset && (*lightI)->getType() != engine::ltAmbient ) { _dxCR( dxLightEnable( lightIndex, TRUE ) ); (*lightI)->apply( lightIndex ); if( lightIndex++ > 7 ) break; } } for( int i=lightIndex; i<8; i++ ) { _dxCR( dxLightEnable( i, FALSE ) ); } }
void CameraEffect::applyDOF(void) { assert( _effectTexture ); // setup effect parameters _pEffect->SetInt( "quality", _quality ); _pEffect->SetFloat( "weight", _weight ); _pEffect->SetFloat( "screenWidth", float( Engine::instance->screenWidth ) ); _pEffect->SetFloat( "screenHeight", float( Engine::instance->screenHeight ) ); _pEffect->SetTexture( "newImage", _newImage->iDirect3DTexture() ); _pEffect->SetTexture( "argumentImage", _effectTexture->iDirect3DTexture() ); _pEffect->SetTechnique( "DepthOfField" ); // blend new image & depth map into the render target IDirect3DSurface9* iDirect3DSurface; _dxCR( _renderTarget->iDirect3DTexture()->GetSurfaceLevel( 0, &iDirect3DSurface ) ); _dxCR( _rts->BeginScene( iDirect3DSurface, &_viewPort ) ); iDirect3DSurface->Release(); _dxCR( iDirect3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, black, 1.0f, 0L ) ); UINT numPasses; _pEffect->Begin( &numPasses, D3DXFX_DONOTSAVESTATE ); for( UINT iPass = 0; iPass < numPasses; iPass++ ) { _pEffect->BeginPass( iPass ); dxRenderRect( 0, 0, Engine::instance->screenWidth-1, Engine::instance->screenHeight-1, white ); _pEffect->EndPass(); } _pEffect->End(); iDirect3DDevice->SetPixelShader(NULL); _rts->EndScene( D3DX_FILTER_LINEAR ); _pEffect->SetTexture( "newImage", NULL ); _pEffect->SetTexture( "argumentImage", NULL ); }
void Rain::onResetDevice(void) { // create rendering resources // WORD is size of index (16 bits), 6 is number of indices per one particle _dxCR( iDirect3DDevice->CreateIndexBuffer( sizeof(WORD) * 6 * maxParticlesPerPass, D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &_indexBuffer, NULL ) ); // 4 is number of vertices per one particle _dxCR( iDirect3DDevice->CreateVertexBuffer( sizeof(RainParticleVertex) * 4 * maxParticlesPerPass, D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC, particleFVF, D3DPOOL_DEFAULT, &_vertexBuffer, NULL ) ); }
void ScreenCamera::beginScene(unsigned int clearMode, const Vector4f& clearColor) { assert( Camera::_currentCamera == NULL ); Camera::_currentCamera = this; // frame synchronization phase Frame::synchronizeAll(); _dxCR( iDirect3DDevice->SetViewport( &_viewPort ) ); if( clearMode ) _dxCR( iDirect3DDevice->Clear( 0, NULL, clearMode, wrap( clearColor ), 1.0f, 0L ) ); _dxCR( iDirect3DDevice->BeginScene() ); // setup transformation matrices if( _frame ) { D3DXMatrixInverse( &viewMatrix, NULL, &_frame->LTM ); _dxCR( iDirect3DDevice->SetTransform( D3DTS_VIEW, &viewMatrix ) ); } projectionMatrix = _projection; _dxCR( iDirect3DDevice->SetTransform( D3DTS_PROJECTION, &_projection ) ); viewPort = _viewPort; eyePos = dxPos( &_frame->LTM ); eyeDirection = dxAt( &_frame->LTM ); fov = _fov; nearClipPlane = _nearClipPlane; farClipPlane = _farClipPlane; // normalize camera eye D3DXVec3Normalize( &eyeDirection, &eyeDirection ); // update frustrum planes updateFrustrum(); }
void CameraEffect::beginScene(unsigned int clearMode, const Vector4f& clearColor) { _effectBlocked = true; assert( Camera::_currentCamera == NULL ); Camera::_currentCamera = this; // frame synchronization phase Frame::synchronizeAll(); _dxCR( iDirect3DDevice->SetViewport( &_viewPort ) ); if( _currentEffect == engine::pfxNone || _currentEffect == engine::pfxBloom ) { IDirect3DSurface9* iDirect3DSurface; _renderTarget->iDirect3DTexture()->GetSurfaceLevel( 0, &iDirect3DSurface ); _dxCR( _rts->BeginScene( iDirect3DSurface, &_viewPort ) ); iDirect3DSurface->Release(); } else { IDirect3DSurface9* iDirect3DSurface; _newImage->iDirect3DTexture()->GetSurfaceLevel( 0, &iDirect3DSurface ); _dxCR( _rts->BeginScene( iDirect3DSurface, &_viewPort ) ); iDirect3DSurface->Release(); } if( clearMode ) _dxCR( iDirect3DDevice->Clear( 0, NULL, clearMode, wrap( clearColor ), 1.0f, 0L ) ); // setup transformation matrices if( _frame ) { D3DXMatrixInverse( &Camera::viewMatrix, NULL, &_frame->LTM ); _dxCR( iDirect3DDevice->SetTransform( D3DTS_VIEW, &Camera::viewMatrix ) ); } Camera::projectionMatrix = _projection; _dxCR( iDirect3DDevice->SetTransform( D3DTS_PROJECTION, &_projection ) ); Camera::viewPort = _viewPort; Camera::eyePos = dxPos( &_frame->LTM ); Camera::eyeDirection = dxAt( &_frame->LTM ); Camera::fov = _fov; Camera::nearClipPlane = _nearClipPlane; Camera::farClipPlane = _farClipPlane; // normalize camera eye D3DXVec3Normalize( &eyeDirection, &eyeDirection ); // frustrum planes updateFrustrum(); }
CameraEffect::CameraEffect(Texture* renderTarget) { assert( renderTarget->iDirect3DTexture() ); _frame = NULL; _renderTarget = renderTarget; _renderTarget->_numReferences++; _currentEffect = engine::pfxNone; _newImage = NULL; _prevImage = NULL; _effectTexture = NULL; _prevIsEmpty = true; _weight = 0; _quality = 0; // create render-to-surface for specified render target _dxCR( D3DXCreateRenderToSurface( iDirect3DDevice, _renderTarget->getWidth(), _renderTarget->getHeight(), _renderTarget->getFormat(), true, dxPresentParams.AutoDepthStencilFormat, &_rts ) ); // setup viewport _viewPort.X = 0, _viewPort.Y = 0; _viewPort.Width = renderTarget->getWidth(); _viewPort.Height = renderTarget->getHeight(); _viewPort.MinZ = 0; _viewPort.MaxZ = 1; _fov = 60.0f; _nearClipPlane = 0; _farClipPlane = 100; updateProjection(); }
void BSPSector::render(void) { currentSector = this; Engine::statistics.bspRendered++; illuminate( 0 ); if( _geometry ) { _dxCR( iDirect3DDevice->SetTransform( D3DTS_WORLD, &identity ) ); _geometry->render(); } for( AtomicI atomicI = _atomicsInSector.begin(); atomicI != _atomicsInSector.end(); atomicI++ ) { if( (*atomicI)->flags() & engine::afRender ) (*atomicI)->render(); } currentSector = NULL; }
void BSPSector::renderDepthMap(void) { currentSector = this; Engine::statistics.bspRendered++; // render sector geometry if( _geometry ) { _dxCR( iDirect3DDevice->SetTransform( D3DTS_WORLD, &identity ) ); _geometry->renderDepthMap(); } // render sector atomics (alpha-sorting is not significant now!) for( AtomicI atomicI = _atomicsInSector.begin(); atomicI != _atomicsInSector.end(); atomicI++ ) { if( (*atomicI)->flags() & engine::afRender ) (*atomicI)->renderDepthMap(); } currentSector = NULL; }
void dxRenderLine(Line* line, const Color* color, Matrix* ltm) { if( !_lineVB ) createLineVB(); // make line vertex data void* lockedData; _dxCR( _lineVB->Lock( 0, sizeof(Vector)*2, &lockedData, D3DLOCK_DISCARD ) ); char* vertexStream = (char*)( lockedData ); int offset = 0; *( (Vector*)( vertexStream + offset ) ) = line->start; offset += sizeof( Vector ); *( (Vector*)( vertexStream + offset ) ) = line->end; offset += sizeof( Vector ); _dxCR( _lineVB->Unlock() ); // render line if( ltm ) _dxCR( iDirect3DDevice->SetTransform( D3DTS_WORLD, ltm ) ); else { _dxCR( iDirect3DDevice->SetTransform( D3DTS_WORLD, &identity ) ); } setWireRenderStates( color ); _dxCR( dxSetFVF( D3DFVF_XYZ ) ); _dxCR( dxSetStreamSource( 0, _lineVB, 0, sizeof(Vector) ) ); _dxCR( dxDrawPrimitive( D3DPT_LINELIST, 0, 1 ) ); }
void Geometry::renderDepthMap(void) { // for each triangle - the fill rule is simple opaque black-colored painting _dxCR( dxSetRenderState( D3DRS_TEXTUREFACTOR, depthColor ) ); _dxCR( dxSetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 ) ); _dxCR( dxSetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TFACTOR ) ); _dxCR( dxSetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 ) ); _dxCR( dxSetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR ) ); _dxCR( dxSetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE ) ); _dxCR( dxSetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE ) ); int shaderId; int numAttributes = _mesh->NumAttributeGroups; for( int i=0; i<numAttributes; i++ ) { shaderId = _mesh->getAttributeId( i ); if( !shader( shaderId )->isInvisible() ) { _mesh->renderSubset( i, shader( shaderId ) ); } } }
void Camera::renderTextureAdditive(engine::ITexture* texture) { Texture* t = dynamic_cast<Texture*>( texture ); assert( t ); t->apply( 0 ); dxSetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0 ); dxSetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 ); dxSetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE ); dxSetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 ); dxSetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE ); _dxCR( dxSetRenderState( D3DRS_ALPHABLENDENABLE, TRUE ) ); _dxCR( dxSetRenderState( D3DRS_SRCBLEND, D3DBLEND_ONE ) ); _dxCR( dxSetRenderState( D3DRS_DESTBLEND, D3DBLEND_ONE ) ); _dxCR( dxSetRenderState( D3DRS_BLENDOP, D3DBLENDOP_ADD ) ); dxRenderRect( 0, 0, _viewPort.Width, _viewPort.Height, white ); _dxCR( dxSetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA ) ); _dxCR( dxSetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA ) ); }
void FxWater::render(Mesh* mesh, int subsetId, Shader* shader) { if( !_effect ) { mesh->renderSubset( subsetId, shader ); return; } shader->apply(); // ambient lighting _effect->SetVector( "ambientColor", (D3DXVECTOR4*)( Shader::globalAmbient() ) ); Matrix world; _dxCR( iDirect3DDevice->GetTransform( D3DTS_WORLD, &world ) ); Matrix iWorld; D3DXMatrixInverse( &iWorld, NULL, &world ); // camera properties (in object space) Vector cameraPos; Vector cameraDir; D3DXVec3TransformCoord( &cameraPos, &Camera::eyePos, &iWorld ); D3DXVec3TransformNormal( &cameraDir, &Camera::eyeDirection, &iWorld ); Quartector cameraPosQ( cameraPos.x, cameraPos.y, cameraPos.z, 1.0f ); Quartector cameraDirQ( cameraDir.x, cameraDir.y, cameraDir.z, 0.0f ); _effect->SetVector( "cameraPos", &cameraPosQ ); _effect->SetVector( "cameraDir", &cameraDirQ ); // dynamic lighting (transformed in object space) D3DLIGHT9 lightProperties; BOOL lightEnabled; Quartector lightPos; D3DCOLORVALUE lightColor; Vector temp, temp2; for( unsigned int lightId=0; lightId<8; lightId++ ) { // check light is enabled _dxCR( iDirect3DDevice->GetLightEnable( lightId, &lightEnabled ) ); if( !lightEnabled ) continue; // retrieve light properties _dxCR( iDirect3DDevice->GetLight( lightId, &lightProperties ) ); if( lightProperties.Type == D3DLIGHT_POINT ) { // retrieve light props temp.x = lightProperties.Position.x; temp.y = lightProperties.Position.y; temp.z = lightProperties.Position.z; D3DXVec3TransformCoord( &temp2, &temp, &iWorld ); lightPos.x = temp2.x; lightPos.y = temp2.y; lightPos.z = temp2.z; lightPos.w = 1.0f; lightColor = lightProperties.Diffuse; // setup effect _effect->SetVector( "lightPos", (D3DXVECTOR4*)&lightPos ); _effect->SetVector( "lightColor", (D3DXVECTOR4*)&lightColor ); break; } } // material color properties D3DMATERIAL9 material; iDirect3DDevice->GetMaterial( &material ); _effect->SetVector( "materialDiffuse", (D3DXVECTOR4*)&material.Diffuse ); _effect->SetVector( "materialSpecular", (D3DXVECTOR4*)&material.Specular ); _effect->SetFloat( "materialPower", material.Power ); // world matrix // _effect->SetMatrix( "world", &world ); // inversion of world matrix // _effect->SetMatrix( "iWorld", &iWorld ); // WVP matrix Matrix worldViewProj; D3DXMatrixMultiply( &worldViewProj, &world, &Camera::viewMatrix ); D3DXMatrixMultiply( &worldViewProj, &worldViewProj, &Camera::projectionMatrix ); _effect->SetMatrix( "worldViewProj", &worldViewProj ); // base texture _effect->SetTexture( "baseTexture", shader->layerTexture( 0 )->iDirect3DTexture() ); // normal maps _effect->SetTexture( "normalMap", shader->normalMap()->iDirect3DTexture() ); // environment map _effect->SetTexture( "environmentMap", shader->environmentMap()->iDirect3DCubeTexture() ); // arguments Quartector uvOffset1( _arguments.uvOffset1.x, _arguments.uvOffset1.y, 0, 0 ); Quartector uvOffset2( _arguments.uvOffset2.x, _arguments.uvOffset2.y, 0, 0 ); _effect->SetFloat( "Reflectivity", _arguments.reflectivity ); _effect->SetVector( "UvOffset1", &uvOffset1 ); _effect->SetVector( "UvOffset2", &uvOffset2 ); // render unsigned int numPasses; _effect->Begin( &numPasses, D3DXFX_DONOTSAVESTATE ); for( unsigned int pass = 0; pass < numPasses; pass++ ) { _effect->BeginPass( pass ); mesh->renderSubset( subsetId, shader ); // ms2010error _effect->EndPass(); } _effect->End(); _effect->SetTexture( "baseTexture", NULL ); _effect->SetTexture( "normalMap", NULL ); _effect->SetTexture( "environmentMap", NULL ); iDirect3DDevice->SetVertexShader(NULL); iDirect3DDevice->SetPixelShader(NULL); }
void CameraEffect::applyBloom(void) { // 1) _renderTarget -> _newImage with BrightPass effect // setup effect _pEffect->SetInt( "quality", _quality ); _pEffect->SetFloat( "weight", _weight ); _pEffect->SetFloat( "screenWidth", float( _renderTarget->getWidth() ) ); _pEffect->SetFloat( "screenHeight", float( _renderTarget->getHeight() ) ); _pEffect->SetTexture( "newImage", _renderTarget->iDirect3DTexture() ); _pEffect->SetTechnique( "BrightPass" ); // render effect IDirect3DSurface9* iDirect3DSurface; _dxCR( _newImage->iDirect3DTexture()->GetSurfaceLevel( 0, &iDirect3DSurface ) ); _dxCR( _rts->BeginScene( iDirect3DSurface, &_viewPort ) ); iDirect3DSurface->Release(); _dxCR( iDirect3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, black, 1.0f, 0L ) ); UINT numPasses; _pEffect->Begin( &numPasses, D3DXFX_DONOTSAVESTATE ); UINT iPass; for( iPass = 0; iPass < numPasses; iPass++ ) { _pEffect->BeginPass( iPass ); dxRenderRect( 0, 0, _renderTarget->getWidth()-1, _renderTarget->getHeight()-1, white ); _pEffect->EndPass(); } _pEffect->End(); _rts->EndScene( D3DX_FILTER_LINEAR ); // 2) setup bloom float filterWidth = float( _renderTarget->getWidth() ); float filterHeight = float( _renderTarget->getHeight() ); Quartector texelKernel[13]; for( int i=0; i<13; i++ ) { texelKernel[i] = Quartector( float( i-6 ) / filterWidth, float( i-6 ) / filterHeight, 0,0 ); } _pEffect->SetVectorArray( "TexelKernel", texelKernel, 13 ); // 3) _newImage -> _renderTarget with HorizontalBloom effect // setup effect _pEffect->SetInt( "quality", _quality ); _pEffect->SetFloat( "weight", _vector[0] ); _pEffect->SetFloat( "screenWidth", float( _renderTarget->getWidth() ) ); _pEffect->SetFloat( "screenHeight", float( _renderTarget->getHeight() ) ); _pEffect->SetTexture( "newImage", _newImage->iDirect3DTexture() ); _pEffect->SetTechnique( "HorizontalBloom" ); // render effect _dxCR( _renderTarget->iDirect3DTexture()->GetSurfaceLevel( 0, &iDirect3DSurface ) ); _dxCR( _rts->BeginScene( iDirect3DSurface, &_viewPort ) ); iDirect3DSurface->Release(); _dxCR( iDirect3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, black, 1.0f, 0L ) ); _pEffect->Begin( &numPasses, D3DXFX_DONOTSAVESTATE ); for( iPass = 0; iPass < numPasses; iPass++ ) { _pEffect->BeginPass( iPass ); dxRenderRect( 0, 0, _renderTarget->getWidth()-1, _renderTarget->getHeight()-1, white ); _pEffect->EndPass(); } _pEffect->End(); _rts->EndScene( D3DX_FILTER_LINEAR ); // 4) _renderTarget -> _newImage with VerticalBloom effect // setup effect _pEffect->SetInt( "quality", _quality ); _pEffect->SetFloat( "weight", _vector[0] ); _pEffect->SetFloat( "screenWidth", float( _renderTarget->getWidth() ) ); _pEffect->SetFloat( "screenHeight", float( _renderTarget->getHeight() ) ); _pEffect->SetTexture( "newImage", _renderTarget->iDirect3DTexture() ); _pEffect->SetTechnique( "VerticalBloom" ); // render effect _dxCR( _newImage->iDirect3DTexture()->GetSurfaceLevel( 0, &iDirect3DSurface ) ); _dxCR( _rts->BeginScene( iDirect3DSurface, &_viewPort ) ); iDirect3DSurface->Release(); _dxCR( iDirect3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, black, 1.0f, 0L ) ); _pEffect->Begin( &numPasses, D3DXFX_DONOTSAVESTATE ); for( iPass = 0; iPass < numPasses; iPass++ ) { _pEffect->BeginPass( iPass ); dxRenderRect( 0, 0, _renderTarget->getWidth()-1, _renderTarget->getHeight()-1, white ); _pEffect->EndPass(); } _pEffect->End(); _rts->EndScene( D3DX_FILTER_LINEAR ); // 5) copy _newImage to _renderTarget with no effect // setup effect _pEffect->SetInt( "quality", _quality ); _pEffect->SetFloat( "weight", _vector[0] ); _pEffect->SetFloat( "screenWidth", float( _renderTarget->getWidth() ) ); _pEffect->SetFloat( "screenHeight", float( _renderTarget->getHeight() ) ); _pEffect->SetTexture( "newImage", _newImage->iDirect3DTexture() ); _pEffect->SetTechnique( "CopySurface" ); // render effect _dxCR( _renderTarget->iDirect3DTexture()->GetSurfaceLevel( 0, &iDirect3DSurface ) ); _dxCR( _rts->BeginScene( iDirect3DSurface, &_viewPort ) ); iDirect3DSurface->Release(); _dxCR( iDirect3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, black, 1.0f, 0L ) ); _pEffect->Begin( &numPasses, D3DXFX_DONOTSAVESTATE ); for( iPass = 0; iPass < numPasses; iPass++ ) { _pEffect->BeginPass( iPass ); dxRenderRect( 0, 0, _renderTarget->getWidth()-1, _renderTarget->getHeight()-1, white ); _pEffect->EndPass(); } _pEffect->End(); _rts->EndScene( D3DX_FILTER_LINEAR ); // reset shaders iDirect3DDevice->SetPixelShader(NULL); _pEffect->SetTexture( "newImage", NULL ); }
void CameraEffect::applyMotionBlur(void) { if( _prevIsEmpty || _weight == 0.0f ) { _prevIsEmpty = false; // setup new image for texture rendering _newImage->apply( 0 ); dxSetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0 ); dxSetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 ); dxSetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE ); dxSetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 ); dxSetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE ); dxSetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE ); dxSetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE ); // render new image in to the previous image IDirect3DSurface9* iDirect3DSurface; _dxCR( _prevImage->iDirect3DTexture()->GetSurfaceLevel( 0, &iDirect3DSurface ) ); _dxCR( _rts->BeginScene( iDirect3DSurface, &_viewPort ) ); iDirect3DSurface->Release(); _dxCR( iDirect3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, black, 1.0f, 0L ) ); dxRenderRect( 0, 0, Engine::instance->screenWidth-1, Engine::instance->screenHeight-1, white ); _dxCR( _rts->EndScene( D3DX_FILTER_LINEAR ) ); // render new image in to the render target _dxCR( _renderTarget->iDirect3DTexture()->GetSurfaceLevel( 0, &iDirect3DSurface ) ); _dxCR( _rts->BeginScene( iDirect3DSurface, &_viewPort ) ); iDirect3DSurface->Release(); _dxCR( iDirect3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, black, 1.0f, 0L ) ); dxRenderRect( 0, 0, Engine::instance->screenWidth-1, Engine::instance->screenHeight-1, white ); _dxCR( _rts->EndScene( D3DX_FILTER_LINEAR ) ); } else { // setup blending of new image & previous image _prevImage->apply( 0 ); _newImage->apply( 1 ); Color w = D3DCOLOR_RGBA( int(255 * _weight), int(255 * _weight), int(255 * _weight), 255 ); dxSetRenderState( D3DRS_TEXTUREFACTOR, w ); dxSetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0 ); dxSetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 ); dxSetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE ); dxSetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 ); dxSetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE ); dxSetTextureStageState( 1, D3DTSS_TEXCOORDINDEX, 0 ); dxSetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_LERP ); dxSetTextureStageState( 1, D3DTSS_COLORARG0, D3DTA_TFACTOR ); dxSetTextureStageState( 1, D3DTSS_COLORARG1, D3DTA_CURRENT ); dxSetTextureStageState( 1, D3DTSS_COLORARG2, D3DTA_TEXTURE ); dxSetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 ); dxSetTextureStageState( 1, D3DTSS_ALPHAARG1, D3DTA_CURRENT ); dxSetTextureStageState( 2, D3DTSS_ALPHAOP, D3DTOP_DISABLE ); dxSetTextureStageState( 2, D3DTSS_COLOROP, D3DTOP_DISABLE ); // blend new image & prev image into the render target IDirect3DSurface9* iDirect3DSurface; _dxCR( _renderTarget->iDirect3DTexture()->GetSurfaceLevel( 0, &iDirect3DSurface ) ); _dxCR( _rts->BeginScene( iDirect3DSurface, &_viewPort ) ); iDirect3DSurface->Release(); _dxCR( iDirect3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, black, 1.0f, 0L ) ); dxRenderRect( 0, 0, Engine::instance->screenWidth-1, Engine::instance->screenHeight-1, white ); _rts->EndScene( D3DX_FILTER_LINEAR ); // setup rendering of render target _renderTarget->apply( 0 ); dxSetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0 ); dxSetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 ); dxSetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE ); dxSetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 ); dxSetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE ); dxSetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE ); dxSetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE ); // copy target image into the previous image _dxCR( _prevImage->iDirect3DTexture()->GetSurfaceLevel( 0, &iDirect3DSurface ) ); _dxCR( _rts->BeginScene( iDirect3DSurface, &_viewPort ) ); iDirect3DSurface->Release(); _dxCR( iDirect3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, black, 1.0f, 0L ) ); dxRenderRect( 0, 0, Engine::instance->screenWidth-1, Engine::instance->screenHeight-1, white ); _dxCR( _rts->EndScene( D3DX_FILTER_LINEAR ) ); } }
void Rain::render(void) { float dot; Vector x,y,z; Vector vector; Matrix m; unsigned int i; // culling value float cullDot = cos( Camera::fov * D3DX_PI / 180.0f ); // lock buffers void* vertexData = NULL; void* indexData = NULL; _dxCR( _vertexBuffer->Lock( 0, _numParticles * 4 * sizeof( RainParticleVertex ), &vertexData, D3DLOCK_DISCARD ) ); _dxCR( _indexBuffer->Lock( 0, _numParticles * 6 * sizeof( WORD ), &indexData, D3DLOCK_DISCARD ) ); assert( vertexData ); assert( indexData ); RainParticleVertex* vertex = (RainParticleVertex*)( vertexData ); WORD* index = (WORD*)( indexData ); // render particles RainParticle* particle; unsigned int numVisibleParticles = 0; for( i=0; i<_numParticles; i++ ) { // particle pointer particle = _particles + i; // build billboard matrix z = particle->pos - Camera::eyePos; D3DXVec3Normalize( &z, &z ); // particle culling dot = D3DXVec3Dot( &z, &Camera::eyeDirection ); if( -dot <= cullDot ) continue; // rest of billboard matrix D3DXVec3Scale( &y, &particle->vel, -1 ); D3DXVec3Normalize( &y, &y ); D3DXVec3Cross( &x, &y, &z ); D3DXVec3Normalize( &x, &x ); x.x *= rainXScale, x.y *= rainXScale, x.z *= rainXScale; y.x *= rainYScale, y.y *= rainYScale, y.z *= rainYScale; z.x *= rainZScale, z.y *= rainZScale, z.z *= rainZScale; // finalize m._11 = x.x, m._12 = x.y, m._13 = x.z, m._14 = 0.0f, m._21 = y.x, m._22 = y.y, m._23 = y.z, m._24 = 0.0f, m._31 = z.x, m._32 = z.y, m._33 = z.z, m._34 = 0.0f, m._41 = 0.0f, m._42 = 0.0f, m._43 = 0.0f, m._44 = 1.0f; // transform vertex coordinates by matrix D3DXVec3TransformCoord( &vertex[0].pos, &billboardVertices[0], &m ); D3DXVec3TransformCoord( &vertex[1].pos, &billboardVertices[1], &m ); D3DXVec3TransformCoord( &vertex[2].pos, &billboardVertices[2], &m ); D3DXVec3TransformCoord( &vertex[3].pos, &billboardVertices[3], &m ); vertex[0].pos.x += particle->pos.x, vertex[0].pos.y += particle->pos.y, vertex[0].pos.z += particle->pos.z, vertex[1].pos.x += particle->pos.x, vertex[1].pos.y += particle->pos.y, vertex[1].pos.z += particle->pos.z, vertex[2].pos.x += particle->pos.x, vertex[2].pos.y += particle->pos.y, vertex[2].pos.z += particle->pos.z, vertex[3].pos.x += particle->pos.x, vertex[3].pos.y += particle->pos.y, vertex[3].pos.z += particle->pos.z; // setup uvs vertex[0].uv = billboardUVs[0]; vertex[1].uv = billboardUVs[1]; vertex[2].uv = billboardUVs[2]; vertex[3].uv = billboardUVs[3]; // setup colors vertex[0].color = vertex[1].color = vertex[2].color = vertex[3].color = _ambient; // indices... index[0] = numVisibleParticles * 4 + 0; index[1] = numVisibleParticles * 4 + 1; index[2] = numVisibleParticles * 4 + 2; index[3] = numVisibleParticles * 4 + 0; index[4] = numVisibleParticles * 4 + 2; index[5] = numVisibleParticles * 4 + 3; // next particle vertex += 4, index += 6, numVisibleParticles++; } // unlock buffers _vertexBuffer->Unlock(); _indexBuffer->Unlock(); // render buffers // render _dxCR( dxSetRenderState( D3DRS_LIGHTING, FALSE ) ); _dxCR( dxSetRenderState( D3DRS_ZWRITEENABLE, FALSE ) ); _dxCR( dxSetRenderState( D3DRS_COLORVERTEX, TRUE ) ); _dxCR( dxSetRenderState( D3DRS_AMBIENTMATERIALSOURCE, D3DMCS_MATERIAL ) ); _dxCR( dxSetRenderState( D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_COLOR1 ) ); _dxCR( dxSetRenderState( D3DRS_SPECULARMATERIALSOURCE, D3DMCS_MATERIAL ) ); _dxCR( dxSetRenderState( D3DRS_EMISSIVEMATERIALSOURCE, D3DMCS_MATERIAL ) ); _shader->apply(); _dxCR( iDirect3DDevice->SetTransform( D3DTS_WORLD, &identity ) ); _dxCR( iDirect3DDevice->SetFVF( particleFVF ) ); _dxCR( iDirect3DDevice->SetStreamSource( 0, _vertexBuffer, 0, sizeof( RainParticleVertex ) ) ); _dxCR( iDirect3DDevice->SetIndices( _indexBuffer ) ); _dxCR( iDirect3DDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 0, numVisibleParticles * 4, 0, numVisibleParticles * 2 ) ); _dxCR( dxSetRenderState( D3DRS_ZWRITEENABLE, TRUE ) ); _dxCR( dxSetRenderState( D3DRS_LIGHTING, TRUE ) ); // debug info //mainwnd::IMainWnd* iMainWnd; //queryInterfaceT( "MainWnd", &iMainWnd ); //iMainWnd->setWindowText( strformat( "Rain::render(): numVisibleParticles = %d", numVisibleParticles ).c_str() ); }
void Geometry::render(void) { // add "false &&" to enable lights for all geometry if( _numPrelights ) { _dxCR( dxSetRenderState( D3DRS_LIGHTING, FALSE ) ); _dxCR( dxSetRenderState( D3DRS_COLORVERTEX, TRUE ) ); _dxCR( dxSetRenderState( D3DRS_AMBIENTMATERIALSOURCE, D3DMCS_MATERIAL ) ); _dxCR( dxSetRenderState( D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_MATERIAL ) ); _dxCR( dxSetRenderState( D3DRS_SPECULARMATERIALSOURCE, D3DMCS_MATERIAL ) ); _dxCR( dxSetRenderState( D3DRS_EMISSIVEMATERIALSOURCE, D3DMCS_COLOR1 ) ); } else { _dxCR( dxSetRenderState( D3DRS_LIGHTING, TRUE ) ); _dxCR( dxSetRenderState( D3DRS_COLORVERTEX, FALSE ) ); _dxCR( dxSetRenderState( D3DRS_AMBIENTMATERIALSOURCE, D3DMCS_MATERIAL ) ); _dxCR( dxSetRenderState( D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_MATERIAL ) ); _dxCR( dxSetRenderState( D3DRS_SPECULARMATERIALSOURCE, D3DMCS_MATERIAL ) ); _dxCR( dxSetRenderState( D3DRS_EMISSIVEMATERIALSOURCE, D3DMCS_MATERIAL ) ); } int shaderId; int numAttributes = _mesh->NumAttributeGroups; for( int i=0; i<numAttributes; i++ ) { shaderId = _mesh->getAttributeId( i ); if( !shader( shaderId )->isInvisible() ) { // shading technique contains alpha blending/testing? if( ( _shaders[shaderId]->getFlags() & engine::sfAlphaBlending || _shaders[shaderId]->getFlags() & engine::sfAlphaTesting ) && ( Atomic::currentAtomic || BSPSector::currentSector ) ) { if( Atomic::currentAtomic ) { BSP* bsp = getAtomicBSP( Atomic::currentAtomic ); if( bsp ) bsp->addAlphaGeometry( Atomic::currentAtomic, i ); } else if( BSPSector::currentSector ) { BSP* bsp = BSPSector::currentSector->bsp(); assert( bsp ); bsp->addAlphaGeometry( BSPSector::currentSector, i ); } } else if( _shaders[shaderId]->effect() ) { reinterpret_cast<Effect*>( _shaders[shaderId]->effect() )->render( _mesh, i, _shaders[shaderId] ); dxSetVertexShader( NULL ); dxSetPixelShader( NULL ); } else { if( _mesh->pSkinInfo ) { int i=0; } _shaders[shaderId]->apply(); _mesh->renderSubset( i, _shaders[shaderId] ); // if shader use non-default lightset, then recalculate // lighting back to default lightset if( _shaders[shaderId]->getLightset() ) { BSP::currentBSP->calculateGlobalAmbient( 0 ); BSPSector::currentSector->illuminate( 0 ); } } } } }
Rain::Rain(unsigned int maxParticles, float emissionSphere, engine::ITexture* texture, Vector4f ambient) { assert( maxParticles > 0 ); assert( texture != NULL ); if( maxParticles > maxParticlesPerPass ) maxParticles = maxParticlesPerPass; _emissionSphere = emissionSphere; _numParticles = maxParticles; _particles = new RainParticle[_numParticles]; _ambient = wrap( ambient ); memset( _particles, 0, sizeof(RainParticle) * _numParticles ); // create rendering resources // WORD is size of index (16 bits), 6 is number of indices per one particle _dxCR( iDirect3DDevice->CreateIndexBuffer( sizeof(WORD) * 6 * maxParticlesPerPass, D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &_indexBuffer, NULL ) ); // 4 is number of vertices per one particle _dxCR( iDirect3DDevice->CreateVertexBuffer( sizeof(RainParticleVertex) * 4 * maxParticlesPerPass, D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC, particleFVF, D3DPOOL_DEFAULT, &_vertexBuffer, NULL ) ); // create shader _shader = dynamic_cast<Shader*>( Engine::instance->createShader( 1, "RainShader" ) ); _shader->_numReferences++; _shader->setLayerTexture( 0, texture ); _shader->setLayerUV( 0, 0 ); _shader->setFlags( _shader->getFlags() | engine::sfAlphaBlending | engine::sfAlphaTesting | engine::sfLighting ); _shader->setFlags( _shader->getFlags() & ~engine::sfCulling ); _shader->setAlphaTestFunction( engine::cfGreater ); _shader->setAlphaTestRef( 0 ); _shader->setSrcBlend( engine::bmSrcAlpha ); _shader->setDestBlend( engine::bmInvSrcAlpha ); _shader->setBlendOp( engine::bpAdd ); _shader->setDiffuseColor( Vector4f( 1,1,1,1 ) ); _shader->setSpecularColor( Vector4f( 1,1,1,1 ) ); // initialize properties _propCenter.x = 0.0f; _propCenter.y = 0.0f; _propCenter.z = 0.0f; _propVelocity.x = 0.0f; _propVelocity.y = -1000.0f; _propVelocity.z = 0.0f; _propNBias = 0.0f; _propTimeSpeed = 1.0f; _useEdgeOffset = 0; // initialize internals _centerOffset.x = _centerOffset.y = _centerOffset.z = 0.0f; _centerVelocity.x = _centerVelocity.y = _centerVelocity.z = 0.0f; // register _rainL.push_back( this ); }
void EffectHLSL::render(Mesh* mesh, int subsetId, Shader* shader) { shader->apply(); // setup effect // ... // ambient lighting _effect->SetVector( "ambientColor", (D3DXVECTOR4*)( Shader::globalAmbient() ) ); Matrix world; _dxCR( iDirect3DDevice->GetTransform( D3DTS_WORLD, &world ) ); Matrix iWorld; D3DXMatrixInverse( &iWorld, NULL, &world ); // camera properties (in object space) if( _flags.camera ) { Vector cameraPos; Vector cameraDir; D3DXVec3TransformCoord( &cameraPos, &Camera::eyePos, &iWorld ); D3DXVec3TransformNormal( &cameraDir, &Camera::eyeDirection, &iWorld ); Quartector cameraPosQ( cameraPos.x, cameraPos.y, cameraPos.z, 1.0f ); Quartector cameraDirQ( cameraDir.x, cameraDir.y, cameraDir.z, 0.0f ); _effect->SetVector( "cameraPos", &cameraPosQ ); _effect->SetVector( "cameraDir", &cameraDirQ ); } // dynamic lighting (transformed in object space) if( _flags.lightPalette ) { D3DLIGHT9 lightProperties; BOOL lightEnabled; Quartector lightPos[8]; D3DCOLORVALUE lightColor[8]; Vector temp, temp2; unsigned int numLights = 0; for( unsigned int lightId=0; lightId<8; lightId++ ) { // exit cycle when palette is full if( numLights == _flags.lightPalette ) break; // check light is enabled _dxCR( iDirect3DDevice->GetLightEnable( lightId, &lightEnabled ) ); if( !lightEnabled ) continue; // retrieve light properties _dxCR( iDirect3DDevice->GetLight( lightId, &lightProperties ) ); if( lightProperties.Type == D3DLIGHT_POINT ) { temp.x = lightProperties.Position.x; temp.y = lightProperties.Position.y; temp.z = lightProperties.Position.z; D3DXVec3TransformCoord( &temp2, &temp, &iWorld ); lightPos[numLights].x = temp2.x; lightPos[numLights].y = temp2.y; lightPos[numLights].z = temp2.z; lightPos[numLights].w = 1.0f; lightColor[numLights] = lightProperties.Diffuse; numLights++; } } if( _flags.lightPalette == 1 && numLights == 1 ) { _effect->SetVector( "lightPos", lightPos ); _effect->SetVector( "lightColor", (D3DXVECTOR4*)lightColor ); } else if( numLights ) { _effect->SetInt( "numLights", numLights ); _effect->SetVectorArray( "lightPos", lightPos, numLights ); _effect->SetVectorArray( "lightColor", (D3DXVECTOR4*)lightColor, numLights ); } } // material color properties if( _flags.material ) { D3DMATERIAL9 material; iDirect3DDevice->GetMaterial( &material ); _effect->SetVector( "materialDiffuse", (D3DXVECTOR4*)&material.Diffuse ); _effect->SetVector( "materialSpecular", (D3DXVECTOR4*)&material.Specular ); _effect->SetFloat( "materialPower", material.Power ); } // world matrix if( _flags.world ) { _effect->SetMatrix( "world", &world ); } // inversion of world matrix if( _flags.iWorld ) { _effect->SetMatrix( "iWorld", &iWorld ); } // WVP matrix if( _flags.worldViewProj ) { Matrix worldViewProj; D3DXMatrixMultiply( &worldViewProj, &world, &Camera::viewMatrix ); D3DXMatrixMultiply( &worldViewProj, &worldViewProj, &Camera::projectionMatrix ); _effect->SetMatrix( "worldViewProj", &worldViewProj ); } // base texture if( _flags.baseTexture ) { _effect->SetTexture( "baseTexture", shader->layerTexture( 0 )->iDirect3DTexture() ); } // normal map if( _flags.normalMap ) { _effect->SetTexture( "normalMap", shader->normalMap()->iDirect3DTexture() ); } // environment map if( _flags.environmentMap ) { _effect->SetTexture( "environmentMap", shader->environmentMap()->iDirect3DCubeTexture() ); } // arguments for( unsigned int i=0; i<_arguments.size(); i++ ) { switch( _arguments[i].type ) { case vtInt: _dxCR( _effect->SetInt( _arguments[i].name.c_str(), *reinterpret_cast<int*>( _buffer + _arguments[i].offset ) ) ); break; case vtFloat: _dxCR( _effect->SetFloat( _arguments[i].name.c_str(), *reinterpret_cast<float*>( _buffer + _arguments[i].offset ) ) ); break; case vtVector4f: _dxCR( _effect->SetVector( _arguments[i].name.c_str(), reinterpret_cast<Quartector*>( _buffer + _arguments[i].offset ) ) ); break; default: assert( !"shouldn't be here!" ); } } // render unsigned int numPasses; _effect->Begin( &numPasses, D3DXFX_DONOTSAVESTATE ); for( unsigned int pass = 0; pass < numPasses; pass++ ) { _effect->BeginPass( pass ); mesh->renderSubset( subsetId, shader ); _effect->EndPass(); } _effect->End(); if( _flags.baseTexture ) _effect->SetTexture( "baseTexture", NULL ); if( _flags.normalMap ) _effect->SetTexture( "normalMap", NULL ); if( _flags.environmentMap ) _effect->SetTexture( "environmentMap", NULL ); iDirect3DDevice->SetVertexShader(NULL); iDirect3DDevice->SetPixelShader(NULL); }
void Geometry::captureMeshData(bool captureShaders) { assert( _mesh->OriginalMeshData.Type == D3DXMESHTYPE_MESH ); // release previous structures if( _vertices ) delete[] _vertices; if( _normals ) delete[] _normals; if( _triangles ) delete[] _triangles; int i; for( i=0; i<engine::maxPrelightLayers; i++ ) { if( _prelights[i] ) { delete[] _prelights[i]; _prelights[i] = NULL; } } for( i=0; i<engine::maxTextureLayers; i++ ) { if( _uvs[i] ) { delete[] _uvs[i]; _uvs[i] = NULL; } } if( _shaders && captureShaders ) { for( i=0; i<_numShaders; i++ ) _shaders[i]->release(); delete[] _shaders; } if( _skinnedVertices ) delete[] _skinnedVertices; _numVertices = _mesh->OriginalMeshData.pMesh->GetNumVertices(); _numTriangles = _mesh->OriginalMeshData.pMesh->GetNumFaces(); _numShaders = _mesh->NumMaterials; _numPrelights = 0; _numUVSets = 0; _sharedShaders = false; // retrieve vertex declaration D3DVERTEXELEMENT9 vertexDeclaration[MAX_FVF_DECL_SIZE]; _dxCR( _mesh->OriginalMeshData.pMesh->GetDeclaration( vertexDeclaration ) ); // analyze vertex declaration _numUVSets = dxGetNumUVs( vertexDeclaration ); _numPrelights = dxGetNumPrelights( vertexDeclaration ); // create device-independent storage _vertices = new Vector[_numVertices]; _normals = new Vector[_numVertices]; _triangles = new Triangle[_numTriangles]; for( int i=0; i<engine::maxPrelightLayers; i++ ) { if( i < _numPrelights ) _prelights[i] = new Color[_numVertices]; else _prelights[i] = NULL; } for( i=0; i<engine::maxTextureLayers; i++ ) { if( i < _numUVSets ) _uvs[i] = new Flector[_numVertices]; else _uvs[i] = NULL; } // retrieve geometry data char* vertexData = (char*)( _mesh->lockVertexBuffer( D3DLOCK_READONLY ) ); char* indexData = (char*)( _mesh->lockIndexBuffer( D3DLOCK_READONLY ) ); DWORD* attrData = (DWORD*)( _mesh->lockAttributeBuffer( D3DLOCK_READONLY ) ); int offset = 0; int j, uvId, prelightId; for( i=0; i<_numVertices; i++ ) { j = prelightId = uvId = 0; while( j<MAX_FVF_DECL_SIZE && vertexDeclaration[j].Stream != 0xFF ) { switch( vertexDeclaration[j].Usage ) { case D3DDECLUSAGE_POSITION: _vertices[i] = *((Vector*)(vertexData+offset)); break; case D3DDECLUSAGE_NORMAL: _normals[i] = *((Vector*)(vertexData+offset)); break; case D3DDECLUSAGE_COLOR: _prelights[prelightId][i] = *((Color*)(vertexData+offset)); prelightId++; break; case D3DDECLUSAGE_TEXCOORD: _uvs[uvId][i] = *((Flector*)(vertexData+offset)); uvId++; break; case D3DDECLUSAGE_BLENDWEIGHT: break; case D3DDECLUSAGE_BLENDINDICES: break; case D3DDECLUSAGE_TANGENT: case D3DDECLUSAGE_BINORMAL: case D3DDECLUSAGE_TESSFACTOR: case D3DDECLUSAGE_POSITIONT: case D3DDECLUSAGE_FOG: case D3DDECLUSAGE_DEPTH: case D3DDECLUSAGE_SAMPLE: break; default: assert( !"shouldn't be here!" ); } offset += dxGetDeclTypeSize( static_cast<D3DDECLTYPE>( vertexDeclaration[j].Type ) ); j++; } } offset = 0; for( i=0; i<_numTriangles; i++ ) { _triangles[i].set( *(WORD*)(indexData+offset), *(WORD*)(indexData+offset+sizeof(WORD)), *(WORD*)(indexData+offset+sizeof(WORD)*2), attrData[i] ); offset += sizeof(WORD)*3; } _mesh->unlockVertexBuffer(); _mesh->unlockIndexBuffer(); _mesh->unlockAttributeBuffer(); // recalculate bounding box _boundingBox.calculate( _numVertices, _vertices ); _boundingSphere.calculate( _numVertices, _vertices ); if( captureShaders ) { _shaders = new Shader*[_numShaders]; memset( _shaders, 0, sizeof(Shader*) * _numShaders ); for( i=0; i<_numShaders; i++ ) { Texture* texture = NULL; if( _mesh->pMaterials[i].pTextureFilename != NULL ) { TextureI textureI = Texture::textures.find( Texture::getTextureNameFromFilePath( _mesh->pMaterials[i].pTextureFilename ) ); if( textureI != Texture::textures.end() ) { texture = textureI->second; } else { texture = Texture::createTexture( _mesh->pMaterials[i].pTextureFilename ); texture->setMagFilter( engine::ftLinear ); texture->setMinFilter( engine::ftLinear ); texture->setMipFilter( engine::ftLinear ); } } Shader* shader = new Shader( texture != NULL ? 1 : 0, strformat( "%s_shader_%d", _name.c_str(), i ).c_str() ); if( texture ) shader->setLayerTexture( 0, texture ); shader->setFlags( shader->getFlags() | engine::sfCaching | engine::sfLighting ); shader->setDiffuseColor( Vector4f( _mesh->pMaterials[i].MatD3D.Diffuse.r, _mesh->pMaterials[i].MatD3D.Diffuse.g, _mesh->pMaterials[i].MatD3D.Diffuse.b, _mesh->pMaterials[i].MatD3D.Diffuse.a ) ); shader->setSpecularColor( Vector4f( _mesh->pMaterials[i].MatD3D.Specular.r, _mesh->pMaterials[i].MatD3D.Specular.g, _mesh->pMaterials[i].MatD3D.Specular.b, _mesh->pMaterials[i].MatD3D.Specular.a ) ); shader->setSpecularPower( _mesh->pMaterials[i].MatD3D.Power ); setShader( i, shader ); } } _vertexDeclaration = dxGetVertexDeclaration( _numUVSets, _numPrelights ); // allocate space for software skinning _skinnedVertices = NULL; if( _mesh->pSkinInfo ) _skinnedVertices = new Vector[_numVertices]; }
XAsset::XAsset(const char* resourcePath) { // report progress if( Engine::instance->progressCallback ) { Engine::instance->progressCallback( wstrformat( Gui::iLanguage->getUnicodeString(4), asciizToUnicode(resourcePath).c_str() ).c_str(), 0.0f, Engine::instance->progressCallbackUserData ); } IResource* resource = getCore()->getResource( resourcePath, "rb" ); assert( resource ); // retrieve file size FILE* file = resource->getFile(); fseek( file, 0, SEEK_END ); int fileSize = ftell( file ); fseek( file, 0, SEEK_SET ); // create buffer & load file data in buffer char* buffer = new char[fileSize]; fread( buffer, 1, fileSize, file ); resource->release(); // report progress if( Engine::instance->progressCallback ) { Engine::instance->progressCallback( wstrformat( Gui::iLanguage->getUnicodeString(4), asciizToUnicode(resourcePath).c_str() ).c_str(), 0.25f, Engine::instance->progressCallbackUserData ); } // create hierarchy allocation interface XAllocateHierarchy xAlloc; // load hierarchy from file D3DXFRAME* rootFrame; ID3DXAnimationController* animController; _dxCR( D3DXLoadMeshHierarchyFromXInMemory( buffer, fileSize, D3DXMESH_MANAGED, iDirect3DDevice, &xAlloc, NULL, &rootFrame, &animController ) ); delete[] buffer; // report progress if( Engine::instance->progressCallback ) { Engine::instance->progressCallback( wstrformat( Gui::iLanguage->getUnicodeString(4), asciizToUnicode(resourcePath).c_str() ).c_str(), 0.5f, Engine::instance->progressCallbackUserData ); } // create D3 animation objects Clump* clump = new Clump( "SCENE_ROOT" ); clump->setFrame( static_cast<Frame*>( rootFrame ) ); if( animController ) { ID3DXAnimationSet* animationSet; const char* animationName; animController->GetAnimationSet( 0, &animationSet ); unsigned int numAnimations = animationSet->GetNumAnimations(); float period = float( animationSet->GetPeriod() ); float numFramesPerSecond = 100.0f/3.0f; float frameTime = 1.0f / numFramesPerSecond; unsigned int numFrames = unsigned int( period * numFramesPerSecond ); if( !numFrames ) numFrames++; AnimationSet* as = new AnimationSet( numAnimations ); for( unsigned int i=0; i<numAnimations; i++ ) { SRT srt; animationSet->GetAnimationNameByIndex( i, &animationName ); Animation* animation = new Animation( animationName, numFrames ); for( unsigned int j=0; j<numFrames; j++ ) { srt.time = j * frameTime; _dxCR( animationSet->GetSRT( srt.time, i, &srt.scale, &srt.rotation, &srt.translation ) ); animation->setKey( j, &srt ); } assert( animation->validateKeys() ); as->setAnimation( i, animation ); } clump->setAnimation( as ); animationSet->Release(); animController->Release(); } // report progress if( Engine::instance->progressCallback ) { Engine::instance->progressCallback( wstrformat( Gui::iLanguage->getUnicodeString(4), asciizToUnicode(resourcePath).c_str() ).c_str(), 0.75f, Engine::instance->progressCallbackUserData ); } // create D3 classes for hierarchy createD3HierarchyClasses( clump, rootFrame ); // resolve nameless phenomena resolveNamelessFrames( clump ); _clumps.push_back( clump ); static_cast<Frame*>( rootFrame )->dirty(); // report progress if( Engine::instance->progressCallback ) { Engine::instance->progressCallback( wstrformat( Gui::iLanguage->getUnicodeString(4), asciizToUnicode(resourcePath).c_str() ).c_str(), 1.0f, Engine::instance->progressCallbackUserData ); } }
void Shader::apply(void) { // simple caching if( ( _lightset == 0 ) && ( _flags & engine::sfCaching ) && ( _lastShader == this ) ) { Engine::instance->statistics.shaderCacheHits++; return; } _lastShader = this; // setup lighting if( _lightset == 0 ) { _materialColor.Emissive = _globalAmbient; dxModulateColorValue( &_materialColor.Emissive, &_materialColor.Diffuse ); _dxCR( dxSetMaterial( &_materialColor ) ); } else { BSP::currentBSP->calculateGlobalAmbient( _lightset ); BSPSector::currentSector->illuminate( _lightset ); _materialColor.Emissive = _globalAmbient; dxModulateColorValue( &_materialColor.Emissive, &_materialColor.Diffuse ); _dxCR( dxSetMaterial( &_materialColor ) ); } // current lightmap (if exists) Texture* lightmap = NULL; if( BSPSector::currentSector ) { if( BSPSector::currentSector->lightmap() ) { lightmap = BSPSector::currentSector->lightmap(); } if( Atomic::currentAtomic && Atomic::currentAtomic->lightmap() ) { lightmap = Atomic::currentAtomic->lightmap(); } } // cannot cache shaders with lightmaps if( lightmap ) _lastShader = NULL; // setup culling if( _flags & engine::sfCulling ) { _dxCR( dxSetRenderState( D3DRS_CULLMODE, D3DCULL_CW ) ); } else { _dxCR( dxSetRenderState( D3DRS_CULLMODE, D3DCULL_NONE ) ); } // setup alpha-blending if( _flags & engine::sfAlphaBlending ) { _dxCR( dxSetRenderState( D3DRS_ALPHABLENDENABLE, TRUE ) ); _dxCR( dxSetRenderState( D3DRS_SRCBLEND, _srcBlend ) ); _dxCR( dxSetRenderState( D3DRS_DESTBLEND, _dstBlend ) ); _dxCR( dxSetRenderState( D3DRS_BLENDOP, _blendOp ) ); } else { _dxCR( dxSetRenderState( D3DRS_ALPHABLENDENABLE, FALSE ) ); } // setup alpha-testing if( _flags & engine::sfAlphaTesting ) { _dxCR( dxSetRenderState( D3DRS_ALPHATESTENABLE, TRUE ) ); _dxCR( dxSetRenderState( D3DRS_ALPHAFUNC, _alphaTestFunction ) ); _dxCR( dxSetRenderState( D3DRS_ALPHAREF, _alphaTestRef ) ); } else { _dxCR( dxSetRenderState( D3DRS_ALPHATESTENABLE, FALSE ) ); } // fixed function pipeline : first texture if( _numLayers ) { assert( _layerTexture[0] ); _layerTexture[0]->apply( 0 ); _dxCR( dxSetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, _layerUV[0] ) ); _dxCR( dxSetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 ) ); _dxCR( dxSetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE ) ); _dxCR( dxSetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 ) ); _dxCR( dxSetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE ) ); } // fixed function pipeline : multitextures int stageId; for( stageId=1; stageId<_numLayers; stageId++ ) { assert( _layerTexture[stageId] ); _layerTexture[stageId]->apply( stageId ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_TEXCOORDINDEX, _layerUV[stageId] ) ); switch( _layerBlending[stageId] ) { case engine::btOver: _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLOROP, D3DTOP_SELECTARG2 ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLORARG2, D3DTA_TEXTURE ) ); case engine::btAdd: _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLOROP, D3DTOP_ADD ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLORARG1, D3DTA_CURRENT ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLORARG2, D3DTA_TEXTURE ) ); break; case engine::btSub: _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLOROP, D3DTOP_SUBTRACT ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLORARG1, D3DTA_CURRENT ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLORARG2, D3DTA_TEXTURE ) ); break; case engine::btAddSigned: _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLOROP, D3DTOP_ADDSIGNED ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLORARG1, D3DTA_CURRENT ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLORARG2, D3DTA_TEXTURE ) ); break; case engine::btAddSmooth: _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLOROP, D3DTOP_ADDSMOOTH ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLORARG1, D3DTA_CURRENT ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLORARG2, D3DTA_TEXTURE ) ); break; case engine::btModulate: _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLOROP, D3DTOP_MODULATE ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLORARG1, D3DTA_CURRENT ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLORARG2, D3DTA_TEXTURE ) ); break; case engine::btModulate2x: _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLOROP, D3DTOP_MODULATE2X ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLORARG1, D3DTA_CURRENT ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLORARG2, D3DTA_TEXTURE ) ); break; case engine::btModulate4x: _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLOROP, D3DTOP_MODULATE4X ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLORARG1, D3DTA_CURRENT ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLORARG2, D3DTA_TEXTURE ) ); break; case engine::btBlendTextureAlpha: _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLOROP, D3DTOP_BLENDTEXTUREALPHA ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLORARG1, D3DTA_TEXTURE ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLORARG2, D3DTA_CURRENT ) ); break; case engine::btLerp: _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLOROP, D3DTOP_LERP ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLORARG1, D3DTA_CURRENT ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLORARG2, D3DTA_TEXTURE ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLORARG0, D3DTA_CONSTANT ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_CONSTANT, _layerConstant[stageId] ) ); break; default: assert( !"Shouldn't be here!" ); } _dxCR( dxSetTextureStageState( stageId, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_ALPHAARG1, D3DTA_CURRENT ) ); } // lighting if( _flags & engine::sfLighting ) { if( lightmap ) { if( _numLayers ) { lightmap->apply( stageId ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_TEXCOORDINDEX, 1 ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLOROP, D3DTOP_MODULATE ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLORARG1, D3DTA_CURRENT ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLORARG2, D3DTA_TEXTURE ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_ALPHAOP, D3DTOP_MODULATE ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_ALPHAARG1, D3DTA_CURRENT ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_ALPHAARG2, D3DTA_TEXTURE ) ); stageId++; } else { lightmap->apply( 0 ); _dxCR( dxSetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 1 ) ); _dxCR( dxSetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 ) ); _dxCR( dxSetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE ) ); _dxCR( dxSetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 ) ); _dxCR( dxSetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE ) ); stageId++; } } else { if( _numLayers ) { _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLOROP, D3DTOP_MODULATE ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLORARG1, D3DTA_CURRENT ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLORARG2, D3DTA_DIFFUSE ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_ALPHAOP, D3DTOP_MODULATE ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_ALPHAARG1, D3DTA_CURRENT ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE ) ); stageId++; } else { _dxCR( dxSetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG2 ) ); _dxCR( dxSetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE ) ); _dxCR( dxSetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG2 ) ); _dxCR( dxSetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE ) ); } } } // normal map ( for programmable pipeline only ) /*if( _normalMap ) { _normalMap->apply( stageId ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_TEXCOORDINDEX, 0 ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLOROP, D3DTOP_SELECTARG1 ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLORARG1, D3DTA_CURRENT ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_ALPHAARG1, D3DTA_CURRENT ) ); stageId++; }*/ // environment map ( for programmable pipeline only ) /*if( _environmentMap ) { _environmentMap->apply( stageId ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR | 1 ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLOROP, D3DTOP_ADD ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLORARG1, D3DTA_CURRENT ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_COLORARG2, D3DTA_TEXTURE ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 ) ); _dxCR( dxSetTextureStageState( stageId, D3DTSS_ALPHAARG1, D3DTA_CURRENT ) ); stageId++; }*/ _dxCR( dxSetTextureStageState( stageId, D3DTSS_ALPHAOP, D3DTOP_DISABLE ) ); //_dxCR( dxSetTextureStageState( stageId, D3DTSS_COLOROP, D3DTOP_DISABLE ) ); // ms2010error }
static void setWireRenderStates(const Color* color) { _dxCR( dxSetRenderState( D3DRS_COLORVERTEX, FALSE ) ); _dxCR( dxSetRenderState( D3DRS_AMBIENTMATERIALSOURCE, D3DMCS_MATERIAL ) ); _dxCR( dxSetRenderState( D3DRS_EMISSIVEMATERIALSOURCE, D3DMCS_MATERIAL ) ); _dxCR( dxSetRenderState( D3DRS_SPECULARMATERIALSOURCE, D3DMCS_MATERIAL ) ); _dxCR( dxSetRenderState( D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_MATERIAL ) ); D3DMATERIAL9 material; material.Diffuse = _wrap( wrap( *color ) ); material.Ambient = material.Diffuse; material.Specular = material.Diffuse; material.Emissive = material.Diffuse; _dxCR( dxSetMaterial( &material ) ); _dxCR( dxSetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG2 ) ); _dxCR( dxSetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE ) ); _dxCR( dxSetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG2 ) ); _dxCR( dxSetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE ) ); _dxCR( dxSetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE ) ); _dxCR( dxSetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE ) ); }