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 8 of 8

Thread: Dynamic arrays on GLSL

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2017
    Posts
    26

    Dynamic arrays on GLSL

    I have worked with C++/OpenSceneGraph/GLSL integration and I need to handle dynamic array on shader.

    My dynamic data array of vec3 was converted into 1D-texture to pass as uniform to fragment (I'm using GLSL 1.3), as follows:
    Code :
        osg::ref_ptr<osg::Image> image = new osg::Image; 
        image->setImage(myVec3Array->size(), 1, 1, GL_RGBA8, GL_RGBA, GL_FLOAT, (unsigned char*) &myVec3Array[0], osg::Image::NO_DELETE);
     
        // Pass the texture to GLSL as uniform
        osg::StateSet* ss = scene->getOrCreateStateSet();
        ss->addUniform( new osg::Uniform("vertexMap", texture) );

    For now, I would like to retrieve my raw array of vec3 on fragment shader. How can I do this process? Does a texture2D function only return normalized values?

    Thanks in advance.

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,928
    Quote Originally Posted by romulogcerqueira View Post
    For now, I would like to retrieve my raw array of vec3 on fragment shader. How can I do this process? Does a texture2D function only return normalized values?
    If you want to store floating-point values directly, the texture needs to have a floating-point internal format (e.g. GL_RGB32F). Floating-point textures require OpenGL 3.0 (which you should already have if you're using GLSL 1.3) or the ARB_texture_float extension.

    If you don't need the full range and precision of IEEE-754 single-precision floats, you can use half-precision floats (e.g. GL_RGB16F), which have a 5-bit exponent and 10-bit significand.

    If you want constant absolute error (rather than relative error), then you can use a normalised format or an integer format and perform the scaling in the fragment shader.

  3. #3
    Junior Member Newbie
    Join Date
    Dec 2017
    Posts
    26
    Hi GClements,

    I will can handle the floating-point textures on C++/OSG side, however I still have a question: how can I access this floating-point values on shader side? Could you give me an example?

    Thanks in advance.

  4. #4
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,928
    Quote Originally Posted by romulogcerqueira View Post
    I will can handle the floating-point textures on C++/OSG side, however I still have a question: how can I access this floating-point values on shader side? Could you give me an example?
    Just use texture2D() etc. The values won't be limited to [0,1] if the texture has a floating-point internal format.

  5. #5
    Junior Member Newbie
    Join Date
    Dec 2017
    Posts
    26
    Thanks, GClements.

  6. #6
    Junior Member Newbie
    Join Date
    Dec 2017
    Posts
    26
    GClements,

    I'm facing with texture coordinate problem. I passed the floating matrix of vec3 as texture to shader, however I would like to retrieve these values there . Could you help me?

  7. #7
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,928
    Quote Originally Posted by romulogcerqueira View Post
    I'm facing with texture coordinate problem. I passed the floating matrix of vec3 as texture to shader, however I would like to retrieve these values there.
    If you're using a texture as an array, you should probably use texelFetch() to retrieve values. That avoids filtering and the need to convert indices to normalised texture coordinates.

  8. #8
    Junior Member Newbie
    Join Date
    Dec 2017
    Posts
    26
    It worked, GClements. Thanks!

Posting Permissions

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