Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

texture.cpp File Reference

#include <cmath>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstdio>
#include <cassert>
#include <config.h>
#include <celmath/vecmath.h>
#include <celutil/filetype.h>
#include <celutil/debug.h>
#include "gl.h"
#include "glext.h"
#include "celestia.h"
#include <jpeglib.h>
#include "png.h"
#include "texture.h"
#include "virtualtex.h"

Include dependency graph for texture.cpp:

Go to the source code of this file.

Defines

#define GL_TEXTURE_MAX_LEVEL   0x813D
#define JPEG_SUPPORT
#define png_jmpbuf(png_ptr)   png_ptr->jmpbuf
#define png_set_gray_1_2_4_to_8(p)   png_set_expand(p)
#define png_set_palette_to_rgb(p)   png_set_expand(p)
#define png_set_tRNS_to_alpha(p)   png_set_expand(p)
#define PNG_SUPPORT

Functions

static int CalcMipLevelCount (int w, int h)
TextureCreateProceduralCubeMap (int size, int format, ProceduralTexEval func)
TextureCreateProceduralTexture (int width, int height, int format, TexelFunctionObject &func, Texture::AddressMode addressMode, Texture::MipMapMode mipMode)
TextureCreateProceduralTexture (int width, int height, int format, ProceduralTexEval func, Texture::AddressMode addressMode, Texture::MipMapMode mipMode)
static TextureCreateTextureFromImage (Image &img, Texture::AddressMode addressMode, Texture::MipMapMode mipMode)
static Vec3f cubeVector (int face, float s, float t)
static int getCompressedBlockSize (int format)
static GLenum GetGLTexAddressMode (Texture::AddressMode addressMode)
static int getInternalFormat (int format)
static const TextureCapsGetTextureCaps ()
static int ilog2 (unsigned int x)
static bool isPow2 (int x)
TextureLoadHeightMapFromFile (const string &filename, float height, Texture::AddressMode addressMode)
static void LoadMiplessTexture (Image &img, GLenum target)
static void LoadMipmapSet (Image &img, GLenum target)
TextureLoadTextureFromFile (const string &filename, Texture::AddressMode addressMode, Texture::MipMapMode mipMode)
static void SetBorderColor (Color borderColor, GLenum target)
static bool testMaxLevel ()

Variables

static TextureCaps texCaps
static bool texCapsInitialized = false


Define Documentation

#define GL_TEXTURE_MAX_LEVEL   0x813D
 

Definition at line 49 of file texture.cpp.

Referenced by ImageTexture::ImageTexture(), and testMaxLevel().

#define JPEG_SUPPORT
 

Definition at line 11 of file texture.cpp.

#define png_jmpbuf png_ptr   )     png_ptr->jmpbuf
 

Definition at line 77 of file texture.cpp.

#define png_set_gray_1_2_4_to_8 p   )     png_set_expand(p)
 

Definition at line 83 of file texture.cpp.

#define png_set_palette_to_rgb p   )     png_set_expand(p)
 

Definition at line 82 of file texture.cpp.

#define png_set_tRNS_to_alpha p   )     png_set_expand(p)
 

Definition at line 84 of file texture.cpp.

#define PNG_SUPPORT
 

Definition at line 12 of file texture.cpp.


Function Documentation

static int CalcMipLevelCount int  w,
int  h
[static]
 

Definition at line 336 of file texture.cpp.

References ilog2(), and max.

Referenced by CubeMap::CubeMap(), ImageTexture::ImageTexture(), and TiledTexture::TiledTexture().

00337 {
00338     return max(ilog2(w), ilog2(h)) + 1;
00339 }

Texture* CreateProceduralCubeMap int  size,
int  format,
ProceduralTexEval  func
 

Definition at line 901 of file texture.cpp.

References cubeVector(), Image::getComponents(), Image::getPixelRow(), Vector3< T >::x, Vector3< T >::y, and Vector3< T >::z.

Referenced by Renderer::init().

