gpt4 book ai didi

c++ - OpenGL绘制多个对象的正确方法?

转载 作者:行者123 更新时间:2023-12-03 07:22:46 26 4
gpt4 key购买 nike

我试图了解如何正确绘制多个对象,并做到这一点,我想面对面绘制一个立方体。问题是我显然做得不好,因为只画了第一张脸。我有24个顶点结构(每个面4个顶点)和36个索引(每个面6个,每个三角形3个)。随时纠正我的任何问题。

更新

//array of vertex -- this is only the first face

Vertex vertices[24] = {
// vertex 0
{glm::vec3(-1.0f, -1.0f, 1.0f), glm::vec3( 1.0f, 0.0f, 0.0f), glm::vec2(0.0f,0.0f), glm::vec3(0.0,0.0,0.0), glm::vec3(0.0,0.0,0.0)},
// vertex 1
{glm::vec3( 1.0f, -1.0f, 1.0f), glm::vec3( 1.0f, 1.0f, 0.0f), glm::vec2(1.0f,0.0f), glm::vec3(0.0,0.0,0.0), glm::vec3(0.0,0.0,0.0)},
// vertex 2
{glm::vec3( 1.0f, 1.0f, 1.0f), glm::vec3( 0.0f, 0.0f, 1.0f), glm::vec2(1.0f,1.0f), glm::vec3(0.0,0.0,0.0), glm::vec3(0.0,0.0,0.0)},
// vertex 3
{glm::vec3(-1.0f, 1.0f, 1.0f), glm::vec3( 0.0f, 1.0f, 0.0f), glm::vec2(0.0f,1.0f), glm::vec3(0.0,0.0,0.0), glm::vec3(0.0,0.0,0.0)},

unsigned int indices[36] = {
0, 2, 1, // front
2, 0, 3,
8, 10,9, // face dx
10,8,11,
12,14,13,// face sx
14,12,15,
4, 6, 5, // back face
6, 4, 7,
20,22,21,// bottom face
22,20,23,
16,18,17,// top face
18,16,19
};

// creation and population of buffers
// i am only putting here two because it's the same process for all six faces
// face 1
glGenVertexArrays(1,&global.vao0);
glGenBuffers(1,&global.vb0);
glBindVertexArray(global.vao0);
glBindBuffer(GL_ARRAY_BUFFER,global.vb0);
glGenBuffers(1,&global.ib0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,global.ib0);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex)*4, &vertices[0], GL_STATIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int)*6, &indices[0], GL_STATIC_DRAW);

//face 2
glGenVertexArrays(1,&global.vao1);
glGenBuffers(1,&global.vb1);
glBindVertexArray(global.vao1);
glBindBuffer(GL_ARRAY_BUFFER,global.vb1);
glGenBuffers(1,&global.ib1);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,global.ib1);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex)*4, &vertices[4], GL_STATIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int)*6, &indices[6], GL_STATIC_DRAW);
然后我去画画
//my vertex struct has 5 attributes of course, but don't mind those because that's not the point
//drawing face 1
glBindVertexArray(global.vao0);
glBindBuffer(GL_ARRAY_BUFFER,global.vb0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, global.ib0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
reinterpret_cast<GLvoid*>(offsetof(struct Vertex, position)));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
reinterpret_cast<GLvoid*>(offsetof(struct Vertex, color)));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex),
reinterpret_cast<GLvoid*>(offsetof(struct Vertex, textcoord)));
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
reinterpret_cast<GLvoid*>(offsetof(struct Vertex, normal)));
glEnableVertexAttribArray(4);
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
reinterpret_cast<GLvoid*>(offsetof(struct Vertex, tangent)));
global.t0.Bind(GL_TEXTURE0); // texture representing this face
glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,0);
glBindVertexArray(0);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
glDisableVertexAttribArray(3);
glDisableVertexAttribArray(4);

//drawing face 2
glBindVertexArray(global.vao1);
glBindBuffer(GL_ARRAY_BUFFER,global.vb1);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, global.ib1);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
reinterpret_cast<GLvoid*>(offsetof(struct Vertex, position)));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
reinterpret_cast<GLvoid*>(offsetof(struct Vertex, color)));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex),
reinterpret_cast<GLvoid*>(offsetof(struct Vertex, textcoord)));
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
reinterpret_cast<GLvoid*>(offsetof(struct Vertex, normal)));
glEnableVertexAttribArray(4);
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
reinterpret_cast<GLvoid*>(offsetof(struct Vertex, tangent)));
global.t1.Bind(GL_TEXTURE0); // different texture to see if i'm drawing it correctly
glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,0);
glBindVertexArray(0);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
glDisableVertexAttribArray(3);
glDisableVertexAttribArray(4);

... same thing for other faces
我究竟做错了什么?

最佳答案

global.vb1包含4个顶点。但是,global.ib1中包含的索引是8、10、9、10、8、11。您期望什么?缓冲区中只有4个顶点。因此,索引必须在[0,3]范围内。
如果将顶点分成6个缓冲区,每个缓冲区有4个顶点,则每个缓冲区的对应索引必须为0、1、2、0、2、3:glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int)*6, &indices[6], GL_STATIC_DRAW);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,global.ib1); 
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int)*6, &indices[0], GL_STATIC_DRAW);

关于c++ - OpenGL绘制多个对象的正确方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64596045/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com