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

  • SEARCH
  • Home
  • 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 9011481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:44:38+00:00 2026-06-16T02:44:38+00:00

I am trying to draw a quadrat using a Vertex Buffer Object in OpenGL

  • 0

I am trying to draw a quadrat using a Vertex Buffer Object in OpenGL with Qt.
Here is my geometry:

numVertices = 4;
vertices = new float[3*numVertices];
int i = 0;
vertices[i++] = 0.0f; vertices[i++] = 0.0f; vertices[i++] = 0.0f; // (0,0,0)
vertices[i++] = 1.0f; vertices[i++] = 0.0f; vertices[i++] = 0.0f; // (1,0,0)
vertices[i++] = 1.0f; vertices[i++] = 1.0f; vertices[i++] = 0.0f; // (1,1,0)
vertices[i++] = 0.0f; vertices[i++] = 1.0f; vertices[i++] = 0.0f; // (0,1,0)
i = 0;
// spilt quad into two triangles:
numTriangles = 2;
indices = new unsigned int[numTriangles*3];
indices[i++] = 0; indices[i++] = 1; indices[i++] = 2; 
indices[i++] = 0; indices[i++] = 2; indices[i++] = 3;

Next in initializeGL method:

QGLBuffer vertexBuffer;
vertexBuffer.create();
vertexBuffer.bind();
vertexBuffer.allocate(vertices, numVertices*sizeof(float));

QGLShaderProgram* shaderProgram_ = new QGLShaderProgram;
shaderProgram_->addShaderFromSourceFile(QGLShader::Vertex,"C:/src/light.vert.glsl") ) {
shaderProgram_->addShaderFromSourceFile(QGLShader::Fragment, "C:/src/light.frag.glsl");
bool ok = shaderProgram_->link();
ok = shaderProgram_->bind();

I think all the VBO part does is to copy vertices to GPU? (Why so many lines?)

The shader part worked fine with old style glBegin(GL_QUADS);

Next in my paintGL method:

shaderProgram_->setAttributeBuffer("vertex", GL_FLOAT, 0, 3, 0);
shaderProgram_->enableAttributeArray("vertex");
glDrawElements(GL_TRIANGLES, numTriangles, GL_UNSIGNED_INT, indices);

What are the two first lines doing? Maybe telling the shader that there is a vertex buffer named “vertex” of type GL_FLOAT?
However I did not specify any name when creating the VBO!? How do OpenGL know that this is “vertex”?

Anyway I am not seing anything!?
Are there any steps I am missing?
My shaders are simple pass trough:

# version 120  
varying vec4 color;
void main() {
    vec4 vertex = gl_Vertex;
    // pass trough:
    gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * vertex; 
    color = gl_Color; 
}

# version 120 
varying vec4 color;
void main (void) 
{   
    // pass-trough:    
 gl_FragColor = color;
 }
  • 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-16T02:44:40+00:00Added an answer on June 16, 2026 at 2:44 am

    I guess you’ve lost 3 here:

    vertexBuffer.allocate(vertices, numVertices*sizeof(float));
    

    ->

    vertexBuffer.allocate(vertices, numVertices*sizeof(float)*3);
    

    I think all the VBO part does is to copy vertices to GPU? (Why so many
    lines?)

    Yes. That’s how OpenGL works. To store something in VBO you have to create it, bind it and copy data.

    What are the two first lines doing? Maybe telling the shader that
    there is a vertex buffer named “vertex” of type GL_FLOAT? However I
    did not specify any name when creating the VBO!? How do OpenGL know
    that this is “vertex”?

    First line:

    QGLShaderProgram::setAttributeBuffer() “Sets an array of vertex values on the attribute called name in this shader program, starting at a specific offset in the currently bound vertex buffer.” – from manual. Again, that’s how OpenGL works. It’s a state machine. You bind specific buffer to GL_ARRAY_BUFFER binding point, next tell OpenGL that this is the buffer where “vertex” attribute data is stored. “vertex” is attribute name that you have in your shader program. (May be you should change it to “gl_Vertex”) Guess, Qt calls glVertexAttribLocation() to find location of your attribute and next calls glVertexAttribPointer().

    Second line:

    OpenGL must know that it should copy data from some buffer to some special place where shader program can find it. This is done by enabling attribute arrays for specific location. Guess, Qt calls glVertexAttribLocation() and glEnableAttribArray() here.

    BTW:
    Do you specify any modelview and projection matrices in your code? I’ not sure if they are set by default. Try removing these values form shaders for testing purposes.

    Added:

    glDrawElements(GL_TRIANGLES, numTriangles, GL_UNSIGNED_INT, indices);
    

    Second parameter is not number of triangles but number of indices, that will be read from indices array. Here should be numTriangles*3

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When trying to draw the following quads in OpenGL using a vertex array (instead
I'm trying to draw an open rectangle using a Polygon: int[] xPoints = {1,1,3,3};
I'm trying to draw a circle in c using opengl, that's smaller than the
I'm trying to draw image using UIImage 's drawInRect: method. Here is the code:
I am trying to draw in a web worker using html5 canvas. The worker
I'm trying to draw a graph using the coreplot library. I'm looking for a
I'm trying to draw selection/focus frame in my custom windows forms control using visual
I am trying to draw a line using the Graphics 2D but then the
I am trying to draw a line using on touch method. I want that
I am trying to draw a circle using mouse on the canvas using mouse

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.