Jump to content
  • Advertisement
Sign in to follow this  
thefrost

OpenGL [OGL] Weird 3D Texture Problem

This topic is 2916 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello! I have a nice little terrain system that uses pre-created transitioning tiles that are stored inside a 3D texture. Each tileset (texture + its transitioning textures) has 16 tiles that are each stored as a layer in the 3D texture.

All the tilesets are compiled into a single 3D texture. The corresponding texture layer would be accessed via ((tilesetNumber * 16) + tileNumber).

In my test case scenario I have 2 tilesets, totaling 32 texture layers. When rendered only textures 0 and 16 get applied to the terrain, regardless of the R texture coordinate.

If I set the R texture coordinate to 0.0f it will draw texture 0, anything greater than 0.0f will draw texture 16. What's going on?!?!?!?

Here is the relevant parts of my code:

// Texture size parameters
s32 w = 128;
s32 h = 128;
s32 d = 32;
s32 bpp = 4;

// Stores Pixels from layers
// size = (textureWidth * textureHeight * textureDepth * textureBPP)
u8 * Pixels = NULL;

// code to read in the layers
// I have thoroughly verified that this code works as expected (no need to post it)

// Texture formats. In the test case scenario its GL_RGBA
u32 format = GL_RGBA;
u32 internalFormat = GL_RGBA;
u32 GlID = NULL;

// Upload texture data and set texture parameters
glGenTextures(1, &GlID);
glBindTexture(GL_TEXTURE_3D, GlID);

glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// glTexParameteri(GL_TEXTURE_3D, GL_GENERATE_MIPMAP, GL_TRUE);

glTexImage3D(GL_TEXTURE_3D, 0, internalFormat, w, h, d, 0, format, GL_UNSIGNED_BYTE, Pixels);

// Render the test-case scenario
glActiveTextureARB(GL_TEXTURE0_ARB);
glEnable(GL_TEXTURE_3D);
glBindTexture(GL_TEXTURE_3D, GlID);

//! NOTE: +Y is up
glBegin(GL_QUADS);

glTexCoord3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 0.0f, 0.0f);

glTexCoord3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.0f, 0.0f, 0.0f);

glTexCoord3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, 0.0f, 1.0f);

glTexCoord3f(0.0f, 1.0f, 1.0f);
glVertex3f(0.0f, 0.0f, 1.0f);

glEnd();

glActiveTextureARB(GL_TEXTURE0_ARB);
glDisable(GL_TEXTURE_3D);


This test-case scenario should render a quad with texture 1 wrapped to it, but texture 16 appears instead!?!? I am probably messing up some weird OpenGL setting, but now matter how I tweak the settings the results stay the same! How would I get it so that the R texture coordinate corresponds to the appropriate layer in the 3D texture?

Thanks beforehand for your help.

[Edited by - thefrost on June 30, 2010 7:45:18 PM]

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by thefrost
..
glTexCoord3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 0.0f, 0.0f);

glTexCoord3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.0f, 0.0f, 0.0f);

glTexCoord3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, 0.0f, 1.0f);

glTexCoord3f(0.0f, 1.0f, 1.0f);
glVertex3f(0.0f, 0.0f, 1.0f);
..

This test-case scenario should render a quad with texture 1 wrapped to it, but texture 16 appears instead!?!?


No, you are wrong here :-) This code should render texture 16, because the texture coords are nomalized, that is texture 16 = max = r 1.0. To render the correct layer you have to divide the layer by max layers.

Some notes on using 3d textures for terrain rendering:
I once tried the same approach and I had to learn that this will not work. The trouble with 3d textures and terrain is, that you want "layers" of 2d textures, but you got one 3d texture block which will be mipmapped ! This will merge your textures to a really ugly color mess. What you really want are texture arrays.

Share this post


Link to post
Share on other sites
Thank you, so much, for your input!

I figured it would be something like that. The weird thing is that I have gotten it working with non-normalized R tcoord values 0.o!

I will definitely take a look at the texture arrays. Again, thanks!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!