nux-1.16.0
|
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 "NuxImage/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::IEvent event; 00096 memset(&event, 0, sizeof(nux::IEvent)); 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.e_event == 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 CHECKGL( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) ); 00123 CHECKGL( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) ); 00124 00125 glEnable(GL_TEXTURE_2D); 00126 glDisable(GL_TEXTURE_3D); 00127 glDisable(GL_TEXTURE_CUBE_MAP); 00128 00129 if (i > 0) 00130 x += tex[i-1]->GetWidth () + 5; 00131 00132 int width = tex[i]->GetWidth (); 00133 int height = tex[i]->GetHeight (); 00134 00135 glBegin(GL_QUADS); 00136 { 00137 glMultiTexCoord2fARB(GL_TEXTURE0, 0.0f, 0.0f); 00138 glVertex3f(x, y, 0); 00139 glMultiTexCoord2fARB(GL_TEXTURE0, 0.0f, 1.0); 00140 glVertex3f(x, y + height, 0); 00141 glMultiTexCoord2fARB(GL_TEXTURE0, 1.0f, 1.0); 00142 glVertex3f(x + width, y + height, 0); 00143 glMultiTexCoord2fARB(GL_TEXTURE0, 1.0f, 0.0); 00144 glVertex3f(x + width, y, 0); 00145 } 00146 glEnd(); 00147 } 00148 00149 m_GLWindow->SwapBuffer(); 00150 } while(event.e_event != nux::NUX_TERMINATE_APP); 00151 00152 for (int i = 0; i < 9; i++) 00153 { 00154 tex[i].Release (); 00155 } 00156 delete m_GLWindow; 00157 } 00158 00159 00160 int main(int argc, char **argv) 00161 00162 { 00163 nux::NuxCoreInitialize(0); 00164 nux::NuxGraphicsInitialize(); 00165 00166 RenderTexturePowerOfTwo (); 00167 00168 return 0; 00169 } 00170