Part of the Khronos Group

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 10 of 11

Thread: texture to texture array

Threaded View

  1. #1
    Intern Newbie
    Join Date
    Dec 2011

    Question texture to texture array


    I need advice about sampler2DArray. On OpenGL IRC I spoke about fast loading textures throught uniforms. Guys direct me to glTexStorage3D for 2D textures.

    Now I'm trouble with porting shaders from sampler2D to sampler2DArray.

    Tessellation ev. shader before:
    Code :
    #version 400
    layout(quads, fractional_even_spacing, cw) in;
    in terrainVertex
        vec2 position;
    } In[];
    out worldVertex
        vec4 worldPosition;
        vec3 worldNormal;
        vec4 position;
        vec3 normal;
        vec2 texCoords;
    } Out;
    uniform sampler2D heightMap;
    // The number of triangles created per height-map texel
    uniform int maxTrianglesPerTexel = 10;
    // Distance between each tessellation point at max tess level
    uniform float horizontalScale = 10.0;
    // Transformation matrices
    uniform mat4 modelMatrix;
    uniform mat4 modelViewMatrix;
    uniform mat3 worldNormalMatrix;
    uniform mat3 normalMatrix;
    uniform mat4 mvp;
    const float maxTessLevel = 64.0;
    void main()
        // Calculate extent of this patch in texture coords [0,1]
        vec2 patchExtent = maxTessLevel / (textureSize(heightMap, 0) * maxTrianglesPerTexel);
        // Calculate the texture coordinates
        Out.texCoords = In[0].position.xy + gl_TessCoord.xy * patchExtent;
        // Calculate the model-space position
        vec4 position;
        position.xz = Out.texCoords * horizontalScale;
        position.y  = texture(heightMap, Out.texCoords).r;
        position.w  = 1.0;
        // Transform the position to world coordinates and to eye space
        Out.worldPosition = modelMatrix * position;
        Out.position      = modelViewMatrix * position;
        // Calculate the normal
        // This could be moved to a one-time pre-process step to create a normal map.
        // This would be a good candidate for a compute shader.
        // For deformable terrain, would need re-generating when terrain is modified
        const ivec3 offset = ivec3(-1, 0, 1); // Texel offsets
        float delta  = 2.0 * horizontalScale / textureSize(heightMap, 0).x; // Horizontal displacement in world coords
        float left   = textureOffset(heightMap, Out.texCoords, offset.xy).r;
        float right  = textureOffset(heightMap, Out.texCoords, offset.zy).r;
        float top    = textureOffset(heightMap, Out.texCoords, offset.yz).r;
        float bottom = textureOffset(heightMap, Out.texCoords, offset.yx).r;
        vec3 x = normalize(vec3(delta, right - left, 0.0));
        vec3 z = normalize(vec3(0.0, top - bottom, delta));
        vec3 n = cross(z, x);
        Out.worldNormal = worldNormalMatrix * n;
        Out.normal      = normalMatrix * n;
        // Transform to clip-space
        gl_Position = mvp * position;

    I don't know how to use textureOffset(..) (left, right, top, bottom) in sampler2DArray, also texture(..) (position.y), textureSize(..) (patchExtent)

    I read so I should use "actual_layer = max(0, min(d​ - 1, floor(layer​ + 0.5)) )" ? And use for in shader? Is this correct way? I don't think it's correct way.

    Thanks for help.
    Last edited by glararan; 09-28-2013 at 06:03 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts