webgl glsl模拟texture3d

时间:2013-01-04 04:07:27

标签: opengl-es glsl textures webgl voxel

我正在将一块opengl移植到webgl,我正在尝试模拟texture3d。某种程度上出了问题。

不需要插值,因为它仅用于计算。我不确定原始代码的几何部分,现在通过纹理获取每层属性。

更新:好的我重写了纹理3d功能,我还是遇到了一些问题:

const vec3 volumeTextureSize = vec3( 256.0, 128.0, 32.0 );
const vec2 imageSize = vec2( 1024.0, 1024.0 );

vec2 uvFromUvw( vec3 uvw ) {
    const vec3 size = volumeTextureSize;
    const vec2 layersPerDim = size.xy / imageSize.xy;
    const vec2 pixelsPerLayer = imageSize / layersPerDim;

    // normalized in
    float layer = floor(uvw.z * size.z);

    float layerX = mod(layer, layersPerDim.x);
    float layerY = floor(layer / layersPerDim.x);

    vec2 layerUv = vec2(layerX, layerY) * pixelsPerLayer;
    vec2 layerSpaceUv = (uvw.xy * size.xy) / layersPerDim;

    vec2 uv = layerSpaceUv + layerUv;

    uv /= imageSize;

    return uv;
}

vec4 texture3D( sampler2D tex, vec3 uvw ) {
    vec2 uv = uvFromUvw( uvw );
    return texture2D(tex, uv);
}

vec3 uvwFromUv( vec2 uv ) {
    const vec3 size = volumeTextureSize;
    const vec2 layersPerDim =  imageSize.xy / size.xy;
    const vec2 pixelsPerLayer = imageSize / layersPerDim;

    vec2 normUv = uv / imageSize;

    vec2 layerUv = floor(vec2(normUv * layersPerDim.xy));

    vec3 uvw = vec3(0.0);

    uvw.z = floor(layerUv.x + floor( layerUv.y * layersPerDim.x));
    uvw.xy = uv - (layerUv * pixelsPerLayer.xy);

    // normalized coords
    // uvw.xy /= size.xy;
    uvw.z /= layersPerDim.x * layersPerDim.y;

    return uvw;
}

这是我要移植的原始代码:

glActiveTexture(GL_TEXTURE0 + deltaJUnit);
glGenTextures(1, &deltaJTexture);
glBindTexture(GL_TEXTURE_3D, deltaJTexture);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB16F_ARB, RES_MU_S * RES_NU, RES_MU, RES_R, 0, GL_RGB, GL_FLOAT, NULL);



    glFramebufferTextureEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, deltaJTexture, 0);

    for (int layer = 0; layer < RES_R; ++layer) {
        setLayer(jProg, layer);
        .. also set uniforms
        drawQuad();
    }

和glsl

#ifdef _VERTEX_

void main() {
    gl_Position = gl_Vertex;
}

#endif

#ifdef _GEOMETRY_
#extension GL_EXT_geometry_shader4 : enable

void main() {
    gl_Position = gl_PositionIn[0];
    gl_Layer = layer;
    EmitVertex();
    gl_Position = gl_PositionIn[1];
    gl_Layer = layer;
    EmitVertex();
    gl_Position = gl_PositionIn[2];
    gl_Layer = layer;
    EmitVertex();
    EndPrimitive();
}

#endif

0 个答案:

没有答案