ofVboMesh and draw(...)

Hi forum,

I am getting error if i try to call the draw(…) function of the ofVboMesh class . The function takes the ofPrimitiveMode as a function input parameter and i am using OF_PRIMITIVE_TRIANGLES. Lets skim through the following snippet that i am using

.....................
   ofEasyCam cam;
   ofMesh mesh;
   ofVboMesh vboMesh;
   ofImage img;
};


//--------------------------------------------------------------
void ofApp::setup()
{
   ofSetFrameRate(0);
   ofSetVerticalSync(true);

   
   if(!loadShaders())
   {
      OF_EXIT_APP(1);
   }
   else
   {
      //1
      mesh.addVertex(ofVec3f(-0.25f,  0.25f, -0.25f));
      mesh.addVertex(ofVec3f(-0.25f, -0.25f, -0.25f));
      mesh.addVertex(ofVec3f( 0.25f, -0.25f, -0.25f));

      //2
      mesh.addVertex(ofVec3f( 0.25f, -0.25f, -0.25f));
      mesh.addVertex(ofVec3f( 0.25f,  0.25f, -0.25f));
      mesh.addVertex(ofVec3f( -0.25f,  0.25f, -0.25f));

      //3
      mesh.addVertex(ofVec3f( 0.25f, -0.25f, -0.25f));
      mesh.addVertex(ofVec3f( 0.25f, -0.25f,  0.25f));
      mesh.addVertex(ofVec3f( 0.25f,  0.25f, -0.25f));

      //4
      mesh.addVertex(ofVec3f( 0.25f, -0.25f,  0.25f));
      mesh.addVertex(ofVec3f( 0.25f,  0.25f,  0.25f));
      mesh.addVertex(ofVec3f( 0.25f,  0.25f, - 0.25f));

      //5
      mesh.addVertex(ofVec3f( 0.25f, -0.25f,  0.25f));
      mesh.addVertex(ofVec3f( -0.25f, -0.25f,  0.25f));
      mesh.addVertex(ofVec3f( 0.25f,  0.25f,  0.25f));

      //6
      mesh.addVertex(ofVec3f( -0.25f, -0.25f,  0.25f));
      mesh.addVertex(ofVec3f( -0.25f,  0.25f,  0.25f));
      mesh.addVertex(ofVec3f( 0.25f,  0.25f,  0.25f ));

      //7
      mesh.addVertex(ofVec3f( -0.25f, -0.25f,  0.25f ));
      mesh.addVertex(ofVec3f( -0.25f, -0.25f, -0.25f ));
      mesh.addVertex(ofVec3f( -0.25f,  0.25f,  0.25f ));

      //8
      mesh.addVertex(ofVec3f( -0.25f, -0.25f, -0.25f ));
      mesh.addVertex(ofVec3f( -0.25f,  0.25f, -0.25f ));
      mesh.addVertex(ofVec3f( -0.25f,  0.25f,  0.25f ));

      //9
      mesh.addVertex(ofVec3f( -0.25f, -0.25f,  0.25f ));
      mesh.addVertex(ofVec3f( 0.25f, -0.25f,  0.25f ));
      mesh.addVertex(ofVec3f( 0.25f, -0.25f, -0.25f ));

      //10
      mesh.addVertex(ofVec3f( 0.25f, -0.25f, -0.25f ));
      mesh.addVertex(ofVec3f( -0.25f, -0.25f, -0.25f ));
      mesh.addVertex(ofVec3f( -0.25f, -0.25f,  0.25f ));

      //11
      mesh.addVertex(ofVec3f( -0.25f,  0.25f, -0.25f ));
      mesh.addVertex(ofVec3f( 0.25f,  0.25f, -0.25f ));
      mesh.addVertex(ofVec3f( 0.25f,  0.25f,  0.25f ));      
      
      //12
      mesh.addVertex(ofVec3f( 0.25f,  0.25f,  0.25f ));
      mesh.addVertex(ofVec3f( -0.25f,  0.25f,  0.25f ));
      mesh.addVertex(ofVec3f( -0.25f,  0.25f, -0.25f ));             
      
      
   }      
}



void ofApp::draw()
{
   static const GLfloat green[] = {0.0f,0.25f,0.0f,1.0f};
   static const GLfloat one = 1.0f;

   glClearBufferfv(GL_COLOR,0,green);
   glClearBufferfv(GL_DEPTH,0,&one);

   float currentTime = ofGetElapsedTimef() * 0.3f;

   for(unsigned int i = 0; i < 24; ++i)
   {
      shader.begin();      
      ofRotateX(currentTime * 81.0);
      ofRotateY(currentTime * 45.0);
      ofTranslate(sinf(2.1f * currentTime) * 2.0f,
		  cosf(1.7f * currentTime) * 2.0f,
		  sinf(1.3f * currentTime) * cosf(1.5f * currentTime) * 2.0f);
      ofTranslate(0.0f,0.0f,-6.0f);

      mesh.draw(OF_PRIMITIVE_TRIANGLES);
      shader.end();
   }   
}