00903 {
00904     Image* faces[6];
00905     bool failed = false;
00906 
00907     int i = 0;
00908     for (i = 0; i < 6; i++)
00909     {
00910         faces[i] = NULL;
00911         faces[i] = new Image(format, size, size);
00912         if (faces == NULL)
00913             failed = true;
00914     }
00915 
00916     if (!failed)
00917     {
00918         for (int i = 0; i < 6; i++)
00919         {
00920             Image* face = faces[i];
00921             for (int y = 0; y < size; y++)
00922             {
00923                 for (int x = 0; x < size; x++)
00924                 {
00925                     float s = ((float) x + 0.5f) / (float) size * 2 - 1;
00926                     float t = ((float) y + 0.5f) / (float) size * 2 - 1;
00927                     Vec3f v = cubeVector(i, s, t);
00928                     func(v.x, v.y, v.z,
00929                          face->getPixelRow(y) + x * face->getComponents());
00930                 }
00931             }
00932         }
00933     }
00934 
00935     Texture* tex = new CubeMap(faces);
00936 
00937     // Clean up the images
00938     for (i = 0; i < 6; i++)
00939     {
00940         if (faces[i] != NULL)
00941             delete faces[i];
00942     }
00943 
00944     return tex;
00945 }

Texture* CreateProceduralTexture int  width,
int  height,
int  format,
TexelFunctionObject func,
Texture::AddressMode  addressMode,
Texture::MipMapMode  mipMode
 

Definition at line 839 of file texture.cpp.

References Image::getComponents(), and Image::getPixelRow().

00844 {
00845     Image* img = new Image(format, width, height);
00846     if (img == NULL)
00847         return NULL;
00848 
00849     for (int y = 0; y < height; y++)
00850     {
00851         for (int x = 0; x < width; x++)
00852         {
00853             float u = (float) x / (float) width * 2 - 1;
00854             float v = (float) y / (float) height * 2 - 1;
00855             func(u, v, 0, img->getPixelRow(y) + x * img->getComponents());
00856         }
00857     }
00858 
00859     Texture* tex = new ImageTexture(*img, addressMode, mipMode);
00860     delete img;
00861 
00862     return tex;
00863 }

Texture* CreateProceduralTexture int  width,
int  height,
int  format,
ProceduralTexEval  func,
Texture::AddressMode  addressMode,
Texture::MipMapMode  mipMode
 

Definition at line 812 of file texture.cpp.

Referenced by Renderer::init(), and Galaxy::renderGalaxyPointSprites().

00817 {
00818     Image* img = new Image(format, width, height);
00819     if (img == NULL)
00820         return NULL;
00821 
00822     for (int y = 0; y < height; y++)
00823     {
00824         for (int x = 0; x < width; x++)
00825         {
00826             float u = (float) x / (float) width * 2 - 1;
00827             float v = (float) y / (float) height * 2 - 1;
00828             func(u, v, 0, img->getPixelRow(y) + x * img->getComponents());
00829         }
00830     }
00831 
00832     Texture* tex = new ImageTexture(*img, addressMode, mipMode);
00833     delete img;
00834 
00835     return tex;
00836 }

static Texture* CreateTextureFromImage Image img,
Texture::AddressMode  addressMode,
Texture::MipMapMode  mipMode
[static]
 

Definition at line 954 of file texture.cpp.

References GetTextureCaps(), isPow2(), max, and TextureCaps::maxTextureSize.

Referenced by LoadHeightMapFromFile(), and LoadTextureFromFile().

00957 {
00958     // Require texture dimensions to be powers of two.  Even though the
00959     // OpenGL driver will automatically rescale textures with non-power of
00960     // two sizes, some quality loss may result.  The power of two requirement
00961     // forces texture creators to resize their textures in an image editing
00962     // program, hopefully resulting in  textures that look as good as possible
00963     // when rendered on current hardware.
00964     if (!isPow2(img.getWidth()) || !isPow2(img.getHeight()))
00965     {
00966         clog << "Texture has non-power of two dimensions.\n";
00967         return NULL;
00968     }
00969 
00970     bool splittingAllowed = true;
00971     Texture* tex = NULL;
00972 
00973     int maxDim = GetTextureCaps().maxTextureSize;
00974     //int maxDim = 256;
00975     if ((img.getWidth() > maxDim || img.getHeight() > maxDim) &&
00976         splittingAllowed)
00977     {
00978         // The texture is too large; we need to split it.
00979         int uSplit = max(1, img.getWidth() / maxDim);
00980         int vSplit = max(1, img.getHeight() / maxDim);
00981         
00982         tex = new TiledTexture(img, uSplit, vSplit, mipMode);
00983     }
00984     else
00985     {
00986         // The image is small enough to fit in a single texture; or, splitting
00987         // was disallowed so we'll scale the large image down to fit in
00988         // an ordinary texture.
00989         tex = new ImageTexture(img, addressMode, mipMode);
00990     }
00991 
00992     return tex;
00993 }

