PDA

View Full Version : Map triangle texture to a right triangle

enigmagame
07-04-2008, 04:53 AM
Hi!
I must create a texture map, from an original image, in this manner:

Each triangle is mapped to a right angle triangle and these are packed together in arbitrary positions with a few pixels of padding between the triangles.

So, for example, this is the original image (there are two triangles):
http://img157.imageshack.us/img157/3522/tex1ou7.th.jpg (http://img157.imageshack.us/my.php?image=tex1ou7.jpg)
I must map each triangle to a right triangle of "opportune dimensions", with my professor we have found a way to find this dimension.
1)First I find the projected area in pixel of the original triangle.
2)Second I create a right triangle that approximates the area of the original triangle, this right triangle must have an area of 2^n pixels.
In our example: the projected area of each triangle in the original image is 37890 pixels, so I must use a right triangle that have an area of 2^16 pixels.
I make this mapping, and this is the result texture:
http://img157.imageshack.us/img157/3091/texture16uj8.th.jpg (http://img157.imageshack.us/my.php?image=texture16uj8.jpg)
Now, with this image I texture my model, and this is the result:
http://img76.imageshack.us/img76/1615/risultato16hx8.th.jpg (http://img76.imageshack.us/my.php?image=risultato16hx8.jpg)
The main problem is the blur. But this problem is soluble or not?
Thanks, for any questions I'm here!

ZbuffeR
07-04-2008, 02:11 PM
"problem is the blur" : you mean the blur on the rendered image ?
1) the triangle area is smaller than the texture. Try with "exact" surface correspondance.
2) maybe disabling mipmapping will help removing some blur.
3) And most important, try not shear your triangles ! In your texture, you should have the top angle at the extreme right, not at the left. Your texture is already blurred.

Zengar
07-04-2008, 02:47 PM
Uhm, mipmapping is there to avoid "blur", as it provides better quality filtering. Still, I don't really understand what enigmagame is trying to do here. Why splitting the texture in the first place? And why that overly complicated stuff with triangle area and such – wouldn't it be easier to draw triangles such that their size matcher the texture size in pixels?

And if I understand your problem correctly (just a guess): you start with a large texture, which you re-render as two small triangles, probably at smaller resolution. So you end up loosing texture resolution, thus introducing the blur.

ZbuffeR
07-04-2008, 03:36 PM
Zengar, sorry to nitpick, but mipmapping (with default bilinear filtering) does introduce more blur, especially in this case where the triangle surface is just a bit less than orignial texture : the second mipmap level is used, which happen to have half the x and y resolution.
Trilinear filtering + high level anisotropic filtering should approach the ideal filtering.
However, in some cases disabling mipmapping simply looks sharper, so sharp in fact that ugly and distracting shimmering appear as soon as animation happens...

Zengar
07-04-2008, 03:42 PM
Ok, you are right, I haven't considered that case. But if the minification is significant, mipmaps indeed provide better quality (and avoid that shimmering you speak about) :)

enigmagame
07-05-2008, 12:33 AM
"problem is the blur" : you mean the blur on the rendered image ?
1) the triangle area is smaller than the texture. Try with "exact" surface correspondance.
2) maybe disabling mipmapping will help removing some blur.
3) And most important, try not shear your triangles ! In your texture, you should have the top angle at the extreme right, not at the left. Your texture is already blurred.

1)Ok, I've tried with the "same" surface correspondance.
2)Mipmapping is not enable (I think).
3)Now the triangles are correctly mapped (I think).
I've modified the starting image with two numbers:
http://img502.imageshack.us/img502/1238/tex1ya9.th.jpg (http://img502.imageshack.us/my.php?image=tex1ya9.jpg)
This is the texture map:
http://img53.imageshack.us/img53/9926/texrn2.th.jpg (http://img53.imageshack.us/my.php?image=texrn2.jpg)
... and yes, the texture map is already blurred (as the previous), why?
This is the result after texture mapping:
http://img79.imageshack.us/img79/6033/24974125qo0.th.jpg (http://img79.imageshack.us/my.php?image=24974125qo0.jpg)

enigmagame
07-05-2008, 12:44 AM
Still, I don't really understand what enigmagame is trying to do here. Why splitting the texture in the first place? And why that overly complicated stuff with triangle area and such – wouldn't it be easier to draw triangles such that their size matcher the texture size in pixels?

This is a method that I must implement. Think this: I've a textured model (the cube with the wood texture), I take a screenshot of this model and I store the data on a image, the result image is the first in my post. Now suppose that I want texture my model (now without the original texture) with this image, it's run correctly. But I must organize this image in a compact texture map, method says: map each triangle to a righ triangle and these are packed together in arbitrary positions. If I use right triangle of 2^n dimension the packing are simple.
For example:
- Two triangles of size 2^n can be united to form a square.
- Two triangles of size 2^(n-1) can be united, and the result can be united to a triangle of size 2^n to form a square.
- ...
Think that now I'm working on a simple model (cube) in a simple position (front). But this method must be applied to a general model, for example a teapot.

