35Ogre::MeshPtr
SBaseMeshsTools::CreateSphere(
const Ogre::String& strName,
float radius,
int nRings,
int nSegments,
bool bNormals,
bool bTexCoords, Ogre::String groupName)
37 Ogre::MeshPtr pSphere = Ogre::MeshManager::getSingleton().createManual(strName, groupName);
38 Ogre::SubMesh* pSphereVertex = pSphere->createSubMesh();
39 pSphere->sharedVertexData =
new Ogre::VertexData();
40 CreateSphere(pSphere->sharedVertexData, pSphereVertex->indexData, radius, nRings, nSegments, bNormals, bTexCoords);
43 pSphereVertex->useSharedVertices =
true;
44 pSphere->_setBounds(Ogre::AxisAlignedBox(Ogre::Vector3(-radius, -radius, -radius), Ogre::Vector3(radius, radius, radius)),
false);
45 pSphere->_setBoundingSphereRadius(radius);
52void SBaseMeshsTools::CreateSphere(Ogre::VertexData*& vertexData, Ogre::IndexData*& indexData,
float radius,
int nRings,
int nSegments,
bool bNormals,
bool bTexCoords)
54 assert(vertexData && indexData);
57 Ogre::VertexDeclaration* vertexDecl = vertexData->vertexDeclaration;
58 size_t currOffset = 0;
61 vertexDecl->addElement(0, currOffset, Ogre::VET_FLOAT3, Ogre::VES_POSITION);
62 currOffset += Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT3);
67 vertexDecl->addElement(0, currOffset, Ogre::VET_FLOAT3, Ogre::VES_NORMAL);
68 currOffset += Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT3);
74 vertexDecl->addElement(0, currOffset, Ogre::VET_FLOAT2, Ogre::VES_TEXTURE_COORDINATES, 0);
75 currOffset += Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT2);
79 vertexData->vertexCount = (nRings+1) * (nSegments+1);
80 Ogre::HardwareVertexBufferSharedPtr vBuf = Ogre::HardwareBufferManager::getSingleton().createVertexBuffer(vertexDecl->getVertexSize(0), vertexData->vertexCount, Ogre::HardwareBuffer::HBU_STATIC,
false);
81 Ogre::VertexBufferBinding* binding = vertexData->vertexBufferBinding;
82 binding->setBinding(0, vBuf);
83 float* pVertex =
static_cast<float*
>(vBuf->lock(Ogre::HardwareBuffer::HBL_WRITE_ONLY));
86 indexData->indexCount = 6 * nRings * (nSegments+1);
87 indexData->indexBuffer = Ogre::HardwareBufferManager::getSingleton().createIndexBuffer(Ogre::HardwareIndexBuffer::IT_16BIT, indexData->indexCount, Ogre::HardwareBuffer::HBU_STATIC,
false);
88 Ogre::HardwareIndexBufferSharedPtr iBuf = indexData->indexBuffer;
89 unsigned short* pIndices =
static_cast<unsigned short*
>(iBuf->lock(Ogre::HardwareBuffer::HBL_WRITE_ONLY));
91 float fDeltaRingAngle = (Ogre::Math::PI / nRings);
92 float fDeltaSegAngle = (2 * Ogre::Math::PI / nSegments);
93 unsigned short wVerticeIndex = 0 ;
96 for(
int ring = 0; ring <= nRings; ring++)
98 float r0 = radius * sinf (ring * fDeltaRingAngle);
99 float y0 = radius * cosf (ring * fDeltaRingAngle);
102 for(
int seg = 0; seg <= nSegments; seg++)
104 float x0 = r0 * sinf(seg * fDeltaSegAngle);
105 float z0 = r0 * cosf(seg * fDeltaSegAngle);
114 Ogre::Vector3 vNormal = Ogre::Vector3(x0, y0, z0).normalisedCopy();
115 *pVertex++ = vNormal.x;
116 *pVertex++ = vNormal.y;
117 *pVertex++ = vNormal.z;
122 *pVertex++ = (float) seg / (
float) nSegments;
123 *pVertex++ = (float) ring / (
float) nRings;
129 *pIndices++ = wVerticeIndex + nSegments + 1;
130 *pIndices++ = wVerticeIndex;
131 *pIndices++ = wVerticeIndex + nSegments;
132 *pIndices++ = wVerticeIndex + nSegments + 1;
133 *pIndices++ = wVerticeIndex + 1;
134 *pIndices++ = wVerticeIndex;
147 Ogre::MeshPtr pCone = Ogre::MeshManager::getSingleton().createManual(strName, groupName);
148 Ogre::SubMesh *pConeVertex = pCone->createSubMesh();
149 pCone->sharedVertexData =
new Ogre::VertexData();
151 CreateCone(pCone->sharedVertexData, pConeVertex->indexData, radius, height, nVerticesInBase);
154 pConeVertex->useSharedVertices =
true;
155 pCone->_setBounds(Ogre::AxisAlignedBox(Ogre::Vector3(-radius, 0, -radius), Ogre::Vector3(radius, height, radius)),
false);
156 pCone->_setBoundingSphereRadius(Ogre::Math::Sqrt(height*height + radius*radius));
166 assert(vertexData && indexData);
169 Ogre::VertexDeclaration* vertexDecl = vertexData->vertexDeclaration;
171 size_t currOffset = 0;
174 vertexDecl->addElement(0, currOffset, Ogre::VET_FLOAT3, Ogre::VES_POSITION);
175 currOffset += Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT3);
178 vertexDecl->addElement(0, currOffset, Ogre::VET_FLOAT3, Ogre::VES_NORMAL);
179 currOffset += Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT3);
182 vertexDecl->addElement(0, currOffset, Ogre::VET_FLOAT2, Ogre::VES_TEXTURE_COORDINATES, 0);
183 currOffset += Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT2);
186 vertexData->vertexCount = nVerticesInBase + 1;
187 Ogre::HardwareVertexBufferSharedPtr vBuf = Ogre::HardwareBufferManager::getSingleton().createVertexBuffer(vertexDecl->getVertexSize(0), vertexData->vertexCount, Ogre::HardwareBuffer::HBU_STATIC,
false);
188 Ogre::VertexBufferBinding* binding = vertexData->vertexBufferBinding;
189 binding->setBinding(0, vBuf);
190 float* pVertex =
static_cast<float*
>(vBuf->lock(Ogre::HardwareBuffer::HBL_WRITE_ONLY));
193 indexData->indexCount = (3 * nVerticesInBase) + (3 * (nVerticesInBase - 2));
194 indexData->indexBuffer = Ogre::HardwareBufferManager::getSingleton().createIndexBuffer(Ogre::HardwareIndexBuffer::IT_16BIT, indexData->indexCount, Ogre::HardwareBuffer::HBU_STATIC,
false);
195 Ogre::HardwareIndexBufferSharedPtr iBuf = indexData->indexBuffer;
196 unsigned short* pIndices =
static_cast<unsigned short*
>(iBuf->lock(Ogre::HardwareBuffer::HBL_WRITE_ONLY));
211 float fDeltaBaseAngle = (2 * Ogre::Math::PI) / nVerticesInBase;
212 for (
int i=0; i<nVerticesInBase; i++)
214 float angle = i * fDeltaBaseAngle;
215 float x0 = radius * cosf(angle);
217 float z0 = radius * sinf(angle);
222 Ogre::Vector3 vNormal = Ogre::Vector3(x0, y0, z0).normalisedCopy();
223 *pVertex++ = vNormal.x;
224 *pVertex++ = vNormal.y;
225 *pVertex++ = vNormal.z;
227 *pVertex++ = (float)i / (
float)nVerticesInBase;
233 for (
int i=0; i<nVerticesInBase; i++)
236 *pIndices++ = (i%nVerticesInBase) + 1;
237 *pIndices++ = ((i+1)%nVerticesInBase) + 1;
241 for (
int i=0; i<nVerticesInBase-2; i++)
255 assert(baseLength > 0);
256 assert(bottomDistance > 0);
257 assert(upDistance > 0);
259 Ogre::MeshPtr pOctahedron = Ogre::MeshManager::getSingleton().createManual(strName, groupName);
260 Ogre::SubMesh *pOctahedronVertex = pOctahedron->createSubMesh();
261 pOctahedron->sharedVertexData =
new Ogre::VertexData();
263 CreateOctahedron(pOctahedron->sharedVertexData, pOctahedronVertex->indexData, baseLength, bottomDistance, upDistance);
266 pOctahedronVertex->useSharedVertices =
true;
267 float totalLength = bottomDistance + upDistance;
268 pOctahedron->_setBounds(Ogre::AxisAlignedBox(Ogre::Vector3(-baseLength/2, 0, -baseLength/2), Ogre::Vector3(baseLength/2, upDistance, baseLength/2)),
false);
270 pOctahedron->_setBoundingSphereRadius(Ogre::Math::Sqrt(totalLength*totalLength + (baseLength/2)*(baseLength/2)));
279 assert(vertexData && indexData);
282 Ogre::VertexDeclaration* vertexDecl = vertexData->vertexDeclaration;
284 size_t currOffset = 0;
287 vertexDecl->addElement(0, currOffset, Ogre::VET_FLOAT3, Ogre::VES_POSITION);
288 currOffset += Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT3);
291 vertexDecl->addElement(0, currOffset, Ogre::VET_FLOAT3, Ogre::VES_NORMAL);
292 currOffset += Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT3);
295 vertexDecl->addElement(0, currOffset, Ogre::VET_FLOAT2, Ogre::VES_TEXTURE_COORDINATES, 0);
296 currOffset += Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT2);
299 vertexData->vertexCount = 6;
300 Ogre::HardwareVertexBufferSharedPtr vBuf = Ogre::HardwareBufferManager::getSingleton().createVertexBuffer(vertexDecl->getVertexSize(0), vertexData->vertexCount, Ogre::HardwareBuffer::HBU_STATIC,
false);
301 Ogre::VertexBufferBinding* binding = vertexData->vertexBufferBinding;
302 binding->setBinding(0, vBuf);
303 float* pVertex =
static_cast<float*
>(vBuf->lock(Ogre::HardwareBuffer::HBL_WRITE_ONLY));
306 indexData->indexCount = 3 * 8;
307 indexData->indexBuffer = Ogre::HardwareBufferManager::getSingleton().createIndexBuffer(Ogre::HardwareIndexBuffer::IT_16BIT, indexData->indexCount, Ogre::HardwareBuffer::HBU_STATIC,
false);
308 Ogre::HardwareIndexBufferSharedPtr iBuf = indexData->indexBuffer;
309 unsigned short* pIndices =
static_cast<unsigned short*
>(iBuf->lock(Ogre::HardwareBuffer::HBL_WRITE_ONLY));
314 *pVertex++ = bottomDistance;
327 float baseOffset = baseLength / 2.0f;
328 *pVertex++ = baseOffset; *pVertex++ = 0; *pVertex++ = baseOffset;
330 Ogre::Vector3 vNormal = Ogre::Vector3(baseOffset, 0.0f, baseOffset).normalisedCopy();
331 *pVertex++ = vNormal.x; *pVertex++ = vNormal.y; *pVertex++ = vNormal.z;
333 *pVertex++ = 0.0f; *pVertex++ = 0.0f;
335 *pVertex++ = baseOffset; *pVertex++ = 0; *pVertex++ = -baseOffset;
337 vNormal = Ogre::Vector3(baseOffset, 0.0f, -baseOffset).normalisedCopy();
338 *pVertex++ = vNormal.x; *pVertex++ = vNormal.y; *pVertex++ = vNormal.z;
340 *pVertex++ = 0.0f; *pVertex++ = 1.0f;
342 *pVertex++ = -baseOffset; *pVertex++ = 0; *pVertex++ = -baseOffset;
344 vNormal = Ogre::Vector3(-baseOffset, 0.0f, -baseOffset).normalisedCopy();
345 *pVertex++ = vNormal.x; *pVertex++ = vNormal.y; *pVertex++ = vNormal.z;
347 *pVertex++ = 1.0f; *pVertex++ = 1.0f;
349 *pVertex++ = -baseOffset; *pVertex++ = 0; *pVertex++ = baseOffset;
351 vNormal = Ogre::Vector3(-baseOffset, 0.0f, baseOffset).normalisedCopy();
352 *pVertex++ = vNormal.x; *pVertex++ = vNormal.y; *pVertex++ = vNormal.z;
354 *pVertex++ = 1.0f; *pVertex++ = 0.0f;
358 *pVertex++ = -upDistance;
372 for (
int i=0; i<4; i++)
375 *pIndices++ = (i%4) + 1;
376 *pIndices++ = ((i+1)%4) + 1;
380 for (
int i=0; i<4; i++)
382 *pIndices++ = ((i+1)%4) + 1;
383 *pIndices++ = (i % 4) + 1;
396 vertexData->vertexCount = 4;
397 vertexData->vertexStart = 0;
399 Ogre::VertexDeclaration* vertexDecl = vertexData->vertexDeclaration;
400 Ogre::VertexBufferBinding* bind = vertexData->vertexBufferBinding;
402 vertexDecl->addElement(0, 0, Ogre::VET_FLOAT3, Ogre::VES_POSITION);
403 Ogre::HardwareVertexBufferSharedPtr vbuf = Ogre::HardwareBufferManager::getSingleton().createVertexBuffer(vertexDecl->getVertexSize(0), vertexData->vertexCount, Ogre::HardwareBuffer::HBU_STATIC);
406 bind->setBinding(0, vbuf);
416 vbuf->writeData(0,
sizeof(data), data,
true);