Back to index

nux  3.0.0
texture_copy_blur.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Inalogic Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU General Public License version 3, as published
00006  * by the  Free Software Foundation.
00007  *
00008  * This program is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranties of
00010  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00011  * PURPOSE.  See the GNU General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * version 3 along with this program.  If not, see
00015  * <http://www.gnu.org/licenses/>
00016  *
00017  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00018  *
00019  */
00020 
00021 
00022 #include "NuxCore/NuxCore.h"
00023 #include "NuxGraphics/BitmapFormats.h"
00024 #include "NuxGraphics/GraphicsDisplay.h"
00025 #include "NuxGraphics/GLWindowManager.h"
00026 #include "NuxGraphics/GraphicsEngine.h"
00027 
00028 /*
00029  * Tests: 
00030  *  - Frame buffer object
00031  *  - Set a texture in the fbo
00032  *  - Set fbo as a render target
00033  *  - Render Quad to fbo
00034  *  - Make a copy of the render target: CreateTextureFromBackBuffer
00035  *  - Deactivate fbo
00036  *  - Blur the copied render target texture
00037  *  - Render to default back buffer
00038  */
00039 
00040 void RenderBlurredCopyOfRenderTarget ()
00041 {
00042   nux::GraphicsDisplay* graphics_display = gGLWindowManager.CreateGLWindow("Window", 600, 300, nux::WINDOWSTYLE_NORMAL, 0, false);
00043   nux::GraphicsEngine* graphics_engine = graphics_display->GetGraphicsEngine();
00044 
00045   graphics_display->ShowWindow();
00046 
00047   nux::ObjectPtr<nux::IOpenGLFrameBufferObject> fbo;
00048   nux::ObjectPtr<nux::IOpenGLBaseTexture> texture_rt;
00049   nux::ObjectPtr<nux::IOpenGLBaseTexture> depth_rt;
00050 
00051   fbo         = graphics_display->GetGpuDevice ()->CreateFrameBufferObject ();
00052   texture_rt  = graphics_display->GetGpuDevice ()->CreateSystemCapableDeviceTexture (graphics_display->GetWindowWidth(), graphics_display->GetWindowHeight(), 1, nux::BITFMT_R8G8B8A8);
00053   depth_rt    = graphics_display->GetGpuDevice ()->CreateSystemCapableDeviceTexture (graphics_display->GetWindowWidth(), graphics_display->GetWindowHeight(), 1, nux::BITFMT_D24S8);
00054 
00055 
00056   int w, h;
00057   graphics_engine->GetWindowSize(w, h);
00058   graphics_engine->SetViewport(0, 0, w, h);
00059   graphics_engine->SetContext(0, 0, w, h);
00060   graphics_engine->Push2DWindow(w, h);
00061 
00062   nux::Event event;
00063   memset(&event, 0, sizeof(nux::Event));
00064 
00065   char fps [25];
00066   int frame_counter = 0;
00067   int frame_periode = 0;
00068   float frame_rate = 0;
00069   float periode_time = 0;
00070   bool first_time = true;
00071   do
00072   {
00073     CHECKGL( glClearColor(0, 0, 0, 1) );
00074     CHECKGL( glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT) );
00075 
00076     graphics_display->GetSystemEvent(&event);
00077     if(first_time || (event.type == nux::NUX_SIZE_CONFIGURATION))
00078     {
00079       first_time = false;
00080       graphics_engine->DisableAllTextureMode(0);
00081       graphics_engine->DisableAllTextureMode(1);
00082       graphics_engine->DisableAllTextureMode(2);
00083       graphics_engine->DisableAllTextureMode(3);
00084       graphics_engine->GetWindowSize(w, h);
00085       graphics_engine->SetViewport(0, 0, w, h);
00086       graphics_engine->SetScissor(0, 0, w, h);
00087       graphics_engine->SetContext(0, 0, w, h);
00088       graphics_engine->Push2DWindow(w, h);
00089 
00090       fbo         = graphics_display->GetGpuDevice ()->CreateFrameBufferObject ();
00091       texture_rt  = graphics_display->GetGpuDevice ()->CreateSystemCapableDeviceTexture (graphics_display->GetWindowWidth(), graphics_display->GetWindowHeight(), 1, nux::BITFMT_R8G8B8A8);
00092       depth_rt    = graphics_display->GetGpuDevice ()->CreateSystemCapableDeviceTexture (graphics_display->GetWindowWidth(), graphics_display->GetWindowHeight(), 1, nux::BITFMT_D24S8);
00093       fbo->FormatFrameBufferObject (graphics_display->GetWindowWidth(), graphics_display->GetWindowHeight(), nux::BITFMT_R8G8B8A8);
00094     }
00095 
00096     fbo->SetRenderTarget (0, texture_rt->GetSurfaceLevel (0));
00097     fbo->SetDepthSurface (depth_rt->GetSurfaceLevel (0));
00098     fbo->Activate();
00099 
00100     for (int i = 0; i < 1; i++)
00101     {
00102       nux::Rect geo (nux::RandomUInt(graphics_display->GetWindowWidth()),
00103         nux::RandomUInt(graphics_display->GetWindowHeight()),
00104         nux::RandomUInt(200),
00105         nux::RandomUInt(200));
00106 
00107       graphics_engine->QRP_Color(geo.x, geo.y, geo.width, geo.height, nux::color::RandomColor());
00108     }
00109 
00110     nux::TexCoordXForm texxform;
00111     // Make a copy of the render target
00112     nux::ObjectPtr <nux::IOpenGLBaseTexture> tex_copy = graphics_engine->CreateTextureFromBackBuffer (0, 0, graphics_display->GetWindowWidth (), graphics_display->GetWindowHeight ());
00113 
00114     // Restore the back buffer
00115     graphics_display->GetGpuDevice ()->DeactivateFrameBuffer ();
00116 
00117     // Make a blurred version of the back buffer
00118     nux::ObjectPtr <nux::IOpenGLBaseTexture> tex_blur = graphics_engine->QRP_GetBlurTexture (
00119       0, 0, tex_copy->GetWidth (), tex_copy->GetHeight (),
00120       tex_copy, texxform, nux::color::White, 1.0f);
00121 
00122     // Render the blurred texture
00123     graphics_engine->QRP_1Tex(0, 0, tex_blur->GetWidth(), tex_blur->GetHeight(), tex_blur, texxform, nux::color::White);
00124 
00125     sprintf(fps, "FPS: %3.2f", frame_rate);
00126     nux::PageBBox page;
00127     page.xmin = 0;
00128     page.xmax = 100;
00129     page.ymin = 0;
00130     page.ymax = 20;
00131     page.x_margin = 0;
00132     page.y_margin = 0;
00133     graphics_engine->RenderColorTextLineStatic(graphics_engine->GetBoldFont (), page, fps, nux::color::White, false, nux::eAlignTextLeft);
00134 
00135     graphics_display->SwapBuffer();
00136 
00137     float frame_time = graphics_display->GetFrameTime();
00138     graphics_display->ResetFrameTime();
00139     periode_time += frame_time;
00140 
00141     frame_counter++;
00142     frame_periode++;
00143 
00144     if (frame_periode >= 100)
00145     {
00146       frame_rate = frame_periode * 1000.0f / periode_time;
00147       periode_time = 0.0f;
00148       frame_periode = 0;
00149     }
00150 
00151   } while((event.type != nux::NUX_TERMINATE_APP) && (event.GetVirtualKeyState(NUX_VK_ESCAPE) == 0));
00152 
00153   fbo.Release ();
00154   texture_rt.Release ();
00155   depth_rt.Release ();
00156 
00157   delete graphics_display;
00158 }
00159 
00160 int main(int argc, char **argv)
00161 {
00162   nux::NuxCoreInitialize(0);
00163   nux::NuxGraphicsInitialize();
00164 
00165   RenderBlurredCopyOfRenderTarget ();
00166 
00167   return 0;
00168 }
00169