예제 #1
0
/**
 * @brief Load an image by file name.
 * @param aName Name of the file.
 */
void PCShaderSurface::LoadImage(HashString const &aName)
{
    /* If the file was already loaded,
       let's avoid assigning a new id. */
    TextureData const& textureData = GetManager()->GetTextureData(aName);
    if(textureData.mTextureID != (unsigned)-1)
    {
        mTextureID = textureData.mTextureID;
        SetTextureSize(Vector3(textureData.mWidth, textureData.mHeight, 0));
    }
    // else we load the image from file
    else if((mSurface = IMG_Load(Common::RelativePath("Art", aName).c_str())))
    {
        if ((mSurface->w & (mSurface->w - 1)) != 0 )
        {
            DebugLogPrint("warning: width of image: %s is not a power of 2\n", aName.ToCharArray());
        }

        if ((mSurface->h & (mSurface->h - 1)) != 0 )
        {
            DebugLogPrint("warning: height of image: %s is not a power of 2\n", aName.ToCharArray());
        }

        SetTextureSize(Vector3(mSurface->w, mSurface->h, 0));
        mNumberOfColors = mSurface->format->BytesPerPixel;
        if (mNumberOfColors == 4)
        {
            if (mSurface->format->Rmask == 0x000000ff)
                mTextureFormat = GL_RGBA;
            else
#ifndef _WIN32
                mTextureFormat = GL_BGRA;
#else
                mTextureFormat = GL_RGBA;
#endif
        }
        else if (mNumberOfColors == 3)
        {
            if (mSurface->format->Rmask == 0x000000ff)
                mTextureFormat = GL_RGB;
            else
#ifndef _WIN32
                mTextureFormat = GL_BGR;
#else
                mTextureFormat = GL_RGB;
#endif
        }
        else
        {
            DebugLogPrint("warning: image %s is not truecolor...  this will probably break\n", aName.ToCharArray());
            DebugLogPrint("warning: bytes per pixel for image %s: %d\n", aName.ToCharArray(), mNumberOfColors);
        }

        AddTexturePairing(aName);
    }
    else
    {
        DebugLogPrint("warning: file: %s not found or incompatible format, check this out\n", aName.ToCharArray());
    }
}
예제 #2
0
/**
 * @brief Loads a text surface in font.
 * @param aFont Font to use.
 * @param aText Text to render.
 * @param aForegroundColor Color of the text.
 * @param aBackgroundColor Color of the background.
 * @param aSize Size of font.
 * @param aMaxWidth Max width of a single line (in pixels).
 * @return
 */