I am having the error at mesh.draw(OF_PRIMITIVE_TRIANGLES);

It says it expects 0 arguments. But if i check the documentation it shows that the draw() function of ofVboMesh takes one arguments. Any hint ?

Thanks

The mesh object your are calling is the ofMesh you defined, not the ofVboMesh. The standard ofMesh takes 0 arguments in it’s draw() method. Since ofVboMesh extends ofMesh, you can replace all of your mesh.addVertex(...) with vboMesh.addVertex(...), and it should work.

Also, I don’t see where you are loading your shaders. Did you omit that part?

Thanks for the hint!

I did the necessary changes as you suggested. Now i manage to get rid of the errors i used to get. But i do not get cubes animating around the scene . Instead i get a bunch of the tiny points around the screen. And here goes the snippet to load the shaders

bool ofApp::loadShaders()
{
   bool shaderLoaded = false;
   
   //now load the shaders
   if(ofIsGLProgrammableRenderer())
   {
      shader.setupShaderFromFile(GL_VERTEX_SHADER,"shaders/shader-spinnycube.vert");
      shader.setupShaderFromFile(GL_FRAGMENT_SHADER,"shaders/shader-spinnycube.frag");

      shader.linkProgram();
      shaderLoaded = true;
   }
   else
   {
      std::cout << "Programmable Shading is not supported. " << std::endl;
      shaderLoaded = false;
   }

   return shaderLoaded;
}

Check the following screen-shot

http://imgur.com/4Pf4VyB

The points are so tiny that they are hardly visible.

I think you should probably revisit some of the basic tutorials:

http://openframeworks.cc/tutorials/

I suspect the objects are so far away because you haven’t set the distance with the ofEasyCam::setDistance() method, nor have you used the camera in your draw() method. When you are translating and rotating, it needs to be drawn between ofPushMatrix(); and ofPopMatrix(); Like this:

  ofPushMatrix();
  ofRotateX(currentTime * 81.0);
  ofRotateY(currentTime * 45.0);
  ofTranslate(sinf(2.1f * currentTime) * 2.0f,
	  cosf(1.7f * currentTime) * 2.0f,
	  sinf(1.3f * currentTime) * cosf(1.5f * currentTime) * 2.0f);
  ofTranslate(0.0f,0.0f,-6.0f);
  ofPopMatrix();

Depending on the desired effect, you could have each rotation and translation call between it’s own set of ofPushMatrix(); and ofPopMatrix();

Hi @section14

I did the suggested changes but i still the blank screen. Then i turned to one of the examples with some code snippet

http://openframeworks.cc/documentation/gl/ofVbo.html

There i can see that each of the vertices are multiplied 100 otherwise i do not see anything on screen , the very same scenario that i am having now with my own simple vbo application.

It will be nice to have some explanation over the alternation of the vertices position.

Now lets back to the initial problem that i am having. What i am trying to achieve is a simple re-creation of an raw OpenGL application and they are not altering the vertex position (i mean the scaling) at all and they get the spinning cube. Some hint to debug this issue?

The changes that you suggested are mentioned in the following snippet:

1.Setting the projection matrix

//--------------------------------------------------------------
void ofApp::windowResized(int w, int h)
{
   ofSetupScreenPerspective(w,h,50.0,0.1f,1000.f);
} 

The updated draw function

//--------------------------------------------------------------
void ofApp::draw()
{
   static const GLfloat green[] = {0.0f,0.25f,0.0f,1.0f};
   static const GLfloat one = 1.0f;

   glClearBufferfv(GL_COLOR,0,green);
   glClearBufferfv(GL_DEPTH,0,&one);

   ofTranslate(ofGetWidth()/2,ofGetHeight()/2,100);

   float currentTime = ofGetElapsedTimef() * 0.3f;// + (float)i;
      
      shader.begin();

      ofScale(100.0f,100.0f,100.0f);
      
      ofPushMatrix();
      ofRotateX(currentTime * 81.0);
      ofPushMatrix();
      ofRotateY(currentTime * 45.0);

      ofPushMatrix();
      ofTranslate(sinf(2.1f * currentTime) * 2.0f,
		  cosf(1.7f * currentTime) * 2.0f,
		  sinf(1.3f * currentTime) * cosf(1.5f * currentTime) * 2.0f);
      
      ofPushMatrix();
      ofTranslate(0.0f,0.0f,-6.0f);
      vbo.draw(GL_TRIANGLES,0,12);
      ofPopMatrix();

      ofPopMatrix();

      ofPopMatrix();
      
      ofPopMatrix();

      shader.end();
   
}

Thanks