~diegosarmentero/nuxplayground/nux-view-html-typos

« back to all changes in this revision

Viewing changes to src/nux-gfx-load-texture-volume/nux-gfx-load-texture-volume.cpp

  • Committer: Jay Taoko
  • Date: 2012-07-05 13:45:27 UTC
  • mfrom: (15 nuxplayground)
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: jay.taoko@canonical.com-20120705134527-32qoul6zg3fd43w4
* Added nux-widgets sample

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "NuxCore/NuxCore.h"
 
2
#include "NuxCore/Math/Matrix4.h"
 
3
#include "NuxGraphics/BitmapFormats.h"
 
4
#include "NuxGraphics/GraphicsDisplay.h"
 
5
#include "NuxGraphics/GLWindowManager.h"
 
6
#include "NuxGraphics/GraphicsEngine.h"
 
7
#include "NuxCore/FilePath.h"
 
8
 
 
9
#if defined(NUX_OS_WINDOWS)
 
10
  #define PKGDATADIR "../../data/"
 
11
#endif
 
12
 
 
13
 
 
14
void RenderVolume(nux::GraphicsEngine* graphics_engine, nux::BaseTexture* volume, int x, int y);
 
15
 
 
16
nux::TextureVolume* LoadVolumeTexture()
 
17
{
 
18
  TCHAR* texture_list[] =
 
19
  {
 
20
    TEXT(PKGDATADIR"/textures/0.png"),
 
21
    TEXT(PKGDATADIR"/textures/1.png"),
 
22
    TEXT(PKGDATADIR"/textures/2.png"),
 
23
    TEXT(PKGDATADIR"/textures/3.png"),
 
24
    TEXT(PKGDATADIR"/textures/4.png"),
 
25
    TEXT(PKGDATADIR"/textures/5.png"),
 
26
    TEXT(PKGDATADIR"/textures/6.png"),
 
27
    TEXT(PKGDATADIR"/textures/7.png"),
 
28
    TEXT(PKGDATADIR"/textures/8.png"),
 
29
    TEXT(PKGDATADIR"/textures/9.png"),
 
30
  };
 
31
 
 
32
#define N 10
 
33
 
 
34
  nux::NBitmapData* texture_bitmap[10];
 
35
 
 
36
  for (int i = 0; i < N; i++)
 
37
  {
 
38
    texture_bitmap[i] = nux::LoadImageFile(texture_list[i]);
 
39
  }
 
40
 
 
41
  int volume_width = texture_bitmap[0]->GetWidth();
 
42
  int volume_height = texture_bitmap[0]->GetHeight();
 
43
  nux::BitmapFormat format = texture_bitmap[0]->GetFormat();
 
44
 
 
45
  for (int i = 1; i < N; i++)
 
46
  {
 
47
    if (volume_width != texture_bitmap[i]->GetWidth() || volume_height != texture_bitmap[i]->GetHeight())
 
48
    {
 
49
      nuxDebugMsg("[LoadVolumeTexture] Invalid volume textures");
 
50
 
 
51
      for (int i = 0; i < N; i++)
 
52
      {
 
53
        delete texture_bitmap[i];
 
54
      }
 
55
      return NULL;
 
56
    }
 
57
  }
 
58
 
 
59
  nux::NVolumeData* volume_bitmap = new nux::NVolumeData(format, volume_width, volume_height, N, 1);
 
60
 
 
61
  for (int i = 0; i < N; i++)
 
62
  {
 
63
    volume_bitmap->SetSurface(i, 0, texture_bitmap[i]->GetSurface(0, 0));
 
64
  }
 
65
 
 
66
  for (int i = 0; i < N; i++)
 
67
  {
 
68
    delete texture_bitmap[i];
 
69
  }
 
70
 
 
71
  nux::TextureVolume* texture_volume = new nux::TextureVolume();
 
72
  texture_volume->Update(volume_bitmap);
 
73
  return texture_volume;
 
74
}
 
75
 
 
76
void RenderVolumeTexture()
 
