Example #1
0
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, PSTR cmdLine, int showCmd){

	//Enable run-time memory check for debug builds.
#if defined (DEBUG) | defined (_DEBUG)
	_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif

	CubeMap cubemap(hInstance);

	if(!cubemap.Init())
		return 0;

	return cubemap.Run();
}
SPtr<Texture> TextureAssetManager::loadCubemap(const std::string &path, const std::string &extension) {
   if (cubemapMap.count(path)) {
      return cubemapMap.at(path);
   }

   const std::string &rightName = "/right." + extension;
   const std::string &leftName = "/left." + extension;
   const std::string &upName = "/up." + extension;
   const std::string &downName = "/down." + extension;
   const std::string &backName = "/back." + extension;
   const std::string &frontName = "/front." + extension;

   ImageInfo right = loadImage(path + rightName);
   ImageInfo left = loadImage(path + leftName);
   ImageInfo up = loadImage(path + upName);
   ImageInfo down = loadImage(path + downName);
   ImageInfo back = loadImage(path + backName);
   ImageInfo front = loadImage(path + frontName);

   SPtr<Texture> cubemap(std::make_shared<Texture>(GL_TEXTURE_CUBE_MAP));
   cubemap->bind();

   glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGB, right.width, right.height, 0, GL_RGB, GL_UNSIGNED_BYTE, right.pixels);
   glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGB, left.width, left.height, 0, GL_RGB, GL_UNSIGNED_BYTE, left.pixels);
   glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_RGB, up.width, up.height, 0, GL_RGB, GL_UNSIGNED_BYTE, up.pixels);
   glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGB, down.width, down.height, 0, GL_RGB, GL_UNSIGNED_BYTE, down.pixels);
   glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL_RGB, back.width, back.height, 0, GL_RGB, GL_UNSIGNED_BYTE, back.pixels);
   glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL_RGB, front.width, front.height, 0, GL_RGB, GL_UNSIGNED_BYTE, front.pixels);

   glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
   glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
   glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);

   cubemap->unbind();

   stbi_image_free(right.pixels);
   stbi_image_free(left.pixels);
   stbi_image_free(up.pixels);
   stbi_image_free(down.pixels);
   stbi_image_free(back.pixels);
   stbi_image_free(front.pixels);

   cubemapMap[path] = cubemap;
   return cubemap;
}
RES ResourceFormatLoaderImage::load(const String &p_path,const String& p_original_path) {
	
	
	if (p_path.extension()=="cube") {
		// open as cubemap txture

		CubeMap* ptr = memnew(CubeMap);
		Ref<CubeMap> cubemap( ptr );

		Error err;
		FileAccess *f = FileAccess::open(p_path,FileAccess::READ,&err);
		if (err) {
		
			ERR_FAIL_COND_V( err, RES() );
		}
		
		String base_path=p_path.substr( 0, p_path.find_last("/")+1 );

		for(int i=0;i<6;i++) {
		
			String file = f->get_line().strip_edges();
			Image image;
			
			Error err = ImageLoader::load_image(base_path+file,&image);

			
			if (err) {
			
				memdelete(f);
				ERR_FAIL_COND_V( err, RES() );
			}
			
			if (i==0) {
			
				//cubemap->create(image.get_width(),image.get_height(),image.get_format(),Texture::FLAGS_DEFAULT|Texture::FLAG_CUBEMAP);
			}
			
			static const CubeMap::Side cube_side[6]= {
				CubeMap::SIDE_LEFT,
				CubeMap::SIDE_RIGHT,
				CubeMap::SIDE_BOTTOM,
				CubeMap::SIDE_TOP,
				CubeMap::SIDE_FRONT,
				CubeMap::SIDE_BACK
			};
			
			cubemap->set_side(cube_side[i],image);
		}
		
		memdelete(f);

		cubemap->set_name(p_path.get_file());

		return cubemap;
	
	} else {
		// simple image	

		ImageTexture* ptr = memnew(ImageTexture);
		Ref<ImageTexture> texture( ptr );

		uint64_t begtime;
		double total;

		Image image;

		if (debug_load_times)
			begtime=OS::get_singleton()->get_ticks_usec();


		Error err = ImageLoader::load_image(p_path,&image);

		if (!err && debug_load_times) {
			double total=(double)(OS::get_singleton()->get_ticks_usec()-begtime)/1000000.0;
			print_line("IMAGE: "+itos(image.get_width())+"x"+itos(image.get_height()));
			print_line("  -load: "+rtos(total));
		}


		ERR_EXPLAIN("Failed loading image: "+p_path);
		ERR_FAIL_COND_V(err, RES());		

#ifdef DEBUG_ENABLED
#ifdef TOOLS_ENABLED

		if (max_texture_size && (image.get_width() > max_texture_size || image.get_height() > max_texture_size)) {


			if (bool(Globals::get_singleton()->get("debug/max_texture_size_alert"))) {
				OS::get_singleton()->alert("Texture is too large: '"+p_path+"', at "+itos(image.get_width())+"x"+itos(image.get_height())+". Max allowed size is: "+itos(max_texture_size)+"x"+itos(max_texture_size)+".","BAD ARTIST, NO COOKIE!");
			}

			ERR_EXPLAIN("Texture is too large: '"+p_path+"', at "+itos(image.get_width())+"x"+itos(image.get_height())+". Max allowed size is: "+itos(max_texture_size)+"x"+itos(max_texture_size)+".");
			ERR_FAIL_V(RES());
		}
#endif
#endif
		
		
		uint32_t flags=0;
		if (bool(GLOBAL_DEF("texture_import/filter",true)))
			flags|=Texture::FLAG_FILTER;
		if (bool(GLOBAL_DEF("texture_import/gen_mipmaps",true)))
			flags|=Texture::FLAG_MIPMAPS;
		if (bool(GLOBAL_DEF("texture_import/repeat",true)))
			flags|=Texture::FLAG_REPEAT;



		if (debug_load_times)
			begtime=OS::get_singleton()->get_ticks_usec();

		//print_line("img: "+p_path+" flags: "+itos(flags));
		texture->create_from_image( image,flags );
		texture->set_name(p_path.get_file());


		if (debug_load_times) {
			total=(double)(OS::get_singleton()->get_ticks_usec()-begtime)/1000000.0;
			print_line("  -make texture: "+rtos(total));
		}

		return RES( texture );
	}
	

}
Example #4
0
RES ResourceFormatLoaderImage::load(const String &p_path, const String& p_original_path, Error *r_error) {
	
	if (r_error)
		*r_error=ERR_CANT_OPEN;

	if (p_path.extension()=="cube") {
		// open as cubemap txture

		CubeMap* ptr = memnew(CubeMap);
		Ref<CubeMap> cubemap( ptr );

		Error err;
		FileAccess *f = FileAccess::open(p_path,FileAccess::READ,&err);
		if (err) {
		
			ERR_FAIL_COND_V( err, RES() );
		}
		
		String base_path=p_path.substr( 0, p_path.find_last("/")+1 );

		for(int i=0;i<6;i++) {
		
			String file = f->get_line().strip_edges();
			Image image;
			
			Error err = ImageLoader::load_image(base_path+file,&image);

			
			if (err) {
			
				memdelete(f);
				ERR_FAIL_COND_V( err, RES() );
			}
			
			if (i==0) {
			
				//cubemap->create(image.get_width(),image.get_height(),image.get_format(),Texture::FLAGS_DEFAULT|Texture::FLAG_CUBEMAP);
			}
			
			static const CubeMap::Side cube_side[6]= {
				CubeMap::SIDE_LEFT,
				CubeMap::SIDE_RIGHT,
				CubeMap::SIDE_BOTTOM,
				CubeMap::SIDE_TOP,
				CubeMap::SIDE_FRONT,
				CubeMap::SIDE_BACK
			};
			
			cubemap->set_side(cube_side[i],image);
		}
		
		memdelete(f);

		cubemap->set_name(p_path.get_file());
		if (r_error)
			*r_error=OK;

		return cubemap;
	
	} else {
		// simple image	

		ImageTexture* ptr = memnew(ImageTexture);
		Ref<ImageTexture> texture( ptr );

		uint64_t begtime;
		double total;

		Image image;

		if (debug_load_times)
			begtime=OS::get_singleton()->get_ticks_usec();


		Error err = ImageLoader::load_image(p_path,&image);

		if (!err && debug_load_times) {
			double total=(double)(OS::get_singleton()->get_ticks_usec()-begtime)/1000000.0;
			print_line("IMAGE: "+itos(image.get_width())+"x"+itos(image.get_height()));
			print_line("  -load: "+rtos(total));
		}


		ERR_EXPLAIN("Failed loading image: "+p_path);
		ERR_FAIL_COND_V(err, RES());		
		if (r_error)
			*r_error=ERR_FILE_CORRUPT;

#ifdef DEBUG_ENABLED
#ifdef TOOLS_ENABLED

		if (max_texture_size && (image.get_width() > max_texture_size || image.get_height() > max_texture_size)) {


			if (bool(Globals::get_singleton()->get("debug/max_texture_size_alert"))) {
				OS::get_singleton()->alert("Texture is too large: '"+p_path+"', at "+itos(image.get_width())+"x"+itos(image.get_height())+". Max allowed size is: "+itos(max_texture_size)+"x"+itos(max_texture_size)+".","BAD ARTIST, NO COOKIE!");
			}

			ERR_EXPLAIN("Texture is too large: '"+p_path+"', at "+itos(image.get_width())+"x"+itos(image.get_height())+". Max allowed size is: "+itos(max_texture_size)+"x"+itos(max_texture_size)+".");
			ERR_FAIL_V(RES());
		}
#endif
#endif
		
		
		uint32_t flags=0;

		FileAccess *f2 = FileAccess::open(p_path+".flags",FileAccess::READ);
		Map<String,bool> flags_found;
		if (f2) {

			while(!f2->eof_reached()) {
				String l2 = f2->get_line();
				int eqpos = l2.find("=");
				if (eqpos!=-1) {
					String flag=l2.substr(0,eqpos).strip_edges();
					String val=l2.substr(eqpos+1,l2.length()).strip_edges().to_lower();
					flags_found[flag]=(val=="true" || val=="1")?true:false;
				}
			}
			memdelete(f2);
		}


		if (flags_found.has("filter")) {
			if (flags_found["filter"])
				flags|=Texture::FLAG_FILTER;
		} else if (bool(GLOBAL_DEF("image_loader/filter",true))) {
			flags|=Texture::FLAG_FILTER;
		}


		if (flags_found.has("gen_mipmaps")) {
			if (flags_found["gen_mipmaps"])
				flags|=Texture::FLAG_MIPMAPS;
		} else if (bool(GLOBAL_DEF("image_loader/gen_mipmaps",true))) {
			flags|=Texture::FLAG_MIPMAPS;
		}

		if (flags_found.has("repeat")) {
			if (flags_found["repeat"])
				flags|=Texture::FLAG_REPEAT;
		} else if (bool(GLOBAL_DEF("image_loader/repeat",true))) {
			flags|=Texture::FLAG_REPEAT;
		}

		if (flags_found.has("anisotropic")) {
			if (flags_found["anisotropic"])
				flags|=Texture::FLAG_ANISOTROPIC_FILTER;
		}

		if (flags_found.has("tolinear")) {
			if (flags_found["tolinear"])
				flags|=Texture::FLAG_CONVERT_TO_LINEAR;
		}
		
		if (flags_found.has("mirroredrepeat")) {
			if (flags_found["mirroredrepeat"])
				flags|=Texture::FLAG_MIRRORED_REPEAT;
		}

		if (debug_load_times)
			begtime=OS::get_singleton()->get_ticks_usec();

		//print_line("img: "+p_path+" flags: "+itos(flags));
		texture->create_from_image( image,flags );
		texture->set_name(p_path.get_file());


		if (debug_load_times) {
			total=(double)(OS::get_singleton()->get_ticks_usec()-begtime)/1000000.0;
			print_line("  -make texture: "+rtos(total));
		}

		if (r_error)
			*r_error=OK;

		return RES( texture );
	}
	

}