Vector3 PCShaderSurface::LoadText(HashString const &aFont, HashString const &aText, Vector4 const &aForegroundColor, Vector4 const &aBackgroundColor, int aSize, int aMaxWidth)
{
    // Endianness is important here
    Uint32 rmask, gmask, bmask, amask;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
    rmask = 0xff000000;
    gmask = 0x00ff0000;
    bmask = 0x0000ff00;
    amask = 0x000000ff;
#else
    rmask = 0x000000ff;
    gmask = 0x0000ff00;
    bmask = 0x00ff0000;
    amask = 0xff000000;
#endif

    HashString const textureDataHash = aFont + aText + Common::IntToString(aSize);
    TextureData const& data = GetManager()->GetTextureData(textureDataHash);
    if(data.mTextureID != (unsigned)-1)
    {
        Vector3 size = Vector3(data.mWidth, data.mHeight, 0);
        mTextureID = data.mTextureID;
        SetTextureSize(size);
        return size;
    }
    else
    {
        if(!TTF_WasInit())
            TTF_Init();
        mFont = TTF_OpenFont(Common::RelativePath("Fonts", aFont).c_str(), aSize);
        if(!mFont)
        {
            mFont = NULL;
            DebugLogPrint("warning: file not found or incompatible format, check this out\n");
            DebugLogPrint("%s", TTF_GetError());
            return Vector3(0, 0, 0);
        }

        // Create text texture
        SDL_Color fgColor = {(Uint8)aForegroundColor.x, (Uint8)aForegroundColor.y, (Uint8)aForegroundColor.z, (Uint8)aForegroundColor.w};
        //SDL_Color bgColor = {(Uint8)aBackgroundColor.x, (Uint8)aBackgroundColor.y, (Uint8)aBackgroundColor.z, (Uint8)aBackgroundColor.w};
        SDL_Surface *msg = TTF_RenderText_Blended_Wrapped(mFont, aText.ToCharArray(), fgColor, aMaxWidth);
        if(!msg)
        {
            DebugLogPrint("TTF_RenderText failed: %s", TTF_GetError());
            assert(msg);
        }

        mTextureFormat = GL_RGBA;
        mSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, msg->w, msg->h, 32, rmask, gmask, bmask, amask);
        SetTextureSize(Vector3(mSurface->w, mSurface->h, 0));
        SDL_BlitSurface(msg, NULL, mSurface, NULL);

        AddTexturePairing(textureDataHash);

        return Vector3(mSurface->w, mSurface->h, 0);
    }
}
		bool ITextureManagerOpenGL::SetTextureData(ITexture *tex, void *data, int width, int height,  TextureFormat Format, ImageFormat InputCPP)
		{
			//IMutexLocker lock(m_mMutexTex);
			if (tex /*&& data */&& width>0 && height>0)
			{
				SetTextureSize(tex,width,height);
				glBindTexture(GL_TEXTURE_2D, *(GLuint*)tex->GetTexture());

				//glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
				//glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
				glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
				glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
				//glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
				//glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);

				int CPP2=GL_DEPTH_COMPONENT;
				bool Error=false;
				if (InputCPP==IIF_RGB)
					CPP2=GL_RGB;
				else if (InputCPP==IIF_RGBA)
					CPP2=GL_RGBA;
				//else 
				//	Error=true;

				//if (Error==false)
				{
					int CPP=GL_RGB;
					bool Error=false;
					if (Format==ITF_RGBA)
						CPP=GL_RGBA;
					else if (Format==ITF_RED)
						CPP=GL_RED;
					else if (Format==ITF_GREEN)
						CPP=GL_GREEN;
					else if (Format==ITF_BLUE)
						CPP=GL_BLUE;
					else if (Format==ITF_ALPHA)
						CPP=GL_RGBA;
					else if (Format==ITF_DEPTH)
						CPP=GL_DEPTH_COMPONENT;
					else if (Format==ITF_DEPTH24)
						CPP=GL_DEPTH_COMPONENT24;
					else if (Format==ITF_DEPTH32)
						CPP=GL_DEPTH_COMPONENT32; // temporary
					else if (Format==ITF_RGB32F)
						CPP=GL_RGB32F_ARB; // GL_RGB32F_ARB
					else if (Format==ITF_RGBA32F)
						CPP=GL_RGBA32F_ARB; // GL_RGBA32F_ARB	/ GL_RGBA_FLOAT32_ATI			
					else if (Format==ITF_RGB16F)
						CPP=GL_RGB16F_ARB;
					else if (Format==ITF_RGBA16F)
						CPP=GL_RGBA16F_ARB;

					/*if (CPP==GL_RGB32F_ARB && isExtensionSupported("GL_RGB32F_ARB")==0)
					{
						ILogger::LogError("GL_RGB32F_ARB not supported\n");
						CPP = GL_RGB;
					}*/


					//if (CPP==GL_RGBA32F_ARB || CPP==GL_RGB32F_ARB || CPP==GL_RGBA_FLOAT32_ATI || CPP==GL_RGBA16F_ARB)
					//	CPP = GL_RGB16F_ARB;
					//if (CPP==GL_DEPTH_COMPONENT32 || CPP==GL_DEPTH_COMPONENT24)
					//	CPP = GL_DEPTH_COMPONENT;

					long inputtype=GL_UNSIGNED_BYTE;
					if (CPP==GL_RGBA32F_ARB || CPP==GL_RGB32F_ARB || CPP==GL_RGBA_FLOAT32_ATI || CPP==GL_RGBA16F_ARB || CPP==GL_RGB16F_ARB)
						inputtype=GL_HALF_FLOAT_ARB;


					glBindTexture(GL_TEXTURE_2D, *(GLuint*)tex->GetTexture());

					glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
					glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

					//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
					//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

					glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
					glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

					glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
					glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

					//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
					//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

					if (CPP2==GL_DEPTH_COMPONENT)
					{
						glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY);
						glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
						glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
					}

					//glTexImage2D(GL_TEXTURE_2D, 0, CPP, Width, Height, 0, CPP, GL_FLOAT, 0);
					glTexImage2D(GL_TEXTURE_2D, 0, CPP, width, height, 0, CPP2, inputtype, data);
					//gluBuild2DMipmaps(GL_TEXTURE_2D, CPP, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, data);
					//delete[] data;
					glBindTexture(GL_TEXTURE_2D, NULL);
					//tex->m_iWidth=width;
					//tex->m_iHeight=height;
					return true;
				}
				glBindTexture(GL_TEXTURE_2D, NULL);
				return false;
			}
			return false;
		}
		bool ITextureManagerOpenGL::SetCompressedTextureData(ITexture *tex, IImageITX* itx)
		{
			if (tex && itx)
			{
				glBindTexture(GL_TEXTURE_2D, *(GLuint*)tex->GetTexture());
				glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
				glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

				SetTextureSize(tex,itx->GetWidth(),itx->GetHeight());

				int blockSize=16;
				if (itx->GetFormat()==IImageITX::ITXF_DXT1 || itx->GetFormat()==IImageITX::ITXF_DXT1a || itx->GetFormat()==IImageITX::ITXF_DXT1nm)
					blockSize=8;

				int Format;
				switch (itx->GetFormat())
				{
				/*case IImageITX::ITXF_RGBA:
					Format=GL_COMPRESSED_RGBA_ARB;
					break;*/
				case IImageITX::ITXF_DXT1:
					Format=GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
					break;
				case IImageITX::ITXF_DXT1a:
					Format=GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
					break;
				case IImageITX::ITXF_DXT1nm:
					Format=GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
					break;
				case IImageITX::ITXF_DXT3:
					Format=GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
					break;
				case IImageITX::ITXF_DXT5:
				case IImageITX::ITXF_DXT5nm:
					Format=GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
					break;
				case IImageITX::ITXF_ATI1:
				case IImageITX::ITXF_ATI2_3Dc:
					Format=GL_ATI_texture_compression_3dc;
					break;
				}

				int nSize;
				int nOffset = 0;
				int Width=itx->GetWidth();
				int Height=itx->GetHeight();
				int NumMipMaps=itx->GetMipMapCount();

				for( int i = 0; i < NumMipMaps; ++i )
				{
					if( Width  == 0 ) Width  = 1;
					if( Height == 0 ) Height = 1;

					nSize = ((Width+3)/4) * ((Height+3)/4) * blockSize;

					if (itx->GetFormat()==IImageITX::ITXF_RGBA)
					{
						glTexImage2D(GL_TEXTURE_2D, 
							i, 
							GL_RGBA, 
							Width, 
							Height, 
							0, 
							GL_BGRA_EXT, 
							GL_UNSIGNED_BYTE, 
							(unsigned char*)itx->GetData() + nOffset);
					}else{
						glCompressedTexImage2DARB( GL_TEXTURE_2D,
							i,
							Format,
							Width,
							Height,
							0,
							nSize,
							(unsigned char*)itx->GetData() + nOffset );
					}

					nOffset += nSize;

					// Half the image size for the next mip-map level...
					Width  = (Width  / 2);
					Height = (Height / 2);
				}

				glBindTexture(GL_TEXTURE_2D, 0);

				return true;
			}
			return false;
		}