Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9070927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:47:58+00:00 2026-06-16T17:47:58+00:00

I am drawing a lot of triangles quite fast using Vertex Array: void initializeGL()

  • 0

I am drawing a lot of triangles quite fast using Vertex Array:

void initializeGL() { 
   ...
   glEnableClientState(GL_VERTEX_ARRAY);
   glEnableClientState(GL_COLOR_ARRAY);
   glVertexPointer(3, GL_FLOAT, 0, vertices);
   glColorPointer(3,  GL_FLOAT, 0,   colors);
}

void paintGL() {
   ...  
   glDrawElements(GL_TRIANGLES, 3*numTriangles, GL_UNSIGNED_INT, indices);
 }

However I want my rendering to go even faster by using Vertex Buffer Objects (VBO).

Am I right in my understanding that the glVertexPointer() tells the GPU where in the CPU it can fetch the vertices data and then the GPU copy it from this location in the CPU for each paintGL()?

And that using VBO would improve on this by writing the vertices data to the GPU only once?

SInce I am using Qt I try to use the QGLBuffer class:

void GLWidget::initializeGL() {
   ... 
   vertexBuffer = new QGLBuffer(QGLBuffer::VertexBuffer);
   vertexBuffer->create();
   vertexBuffer->bind();
   vertexBuffer->setUsagePattern(QGLBuffer::StaticDraw);
   vertexBuffer->allocate(vertices, 3*numVertices*sizeof(float)); // copies vertices to GPU? 
   vertexBuffer->release();

   #define BUFFER_OFFSET(bytes) ((GLubyte*) NULL + (bytes)) 
   glEnableClientState(GL_VERTEX_ARRAY);
   glEnableClientState(GL_COLOR_ARRAY);
   glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));
   glColorPointer(3, GL_FLOAT, 0,  colors);
}  

When I run this it hangs for a long time before crashing :-(.
If I comment out the vertexBuffer->release(); line it displays nothing at all but do not crash.

What am I doing wrong here?

Also: how can I likewise send my colors to the GPU only once?
There is no QGLBuffer::ColorBuffer type!?

EDIT:
I included GLee.[h/c] in my project and replaced the QGLBuffer calls by:

unsigned int vbufferid;
glGenBuffers(1, &vbufferid);  
glBindBuffer(GL_ARRAY_BUFFER, vbufferid); 
int size = 3*numVertices*sizeof(float);
glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW);

but the code still does not draw anything!?

The vbufferid is assigned the value 1 by the glGenBuffers call so I do not think that the problem is my graphics card.

EDIT 2:
I found that if I comment out the glEnableClientState(GL_COLOR_ARRAY); line the triangles are being displayed with VBO (but no color)!

So how do I transfer the color to the GPU when using VBO for vertices?

There is no GL_COLOR_BUFFER type !?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T17:47:59+00:00Added an answer on June 16, 2026 at 5:47 pm

    Am I right in my understanding that the glVertexPointer() tells the GPU where in the CPU it can fetch the vertices data and then the GPU copy it from this location in the CPU for each paintGL()?

    And that using VBO would improve on this by writing the vertices data to the GPU only once?

    That’s about the general idea, though the actual execution (you don’t have to care about) is a bit tricker.

    One common misconception you fell for: OpenGL is not “initialized”. You can upload certain resources to OpenGL in a initial phase, of course, but as a state machine OpenGL needs to be put into the required state right before you need it.

    And then there’s your real problem: Your vertex positions are in a buffer object. Your colors however are not. But you’re giving OpenGL a pointer, which it misinterprets as a offset into the vertex buffer.

    So I suggest you move the following lines right before the glDrawElements call:

    #define BUFFER_OFFSET(bytes) ((GLubyte*) NULL + (bytes)) 
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);
    
    glBindBuffer(GL_ARRAY_BUFFER, vbufferid);
    glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));
    
    glBindBuffer(GL_ARRAY_BUFFER, 0); // or put the colors into a VBO as well
    glColorPointer(3, GL_FLOAT, 0,  colors);
    
    glDrawElements(…
    

    And take note, that the buffer is explicitly bound before doing the calls to glVertexPointer and unbound before the color pointer.

    But, having some of the attributes in a VBO, and some not defeats the purpose. I hence suggest you move all your vertex attributes into VBOs, preferrably a single VBO, either one array after another (stride 0 and with offset between them) or interleaved (stride= the size of a whole attribute vector, with offset being the offset into the first attribute vector).

    Update Code example:

    Uploading to VBO

    unsigned int vbufferid;
    glGenBuffers(1, &vbufferid);  
    glBindBuffer(GL_ARRAY_BUFFER, vbufferid); 
    
    int vertexbufsize = (3)*numVertices*sizeof(float);
    int colorbufsize = (3)*numVertices*sizeof(float);
    glBufferData(GL_ARRAY_BUFFER, vertexbufsize + colorbufsize, NULL, GL_STATIC_DRAW); // data=NULL : initialize, but don't copy
    
    glBufferSubData(GL_ARRAY_BUFFER, 0, vertexbufsize, vertices);
    glBufferSubData(GL_ARRAY_BUFFER, vertexbufsize, colorbufsize, vertices);
    

    Drawing with combined VBO

    #define BUFFER_OFFSET(bytes) ((GLubyte*) NULL + (bytes)) 
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);
    
    glBindBuffer(GL_ARRAY_BUFFER, vbufferid);
    glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));
    glColorPointer(3, GL_FLOAT, 0,  BUFFER_OFFSET(vertexbufsize));
    
    glDrawElements(…
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to draw a lot of lines. I'm using UIBezierPath for drawing lines
I am drawing a sphere using quads. I plot an extra vertex, just to
There are a lot of questions regarding drawing on the canvas element and using
I am new to HTML5.I see lot of charts using sencha,fusion.Is it possible drawing
I have an application drawing a lot of small rectangles (about 1 million) using
Our system uses a lot of large Bitmaps (System.Drawing.Bitmap) and sometimes we run out
I have problems with WPF drawing performance. There are a lot of small EllipseGeometry
We are developing an drawing application for iOS and android. I am using cubic
I have a VB6 application (please don't laugh) which does a lot of drawing
There is a quite big LOB silverlight application and we wrote a lot of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.