00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "KeosMeshGeom.h"
00023 #include "KeosException.h"
00024 #include "KeosRoot.h"
00025 #include "KeosRenderSystem.h"
00026 #include "KeosMesh.h"
00027
00028 namespace Keos
00029 {
00030
00031
00032
00033
00034
00035 CMeshGeom::CMeshGeom(const TVertex* pVertices, ulong nVerticesCount,
00036 const TIndex* pIndices, ulong nIndicesCount, TPrimitiveType PrimitiveType)
00037 {
00038 Assert(pVertices != NULL);
00039 Assert(pIndices != NULL);
00040
00041 TDeclarationElement Elements[] =
00042 {
00043 {0, ELT_USAGE_POSITION, ELT_TYPE_FLOAT3},
00044 {0, ELT_USAGE_NORMAL, ELT_TYPE_FLOAT3},
00045 {0, ELT_USAGE_DIFFUSE, ELT_TYPE_COLOR},
00046 {0, ELT_USAGE_TEXCOORD0, ELT_TYPE_FLOAT2}
00047 };
00048
00049 IRenderSystem* pRenderSystem = CRoot::Instance().GetRenderSystem();
00050
00051 m_pDeclaration = pRenderSystem->CreateVertexDeclaration(Elements);
00052 m_VertexBuffer = pRenderSystem->CreateVertexBuffer(nVerticesCount, 0, pVertices);
00053 m_IndexBuffer = pRenderSystem->CreateIndexBuffer(nIndicesCount, 0, pIndices);
00054
00055 m_nMaterialIndex = -1;
00056 m_bUseSharedVertexBuffer = false;
00057
00058 m_PrimitiveType = PrimitiveType;
00059 m_nPrimitivesCount = GetPrimitivesCount(PrimitiveType);
00060 }
00061
00062
00063 #if KEOS_MESH_SHARED_VERTEX_BUFFER
00064 CMeshGeom::CMeshGeom(const TIndex* pIndices, ulong nIndicesCount, TPrimitiveType PrimitiveType)
00065 {
00066 Assert(pIndices != NULL);
00067
00068 TDeclarationElement Elements[] =
00069 {
00070 {0, ELT_USAGE_POSITION, ELT_TYPE_FLOAT3},
00071 {0, ELT_USAGE_NORMAL, ELT_TYPE_FLOAT3},
00072 {0, ELT_USAGE_DIFFUSE, ELT_TYPE_COLOR},
00073 {0, ELT_USAGE_TEXCOORD0, ELT_TYPE_FLOAT2}
00074 };
00075
00076 IRenderSystem* pRenderSystem = CRoot::Instance().GetRenderSystem();
00077
00078 m_pDeclaration = pRenderSystem->CreateVertexDeclaration(Elements);
00079 m_IndexBuffer = pRenderSystem->CreateIndexBuffer(nIndicesCount, 0, pIndices);
00080
00081 m_nMaterialIndex = -1;
00082 m_bUseSharedVertexBuffer = true;
00083
00084 m_PrimitiveType = PrimitiveType;
00085 m_nPrimitivesCount = GetPrimitivesCount(PrimitiveType);
00086 }
00087 #endif
00088
00089
00090 CMeshGeom::~CMeshGeom()
00091 {}
00092
00093
00094 #if KEOS_DEBUG_MODE
00095 void CMeshGeom::SetMaterialIndex(int nIndex)
00096 {
00097 Assert(nIndex < (int)(m_pParentMesh->GetMaterialsCount()));
00098 m_nMaterialIndex = nIndex;
00099
00100 }
00101 #endif
00102
00103
00104 int CMeshGeom::GetPrimitivesCount(TPrimitiveType PrimitiveType)
00105 {
00106 switch (PrimitiveType)
00107 {
00108 case PT_TRIANGLELIST:
00109 return m_IndexBuffer.GetCount() / 3;
00110 case PT_TRIANGLESTRIP:
00111 return m_IndexBuffer.GetCount() - 2;
00113 case PT_LINELIST:
00114 return m_IndexBuffer.GetCount() / 2;
00115 case PT_LINESTRIP:
00116 return m_IndexBuffer.GetCount() - 2;
00117 case PT_POINTLIST:
00118 return m_IndexBuffer.GetCount();
00119 }
00120
00121 return 0;
00122 }
00123
00124
00125 void CMeshGeom::AssignColor(const CColor& Color)
00126 {
00127 IRenderSystem* pRenderSystem = CRoot::Instance().GetRenderSystem();
00128
00129 if (!m_bUseSharedVertexBuffer)
00130 {
00131 TVertex* Vertices = m_VertexBuffer.Lock(0, 0, LOCK_WRITEONLY);
00132 for (uint i = 0; i < m_VertexBuffer.GetCount(); i++)
00133 {
00134 Vertices[i].Color = pRenderSystem->ConvertColor(Color);
00135 }
00136 m_VertexBuffer.Unlock();
00137 }
00138 else
00139 {
00140 TVertex* Vertices = m_pParentMesh->m_SharedVertexBuffer.Lock(0, 0, LOCK_WRITEONLY);
00141 for (uint i = 0; i < m_pParentMesh->m_SharedVertexBuffer.GetCount(); i++)
00142 {
00143 Vertices[i].Color = pRenderSystem->ConvertColor(Color);
00144 }
00145 m_pParentMesh->m_SharedVertexBuffer.Unlock();
00146 }
00147 }
00148
00149
00150 void CMeshGeom::AssignMaterialColor()
00151 {
00152 if (m_nMaterialIndex == -1) return;
00153 Assert(m_nMaterialIndex < (int)(m_pParentMesh->GetMaterialsCount()));
00154 CMaterial* pMaterial = m_pParentMesh->GetMaterial(m_nMaterialIndex);
00155 AssignColor(pMaterial->GetDiffuseColor());
00156 }
00157
00158 }