~canonical-dx-team/nux/nux.fix-702538

« back to all changes in this revision

Viewing changes to gputests/texture_copy_blur.cpp

  • Committer: Jay Taoko
  • Date: 2011-01-04 03:57:29 UTC
  • mfrom: (157.1.13 nux-holidays)
  • Revision ID: jay.taoko@canonical.com-20110104035729-0xh3iabo7e7dk371
* Added support to copy a texture from the current render target
* Blur, Color matrix, Exponent shaders
* Gaussian blur
* Bug fixes

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2010 Inalogic Inc.
 
3
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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/>
 
16
 *
 
17
 * Authored by: Jay Taoko <jaytaoko@inalogic.com>
 
18
 *
 
19
 */
 
20
 
 
21
 
 
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"
 
27
 
 
28
/*
 
29
 * Tests: 
 
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
 
35
 *  - Deactivate fbo
 
36
 *  - Blur the copied render target texture
 
37
 *  - Render to default back buffer
 
38
 */
 
39
 
 
40
void RenderBlurredCopyOfRenderTarget ()
 
41
{
 
42
  nux::GraphicsDisplay* graphics_display = gGLWindowManager.CreateGLWindow("Window", 600, 300, nux::WINDOWSTYLE_NORMAL, 0, false);
 
43
  nux::GraphicsEngine* graphics_engine = graphics_display->GetGraphicsEngine();
 
44
 
 
45
  graphics_display->ShowWindow();
 
46
 
 
47
  nux::IntrusiveSP<nux::IOpenGLFrameBufferObject> fbo;
 
48
  nux::IntrusiveSP<nux::IOpenGLBaseTexture> texture_rt;
 
49
  nux::IntrusiveSP<nux::IOpenGLBaseTexture> depth_rt;
 
50
 
 
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);
 
54
 
 
55
 
 
56
  int w, h;
 
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);
 
61
 
 
62
  nux::IEvent event;
 
63
  memset(&event, 0, sizeof(nux::IEvent));
 
64
 
 
65
  char fps [25];
 
66
  int frame_counter = 0;
 
67
  int frame_periode = 0;
 
68
  float frame_rate = 0;
 
69
  float periode_time = 0;
 
70
  bool first_time = true;
 
71
  do
 
72
  {
 
73
    CHECKGL( glClearColor(0, 0, 0, 1) );
 
74
    CHECKGL( glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT) );
 
75
 
 
76
    graphics_display->GetSystemEvent(&event);
 
77
    if(first_time || (event.e_event == nux::NUX_SIZE_CONFIGURATION))
 
78
    {
 
79
      first_time = false;
 
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);
 
90
 
 
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);
 
95
    }
 
96
 
 
97
    fbo->SetRenderTarget (0, texture_rt->GetSurfaceLevel (0));
 
98
    fbo->SetDepthSurface (depth_rt->GetSurfaceLevel (0));
 
99
    fbo->Activate();
 
100
 
 
101
    for (int i = 0; i < 1; i++)
 
102
    {
 
103
      nux::Rect geo (nux::RandomUInt(graphics_display->GetWindowWidth()),
 
104
        nux::RandomUInt(graphics_display->GetWindowHeight()),
 
105
        nux::RandomUInt(200),
 
106
        nux::RandomUInt(200));
 
107
 
 
108
      graphics_engine->QRP_Color(geo.x, geo.y, geo.width, geo.height, nux::Color::RandomColor());
 
109
    }
 
110
 
 
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 ());
 
114
 
 
115
    // Restore the back buffer
 
116
    graphics_display->GetGpuDevice ()->DeactivateFrameBuffer ();
 
117
 
 
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);
 
122
 
 
123
    // Render the blurred texture
 
124
    graphics_engine->QRP_1Tex(0, 0, tex_blur->GetWidth(), tex_blur->GetHeight(), tex_blur, texxform, nux::Color::White);
 
125
 
 
126
    sprintf(fps, "FPS: %3.2f", frame_rate);
 
127
    nux::PageBBox page;
 
128
    page.xmin = 0;
 
129
    page.xmax = 100;
 
130
    page.ymin = 0;
 
131
    page.ymax = 20;
 
132
    page.x_margin = 0;
 
133
    page.y_margin = 0;
 
134
    graphics_engine->RenderColorTextLineStatic(graphics_engine->GetBoldFont (), page, fps, nux::Color::White, false, nux::eAlignTextLeft);    
 
135
 
 
136
    graphics_display->SwapBuffer();
 
137
 
 
138
    float frame_time = graphics_display->GetFrameTime();
 
139
    graphics_display->ResetFrameTime();
 
140
    periode_time += frame_time;
 
141
 
 
142
    frame_counter++;
 
143
    frame_periode++;
 
144
 
 
145
    if (frame_periode >= 100)
 
146
    {
 
147
      frame_rate = frame_periode * 1000.0f / periode_time;
 
148
      periode_time = 0.0f;
 
149
      frame_periode = 0;
 
150
    }
 
151
 
 
152
  } while((event.e_event != nux::NUX_TERMINATE_APP) && (event.GetVirtualKeyState(NUX_VK_ESCAPE) == 0));
 
153
 
 
154
  fbo.Release ();
 
155
  texture_rt.Release ();
 
156
  depth_rt.Release ();
 
157
 
 
158
  delete graphics_display;
 
159
}
 
160
 
 
161
int main(int argc, char **argv)
 
162
{
 
163
  nux::NuxCoreInitialize(0);
 
164
  nux::NuxGraphicsInitialize();
 
165
 
 
166
  RenderBlurredCopyOfRenderTarget ();
 
167
 
 
168
  return 0;
 
169
}
 
170