• Advertisement
Sign in to follow this  

help with vbo please.

This topic is 3475 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi, i have a sphere, 65536 faces,65536*4 vertexs, 65536*4*3 coordinates(x,y,s) My FPS in an empty space(it means nothing to render) is 130fps. If a render the sphere with glcalllist, and GL_QUADS, it give me 20-30 fps. I think this is very very slow. Someone in a forum said that i need to do the render with VBO. This is what i do:
#include "allegro.h"
#include "alleggl.h"
#include <math.h>

GLuint vertexBuffer = 0;
GLuint colourBuffer = 0;
float *vector;//vertex of the sphere
float *colors;//colors of the sphere
FONT *agl_font;
int fps_ant=0;

void reshape(GLsizei w, GLsizei h); 
void make_sphere();//generate sphere vertices and colors, fill "vector" with vertex and "colors" with colors
void show_fps(int a);//show FPS
                            
void vbo()
   {  
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);        
      glColor3f(255.0,255.0,255.0);
                  
      glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertexBuffer);              
      glVertexPointer(3, GL_FLOAT, 0, 0);              
      glBindBufferARB(GL_ARRAY_BUFFER_ARB, colourBuffer);              
      glColorPointer (3, GL_FLOAT, 0, 0);              
      glDrawArrays(GL_QUADS, 0,65536*4);
      
      show_fps(fps_ant);            
      glFlush();     
      allegro_gl_flip();          
   }   

void main()
   {
      allegro_init();      
      install_allegro_gl();
      install_keyboard();      
      
      allegro_gl_set( AGL_COLOR_DEPTH, desktop_color_depth() );
      allegro_gl_set( AGL_Z_DEPTH, 16 );
      allegro_gl_set( AGL_DOUBLEBUFFER, 1);
      allegro_gl_set( AGL_RENDERMETHOD, 1);
      allegro_gl_set( AGL_FULLSCREEN, 1); 
      allegro_gl_set( AGL_STENCIL_DEPTH,0); 
      allegro_gl_set( AGL_REQUIRE,AGL_COLOR_DEPTH );
      allegro_gl_set( AGL_SUGGEST,AGL_Z_DEPTH); 
      allegro_gl_set( AGL_REQUIRE,AGL_DOUBLEBUFFER); 
      allegro_gl_set( AGL_REQUIRE,AGL_RENDERMETHOD); 
      allegro_gl_set( AGL_REQUIRE,AGL_FULLSCREEN);
      allegro_gl_set( AGL_SUGGEST,AGL_STENCIL_DEPTH);
      set_gfx_mode(GFX_OPENGL_FULLSCREEN, 1280,800 , 0, 0);
      
      agl_font = allegro_gl_load_system_font("C:\WINDOWS\Fonts\arial.ttf", AGL_FONT_TYPE_OUTLINE,1,1); 
      
      //fill vector with the vertex of the sphere( 4 vertex for each face of the sphere)
      //fill colors of each vertex of the sphere
      make_sphere();           
      
      reshape(SCREEN_W,SCREEN_H);      
      
      glGenBuffersARB(1, &vertexBuffer);
      glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertexBuffer);    
      glBufferDataARB(GL_ARRAY_BUFFER_ARB, 65536*4*3*4, vector, GL_STATIC_DRAW_ARB);
    
      glGenBuffersARB(1, &colourBuffer);
      glBindBufferARB(GL_ARRAY_BUFFER_ARB, colourBuffer);
      glBufferDataARB(GL_ARRAY_BUFFER_ARB, 65536*4*3*4, colors, GL_STATIC_DRAW_ARB);
    
      //  65536*4*3*4  this number is because:
          
      //  65536 : faces of the spheres
      //  4     : vertex of each face
      //  3     : coordinates per vertex
      //  4     : sizeof(vector)
      glEnableClientState(GL_VERTEX_ARRAY);
      glEnableClientState(GL_COLOR_ARRAY);      
      
      clock_t fps_t=clock();
      bool fps=false;
      int fps_f=0;      
      glEnable(GL_DEPTH_TEST);
      glDepthFunc(GL_LEQUAL);      
      int a=-20;
      do
         {  //calculate FPS        
            if((clock()-fps_t)/(double)CLOCKS_PER_SEC>1)
               {     
                  fps_ant=fps_f;         
                  fps_t=clock();
                  fps_f=0;
               }
            fps_f++;               
            
            //MOVE CAMERA Z OUT        
            if(key[KEY_W])
               a++;
               
            //MOVE CAMERA Z IN           
            if(key[KEY_S])
               a--;
               
            //SHOW SPHERE   
            glLoadIdentity();
            glTranslatef(0,0,a);      
            vbo();            
            
         }while(!key[KEY_ESC]);      
      
      free(vector);
      free(colors);
   }END_OF_MAIN();   


void reshape(GLsizei w, GLsizei h) 
   {     
      glMatrixMode (GL_PROJECTION);                  
      glLoadIdentity ();                            
      glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5f, 10000.0f); 
      glMatrixMode (GL_MODELVIEW);                   
      glViewport (0, 0, w, h);                       
   }

void make_sphere()
   {  
      float ratio=10;
      int pa=256;
      int me=256;      
      int nro_lados=pa*me;//65536 faces            
      vector = (float *) malloc (12*nro_lados*sizeof (float));
      colors = (float *) malloc (12*nro_lados*sizeof (float));
               
      //                __________
      //  (v2x,v2y,v2z)|          |(v3x,v3y,v3z)
      //               |          |
      //               |Sphere´s  |
      //               |face      |
      //  (v1x,v1y,v1z)|__________|(v4x,v4y,v4z)                                               

      
      float v1x,v1y,v1z,v2x,v2y,v2z,v3x,v3y,v3z,v4x,v4y,v4z,x1,x2,x3,x4,y1,y2,z1,z2,z3,z4;
      for(int i=0;i<pa;++i)
         for(int j=0;j<me;++j)
            {  
               //generate sphere vertexs/////////////////////////////////////////////               
               y1=2*ratio/pa*i-ratio;
               y2=2*ratio/pa*(i+1)-ratio;
               v1y=y1;  v2y=y2;  v3y=y2;  v4y=y1;               
                              
               x1=sin(360.0/me*j*3.14159265358979/180.0)*sqrt(ratio*ratio-y1*y1);
               x2=sin(360.0/me*j*3.14159265358979/180.0)*sqrt(ratio*ratio-y2*y2);
               x3=sin(360.0/me*(j+1)*3.14159265358979/180.0)*sqrt(ratio*ratio-y1*y1);
               x4=sin(360.0/me*(j+1)*3.14159265358979/180.0)*sqrt(ratio*ratio-y2*y2);
               v1x=x1;  v2x=x2;  v3x=x4;  v4x=x3;
               
               z1=cos(360.0/me*j*3.14159265358979/180.0)*sqrt(ratio*ratio-y1*y1);
               z2=cos(360.0/me*j*3.14159265358979/180.0)*sqrt(ratio*ratio-y2*y2);
               z3=cos(360.0/me*(j+1)*3.14159265358979/180.0)*sqrt(ratio*ratio-y1*y1);
               z4=cos(360.0/me*(j+1)*3.14159265358979/180.0)*sqrt(ratio*ratio-y2*y2);
               v1z=z1;  v2z=z2;  v3z=z4;  v4z=z3;
               //////////////////////////////////////////////////////////////////////
               
                           
               //fill "vector" with sphere's vertexs
               vector[0+12*(i*me+j)] =v1x;   vector[ 1+12*(i*me+j)] =v1y;   vector[ 2+12*(i*me+j)] =v1z;
               vector[3+12*(i*me+j)] =v2x;   vector[ 4+12*(i*me+j)] =v2y;   vector[ 5+12*(i*me+j)] =v2z;
               vector[6+12*(i*me+j)] =v3x;   vector[ 7+12*(i*me+j)] =v3y;   vector[ 8+12*(i*me+j)] =v3z;
               vector[9+12*(i*me+j)] =v4x;   vector[10+12*(i*me+j)] =v4y;   vector[11+12*(i*me+j)] =v4z;               
            }
        //fill "color" of each vertex of each face    
        for(int i=0;i<12*nro_lados;++i)   
           {
              colors[0+i]=0.f; colors[ 1+i]=0.f; colors[ 2+i]=1.f;
              colors[3+i]=0.f; colors[ 4+i]=1.f; colors[ 5+i]=0.f;
              colors[6+i]=1.f; colors[ 7+i]=0.f; colors[ 8+i]=0.f;
              colors[9+i]=1.f; colors[10+i]=0.f; colors[11+i]=1.f;              
              i=i+11;            
           }        
   }
void show_fps(int a)
   {   
       glPushMatrix();
       glLoadIdentity();       
       glTranslatef(-6,5,-10);
       allegro_gl_printf(agl_font,0.0 , 0.0, 0.0, makecol( 255, 255, 000), "FPS=%d",a);       
       glPopMatrix();
   }   
This, for my surprise, give me 2 FPS. What i am doing wrong? Please help. BYE. pd: i put the code beetween "[code and /code]"

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement