Texture splatting: edit in realtime (and what about fog?)

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
suliman
Posts: 379
Joined: Sat Sep 23, 2006 2:06 pm

Texture splatting: edit in realtime (and what about fog?)

Post by suliman »

Hi
I found this code on the forum
http://irrlicht.sourceforge.net/forum/v ... g&start=15

from trivtn

I get it to compile fine. Now my problem is i want to edit the splatting realtime. As seen in:

Code: Select all

        terrain->setMaterialTexture(0,driver->getTexture("Sample.jpg"));
        terrain->setMaterialTexture(1,driver->getTexture("sandstone.jpg"));
        terrain->setMaterialTexture(2,driver->getTexture("grass_1.jpg"));
        terrain->setMaterialTexture(3,driver->getTexture("SANDSHOR.JPG"));
        terrain->setMaterialType((video::E_MATERIAL_TYPE)newMaterialType);      
1. The data where to draw which texture on the terrain is stored in Sample.jpg. How can this be edited in realtime (and updated visually). I have a system for editing height with the mouse and this is stored in a binary file which works fine to load and save. I would prefer to save this "splatting-data" binary also (in arrays of char[][] for example for each texture), i dont mind that it will take more space than image file.

2. Camera fog stopped working when i introduced this code. I saw it doesnt work with custom shaders. Any way to fix this?

