2
* Copyright 2010 Inalogic Inc.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU General Public License version 3, as published
6
* by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful, but
9
* WITHOUT ANY WARRANTY; without even the implied warranties of
10
* MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
11
* PURPOSE. See the GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* version 3 along with this program. If not, see
15
* <http://www.gnu.org/licenses/>
17
* Authored by: Jay Taoko <jaytaoko@inalogic.com>
22
#include "NuxCore/NuxCore.h"
23
#include "NuxImage/BitmapFormats.h"
24
#include "NuxGraphics/GraphicsDisplay.h"
25
#include "NuxGraphics/GLWindowManager.h"
26
#include "NuxGraphics/GraphicsEngine.h"
30
* - Frame buffer object
31
* - Set a texture in the fbo
32
* - Set fbo as a render target
33
* - Render Quad to fbo
34
* - Make a copy of the render target: CreateTextureFromBackBuffer
36
* - Blur the copied render target texture
37
* - Render to default back buffer
40
void RenderBlurredCopyOfRenderTarget ()
42
nux::GraphicsDisplay* graphics_display = gGLWindowManager.CreateGLWindow("Window", 600, 300, nux::WINDOWSTYLE_NORMAL, 0, false);
43
nux::GraphicsEngine* graphics_engine = graphics_display->GetGraphicsEngine();
45
graphics_display->ShowWindow();
47
nux::IntrusiveSP<nux::IOpenGLFrameBufferObject> fbo;
48
nux::IntrusiveSP<nux::IOpenGLBaseTexture> texture_rt;
49
nux::IntrusiveSP<nux::IOpenGLBaseTexture> depth_rt;
51
fbo = graphics_display->GetGpuDevice ()->CreateFrameBufferObject ();
52
texture_rt = graphics_display->GetGpuDevice ()->CreateSystemCapableDeviceTexture (graphics_display->GetWindowWidth(), graphics_display->GetWindowHeight(), 1, nux::BITFMT_R8G8B8A8);
53
depth_rt = graphics_display->GetGpuDevice ()->CreateSystemCapableDeviceTexture (graphics_display->GetWindowWidth(), graphics_display->GetWindowHeight(), 1, nux::BITFMT_D24S8);
57
graphics_engine->GetWindowSize(w, h);
58
graphics_engine->SetViewport(0, 0, w, h);
59
graphics_engine->SetContext(0, 0, w, h);
60
graphics_engine->Push2DWindow(w, h);
63
memset(&event, 0, sizeof(nux::IEvent));
66
int frame_counter = 0;
67
int frame_periode = 0;
69
float periode_time = 0;
70
bool first_time = true;
73
CHECKGL( glClearColor(0, 0, 0, 1) );
74
CHECKGL( glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT) );
76
graphics_display->GetSystemEvent(&event);
77
if(first_time || (event.e_event == nux::NUX_SIZE_CONFIGURATION))
80
graphics_engine->DisableAllTextureMode(0);
81
graphics_engine->DisableAllTextureMode(1);
82
graphics_engine->DisableAllTextureMode(2);
83
graphics_engine->DisableAllTextureMode(3);
84
graphics_engine->SetEnvModeSelectTexture(GL_TEXTURE0);
85
graphics_engine->GetWindowSize(w, h);
86
graphics_engine->SetViewport(0, 0, w, h);
87
graphics_engine->SetScissor(0, 0, w, h);
88
graphics_engine->SetContext(0, 0, w, h);
89
graphics_engine->Push2DWindow(w, h);
91
fbo = graphics_display->GetGpuDevice ()->CreateFrameBufferObject ();
92
texture_rt = graphics_display->GetGpuDevice ()->CreateSystemCapableDeviceTexture (graphics_display->GetWindowWidth(), graphics_display->GetWindowHeight(), 1, nux::BITFMT_R8G8B8A8);
93
depth_rt = graphics_display->GetGpuDevice ()->CreateSystemCapableDeviceTexture (graphics_display->GetWindowWidth(), graphics_display->GetWindowHeight(), 1, nux::BITFMT_D24S8);
94
fbo->FormatFrameBufferObject (graphics_display->GetWindowWidth(), graphics_display->GetWindowHeight(), nux::BITFMT_R8G8B8A8);
97
fbo->SetRenderTarget (0, texture_rt->GetSurfaceLevel (0));
98
fbo->SetDepthSurface (depth_rt->GetSurfaceLevel (0));
101
for (int i = 0; i < 1; i++)
103
nux::Rect geo (nux::RandomUInt(graphics_display->GetWindowWidth()),
104
nux::RandomUInt(graphics_display->GetWindowHeight()),
105
nux::RandomUInt(200),
106
nux::RandomUInt(200));
108
graphics_engine->QRP_Color(geo.x, geo.y, geo.width, geo.height, nux::Color::RandomColor());
111
nux::TexCoordXForm texxform;
112
// Make a copy of the render target
113
nux::ObjectPtr <nux::IOpenGLBaseTexture> tex_copy = graphics_engine->CreateTextureFromBackBuffer (0, 0, graphics_display->GetWindowWidth (), graphics_display->GetWindowHeight ());
115
// Restore the back buffer
116
graphics_display->GetGpuDevice ()->DeactivateFrameBuffer ();
118
// Make a blurred version of the back buffer
119
nux::ObjectPtr <nux::IOpenGLBaseTexture> tex_blur = graphics_engine->QRP_GetBlurTexture (
120
0, 0, tex_copy->GetWidth (), tex_copy->GetHeight (),
121
tex_copy, texxform, nux::Color::White, 1.0f);
123
// Render the blurred texture
124
graphics_engine->QRP_1Tex(0, 0, tex_blur->GetWidth(), tex_blur->GetHeight(), tex_blur, texxform, nux::Color::White);
126
sprintf(fps, "FPS: %3.2f", frame_rate);
134
graphics_engine->RenderColorTextLineStatic(graphics_engine->GetBoldFont (), page, fps, nux::Color::White, false, nux::eAlignTextLeft);
136
graphics_display->SwapBuffer();
138
float frame_time = graphics_display->GetFrameTime();
139
graphics_display->ResetFrameTime();
140
periode_time += frame_time;
145
if (frame_periode >= 100)
147
frame_rate = frame_periode * 1000.0f / periode_time;
152
} while((event.e_event != nux::NUX_TERMINATE_APP) && (event.GetVirtualKeyState(NUX_VK_ESCAPE) == 0));
155
texture_rt.Release ();
158
delete graphics_display;
161
int main(int argc, char **argv)
163
nux::NuxCoreInitialize(0);
164
nux::NuxGraphicsInitialize();
166
RenderBlurredCopyOfRenderTarget ();