/*--------------------------------------------------------------------------- Captures the contents of the frame buffer object for streaming purposes. Returns true if the frame was successfully captured. If the Wait flag is set, the function with use a blocking mutex lock on the Frame texture. ---------------------------------------------------------------------------*/ bool Filter::Capture(Texture &Frame, bool Wait) { if (!Ready()) {return false;} MutexControl Mutex(Frame.GetMutexHandle()); if (!Wait && !Mutex.LockRequest()) {return false;} else {Mutex.Lock();} vector2u Res = Frame.Resolution(); if (Res.U != ViewPort.C2 || Res.V != ViewPort.C3 || Frame.DataType() != Texture::TypeRGB) { Res.Set(ViewPort.C2, ViewPort.C3); Frame.Create(Res, Texture::TypeRGB); } glBindFramebuffer(GL_READ_FRAMEBUFFER, FBOID); glReadPixels(0, 0, Res.U, Res.V, Frame.DataFormat(), Frame.DataCompType(), Frame.Pointer()); glBindFramebuffer(GL_READ_FRAMEBUFFER, 0); #if defined (DEBUG) GLenum Error = glGetError(); if (Error != GL_NO_ERROR) {throw dexception("OpenGL generated an error: %s", Debug::ErrorGL(Error));} #endif return true; }
Texture* SolidColorTextureCache::CreateFor(const Color &color, Texture *existing) { LOG_INFO(LOGCAT_ASSETS, "SolidColorTextureCache: creating texture for color 0x%8x.\n", color.ToInt()); Image *img = new Image(); bool imageCreateSuccess = img->Create(8, 8, IMAGE_FORMAT_RGBA); ASSERT(imageCreateSuccess == true); Texture *texture; if (existing != NULL) { texture = existing; texture->Release(); } else texture = new Texture(); img->Clear(color); bool success = texture->Create(m_graphicsDevice, img); SAFE_DELETE(img); if (!success) { // if we allocated the texture ourselves, we should free it if (existing != NULL) { SAFE_DELETE(texture); } } return texture; }
Texture* Texture::Create3D( const Texture::Properties& properties, const void* ptr) { Texture* texture = VD_NEW(Texture); bool success = texture->Create(properties, ptr); if(!success) { vdLogGlobalWarning("Texture: Error Creating Texture!"); VD_DELETE(texture); return NULL; } return texture; }
Resource* BmpALoader(const std::string& resName) { std::string img = resName.substr(0, resName.size() - 1); std::string alpha = GetFileNoExt(img) + "-a.bmp"; unsigned int w = 0; unsigned int h = 0; unsigned char* rgbdata = LoadDIBitmap(img.c_str(), &w, &h); if (!rgbdata) { //? ReportError("Failed to load texture " + img); return 0; } // Add space for alpha unsigned char* withAlpha = AddAlpha(rgbdata, w, h); Assert(withAlpha); delete [] rgbdata; rgbdata = 0; // Load alpha image unsigned int aw = 0; unsigned int ah = 0; unsigned char* alphadata = LoadDIBitmap(alpha.c_str(), &aw, &ah); Assert(alphadata); Assert(aw == w); Assert(ah == h); // Copy alpha image to alpha channel of RGB image CopyAlpha(alphadata, withAlpha, w, h); delete [] alphadata; alphadata = 0; Texture* pTex = new Texture; pTex->Create(withAlpha, w, h, 4); // 4 bytes per pixel delete [] withAlpha; return (Resource*)pTex; }
void App::init() { pause = true; m_timer = 1000; printf("Loading..."); m_MatProj3D = glm::perspective(35.0f, (float)getScreen().x / (float)getScreen().y, 0.01f, 100.f); m_MatProj2D = ortho(0.0f,(float)getScreen().x,0.0f,(float)getScreen().y); drawMouse = false; draw = true; pause = false; DeferredRendering* mode = new DeferredRendering(getScreen()); //RenderingMode::SetRenderingMode(mode); tex[0].Create(getScreen(),PXF_A8R8G8B8,TEXTURE_2D); tex[1].Create(getScreen(),PXF_A8R8G8B8,TEXTURE_2D); m_fxaa = new AntiAliasing(); blur = new BlurMotionEffect(getScreen()); //drug = new DrugEffect(); //PostEffectMgr::Instance().AddEffect(drug); //PostEffectMgr::Instance().AddEffect(new BlurMotionEffect(getScreen())); //PostEffectMgr::Instance().AddEffect(m_fxaa); //PostEffectMgr::Instance().AddEffect(new Inverse()); m_fps = new GraphicString(ivec2(0,getScreen().y-15),"",Color::black); m_Scene = new SceneMgr(); Texture t; Texture color_gradiant; Material* mat = new Material(); mat->SetTexture(t,0,(TRenderPass)((1<<RENDERPASS_DEFERRED) | (1<<RENDERPASS_ZBUFFER))); mat->SetTexture(color_gradiant,1,(TRenderPass)(1<<RENDERPASS_DEFERRED)); Driver::Get().SetActiveScene(m_Scene); Driver::Get().SetCullFace(2); m_light = new Light(vec3(0, 0 ,10),-normalize(vec3(0,0.2,-1)),LightType::LIGHT_DIR);//new Light(vec3(0,0,10),-normalize(vec3(0,0.5,-1)),LIGHT_SPOT); m_Scene->AddLight(m_light); m_light->SetRange(2000.0f); ShaderProgram prog; ShaderProgram prog2; /*particles1 = new ParticlesEmitter("Shader/particle_1.glsl",250,new Transform(vec3(-6,0,0))); particles2 = new ParticlesEmitter("Shader/particle_2.glsl",250,new Transform(vec3(6,0,0)));*/ string cubemap[6]; /*for(int i = 0; i < 6; i++) { cubemap[i] = StringBuilder("Texture/background/")(i+1)(".png"); }*/ //Texture tex_cubemap; //tex_cubemap.CreateFromFile(cubemap,PXF_A8R8G8B8); mouse_emitter = new ParticlesEmitter(std::string("shader/particle_3.glsl"),2,new Transform()); AWindow* position =new AWindow(); AWindow* velocity = new AWindow(); AWindow* life = new AWindow(); //particles->velocity_buffer[1]); //life->SetBackground(mouse_emitter->extra_buffer[0]); //GUIMgr::Instance().AddWidget(position); //GUIMgr::Instance().AddWidget(velocity); // GUIMgr::Instance().AddWidget(life); SkyBox* box = new SkyBox(); //box->SetTexture(tex_cubemap); cam3D = new FollowCamera(m_MatProj3D,0,0,vec2(-65.7063446,0),10.f);//m_MatProj3D,4.8f,8.8f,vec2(0,-7.55264f),9.87785f); //Follow Camera Theta(4.8) _phi(8.8) angles(0,-7.55264) distance(9.87785) cam2D = new FPCamera(m_MatProj2D); velocity_program.LoadFromFile("Shader/particle_velocity_render.glsl"); mass_program.LoadFromFile("Shader/particle_mass_render.glsl"); tmass.Create(getScreen(),PXF_A8R8G8B8,TEXTURE_2D); tvelocity.Create(getScreen(),PXF_A8R8G8B8,TEXTURE_2D); position->SetBackground(tmass); velocity->SetBackground(tvelocity); Camera::setCurrent(cam3D, CAMERA_3D); Camera::setCurrent(cam2D, CAMERA_2D); m_particles.push_back(new ParticlesEmitter(std::string("shader/particle_4.glsl"),2500 ,new Transform(vec3((0),0.f,0)*30.f))); printf("Loading end"); }
/*--------------------------------------------------------------------------- Loads a PNG file. Image : Image will be returned there. Path : Path to image file, relative to the current working directory. Gamma : Display gamma to apply on textures, 2.2 is typical. ---------------------------------------------------------------------------*/ void PNG::Load(Texture &Image, const std::string &Path, float Gamma) { Destroy(); //Important - set class to reading mode Mode = PNG::ModeRead; #if defined (WINDOWS) if (fopen_s(&File, Path.c_str(), "rb") != 0) {throw dexception("Failed to open file: %s.", Path.c_str());} #else File = fopen(Path.c_str(), "rb"); if (File == nullptr) {throw dexception("Failed to open file: %s.", Path.c_str());} #endif uint8 Header[PNG::HeaderSize]; if (fread((png_bytep)Header, 1, PNG::HeaderSize, File) == 0) {throw dexception("fread( ) failed.");} if (!png_check_sig(Header, PNG::HeaderSize)) {throw dexception("Not a valid PNG file.");} PngPtr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); if (PngPtr == nullptr) {throw dexception("png_create_read_struct( ) failed.");} InfoPtr = png_create_info_struct(PngPtr); if (InfoPtr == nullptr) {throw dexception("png_create_info_struct( ) failed.");} if (setjmp(png_jmpbuf(PngPtr))) {throw dexception("setjmp( ) failed.");} //Read into structures png_init_io(PngPtr, File); png_set_sig_bytes(PngPtr, PNG::HeaderSize); png_read_info(PngPtr, InfoPtr); //Determine image attributes vector2<png_uint_32> Res; int BitDepth, ColourType; png_get_IHDR(PngPtr, InfoPtr, &Res.U, &Res.V, &BitDepth, &ColourType, nullptr, nullptr, nullptr); Texture::TexType Type = Texture::TypeRGB; switch (ColourType) { case PNG_COLOR_TYPE_GRAY : if (BitDepth < 8) { Type = Texture::TypeLum; png_set_expand_gray_1_2_4_to_8(PngPtr); } else if (BitDepth == 16) { Type = Texture::TypeDepth; } break; case PNG_COLOR_TYPE_GRAY_ALPHA : Type = Texture::TypeRGBA; png_set_gray_to_rgb(PngPtr); break; case PNG_COLOR_TYPE_PALETTE : Type = Texture::TypeRGB; png_set_palette_to_rgb(PngPtr); break; case PNG_COLOR_TYPE_RGB : Type = Texture::TypeRGB; png_set_strip_alpha(PngPtr); //If libpng reports RGB, make sure we completely strip alpha break; case PNG_COLOR_TYPE_RGB_ALPHA : Type = Texture::TypeRGBA; break; default : throw dexception("Unknown colour type."); } //Convert palette alpha into a separate alpha channel if (png_get_valid(PngPtr, InfoPtr, PNG_INFO_tRNS)) { png_set_tRNS_to_alpha(PngPtr); if (ColourType == PNG_COLOR_TYPE_PALETTE) {Type = Texture::TypeRGBA;} } //Force 8 bit per channel if (BitDepth >= 16) { if (Type != Texture::TypeDepth) {png_set_strip_16(PngPtr);} } else if (BitDepth < 8) { png_set_packing(PngPtr); } //Specify display and file gamma (assume 0.45455 for missing file gamma) double FileGamma; if (!png_get_gAMA(PngPtr, InfoPtr, &FileGamma)) {FileGamma = 0.45455;} png_set_gamma(PngPtr, Gamma, FileGamma); //Swap byte order for int16 values on big endian machines if ((BitDepth >= 16) && !Math::MachineLittleEndian()) {png_set_swap(PngPtr);} //Apply the above transformation settings png_read_update_info(PngPtr, InfoPtr); //Allocate image Image.Create(cast_vector2(uint, Res), Type); if (Image.GetBytesPerLine() < (usize)png_get_rowbytes(PngPtr, InfoPtr)) {throw dexception("Incorrect scan line size for allocated image.");} //Populate row pointer array Rows.Create((usize)Res.V); for (uiter I = 0; I < (usize)Res.V; I++) { Rows[I] = Image.Address(0, I); } //Decode png_read_image(PngPtr, Rows.Pointer()); png_read_end(PngPtr, InfoPtr); //Clean up Destroy(); }