Back to index

nux  3.0.0
texture_power_of_2.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  *  - load 2d textures of various size from the hard drive
00031  *  - textures have power of two size 256x256, 128x128, ...., 2x2, 1x1
00032  *  - manually load bitmap data into device texture: IOpenGLTexture2D
00033  *  - Lock/Unlock device texture data pointer
00034  *  - Use immediate mode rendering with glBegin, glEnd
00035  */
00036 
00037 void RenderTexturePowerOfTwo ()
00038 {
00039   nux::GraphicsDisplay* m_GLWindow = gGLWindowManager.CreateGLWindow("Window", 570, 270, nux::WINDOWSTYLE_NORMAL, 0, false);
00040   nux::GraphicsEngine* m_GraphicsContext = m_GLWindow->GetGraphicsEngine();
00041 
00042   m_GLWindow->ShowWindow();
00043 
00044   const TCHAR* texture_list [] =
00045   {
00046     TEXT("./data/mipmap256x256.png"),
00047     TEXT("./data/mipmap128x128.png"),
00048     TEXT("./data/mipmap64x64.png"),
00049     TEXT("./data/mipmap32x32.png"),
00050     TEXT("./data/mipmap16x16.png"),
00051     TEXT("./data/mipmap8x8.png"),
00052     TEXT("./data/mipmap4x4.png"),
00053     TEXT("./data/mipmap2x2.png"),
00054     TEXT("./data/mipmap1x1.png")
00055   };
00056 
00057   nux::ObjectPtr<nux::IOpenGLTexture2D> tex [9];
00058 
00059   for (int i = 0; i < 9; i++)
00060   {
00061     nux::NBitmapData *bitmap = nux::LoadImageFile(texture_list[i]);
00062     nux::ImageSurface surface = bitmap->GetSurface(0);
00063 
00064     surface.GetFormat();
00065 
00066     tex[i] = nux::GetGraphicsDisplay()->GetGpuDevice()->CreateTexture(
00067       surface.GetWidth(),
00068       surface.GetHeight(),
00069       1,
00070       surface.GetFormat());
00071 
00072     nux::SURFACE_LOCKED_RECT lockrect;
00073     tex[i]->LockRect(0, &lockrect, 0);
00074 
00075     BYTE                 *dest       = (BYTE *) lockrect.pBits;
00076     const BYTE       *src        = surface.GetPtrRawData();
00077     int              RowByteSize = surface.GetPitch();
00078     int              num_row     = surface.GetBlockHeight();
00079 
00080     for (int Y = 0; Y < num_row; Y++ )
00081     {
00082       // Take Min(RowByteSize, StrideY): the source and the destination may not have the same Pitch but
00083       // they contain the same amount of valid data since they have the same width, height and format.
00084       nux::Memcpy (dest + Y * lockrect.Pitch, &src[Y * RowByteSize], nux::Min (RowByteSize, lockrect.Pitch) );
00085     }
00086     tex[i]->UnlockRect (0);
00087   }
00088 
00089   int w, h;
00090   m_GraphicsContext->GetWindowSize(w, h);
00091   m_GraphicsContext->SetViewport(0, 0, w, h);
00092   m_GraphicsContext->SetContext(0, 0, w, h);
00093   m_GraphicsContext->Push2DWindow(w, h);
00094 
00095   nux::Event event;
00096   memset(&event, 0, sizeof(nux::Event));
00097 
00098   do
00099   {
00100     CHECKGL( glClearColor(0, 0, 0, 1) );
00101     CHECKGL( glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT) );
00102 
00103     m_GLWindow->GetSystemEvent(&event);
00104     if(event.type == nux::NUX_SIZE_CONFIGURATION)
00105     {
00106       m_GraphicsContext->DisableAllTextureMode(0);
00107       m_GraphicsContext->DisableAllTextureMode(1);
00108       m_GraphicsContext->DisableAllTextureMode(2);
00109       m_GraphicsContext->DisableAllTextureMode(3);
00110       m_GraphicsContext->GetWindowSize(w, h);
00111       m_GraphicsContext->SetViewport(0, 0, w, h);
00112       m_GraphicsContext->SetScissor(0, 0, w, h);
00113       m_GraphicsContext->SetContext(0, 0, w, h);
00114       m_GraphicsContext->Push2DWindow(w, h);
00115     }
00116 
00117     int x = 10;
00118     int y = 10;
00119     for (int i = 0; i < 9; i++)
00120     {
00121       m_GraphicsContext->SetTexture(GL_TEXTURE0, tex [i]);
00122 
00123       if (i > 0)
00124         x += tex[i-1]->GetWidth () + 5;
00125 
00126       int width = tex[i]->GetWidth ();
00127       int height = tex[i]->GetHeight ();
00128 
00129       nux::TexCoordXForm texxform;
00130       m_GraphicsContext->QRP_GLSL_1Tex(x, y, width, height, tex[i], texxform, nux::color::White);
00131     }
00132 
00133     m_GLWindow->SwapBuffer();
00134   } while(event.type != nux::NUX_TERMINATE_APP);
00135 
00136   for (int i = 0; i < 9; i++)
00137   {
00138     tex[i].Release ();
00139   }
00140   delete m_GLWindow;
00141 }
00142 
00143 
00144 int main(int argc, char **argv)
00145 
00146 {
00147   nux::NuxCoreInitialize(0);
00148   nux::NuxGraphicsInitialize();
00149 
00150   RenderTexturePowerOfTwo ();
00151 
00152   return 0;
00153 }
00154