Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: How to pass the color data to vertex shader(GLSL)

  1. #1
    Junior Member Newbie
    Join Date
    Jun 2017
    Posts
    5

    How to pass the color data to vertex shader(GLSL)

    For example, I'd like to draw two triangles, here are their vertex data:

    Code cpp:
    GLfloat vertices[NumVertices][2] = {
    {-0.90, -0.90},
    { 0.85, -0.90},
    {-0.90, 0.85},
    { 0.90, -0.85},
    { 0.90, 0.90},
    {-0.85, 0.90},
    };
     
    GLfloat colors[NumVertices][3] = {
    {1.0,0.0,0.0},
    {1.0,0.0,0.0},
    {1.0,0.0,0.0},
    {1.0,0.0,0.0},
    {1.0,0.0,0.0},
    {1.0,0.0,0.0},
    };

    I used the following code to pass the data to vertex shader:

    Code cpp:
    glGenVertexArrays(NumVAOs,VAOs);
    glBindVertexArray(VAOs[Triangles]);
    glGenBuffers(NumBuffers,Buffers);
    glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer]);
    glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_DYNAMIC_DRAW);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0,0);
    glEnableVertexAttribArray(0);
     
     
    glGenVertexArrays(1,ColorObj);
    glBindVertexArray(ColorObj[0]);
    glGenBuffers(1,ColorBuf);
    glBindBuffer(GL_ARRAY_BUFFER,ColorBuf[0]);
    glBufferData(GL_ARRAY_BUFFER,sizeof(colors),colors,GL_DYNAMIC_DRAW);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0,0);
    glEnableVertexAttribArray(1);

    and the GLSL code of shaders are:

    Code glsl:
    #version 330 
    layout(location = 0) in vec4 vPosition; 
    layout(location = 1) in vec3 vColor11; 
    out vec3 vColorOut; 
    void main() 
    { 
    gl_Position = vPosition; 
    vColorOut = vColor11;
    }
     
     
    #version 330
    in vec3 vColorOut;
    out vec3 fColor; 
    void main() 
    { 
    fColor = vColorOut;
    }

    However, the program is failed of drawing triangles. But, if I use the static color data (like using: vColorOut = vec3(1.0,0.0,0.0)), there are two red triangles can be shown correctly.

    Am I wrong in using "glVertexAttribPointer"?
    Last edited by sc20184; 06-13-2017 at 07:33 AM.

  2. #2

  3. #3
    Junior Member Newbie
    Join Date
    Jun 2017
    Posts
    5
    Thanks for your reply. Could you give me more detail, because I did write the code of glEnableVertexAttribArray(0); and glEnableVertexAttribArray(1);

  4. #4
    Intern Contributor
    Join Date
    Jan 2017
    Posts
    54
    I'm very much a newbie like you.

    I'd recommend this series of tutorials. They're well regarded in this forum.

    The particular lesson in the link addresses your question.

    Your code examples demonstrate several gaps in your understanding of OpenGL. I suggest you start at the first lesson. In an hour or two you'll understand what you need to do.
    Last edited by larryl; 06-13-2017 at 06:12 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •