Map triangle texture to a right triangle

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):

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:

Now, with this image I texture my model, and this is the result:

The main problem is the blur. But this problem is soluble or not?
Thanks, for any questions I’m here!

“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.

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.

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…

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) :slight_smile:

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:

This is the texture map:

… and yes, the texture map is already blurred (as the previous), why?
This is the result after texture mapping:

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.

Nobody have an idea?

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.

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

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

This is the original texture:

And this is the texture map:

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.

Put finer white lines between the finer black lines.
and add horizontal lines too.

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.
Use this before uploading the texture :

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, &maxAnisotropy);
// use it :-)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, maxAnisotropy);

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.

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.

// use this before uploading your texture.
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_PACK_ALIGNMENT, 1);

This is a 10+ years old pitfall :

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

Where does it crashes ?
When uploading the texture ?
Run it with a debugger, check glGetError, etc.

I don’t understand, for example if this is my drawing function:

glBindTexture(GL_TEXTURE_2D, t[0]);
for() //Numbers of triangles
	for() //Number of vertices
		glTexCoord4fv(); glVertex3fv();

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…