void Scene::initGeometry(const char *filename) { /// Open a DRONE file if (!filename || drn_open( &m_cache, filename, DRN_READ_MMAP) < 0) { fprintf( stderr, "\"%s\" is an invalid filename.\n", filename); exit( EXIT_FAILURE ); } m_bFileLoaded = true; /// Check if it matches the 'DRONE scene' format if (0 != strcmp(drn_get_description(&m_cache), DRN_SCENE_CACHE_DESCRIPTION)) { fprintf( stderr, "\"%s\" is an invalid drone scene file.\n", filename); exit( EXIT_FAILURE ); } /// Retrieve the scene chunk id drn_chunk_id_t sceneEntryId; { uint64_t status; status = drn_get_matching_chunks( &m_cache, drn_get_map_id( &m_cache, DRN_SCENE_CHUNK_TYPE_TAG), DRN_SCENE_CHUNK_TAG_TYPE_VALUE, 1, &sceneEntryId); if (1u != status) { fprintf( stderr, "Error: could not retrieve the scene chunk id.\n", filename); exit( EXIT_FAILURE ); } } /// Retrieve scene datas drn_scene::resolveScene(&m_cache, &m_drnScene, drn_get_chunk(&m_cache, sceneEntryId)); m_bSceneResolved = true; /// Resize global buffers m_meshInit.resize(m_drnScene.numMeshes, false); m_primitives.resize(m_drnScene.numMeshes); //--------------- cubewire::setup( m_cubeWire ); //-------------- glGenVertexArrays( 1, &m_screenmappingVAO); }
size_t loadInMemory(std::vector<Chunk>& memory, bool* loadedLeaf, Leaf l, double distance, uint16_t nbSub_lvl2, size_t freeMemory){ drn_t cache; /* check if we can load the leaf - enough size in the memory */ uint32_t lengthVoxelArray = nbSub_lvl2*nbSub_lvl2*nbSub_lvl2; uint32_t leafBytesSize = lengthVoxelArray*VOXELDATA_BYTES_SIZE + l.nbVertices_lvl2*3*sizeof(double); //~ std::cout<<"//-> LeafBytesSize : "<<leafBytesSize<<std::endl; while(leafBytesSize > freeMemory && memory.size()>0){ freeMemory += freeInMemory(memory, loadedLeaf); } /* load the voxel data */ uint32_t test_cache = drn_open(&cache, "./voxels_data/voxel_intersec_1.data", DRN_READ_NOLOAD); if(test_cache <0){ throw std::runtime_error("unable to open data file"); } VoxelData* voxArray = NULL; voxArray = new VoxelData[lengthVoxelArray]; test_cache = drn_read_chunk(&cache, 2*l.id + CONFIGCHUNK_OFFSET, voxArray); if(test_cache <0){ throw std::runtime_error("unable to read data file"); } test_cache = drn_close(&cache); if(test_cache <0){ throw std::runtime_error("unable to close data file"); } /* load the mesh */ test_cache = drn_open(&cache, "./voxels_data/voxel_intersec_1.data", DRN_READ_MMAP); if(test_cache <0){ throw std::runtime_error("unable to open data file in MMAP mode"); } const void* pMesh = drn_get_chunk(&cache, 2*l.id + 1 + CONFIGCHUNK_OFFSET); if(NULL == pMesh){ throw std::runtime_error("unable to get a chunk pointer"); } GLuint meshVBO = 0; glGenBuffers(1, &meshVBO); glBindBuffer(GL_ARRAY_BUFFER, meshVBO); glBufferData(GL_ARRAY_BUFFER, l.nbVertices_lvl2*sizeof(Vertex), pMesh, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); test_cache = drn_close(&cache); if(test_cache <0){ throw std::runtime_error("unable to close data file"); } /* setting the mesh */ GLuint meshVAO = 0; glGenVertexArrays(1, &meshVAO); glBindVertexArray(meshVAO); glEnableVertexAttribArray(POSITION_LOCATION); glEnableVertexAttribArray(NORMAL_LOCATION); glEnableVertexAttribArray(BENDING_LOCATION); glEnableVertexAttribArray(DRAIN_LOCATION); glEnableVertexAttribArray(GRADIENT_LOCATION); glEnableVertexAttribArray(SURFACE_LOCATION); glBindBuffer(GL_ARRAY_BUFFER, meshVBO); glVertexAttribPointer(POSITION_LOCATION, 3, GL_DOUBLE, GL_FALSE, sizeof(Vertex), reinterpret_cast<const GLvoid*>(0)); glVertexAttribPointer(NORMAL_LOCATION, 3, GL_DOUBLE, GL_FALSE, sizeof(Vertex), reinterpret_cast<const GLvoid*>(3*sizeof(GLdouble))); glVertexAttribPointer(BENDING_LOCATION, 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<const GLvoid*>(6*sizeof(GLdouble))); glVertexAttribPointer(DRAIN_LOCATION, 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<const GLvoid*>(6*sizeof(GLdouble)+sizeof(GLfloat))); glVertexAttribPointer(GRADIENT_LOCATION, 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<const GLvoid*>(6*sizeof(GLdouble)+2*sizeof(GLfloat))); glVertexAttribPointer(SURFACE_LOCATION, 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<const GLvoid*>(6*sizeof(GLdouble)+3*sizeof(GLfloat))); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); //add the new chunk to the vector Chunk newChunk; newChunk.voxels = voxArray; newChunk.pos = l.pos; newChunk.idxLeaf = l.id; newChunk.d = distance; newChunk.vao = meshVAO; newChunk.vbo = meshVBO; newChunk.byteSize = leafBytesSize; memory.push_back(newChunk); //~ std::cout<<"//-> Leaf "<<l.id<<" loaded."<<std::endl; loadedLeaf[l.id] = true; freeMemory -= leafBytesSize; return freeMemory; }