Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: texture to texture array

  1. #1
    Intern Newbie
    Join Date
    Dec 2011
    Posts
    40

    Question texture to texture array

    Hi,

    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 http://www.opengl.org/wiki/Array_Texture 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.

  2. #2
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    383
    Quote Originally Posted by glararan View Post
    I don't know how to use textureOffset(..) (left, right, top, bottom) in sampler2DArray
    The texture functions work the same as for sampler2D, except that the coordinates contain an extra component to select the layer. For textureOffset, the offset parameter is still an ivec2 (i.e. the offset only applies to selection of texels within a layer, not the selection of a layer).

    Quote Originally Posted by glararan View Post
    I read http://www.opengl.org/wiki/Array_Texture 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.
    That just specifies that the layer is rounded to the nearest integer then clamped. Typically, the layer is an integer; although it may have been passed via a floating-point attribute, you wouldn't normally interpolate it.

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
  •