Results 1 to 4 of 4

Thread: not able to send array using uniform buffer object

  1. #1
    Junior Member Newbie
    Join Date
    Sep 2012
    Posts
    9

    not able to send array using uniform buffer object

    Hi,
    I'm trying to send an array to the fragment shader via a uniform buffer object, but it seems that the fragment shader is not receiving the values of the array.
    The idea is that the fragment shader receives an array, and, depending on the values on the array, polygons are colored red or blue.
    In each frame the values on the array change; I also expect that the colors on the polygons change. Since this is not the case, I'm guessing that the problem is how do I send the array via the uniform buffer object.

    Could you please have a look at what is wrong in this code?
    The relevant code is commented as: //uniforn buffer object//

    Code :
    class World {
    private:
        glm::vec2 ballsCenter[500];
    ...
    }

    //executed once
    Code :
    void World::initOpengl() {
     
        GLuint buffer[2];
        glGenBuffers(2                  , buffer );
     
        ...
     
        //uniforn buffer object// 
        glBindBuffer(GL_UNIFORM_BUFFER, buffer[1]);
        glBufferData(GL_UNIFORM_BUFFER, sizeof(ballsCenter),NULL, GL_DYNAMIC_DRAW);
     
        programID = InitShader( "vshader32.glsl", "fshader32.glsl" );
        glUseProgram( programID );
     
        ...
     
        //uniforn buffer object// 
        GLuint myArrayBlockIdx = glGetUniformBlockIndex (programID, "BallsCenter");
        glUniformBlockBinding(programID, myArrayBlockIdx, 0);
        glBindBufferBase(GL_UNIFORM_BUFFER, 0, buffer[1]);
     
        ...
    }

    //each frame
    Code :
    int World::render() {
        ...
        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
     
        glUniformMatrix4fv(orthoID, 1, GL_FALSE, &control.orthoProjection[0][0]);
     
        moveParticles();
        for(int32 index=0; index<particlesCount; ++index) {
            const float32 xBall = particle[index].x;
            const float32 yBall = particle[index].y;
     
            ballCenter[index] = glm::vec2(xBall, yBall);
            ...
        }
     
        //uniforn buffer object// 
        glBufferSubData( GL_UNIFORM_BUFFER, 0, sizeof(ballCenter) *particlesCount       ,ballCenter);
        glDrawArrays(GL_TRIANGLES, 0, particlesCount*6);
     
        ...
     
        glfwSwapBuffers();
     
        return 0;
    }

    //Vertex shader
    Code :
    #version 400
    in  vec2 vPosition;
     
    uniform mat4 ortho;
     
    void main() {
        gl_Position =ortho * vec4(vPosition.x, vPosition.y ,0.0, 1.0);
    }


    //Fragment shader
    Code :
    #version 400
    in  float color;
    uniform float resolutionHeight;
    uniform float resolutionWidth;
    uniform mat4 ortho;
     
    //uniforn buffer object// 
    layout (std140) uniform BallsCenter {
       vec2 ballsCenter[500];
    };
     
     
    void main() {
        float threshold = 1.0;
        bool red=false;
        for(int i=0; i<50; ++i){
            vec4 centerV4= ortho * vec4(ballsCenter[i],0,1);
     
            //uniforn buffer object//
     
            float dist = length( centerV4.xy - vec2(gl_FragCoord.x/resolutionWidth,gl_FragCoord.y/resolutionHeight) );
            if (dist < threshold){
                red=true;
                break;
            }
        }
     
        if(red)
            gl_FragColor = vec4(1,0,0,1);	//red
        else
            gl_FragColor = vec4(0,0,1,1);	//blue
    }

  2. #2
    Junior Member Newbie
    Join Date
    Sep 2012
    Posts
    9
    bump ...

  3. #3
    Member Contributor Brokenmind's Avatar
    Join Date
    Feb 2014
    Location
    Aachen / Germany
    Posts
    78
    Unfortunately, I'm not familiar with uniform buffer objects, but I have a different approach that could work for you:

    With what I coded so far, I found out that shaders seem to have a problem with large arrays of data transmitted to them. Large in this case means something between 170 and 250, where my shaders stop compiling. The solution is storing the floats in textures and interpreting its pixels not as colors, but as uniform information to be read with some other variable (in my case gl_instanceID). Maybe you could try to lower your array size until it works (to find out the restrictions and whether my assumption is correct) and switch to using a texture instead.

  4. #4
    Junior Member Newbie
    Join Date
    Sep 2012
    Posts
    9
    Thanks for the prompt reply; On Arcsynthesis there's a good tutorial about it.

Similar Threads

  1. Sending an array of structs to shader via an Uniform Buffer Object
    By cevapanda in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 09-19-2016, 10:38 AM
  2. how to send vertices array using buffer
    By shinx200 in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 04-15-2015, 06:03 AM
  3. Problem with uniform buffer array and older AMD hardware
    By Nikki_k in forum OpenGL: Advanced Coding
    Replies: 0
    Last Post: 11-08-2014, 02:04 AM
  4. AMD: Uniform buffer with array inside
    By kozmo in forum OpenGL: Drivers
    Replies: 0
    Last Post: 09-09-2011, 05:48 AM
  5. Sharing an uniform buffer across a block array.
    By imported_Groovounet in forum OpenGL: Advanced Coding
    Replies: 9
    Last Post: 09-02-2010, 09:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean