Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

ALerpParticle.cpp

Go to the documentation of this file.
00001 /*
00002 CS Senior Project 2003
00003 Team : Leftfield
00004 Project : ModernWarfare
00005 Members :
00006 - Russ Christensen              <rchriste@cs.utah.edu>
00007 - Todd Smith                    <tcsmith@cs.utah.edu>
00008 - Usit Duongsaa                 <duongsaa@cs.utah.edu>
00009 Copyright 2003 Russ Christensen, Usit Duongsaa, and Todd Smith. All rights reserved.
00010 
00011 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
00012 
00013 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
00014 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 
00015 THIS SOFTWARE IS PROVIDED BY RUSS CHRISTENSEN, USIT DUONGSAA, AND TODD SMITH ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RUSS, USIT, TODD OR OTHER CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00016 */
00017 
00022 #include "AEngine.h"
00023 #include "AState.h"
00024 #include "AUtil.h"
00025 
00026 const int MAX_LERP_COUNT = 4096;
00027 
00028 //------------------------------------------------------------------------------------
00029 void flare_init()
00030 {
00031         int vbByteSize = MAX_LERP_COUNT * sizeof(LerpParticleVertex);
00032         if( FAILED( AS.pDevice->CreateVertexBuffer( vbByteSize, D3DUSAGE_WRITEONLY|D3DUSAGE_DYNAMIC|D3DUSAGE_POINTS,
00033                                                                         0, D3DPOOL_DEFAULT, &AS.flare_vb ) ))
00034                 throw Error("Can't create flareVB");
00035 
00036         AS.flare_startRenderingIndex = 0;
00037         AS.flare_renderCount = 0;
00038 
00039         AS.vshader_lerpParticle = util_loadVertexShader( "shaders\\lerpParticle.vso", dwLerpParticleVertexDecl );
00040         AS.pshader_lerpParticle = util_loadPixelShader(  "shaders\\lerpParticle.pso" );
00041 }
00042 //------------------------------------------------------------------------------------
00043 void flare_process()
00044 {
00045         if( AS.flare_renderCount==0 ) return;   
00046         device_setVertexShader( AS.vshader_lerpParticle );
00047         device_setPixelShader(  AS.pshader_lerpParticle );      
00048         device_setTexture( 3, AS.res_textures[ AS.flare_texture ] );
00049         device_setStreamSource( 0, AS.flare_vb, sizeof(LerpParticleVertex) );
00050 
00051         Matx matx;
00052         FLOAT sizeCalcConst[4] = { 3.0f, 640.0f, 0.0f, 0.0f };
00053         D3DXMatrixTranspose( &matx, &AS.camera_world2projMatrix);
00054         AS.pDevice->SetVertexShaderConstant( 0, &matx, 4 );                     // world->proj matrix
00055         AS.pDevice->SetVertexShaderConstant( 4, &AS.camera_pos[0], 1 );         // camera pos
00056         AS.pDevice->SetVertexShaderConstant( 5, &sizeCalcConst, 1 );            // constants
00057 
00058         device_setRenderStateBlendMode( BlendModes::ADD );      
00059 
00060         device_setRenderState( D3DRS_ZWRITEENABLE, FALSE );
00061         device_setRenderState( D3DRS_ZENABLE, FALSE );
00062         device_setRenderState( D3DRS_POINTSPRITEENABLE, TRUE );
00063         device_setTextureStageState( 3, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
00064         device_setTextureStageState( 3, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
00065         device_setTextureStageState( 3, D3DTSS_MIPFILTER, D3DTEXF_POINT );
00066                 
00067         AS.pDevice->DrawPrimitive( D3DPT_POINTLIST, AS.flare_startRenderingIndex, AS.flare_renderCount );       
00068 
00069         device_setRenderState( D3DRS_POINTSPRITEENABLE, FALSE );
00070         AS.flare_startRenderingIndex += AS.flare_renderCount;
00071         AS.flare_renderCount = 0;
00072 }
00073 //------------------------------------------------------------------------------------
00074 void flare_begin( ATexture texture, int maxParticles )
00075 {
00076         assert(maxParticles>=0 && maxParticles<MAX_LERP_COUNT); 
00077 
00078         // check if there's enough space in current VB
00079         HRESULT res;
00080         if( AS.flare_startRenderingIndex + maxParticles < MAX_LERP_COUNT )
00081         {
00082                 res = AS.flare_vb->Lock( AS.flare_startRenderingIndex*sizeof(LerpParticleVertex),
00083                                                                  maxParticles*sizeof(LerpParticleVertex),
00084                                                                  (BYTE**)&AS.flare_pVertex, D3DLOCK_NOOVERWRITE );
00085         if( FAILED(res)) throw Error("Can't Lock flareVB");
00086         }
00087         else                    // renew VB
00088         {
00089                 AS.flare_startRenderingIndex = 0;
00090                 res = AS.flare_vb->Lock( AS.flare_startRenderingIndex*sizeof(LerpParticleVertex),
00091                                                                  maxParticles*sizeof(LerpParticleVertex),
00092                                                                  (BYTE**)&AS.flare_pVertex, D3DLOCK_DISCARD );
00093         if( FAILED(res)) throw Error("Can't Lock/Renew flareVB");
00094         }
00095 
00096         AS.flare_reservedCount= maxParticles;
00097         AS.flare_renderCount = 0;
00098         AS.flare_texture = texture;
00099 }
00100 //------------------------------------------------------------------------------------
00101 void flare_addParticle( const Vec3D &pos1, const Vec3D &pos2, const Vec4D multiplier,
00102                                                 FLOAT dist, FLOAT size )
00103 {
00104         if(AS.flare_renderCount >= AS.flare_reservedCount ) return;     // no more than you reserved    
00105         int index = AS.flare_renderCount;
00106         AS.flare_pVertex[index].pos1 = pos1;
00107         AS.flare_pVertex[index].pos2 = pos2;
00108         AS.flare_pVertex[index].colorMultiplier = multiplier;
00109         AS.flare_pVertex[index].dist = dist;    
00110         AS.flare_pVertex[index].size = size;
00111         AS.flare_renderCount++;
00112 }
00113 //------------------------------------------------------------------------------------
00114 void flare_end()
00115 {
00116         AS.flare_vb->Unlock();
00117 }
00118 //------------------------------------------------------------------------------------

Generated on Wed Apr 23 05:50:13 2003 for Modern Warfare by doxygen1.3-rc2