77
{
 
78
  nux::GraphicsDisplay* graphics_display = gGLWindowManager.CreateGLWindow("Load Volume Texture", 750, 100, nux::WINDOWSTYLE_NORMAL, 0, false);
 
79
  nux::GraphicsEngine* graphics_engine = graphics_display->GetGraphicsEngine();
 
80
 
 
81
  graphics_display->ShowWindow();
 
82
 
 
83
  nux::BaseTexture* texture = LoadVolumeTexture();
 
84
  nux::ObjectPtr<nux::CachedTextureVolume> cached_texture = texture->GetCachedTexture();
 
85
  cached_texture->m_Texture->SetFiltering(GL_LINEAR, GL_LINEAR);
 
86
  cached_texture.Release();
 
87
 
 
88
  int w, h;
 
89
  graphics_engine->GetWindowSize(w, h);
 
90
  graphics_engine->SetViewport(0, 0, w, h);
 
91
  graphics_engine->SetOrthographicProjectionMatrix(w, h);
 
92
 
 
93
  nux::Event event;
 
94
  memset(&event, 0, sizeof(nux::Event));
 
95
  bool first_time = true;
 
96
 
 
97
  do
 
98
  {
 
99
    CHECKGL(glClearColor(0.36f, 0.36f, 0.36f, 1));
 
100
    CHECKGL(glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT));
 
101
 
 
102
    graphics_display->GetSystemEvent(&event);
 
103
 
 
104
    if(first_time || (event.type == nux::NUX_SIZE_CONFIGURATION))
 
105
    {
 
106
      first_time = false;
 
107
      graphics_engine->DisableAllTextureMode(0);
 
108
      graphics_engine->DisableAllTextureMode(1);
 
109
      graphics_engine->DisableAllTextureMode(2);
 
110
      graphics_engine->DisableAllTextureMode(3);
 
111
 
 
112
      graphics_engine->GetWindowSize(w, h);
 
113
      graphics_engine->SetViewport(0, 0, w, h);
 
114
      graphics_engine->SetScissor(0, 0, w, h);
 
115
      graphics_engine->SetOrthographicProjectionMatrix(w, h);
 
116
    }
 
117
 
 
118
    graphics_engine->SetViewport(0, 0, w, h);
 
119
    graphics_engine->SetScissor(0, 0, w, h);
 
120
    graphics_engine->ApplyModelViewMatrix();
 
121
    graphics_engine->SetOrthographicProjectionMatrix(w, h);
 
122
 
 
123
    nux::Matrix4 modelview_matrix = graphics_engine->GetOpenGLModelViewMatrix();
 
124
    nux::Matrix4 projection_matrix = graphics_engine->GetOpenGLProjectionMatrix();
 
125
 
 
126
    glMatrixMode(GL_MODELVIEW);
 
127
    glLoadMatrixf((float*)(modelview_matrix.m));
 
128
    
 
129
    glMatrixMode(GL_PROJECTION);
 
130
    glLoadMatrixf((float*)(projection_matrix.m));
 
131
 
 
132
    if(texture->Type().IsObjectType(nux::TextureVolume::StaticObjectType))
 
133
    {
 
134
      RenderVolume(graphics_engine, texture, 10, 10);
 
135
    }
 
136
 
 
137
//     else if(texture->Type().IsObjectType(nux::TextureFrameAnimation::StaticObjectType))
 
138
//     {
 
139
//       nux::ObjectPtr<nux::CachedBaseTexture> glr = nux::GetGraphicsDisplay()->GetGraphicsEngine()->ResourceCache.FindCachedResourceById(texture->GetResourceIndex());
 
140
//       nux::ObjectPtr<nux::IOpenGLAnimatedTexture> AnimatedTextureRef = glr->m_Texture; //glr->m_Texture.CastRef<nux::IOpenGLAnimatedTexture>();
 
141
// 
 
142
//       AnimatedTextureRef->PresentNextFrame();
 
143
//       graphics_engine->SetTexture(GL_TEXTURE0, (nux::BaseTexture*) texture);
 
144
//       CHECKGL( glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR) );
 
145
//       CHECKGL( glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR) );
 
146
//       //graphics_engine->SetEnvModeSelectTexture(GL_TEXTURE0);
 
147
// 
 
148
//       glEnable(GL_TEXTURE_RECTANGLE_ARB);
 
149
//       glDisable(GL_TEXTURE_3D);
 
150
//       glDisable(GL_TEXTURE_CUBE_MAP);
 
151
//       glEnable(GL_BLEND);
 
152
//       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 
153
// 
 
154
//       int x = 10;
 
155
//       int y = 10;
 
156
//       int width = texture->GetWidth();
 
157
//       int height = texture->GetHeight();
 
158
// 
 
159
//       glBegin(GL_QUADS);
 
160
//       {
 
161
//         glMultiTexCoord2fARB(GL_TEXTURE0, 0.0f, 0.0f);
 
162
//         glVertex3f(x,  y, 0);
 
163
//         glMultiTexCoord2fARB(GL_TEXTURE0, 0.0f, texture->GetHeight());
 
164
//         glVertex3f(x,  y + height, 0);
 
165
//         glMultiTexCoord2fARB(GL_TEXTURE0, texture->GetWidth(), texture->GetHeight());
 
166
//         glVertex3f(x + width,  y + height, 0);
 
167
//         glMultiTexCoord2fARB(GL_TEXTURE0, texture->GetWidth(), 0.0f);
 
168
//         glVertex3f(x + width,  y, 0);
 
169
//       }
 
170
//       glEnd();
 
171
//       glDisable(GL_BLEND);
 
172
//     }
 
173
//     else
 
174
//     {
 
175
//       graphics_engine->SetTexture(GL_TEXTURE0, (nux::BaseTexture*) texture);
 
176
//       CHECKGL( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) );
 
177
//       CHECKGL( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) );
 
178
// 
 
179
//       //graphics_engine->SetEnvModeSelectTexture(GL_TEXTURE0);
 
180
// 
 
181
//       glEnable(GL_TEXTURE_2D);
 
182
//       glDisable(GL_TEXTURE_3D);
 
183
//       glDisable(GL_TEXTURE_CUBE_MAP);
 
184
// 
 
185
//       int x = 10;
 
186
//       int y = 10;
 
187
//       int width = 256;
 
188
//       int height = 256;
 
189
// 
 
190
//       glBegin(GL_QUADS);
 
191
//       {
 
192
//         glMultiTexCoord2fARB(GL_TEXTURE0, 0.0f, 0.0f);
 
193
//         glVertex3f(x,  y, 0);
 
194
//         glMultiTexCoord2fARB(GL_TEXTURE0, 0.0f, 1.0);
 
195
//         glVertex3f(x,  y + height, 0);
 
196
//         glMultiTexCoord2fARB(GL_TEXTURE0, 1.0f, 1.0);
 
197
//         glVertex3f(x + width,  y + height, 0);
 
198
//         glMultiTexCoord2fARB(GL_TEXTURE0, 1.0f, 0.0);
 
199
//         glVertex3f(x + width,  y, 0);
 
200
//       }
 
201
//       glEnd();
 
202
// 
 
203
//       //glDisable(GL_TEXTURE_3D);
 
204
// 
 
205
//   }
 
206
 
 
207
    //sprintf(gFPS, "FPS: %3.2f", nux::GetGraphicsDisplay()->GetFrameRate());
 
208
    nux::PageBBox page;
 
209
    page.xmin = 0;
 
210
    page.xmax = 100;
 
211
    page.ymin = 0;
 
212
    page.ymax = 30;
 
213
    page.x_margin = 0;
 
214
    page.y_margin = 0;
 
215
    //graphics_engine->RenderColorTextLineStatic(GFontBold, page, gFPS, 0xffff0000, eAlignTextLeft);     
 
216
 
 
217
      graphics_display->SwapBuffer();
 
218
  } while(event.type != nux::NUX_TERMINATE_APP);
 
