void ESKOgre::createFakeEntity(Ogre::SceneManager *mSceneMgr) { Ogre::MeshPtr msh = Ogre::MeshManager::getSingleton().createManual(name + "_skeleton", XENOVIEWER_RESOURCE_GROUP); msh->setSkeletonName(name); Ogre::SubMesh* sub = msh->createSubMesh(); const size_t nVertices = 3; const size_t nVertCount = 3; const size_t vbufCount = nVertCount*nVertices; float *vertices = (float *)malloc(sizeof(float)*vbufCount); for (size_t i = 0; i < nVertices; i++) { vertices[i*nVertCount] = 0.0; vertices[i*nVertCount + 1] = 0.0; vertices[i*nVertCount + 2] = 0.0; } const size_t ibufCount = 3; unsigned short *faces = (unsigned short *)malloc(sizeof(unsigned short) * ibufCount); for (size_t i = 0; i < ibufCount; i++) { faces[i] = i; } msh->sharedVertexData = new Ogre::VertexData(); msh->sharedVertexData->vertexCount = nVertices; Ogre::VertexDeclaration* decl = msh->sharedVertexData->vertexDeclaration; size_t offset = 0; decl->addElement(0, offset, Ogre::VET_FLOAT3, Ogre::VES_POSITION); offset += Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT3); Ogre::HardwareVertexBufferSharedPtr vbuf = Ogre::HardwareBufferManager::getSingleton().createVertexBuffer(offset, msh->sharedVertexData->vertexCount, Ogre::HardwareBuffer::HBU_STATIC_WRITE_ONLY); vbuf->writeData(0, vbuf->getSizeInBytes(), vertices, true); Ogre::VertexBufferBinding* bind = msh->sharedVertexData->vertexBufferBinding; bind->setBinding(0, vbuf); Ogre::HardwareIndexBufferSharedPtr ibuf = Ogre::HardwareBufferManager::getSingleton().createIndexBuffer(Ogre::HardwareIndexBuffer::IT_16BIT, ibufCount, Ogre::HardwareBuffer::HBU_STATIC_WRITE_ONLY); ibuf->writeData(0, ibuf->getSizeInBytes(), faces, true); sub->useSharedVertices = true; sub->indexData->indexBuffer = ibuf; sub->indexData->indexCount = ibufCount; sub->indexData->indexStart = 0; msh->_setBounds(Ogre::AxisAlignedBox(-100, -100, -100, 100, 100, 100)); msh->_setBoundingSphereRadius(100); msh->load(); free(faces); free(vertices); skeleton_entity = mSceneMgr->createEntity(name + "_skeleton"); skeleton_node = mSceneMgr->getRootSceneNode()->createChildSceneNode(); skeleton_node->attachObject(skeleton_entity); skeleton_node->setVisible(false); }
void NxMeshManager::SetPivotTransform( Ogre::MeshPtr mesh, const Nx::Vector3 & Position, const Nx::Quaternion & Rotation, const Nx::Vector3 & Scale ) { //from mesh magick / mit licence Nx::Matrix4 transform = Nx::Matrix4::IDENTITY; Nx::Vector3 translate = Nx::Vector3::ZERO; // Apply current transform to the mesh, to get the bounding box to // base te translation on. AxisAlignedBox aabb = getMeshAabb( mesh, transform); //if (alignment == "left") //{ // translate = Vector3(-aabb.getMinimum().x, 0, 0); //} //else if (alignment == "center") //{ // translate = Vector3(-aabb.getCenter().x, 0, 0); //} //else if (alignment == "right") //{ // translate = Vector3(-aabb.getMaximum().x, 0, 0); //} //Position .. only support pivot down / centered //translate = Vector3(0, -aabb.getMinimum().y, 0);// pivot down translate = Position; transform = Nx::Matrix4::getTrans(translate) * transform; //rotation transform = Nx::Matrix4(Rotation) * transform; //scale transform = Nx::Matrix4::getScale(Scale) * transform; // Check whether we have to flip vertex winding. // We do have to, if we changed our right hand base. // We can test it by using the cross product from X and Y and see, if it is a non-negative // projection on Z. Actually it should be exactly Z, as we don't do non-uniform scaling yet, // but the test is cheap either way. Nx::Matrix3 m3; transform.extract3x3Matrix(m3); if (m3.GetColumn(0).crossProduct(m3.GetColumn(1)).dotProduct(m3.GetColumn(2)) < 0) { LogMsg("SetPivotPosition : Flipping vertex winding ... " ); mFlipVertexWinding = true; } //mTransform = transform; NxMat4toOgre( mTransform, transform ) ; mBoundingBox.setNull(); if( mesh->sharedVertexData != NULL) { processVertexData( mesh->sharedVertexData); }else { LogMsg("mesh->sharedVertexData NULL"); } for( int i = 0; i < mesh->getNumSubMeshes(); i++ ) { SubMesh* submesh = mesh->getSubMesh(i); if( submesh->vertexData != NULL ) { LogMsg("SetPivotPosition : Processing vertex data ... " ); processVertexData(submesh->vertexData); }else { LogMsg("submesh->vertexData NULL"); } if (submesh->indexData != NULL) { LogMsg("SetPivotPosition : Processing Index data .." ); processIndexData(submesh->indexData); }else { LogMsg("submesh->indexData NULL"); } } //process pose for( unsigned short i = 0; i < mesh->getPoseCount(); ++i ) { Ogre::Pose * pose = mesh->getPose(i); Ogre::Matrix3 m3x3; mTransform.extract3x3Matrix(m3x3); Pose::VertexOffsetIterator it = pose->getVertexOffsetIterator(); while (it.hasMoreElements()) { Ogre::Vector3 offset = it.peekNextValue(); Ogre::Vector3 newOffset = m3x3 * offset; *it.peekNextValuePtr() = newOffset; it.moveNext(); } } mesh->_setBounds( mBoundingBox, false ); }
Ogre::Entity* ModelFile::GetModel( const ModelInfo& info ) { VectorTexForGen textures; Ogre::MeshPtr mesh = Ogre::MeshManager::getSingleton().create( info.data.name + "export", "General" ); Ogre::SkeletonPtr skeleton = Ogre::SkeletonManager::getSingleton().create( info.data.name + "export", "General" ); int number_of_bones = GetU8( 0x02 ); int number_of_parts = GetU8( 0x03 ); int offset_to_bones = GetU32LE( 0x0c ); int offset_to_parts = GetU32LE( 0x10 ); Ogre::Bone* root1 = skeleton->createBone( "0", 0 ); Ogre::Bone* root2 = skeleton->createBone( "1", 1 ); root1->addChild( root2 ); for( int i = 0; i < number_of_bones; ++i ) { Bone bone; bone.parent_id = ( i != 0 ) ? ( s8 )GetU8( offset_to_bones + i * 0x04 + 0x03 ) : -1; bone.length = ( s16 )GetU16LE( offset_to_bones + i * 0x04 + 0x00 ); m_Skeleton.push_back(bone); Ogre::Bone* bone1 = skeleton->createBone( Ogre::StringConverter::toString( i * 2 + 2 ), i * 2 + 2 ); Ogre::Bone* bone2 = skeleton->createBone( Ogre::StringConverter::toString( i * 2 + 3 ), i * 2 + 3 ); LOGGER->Log( "Add skeleton bone: bone_id = " + Ogre::StringConverter::toString( i ) + ", length = " + Ogre::StringConverter::toString( bone.length ) + ", parent = " + Ogre::StringConverter::toString( bone.parent_id ) + ".\n" ); if( bone.parent_id == -1 ) { skeleton->getBone( 1 )->addChild( bone1 ); } else { skeleton->getBone( bone.parent_id * 2 + 3 )->addChild( bone1 ); } bone1->addChild( bone2 ); } AnimationExtractor( skeleton, info, m_Skeleton ); // draw skeleton { //DrawSkeleton( m_Skeleton, mesh ); } for( int i = 0; i < number_of_parts; ++i ) { MeshExtractor( info.data, "ffix/field_model/" + info.data.name, this, offset_to_parts + i * 0x28, textures, mesh ); } // <OGRE> /////////////////////////////// skeleton->optimiseAllAnimations(); Ogre::SkeletonSerializer skeleton_serializer; skeleton_serializer.exportSkeleton( skeleton.getPointer(), "exported/models/field/units/" + info.data.name + ".skeleton" ); // Update bounds Ogre::AxisAlignedBox aabb( -999, -999, -999, 999, 999, 999 ); mesh->_setBounds( aabb, false ); mesh->_setBoundingSphereRadius( 999 ); mesh->setSkeletonName( "models/field/units/" + info.data.name + ".skeleton" ); Ogre::MeshSerializer ser; ser.exportMesh( mesh.getPointer(), "exported/models/field/units/" + info.data.name + ".mesh" ); // create and export textures for model //if (textures.size() > 0) { Vram* vram = new Vram(); File* tex = new File( "./data/field/5/1b/2/4/1.tim" ); LoadTimFileToVram( tex, 0, vram ); delete tex; tex = new File( "./data/field/5/1b/2/4/2.tim" ); LoadTimFileToVram( tex, 0, vram ); delete tex; vram->Save( "1.jpg" ); CreateTexture( vram, info.data, "exported/models/field/units/" + info.data.name + ".png", textures ); delete vram; } CreateMaterial( "ffix/field_model/" + info.data.name, "exported/models/field/units/" + info.data.name + ".material", ( textures.size() > 0 ) ? "models/field/units/" + info.data.name + ".png" : "", "", "" ); Ogre::SceneManager* scene_manager = Ogre::Root::getSingleton().getSceneManager( "Scene" ); Ogre::Entity* thisEntity = scene_manager->createEntity( info.data.name, "models/field/units/" + info.data.name + ".mesh" ); //thisEntity->setDisplaySkeleton(true); //thisEntity->setDebugDisplayEnabled(true); thisEntity->setVisible( false ); thisEntity->getAnimationState( info.animations_name[ 0 ] )->setEnabled(true); thisEntity->getAnimationState( info.animations_name[ 0 ] )->setLoop(true); Ogre::SceneNode* thisSceneNode = scene_manager->getRootSceneNode()->createChildSceneNode(); thisSceneNode->setPosition( 0, 0, 0 ); thisSceneNode->roll( Ogre::Radian( Ogre::Degree( 180.0f ) ) ); thisSceneNode->yaw( Ogre::Radian( Ogre::Degree( 120.0f ) ) ); thisSceneNode->pitch( Ogre::Radian( Ogre::Degree(90.0f ) ) ); thisSceneNode->attachObject( thisEntity ); return thisEntity; }
void MilkshapePlugin::doExportMesh(msModel* pModel) { // Create singletons Ogre::SkeletonManager skelMgr; Ogre::DefaultHardwareBufferManager defHWBufMgr; Ogre::LogManager& logMgr = Ogre::LogManager::getSingleton(); Ogre::MeshManager meshMgr; // // choose filename // OPENFILENAME ofn; memset (&ofn, 0, sizeof (OPENFILENAME)); char szFile[MS_MAX_PATH]; char szFileTitle[MS_MAX_PATH]; char szDefExt[32] = "mesh"; char szFilter[128] = "OGRE .mesh Files (*.mesh)\0*.mesh\0All Files (*.*)\0*.*\0\0"; szFile[0] = '\0'; szFileTitle[0] = '\0'; ofn.lStructSize = sizeof (OPENFILENAME); ofn.lpstrDefExt = szDefExt; ofn.lpstrFilter = szFilter; ofn.lpstrFile = szFile; ofn.nMaxFile = MS_MAX_PATH; ofn.lpstrFileTitle = szFileTitle; ofn.nMaxFileTitle = MS_MAX_PATH; ofn.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST; ofn.lpstrTitle = "Export to OGRE Mesh"; if (!::GetSaveFileName (&ofn)) return /*0*/; logMgr.logMessage("Creating Mesh object..."); Ogre::MeshPtr ogreMesh = Ogre::MeshManager::getSingleton().create("export", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); logMgr.logMessage("Mesh object created."); bool foundBoneAssignment = false; // No shared geometry int i; int wh, numbones; int intweight[3], intbones[3]; size_t j; Ogre::Vector3 min, max, currpos; Ogre::Real maxSquaredRadius; bool first = true; for (i = 0; i < msModel_GetMeshCount (pModel); i++) { msMesh *pMesh = msModel_GetMeshAt (pModel, i); logMgr.logMessage("Creating SubMesh object..."); Ogre::SubMesh* ogreSubMesh = ogreMesh->createSubMesh(); logMgr.logMessage("SubMesh object created."); // Set material logMgr.logMessage("Getting SubMesh Material..."); int matIdx = msMesh_GetMaterialIndex(pMesh); if (matIdx == -1) { // No material, use blank ogreSubMesh->setMaterialName("BaseWhite"); logMgr.logMessage("No Material, using default 'BaseWhite'."); } else { msMaterial *pMat = msModel_GetMaterialAt(pModel, matIdx); ogreSubMesh->setMaterialName(pMat->szName); logMgr.logMessage("SubMesh Material Done."); } logMgr.logMessage("Setting up geometry..."); // Set up mesh geometry ogreSubMesh->vertexData = new Ogre::VertexData(); ogreSubMesh->vertexData->vertexCount = msMesh_GetVertexCount (pMesh); ogreSubMesh->vertexData->vertexStart = 0; Ogre::VertexBufferBinding* bind = ogreSubMesh->vertexData->vertexBufferBinding; Ogre::VertexDeclaration* decl = ogreSubMesh->vertexData->vertexDeclaration; // Always 1 texture layer, 2D coords #define POSITION_BINDING 0 #define NORMAL_BINDING 1 #define TEXCOORD_BINDING 2 decl->addElement(POSITION_BINDING, 0, Ogre::VET_FLOAT3, Ogre::VES_POSITION); decl->addElement(NORMAL_BINDING, 0, Ogre::VET_FLOAT3, Ogre::VES_NORMAL); decl->addElement(TEXCOORD_BINDING, 0, Ogre::VET_FLOAT2, Ogre::VES_TEXTURE_COORDINATES); // Create buffers Ogre::HardwareVertexBufferSharedPtr pbuf = Ogre::HardwareBufferManager::getSingleton(). createVertexBuffer(decl->getVertexSize(POSITION_BINDING), ogreSubMesh->vertexData->vertexCount, Ogre::HardwareBuffer::HBU_DYNAMIC, false); Ogre::HardwareVertexBufferSharedPtr nbuf = Ogre::HardwareBufferManager::getSingleton(). createVertexBuffer(decl->getVertexSize(NORMAL_BINDING), ogreSubMesh->vertexData->vertexCount, Ogre::HardwareBuffer::HBU_DYNAMIC, false); Ogre::HardwareVertexBufferSharedPtr tbuf = Ogre::HardwareBufferManager::getSingleton(). createVertexBuffer(decl->getVertexSize(TEXCOORD_BINDING), ogreSubMesh->vertexData->vertexCount, Ogre::HardwareBuffer::HBU_DYNAMIC, false); bind->setBinding(POSITION_BINDING, pbuf); bind->setBinding(NORMAL_BINDING, nbuf); bind->setBinding(TEXCOORD_BINDING, tbuf); ogreSubMesh->useSharedVertices = false; float* pPos = static_cast<float*>( pbuf->lock(Ogre::HardwareBuffer::HBL_DISCARD)); logMgr.logMessage("Doing positions and texture coords..."); for (j = 0; j < ogreSubMesh->vertexData->vertexCount; ++j) { logMgr.logMessage("Doing vertex " + Ogre::StringConverter::toString(j)); msVertex *pVertex = msMesh_GetVertexAt (pMesh, (int)j); msVertexEx *pVertexEx=msMesh_GetVertexExAt(pMesh, (int)j); msVec3 Vertex; msVertex_GetVertex (pVertex, Vertex); *pPos++ = Vertex[0]; *pPos++ = Vertex[1]; *pPos++ = Vertex[2]; // Deal with bounds currpos = Ogre::Vector3(Vertex[0], Vertex[1], Vertex[2]); if (first) { min = max = currpos; maxSquaredRadius = currpos.squaredLength(); first = false; } else { min.makeFloor(currpos); max.makeCeil(currpos); maxSquaredRadius = std::max(maxSquaredRadius, currpos.squaredLength()); } int boneIdx = msVertex_GetBoneIndex(pVertex); if (boneIdx != -1) { foundBoneAssignment = true; numbones = 1; intbones[0] = intbones[1] = intbones[2] = -1; intweight[0] = intweight[1] = intweight[2] = 0; for(wh = 0; wh < 3; ++wh) { intbones[wh] = msVertexEx_GetBoneIndices(pVertexEx, wh); if(intbones[wh] == -1) break; ++numbones; intweight[wh] = msVertexEx_GetBoneWeights(pVertexEx, wh); } // for(k) Ogre::VertexBoneAssignment vertAssign; vertAssign.boneIndex = boneIdx; vertAssign.vertexIndex = (unsigned int)j; if(numbones == 1) { vertAssign.weight = 1.0; } // single assignment else { vertAssign.weight=(Ogre::Real)intweight[0]/100.0; } ogreSubMesh->addBoneAssignment(vertAssign); if(numbones > 1) { // this somewhat contorted logic is because the first weight [0] matches to the bone assignment // located with pVertex. The next two weights [1][2] match up to the first two bones found // with pVertexEx [0][1]. The weight for the fourth bone, if present, is the unassigned weight for(wh = 0; wh < 3; wh++) { boneIdx = intbones[wh]; if(boneIdx == -1) break; vertAssign.boneIndex = boneIdx; vertAssign.vertexIndex = (unsigned int)j; if(wh == 2) { // fourth weight is 1.0-(sumoffirstthreeweights) vertAssign.weight = 1.0-(((Ogre::Real)intweight[0]/100.0)+ ((Ogre::Real)intweight[1]/100.0)+((Ogre::Real)intweight[2]/100.0)); } else { vertAssign.weight=(Ogre::Real)intweight[wh+1]; } ogreSubMesh->addBoneAssignment(vertAssign); } // for(k) } // if(numbones) } } pbuf->unlock(); float* pTex = static_cast<float*>( tbuf->lock(Ogre::HardwareBuffer::HBL_DISCARD)); logMgr.logMessage("Doing uvs, normals and indexes (v2)..."); // Aargh, Milkshape uses stupid separate normal indexes for the same vertex like 3DS // Normals aren't described per vertex but per triangle vertex index // Pain in the arse, we have to do vertex duplication again if normals differ at a vertex (non smooth) // WHY don't people realise this format is a pain for passing to 3D APIs in vertex buffers? float* pNorm = static_cast<float*>( nbuf->lock(Ogre::HardwareBuffer::HBL_DISCARD)); ogreSubMesh->indexData->indexCount = msMesh_GetTriangleCount (pMesh) * 3; // Always use 16-bit buffers, Milkshape can't handle more anyway Ogre::HardwareIndexBufferSharedPtr ibuf = Ogre::HardwareBufferManager::getSingleton(). createIndexBuffer(Ogre::HardwareIndexBuffer::IT_16BIT, ogreSubMesh->indexData->indexCount, Ogre::HardwareBuffer::HBU_STATIC_WRITE_ONLY); ogreSubMesh->indexData->indexBuffer = ibuf; unsigned short *pIdx = static_cast<unsigned short*>( ibuf->lock(Ogre::HardwareBuffer::HBL_DISCARD)); for (j = 0; j < ogreSubMesh->indexData->indexCount; j+=3) { msTriangle *pTriangle = msMesh_GetTriangleAt (pMesh, (int)j/3); msTriangleEx *pTriangleEx=msMesh_GetTriangleExAt(pMesh, (int)j/3); word nIndices[3]; msTriangle_GetVertexIndices (pTriangle, nIndices); msVec3 Normal; msVec2 uv; int k, vertIdx; for (k = 0; k < 3; ++k) { vertIdx = nIndices[k]; // Face index pIdx[j+k] = vertIdx; // Vertex normals // For the moment, ignore any discrepancies per vertex msTriangleEx_GetNormal(pTriangleEx, k, &Normal[0]); msTriangleEx_GetTexCoord(pTriangleEx, k, &uv[0]); pTex[(vertIdx*2)]=uv[0]; pTex[(vertIdx*2)+1]=uv[1]; pNorm[(vertIdx*3)] = Normal[0]; pNorm[(vertIdx*3)+1] = Normal[1]; pNorm[(vertIdx*3)+2] = Normal[2]; } } // Faces nbuf->unlock(); ibuf->unlock(); tbuf->unlock(); // Now use Ogre's ability to reorganise the vertex buffers the best way Ogre::VertexDeclaration* newDecl = ogreSubMesh->vertexData->vertexDeclaration->getAutoOrganisedDeclaration( foundBoneAssignment, false); Ogre::BufferUsageList bufferUsages; for (size_t u = 0; u <= newDecl->getMaxSource(); ++u) bufferUsages.push_back(Ogre::HardwareBuffer::HBU_STATIC_WRITE_ONLY); ogreSubMesh->vertexData->reorganiseBuffers(newDecl, bufferUsages); logMgr.logMessage("Geometry done."); } // SubMesh // Set bounds ogreMesh->_setBoundingSphereRadius(Ogre::Math::Sqrt(maxSquaredRadius)); ogreMesh->_setBounds(Ogre::AxisAlignedBox(min, max), false); // Keep hold of a Skeleton pointer for deletion later // Mesh uses Skeleton pointer for skeleton name Ogre::SkeletonPtr pSkel; if (exportSkeleton && foundBoneAssignment) { // export skeleton, also update mesh to point to it pSkel = doExportSkeleton(pModel, ogreMesh); } else if (!exportSkeleton && foundBoneAssignment) { // We've found bone assignments, but skeleton is not to be exported // Prompt the user to find the skeleton if (!locateSkeleton(ogreMesh)) return; } // Export logMgr.logMessage("Creating MeshSerializer.."); Ogre::MeshSerializer serializer; logMgr.logMessage("MeshSerializer created."); // Generate LODs if required if (generateLods) { // Build LOD depth list Ogre::Mesh::LodDistanceList distList; float depth = 0; for (unsigned short depthidx = 0; depthidx < numLods; ++depthidx) { depth += lodDepthIncrement; distList.push_back(depth); } ogreMesh->generateLodLevels(distList, lodReductionMethod, lodReductionAmount); } if (generateEdgeLists) { ogreMesh->buildEdgeList(); } if (generateTangents) { unsigned short src, dest; ogreMesh->suggestTangentVectorBuildParams(tangentSemantic, src, dest); ogreMesh->buildTangentVectors(tangentSemantic, src, dest, tangentsSplitMirrored, tangentsSplitRotated, tangentsUseParity); } // Export Ogre::String msg; msg = "Exporting mesh data to file '" + Ogre::String(szFile) + "'"; logMgr.logMessage(msg); serializer.exportMesh(ogreMesh.getPointer(), szFile); logMgr.logMessage("Export successful"); Ogre::MeshManager::getSingleton().remove(ogreMesh->getHandle()); if (!pSkel.isNull()) Ogre::SkeletonManager::getSingleton().remove(pSkel->getHandle()); if (exportMaterials && msModel_GetMaterialCount(pModel) > 0) { doExportMaterials(pModel); } }
Ogre::SceneNode *TutorialApplication::loadBSP(std::shared_ptr<l2p::UModel> m, bool ignoreNonVisible) { l2p::Name name = m->package->name; std::vector<float> vertex_data; std::vector<uint32_t> index_buf; l2p::Box bounds; // Build vertex and index buffer. for (auto ni = m->nodes.begin(), ne = m->nodes.end(); ni != ne; ++ni) { l2p::BSPNode &n = *ni; l2p::BSPSurface &s = m->surfaces[n.surface]; if (ignoreNonVisible && ignoreNode(m.get(), n, s)) continue; uint32_t vert_start = vertex_data.size() / 8; const Ogre::Vector3 uvec = ogre_cast(m->vectors[s.U]); const Ogre::Vector3 vvec = ogre_cast(m->vectors[s.V]); const Ogre::Vector3 base = ogre_cast(m->points[s.base]); int usize = 0; int vsize = 0; std::shared_ptr<l2p::UTexture> mat = s.material; if (mat) { usize = mat->USize; vsize = mat->VSize; } if (usize == 0 || vsize == 0) usize = vsize = 64; // Vertex buffer. if (n.num_verticies > 0) { l2p::Vector Normal = m->vectors[s.normal]; for (uint32_t vert_index = 0; vert_index < n.num_verticies; ++vert_index) { const l2p::Vector &pos = m->points[m->vertexes[n.vert_pool + vert_index].vertex]; const Ogre::Vector3 dist(ogre_cast(pos) - base); const Ogre::Vector2 tcoord((dist | uvec) / float(usize), (dist | vvec) / float(vsize)); bounds += pos; vertex_data.push_back(pos.X); vertex_data.push_back(pos.Y); vertex_data.push_back(pos.Z); vertex_data.push_back(Normal.X); vertex_data.push_back(Normal.Y); vertex_data.push_back(Normal.Z); vertex_data.push_back(tcoord.x); vertex_data.push_back(tcoord.y); } if (s.flags & l2p::PF_TwoSided) { for (uint32_t vert_index = 0; vert_index < n.num_verticies; ++vert_index) { const l2p::Vector &pos = m->points[m->vertexes[n.vert_pool + vert_index].vertex]; const Ogre::Vector3 dist(ogre_cast(pos) - base); const Ogre::Vector2 tcoord((dist | uvec) / float(usize), (dist | vvec) / float(vsize)); vertex_data.push_back(pos.X); vertex_data.push_back(pos.Y); vertex_data.push_back(pos.Z); vertex_data.push_back(Normal.X); vertex_data.push_back(Normal.Y); vertex_data.push_back(-Normal.Z); vertex_data.push_back(tcoord.x); vertex_data.push_back(tcoord.y); } } } // Index buffer. for (int verti = 2; verti < n.num_verticies; ++verti) { index_buf.push_back(vert_start); index_buf.push_back(vert_start + verti - 1); index_buf.push_back(vert_start + verti); } if (s.flags & l2p::PF_TwoSided) { for (int verti = 2; verti < n.num_verticies; ++verti) { index_buf.push_back(vert_start); index_buf.push_back(vert_start + verti); index_buf.push_back(vert_start + verti - 1); } } } if (vertex_data.size() == 0 || index_buf.size() == 0) return nullptr; Ogre::MeshPtr mesh = Ogre::MeshManager::getSingleton().createManual(Ogre::String(name) + Ogre::String(m->name), "General"); Ogre::VertexData *data = new Ogre::VertexData(); mesh->sharedVertexData = data; data->vertexCount = vertex_data.size() / 8; Ogre::VertexDeclaration *decl = data->vertexDeclaration; uint32_t offset = 0; decl->addElement(0, offset, Ogre::VET_FLOAT3, Ogre::VES_POSITION); offset += Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT3); decl->addElement(0, offset, Ogre::VET_FLOAT3, Ogre::VES_NORMAL); offset += Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT3); decl->addElement(0, offset, Ogre::VET_FLOAT2, Ogre::VES_TEXTURE_COORDINATES); offset += Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT2); Ogre::HardwareVertexBufferSharedPtr vbuf = Ogre::HardwareBufferManager::getSingleton().createVertexBuffer( offset, data->vertexCount, Ogre::HardwareBuffer::HBU_STATIC_WRITE_ONLY); vbuf->writeData(0, vbuf->getSizeInBytes(), &vertex_data.front(), true); data->vertexBufferBinding->setBinding(0, vbuf); // Setup index buffer. Ogre::HardwareIndexBufferSharedPtr ibuf = Ogre::HardwareBufferManager::getSingleton().createIndexBuffer( Ogre::HardwareIndexBuffer::IT_32BIT, index_buf.size(), Ogre::HardwareBuffer::HBU_STATIC_WRITE_ONLY); ibuf->writeData(0, ibuf->getSizeInBytes(), &index_buf.front(), true); Ogre::SubMesh *subMesh = mesh->createSubMesh(); subMesh->useSharedVertices = true; subMesh->indexData->indexBuffer = ibuf; subMesh->indexData->indexCount = index_buf.size(); subMesh->indexData->indexStart = 0; mesh->_setBounds(Ogre::AxisAlignedBox(bounds.min.X, bounds.min.Y, bounds.min.Z, bounds.max.X, bounds.max.Y, bounds.max.Z)); mesh->_setBoundingSphereRadius((std::max(bounds.max.X - bounds.min.X, std::max(bounds.max.Y - bounds.min.Y, bounds.max.Z - bounds.min.Z))) / 2.0); mesh->load(); Ogre::Entity *ent = mSceneMgr->createEntity(Ogre::String(name) + Ogre::String(m->name) + "E", Ogre::String(name) + Ogre::String(m->name)); ent->setUserAny(Ogre::Any(static_cast<l2p::UObject*>(m.get()))); ent->setMaterialName("StaticMesh/Default"); Ogre::SceneNode *node = mUnrealCordNode->createChildSceneNode(); node->attachObject(ent); return node; }
void GeometryFactory::generateSphericDome (const Ogre::String &name, int segments, DomeType type) { // Return now if already exists if (Ogre::MeshManager::getSingleton ().resourceExists (name)) { return; } Ogre::LogManager::getSingleton ().logMessage ( "Caelum: Creating " + name + " sphere mesh resource..."); // Use the mesh manager to create the mesh Ogre::MeshPtr msh = Ogre::MeshManager::getSingleton ().createManual (name, RESOURCE_GROUP_NAME); // Create a submesh Ogre::SubMesh *sub = msh->createSubMesh (); // Create the shared vertex data Ogre::VertexData *vertexData = new Ogre::VertexData (); msh->sharedVertexData = vertexData; // Define the vertices' format Ogre::VertexDeclaration *vertexDecl = vertexData->vertexDeclaration; size_t currOffset = 0; // Position vertexDecl->addElement (0, currOffset, Ogre::VET_FLOAT3, Ogre::VES_POSITION); currOffset += Ogre::VertexElement::getTypeSize (Ogre::VET_FLOAT3); // Normal vertexDecl->addElement (0, currOffset, Ogre::VET_FLOAT3, Ogre::VES_NORMAL); currOffset += Ogre::VertexElement::getTypeSize (Ogre::VET_FLOAT3); // Texture coordinates vertexDecl->addElement (0, currOffset, Ogre::VET_FLOAT2, Ogre::VES_TEXTURE_COORDINATES, 0); currOffset += Ogre::VertexElement::getTypeSize (Ogre::VET_FLOAT2); // Allocate the vertex buffer switch (type) { case DT_GRADIENTS: vertexData->vertexCount = segments * (segments - 1) + 2; break; case DT_STARFIELD: vertexData->vertexCount = (segments + 1) * (segments + 1); break; }; Ogre::HardwareVertexBufferSharedPtr vBuf = Ogre::HardwareBufferManager::getSingleton ().createVertexBuffer (vertexDecl->getVertexSize (0), vertexData->vertexCount, Ogre::HardwareBuffer::HBU_STATIC_WRITE_ONLY, false); Ogre::VertexBufferBinding *binding = vertexData->vertexBufferBinding; binding->setBinding (0, vBuf); float *pVertex = static_cast<float *>(vBuf->lock (Ogre::HardwareBuffer::HBL_DISCARD)); // Allocate the index buffer switch (type) { case DT_GRADIENTS: sub->indexData->indexCount = 2 * segments * (segments - 1) * 3; break; case DT_STARFIELD: sub->indexData->indexCount = 2 * (segments - 1) * segments * 3; break; }; sub->indexData->indexBuffer = Ogre::HardwareBufferManager::getSingleton ().createIndexBuffer (Ogre::HardwareIndexBuffer::IT_16BIT, sub->indexData->indexCount, Ogre::HardwareBuffer::HBU_STATIC_WRITE_ONLY, false); Ogre::HardwareIndexBufferSharedPtr iBuf = sub->indexData->indexBuffer; unsigned short *pIndices = static_cast<unsigned short *>(iBuf->lock (Ogre::HardwareBuffer::HBL_DISCARD)); // Fill the buffers switch (type) { case DT_GRADIENTS: fillGradientsDomeBuffers (pVertex, pIndices, segments); break; case DT_STARFIELD: fillStarfieldDomeBuffers (pVertex, pIndices, segments); break; }; // Close the vertex buffer vBuf->unlock (); // Close the index buffer iBuf->unlock (); // Finishing it... sub->useSharedVertices = true; msh->_setBounds (Ogre::AxisAlignedBox (-1, -1, -1, 1, 1, 1), false); msh->_setBoundingSphereRadius (1); msh->load (); Ogre::LogManager::getSingleton ().logMessage ( "Caelum: generateSphericDome DONE"); }
void RoR::GfxEnvmap::SetupEnvMap() { m_rtt_texture = Ogre::TextureManager::getSingleton().getByName("EnvironmentTexture"); for (int face = 0; face < NUM_FACES; face++) { m_render_targets[face] = m_rtt_texture->getBuffer(face)->getRenderTarget(); m_cameras[face] = gEnv->sceneManager->createCamera("EnvironmentCamera-" + TOSTRING(face)); m_cameras[face]->setAspectRatio(1.0); m_cameras[face]->setProjectionType(Ogre::PT_PERSPECTIVE); m_cameras[face]->setFixedYawAxis(false); m_cameras[face]->setFOVy(Ogre::Degree(90)); m_cameras[face]->setNearClipDistance(0.1f); m_cameras[face]->setFarClipDistance(gEnv->mainCamera->getFarClipDistance()); Ogre::Viewport* v = m_render_targets[face]->addViewport(m_cameras[face]); v->setOverlaysEnabled(false); v->setClearEveryFrame(true); v->setBackgroundColour(gEnv->mainCamera->getViewport()->getBackgroundColour()); m_render_targets[face]->setAutoUpdated(false); } m_cameras[0]->setDirection(+Ogre::Vector3::UNIT_X); m_cameras[1]->setDirection(-Ogre::Vector3::UNIT_X); m_cameras[2]->setDirection(+Ogre::Vector3::UNIT_Y); m_cameras[3]->setDirection(-Ogre::Vector3::UNIT_Y); m_cameras[4]->setDirection(-Ogre::Vector3::UNIT_Z); m_cameras[5]->setDirection(+Ogre::Vector3::UNIT_Z); if (App::diag_envmap.GetActive()) { // create fancy mesh for debugging the envmap Ogre::Overlay* overlay = Ogre::OverlayManager::getSingleton().create("EnvMapDebugOverlay"); if (overlay) { Ogre::Vector3 position = Ogre::Vector3::ZERO; float scale = 1.0f; Ogre::MeshPtr mesh = Ogre::MeshManager::getSingletonPtr()->createManual("cubeMapDebug", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); // create sub mesh Ogre::SubMesh* sub = mesh->createSubMesh(); // Initialize render operation sub->operationType = Ogre::RenderOperation::OT_TRIANGLE_LIST; // sub->useSharedVertices = true; mesh->sharedVertexData = new Ogre::VertexData; sub->indexData = new Ogre::IndexData; // Create vertex declaration size_t offset = 0; mesh->sharedVertexData->vertexDeclaration->addElement(0, offset, Ogre::VET_FLOAT3, Ogre::VES_POSITION); offset += Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT3); mesh->sharedVertexData->vertexDeclaration->addElement(0, offset, Ogre::VET_FLOAT3, Ogre::VES_TEXTURE_COORDINATES); // Create and bind vertex buffer mesh->sharedVertexData->vertexCount = 14; Ogre::HardwareVertexBufferSharedPtr vertexBuffer = Ogre::HardwareBufferManager::getSingleton().createVertexBuffer( mesh->sharedVertexData->vertexDeclaration->getVertexSize(0), mesh->sharedVertexData->vertexCount, Ogre::HardwareBuffer::HBU_STATIC_WRITE_ONLY); mesh->sharedVertexData->vertexBufferBinding->setBinding(0, vertexBuffer); // Vertex data static const float vertexData[] = { // Position Texture coordinates // Index 0.0, 2.0, -1.0, 1.0, 1.0, // 0 0.0, 1.0, -1.0, -1.0, 1.0, // 1 1.0, 2.0, -1.0, 1.0, -1.0, // 2 1.0, 1.0, -1.0, -1.0, -1.0, // 3 2.0, 2.0, 1.0, 1.0, -1.0, // 4 2.0, 1.0, 1.0, -1.0, -1.0, // 5 3.0, 2.0, 1.0, 1.0, 1.0, // 6 3.0, 1.0, 1.0, -1.0, 1.0, // 7 4.0, 2.0, -1.0, 1.0, 1.0, // 8 4.0, 1.0, -1.0, -1.0, 1.0, // 9 1.0, 3.0, -1.0, 1.0, 1.0, // 10 2.0, 3.0, 1.0, 1.0, 1.0, // 11 1.0, 0.0, -1.0, -1.0, 1.0, // 12 2.0, 0.0, 1.0, -1.0, 1.0, // 13 }; // Fill vertex buffer float* pData = static_cast<float*>(vertexBuffer->lock(Ogre::HardwareBuffer::HBL_DISCARD)); for (size_t vertex = 0, i = 0; vertex < mesh->sharedVertexData->vertexCount; vertex++) { // Position *pData++ = position.x + scale * vertexData[i++]; *pData++ = position.y + scale * vertexData[i++]; *pData++ = 0.0; // Texture coordinates *pData++ = vertexData[i++]; *pData++ = vertexData[i++]; *pData++ = vertexData[i++]; } vertexBuffer->unlock(); // Create index buffer sub->indexData->indexCount = 36; Ogre::HardwareIndexBufferSharedPtr indexBuffer = Ogre::HardwareBufferManager::getSingleton().createIndexBuffer( Ogre::HardwareIndexBuffer::IT_16BIT, sub->indexData->indexCount, Ogre::HardwareBuffer::HBU_STATIC_WRITE_ONLY); sub->indexData->indexBuffer = indexBuffer; // Index data static const Ogre::uint16 indexData[] = { // Indices // Face 0, 1, 2, // 0 2, 1, 3, // 1 2, 3, 4, // 2 4, 3, 5, // 3 4, 5, 6, // 4 6, 5, 7, // 5 6, 7, 8, // 6 8, 7, 9, // 7 10, 2, 11, // 8 11, 2, 4, // 9 3, 12, 5, // 10 5, 12, 13, // 11 }; // Fill index buffer indexBuffer->writeData(0, indexBuffer->getSizeInBytes(), indexData, true); mesh->_setBounds(Ogre::AxisAlignedBox::BOX_INFINITE); mesh->_setBoundingSphereRadius(10); mesh->load(); Ogre::Entity* e = gEnv->sceneManager->createEntity(mesh->getName()); e->setCastShadows(false); e->setRenderQueueGroup(Ogre::RENDER_QUEUE_OVERLAY - 1); e->setVisible(true); e->setMaterialName("tracks/EnvMapDebug"); Ogre::SceneNode* mDebugSceneNode = new Ogre::SceneNode(gEnv->sceneManager); mDebugSceneNode->attachObject(e); mDebugSceneNode->setPosition(Ogre::Vector3(0, 0, -5)); mDebugSceneNode->setFixedYawAxis(true, Ogre::Vector3::UNIT_Y); mDebugSceneNode->setVisible(true); mDebugSceneNode->_update(true, true); mDebugSceneNode->_updateBounds(); overlay->add3D(mDebugSceneNode); overlay->show(); } } }
void BasicTutorial2::createColourCube() { /// Create the mesh via the MeshManager Ogre::MeshPtr msh = MeshManager::getSingleton().createManual("ColourCube", "General"); /// Create one submesh SubMesh* sub = msh->createSubMesh(); const float sqrt13 = 0.577350269f; /* sqrt(1/3) */ /// Define the vertices (8 vertices, each have 3 floats for position and 3 for normal) const size_t nVertices = 8; const size_t vbufCount = 3*2*nVertices; float vertices[vbufCount] = { -100.0,100.0,-100.0, //0 position -sqrt13,sqrt13,-sqrt13, //0 normal 100.0,100.0,-100.0, //1 position sqrt13,sqrt13,-sqrt13, //1 normal 100.0,-100.0,-100.0, //2 position sqrt13,-sqrt13,-sqrt13, //2 normal -100.0,-100.0,-100.0, //3 position -sqrt13,-sqrt13,-sqrt13, //3 normal -100.0,100.0,100.0, //4 position -sqrt13,sqrt13,sqrt13, //4 normal 100.0,100.0,100.0, //5 position sqrt13,sqrt13,sqrt13, //5 normal 100.0,-100.0,100.0, //6 position sqrt13,-sqrt13,sqrt13, //6 normal -100.0,-100.0,100.0, //7 position -sqrt13,-sqrt13,sqrt13, //7 normal }; RenderSystem* rs = Root::getSingleton().getRenderSystem(); RGBA colours[nVertices]; RGBA *pColour = colours; // Use render system to convert colour value since colour packing varies rs->convertColourValue(ColourValue(1.0,0.0,0.0), pColour++); //0 colour rs->convertColourValue(ColourValue(1.0,1.0,0.0), pColour++); //1 colour rs->convertColourValue(ColourValue(0.0,1.0,0.0), pColour++); //2 colour rs->convertColourValue(ColourValue(0.0,0.0,0.0), pColour++); //3 colour rs->convertColourValue(ColourValue(1.0,0.0,1.0), pColour++); //4 colour rs->convertColourValue(ColourValue(1.0,1.0,1.0), pColour++); //5 colour rs->convertColourValue(ColourValue(0.0,1.0,1.0), pColour++); //6 colour rs->convertColourValue(ColourValue(0.0,0.0,1.0), pColour++); //7 colour /// Define 12 triangles (two triangles per cube face) /// The values in this table refer to vertices in the above table const size_t ibufCount = 36; unsigned short faces[ibufCount] = { 0,2,3, 0,1,2, 1,6,2, 1,5,6, 4,6,5, 4,7,6, 0,7,4, 0,3,7, 0,5,1, 0,4,5, 2,7,3, 2,6,7 }; /// Create vertex data structure for 8 vertices shared between submeshes msh->sharedVertexData = new VertexData(); msh->sharedVertexData->vertexCount = nVertices; /// Create declaration (memory format) of vertex data VertexDeclaration* decl = msh->sharedVertexData->vertexDeclaration; size_t offset = 0; // 1st buffer decl->addElement(0, offset, VET_FLOAT3, VES_POSITION); offset += VertexElement::getTypeSize(VET_FLOAT3); decl->addElement(0, offset, VET_FLOAT3, VES_NORMAL); offset += VertexElement::getTypeSize(VET_FLOAT3); /// Allocate vertex buffer of the requested number of vertices (vertexCount) /// and bytes per vertex (offset) HardwareVertexBufferSharedPtr vbuf = HardwareBufferManager::getSingleton().createVertexBuffer( offset, msh->sharedVertexData->vertexCount, HardwareBuffer::HBU_STATIC_WRITE_ONLY); /// Upload the vertex data to the card vbuf->writeData(0, vbuf->getSizeInBytes(), vertices, true); /// Set vertex buffer binding so buffer 0 is bound to our vertex buffer VertexBufferBinding* bind = msh->sharedVertexData->vertexBufferBinding; bind->setBinding(0, vbuf); // 2nd buffer offset = 0; decl->addElement(1, offset, VET_COLOUR, VES_DIFFUSE); offset += VertexElement::getTypeSize(VET_COLOUR); /// Allocate vertex buffer of the requested number of vertices (vertexCount) /// and bytes per vertex (offset) vbuf = HardwareBufferManager::getSingleton().createVertexBuffer( offset, msh->sharedVertexData->vertexCount, HardwareBuffer::HBU_STATIC_WRITE_ONLY); /// Upload the vertex data to the card vbuf->writeData(0, vbuf->getSizeInBytes(), colours, true); /// Set vertex buffer binding so buffer 1 is bound to our colour buffer bind->setBinding(1, vbuf); /// Allocate index buffer of the requested number of vertices (ibufCount) HardwareIndexBufferSharedPtr ibuf = HardwareBufferManager::getSingleton(). createIndexBuffer( HardwareIndexBuffer::IT_16BIT, ibufCount, HardwareBuffer::HBU_STATIC_WRITE_ONLY); /// Upload the index data to the card ibuf->writeData(0, ibuf->getSizeInBytes(), faces, true); /// Set parameters of the submesh sub->useSharedVertices = true; sub->indexData->indexBuffer = ibuf; sub->indexData->indexCount = ibufCount; sub->indexData->indexStart = 0; /// Set bounding information (for culling) msh->_setBounds(AxisAlignedBox(-100,-100,-100,100,100,100)); msh->_setBoundingSphereRadius(Math::Sqrt(3*100*100)); /// Notify -Mesh object that it has been loaded msh->load(); }
void World::LoadSceneFile(string sceneFile) { Ogre::LogManager::getSingleton().setLogDetail(Ogre::LL_LOW); Ogre::SceneNode* worldNode = m_sceneMgr->getRootSceneNode()->createChildSceneNode(); worldNode->rotate(Ogre::Vector3::UNIT_X, Ogre::Degree(-90.0f)); string meshName; float fDrawDistance; bool bIsLod; Ogre::Vector3 position; Ogre::Quaternion rotation; ifstream sceneStream(sceneFile); // skip the title and the table title of the file string strline; int i = 0; while (!sceneStream.eof()) { getline(sceneStream, strline); if (strline.length() == 0) continue; std::istringstream ss(strline); std::string token; getline(ss, token, ','); boost::trim(token); meshName = token; getline(ss, token, ','); boost::trim(token); fDrawDistance = lexical_cast<float>(token); getline(ss, token, ','); boost::trim(token); bIsLod = lexical_cast<bool>(token); getline(ss, token, ','); boost::trim(token); position.x = lexical_cast<float>(token); getline(ss, token, ','); boost::trim(token); position.y = lexical_cast<float>(token); getline(ss, token, ','); boost::trim(token); position.z = lexical_cast<float>(token); getline(ss, token, ','); boost::trim(token); rotation.w = lexical_cast<float>(token); getline(ss, token, ','); boost::trim(token); rotation.x = lexical_cast<float>(token); getline(ss, token, ','); boost::trim(token); rotation.y = lexical_cast<float>(token); getline(ss, token, ','); boost::trim(token); rotation.z = lexical_cast<float>(token); Ogre::MeshPtr mesh; Ogre::Entity* entity; Ogre::Entity* realEntity = m_sceneMgr->createEntity(meshName); if (fDrawDistance > 300.0f && !bIsLod) continue; if (bIsLod) { mesh = Ogre::MeshManager::getSingleton().createManual(meshName + "lod" + std::to_string(i), "General"); // empty mesh mesh->createManualLodLevel(0, "NULL"); mesh->createManualLodLevel(700, meshName); mesh->_setBounds( realEntity->getMesh()->getBounds()); mesh->_setBoundingSphereRadius(realEntity->getMesh()->getBoundingSphereRadius()); mesh->load(); entity->setRenderingDistance(fDrawDistance); entity = m_sceneMgr->createEntity(mesh->getName()); } else { mesh = Ogre::MeshManager::getSingleton().getByName(meshName); entity = m_sceneMgr->createEntity(meshName); entity->setRenderingDistance(fDrawDistance); } m_sceneMgr->destroyEntity(realEntity); Ogre::SceneNode* sceneNode = worldNode->createChildSceneNode(); sceneNode->attachObject(entity); fix_gta_coord(rotation); sceneNode->rotate(rotation); sceneNode->setPosition(position); i++; } worldNode->setScale(5.0f, 5.0f, 5.0f); sceneStream.close(); Ogre::LogManager::getSingleton().setLogDetail(Ogre::LL_NORMAL); }
void TutorialApplication::createSphere(const std::string& strName, const float r, const int nRings, const int nSegments) { Ogre::MeshPtr pSphere = Ogre::MeshManager::getSingleton().createManual(Ogre::String(strName), Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); Ogre::SubMesh *pSphereVertex = pSphere->createSubMesh(); pSphere->sharedVertexData = new Ogre::VertexData(); Ogre::VertexData* vertexData = pSphere->sharedVertexData; // define the vertex format Ogre::VertexDeclaration* vertexDecl = vertexData->vertexDeclaration; size_t currOffset = 0; // positions vertexDecl->addElement(0, currOffset, Ogre::VET_FLOAT3, Ogre::VES_POSITION); currOffset += Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT3); // normals vertexDecl->addElement(0, currOffset, Ogre::VET_FLOAT3, Ogre::VES_NORMAL); currOffset += Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT3); // two dimensional texture coordinates vertexDecl->addElement(0, currOffset, Ogre::VET_FLOAT2, Ogre::VES_TEXTURE_COORDINATES, 0); currOffset += Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT2); // allocate the vertex buffer vertexData->vertexCount = (nRings + 1) * (nSegments+1); Ogre::HardwareVertexBufferSharedPtr vBuf = Ogre::HardwareBufferManager::getSingleton().createVertexBuffer(vertexDecl->getVertexSize(0), vertexData->vertexCount, Ogre::HardwareBuffer::HBU_STATIC_WRITE_ONLY, false); Ogre::VertexBufferBinding* binding = vertexData->vertexBufferBinding; binding->setBinding(0, vBuf); float* pVertex = static_cast<float*>(vBuf->lock(Ogre::HardwareBuffer::HBL_DISCARD)); // allocate index buffer pSphereVertex->indexData->indexCount = 6 * nRings * (nSegments + 1); pSphereVertex->indexData->indexBuffer = Ogre::HardwareBufferManager::getSingleton().createIndexBuffer(Ogre::HardwareIndexBuffer::IT_16BIT, pSphereVertex->indexData->indexCount, Ogre::HardwareBuffer::HBU_STATIC_WRITE_ONLY, false); Ogre::HardwareIndexBufferSharedPtr iBuf = pSphereVertex->indexData->indexBuffer; unsigned short* pIndices = static_cast<unsigned short*>(iBuf->lock(Ogre::HardwareBuffer::HBL_DISCARD)); float fDeltaRingAngle = (Ogre::Math::PI / nRings); float fDeltaSegAngle = (2 * Ogre::Math::PI / nSegments); unsigned short wVerticeIndex = 0 ; // Generate the group of rings for the sphere for( int ring = 0; ring <= nRings; ring++ ) { float r0 = r * sinf (ring * fDeltaRingAngle); float y0 = r * cosf (ring * fDeltaRingAngle); // Generate the group of segments for the current ring for(int seg = 0; seg <= nSegments; seg++) { float x0 = r0 * sinf(seg * fDeltaSegAngle); float z0 = r0 * cosf(seg * fDeltaSegAngle); // Add one vertex to the strip which makes up the sphere *pVertex++ = x0; *pVertex++ = y0; *pVertex++ = z0; Ogre::Vector3 vNormal = Ogre::Vector3(x0, y0, z0).normalisedCopy(); *pVertex++ = vNormal.x; *pVertex++ = vNormal.y; *pVertex++ = vNormal.z; *pVertex++ = (float) seg / (float) nSegments; *pVertex++ = (float) ring / (float) nRings; if (ring != nRings) { // each vertex (except the last) has six indices pointing to it *pIndices++ = wVerticeIndex + nSegments + 1; *pIndices++ = wVerticeIndex; *pIndices++ = wVerticeIndex + nSegments; *pIndices++ = wVerticeIndex + nSegments + 1; *pIndices++ = wVerticeIndex + 1; *pIndices++ = wVerticeIndex; wVerticeIndex ++; } }; // end for seg } // end for ring // Unlock vBuf->unlock(); iBuf->unlock(); // Generate face list pSphereVertex->useSharedVertices = true; // the original code was missing this line: pSphere->_setBounds( Ogre::AxisAlignedBox( Ogre::Vector3(-r, -r, -r), Ogre::Vector3(r, r, r) ), false ); pSphere->_setBoundingSphereRadius(r); // this line makes clear the mesh is loaded (avoids memory leaks) pSphere->load(); }
Ogre::Entity* StageFile::GetModel( const StageInfo& info ) { //DumpSettings("exported/" + info.data.name + ".lua"); VectorTexForGen textures; Ogre::MeshPtr mesh = Ogre::MeshManager::getSingleton().create(info.data.name + "export", "General"); Ogre::SkeletonPtr skeleton = Ogre::SkeletonManager::getSingleton().create(info.data.name + "export", "General"); u32 number_of_files = GetU32LE(0); LOGGER->Log("Number of file " + IntToString(number_of_files) + "\n"); Ogre::Bone* root1 = skeleton->createBone( "0", 0 ); Ogre::Bone* root2 = skeleton->createBone( "1", 1 ); root1->addChild( root2 ); Ogre::Animation* anim = skeleton->createAnimation( "Idle", 1 ); Ogre::NodeAnimationTrack* track1 = anim->createNodeTrack( 0, root1 ); track1->removeAllKeyFrames(); Ogre::TransformKeyFrame* frame1 = track1->createNodeKeyFrame( 0 ); Ogre::Matrix3 matrix; matrix.FromEulerAnglesYXZ( Ogre::Radian( Ogre::Degree( 0 ) ), Ogre::Radian( Ogre::Degree( -90 ) ), Ogre::Radian( Ogre::Degree( 0 ) ) ); Ogre::Quaternion rot; rot.FromRotationMatrix( matrix ); frame1->setRotation( rot ); for (u32 i = 1; i < number_of_files - 1; ++i) { int offset_to_vertex = GetU32LE(0x04 + i * 0x04); MeshExtractor(info.data, "ffvii/battle_stage/" + info.data.name, this, offset_to_vertex, textures, mesh, Ogre::StringConverter::toString(i), 1); } // <OGRE> /////////////////////////////// skeleton->optimiseAllAnimations(); Ogre::SkeletonSerializer skeleton_serializer; skeleton_serializer.exportSkeleton(skeleton.getPointer(), "exported/models/ffvii/battle/stages/" + info.data.name + ".skeleton"); // Update bounds Ogre::AxisAlignedBox aabb(-999, -999, -999, 999, 999, 999); mesh->_setBounds(aabb, false); mesh->_setBoundingSphereRadius(999); mesh->setSkeletonName( "models/ffvii/battle/stages/" + info.data.name + ".skeleton" ); Ogre::MeshSerializer ser; ser.exportMesh(mesh.getPointer(), "exported/models/ffvii/battle/stages/" + info.data.name + ".mesh"); // create and export textures for model if( textures.size() > 0 ) { int number_of_files = GetU32LE( 0x00 ); int offset_to_texture = GetU32LE( number_of_files * 0x04 ); Vram* vram = Vram::MakeInstance().release(); LoadTimFileToVram( this, offset_to_texture, vram ); //vram->Save( "qqq" ); CreateTexture( vram, info.data, "exported/models/ffvii/battle/stages/" + info.data.name + ".png", textures ); delete vram; } CreateMaterial("ffvii/battle_stage/" + info.data.name, "exported/models/ffvii/battle/stages/" + info.data.name + ".material", "models/ffvii/battle/stages/" + info.data.name + ".png", "", ""); Ogre::SceneManager* scene_manager = Ogre::Root::getSingleton().getSceneManager( "Scene" ); Ogre::Entity* thisEntity = scene_manager->createEntity( info.data.name, "models/ffvii/battle/stages/" + info.data.name + ".mesh" ); //thisEntity->setDisplaySkeleton(true); //thisEntity->setDebugDisplayEnabled(true); thisEntity->setVisible(false); thisEntity->getAnimationState( "Idle" )->setEnabled( true ); thisEntity->getAnimationState( "Idle" )->setLoop( true ); Ogre::SceneNode* thisSceneNode = scene_manager->getRootSceneNode()->createChildSceneNode(); thisSceneNode->setPosition( 0, 0, 0 ); thisSceneNode->roll( Ogre::Radian( Ogre::Degree( 180.0f ) ) ); thisSceneNode->yaw( Ogre::Radian( Ogre::Degree( 120.0f ) ) ); thisSceneNode->pitch( Ogre::Radian( Ogre::Degree( 90.0f ) ) ); thisSceneNode->attachObject( thisEntity ); return thisEntity; }
bool Terrain::createTerrain() { if(mMainViewport == NULL) mMainViewport = Core::getSingleton().mCamera->getViewport(); Ogre::CompositorManager::getSingleton().addCompositor(mMainViewport, "DemoCompositor"); Ogre::CompositorManager::getSingleton().setCompositorEnabled(mMainViewport, "DemoCompositor", true); mMapData = MapDataManager::getSingletonPtr(); DataLibrary* datalib = DataLibrary::getSingletonPtr(); int terrainszie = mMapData->getMapSize() + 2 * MAPBOLDER + 1; Core::getSingleton().mSceneMgr->setSkyBox(true, "SkyBox",200); Ogre::GpuSharedParametersPtr sharedparams = Ogre::GpuProgramManager::getSingleton().getSharedParameters("TestSharedParamsName"); float border = mMapData->getMapSize() * 12.0f; sharedparams->setNamedConstant("border", border); //创建灯光 Core::getSingleton().mSceneMgr->setAmbientLight(Ogre::ColourValue(0.5f, 0.5f, 0.5f)); mLight = Core::getSingleton().mSceneMgr->createLight("TerrainLight"); mLight->setType(Ogre::Light::LT_DIRECTIONAL); mLight->setPosition(-500.0f,500.0f, 500.0f); mLight->setDirection(1.0f, -1.0f, -1.0f); mLight->setDiffuseColour(Ogre::ColourValue(0.5f, 0.5f,0.5f)); mLight->setSpecularColour(Ogre::ColourValue(0.8f, 0.8f,0.8f)); //设置深度图投影 Ogre::TexturePtr tex = Ogre::TextureManager::getSingleton().getByName("shadowdepthmap"); if(tex.isNull()) tex = Ogre::TextureManager::getSingleton().createManual("shadowdepthmap", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D, 2048, 2048, 0, Ogre::PF_FLOAT16_R, Ogre::TU_RENDERTARGET); mShadowDepthMapTarget = tex->getBuffer()->getRenderTarget(); Ogre::Viewport* vp = mShadowDepthMapTarget->addViewport(CameraContral::getSingleton().getShadowMapCamera()); vp->setSkiesEnabled(false); vp->setOverlaysEnabled(false); vp->setVisibilityMask(VISMASK_OPAQUE); vp->setMaterialScheme("WriteDepthMap"); vp->setBackgroundColour(Ogre::ColourValue(1.0f,1.0f,1.0f)); mShadowDepthMapTarget->addListener(this); //弱爆了…… Ogre::MaterialPtr mat; mat = Ogre::MaterialManager::getSingleton().getByName("TerrainTile"); Ogre::AliasTextureNamePairList texAliasList; std::string texname; datalib->getData("GameData/BattleData/MapData/Ground/G0Tex",texname); texAliasList.insert(std::make_pair("Diffuse",texname)); datalib->getData("GameData/BattleData/MapData/Ground/G1Tex",texname); texAliasList.insert(std::make_pair("Diffuse1",texname)); datalib->getData("GameData/BattleData/MapData/Ground/G2Tex",texname); texAliasList.insert(std::make_pair("Diffuse2",texname)); datalib->getData("GameData/BattleData/MapData/Ground/G3Tex",texname); texAliasList.insert(std::make_pair("Diffuse3",texname)); mat->applyTextureAliases(texAliasList); texAliasList.clear(); mat = Ogre::MaterialManager::getSingleton().getByName("CliffMat1"); datalib->getData("GameData/BattleData/MapData/Ground/G0Tex",texname); texAliasList.insert(std::make_pair("Diffuse",texname)); texAliasList.insert(std::make_pair("Diffuse1","Cliff.tga")); mat->applyTextureAliases(texAliasList); texAliasList.clear(); mat = Ogre::MaterialManager::getSingleton().getByName("CliffMat2"); datalib->getData("GameData/BattleData/MapData/Ground/G1Tex",texname); texAliasList.insert(std::make_pair("Diffuse",texname)); texAliasList.insert(std::make_pair("Diffuse1","Cliff.tga")); mat->applyTextureAliases(texAliasList); texAliasList.clear(); mat = Ogre::MaterialManager::getSingleton().getByName("CliffMat3"); datalib->getData("GameData/BattleData/MapData/Ground/G2Tex",texname); texAliasList.insert(std::make_pair("Diffuse",texname)); texAliasList.insert(std::make_pair("Diffuse1","Cliff.tga")); mat->applyTextureAliases(texAliasList); texAliasList.clear(); mat = Ogre::MaterialManager::getSingleton().getByName("CliffMat4"); datalib->getData("GameData/BattleData/MapData/Ground/G3Tex",texname); texAliasList.insert(std::make_pair("Diffuse",texname)); texAliasList.insert(std::make_pair("Diffuse1","Cliff.tga")); mat->applyTextureAliases(texAliasList); texAliasList.clear(); mat = Ogre::MaterialManager::getSingleton().getByName("BankMat1"); datalib->getData("GameData/BattleData/MapData/Ground/G0Tex",texname); texAliasList.insert(std::make_pair("Diffuse",texname)); texAliasList.insert(std::make_pair("Diffuse1","Cliff.tga")); mat->applyTextureAliases(texAliasList); texAliasList.clear(); mat = Ogre::MaterialManager::getSingleton().getByName("BankMat2"); datalib->getData("GameData/BattleData/MapData/Ground/G1Tex",texname); texAliasList.insert(std::make_pair("Diffuse",texname)); texAliasList.insert(std::make_pair("Diffuse1","Cliff.tga")); mat->applyTextureAliases(texAliasList); texAliasList.clear(); mat = Ogre::MaterialManager::getSingleton().getByName("BankMat3"); datalib->getData("GameData/BattleData/MapData/Ground/G2Tex",texname); texAliasList.insert(std::make_pair("Diffuse",texname)); texAliasList.insert(std::make_pair("Diffuse1","Cliff.tga")); mat->applyTextureAliases(texAliasList); texAliasList.clear(); mat = Ogre::MaterialManager::getSingleton().getByName("BankMat4"); datalib->getData("GameData/BattleData/MapData/Ground/G3Tex",texname); texAliasList.insert(std::make_pair("Diffuse",texname)); texAliasList.insert(std::make_pair("Diffuse1","Cliff.tga")); mat->applyTextureAliases(texAliasList); texAliasList.clear(); //创建地面Mesh mTerrainNode = Core::getSingleton().mSceneMgr->getRootSceneNode()->createChildSceneNode("TerrainNode"); int numVertices = terrainszie * terrainszie * VERTEX_QUAD; int numIndex = terrainszie * terrainszie * VERTEX_PREQUAD; Ogre::MeshPtr mTerrainMesh = Ogre::MeshManager::getSingleton().createManual("TerrianMesh", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); Ogre::SubMesh* subMesh = mTerrainMesh->createSubMesh(); subMesh->useSharedVertices=false; subMesh->setMaterialName("TerrainTile"); // 创建顶点数据结构 subMesh->vertexData = new Ogre::VertexData(); subMesh->vertexData->vertexStart = 0; subMesh->vertexData->vertexCount = numVertices; //顶点声明与缓冲区绑定 Ogre::VertexDeclaration* vdecl = subMesh->vertexData->vertexDeclaration; Ogre::VertexBufferBinding* vbind = subMesh->vertexData->vertexBufferBinding; //设置顶点数据结构 size_t offsetUV = 0; vdecl->addElement(VERTEX_POS_BINDING, 0, Ogre::VET_FLOAT3,Ogre::VES_POSITION);//向顶点添加一个位置元素 vdecl->addElement(VERTEX_NOM_BINDING, 0, Ogre::VET_FLOAT3,Ogre::VES_NORMAL); for(int i = 0 ; i < TEXTURE_COUNT ; i ++) { offsetUV += vdecl->addElement (VERTEX_UV_BINDING, offsetUV, Ogre::VET_FLOAT2, Ogre::VES_TEXTURE_COORDINATES , i).getSize(); } // 创建世界坐标顶点缓冲区 Ogre::HardwareVertexBufferSharedPtr vbufPos = Ogre::HardwareBufferManager::getSingleton().createVertexBuffer( vdecl->getVertexSize(VERTEX_POS_BINDING), numVertices, Ogre::HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY); vbind->setBinding(VERTEX_POS_BINDING, vbufPos); Ogre::HardwareVertexBufferSharedPtr vbufNOM = Ogre::HardwareBufferManager::getSingleton().createVertexBuffer( vdecl->getVertexSize(VERTEX_NOM_BINDING), numVertices, Ogre::HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY); vbind->setBinding(VERTEX_NOM_BINDING, vbufNOM); // 创建纹理坐标顶点缓冲区 Ogre::HardwareVertexBufferSharedPtr vbufUV = Ogre::HardwareBufferManager::getSingleton().createVertexBuffer( vdecl->getVertexSize(VERTEX_UV_BINDING), numVertices, Ogre::HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY); vbind->setBinding(VERTEX_UV_BINDING, vbufUV); // 创建索引缓冲区 Ogre::HardwareIndexBufferSharedPtr indexBuffer = Ogre::HardwareBufferManager::getSingleton().createIndexBuffer( Ogre::HardwareIndexBuffer::IT_16BIT , numIndex, Ogre::HardwareBuffer::HBU_STATIC_WRITE_ONLY); //创建地形 float* pBufferPos = (float*)vbufPos->lock(Ogre::HardwareBuffer::HBL_DISCARD); float* pBufferUV = (float*)vbufUV->lock(Ogre::HardwareBuffer::HBL_DISCARD); float* pBufferNom = (float*)vbufNOM->lock(Ogre::HardwareBuffer::HBL_DISCARD); float startpos = - terrainszie * TILESIZE / 2; for(int y = 0 ; y < terrainszie; y ++) { for(int x = 0 ; x < terrainszie; x ++) { createTile(x, y, startpos + x * TILESIZE, startpos + y * TILESIZE, pBufferPos, pBufferUV, pBufferNom); pBufferPos += 3 * VERTEX_QUAD ; pBufferNom += 3 * VERTEX_QUAD ; pBufferUV += 2 * VERTEX_QUAD * 4; } } vbufNOM->unlock(); vbufUV->unlock(); vbufPos->unlock(); //写入索引信息 // 锁定索引缓冲区 Ogre::ushort* pIdx = (Ogre::ushort*)indexBuffer->lock(Ogre::HardwareBuffer::HBL_DISCARD); for(int y = 0 ; y < terrainszie ; y ++) { for(int x = 0 ; x < terrainszie ; x ++) { Ogre::ushort iIndexTopLeft = (x + y * terrainszie) * VERTEX_QUAD; Ogre::ushort iIndexTopRight = iIndexTopLeft + 1; Ogre::ushort iIndexBottomLeft = iIndexTopLeft + 2; Ogre::ushort iIndexBottomRight = iIndexTopLeft + 3; *pIdx++ = iIndexBottomLeft; *pIdx++ = iIndexBottomRight; *pIdx++ = iIndexTopLeft; *pIdx++ = iIndexBottomRight; *pIdx++ = iIndexTopRight; *pIdx++ = iIndexTopLeft; } } indexBuffer->unlock(); //设置模型的的索引数据 subMesh->indexData->indexBuffer = indexBuffer; subMesh->indexData->indexStart = 0; subMesh->indexData->indexCount =numIndex; Ogre::AxisAlignedBox meshBounds(startpos,0,startpos, -startpos,5,-startpos); mTerrainMesh->_setBounds(meshBounds); mTerrainEntity = Core::getSingleton().mSceneMgr->createEntity("TerrianMesh"); mTerrainNode->attachObject(mTerrainEntity); mTerrainEntity->setQueryFlags(QUERYMASK_TERRAIN); mTerrainNode->setPosition(0,0,0); //创建水面 tex = Ogre::TextureManager::getSingleton().getByName("reflection"); if(tex.isNull()) tex = Ogre::TextureManager::getSingleton().createManual("reflection", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D, 512, 512, 0, Ogre::PF_R8G8B8, Ogre::TU_RENDERTARGET); mReflectionTarget = tex->getBuffer()->getRenderTarget(); mReflectionTarget->addViewport(Core::getSingleton().mCamera)->setOverlaysEnabled(false); mReflectionTarget->addListener(this); // mat = Ogre::MaterialManager::getSingleton().getByName("ReflectionWater"); // tech = mat->getTechnique(0); // pass = tech->getPass(0); // tu = pass->getTextureUnitState(1); // tu->setTextureName(tex->getName()); mWaterPlane = Ogre::Plane(Ogre::Vector3::UNIT_Y, WATERHEIGHT); mWaterNode = Core::getSingleton().mSceneMgr->getRootSceneNode()->createChildSceneNode("WaterNode"); mWaterObject = Core::getSingleton().mSceneMgr->createManualObject("WaterObject"); mWaterObject->begin("DemoWater",Ogre::RenderOperation::OT_TRIANGLE_LIST); startpos += TILESIZE/2; for(int y = 0; y < terrainszie; y++) for(int x = 0; x < terrainszie; x++) { if(mMapData->getTerrainType(x -MAPBOLDER, y -MAPBOLDER ) == Water) { mWaterObject->position(startpos + x * TILESIZE, 0.0f, startpos + y * TILESIZE); mWaterObject->colour(1.0f,1.0f,1.0f); mWaterObject->normal(0.0f,1.0f,0.0f); mWaterObject->textureCoord(0.0f,0.0f); mWaterObject->position(startpos + (x+1) * TILESIZE, 0.0f, startpos + (y+1) * TILESIZE); mWaterObject->colour(1.0f,1.0f,1.0f); mWaterObject->normal(0.0f,1.0f,0.0f); mWaterObject->textureCoord(1.0f,1.0f); mWaterObject->position(startpos + (x+1) * TILESIZE, 0.0f, startpos + y * TILESIZE); mWaterObject->colour(1.0f,1.0f,1.0f); mWaterObject->normal(0.0f,1.0f,0.0f); mWaterObject->textureCoord(1.0f,0.0f); mWaterObject->position(startpos + (x+1) * TILESIZE, 0.0f, startpos + (y+1) * TILESIZE); mWaterObject->colour(1.0f,1.0f,1.0f); mWaterObject->normal(0.0f,1.0f,0.0f); mWaterObject->textureCoord(1.0f,1.0f); mWaterObject->position(startpos + x * TILESIZE, 0.0f, startpos + y * TILESIZE); mWaterObject->colour(1.0f,1.0f,1.0f); mWaterObject->normal(0.0f,1.0f,0.0f); mWaterObject->textureCoord(0.0f,0.0f); mWaterObject->position(startpos + x * TILESIZE, 0.0f, startpos + (y+1) * TILESIZE); mWaterObject->colour(1.0f,1.0f,1.0f); mWaterObject->normal(0.0f,1.0f,0.0f); mWaterObject->textureCoord(0.0f,1.0f); } } mWaterObject->end(); mWaterNode->attachObject(mWaterObject); mWaterNode->setPosition(0,WATERHEIGHT,0); //设置摄像机移动范围 float minx = 0.0f;// = ( - (float)(terrainszie - 2 * MAPBOLDER) / 2.0f - 1.0f) * TILESIZE ; getWorldCoords(0,0,minx,minx); minx -= TILESIZE/2; CameraContral::getSingleton().setMoveRect(minx, minx); CameraContral::getSingleton().resetCamera(); //深度投影测试 // Ogre::MeshManager::getSingleton().createPlane("testplane", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, // mWaterPlane, 64, 64, 1, 1, true, 1, 1, 1, Ogre::Vector3::UNIT_Z); // Ogre::Entity* testent = Core::getSingleton().mSceneMgr->createEntity("testplaneent", "testplane"); // testent->setMaterialName("DepthTest"); // Ogre::SceneNode* testnode = Core::getSingleton().mSceneMgr->getRootSceneNode()->createChildSceneNode(); // testnode->attachObject(testent); // testnode->setPosition(0.0f,10.0f,0.0f); return true; }