Beispiel #1
0
Datei: map.cpp Projekt: psde/GBH
Map::Map(const char *map, Style* style){
	this->style = style;

	Filereader* reader = new Filereader(map);

	Chunk *animation_data;
	Chunk *dmap_data;

	Chunk *chk;
	while((chk = reader->getNextChunk())){
		if(strncmp(chk->header->type,"ANIM",4) == 0) {
			animation_data = chk;
		}
		if(strncmp(chk->header->type,"DMAP",4) == 0) {
			dmap_data = chk;
		}
	}

	// Read animation data
	char* starting = animation_data->data;
	char* offset = animation_data->data;
	while((int)offset - (int)starting < animation_data->header->size )
	{
		TileAnimation anim;

		anim.base = *reinterpret_cast<short*>(offset);
		offset += sizeof(short);

		anim.frame_rate = *reinterpret_cast<char*>(offset);
		offset += sizeof(char);

		anim.repeat = *reinterpret_cast<unsigned char*>(offset);
		offset += sizeof(unsigned char);

		anim.anim_length = *reinterpret_cast<char*>(offset);
		offset += sizeof(char);

		// unused char in file structure
		offset += sizeof(char);

		anim.tiles = reinterpret_cast<short*>(offset);
		offset += anim.anim_length * sizeof(short);

		std::vector<int> animationTiles;

		for(int i=0; i<anim.anim_length;i++)
		{
			animationTiles.push_back(anim.tiles[i]);
		}

		animatedGeom[anim.base].curTile = 0;
		animatedGeom[anim.base].tick = 0;
		animatedGeom[anim.base].anim = anim;
		animatedGeom[anim.base].animationTiles = animationTiles;

		animatedGeom[anim.base+1000].curTile = 0;
		animatedGeom[anim.base+1000].tick = 0;
		animatedGeom[anim.base+1000].anim = anim;
		animatedGeom[anim.base+1000].animationTiles = animationTiles;

		tileAnimations.push_back(anim);

		std::cout << anim.base << " " << (int)anim.frame_rate << " " << (int)anim.repeat << " " << (int)anim.anim_length << " " << anim.tiles[1] << " " << animationTiles.size() << std::endl;
	}

	// Read dmap data
	CompressedMap c_map;
	offset = dmap_data->data;
	c_map.base = reinterpret_cast<int*>(offset);
	offset += 256 * 256 * sizeof(int);

	c_map.column_words = *reinterpret_cast<int*>(offset);
	offset += sizeof(int);

	c_map.columns = reinterpret_cast<int*>(offset);
	offset += c_map.column_words * sizeof(int);

	c_map.num_blocks = *reinterpret_cast<int*>(offset);
	offset += sizeof(int);

	c_map.blocks = reinterpret_cast<BlockInfo*>(offset);
	offset += c_map.num_blocks * sizeof(BlockInfo);

	for(int x=0;x<255;x++) for(int y=0;y<255;y++) 
	{
		int base = c_map.base[y*256+x];
		

		ColInfo* column = reinterpret_cast<ColInfo*>(c_map.columns + base);

		int i = 0;

		for(i=0;i<(column->height-column->offset);i++){
			this->addBlock(c_map.blocks[column->blockd[i]], Vector3(x, -y, i+column->offset));
		}
	}
	delete reader;

	GLenum FBOstatus;
	
	glGenTextures(1, &depthTextureId);
	glBindTexture(GL_TEXTURE_2D, depthTextureId);
	
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
	
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
	glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY); 
	
	glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, shadowMapWidth, shadowMapHeight, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0);
	glBindTexture(GL_TEXTURE_2D, 0);
	
	glGenFramebuffers(1, &fboId);
	glBindFramebuffer(GL_FRAMEBUFFER, fboId);
	
	glDrawBuffer(GL_NONE);
	glReadBuffer(GL_NONE);
	
	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTextureId, 0);
	
	FBOstatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
	if(FBOstatus != GL_FRAMEBUFFER_COMPLETE)
	{
		std::cout << "FBO error: " << FBOstatus << std::endl;
	}
	glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
}