219
 
 
220
  texture->UnReference();
 
221
  delete graphics_display;
 
222
}
 
223
 
 
224
void RenderVolume(nux::GraphicsEngine* graphics_engine, nux::BaseTexture* volume, int x, int y)
 
225
{
 
226
  // This function assume the projection matrix is orthographic
 
227
  if(!volume->Type().IsObjectType(nux::TextureVolume::StaticObjectType))
 
228
    return;
 
229
 
 
230
  graphics_engine->SetTexture(GL_TEXTURE0, (nux::BaseTexture*)volume);
 
231
  CHECKGL( glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) );
 
232
  CHECKGL( glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) );
 
233
 
 
234
  int width = 64;
 
235
  int height = 64;
 
236
 
 
237
  float depth = 0.0;
 
238
  for (int i = 0; i < volume->GetDepth(); i++)
 
239
  {
 
240
    glBegin(GL_QUADS);
 
241
    {
 
242
      glMultiTexCoord3fARB(GL_TEXTURE0, 0.0f, 0.0f, depth);
 
243
      glVertex3f(x, y, 0);
 
244
        
 
245
      glMultiTexCoord3fARB(GL_TEXTURE0, 0.0f, 1.0, depth);
 
246
      glVertex3f(x, y + height, 0);
 
247
        
 
248
      glMultiTexCoord3fARB(GL_TEXTURE0, 1.0f, 1.0, depth);
 
249
      glVertex3f(x + width, y + height, 0);
 
250
 
 
251
      glMultiTexCoord3fARB(GL_TEXTURE0, 1.0f, 0.0, depth);
 
252
      glVertex3f(x + width, y, 0);
 
253
    }
 
254
    glEnd();
 
255
    
 
256
    x += 70;
 
257
 
 
258
    depth += 1.0f / volume->GetDepth() + 0.001;
 
259
    if (depth > 1.0f)
 
260
    {
 
261
      depth = 1.0f;
 
262
    }
 
263
  }
 
264
}
 
265
 
 
266
 
 
267
int main(int argc, char **argv)
 
268
{
 
269
  nux::NuxCoreInitialize(0);
 
270
  nux::NuxGraphicsInitialize();
 
271
 
 
272
  RenderVolumeTexture();
 
273
  return 0;
 
274
}
 
 
b'\\ No newline at end of file'