00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "staticmesh.h"
00023 #include <zeitgeist/logserver/logserver.h>
00024 #include <kerosin/openglserver/openglserver.h>
00025 #include <kerosin/materialserver/material.h>
00026 #include <kerosin/materialserver/materialserver.h>
00027
00028 using namespace boost;
00029 using namespace kerosin;
00030 using namespace salt;
00031 using namespace std;
00032 using namespace zeitgeist;
00033 using namespace oxygen;
00034
00035 StaticMesh::StaticMesh() : mScale(1.0f,1.0f,1.0f)
00036 {
00037 }
00038
00039 StaticMesh::~StaticMesh()
00040 {
00041 }
00042
00043 void StaticMesh::ComputeBoundingBox()
00044 {
00045 }
00046
00047 void StaticMesh::CalcBoundingBox()
00048 {
00049 mLocalBoundingBox.Init();
00050
00051 if (mMesh.get() == 0)
00052 {
00053 return;
00054 }
00055
00056 const int n = mMesh->GetVertexCount() * 3;
00057
00058 const float* arPos = mMesh->GetPos().get();
00059 if (arPos == 0)
00060 {
00061 return;
00062 }
00063
00064 for (int i = 0; i<n; ++i)
00065 {
00066 const float* v = arPos + (i * 3);
00067 mLocalBoundingBox.Encapsulate
00068 (
00069 v[0]*mScale[0],
00070 v[1]*mScale[1],
00071 v[2]*mScale[2]
00072 );
00073 }
00074 }
00075
00076 void StaticMesh::RenderInternal()
00077 {
00078 if (mMesh.get() == 0)
00079 {
00080 return;
00081 }
00082
00083 const float* pos = mMesh->GetPos().get();
00084 if (pos == 0)
00085 {
00086 return;
00087 }
00088
00089 glVertexPointer(3, GL_FLOAT, 0, pos);
00090 glEnableClientState (GL_VERTEX_ARRAY);
00091
00092 const float* tex = mMesh->GetTexCoords().get();
00093 if (tex != 0)
00094 {
00095 glTexCoordPointer(3, GL_FLOAT, 0, tex);
00096 glEnableClientState (GL_TEXTURE_COORD_ARRAY);
00097 }
00098
00099 const float* normal = mMesh->GetNormals().get();
00100 if (normal != 0)
00101 {
00102 glNormalPointer(GL_FLOAT, 0, normal);
00103 glEnableClientState(GL_NORMAL_ARRAY);
00104 }
00105
00106 glCullFace(GL_BACK);
00107 glEnable(GL_CULL_FACE);
00108 glScalef(mScale[0],mScale[1],mScale[2]);
00109
00110 TriMesh::TFaces::const_iterator iter = mMesh->GetFaces().begin();
00111 int i=0;
00112 while (iter != mMesh->GetFaces().end())
00113 {
00114 const shared_ptr<Material> material = mMaterials[i];
00115
00116 if (material.get() != 0)
00117 {
00118 material->Bind();
00119
00120 const TriMesh::Face& face = (*iter);
00121 const shared_ptr<IndexBuffer>& idx = face.indeces;
00122
00123 glDrawElements(GL_TRIANGLES, idx->GetNumIndex(),
00124 GL_UNSIGNED_INT, idx->GetIndex().get());
00125 }
00126
00127 ++i;
00128 ++iter;
00129 }
00130
00131 glDisableClientState(GL_VERTEX_ARRAY);
00132 glDisableClientState(GL_TEXTURE_COORD_ARRAY );
00133 glDisableClientState(GL_NORMAL_ARRAY);
00134 }
00135
00136 const Vector3f& StaticMesh::GetScale()
00137 {
00138 return mScale;
00139 }
00140
00141 void StaticMesh::SetScale(const salt::Vector3f& scale)
00142 {
00143 mScale = scale;
00144 }
00145
00146 bool StaticMesh::Load(const std::string& name)
00147 {
00148 ParameterList parameter;
00149 return Load(name,parameter);
00150 }
00151
00152 bool StaticMesh::Load(const std::string& name, const ParameterList& parameter)
00153 {
00154 mMeshName = name;
00155 mMeshParameter = parameter;
00156 mMesh.reset();
00157 mMaterials.clear();
00158 ComputeBoundingBox();
00159
00160 shared_ptr<GeometryServer> geometryServer = shared_dynamic_cast<GeometryServer>
00161 (GetCore()->Get("/sys/server/geometry"));
00162
00163 if (geometryServer.get() == 0)
00164 {
00165 GetLog()->Error()
00166 << "(StaticMesh) ERROR: cannot get GeometryServer\n";
00167 return false;
00168 }
00169
00170 shared_ptr<MaterialServer> materialServer = shared_dynamic_cast<MaterialServer>
00171 (GetCore()->Get("/sys/server/material"));
00172
00173 if (materialServer.get() == 0)
00174 {
00175 GetLog()->Error()
00176 << "(StaticMesh) ERROR: cannot get MaterialServer\n";
00177 return false;
00178 }
00179
00180 mMesh = geometryServer->GetMesh(name,parameter);
00181
00182 if (mMesh.get() == 0)
00183 {
00184 return false;
00185 }
00186
00187 ComputeBoundingBox();
00188
00189
00190 for (
00191 TriMesh::TFaces::const_iterator iter = mMesh->GetFaces().begin();
00192 iter != mMesh->GetFaces().end();
00193 ++iter
00194 )
00195 {
00196 const TriMesh::Face& face = (*iter);
00197
00198 shared_ptr<Material> material =
00199 materialServer->GetMaterial(face.material);
00200
00201 if (material.get() == 0)
00202 {
00203 material = materialServer->GetMaterial("default");
00204 }
00205
00206 mMaterials.push_back(material);
00207 }
00208
00209 return true;
00210 }
00211
00212 const shared_ptr<TriMesh> StaticMesh::GetMesh()
00213 {
00214 return mMesh;
00215 }
00216
00217 const std::string& StaticMesh::GetMeshName()
00218 {
00219 return mMeshName;
00220 }
00221
00222 const ParameterList& StaticMesh::GetMeshParameter()
00223 {
00224 return mMeshParameter;
00225 }
00226