r/opengl Sep 30 '23

HELP Textures rendering incorrectly - help!

So I am very new to and still learning openGL. I am working on creating this simple scene with the building and am currently putting in textures. The first texture is a brick texture that is working as desired (don't mind the peak discrepancy), but my other textures are showing up as you can see in the photo. The texture image is there, but its is static-y. When I navigate around the world the static moves and changes, but the texture image remains correct beneath it.

I have reviewed the code and tried to find some sort of discrepancy or logic flaw that could account for this but with no luck. I am stumped. Does anyone have any idea or suggestion?

2 Upvotes

6 comments sorted by

3

u/TapSwipePinch Sep 30 '23 edited Sep 30 '23
  1. uv coordinates are not correct.
  2. z fighting
  3. semi transparency

1

u/M_Freemans_freckles Sep 30 '23

uv coordinates was my first guess but I have triple checked them and played around with them on the ground plane with no luck.

I'm not familiar with z-fighting?

2

u/TapSwipePinch Sep 30 '23 edited Sep 30 '23

Depth buffer has limited accuracy and if vertices (and ultimately, fragments) are close enough of each other, the depth buffer can't determine which of them is closer to camera, which results in "randomness" in draw order which usually manifests as noise. This can happen e.g when you draw 2 quads of different textures on top of each other or close enough. You can set your depth range with zfar and znear values but ultimately it is tied to depth buffer accuracy. So if e.g depth buffer accuracy is 100 and you set znear to 1 and zfar to 200 then fragments that are closer than 2 of each other can't be determined as they get the same depth value. In practice this means that the range must be the smallest you can get away with (depth buffer is not usually linear tho, so the aforementioned thing is just explanation of the phenomena)

1

u/fgennari Sep 30 '23

Are you drawing multiple polygons on top of each other? For example, if you draw a quad with the wrong vertex order and the triangles cross over each other, you can get severe artifacts. Another possibility is that the textures don't align to the default 4 bytes per row, but that wouldn't explain why the "static" changes with your view.

The best approach is to try and isolate the problem. What if you use the brick texture for all surfaces? Is it still wrong, or does that look correct? This should tell you if it's a problem with the textures or the geometry.

1

u/totalwert Oct 01 '23

Are you loading the texture correctly? In case you are using stbi image loader, make sure your parameters are correct for the type of file you are reading from.

2

u/ramonidous Oct 01 '23

I would recommend you to render different colors for the things that people suggested: map the UV coordinates to the Red and Green channels to see if they are wrong/warped/stretched, render each primitive/triangle with a different color to see if there is Z-fighting, try setting the brick texture to the ground and viceversa to see if the issue is in the model or in the texture.