How to visualize 3D texture data with 2d textures in OpenGL ES 2.0?

OpenGL ES 2.0 supports 3D textures with extensions. Unfortunately, this type of extension is not supported on many devices. So what I triyng to do is use 2d textures instead of 3d textures. First, I matched the 3D texture data with a 2d texture atlas. For example, instead of having a three-dimensional texture with 128x128x4, I will have a 2d texture atlas containing 4 2D textures (128x128). The fragment shader will look something like this:

precision mediump float; uniform sampler2D s_texture; uniform vec2 2DTextureSize; uniform vec3 3DTextureSize; varying vec3 texCoords; vec2 To2DCoords(vec3 coords) { float u = coords.x + 3DTextureSize.x*(coords.z - 2DTextureSize.x *floor(coords.z/2DTextureSize.x)); float v = coords.y + 3DTextureSize.y*floor(coords.x/2DTextureSize.x); return vec2(u,v); } void main() { gl_FragColor = texture2D(s_texture,To2DCoords(texCoords)); } 

The To2DCoords method is inspired by the algorithm found at: http://http.developer.nvidia.com/GPUGems3/gpugems3_ch29.html

The problem is that during visualization each of them got messed up, comparing it with a 3D texture. So what am I doing wrong?

+4
source share
1 answer

According to your code, the input and output of To2DCoords () should be in pixel coordinates (0 ~ 255), and not in texture coordinates (0 ~ 1,0) if the texture size is 256x256, for example.

Your code should look like this:

 gl_FragColor = texture2D(s_texture,To2DCoords(texCoords*3DTextureSize)/2DTextureSize); 
0
source

Source: https://habr.com/ru/post/1398234/


All Articles