Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
182 views
in Technique[技术] by (71.8m points)

Creating Two Objects OpenGL

I'm trying to display two triangles in one window using this method. I can display them independently, but when I call them at the same time, one overwrites the other.

glGenVertexArrays(1, &mesh.vao); // we can also generate multiple VAOs or buffers at the same time
glBindVertexArray(mesh.vao);

// Create 2 buffers: first one for the vertex data; second one for the indices
glGenBuffers(2, mesh.vbos);
glBindBuffer(GL_ARRAY_BUFFER, mesh.vbos[0]); // Activates the buffer
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW); // Sends vertex or coordinate data to the GPU

mesh.nIndices = sizeof(indices) / sizeof(indices[0]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh.vbos[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

// Strides between vertex coordinates is 6 (x, y, z, r, g, b, a). A tightly packed stride is 0.
GLint stride = sizeof(float) * (floatsPerVertex + floatsPerColor);// The number of floats before each

// Create Vertex Attribute Pointers
glVertexAttribPointer(0, floatsPerVertex, GL_FLOAT, GL_FALSE, stride, 0);
glEnableVertexAttribArray(0);

glVertexAttribPointer(1, floatsPerColor, GL_FLOAT, GL_FALSE, stride, (char*)(sizeof(float) * floatsPerVertex));
glEnableVertexAttribArray(1);


// Create 2 buffers: first one for the vertex data; second one for the indices
glGenVertexArrays(1, &mesh.vao2); // we can also generate multiple VAOs or buffers at the same time
glBindVertexArray(mesh.vao2);

glBindBuffer(GL_ARRAY_BUFFER, mesh.vbos2[0]); // Activates the buffer
glBufferData(GL_ARRAY_BUFFER, sizeof(verts2), verts2, GL_STATIC_DRAW); // Sends vertex or coordinate data to the GPU

mesh.nIndices2 = sizeof(indices2) / sizeof(indices2[0]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh.vbos2[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices2), indices2, GL_STATIC_DRAW);

// Strides between vertex coordinates is 6 (x, y, z, r, g, b, a). A tightly packed stride is 0.
GLint stride2 = sizeof(float) * (floatsPerVertex + floatsPerColor);// The number of floats before each

// Create Vertex Attribute Pointers
glVertexAttribPointer(0, floatsPerVertex, GL_FLOAT, GL_FALSE, stride2, 0);
glEnableVertexAttribArray(0);

glVertexAttribPointer(1, floatsPerColor, GL_FLOAT, GL_FALSE, stride2, (char*)(sizeof(float) * floatsPerVertex));
glEnableVertexAttribArray(1); 

Calling functions in rendering loop:

glBindVertexArray(gMesh.vao);

// Draws the triangles
glDrawElements(GL_TRIANGLES, gMesh.nIndices, GL_UNSIGNED_SHORT, NULL); // Draws the triangle

glBindVertexArray(gMesh.vao2);

glDrawElements(GL_TRIANGLES, gMesh.nIndices2, GL_UNSIGNED_SHORT, NULL); // Draws the triangle

// Deactivate the Vertex Array Object
glBindVertexArray(0);

These are the two Triangles:

GLfloat verts[] = {
    -0.75f,  1.0f, 0.0f,   1.0f, 0.0f, 0.0f, 1.0f, // Top Right Vertex 0
     -1.0f, 0.0f, 0.0f,   1.0f, 0.0f, 0.0f, 1.0f, // Bottom Right Vertex 1
    -0.5, 0.0f, 0.0f,   1.0f, 0.0f, .0f, 1.0f, // Bottom Left Vertex 2
};
GLushort indices[] = {
    0, 1, 2,  // Triangle 1
};
GLfloat verts2[] = {
    0.75f,  1.0f, 0.0f,   1.0f, 0.0f, 0.0f, 1.0f, // Top Right Vertex 0
     1.0f, 0.0f, 0.0f,   0.0f, 1.0f, 0.0f, 1.0f, // Bottom Right Vertex 1
    0.5f, 0.0f, 0.0f,   0.0f, 0.0f, 1.0f, 1.0f, // Bottom Left Vertex 2
};
GLushort indices2[] = {
    0, 1, 2,  // Triangle 1
};
const GLuint floatsPerVertex = 3;
const GLuint floatsPerColor = 4;
question from:https://stackoverflow.com/questions/65897582/creating-two-objects-opengl

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...