enigmagame
07-06-2008, 07:07 AM
Nobody have an idea?

ZbuffeR
07-06-2008, 07:36 AM
Ah if you need a more general case...

Unless texels are sampled _exactly_ in the same way, you _will_ have some blurring.
Go twice larger to somewhat limit the effects of uneven sampling.
And you should start with better textures anyway : even in the orignal one, some resampling can bee seen.
I would start with a clean texture with both horizontal and vertical features to see better when the sampling is correct or not. Some artificial stuff like black/white checkerboard and lines of various width can help.

Did you try disabling/enabling mipmapping ? What results ?

Did you try trilinear filtering with as high as you can anisotropic filtering ?

Read to understand better what is sampling theory (http://en.wikipedia.org/wiki/Nyquist%E2%80%93Shannon_sampling_theorem).

enigmagame
07-06-2008, 09:16 AM
Did you try disabling/enabling mipmapping ? What results?

In this example I use a rectangular texture, using GL_TEXTURE_RECTANGLE_ARB extension, mipmapping is not supported, so is disabled.

Did you try trilinear filtering with as high as you can anisotropic filtering?

I use GL_LINEAR filter, can I use another filter with this extension?

I would start with a clean texture with both horizontal and vertical features to see better when the sampling is correct or not. Some artificial stuff like black/white checkerboard and lines of various width can help.

This is the original texture:
http://img374.imageshack.us/img374/4915/texlv6.th.jpg (http://img374.imageshack.us/my.php?image=texlv6.jpg)
And this is the texture map:
http://img368.imageshack.us/img368/9461/15788822nk0.th.jpg (http://img368.imageshack.us/my.php?image=15788822nk0.jpg)

-NiCo-
07-06-2008, 10:27 AM
Like ZbuffeR said

"Unless texels are sampled _exactly_ in the same way, you _will_ have some blurring."

This is also the case with linear filtering. For example, if you apply linear filtering and sample your original texture at texel corners, then you are actually performing a 2x2 box filtering which causes the blurring. Evidently, you would get the same result by sampling the second mipmap level at its texel centers.

ZbuffeR
07-06-2008, 10:49 AM
Put finer white lines between the finer black lines.

You can see the blur is not so important.
If I were you, I would double the resolution of the original triangle, then keep the same algorithm for the texture map generation.

For best results, use trilinear filtering (GL_LINEAR_MIPMAP_LINEAR) for minification filter, and don't forget to generate mipmaps.
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);

Activate anisotropic filtering (it will help the less ideal cases, the triangle is distorted) :

float maxAnisotropy;
//get the max value usable by the implementation
glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &amp;maxAnisotropy);
// use it :-)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, maxAnisotropy);

ZbuffeR
07-06-2008, 11:04 AM
And by the way, you can probably get rid of TEXTURE_RECTANGLE. They have way too many limitations. Modern graphic cards can all do non-power of two with classic TEXTURE_2D. Or work on a 2048*1024 texture if you really need the power-of-two.

enigmagame
07-06-2008, 12:17 PM
And by the way, you can probably get rid of TEXTURE_RECTANGLE. They have way too many limitations. Modern graphic cards can all do non-power of two with classic TEXTURE_2D. Or work on a 2048*1024 texture if you really need the power-of-two.

I can load a non-power of two texture with GL_TEXTURE_2D? Because if I try this, my program crash.

ZbuffeR
07-07-2008, 01:48 AM
If it is not supported, you should see white instead of texture.

You crash is elsewhere : beware of pack/unpack alignment ! Default is 4, which mean you did not have to think about it before, as most power-of-two sizes are multiple of 4.

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_PACK_ALIGNMENT, 1);

This is a 10+ years old pitfall : http://www.opengl.org/resources/features/KilgardTechniques/oglpitfall/

enigmagame
07-08-2008, 04:10 AM
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_PACK_ALIGNMENT, 1);

Thanks for the help, I've tried this, but the program crash again.

ZbuffeR
07-08-2008, 08:03 AM
Where does it crashes ?
Run it with a debugger, check glGetError, etc.

enigmagame
07-08-2008, 08:43 AM
Hangs...
I don't understand, for example if this is my drawing function:

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, t[0]);
for() //Numbers of triangles
{
glBegin(GL_TRIANGLES);
for() //Number of vertices
{
glTexCoord4fv(); glVertex3fv();
}
glEnd();
}
glDisable(GL_TEXTURE_2D);

It runs, but at the end, hangs.
If I comment glTexImage2D(), I don't load the image, in this case it's run without hangs.
glGetError() not returns anything...