Back to index

nux  3.0.0
texture_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  *  - Deactivate fbo
00035  *  - Blur the render target texture
00036  *  - Render to default back buffer
00037  */
00038  
00039 void RenderBlurredTexture ()
00040 {
00041   nux::GraphicsDisplay* graphics_display = gGLWindowManager.CreateGLWindow("Window", 600, 300, nux::WINDOWSTYLE_NORMAL, 0, false);
00042   nux::GraphicsEngine* graphics_engine = graphics_display->GetGraphicsEngine();
00043 
00044   graphics_display->ShowWindow();
00045 
00046   nux::ObjectPtr<nux::IOpenGLFrameBufferObject> fbo;
00047   nux::ObjectPtr<nux::IOpenGLBaseTexture> texture_rt;
00048   nux::ObjectPtr<nux::IOpenGLBaseTexture> depth_rt;
00049 
00050   fbo         = graphics_display->GetGpuDevice ()->CreateFrameBufferObject ();
00051   texture_rt  = graphics_display->GetGpuDevice ()->CreateSystemCapableDeviceTexture (graphics_display->GetWindowWidth(), graphics_display->GetWindowHeight(), 1, nux::BITFMT_R8G8B8A8);
00052   depth_rt    = graphics_display->GetGpuDevice ()->CreateSystemCapableDeviceTexture (graphics_display->GetWindowWidth(), graphics_display->GetWindowHeight(), 1, nux::BITFMT_D24S8);
00053 
00054 
00055   int w, h;
00056   graphics_engine->GetWindowSize(w, h);
00057   graphics_engine->SetViewport(0, 0, w, h);
00058   graphics_engine->SetContext(0, 0, w, h);
00059   graphics_engine->Push2DWindow(w, h);
00060 
00061   nux::Event event;
00062   memset(&event, 0, sizeof(nux::Event));
00063   
00064   char fps [25];
00065   int frame_counter = 0;
00066   int frame_periode = 0;
00067   float frame_rate = 0;
00068   float periode_time = 0;
00069   bool first_time = true;
00070   do
00071   {
00072     CHECKGL( glClearColor(0, 0, 0, 1) );
00073     CHECKGL( glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT) );
00074 
00075     graphics_display->GetSystemEvent(&event);
00076     if(first_time || (event.type == nux::NUX_SIZE_CONFIGURATION))
00077     {
00078       first_time = false;
00079       graphics_engine->DisableAllTextureMode(0);
00080       graphics_engine->DisableAllTextureMode(1);
00081       graphics_engine->DisableAllTextureMode(2);
00082       graphics_engine->DisableAllTextureMode(3);
00083       graphics_engine->GetWindowSize(w, h);
00084       graphics_engine->SetViewport(0, 0, w, h);
00085       graphics_engine->SetScissor(0, 0, w, h);
00086       graphics_engine->SetContext(0, 0, w, h);
00087       graphics_engine->Push2DWindow(w, h);
00088 
00089       fbo         = graphics_display->GetGpuDevice ()->CreateFrameBufferObject ();
00090       texture_rt  = graphics_display->GetGpuDevice ()->CreateSystemCapableDeviceTexture (graphics_display->GetWindowWidth(), graphics_display->GetWindowHeight(), 1, nux::BITFMT_R8G8B8A8);
00091       depth_rt    = graphics_display->GetGpuDevice ()->CreateSystemCapableDeviceTexture (graphics_display->GetWindowWidth(), graphics_display->GetWindowHeight(), 1, nux::BITFMT_D24S8);
00092       fbo->FormatFrameBufferObject (graphics_display->GetWindowWidth(), graphics_display->GetWindowHeight(), nux::BITFMT_R8G8B8A8);
00093     }
00094 
00095     fbo->SetRenderTarget (0, texture_rt->GetSurfaceLevel (0));
00096     fbo->SetDepthSurface (depth_rt->GetSurfaceLevel (0));
00097     fbo->Activate();
00098     for (int i = 0; i < 1; i++)
00099     {
00100       nux::Rect geo (nux::RandomUInt(graphics_display->GetWindowWidth()),
00101         nux::RandomUInt(graphics_display->GetWindowHeight()),
00102         nux::RandomUInt(200),
00103         nux::RandomUInt(200));
00104 
00105       graphics_engine->QRP_Color(geo.x, geo.y, geo.width, geo.height, nux::color::RandomColor());
00106     }
00107 
00108     // Restore the back buffer
00109     graphics_display->GetGpuDevice ()->DeactivateFrameBuffer ();
00110 
00111     nux::TexCoordXForm texxform;
00112     // Make a blurred copy of the previous render target
00113     nux::ObjectPtr <nux::IOpenGLBaseTexture> tex_blur = graphics_engine->QRP_GetBlurTexture (
00114       0, 0, texture_rt->GetWidth (), texture_rt->GetHeight (),
00115       texture_rt, texxform, nux::color::White, 1.0f);
00116 
00117     graphics_engine->QRP_1Tex(0, 0, tex_blur->GetWidth(), tex_blur->GetHeight(), tex_blur, texxform, nux::color::White);
00118 
00119     sprintf(fps, "FPS: %3.2f", frame_rate);
00120     nux::PageBBox page;
00121     page.xmin = 0;
00122     page.xmax = 100;
00123     page.ymin = 0;
00124     page.ymax = 20;
00125     page.x_margin = 0;
00126     page.y_margin = 0;
00127     graphics_engine->RenderColorTextLineStatic(graphics_engine->GetBoldFont (), page, fps, nux::color::White, false, nux::eAlignTextLeft);
00128 
00129     graphics_display->SwapBuffer();
00130 
00131     float frame_time = graphics_display->GetFrameTime();
00132     graphics_display->ResetFrameTime();
00133     periode_time += frame_time;
00134 
00135     frame_counter++;
00136     frame_periode++;
00137 
00138     if (frame_periode >= 100)
00139     {
00140       frame_rate = frame_periode * 1000.0f / periode_time;
00141       periode_time = 0.0f;
00142       frame_periode = 0;
00143     }
00144 
00145   } while((event.type != nux::NUX_TERMINATE_APP) && (event.GetVirtualKeyState(NUX_VK_ESCAPE) == 0));
00146 
00147   fbo.Release ();
00148   texture_rt.Release ();
00149   depth_rt.Release ();
00150 
00151   delete graphics_display;
00152 }
00153 
00154 
00155 int main(int argc, char **argv)
00156 {
00157   nux::NuxCoreInitialize(0);
00158   nux::NuxGraphicsInitialize();
00159 
00160   RenderBlurredTexture ();
00161 
00162   return 0;
00163 }
00164