Author Topic: CLAMP Or REPEAT?  (Read 12359 times)

Michael Lobko-Lobanovsky

  • Administrator
  • *****
  • Posts: 1481
CLAMP Or REPEAT?
« on: February 28, 2018, 11:09:27 am »
When loading the image as an OpenGL texture, it is important to decide whether it's going to be multiply tiled across the surface or just cover it once across its entire area.

If it is to be tiled, then its wrap S/T properties should be set as follows:

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);


But if it is supposed to cover the whole surface, then its wrap S/T properties should be defined as follows:

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);


Failure to observe this rule leads to texture edge artifacts especially prominent when the textured surface is being moved in the viewport. Start auto-rotating the carousel model and watch some of its textured quads flicker along their side edges. (see picture below)

This happens because when OpenGL prepares to tile a texture, it automatically blends the colors of texture border pixel rows and columns to make the seams between successive tiles less prominent. The artifact is most vivid when the opposite sides differ very much in color and/or brightness.
Mike
(3.6GHz Intel Core i5 Quad w/ 16GB RAM, nVidia GTX 1060Ti w/ 6GB VRAM, Windows 7 Ultimate Sp1)

Patrice Terrier

  • Administrator
  • *****
  • Posts: 1980
    • zapsolution
Re: CLAMP Or REPEAT?
« Reply #1 on: February 28, 2018, 12:17:41 pm »
perhaps a new #meta ?

We are using already GL_CLAMP_TO_EDGE for the wallpaper.
For most of my models, as long as i can remember, i never used tiled texture...
« Last Edit: February 28, 2018, 12:29:13 pm by Patrice Terrier »
Patrice
(Always working with the latest Windows version available...)

Michael Lobko-Lobanovsky

  • Administrator
  • *****
  • Posts: 1481
Re: CLAMP Or REPEAT?
« Reply #2 on: February 28, 2018, 04:44:06 pm »
Currently there are only two textures that we clamp to edge at all times. They are the wallpaper and FPS digit textures. And we repeat all others that are mipmapped in Mobj_MakeMultipleTexture(). Consider this:

...................
if (mt[K].Square == 4) { // MipMapping in ZI_CreateGLTextureFromFileEX
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    gluBuild2DMipmaps(GL_TEXTURE_2D, 4, xSize, ySize, GL_RGBA, GL_UNSIGNED_BYTE, lpArray);
} else {
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); // Avoid wallpaper bottom line artifacts
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); // ditto
    glTexImage2D(GL_TEXTURE_2D, 0, 4, xSize, ySize, 0, GL_RGBA, GL_UNSIGNED_BYTE, lpArray);
}
.....................


Tilable textures and those textures that cover the meshes whose UV (i.e. S/T) coords exceed the respective texture sizes (i.e. expect the texture to be infinitely repeatable along S and/or T axes for easier texturing) must be loaded as wrappable (repeatable). OTOH those that don't, should be clamped to edge regardless of mipmapping.

For simplification, they may be made repeatable too if they don't generate visible artifacts (all the edges are more or less equally colored and bright). But if they do, they should be clamped to edge explicitly. The bad thing is that it also concerns alpha textures, so that if one edge is opaque and the opposite one transparent, the transparent edge is going to acquire a one pixel wide opaque artifact.

IIRC the Wavefront Object format spec already defines officially some optional parameters to the map_XXX statements to precise if the respective texture is supposed to repeat or clamp. If no parameter is specified at all, the loader should default to GL_REPEAT. But what OBJ model loader/exporter observes the Wavefront format specs these days, after all? ;)
« Last Edit: February 28, 2018, 04:59:20 pm by Michael Lobko-Lobanovsky »
Mike
(3.6GHz Intel Core i5 Quad w/ 16GB RAM, nVidia GTX 1060Ti w/ 6GB VRAM, Windows 7 Ultimate Sp1)

Patrice Terrier

  • Administrator
  • *****
  • Posts: 1980
    • zapsolution