static Vec3f cubeVector int  face,
float  s,
float  t
[static]
 

Definition at line 867 of file texture.cpp.

References Vector3< T >::normalize().

Referenced by CreateProceduralCubeMap().

00868 {
00869     Vec3f v;
00870     switch (face)
00871     {
00872     case 0:
00873         v = Vec3f(1.0f, -t, -s);
00874         break;
00875     case 1:
00876         v = Vec3f(-1.0f, -t, s);
00877         break;
00878     case 2:
00879         v = Vec3f(s, 1.0f, t);
00880         break;
00881     case 3:
00882         v = Vec3f(s, -1.0f, -t);
00883         break;
00884     case 4:
00885         v = Vec3f(s, -t, 1.0f);
00886         break;
00887     case 5:
00888         v = Vec3f(-s, -t, -1.0f);
00889         break;
00890     default:
00891         // assert(false);
00892         break;
00893     }
00894 
00895     v.normalize();
00896 
00897     return v;
00898 }

static int getCompressedBlockSize int  format  )  [static]
 

Definition at line 217 of file texture.cpp.

References GL_COMPRESSED_RGBA_S3TC_DXT1_EXT.

Referenced by TiledTexture::TiledTexture().

00218 {
00219     if (format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT)
00220         return 8;
00221     else
00222         return 16;
00223 }

static GLenum GetGLTexAddressMode Texture::AddressMode  addressMode  )  [static]
 

Definition at line 226 of file texture.cpp.

References TextureCaps::clampToBorderSupported, TextureCaps::clampToEdgeSupported, GetTextureCaps(), GL_CLAMP_TO_BORDER_ARB, and GL_CLAMP_TO_EDGE.

Referenced by ImageTexture::ImageTexture(), and TiledTexture::TiledTexture().

00227 {
00228     const TextureCaps& caps = GetTextureCaps();
00229 
00230     switch (addressMode)
00231     {
00232     case Texture::Wrap:
00233         return GL_REPEAT;
00234 
00235     case Texture::EdgeClamp:
00236         return caps.clampToEdgeSupported ? GL_CLAMP_TO_EDGE : GL_CLAMP;
00237 
00238     case Texture::BorderClamp:
00239         if (caps.clampToBorderSupported)
00240             return GL_CLAMP_TO_BORDER_ARB;
00241         else
00242             return caps.clampToEdgeSupported ? GL_CLAMP_TO_EDGE : GL_CLAMP;
00243     }
00244 
00245     return 0;
00246 }

static int getInternalFormat int  format  )  [static]
 

Definition at line 157 of file texture.cpp.

References GL_BGR_EXT, GL_BGRA_EXT, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, and GL_DSDT_NV.

Referenced by CubeMap::CubeMap(), ImageTexture::ImageTexture(), LoadMiplessTexture(), LoadMipmapSet(), and TiledTexture::TiledTexture().

00158 {
00159     switch (format)
00160     {
00161     case GL_RGBA:
00162     case GL_BGRA_EXT:
00163         return 4;
00164     case GL_RGB:
00165     case GL_BGR_EXT:
00166         return 3;
00167     case GL_LUMINANCE_ALPHA:
00168         return 2;
00169     case GL_ALPHA:
00170     case GL_INTENSITY:
00171     case GL_LUMINANCE:
00172         return 1;
00173     case GL_DSDT_NV:
00174         return format;
00175     case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
00176     case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
00177     case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
00178         return format;
00179     default:
00180         return 0;
00181     }
00182 }

