Additionally pixellating texture at runtime

Anything and everything that's related to OGRE or the wider graphics field that doesn't fit into the other forums.
Post Reply
JasonXtreme
Gnoblar
Posts: 6
Joined: Thu Aug 28, 2014 10:12 am

Additionally pixellating texture at runtime

Post by JasonXtreme »

Here is the situation: I get an image from a video stream and must display it at the full resolution (let's say 16*16). So on a quad, the texture coordinates would be the fill 0,0 to 1,1. In relation to this quad is a smaller one with its own user defined "resolution" and part of the original texture it represents - shared via the same material pointer.

Practical example:

The main quad is 16*16, and the smaller one fits the area from 0.25,0.25 to 0.75,0.75 in texture coordinates, in other words the middle 8*8 pixels. Now all is fine if the user requests a 8*8 or larger texture, but if the requested resolution is smaller (4*4) I would need to additionally pixelate (basically just average from 8*8 to 4*4) the texture on the smaller screen to give an accurate representation.

This is done at about 30 fps in line with other very cpu demanding tasks, and the actual resolutions range all the way up to 1080p. So I was wondering, was there a way in Ogre to add a sort of a custom filter to a texture at runtime, so I wouldn't have to generate two separate textures each frame?

Thank you in advance,
Matija
User avatar
tod
Troll
Posts: 1394
Joined: Wed Aug 02, 2006 9:41 am
Location: Bucharest
x 94
Contact:

Re: Additionally pixellating texture at runtime

Post by tod »

I think you can very easily do that with a shader that samples the texture according to some parameters.


For coordinates between 0.25 and 0.75 for the "small' texture the shader would be something like this

Code: Select all

void fragshader(
            float2 texcoord        : TEXCOORD0,
 
            out float4 color    : COLOR,
 
            uniform sampler2D texture)
{
  newcoord = 0.5 * textcoor + 0.25;  
    color = tex2D(texture, newcoord);        // Sample the texture
}
Just add some parameters for the factors and you should have user specified dimensions.
JasonXtreme
Gnoblar
Posts: 6
Joined: Thu Aug 28, 2014 10:12 am

Re: Additionally pixellating texture at runtime

Post by JasonXtreme »

This seems nice and simple. I am fairly new to shaders, but as I understand it, a shader is something that applies to a material. Since the parent material changes the texture each frame, I would have to notify the child to update its own material which includes resampling and changing the texture. Won't this be a bit costly?
User avatar
tod
Troll
Posts: 1394
Joined: Wed Aug 02, 2006 9:41 am
Location: Bucharest
x 94
Contact:

Re: Additionally pixellating texture at runtime

Post by tod »

You don't need to change the material each frame, you just change the texture it points to. Search about dynamic textures on the forums, and also the wiki->code snippets.
I think you can actually do it all in one material and have the shader use the correct UV's on the borders and the modified ones inside the small rectangle.
JasonXtreme
Gnoblar
Posts: 6
Joined: Thu Aug 28, 2014 10:12 am

Re: Additionally pixellating texture at runtime

Post by JasonXtreme »

That's just it, the bigger rectangle needs to keep the original resolution. So if I want one modified texture and one original, I think I can't use the same material for both quads (I could be wrong here). Thought I could reuse the texture I suppose...
User avatar
tod
Troll
Posts: 1394
Joined: Wed Aug 02, 2006 9:41 am
Location: Bucharest
x 94
Contact:

Re: Additionally pixellating texture at runtime

Post by tod »

In this case, 2 quads using the same texture, the material is the same. For the small quad, you just have to modify the UVs. Then you don't need to modify any shaders. You may want to disable texture filtering if you really want stuff to be pixelated.
JasonXtreme
Gnoblar
Posts: 6
Joined: Thu Aug 28, 2014 10:12 am

Re: Additionally pixellating texture at runtime

Post by JasonXtreme »

Texture filtering is out of the question, as I need *very* precise control over how pixelated it is. To give you a bit of an insight, the smaller quad reprisents an actual low-res screen, and I need the resolutions to match exactly I'm afraid. The idea of modifying UVs, would you be so kind as to elaborate on that?
Post Reply