Back to index

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