static const TextureCaps& GetTextureCaps  )  [static]
 

Definition at line 132 of file texture.cpp.

References TextureCaps::autoMipMapSupported, TextureCaps::clampToBorderSupported, TextureCaps::clampToEdgeSupported, TextureCaps::compressionSupported, ExtensionSupported(), InitExtension(), TextureCaps::maxLevelSupported, TextureCaps::maxTextureSize, testMaxLevel(), texCaps, and texCapsInitialized.

Referenced by CreateTextureFromImage(), and GetGLTexAddressMode().

00133 {
00134     if (!texCapsInitialized)
00135     {
00136         texCapsInitialized = true;
00137         texCaps.compressionSupported = ExtensionSupported("GL_ARB_texture_compression");
00138         if (texCaps.compressionSupported)
00139             InitExtension("GL_ARB_texture_compression");
00140 
00141 #ifdef GL_VERSION_1_2
00142         texCaps.clampToEdgeSupported = true;
00143 #else
00144         texCaps.clampToEdgeSupported = ExtensionSupported("GL_EXT_texture_edge_clamp");
00145 #endif // GL_VERSION_1_2
00146         texCaps.clampToBorderSupported = ExtensionSupported("GL_ARB_texture_border_clamp");
00147         texCaps.autoMipMapSupported = ExtensionSupported("GL_SGIS_generate_mipmap");
00148         texCaps.maxLevelSupported = testMaxLevel();
00149         glGetIntegerv(GL_MAX_TEXTURE_SIZE, &texCaps.maxTextureSize);
00150     }
00151 
00152     return texCaps;
00153 }

static int ilog2 unsigned int  x  )  [static]
 

Definition at line 322 of file texture.cpp.

Referenced by CalcMipLevelCount().

00323 {
00324     int n = -1;
00325 
00326     while (x != 0)
00327     {
00328         x >>= 1;
00329         n++;
00330     }
00331 
00332     return n;
00333 }

static bool isPow2 int  x  )  [static]
 

Definition at line 948 of file texture.cpp.

Referenced by CreateTextureFromImage(), CreateVirtualTexture(), VirtualTexture::loadTileTexture(), and VirtualTexture::VirtualTexture().

00949 {
00950     return ((x & (x - 1)) == 0);
00951 }

Texture* LoadHeightMapFromFile const string filename,
float  height,
Texture::AddressMode  addressMode
 

Definition at line 1018 of file texture.cpp.

References Image::computeNormalMap(), CreateTextureFromImage(), and LoadImageFromFile().

Referenced by TextureInfo::load().

01021 {
01022     Image* img = LoadImageFromFile(filename);
01023     if (img == NULL)
01024         return NULL;
01025     Image* normalMap = img->computeNormalMap(height,
01026                                              addressMode == Texture::Wrap);
01027     delete img;
01028     if (normalMap == NULL)
01029         return NULL;
01030 
01031     Texture* tex = CreateTextureFromImage(*normalMap, addressMode,
01032                                           Texture::DefaultMipMaps);
01033     delete normalMap;
01034 
01035     return tex;
01036 }

static void LoadMiplessTexture Image img,
GLenum  target
[static]
 

Definition at line 294 of file texture.cpp.

References getInternalFormat(), and glx::glCompressedTexImage2DARB.

Referenced by CubeMap::CubeMap(), ImageTexture::ImageTexture(), and TiledTexture::TiledTexture().

00295 {
00296     int internalFormat = getInternalFormat(img.getFormat());
00297 
00298     if (img.isCompressed())
00299     {
00300         glx::glCompressedTexImage2DARB(target,
00301                                        0,
00302                                        internalFormat,
00303                                        img.getWidth(), img.getHeight(),
00304                                        0,
00305                                        img.getMipLevelSize(0),
00306                                        img.getMipLevel(0));
00307     }
00308     else
00309     {
00310         glTexImage2D(target,
00311                      0,
00312                      internalFormat,
00313                      img.getWidth(), img.getHeight(),
00314                      0,
00315                      (GLenum) img.getFormat(),
00316                      GL_UNSIGNED_BYTE,
00317                      img.getMipLevel(0));
00318     }
00319 }