Thanks a lot
Erik
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Texture splatting: edit in realtime (and what about fog?

Post by mongoose7 »

1. If you set

Code: Select all

t = driver->getTexture("Sample.jpg");
before

Code: Select all

terrain->setMaterialTexture(0, t);
then you will have a handle to the texture. You can now update it in realtime. (Err, I only now how to do it in OpenGL, but I believe Irrlicht has the functionality also.)

2. Add the fog calculation to the shader.
suliman
Posts: 379
Joined: Sat Sep 23, 2006 2:06 pm

Re: Texture splatting: edit in realtime (and what about fog?

Post by suliman »

Thanks
Once i excess the texture in the way you describe (which works fine) how do i edit RED at point x,y and ALPHA at point x,y (any of the 4 ARGB is what i need)?

Also, adding about fog calculation... Where do i find it and where is it supposed to go?
Here?

Code: Select all

class MyShaderCallBack : public video::IShaderConstantSetCallBack
{
public:
        virtual void OnSetConstants(video::IMaterialRendererServices* services,
                        s32 userData)
        {
                video::IVideoDriver* driver = services->getVideoDriver();
                // set clip matrix
                core::matrix4 worldViewProj;
                worldViewProj = driver->getTransform(video::ETS_PROJECTION);
                worldViewProj *= driver->getTransform(video::ETS_VIEW);
                worldViewProj *= driver->getTransform(video::ETS_WORLD);
                if (UseHighLevelShaders)
                        services->setVertexShaderConstant("matViewProjection", worldViewProj.pointer(), 16);
                else
                        services->setVertexShaderConstant(worldViewProj.pointer(), 4, 4);
                }
};
or where shader materials are created? (addHighLevelShaderMaterialFromFiles())

Yes im very new to shaders:)
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Texture splatting: edit in realtime (and what about fog?

Post by mongoose7 »

Way-hay. Textures are related to the scene via texture coords. Texture coords are related to vertices. Vertices are related to screen position. So, from (x,y) you need to find a triangle in the scene, which will give you three vertices. You will need to interpolate between them to get the point on the triangle corresponding to (x,y). Now you will need to interpolate the (u,v) values to find the (u,v) corresponding to a point in the texture.

Maybe you should say what you are really trying to do?

For shaders, see example 10. Basically, gpu = driver->getGPUProgrammingServices(), gpu->addHighLevelShaderMaterialFromFiles() will compile your shaders. You only have to write them now!
suliman
Posts: 379
Joined: Sat Sep 23, 2006 2:06 pm

Re: Texture splatting: edit in realtime (and what about fog?

Post by suliman »

What im really trying to do?
To make a mapeditor. I can edit height of terrain, store it to a binary file to save/load, and get back the exact same terrainSceneNode from it by going through the vertices and setting them using my char-value. Now i need to add functionality so i can edit the texture on the terrain as well.

Regarding adding fog:
Ive seen example 10. But this is not about fog. Ive no idea where to start coding fog myself, i thought i could use the engine code for fog somehow?
E
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Texture splatting: edit in realtime (and what about fog?

Post by mongoose7 »

If you use planar mapping it should be easy. From the screen (x,y) coords you get the terrain (x,y) coords and map them to the (u,v) coords. Change the texture and then copy it back to the GPU.

You asked about fog when using shaders. I should think in this case you wouldn't need shaders so you could get fog from Irrlicht.
suliman
Posts: 379
Joined: Sat Sep 23, 2006 2:06 pm

Re: Texture splatting: edit in realtime (and what about fog?

Post by suliman »

But how do i set the redcolor of a pixel? I try

Code: Select all

        u8 * pixel=(u8*)t->lock();
        int texSize=256;
 
        int x=5;
        int y=5;
 
        pixel[texSize*x+y]=video::SColor(255,255,0,0);
 
        t->unlock();
This complains of course because SColor cannot be converted to u8 but i dont know how to write this really. And would it work if i could get it to compile?
suliman
Posts: 379
Joined: Sat Sep 23, 2006 2:06 pm

Re: Texture splatting: edit in realtime (and what about fog?

Post by suliman »

To clearify, i can use:

Code: Select all

for(int i=0;i<texSize;i++)
for(int ii=0;ii<texSize;ii++)
    pixel[texSize*x+y]=0;
but this doesnt seem to change anything at all. And if i manually change the texture (sample.jp)g in photoshop before i run my program i can change what textures are drawn where on the terrain.
I try use both u8 and u32 as pixel pointer but im a bit lost...
blAaarg
Posts: 94
Joined: Tue Mar 02, 2010 9:11 pm
Location: SoCal

Re: Texture splatting: edit in realtime (and what about fog?

Post by blAaarg »

Basically, your last post loops through i and ii but doesn't do anything to change x or y. So, you keep setting the same pixel, possibly incorectly (see below).

Also, y should be multiplied by the pitch and x should be multiplied by the number-of-bytes-per-pixel when using u8 pointers to pixels .
(ITexture::getColorFormat() indicates the number of bytes-per-pixel if you understand how to interpret ECOLOR_FORMAT).
See ITexture::getPitch(); The pitch is the number-of-pixels-on-a-line * Number-of-bytes-per-pixel (e.g. 4 for ARGB)+ some-amount-of-overhead-bytes-that-hang-onto-the-end-of-a-line-of-pixels (typically 0 to 4 bytes, depending on the video hardware.) Irrlicht will figure that out for you at run-time, so if you're using u8's as a pointer, using (y*pitch) is a convenient way to jump down "y number of rows" on your texture before adding x*bytes-per-pixel.

If you are using u8's as pointers to a specific position in the texture, once you find the pixel you will need to offset the pointer by 0 to 3 bytes before setting it, in order to point it at the specific A, R, G or B value. So, like

Code: Select all

pixel[(y*pitch) + (x*bytesPerPixel) + 3]=255;
will set the last byte of a pixel to 255.

Also, the format that the video card uses to represent a pixel may not be ARGB like we think. Grrr :x ! ( Mine, for instance, uses BGRA.) I totally forget the rules to figuring this out, though hybrid has explained this at least a few times somewhere here on the boards. (I forget what to search on though. It would be nice to be able to find that in the docs. I just can't right now :oops: )

Also, for efficiency, try to remove as much code between lock() and unlock() as possible, so, don't initialize texSize, x, or y there.
Last edited by blAaarg on Mon Nov 21, 2011 9:32 am, edited 1 time in total.
"Computers don't make mistakes! What they do they do on purpose!!"

-Dale Gribble
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Texture splatting: edit in realtime (and what about fog?

Post by mongoose7 »

I think a texture has a getColorFormat or getTextureFormat member.
blAaarg
Posts: 94
Joined: Tue Mar 02, 2010 9:11 pm
Location: SoCal

Re: Texture splatting: edit in realtime (and what about fog?

Post by blAaarg »

Wut? Sorry if that was sloppy. I mentioned the getColorFormat() and you're right, there's a protected member which checks the desired format, not the actual one given, if I understand correctly.

I probably mentioned too much at once, but that's still just a start of what you gotta know to write to textures directly.
"Computers don't make mistakes! What they do they do on purpose!!"

-Dale Gribble
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Texture splatting: edit in realtime (and what about fog?

Post by hybrid »

If you only change a few (probably even not consecutive) pixels, you can use setPixel(x,y,color) on the image of the texture. Accessing via lock and unlock is only to optimize the access times. Accessing the pixels directly requires the proper access scheme. For ARGB8 you use the u32* cast, for the 16bit formats u16*. For RGB8 you must use u8* and skip by three in each step. Not sure if this works under big endian as well, though...
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Texture splatting: edit in realtime (and what about fog?

Post by mongoose7 »

1. Do you have to remember the texture format yourself? Presumably if you use Irrlicht's loading code, you don't know the format. So you need a function call to get it?
2. Yes, a u32* cast for ARGB8 is not endian independent. Would be better to return u8*? Could be that there are very few cases of Irrlicht being used on a big-endian platform. But someone said that ARM is big-endian? I use Sun SPARC, but that platform is *dead*.
suliman
Posts: 379
Joined: Sat Sep 23, 2006 2:06 pm

Re: Texture splatting: edit in realtime (and what about fog?

Post by suliman »

I will change clusters (by using a paintbrush basically). And must also reset the texture.

Frankly, i would prefer if i could just use

Code: Select all

char texture1[mapSize][mapSize];
char texture2[mapSize][mapSize];
etc
to splatt my textures on my terrain (intead of using graphical data for where to draw which texture). Is it doable and in that case how? It makes everything so much easier for me.

Thanks
Erik
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Texture splatting: edit in realtime (and what about fog?

Post by mongoose7 »

There appears to be no function to create a texture from a memory buffer (even though OpenGL supports it). But you could have, say, five textures you read from files and apply one to each part of the mesh, depending on its properties, but it wouldn't look like painting. Or you could assume your texture is ARGB8 or RGB8 (I don't know what the default is) and fix your test code as blAarg and hybrid suggested. This would be a better idea because you can make arbitrary changes to your textures after you get your mind thinking in the right direction.
Post Reply