Back to index

nux  3.0.0
quad_2texmod.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  *  - Create a check board texture
00031  *  - Create a second texture and fill it manually with color values
00032  *  - Render full window background quad
00033  *  - enable blending
00034  *  - Render the two textures with QRP_2TexMod: result = (Tex0*Color0) * (Tex1*color1)
00035  *  - Disable blending
00036  */
00037 
00038 void QuadRendering_2TexMod ()
00039 {
00040   nux::GraphicsDisplay* graphics_display = gGLWindowManager.CreateGLWindow("QuadRendering: 2TexMod", 570, 270, nux::WINDOWSTYLE_NORMAL, 0, false);
00041   nux::GraphicsEngine* graphics_engine = graphics_display->GetGraphicsEngine();
00042 
00043   graphics_display->ShowWindow();
00044 
00045   int size = 256;
00046 
00047   nux::NTextureData checkboard_texture_data;
00048   MakeCheckBoardImage (checkboard_texture_data.GetSurface (0), size, size, nux::Color (0xff000000), nux::color::LightSeaGreen, 4, 4);
00049   nux::BaseTexture* checkboard_texture = graphics_display->GetGpuDevice ()->CreateSystemCapableTexture ();
00050   checkboard_texture->Update (&checkboard_texture_data);
00051 
00052 
00053   // Create a Texture map of size 256x1
00054   nux::NTextureData texture_data (nux::BITFMT_R8G8B8A8, size, 1, 1);
00055   // Get the first surface (mip 0) of the texture map
00056   nux::ImageSurface surface = texture_data.GetSurface (0);
00057 
00058   nux::ObjectPtr<nux::IOpenGLTexture2D> gradient_texture = graphics_display->GetGpuDevice ()->CreateSystemCapableDeviceTexture (
00059     texture_data.GetWidth (),
00060     texture_data.GetHeight (),
00061     1,
00062     texture_data.GetFormat ());
00063 
00064 
00065   nux::SURFACE_LOCKED_RECT lockrect;
00066   gradient_texture->LockRect (0, &lockrect, 0);
00067 
00068   BYTE *dest       = (BYTE *) lockrect.pBits;
00069   int  num_row     = surface.GetBlockHeight ();
00070 
00071   // Fill the gradient texture with White and an alpha value going from 1.0f to 0.0f
00072   for (int y = 0; y < num_row; y++)
00073   {
00074     // Take Min(RowByteSize, StrideY): the source and the destination may not have the same Pitch but
00075     // they contain the same amount of valid data since they have the same width, height and format.
00076     for (int x = 0; x < texture_data.GetWidth (); x++)
00077     {
00078       *(dest + y * lockrect.Pitch + 4*x + 0) = 0xff; //red
00079       *(dest + y * lockrect.Pitch + 4*x + 1) = 0xff; //green
00080       *(dest + y * lockrect.Pitch + 4*x + 2) = 0xff; //blue
00081       *(dest + y * lockrect.Pitch + 4*x + 3) = 255 - 255 * ((float)x/(float)texture_data.GetWidth ());
00082     }
00083   }
00084 
00085   gradient_texture->UnlockRect (0);
00086 
00087 
00088   int w, h;
00089   nux::Event event;
00090   memset(&event, 0, sizeof(nux::Event));
00091   graphics_engine->GetWindowSize(w, h);
00092   graphics_engine->SetViewport(0, 0, w, h);
00093   graphics_engine->SetContext(0, 0, w, h);
00094   graphics_engine->Push2DWindow(w, h);
00095 
00096   do
00097   {
00098     CHECKGL( glClearColor(0, 0, 0, 1) );
00099     CHECKGL( glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT) );
00100 
00101     graphics_display->GetSystemEvent(&event);
00102     if(event.type == nux::NUX_SIZE_CONFIGURATION)
00103     {
00104       graphics_engine->GetWindowSize(w, h);
00105       graphics_engine->SetViewport(0, 0, w, h);
00106       graphics_engine->SetScissor(0, 0, w, h);
00107       graphics_engine->SetContext(0, 0, w, h);
00108       graphics_engine->Push2DWindow(w, h);
00109     }
00110 
00111     nux::Rect geo (
00112       ((int)graphics_display->GetWindowWidth() - size) /2,
00113       ((int)graphics_display->GetWindowHeight() - size) /2,
00114       size,
00115       size);
00116 
00117     // Paint a rectangular quad over the entire window
00118     graphics_engine->QRP_Color(0, 0, graphics_display->GetWindowWidth(), graphics_display->GetWindowHeight(), nux::color::Aubergine);
00119 
00120     // Enable blending
00121     graphics_display->GetGraphicsEngine()->GetRenderStates ().SetBlend(true, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00122 
00123     nux::TexCoordXForm texxform0;
00124     nux::TexCoordXForm texxform1;
00125 
00126     // Modulate the checkboard and the gradient texture
00127     graphics_engine->QRP_2TexMod(geo.x, geo.y, geo.width, geo.height,
00128       gradient_texture, texxform0, nux::color::White,
00129       checkboard_texture->GetDeviceTexture (), texxform1, nux::color::White);
00130 
00131     graphics_display->GetGraphicsEngine()->GetRenderStates ().SetBlend(false);
00132 
00133     graphics_display->SwapBuffer();
00134   } while(event.type != nux::NUX_TERMINATE_APP);
00135 
00136   gradient_texture.Release ();
00137   checkboard_texture->UnReference ();
00138 
00139   delete graphics_display;
00140 }
00141 
00142 
00143 int main(int argc, char **argv)
00144 
00145 {
00146   nux::NuxCoreInitialize(0);
00147   nux::NuxGraphicsInitialize();
00148 
00149   QuadRendering_2TexMod ();
00150 
00151   return 0;
00152 }
00153