Re: CLAMP Or REPEAT?
« Reply #3 on: February 28, 2018, 04:52:06 pm »
Mike

I think that, when we are using billboard we should use clamping anyway, because we are always facing the camera, and using a flat square mesh.
« Last Edit: February 28, 2018, 04:55:29 pm by Patrice Terrier »
Patrice
(Always working with the latest Windows version available...)

Michael Lobko-Lobanovsky

  • Administrator
  • *****
  • Posts: 1481
Re: CLAMP Or REPEAT?
« Reply #4 on: February 28, 2018, 05:10:25 pm »
Then we should recognize those official map_XXX params in our material importer. OMG, it's been such a long time since I looked into the OBJ specs last, I don't even remember where they are on my disks...  ;)

OTOH we may make our billboards always load their textures as clamped to edge driven by the #billboard meta...

OK I'm going to add that #billboard feature to the texture loader.
« Last Edit: February 28, 2018, 05:27:08 pm by Michael Lobko-Lobanovsky »
Mike
(3.6GHz Intel Core i5 Quad w/ 16GB RAM, nVidia GTX 1060Ti w/ 6GB VRAM, Windows 7 Ultimate Sp1)

Patrice Terrier

  • Administrator
  • *****
  • Posts: 1980
    • zapsolution
Re: CLAMP Or REPEAT?
« Reply #5 on: February 28, 2018, 05:59:51 pm »
Quote
OK I'm going to add that #billboard feature to the texture loader.
Ok, fine!

Looking for individual mesh rotation, would be great for the ISD engine ;)
« Last Edit: February 28, 2018, 08:03:23 pm by Patrice Terrier »
Patrice
(Always working with the latest Windows version available...)

Michael Lobko-Lobanovsky

  • Administrator
  • *****
  • Posts: 1481
Re: CLAMP Or REPEAT?
« Reply #6 on: February 28, 2018, 06:28:02 pm »
Patrice,

You must be prepared to fix your ZI_UpdateNamedGLTextureFromFileEx() accordingly! If you don't do that, we won't be able to use the mods!

Quote
... the IPS engine ...

Any doubts as to its exactness again? ;D

Quote
... individual mesh rotation ...

Uhuh, and then comes "individual mesh translation", and finally "individual mesh animation". Been there, seen that.  :D
Mike
(3.6GHz Intel Core i5 Quad w/ 16GB RAM, nVidia GTX 1060Ti w/ 6GB VRAM, Windows 7 Ultimate Sp1)

Patrice Terrier

  • Administrator
  • *****
  • Posts: 1980
    • zapsolution
Re: CLAMP Or REPEAT?
« Reply #7 on: February 28, 2018, 06:47:53 pm »
Quote
You must be prepared to fix your ZI_UpdateNamedGLTextureFromFileEx() accordingly!

That should do it  :)
Code: [Select]
long Mobj_UpdateNamedGLTextureFromFileEx (IN WCHAR* sFullName, IN long NamedTexture, IN long nSquareMode) {
    long nRet = 0;
    if (glIsTexture(NamedTexture)) {
       BYTE* lpArray = 0;
       long xSize = 0, ySize = 0;
       if (Mobj_CreateGLTextureFromFileEX(sFullName, xSize, ySize, lpArray, nSquareMode)) {
          GL_BindTexture(GL_TEXTURE_2D, NamedTexture); nRet = glGetError();
          if (nRet == 0) {
             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
             glTexImage2D(GL_TEXTURE_2D, 0, 4, xSize, ySize, 0, GL_RGBA, GL_UNSIGNED_BYTE, lpArray);
             nRet = glGetError();
          }
          free (lpArray);
       }
    } else {
       nRet = GL_INVALID_ENUM;
    }
    return nRet;
}

Wanted to type ISD, but come to IPS, go figure  ;D
« Last Edit: February 28, 2018, 06:52:35 pm by Patrice Terrier »
Patrice
(Always working with the latest Windows version available...)

Michael Lobko-Lobanovsky

  • Administrator
  • *****
  • Posts: 1481
Re: CLAMP Or REPEAT?
« Reply #8 on: February 28, 2018, 07:19:16 pm »
OK that'll do, by the looks of it. So, I'm adding this func to our sources?


(69 is 69  ;D)
Mike
(3.6GHz Intel Core i5 Quad w/ 16GB RAM, nVidia GTX 1060Ti w/ 6GB VRAM, Windows 7 Ultimate Sp1)

Patrice Terrier

  • Administrator
  • *****
  • Posts: 1980
    • zapsolution
Re: CLAMP Or REPEAT?
« Reply #9 on: February 28, 2018, 07:21:24 pm »
Quote
OK that'll do, by the looks of it. So, I'm adding this func to our sources?
Yes, that was the purpose of it.
Patrice
(Always working with the latest Windows version available...)

Michael Lobko-Lobanovsky

  • Administrator
  • *****
  • Posts: 1481
Re: CLAMP Or REPEAT?
« Reply #10 on: February 28, 2018, 07:49:15 pm »
Patrice,

I have to figure out how to elegantly let the texture cache know some textures need CLAMP instead of REPEAT in Mobj_importMaterials(), and this may need some time. I'm not sure I'll have it ready before I Zzzzzz... (doze off) for tonight, so in the meantime, please think over and let me know the following.

Model rotation is simple. Mesh rotation is almost as simple to implement. But then, it would be only logical to add light source rotation option as well.

Moreover, there are A LOT of VERY spectacular animation effects that could be done with OpenGL texture coord matrix, let alone geometry animation effects like water surface waving and rippling, human chest breathing animation, and the like.

Are you really sure you want to whip me into adding all this to v2.5 before it goes public?
Mike
(3.6GHz Intel Core i5 Quad w/ 16GB RAM, nVidia GTX 1060Ti w/ 6GB VRAM, Windows 7 Ultimate Sp1)

Patrice Terrier

  • Administrator
  • *****
  • Posts: 1980
    • zapsolution
Re: CLAMP Or REPEAT?
« Reply #11 on: February 28, 2018, 08:02:19 pm »
Quote
Model rotation is simple. Mesh rotation is almost as simple to implement. But then, it would be only logical to add light source rotation option as well.
I think that me must process step by step, and mesh rotation is the first one...
Patrice
(Always working with the latest Windows version available...)

Michael Lobko-Lobanovsky

  • Administrator
  • *****
  • Posts: 1481
Re: CLAMP Or REPEAT?
« Reply #12 on: February 28, 2018, 08:14:05 pm »
Heh...  ;)

There is also that other "politically correct" answer that's been always driving me crazy: "Why not?" ;D
« Last Edit: February 28, 2018, 08:22:43 pm by Michael Lobko-Lobanovsky »
Mike
(3.6GHz Intel Core i5 Quad w/ 16GB RAM, nVidia GTX 1060Ti w/ 6GB VRAM, Windows 7 Ultimate Sp1)

Patrice Terrier

  • Administrator
  • *****
  • Posts: 1980
    • zapsolution
Re: CLAMP Or REPEAT?
« Reply #13 on: February 28, 2018, 08:54:09 pm »
Quote
"Why not?"

Because i would like to complete the ISD propulsor plasma rotation first, but of course light rotation would be also very nice, but i want you to go earlier in bed for safety health purpose ;)
Patrice
(Always working with the latest Windows version available...)

Michael Lobko-Lobanovsky

  • Administrator
  • *****
  • Posts: 1481
Re: CLAMP Or REPEAT?
« Reply #14 on: February 28, 2018, 09:50:07 pm »
I think plasma texture matrix rotation would be more computationally efficient than mesh geo rotation in real time. Can this wait still, do you think? There are a number of texture effects that should be implemented in a bundle.
Mike
(3.6GHz Intel Core i5 Quad w/ 16GB RAM, nVidia GTX 1060Ti w/ 6GB VRAM, Windows 7 Ultimate Sp1)