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 8171261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:24:31+00:00 2026-06-06T21:24:31+00:00

I am learning OpenGL 4.0 and I want to draw a simple triangle using

  • 0

I am learning OpenGL 4.0 and I want to draw a simple triangle using OpenGL 4.0 and GLSL. I’m using VAO with interleaved arrays to do it, but the result it display is not what I want:

enter image description here

Now I post part of my code:

void SceneBasic::setupVAOInterleavedArrays()
{
    //三角形的顶点和颜色信息数组:混合数组
    float positionAndColorData[] = {
        -0.8f, -0.8f, 0.0f,1.0f, 0.0f, 0.0f,
        0.8f, -0.8f, 0.0f,0.0f, 1.0f, 0.0f,
        0.0f,  0.8f, 0.0f,0.0f, 0.0f, 1.0f };

    //glInterleavedArrays(GL_C3F_V3F,0,positionAndColorData)

    GLuint vboHandle;//VBO
    glGenBuffers(1,&vboHandle);

    glBindBuffer(GL_ARRAY_BUFFER,vboHandle);
    glBufferData(GL_ARRAY_BUFFER,18 * sizeof(float),
        positionAndColorData,GL_STATIC_DRAW);

    //VAO
    glGenVertexArrays(1,&vaoHandle);
    glBindVertexArray(vaoHandle);

    //enable the generic vertex attribute indexes
    //indicates that the values for the attributes will be accessed
    //and used for rendering
    glEnableVertexAttribArray(0);//position
    glEnableVertexAttribArray(1);//color

    //make the connection between the buffer objects and the generic 
    //vertex attributes indexes
    glBindBuffer(GL_ARRAY_BUFFER,vboHandle);
    glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,3 * sizeof(float),BUFFER_OFFSET(0));
    glBindBuffer(GL_ARRAY_BUFFER,vboHandle);
    glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,3 * sizeof(float),BUFFER_OFFSET(3 * sizeof(float)));
}

void SceneBasic::initScene()
{
    compileAndLinkShader();

    //setupVAO();
    setupVAOInterleavedArrays();
}

void SceneBasic::render()
{
    glClear(GL_COLOR_BUFFER_BIT);

    glBindVertexArray(vaoHandle);
    glDrawArrays(GL_TRIANGLES,0,3);
    glBindVertexArray(0);
}

But if I don’t use interleaved array, the result is right:
enter image description here

This is the part of my code when I don’t use interleaved arrays:

void SceneBasic::setupVAO()
{
    //三角形的顶点和颜色信息数组
    float positionData[] = {
        -0.8f, -0.8f, 0.0f,
        0.8f, -0.8f, 0.0f,
        0.0f,  0.8f, 0.0f };
    float colorData[] = {
        1.0f, 0.0f, 0.0f,
        0.0f, 1.0f, 0.0f,
        0.0f, 0.0f, 1.0f };

    glGenBuffers(2,vboHandles);
    GLuint positionBufferHandle = vboHandles[0];
    GLuint colorBufferHandle = vboHandles[1];

    glBindBuffer(GL_ARRAY_BUFFER,positionBufferHandle);
    glBufferData(GL_ARRAY_BUFFER,9 * sizeof(float),
        positionData,GL_STATIC_DRAW);

    glBindBuffer(GL_ARRAY_BUFFER,colorBufferHandle);
    glBufferData(GL_ARRAY_BUFFER,9 * sizeof(float),
        colorData,GL_STATIC_DRAW);

    //VAO
    glGenVertexArrays(1,&vaoHandle);
    glBindVertexArray(vaoHandle);

    //enable the generic vertex attribute indexes
    //indicates that the values for the attributes will be acessed
    //and used for rendering
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);

    //make the connection between the buffer objects abd the generic 
    //vertex attributes indexes
    glBindBuffer(GL_ARRAY_BUFFER,positionBufferHandle);
    glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,BUFFER_OFFSET(0));

    glBindBuffer(GL_ARRAY_BUFFER,colorBufferHandle);
    glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,0,BUFFER_OFFSET(0));
}

I am so curious, why is my code not producing the expected result when I use interleaved arrays?

  • 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-06T21:24:33+00:00Added an answer on June 6, 2026 at 9:24 pm

    The stride is wrong, since you have 6 elements per vertex, you need to pass 6 * sizeof (float) as the stride.

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

Sidebar

Related Questions

I want to start learning OpenGL but I don't really want to have to
I need to visualize 3D point clouds using C++, I started learning OpenGL but
I'm learning OpenGL ES. When I'm using texture for triangle, I meet error. Here
i'm learning opengl and I want to draw 50 texture(from one variable) this is
I'm learning OpenGL and SDL and so far I've been able to properly draw
The problem is that, shaders (pretty simple ones, as I'm learning OpenGL) fail to
I am busy climbing the learning curve for OpenGL, using Delphi (pascal); I am
I am starting learning OpenGL and I am not sure, how to set it
Im using OpenGL for an app that im trying to make. I've been learning
I recently satrted learning OpenGL, and it seems pretty intuitive so far, but this

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.