3

I'm trying to write something in OpenGL, and I'm a beginner so sorry for any mistakes I make.

in general I just wanted to draw two triangles with different colours and I did using the following code:

float vertices[] = {
        -0.5f, -0.6f, 0.0f,
        0.5f, -0.6f, 0.0f,
        0.4f,  0.5f, 0.0f,
        0.5f, 0.6f, 0.0f,
        -0.5f, 0.6f, 0.0f,
        -0.4f,  -0.5f, 0.0f

};

void display() {
    std::cout << "frame";
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
    glClear(GL_COLOR_BUFFER_BIT);         // Clear the color buffer
    // activate and specify pointer to vertex array
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, 0, vertices);

// draw a cube
    glColor3f(1.0f, 0.0f, 0.0f); // Red

    glDrawArrays(GL_TRIANGLES, 0, 3);
    //glColor3f(0.0f, 1.0f, 0.0f); // Green
    glDrawArrays(GL_TRIANGLES, 3, 3);

    glDisableClientState(GL_VERTEX_ARRAY);
    glFlush();  // Render now
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);                 // Initialize GLUT
    glutCreateWindow("OpenGL Setup Test"); // Create a window with the given title
    glutInitWindowSize(320, 320);   // Set the window's initial width & height
    glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
    glutDisplayFunc(display); // Register display callback handler for window re-paint
    glutMainLoop();           // Enter the infinitely event-processing loop
    return 0;
}

now.. if I . want to draw both triangles in the same command I can do

    glDrawArrays(GL_TRIANGLES, 0, 6);

but then it draws the two triangles in the same colour.

is there a way to draw each triangle in a different colour by still using only one glDrawArrays() command?

if not.. is there some other command I should go for ?

thank you

ufk
  • 30,912
  • 70
  • 235
  • 386
  • *"if not.. is there some other command I should go for ?"* Fixed function attributes and client-side capability is deprecated since decades.See [Fixed Function Pipeline](https://www.khronos.org/opengl/wiki/Fixed_Function_Pipeline) and [Legacy OpenGL](https://www.khronos.org/opengl/wiki/Legacy_OpenGL). Read about [Vertex Specification](https://www.khronos.org/opengl/wiki/Vertex_Specification) and [Shader](https://www.khronos.org/opengl/wiki/Shader) for a state of the art way of rendering. – Rabbid76 Jan 13 '19 at 11:46

2 Answers2

2

In the description of glDrawArrays it is written :

Instead of calling a GL procedure to pass each individual vertex attribute, you can use glVertexAttribPointer to prespecify separate arrays of vertices, normals, and colors and use them to construct a sequence of primitives with a single call to glDrawArrays.

Is that your solution ?

bruno
  • 32,421
  • 7
  • 25
  • 37
  • I've no idea how this is an (accepted) answers to the question. For the use of [`glVertexAttribPointer`](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glVertexAttribPointer.xhtml) a shader program is required and it doesn't interact with the fixed function attributes and client-side capability ([`glEnableClientState`](https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glEnableClientState.xml)). – Rabbid76 Jan 13 '19 at 11:58
  • See also the answer to [What are the Attribute locations for fixed function pipeline in OpenGL 4.0++ core profile?](https://stackoverflow.com/questions/20573235/what-are-the-attribute-locations-for-fixed-function-pipeline-in-opengl-4-0-cor) – Rabbid76 Jan 13 '19 at 11:58
1

"if not.. is there some other command I should go for ?" Fixed function attributes and client-side capability is deprecated since decades.See Fixed Function Pipeline and Legacy OpenGL. Read about Vertex Specification and Shader for a state of the art way of rendering.


Anyway you can define an array of color attributes by glColorPointer, so each vertex coordinate is associated to an individual color attribute:

float colors[] = {
     1.0f, 0.0f, 0.0f, // red
     1.0f, 0.0f, 0.0f,
     1.0f, 0.0f, 0.0f,
     0.0f, 1.0f, 0.0f, // green
     0.0f, 1.0f, 0.0f,
     0.0f, 1.0f, 0.0f
};

glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices);

glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(3, GL_FLOAT, 0, colors);

glDrawArrays(GL_TRIANGLES, 0, 6);
Rabbid76
  • 202,892
  • 27
  • 131
  • 174