static void LoadMipmapSet Image img,
GLenum  target
[static]
 

Definition at line 259 of file texture.cpp.

References getInternalFormat(), glx::glCompressedTexImage2DARB, and max.

Referenced by CubeMap::CubeMap(), ImageTexture::ImageTexture(), and TiledTexture::TiledTexture().

00260 {
00261     int internalFormat = getInternalFormat(img.getFormat());
00262 
00263     for (int mip = 0; mip < img.getMipLevelCount(); mip++)
00264     {
00265         uint mipWidth  = max((uint) img.getWidth() >> mip, 1u);
00266         uint mipHeight = max((uint) img.getHeight() >> mip, 1u);
00267             
00268         if (img.isCompressed())
00269         {
00270             glx::glCompressedTexImage2DARB(target,
00271                                            mip,
00272                                            internalFormat,
00273                                            mipWidth, mipHeight,
00274                                            0,
00275                                            img.getMipLevelSize(mip),
00276                                            img.getMipLevel(mip));
00277         }
00278         else
00279         {
00280             glTexImage2D(target,
00281                          mip,
00282                          internalFormat,
00283                          mipWidth, mipHeight,
00284                          0,
00285                          (GLenum) img.getFormat(),
00286                          GL_UNSIGNED_BYTE,
00287                          img.getMipLevel(mip));
00288         }
00289     }
00290 }

Texture* LoadTextureFromFile const string filename,
Texture::AddressMode  addressMode,
Texture::MipMapMode  mipMode
 

Definition at line 996 of file texture.cpp.

References Content_CelestiaTexture, CreateTextureFromImage(), DetermineFileType(), LoadImageFromFile(), and LoadVirtualTexture().

Referenced by Renderer::init(), CelestiaCore::initRenderer(), and TextureInfo::load().

00999 {
01000     // Check for a Celestia texture--these need to be handled specially.
01001     if (DetermineFileType(filename) == Content_CelestiaTexture)
01002         return LoadVirtualTexture(filename);
01003 
01004     // All other texture types are handled by first loading an image, then
01005     // creating a texture from that image.
01006     Image* img = LoadImageFromFile(filename);
01007     if (img == NULL)
01008         return NULL;
01009 
01010     Texture* tex = CreateTextureFromImage(*img, addressMode, mipMode);
01011     delete img;
01012 
01013     return tex;
01014 }

static void SetBorderColor Color  borderColor,
GLenum  target
[static]
 

Definition at line 249 of file texture.cpp.

References Color::alpha(), Color::blue(), Color::green(), and Color::red().

Referenced by CubeMap::setBorderColor(), TiledTexture::setBorderColor(), and ImageTexture::setBorderColor().

00250 {
00251     float bc[4] = { borderColor.red(), borderColor.green(),
00252                     borderColor.blue(), borderColor.alpha() };
00253     glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, bc);
00254 }

static bool testMaxLevel  )  [static]
 

Definition at line 109 of file texture.cpp.

References GL_TEXTURE_MAX_LEVEL.

Referenced by GetTextureCaps().

00110 {
00111     unsigned char texels[64];
00112 
00113     glEnable(GL_TEXTURE_2D);
00114     // Test whether GL_TEXTURE_MAX_LEVEL is supported . . .
00115     glTexImage2D(GL_TEXTURE_2D,
00116                  0,
00117                  GL_LUMINANCE,
00118                  8, 8,
00119                  0,
00120                  GL_LUMINANCE,
00121                  GL_UNSIGNED_BYTE,
00122                  texels);
00123     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 2);
00124     float maxLev = -1.0f;
00125     glGetTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, &maxLev);
00126     glDisable(GL_TEXTURE_2D);
00127 
00128     return maxLev == 2;
00129 }


Variable Documentation

TextureCaps texCaps [static]
 

Definition at line 106 of file texture.cpp.

Referenced by GetTextureCaps().

bool texCapsInitialized = false [static]
 

Definition at line 94 of file texture.cpp.

Referenced by GetTextureCaps().


Generated on Sat Jan 14 22:31:25 2006 for Celestia by  doxygen 1.4.1