Rendering two objects with OpenGL and vertex array objects

后端 未结 1 1579
南方客
南方客 2021-01-22 22:29

I am trying to use OpenGL with shaders to render two triangles on my screen; a big red one, and a smaller blue one. Each triangle is defined by a set of vertex positions and col

1条回答
  •  别那么骄傲
    2021-01-22 22:56

    glEnableVertexAttribArray must be called for each vao:

    // Fill the buffer data
    glBindVertexArray(object1_vertex_array);
    glBindBuffer(GL_ARRAY_BUFFER, object1_vertex_buffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(object1_vertices), &object1_vertices[0], GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glBindBuffer(GL_ARRAY_BUFFER, object1_colour_buffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(object1_colours), &object1_colours[0], GL_STATIC_DRAW);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object1_index_buffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, 3 * sizeof(int), &object1_indices[0], GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    
    glBindVertexArray(object2_vertex_array);
    glBindBuffer(GL_ARRAY_BUFFER, object2_vertex_buffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(object2_vertices), &object2_vertices[0], GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glBindBuffer(GL_ARRAY_BUFFER, object2_colour_buffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(object2_colours), &object2_colours[0], GL_STATIC_DRAW);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object2_index_buffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, 3 * sizeof(int), &object2_indices[0], GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    

    0 讨论(0)
提交回复
热议问题