PageRenderTime 295ms CodeModel.GetById 140ms app.highlight 13ms RepoModel.GetById 138ms app.codeStats 0ms

/opengles/src/Texture.cpp

http://ftk.googlecode.com/
C++ | 176 lines | 89 code | 38 blank | 49 comment | 20 complexity | 17ee9af9430b3a9286656fa112b37b6c MD5 | raw file
  1// ==========================================================================
  2//
  3// Texture.cpp		Texture Class for 3D Rendering Library
  4//
  5// --------------------------------------------------------------------------
  6//
  7// 10-15-2003		Hans-Martin Will	initial version
  8//
  9// --------------------------------------------------------------------------
 10//
 11// Copyright (c) 2004, Hans-Martin Will. All rights reserved.
 12// 
 13// Redistribution and use in source and binary forms, with or without 
 14// modification, are permitted provided that the following conditions are 
 15// met:
 16// 
 17//	 *  Redistributions of source code must retain the above copyright
 18// 		notice, this list of conditions and the following disclaimer. 
 19//   *	Redistributions in binary form must reproduce the above copyright
 20// 		notice, this list of conditions and the following disclaimer in the 
 21// 		documentation and/or other materials provided with the distribution. 
 22// 
 23// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 24// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 25// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 26// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 27// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 
 28// OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 29// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 30// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 31// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 32// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
 33// THE POSSIBILITY OF SUCH DAMAGE.
 34//
 35// ==========================================================================
 36
 37
 38#include "stdafx.h"
 39#include "Texture.h"
 40#include <stdlib.h>
 41
 42using namespace EGL;
 43
 44
 45// --------------------------------------------------------------------------
 46// Class Texture
 47// --------------------------------------------------------------------------
 48
 49
 50const U8 Texture :: s_BytesPerPixel[] = {
 51	1,				// ALPHA
 52	1,				// LUMINANCE
 53	2,				// LUMINANCE_ALPHA
 54	3,				// RGB8
 55	4,				// RGBA8
 56	2,				// RGB565
 57	2,				// RGBA4444
 58	2				// RGBA5551
 59};
 60
 61
 62void Texture :: Init()	
 63{
 64	m_Data = 0;
 65	m_InternalFormat = RasterizerState::TextureFormatInvalid;
 66}
 67
 68
 69void Texture :: Dispose() {
 70	if (m_Data != 0) {
 71		free(m_Data);
 72		m_Data = 0;
 73	}
 74}
 75
 76
 77namespace {
 78	U32 Log(U32 value) {
 79		U32 result = 0;
 80		U32 mask = 1;
 81
 82		while ((value & mask) != value) {
 83			++result;
 84			mask = (mask << 1) | 1;
 85		}
 86
 87		return result;
 88	}
 89}
 90
 91
 92bool Texture :: Initialize(U32 width, U32 height, RasterizerState::TextureFormat format) {
 93
 94	if (m_Data != 0) {
 95		free(m_Data);
 96	}
 97
 98	m_LogWidth = Log(width);
 99	m_LogHeight = Log(height);
100
101	U32 pixels = width * height;
102	m_InternalFormat = format;
103	size_t bytes = pixels * GetBytesPerPixel();
104	m_Data = malloc(bytes);
105
106	return m_Data != 0;
107}
108
109
110
111U32 Texture :: GetLogBytesPerPixel() const {
112	return Log(GetBytesPerPixel());
113}
114
115
116// --------------------------------------------------------------------------
117// Class MultiTexture
118// --------------------------------------------------------------------------
119
120
121MultiTexture :: MultiTexture():
122	m_MinFilterMode(RasterizerState::FilterModeNearest),
123	m_MagFilterMode(RasterizerState::FilterModeNearest),
124	m_MipmapFilterMode(RasterizerState::FilterModeNone),
125	m_WrappingModeS(RasterizerState::WrappingModeRepeat),
126	m_WrappingModeT(RasterizerState::WrappingModeRepeat)
127{
128	for (size_t index = 0; index <= MAX_LEVELS; ++index) {
129		m_TextureLevels[index].Init();
130	}
131}
132
133
134MultiTexture :: ~MultiTexture() {
135	for (size_t index = 0; index <= MAX_LEVELS; ++index) {
136		m_TextureLevels[index].Dispose();
137	}
138}
139
140
141// --------------------------------------------------------------------------
142// Verify that this texture is consistently defined across all mipmapping 
143// levels (as specified in section 3.8.9)
144//
145// Returns:
146//	true		-	if this texture is defined consistently across all
147//					levels
148// --------------------------------------------------------------------------
149bool MultiTexture :: IsComplete() const {
150
151	U32 width = m_TextureLevels[0].GetWidth();
152	U32 height = m_TextureLevels[0].GetHeight();
153	RasterizerState::TextureFormat format = m_TextureLevels[0].GetInternalFormat();
154
155	for (int index = 1; index < MAX_LEVELS; ++index) {
156		if (width == 1 && height == 1) {
157			return true;
158		}
159
160		if (width > 1) {
161			width = width / 2;
162		}
163
164		if (height > 1) {
165			height = height / 2;
166		}
167
168		if (m_TextureLevels[index].GetWidth() != width ||
169			m_TextureLevels[index].GetHeight() != height ||
170			m_TextureLevels[index].GetInternalFormat() != format) {
171			return false;
172		}
173	}
174
175	return true;
176}