void NeLTexture::loadFromMemory(const void *buffPtr, uint buffWidth, uint buffHeight, PixelFormat pixelFormat) { // this object can only contain one texture at a time, free the old one if it exists. freeNeLTexture(); /** * This debugging is handy when you're not sure if the memory buffer * being sent to you from CEGUI is valid. Uncomment it and it'll create * a file called loadfrommem###.tga in your working directory. * * NLMISC::CBitmap btm; * btm.reset(); * btm.resize(buffWidth,buffHeight,NLMISC::CBitmap::RGBA); * uint8 *dest=&(btm.getPixels()[0]); * memcpy(dest,buffPtr,buffWidth*buffHeight*4); * std::string filename = NLMISC::CFile::findNewFile("loadfrommem.tga"); * NLMISC::COFile fs(filename); * btm.writeTGA(fs); */ int size=4; switch(pixelFormat) { case PF_RGB: size = 3; break; case PF_RGBA: size = 4; break; } // copy the memory stream for use in the NeL texture. uint8 *pTmpBuf=new uint8[buffWidth*buffHeight*size]; memcpy(pTmpBuf,buffPtr,buffWidth*buffHeight*size); // create the texture m_TextureMem=new NL3D::CTextureMem( pTmpBuf,buffWidth*buffHeight*size,true,false,buffWidth,buffHeight,NLMISC::CBitmap::RGBA); m_TextureMem->setWrapS(NL3D::ITexture::Clamp); m_TextureMem->setWrapT(NL3D::ITexture::Clamp); m_TextureMem->setFilterMode(NL3D::ITexture::Linear, NL3D::ITexture::LinearMipMapOff); m_TextureMem->setReleasable(false); m_TextureMem->generate(); if(m_TextureMem == 0) { // failed to load the texture. nlinfo("Failed to load texture from memory"); return; } /** * Configure the material. This is a little more complicated than loading a texture from * a file, since CTextureFileUser and CTextureFile take care of prepping all of the * necessary alpha settings. */ NL3D::CMaterial *mat = m_Material.getObjectPtr(); mat->initUnlit(); mat->setShader(NL3D::CMaterial::Normal); mat->setTexture(0, m_TextureMem); /** * We still use alpha testing to cull out pixels to speed up * blending and multitexturing. */ m_Material.setAlphaTest(true); m_Material.setAlphaTestThreshold(0.1f); m_Material.setBlend(true); m_Material.setBlendFunc(NL3D::UMaterial::srcalpha,NL3D::UMaterial::invsrcalpha); m_Material.texEnvOpRGB(0, NL3D::UMaterial::Modulate); m_Material.texEnvArg0RGB(0, NL3D::UMaterial::Texture, NL3D::UMaterial::SrcColor); m_Material.texEnvArg1RGB(0, NL3D::UMaterial::Diffuse, NL3D::UMaterial::SrcColor); m_Material.texEnvOpAlpha(0, NL3D::UMaterial::Modulate); m_Material.texEnvArg0Alpha(0, NL3D::UMaterial::Texture, NL3D::UMaterial::SrcAlpha); m_Material.texEnvArg1Alpha(0, NL3D::UMaterial::Diffuse, NL3D::UMaterial::SrcAlpha); m_Material.setZFunc(NL3D::UMaterial::always); m_Material.setDoubleSided(true); // make sure to record our changes. d_width=buffWidth; d_height=buffHeight; m_UsingFile=false; m_UsingMem=true; }