00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00022 #include "AEngine.h"
00023 #include "AUtil.h"
00024
00025
00026
00028
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
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
00064 AS.vshader_mesh = util_loadVertexShader( "shaders\\mesh.vso", dwMeshVertexDecl );
00065 AS.pshader_mesh = util_loadPixelShader( "shaders\\mesh.pso" );
00066 }
00067
00068
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
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
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 );
00122 AS.pDevice->SetVertexShaderConstant( 4, &matLighting, 4 );
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
00132 AS.pDevice->SetPixelShaderConstant( 0, &e.stripColor, 1 );
00133
00134
00135 device_setTexture( 0, AS.res_textures[e.texture] );
00136 if(e.bumpmap!=NULL) device_setTexture( 0, AS.res_textures[e.bumpmap] );
00137
00138
00139 AS.pDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, m.VBmin, m.VBcount, m.IBstart, m.TriCount );
00140 }
00141 }
00142