How do you read a pixel from normalMap?

Problems building or running the engine, queries about how to use features etc.
Post Reply
User avatar
shocoben
Gnoblar
Posts: 5
Joined: Fri Feb 07, 2014 11:54 pm

How do you read a pixel from normalMap?

Post by shocoben »

Hi ! I've a basic question :oops: , but I've not been able to find answer to my question, only some ideas : How do you read a pixel from normalMap?

I want to do it to get a normal at given world point (which I currently normalize with the terrain Width(x) and Height(z) ) on the terrain.

What i'm currently trying to do :

Code: Select all

	unsigned char* data = new unsigned char[texturePtr->getWidth() * texturePtr->getHeight() * 3];
	
	texturePtr->getBuffer()->blitFromMemory(PixelBox(texturePtr->getWidth(),texturePtr->getHeight(), 1, PF_R8G8B8, (void*) data));
	unsigned char* red = &data[(0 + 0 * texturePtr->getWidth()) * 3];
	unsigned char* green = &data[(0 + 0 * texturePtr->getWidth()) * 3 + 1];
	unsigned char* blue = &data[(0 + 0 * texturePtr->getWidth()) * 3 + 2];

	std::cout<<"r " << *red << " g " << *green << "b " << *blue << std::endl;

Thanks for your help :D
scrawl
OGRE Expert User
OGRE Expert User
Posts: 1119
Joined: Sat Jan 01, 2011 7:57 pm
x 216

Re: How do you read a pixel from normalMap?

Post by scrawl »

blitFromMemory will fill the texture with some data from system memory. But you want to do the opposite, so use blitToMemory.
User avatar
shocoben
Gnoblar
Posts: 5
Joined: Fri Feb 07, 2014 11:54 pm

Re: How do you read a pixel from normalMap?

Post by shocoben »

Well, thank your to point at this , but it still doesn't work, green, red or blue are always equals to "0 \0". :(
scrawl
OGRE Expert User
OGRE Expert User
Posts: 1119
Joined: Sat Jan 01, 2011 7:57 pm
x 216

Re: How do you read a pixel from normalMap?

Post by scrawl »

Then how about showing us the updated code?
User avatar
shocoben
Gnoblar
Posts: 5
Joined: Fri Feb 07, 2014 11:54 pm

Re: How do you read a pixel from normalMap?

Post by shocoben »

Oops, sorry :). Maybe i forgot to say that I want to read a normal from a Ogre::Terrain->getTerrainNormalMap() ? I call readPixelFromTexture(_terrain->getTerrainNormalMap(), normal) it after Ogre::Terrain::load().
Do I have to ask to calculate normals?

Code: Select all

//in MainScene::load() ... after the creation of the scene and of the terrain /
_terrain->load();
Vector3* normal = new Vector3(0,0,0)
readPixelFromTexture(_terrain->getTerrainNormalMap(), normal);

Code: Select all

void MainScene::readPixelFromTexture(Ogre::TexturePtr texturePtr,Ogre::Vector3* output)
{

        //there is a problems, looks like the NormalMap is a PF_L16, does blitToMemory convert PF_L16 to PF_R8G8B8 ?

	unsigned char* datas = new unsigned char[texturePtr->getWidth() * texturePtr->getHeight() * 3];
	texturePtr->getBuffer()->blitToMemory(PixelBox(texturePtr->getWidth(), texturePtr->getHeight(),1, PF_R8G8B8, datas));

	for (int y = 0; y < texturePtr->getHeight(); ++y)
	{
		for (int x = 0; x < texturePtr->getWidth(); ++x)
		{
			unsigned char r = datas[(x + y * texturePtr->getWidth()) * 3];
			unsigned char g = datas[(x + 1 + y * texturePtr->getWidth()) * 3];
			unsigned char b = datas[(x + 2 + y * texturePtr->getWidth()) * 3];

			std::cout << " r " << r << " g " << g << " b " << b << std::endl;
		}
	}
}
Post Reply