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

AMesh.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 "AUtil.h"
00024 
00025 //------------------------------------------------------------------------------------
00026 //------------------------------------------------------------------------------------
00028 class MeshRequestEntryComparer          
00029 {
00030 public:
00032         bool operator()(const MeshRenderRequestEntry &L, const MeshRenderRequestEntry &R )
00033         {
00034                 return (L.texture*1000+L.bumpmap)*1000+L.mesh < (R.texture*1000+R.bumpmap)*1000+R.mesh;                 
00035         }
00036 };
00037 //------------------------------------------------------------------------------------
00038 //------------------------------------------------------------------------------------
00039 void mesh_init()
00040 {
00041         if(AS.res_meshes.size()==0) return;
00042 
00043         // create Vertex&Index Buffer
00044         int vbByteSize = (int)AS.mesh_raw_vb.size() * sizeof(MeshVertex);
00045         int ibByteSize = (int)AS.mesh_raw_ib.size() * sizeof(USHORT);
00046         sys_console() << "Mesh : VertexBuffer Size = " << (int)AS.mesh_raw_vb.size() << endl;
00047         sys_console() << "Mesh : IndexBuffer  Size = " << (int)AS.mesh_raw_ib.size() << endl;
00048 
00049         AS.pDevice->CreateVertexBuffer( vbByteSize, D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED, &(AS.mesh_vb) );
00050         AS.pDevice->CreateIndexBuffer(  ibByteSize, D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &(AS.mesh_ib) );
00051         
00052         MeshVertex *pVBdata;
00053         USHORT     *pIBdata;
00054         if( FAILED( AS.mesh_vb->Lock(0,0, (BYTE**)&pVBdata, 0) )) throw Error("Can't Lock MeshVB");
00055         if( FAILED( AS.mesh_ib->Lock(0,0, (BYTE**)&pIBdata, 0) )) throw Error("Can't Lock MeshIB");
00056 
00057         memcpy( pVBdata, &(AS.mesh_raw_vb[0]), vbByteSize );
00058         memcpy( pIBdata, &(AS.mesh_raw_ib[0]), ibByteSize );    
00059         
00060         AS.mesh_vb->Unlock();
00061         AS.mesh_ib->Unlock();
00062 
00063         // create V&P Shaders
00064         AS.vshader_mesh = util_loadVertexShader( "shaders\\mesh.vso", dwMeshVertexDecl );
00065         AS.pshader_mesh = util_loadPixelShader(  "shaders\\mesh.pso" ); 
00066 }
00067 //------------------------------------------------------------------------------------
00068 // add a request entry to the rendering list
00069 void mesh_render( const AMesh mesh, const Vec3D pos, const Vec3D size, const Vec3D dir,
00070                                   const ATexture texture, const ATexture bumpmap, const Vec3D stripColor )
00071 {
00072         AS.mesh_renderList.push_back( MeshRenderRequestEntry(mesh,pos,size,dir,
00073                                                                                                                  texture,bumpmap,stripColor) ); 
00074 }
00075 //------------------------------------------------------------------------------------
00076 void mesh_flush()
00077 {
00078         AS.mesh_renderList.resize(0);
00079 }
00080 //------------------------------------------------------------------------------------
00081 // process all stock-up requests
00082 void mesh_processRenderRequests()
00083 {
00084         if(AS.res_meshes.size()==0) return;
00085         if(AS.mesh_renderList.size()==0) return;
00086 
00087         device_setVertexShader( AS.vshader_mesh );
00088         device_setPixelShader(  AS.pshader_mesh );
00089         device_setStreamSource( 0, AS.mesh_vb, sizeof(MeshVertex) );
00090         device_setIndexSource( AS.mesh_ib );
00091         device_setRenderState( D3DRS_ZENABLE,  TRUE );
00092         device_setRenderState( D3DRS_ZWRITEENABLE,  TRUE );
00093         device_setRenderState( D3DRS_SPECULARENABLE, FALSE );
00094         device_setTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
00095         device_setTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
00096         device_setTextureStageState( 0, D3DTSS_MIPFILTER, D3DTEXF_LINEAR );
00097 
00099         device_setRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
00100         device_setRenderStateBlendMode( BlendModes::NONE );
00101 
00102         sort(AS.mesh_renderList.begin(), AS.mesh_renderList.end(), MeshRequestEntryComparer());
00103 
00104         Matx matTrans, matScale, matTheta, matFinal, matLighting;
00105         int last = (int)AS.mesh_renderList.size();
00106 
00107         
00108         for( int i=0; i<last; i++ )
00109         {
00110                 MeshRenderRequestEntry &e = AS.mesh_renderList[i];
00111                 MeshEntry &m = AS.res_meshes[e.mesh];
00112                 
00113                 // construct xform matrix
00114                 D3DXMatrixTranslation( &matTrans, e.pos[0], e.pos[1], e.pos[2] );
00115                 D3DXMatrixRotationZ( &matTheta, D3DXToRadian(e.dir[0]) );
00116                 D3DXMatrixScaling( &matScale, e.size[0]/m.size[0], e.size[1]/m.size[1], e.size[2]/m.size[2] );
00117                 matFinal = matScale * matTheta * matTrans * AS.camera_world2projMatrix;
00118                 D3DXMatrixTranspose( &matFinal, &matFinal);
00119                 D3DXMatrixTranspose( &matLighting, &matTheta);
00120 
00121                 AS.pDevice->SetVertexShaderConstant( 0, &matFinal, 4 );                 // world->proj matrix
00122                 AS.pDevice->SetVertexShaderConstant( 4, &matLighting, 4 );              // rotate normals matrix
00123 
00124                 FLOAT lightVec[4] = { 0.5882f, 0.1961f, 0.2843f, 0.0f };
00125                 AS.pDevice->SetVertexShaderConstant( 8, &lightVec, 1 );
00126 
00127                 FLOAT t = 0.4f;
00128                 FLOAT ambient[4] = { t,t,t,0.0f };
00129                 AS.pDevice->SetVertexShaderConstant( 9, &ambient, 1 );
00130 
00131                 // strip color
00132                 AS.pDevice->SetPixelShaderConstant( 0, &e.stripColor, 1 );
00133 
00134                 // set texture & bumpmap
00135                 device_setTexture( 0, AS.res_textures[e.texture] );
00136                 if(e.bumpmap!=NULL)  device_setTexture( 0, AS.res_textures[e.bumpmap] );
00137 
00138                 // let the hardware render
00139                 AS.pDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, m.VBmin, m.VBcount, m.IBstart, m.TriCount );
00140         }       
00141 }
00142 //------------------------------------------------------------------------------------

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