GL_UNIFORM_BUFFER and OF

Hi Forum,

is there any way to create uniform buffer with OF as follows:

glGenBuffers(1, &rain_buffer);
glBindBuffer(GL_UNIFORM_BUFFER, rain_buffer);
glBufferData(GL_UNIFORM_BUFFER, 256 * sizeof(vmath::vec4), NULL, GL_DYNAMIC_DRAW);

I guess that i have edit the ofVbo class to arrange this. Any suggestion from the community ?

Thanks

there’s an OF addon to use uniform buffers: https://github.com/Ahbee/ofxUbo

1 Like

Hi ,

I tried to run all the examples that came along with the plugin and all of them failed. The first one is example_BasicUsage and i get the following compilation error:

Ubo/src/ofxUbo.cpp
../../../addons/ofxUbo/src/ofxUbo.cpp: In static member function ‘static GLuint ofxUboSingeltons::findFirstAvaliableBindingSpot()’:
../../../addons/ofxUbo/src/ofxUbo.cpp:189:48: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < bindingPointSlots.size(); i++) {
                                                ^
../../../addons/ofxUbo/src/ofxUbo.cpp: In function ‘std::ostream& operator<<(std::ostream&, const ofxUboLayout&)’:
../../../addons/ofxUbo/src/ofxUbo.cpp:203:49: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < layout.uniformData.size(); i++) {
                                                 ^
../../../addons/ofxUbo/src/ofxUbo.cpp: In copy constructor ‘ofxUboShader::ofxUboShader(const ofxUboShader&)’:
../../../addons/ofxUbo/src/ofxUbo.cpp:231:10: warning: ‘auto’ changes meaning in C++11; please remove it [-Wc++0x-compat]
     for (auto it = ubos.begin(); it!=ubos.end(); it++) {
          ^
../../../addons/ofxUbo/src/ofxUbo.cpp:231:15: error: ‘it’ does not name a type
     for (auto it = ubos.begin(); it!=ubos.end(); it++) {
               ^
../../../addons/ofxUbo/src/ofxUbo.cpp:231:34: error: expected ‘;’ before ‘it’
     for (auto it = ubos.begin(); it!=ubos.end(); it++) {
                                  ^
../../../addons/ofxUbo/src/ofxUbo.cpp:231:34: error: ‘it’ was not declared in this scope
../../../addons/ofxUbo/src/ofxUbo.cpp: In member function ‘ofxUboShader& ofxUboShader::operator=(const ofxUboShader&)’:
../../../addons/ofxUbo/src/ofxUbo.cpp:243:10: warning: ‘auto’ changes meaning in C++11; please remove it [-Wc++0x-compat]
     for (auto it = ubos.begin(); it!=ubos.end(); it++) {
          ^
../../../addons/ofxUbo/src/ofxUbo.cpp:243:15: error: ‘it’ does not name a type
     for (auto it = ubos.begin(); it!=ubos.end(); it++) {
               ^
../../../addons/ofxUbo/src/ofxUbo.cpp:243:34: error: expected ‘;’ before ‘it’
     for (auto it = ubos.begin(); it!=ubos.end(); it++) {
                                  ^
../../../addons/ofxUbo/src/ofxUbo.cpp:243:34: error: ‘it’ was not declared in this scope
../../../addons/ofxUbo/src/ofxUbo.cpp: In member function ‘ofxUboLayout ofxUboShader::getLayout(const string&)’:
../../../addons/ofxUbo/src/ofxUbo.cpp:295:20: warning: unused variable ‘index’ [-Wunused-variable]
     int actualLen, index, uniType,uniSize, uniMatStride, uniArrayStride, uniOffset;
                    ^
make[1]: *** [../../../addons/obj/linux/Release/ofxUbo/src/ofxUbo.o] Error 1
make[1]: Leaving directory `/home/sajjad/Downloads/openFrameworks/master/openFrameworks-master/examples/addons/example_BasicUsage'
make: *** [Release] Error 2

And the next one is example_lights. I got the similar error:

src/ofxUbo.o -c ../../../addons/ofxUbo/src/ofxUbo.cpp
../../../addons/ofxUbo/src/ofxUbo.cpp: In static member function ‘static GLuint ofxUboSingeltons::findFirstAvaliableBindingSpot()’:
../../../addons/ofxUbo/src/ofxUbo.cpp:189:48: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < bindingPointSlots.size(); i++) {
                                                ^
../../../addons/ofxUbo/src/ofxUbo.cpp: In function ‘std::ostream& operator<<(std::ostream&, const ofxUboLayout&)’:
../../../addons/ofxUbo/src/ofxUbo.cpp:203:49: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < layout.uniformData.size(); i++) {
                                                 ^
../../../addons/ofxUbo/src/ofxUbo.cpp: In copy constructor ‘ofxUboShader::ofxUboShader(const ofxUboShader&)’:
../../../addons/ofxUbo/src/ofxUbo.cpp:231:10: warning: ‘auto’ changes meaning in C++11; please remove it [-Wc++0x-compat]
     for (auto it = ubos.begin(); it!=ubos.end(); it++) {
          ^
../../../addons/ofxUbo/src/ofxUbo.cpp:231:15: error: ‘it’ does not name a type
     for (auto it = ubos.begin(); it!=ubos.end(); it++) {
               ^
../../../addons/ofxUbo/src/ofxUbo.cpp:231:34: error: expected ‘;’ before ‘it’
     for (auto it = ubos.begin(); it!=ubos.end(); it++) {
                                  ^
../../../addons/ofxUbo/src/ofxUbo.cpp:231:34: error: ‘it’ was not declared in this scope
../../../addons/ofxUbo/src/ofxUbo.cpp: In member function ‘ofxUboShader& ofxUboShader::operator=(const ofxUboShader&)’:
../../../addons/ofxUbo/src/ofxUbo.cpp:243:10: warning: ‘auto’ changes meaning in C++11; please remove it [-Wc++0x-compat]
     for (auto it = ubos.begin(); it!=ubos.end(); it++) {
          ^
../../../addons/ofxUbo/src/ofxUbo.cpp:243:15: error: ‘it’ does not name a type
     for (auto it = ubos.begin(); it!=ubos.end(); it++) {
               ^
../../../addons/ofxUbo/src/ofxUbo.cpp:243:34: error: expected ‘;’ before ‘it’
     for (auto it = ubos.begin(); it!=ubos.end(); it++) {
                                  ^
../../../addons/ofxUbo/src/ofxUbo.cpp:243:34: error: ‘it’ was not declared in this scope
../../../addons/ofxUbo/src/ofxUbo.cpp: In member function ‘ofxUboLayout ofxUboShader::getLayout(const string&)’:
../../../addons/ofxUbo/src/ofxUbo.cpp:295:20: warning: unused variable ‘index’ [-Wunused-variable]
     int actualLen, index, uniType,uniSize, uniMatStride, uniArrayStride, uniOffset;
                    ^
make[1]: *** [../../../addons/obj/linux/Release/ofxUbo/src/ofxUbo.o] Error 1
make[1]: Leaving directory `/home/sajjad/Downloads/openFrameworks/master/openFrameworks-master/examples/addons/example_lights'
make: *** [Release] Error 2

Where should i look into now ?

Thanks

This seems to be the defining line. Compile with c++11 support. This support can be enabled with the -std=c++11 or -std=gnu++11 compiler options. Or replace auto with the correct iterator type.

Best, V

1 Like

Thanks,

The issue is solved. Now i have going through another one while using ofxUbo to develop a small app.

Please check the following snippet:

struct BlobSettings
{
  ofVec4f InnerColor;
  ofVec4f OuterColor;
  float RadiusInner;
  float RadiusOuter;
};

Then i declare a class as follows:

class ofApp : public ofBaseApp
{
   
public:
   void setup();
   void update();
   void draw();
   
   void keyPressed(int key);
   void keyReleased(int key);
   void mouseMoved(int x, int y );
   void mouseDragged(int x, int y, int button);
   void mousePressed(int x, int y, int button);
   void mouseReleased(int x, int y, int button);
   void windowResized(int w, int h);
   void dragEvent(ofDragInfo dragInfo);
   void gotMessage(ofMessage msg);

private:
   ofxUboShader shader;
   ofVbo vbo;

   BlobSettings blob;
};

Now lets take a look inside the class definition.

void ofApp::setup()
{
   //load the shaders
   //load the programmable shaders
   if(ofIsGLProgrammableRenderer())
   {
      shader.load("shaders/uniformblock");      
   }
   else
   {
      std::cout << "Programmable Shading is not supported. " << std::endl;
      exit();
   }

   //position data
   float positionData[] =
      {
	 -0.8f, -0.8f, 0.0f,1.0f,
	 0.8f, -0.8f, 0.0f,1.0f,
	 0.8f,  0.8f, 0.0f,1.0f,
	 -0.8f, -0.8f, 0.0f,1.0f,
	 0.8f, 0.8f, 0.0f,1.0f,
	 -0.8f, 0.8f, 0.0f,1.0f
      };
   
   float tcData[] =
      {
	 0.0f, 0.0f,
	 1.0f, 0.0f,
	 1.0f, 1.0f,
	 0.0f, 0.0f,
	 1.0f, 1.0f,
	 0.0f, 1.0f
      };

   //set the vertex and texture data for the vertex buffer object
   vbo.setVertexData(&positionData[0],6,24,GL_STATIC_DRAW);
   vbo.setTexCoordData(&tcData[0],12,GL_STATIC_DRAW);

   //pull out the current renderer
   ofPtr<ofBaseRenderer> renderer = ofGetCurrentRenderer();
   //set the blend mode with the current renderer
   renderer->setBlendMode(OF_BLENDMODE_ALPHA);

   //print the layouts for the uniform buffer
   shader.printLayout("BlobSettings");

   //setup the blob value
   blob.InnerColor = ofVec4f(0.0f,0.0f,0.0f,0.0f);
   blob.OuterColor = ofVec4f(1.0f,1.0f,0.75f,1.0f);
   blob.RadiusInner = 0.25f;
   blob.RadiusOuter = 0.45f;
   
}

At last the draw function:

void ofApp::draw()
{
   
   shader.begin();
   shader.setUniformBuffer("BlobSettings", blob);

   //just draw the blob
   vbo.draw(GL_TRIANGLES,0,6);

   shader.end();
   
}

I am having blank screen - probably one of the most common problem anyone encounter when start with something based on OpenGL. Now let get inside the shader:

#version 430 core

in vec4 position;
in vec2 texcoord;
uniform mat4 modelViewProjectionMatrix;

out vec2 TexCoord;

void main()
{
    TexCoord = texcoord;
    gl_Position = modelViewProjectionMatrix * position;
}

And the fragment shader:

#version 430 core

in vec2 TexCoord;

out vec4 outputColor;

uniform BlobSettings
{
  vec4 InnerColor;
  vec4 OuterColor;
  float RadiusInner;
  float RadiusOuter;
};

void main()
{
    float dx = TexCoord.x - 0.5;
    float dy = TexCoord.y - 0.5;

    float dist = sqrt(dx * dx + dy * dy);
    outputColor =
       mix( InnerColor, OuterColor,
             smoothstep( RadiusInner, RadiusOuter, dist )
            );
}

What am i missing folks? Some hint will help me to debug this type of issue.

Thanks

setVetrexData and setTexCoordData are wrong, should be

 vbo.setVertexData(&positionData[0], 4, 6, GL_STATIC_DRAW,sizeof(ofVec4f));
 vbo.setTexCoordData(&tcData[0],6,GL_STATIC_DRAW,sizeof(ofVec2f));

Also you did not change the Projection Matrix to represent your coordinate system. So in in the shader you probably want this, Dont multiply by anything.

gl_Position = position;
1 Like