Example #1
0
	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;
	}
Example #2
0
CFXAA::CFXAA(NL3D::UDriver *driver) : m_Driver(driver), m_PP(NULL), m_VP(NULL), m_Width(~0), m_Height(~0)
{
	nldebug("3D: Create FXAA");

	CDriverUser *dru = static_cast<CDriverUser *>(driver);
	NL3D::IDriver *drv = (dru)->getDriver();

	if (drv->supportBloomEffect() && drv->supportNonPowerOfTwoTextures())
	{
		m_PP = new CPixelProgram();
		// arbfp1
		{
			IProgram::CSource *source = new IProgram::CSource();
			source->Features.MaterialFlags = CProgramFeatures::TextureStages;
			source->Profile = IProgram::arbfp1;
			source->setSourcePtr(a_arbfp1);
			m_PP->addSource(source);
		}
		// ps_2_0
		{
			IProgram::CSource *source = new IProgram::CSource();
			source->Features.MaterialFlags = CProgramFeatures::TextureStages;
			source->Profile = IProgram::ps_2_0;
			source->setSourcePtr(a_ps_2_0);
			m_PP->addSource(source);
		}
		if (!drv->compilePixelProgram(m_PP))
		{
			nlwarning("3D: No supported pixel program for FXAA effect");

			delete m_PP;
			m_PP = NULL;
		}
		else
		{
			nldebug("3D: FXAA pixel program available");
		}
	}

	if (!m_PP)
	{
		return;
	}

	// create vp
	{
		m_VP = new CVertexProgram();
		// nelvp
		{
			IProgram::CSource *source = new IProgram::CSource();
			source->Features.MaterialFlags = CProgramFeatures::TextureStages;
			source->Profile = IProgram::nelvp;
			source->setSourcePtr(a_nelvp);
			m_VP->addSource(source);
		}
		if (!drv->compileVertexProgram(m_VP))
		{
			nlwarning("3D: No supported vertex program for FXAA effect");

			delete m_VP;
			m_VP = NULL;
			delete m_PP;
			m_PP = NULL;
		}
		else
		{
			nldebug("3D: FXAA vertex program available");
		}
	}

	if (!m_VP)
	{
		return;
	}
	
	// create material and vb
	{
		m_Mat = m_Driver->createMaterial();
		m_Mat.initUnlit();
		m_Mat.setColor(CRGBA::White);
		m_Mat.setBlend (false);
		m_Mat.setAlphaTest (false);
		NL3D::CMaterial *mat = m_Mat.getObjectPtr();
		mat->setShader(NL3D::CMaterial::Normal);
		mat->setBlendFunc(CMaterial::one, CMaterial::zero);
		mat->setZWrite(false);
		mat->setZFunc(CMaterial::always);
		mat->setDoubleSided(true);

		m_QuadUV.V0 = CVector(0.f, 0.f, 0.5f);
		m_QuadUV.V1 = CVector(1.f, 0.f, 0.5f);
		m_QuadUV.V2 = CVector(1.f, 1.f, 0.5f);
		m_QuadUV.V3 = CVector(0.f, 1.f, 0.5f);

		/*if (drv->textureCoordinateAlternativeMode())
		{
			m_QuadUV.Uv0 = CUV(0.f,  1.f);
			m_QuadUV.Uv1 = CUV(1.f, 1.f);
			m_QuadUV.Uv2 = CUV(1.f, 0.f);
			m_QuadUV.Uv3 = CUV(0.f,  0.f);
		}
		else
		{*/
			m_QuadUV.Uv0 = CUV(0.f,  0.f);
			m_QuadUV.Uv1 = CUV(1.f, 0.f);
			m_QuadUV.Uv2 = CUV(1.f, 1.f);
			m_QuadUV.Uv3 = CUV(0.f,  1.f);
		/*}*/

		/*CVertexBuffer &vb = m_VB;
		vb.clearValueEx();
		vb.addValueEx(CVertexBuffer::Position, CVertexBuffer::Float3);
		vb.addValueEx(CVertexBuffer::TexCoord0, CVertexBuffer::Float2);
		vb.addValueEx(CVertexBuffer::TexCoord1, CVertexBuffer::Float4);
		vb.initEx();
		vb.setPreferredMemory(CVertexBuffer::RAMVolatile, false);
		vb.setNumVertices(4);*/
	}
}