gfxCubemap.cpp

Engine/source/gfx/gfxCubemap.cpp

More...

Detailed Description

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#include "gfx/gfxCubemap.h"
 25#include "gfx/gfxDevice.h"
 26#include "gfx/bitmap/gBitmap.h"
 27#include "gfx/gfxTextureManager.h"
 28
 29
 30GFXCubemap::GFXCubemap()
 31{
 32   mPath = "";
 33   mMipMapLevels = 0;
 34   mInitialized = false;
 35}
 36
 37GFXCubemap::~GFXCubemap()
 38{
 39   // If we're not dynamic and we were loaded from a
 40   // file then give the texture manager a chance to
 41   // remove us from the cache.
 42   if ( mPath.isNotEmpty() )
 43      TEXMGR->releaseCubemap( this );
 44}
 45
 46U32 GFXCubemap::zUpFaceIndex(const U32 index)
 47{
 48   switch (index)
 49   {
 50   case 2:
 51      return 4;
 52   case 3:
 53      return 5;
 54   case 4:
 55      return 2;
 56   case 5:
 57      return 3;
 58   default:
 59      return index;
 60   };
 61}
 62
 63void GFXCubemap::initNormalize( U32 size )
 64{
 65   Point3F axis[6] =
 66         {Point3F(1.0, 0.0, 0.0), Point3F(-1.0,  0.0,  0.0),
 67         Point3F(0.0, 1.0, 0.0), Point3F( 0.0, -1.0,  0.0),
 68         Point3F(0.0, 0.0, 1.0), Point3F( 0.0,  0.0, -1.0),};
 69   Point3F s[6] =
 70         {Point3F(0.0, 0.0, -1.0), Point3F( 0.0, 0.0, 1.0),
 71         Point3F(1.0, 0.0,  0.0), Point3F( 1.0, 0.0, 0.0),
 72         Point3F(1.0, 0.0,  0.0), Point3F(-1.0, 0.0, 0.0),};
 73   Point3F t[6] =
 74         {Point3F(0.0, -1.0, 0.0), Point3F(0.0, -1.0,  0.0),
 75         Point3F(0.0,  0.0, 1.0), Point3F(0.0,  0.0, -1.0),
 76         Point3F(0.0, -1.0, 0.0), Point3F(0.0, -1.0,  0.0),};
 77
 78   F32 span = 2.0;
 79   F32 start = -1.0;
 80
 81   F32 stride = span / F32(size - 1);
 82   GFXTexHandle faces[6];
 83
 84   for(U32 i=0; i<6; i++)
 85   {
 86      GFXTexHandle &tex = faces[i];
 87      GBitmap *bitmap = new GBitmap(size, size);
 88
 89      // fill in...
 90      for(U32 v=0; v<size; v++)
 91      {
 92         for(U32 u=0; u<size; u++)
 93         {
 94            Point3F vector;
 95            vector = axis[i] +
 96               ((F32(u) * stride) + start) * s[i] +
 97               ((F32(v) * stride) + start) * t[i];
 98            vector.normalizeSafe();
 99            vector = ((vector * 0.5) + Point3F(0.5, 0.5, 0.5)) * 255.0;
100            vector.x = mClampF(vector.x, 0.0f, 255.0f);
101            vector.y = mClampF(vector.y, 0.0f, 255.0f);
102            vector.z = mClampF(vector.z, 0.0f, 255.0f);
103            // easy way to avoid knowledge of the format (RGB, RGBA, RGBX, ...)...
104            U8 *bits = bitmap->getAddress(u, v);
105            bits[0] = U8(vector.x);
106            bits[1] = U8(vector.y);
107            bits[2] = U8(vector.z);
108         }
109      }
110
111      tex.set(bitmap, &GFXStaticTextureSRGBProfile, true, "Cubemap");
112   }
113
114   initStatic(faces);
115}
116
117const String GFXCubemap::describeSelf() const
118{
119   // We've got nothing
120   return String();
121}
122
123
124bool GFXCubemapHandle::set( const String &cubemapDDS )
125{
126   /// Free the previous handle to give us
127   /// back any texture memory when it can.
128   free();
129
130   // Let the texture manager find this for us.
131   StrongRefPtr<GFXCubemap>::set( TEXMGR->createCubemap( cubemapDDS ) );
132
133   return isValid();
134}
135
136const String GFXCubemapArray::describeSelf() const
137{
138   // We've got nothing
139   return String();
140}
141
142