bool Topic_04_NavMesh::onCreate(int a_argc, char* a_argv[]) 
{
	// initialise the Gizmos helper class
	Gizmos::create();

	// create a world-space matrix for a camera
	m_cameraMatrix = glm::inverse( glm::lookAt(glm::vec3(10,10,10),glm::vec3(0,0,0), glm::vec3(0,1,0)) );
	
	// create a perspective projection matrix with a 90 degree field-of-view and widescreen aspect ratio
	m_projectionMatrix = glm::perspective(glm::pi<float>() * 0.25f, DEFAULT_SCREENWIDTH/(float)DEFAULT_SCREENHEIGHT, 0.1f, 1000.0f);

	// set the clear colour and enable depth testing and backface culling
	glClearColor(0.25f,0.25f,0.25f,1);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_CULL_FACE);

	m_sponza = new FBXFile();
	m_sponza->load("./resources/models/SponzaSimple.fbx", FBXFile::UNITS_CENTIMETER);
	createOpenGLBuffers(m_sponza);

	m_navMesh = new FBXFile();
	m_navMesh->load("./resources/models/SponzaSimpleNavMesh.fbx", FBXFile::UNITS_CENTIMETER);
	// createOpenGLBuffers(m_navMesh);

	buildNavMesh(m_navMesh->getMeshByIndex(0), m_graph);

	unsigned int vertShader = Utility::loadShader("./resources/shaders/sponza.vert", GL_VERTEX_SHADER);
	unsigned int fragShader = Utility::loadShader("./resources/shaders/sponza.frag", GL_FRAGMENT_SHADER);
	m_shaderProgram = Utility::createProgram(vertShader, 0, 0, 0, fragShader);
	glDeleteShader(vertShader);
	glDeleteShader(fragShader);

	return true;
}
Beispiel #2
0
    void MapBuilder::buildMeshFromFile(char* name)
    {
        FILE* file = fopen(name, "rb");
        if (!file)
            return;

        printf("Building mesh from file\n");
        int tileX, tileY, mapId;
        if (fread(&mapId, sizeof(int), 1, file) != 1)
            return;
        if (fread(&tileX, sizeof(int), 1, file) != 1)
            return;
        if (fread(&tileY, sizeof(int), 1, file) != 1)
            return;

        dtNavMesh* navMesh = NULL;
        buildNavMesh(mapId, navMesh);
        if (!navMesh)
        {
            printf("Failed creating navmesh!              \n");
            fclose(file);
            return;
        }

        uint32 verticesCount, indicesCount;
        if (fread(&verticesCount, sizeof(uint32), 1, file) != 1)
            return;
        if (fread(&indicesCount, sizeof(uint32), 1, file) != 1)
            return;

        float* verts = new float[verticesCount];
        int* inds = new int[indicesCount];

        if (fread(verts, sizeof(float), verticesCount, file) != verticesCount)
            return;
        if (fread(inds, sizeof(int), indicesCount, file) != indicesCount)
            return;

        MeshData data;

        for (uint32 i = 0; i < verticesCount; ++i)
            data.solidVerts.append(verts[i]);

        for (uint32 i = 0; i < indicesCount; ++i)
            data.solidTris.append(inds[i]);

        TerrainBuilder::cleanVertices(data.solidVerts, data.solidTris);
        // get bounds of current tile
        float bmin[3], bmax[3];
        getTileBounds(tileX, tileY, data.solidVerts.getCArray(), data.solidVerts.size() / 3, bmin, bmax);

        // build navmesh tile
        buildMoveMapTile(mapId, tileX, tileY, data, bmin, bmax, navMesh);
        fclose(file);
    }
    void MapBuilder::buildMap(uint32 mapID)
    {
#ifndef __APPLE__
        //printf("[Thread %u] Building map %03u:\n", uint32(ACE_Thread::self()), mapID);
#endif

        std::set<uint32>* tiles = getTileList(mapID);

        // make sure we process maps which don't have tiles
        if (!tiles->size())
        {
            // convert coord bounds to grid bounds
            uint32 minX, minY, maxX, maxY;
            getGridBounds(mapID, minX, minY, maxX, maxY);

            // add all tiles within bounds to tile list.
            for (uint32 i = minX; i <= maxX; ++i)
                for (uint32 j = minY; j <= maxY; ++j)
                    tiles->insert(StaticMapTree::packTileID(i, j));
        }

        if (!tiles->empty())
        {
            // build navMesh
            dtNavMesh* navMesh = NULL;
            buildNavMesh(mapID, navMesh);
            if (!navMesh)
            {
                printf("[Map %04i] Failed creating navmesh!\n", mapID);
                return;
            }

            // now start building mmtiles for each tile
            printf("[Map %04i] We have %u tiles.                          \n", mapID, (unsigned int)tiles->size());
            for (std::set<uint32>::iterator it = tiles->begin(); it != tiles->end(); ++it)
            {
                uint32 tileX, tileY;

                // unpack tile coords
                StaticMapTree::unpackTileID((*it), tileX, tileY);

                if (shouldSkipTile(mapID, tileX, tileY))
                    continue;

                buildTile(mapID, tileX, tileY, navMesh);
            }

            dtFreeNavMesh(navMesh);
        }

        printf("[Map %04u] Complete!\n", mapID);
    }
    void MapBuilder::buildSingleTile(uint32 mapID, uint32 tileX, uint32 tileY)
    {
        dtNavMesh* navMesh = NULL;
        buildNavMesh(mapID, navMesh);
        if (!navMesh)
        {
            printf("Failed creating navmesh!              \n");
            return;
        }

        buildTile(mapID, tileX, tileY, navMesh);
        dtFreeNavMesh(navMesh);
    }
    void MapBuilder::buildMap(int mapID)
    {
        printf("Building map %03u:\n", mapID);

        set<int>* tiles = getTileList(mapID);

        // make sure we process maps which don't have tiles
        if (!tiles->size())
        {
            // convert coord bounds to grid bounds
            int minX, minY, maxX, maxY;
            getGridBounds(mapID, minX, minY, maxX, maxY);

            // add all tiles within bounds to tile list.
            for (int i = minX; i <= maxX; ++i)
                for (int j = minY; j <= maxY; ++j)
                    { tiles->insert(StaticMapTree::packTileID(i, j)); }
        }

        if (!tiles->size())
            { return; }

        // build navMesh
        dtNavMesh* navMesh = NULL;
        buildNavMesh(mapID, navMesh);
        if (!navMesh)
        {
            printf("Failed creating navmesh!              \n");
            return;
        }

        // now start building mmtiles for each tile
        printf("We have %u tiles.                          \n", (unsigned int)tiles->size());
        for (set<int>::iterator it = tiles->begin(); it != tiles->end(); ++it)
        {
            int tileX, tileY;

            // unpack tile coords
            StaticMapTree::unpackTileID((*it), tileX, tileY);

            if (shouldSkipTile(mapID, tileX, tileY))
                { continue; }

            buildTile(mapID, tileX, tileY, navMesh);
        }

        dtFreeNavMesh(navMesh);

        printf("Complete!                               \n\n");
    }
bool RecastInterface::buildNavMesh(std::vector<Ogre::Entity*> sourceMeshes)
{
	if(sourceMeshes.empty())
	{
		std::cout << "NavMesh build failed: No entities provided" << std::endl;
		Ogre::LogManager::getSingletonPtr()->logMessage("NavMesh build failed: No entities provided");
		return false;
	}

	InputGeometry* param = new InputGeometry(sourceMeshes);
	bool result = buildNavMesh(param);
	delete param;

	return result;
}
    void MapBuilder::buildSingleTile(int mapID, int tileX, int tileY)
    {
        dtNavMesh* navMesh = NULL;
        dtNavMeshParams* meshParams = NULL;
        buildNavMesh(mapID, navMesh, meshParams);
        if (!navMesh)
        {
            printf("Failed creating navmesh!              \n");
            return;
        }

        buildTile(mapID, tileX, tileY, navMesh);
        dtFreeNavMesh(navMesh);
        if (meshParams)
          { delete meshParams; }
    }
Beispiel #8
0
static int create_navmesh_exec(bContext *C, wmOperator *op)
{
	Scene* scene= CTX_data_scene(C);
	LinkNode* obs= NULL;
	Base* navmeshBase= NULL;

	CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
		if (base->object->type == OB_MESH) {
			if (base->object->body_type==OB_BODY_TYPE_NAVMESH) {
				if (!navmeshBase || base == scene->basact) {
					navmeshBase= base;
				}
			}
			else {
				BLI_linklist_append(&obs, (void*)base->object);
			}
		}
	}
	CTX_DATA_END;

	if (obs) {
		struct recast_polyMesh *pmesh= NULL;
		struct recast_polyMeshDetail *dmesh= NULL;

		int nverts= 0, ntris= 0;
		int *tris= 0;
		float *verts= NULL;

		createVertsTrisData(C, obs, &nverts, &verts, &ntris, &tris);
		BLI_linklist_free(obs, NULL);
		buildNavMesh(&scene->gm.recastData, nverts, verts, ntris, tris, &pmesh, &dmesh);
		createRepresentation(C, pmesh, dmesh, navmeshBase);

		MEM_freeN(verts);
		MEM_freeN(tris);

		return OPERATOR_FINISHED;
	}
	else {
		BKE_report(op->reports, RPT_ERROR, "No mesh objects found");

		return OPERATOR_CANCELLED;
	}
}
    void MapBuilder::buildMap(int mapID, bool standAlone)
    {
        set<uint32>* tiles = getTileList(mapID);

        // make sure we process maps which don't have tiles
        if (!tiles->size())
        {
            // convert coord bounds to grid bounds
            uint32 minX, minY, maxX, maxY;
            getGridBounds(mapID, minX, minY, maxX, maxY);

            // add all tiles within bounds to tile list.
            for (uint32 i = minX; i <= maxX; ++i)
                for (uint32 j = minY; j <= maxY; ++j)
                    { tiles->insert(StaticMapTree::packTileID(i, j)); }
        }

        if (!tiles->size())
            { return; }

        // build navMesh
        dtNavMesh* navMesh = NULL;
        dtNavMeshParams* meshParams = NULL;
        buildNavMesh(mapID, navMesh, meshParams);
        if (!navMesh)
        {
            printf("Failed creating navmesh for map %03u!              \n", mapID);
            if (meshParams)
                { delete meshParams; }
            return;
        }

        if (activated())
          { dtFreeNavMesh(navMesh); }  // each tile will get it's own pointer to navMesh 

        // now start building/scheduling mmtiles for each tile
        printf(" %s map %03u [%u tiles]\n", activated() ? "Scheduling" : "Building", mapID, (unsigned int)tiles->size());
        for (set<uint32>::iterator it = tiles->begin(); it != tiles->end(); ++it)
        {
            uint32 tileX, tileY;

            // unpack tile coords
            StaticMapTree::unpackTileID((*it), tileX, tileY);

            if (shouldSkipTile(mapID, tileX, tileY))
                { continue; }

            if (!activated())
            {
                buildTile(mapID, tileX, tileY, navMesh);
            }
            else
            {
                dtNavMesh *mesh = NULL;
                buildNavMesh(mapID, mesh, meshParams); //meshParams is not null, so we get a new pointer to dtNavMesh
                if (mesh)
                {
                    TileBuilder* tb = new TileBuilder(this, mapID, tileX, tileY, mesh);
                    Tile_Message_Block *mb = new Tile_Message_Block(tb);
                    if (m_threadPool->putq(mb) == -1)
                      { break; }
                }
            }
        }
        if (activated() && standAlone)
        {
            Tile_Message_Block *finish_mb = new Tile_Message_Block(NULL);
            finish_mb->msg_type(ACE_Message_Block::MB_HANGUP);
            m_threadPool->putq(finish_mb);
            m_threadPool->wait();
        }

        if (!activated())
          { dtFreeNavMesh(navMesh); }

        if (meshParams)
            { delete meshParams; }

        if (!activated())
          { printf(" Map %03u complete!\n\n", mapID); }
    }