gpt4 book ai didi

c++ - OpenGL:两个顶点数组 + 两个索引数组

转载 作者:行者123 更新时间:2023-11-28 02:12:46 25 4
gpt4 key购买 nike

我正尝试从一个简单的 Spring 质量系统的动画开始学习现代 openGL。

我在类里面有一个 Spring 和一个质量 block ,每个都使用一个索引数组来绘制,当我分别绘制它们时,它们都能完美地工作。当我将两者画在一起时,顶点位置都是错误的。我猜这与我如何对顶点缓冲区和索引数组等进行编号有关,但我对内部工作原理的了解不足以弄清楚。

#include "Spring.hpp"
#include "ShaderPaths.hpp"
#include "atlas\gl\Shader.hpp"
#include "atlas\core\Macros.hpp"
#include <atlas/utils/Geometry.hpp>
const int NUM_VERTICES_PER_LINE = 3;
const int NUMFLOATSPERVERTICES = 6;
const int VERTEX_BYTE_SIZE = NUMFLOATSPERVERTICES * sizeof(float);
GLint numSpringIndices, numMassIndices;
#define NUM_ARRAY_ELEMENTS(a) sizeof(a) / sizeof(*a);
Spring::Spring() : anchorPosition{ 0.0f, 1.0f, 0.0f }, mPosition{0.0f, 0.0f, 0.0f} {
USING_ATLAS_GL_NS; //Short for atlas GL namespace
USING_ATLAS_MATH_NS;
glGenVertexArrays(1, &mVertexArrayObject);
glBindVertexArray(mVertexArrayObject);


ShapeData Spring = ObjectGenerator::makeSpring(anchorPosition, stretch, d);
ShapeData Mass = ObjectGenerator::makeMass(calculateConnectionPoint(anchorPosition), massWidth, massHeight);
ShapeData Triangle = ObjectGenerator::makeTriangle();
numSpringIndices = Spring.numIndices;
numMassIndices = Mass.numIndices;
//======= Spring Buffer ======//
glGenBuffers(1, &mSpringBuffer);
glBindBuffer(GL_ARRAY_BUFFER, mSpringBuffer);
glBufferData(GL_ARRAY_BUFFER, Spring.vertexBufferSize(), Spring.vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, VERTEX_BYTE_SIZE, 0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, VERTEX_BYTE_SIZE, (char*)(sizeof(float) * 3));
glGenBuffers(1, &springIndexBufferID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, springIndexBufferID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, Spring.indexBufferSize(), Spring.indices, GL_STATIC_DRAW);

//======= Mass Buffer ======//
glGenBuffers(1, &mMassBuffer);
glBindBuffer(GL_ARRAY_BUFFER, mMassBuffer);
glBufferData(GL_ARRAY_BUFFER, Mass.vertexBufferSize(), Mass.vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(2);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, VERTEX_BYTE_SIZE, 0);
glEnableVertexAttribArray(3);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, VERTEX_BYTE_SIZE, (char*)(sizeof(float) * 3));
glGenBuffers(1, &massIndexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, massIndexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, Mass.indexBufferSize(), Mass.indices, GL_STATIC_DRAW);

//======= Shaders ======//
std::string shaderDir = generated::ShaderPaths::getShaderDirectory();
std::vector<ShaderInfo> springShaders
{
ShaderInfo{ GL_VERTEX_SHADER, shaderDir + "Spring.vs.glsl" },
ShaderInfo{ GL_FRAGMENT_SHADER, shaderDir + "Spring.fs.glsl" }
};
mShaders.push_back(ShaderPointer(new Shader));
mShaders[0]->compileShaders(springShaders);
mShaders[0]->linkShaders();

//===== Clean ups to prevent memory leaks =====//
Spring.cleanup();
Mass.cleanup();
}

Spring::~Spring()
{
glDeleteVertexArrays(1, &mVertexArrayObject);
glDeleteBuffers(1, &mSpringBuffer);
}

void Spring::renderGeometry(atlas::math::Matrix4 projection, atlas::math::Matrix4 view) {
// To avoid warnings from unused variables, you can use the
//UNUSED macro.
UNUSED(projection);
UNUSED(view);
mShaders[0]->enableShaders();
glBindVertexArray(mVertexArrayObject);
GLint dominatingColorUniformLocation = mShaders[0]->getUniformVariable("dominatingColor");
//========== Draw Spring ===============//
glm::vec3 springColor(0.0f, 1.0f, 0.0f);
glUniform3fv(dominatingColorUniformLocation, 1, &springColor[0]); //Send the location of the first float
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, springIndexBufferID);
glDrawElements(GL_LINES, numSpringIndices, GL_UNSIGNED_SHORT, 0);
//======================================//

//========= Draw Mass =================//
glm::vec3 massColor(1.0f, 0.0f, 0.0f);
glUniform3fv(dominatingColorUniformLocation, 1, &massColor[0]); //Send the location of the first float
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, massIndexBuffer);
glDrawElements(GL_TRIANGLES, numMassIndices, GL_UNSIGNED_SHORT, 0);
//======================================//
mShaders[0]->disableShaders();
}
void Spring::updateGeometry(atlas::utils::Time const& t) {
mModel = glm::translate(Matrix4(1.0f), mPosition);
}

glm::vec3 Spring::calculateConnectionPoint(glm::vec3 anchorPosition) {
glm::vec3 temp = glm::vec3(anchorPosition.x, anchorPosition.y - (18 * d), 0.0f);
return temp;
}

对于任何好奇的人来说,ObjectGenerator 类看起来像这样

#include "ObjectGenerator.h"
#define NUM_ARRAY_ELEMENTS(a) sizeof(a) / sizeof(*a);

ShapeData ObjectGenerator::makeSpring(glm::vec3 anchorPosition, GLfloat stretch, GLfloat d) {
ShapeData ret;
static const Vertex vertices[] = {
glm::vec3( anchorPosition.x, anchorPosition.y, 0.0f ), // 0
glm::vec3( 1.0f, 0.0f, 0.0f ), // Spring Color
glm::vec3( anchorPosition.x, anchorPosition.y - d,0.0f ), // 1
glm::vec3( 1.0f, 0.0f, 0.0f ), // Spring Color
glm::vec3( anchorPosition.x + stretch, anchorPosition.y - (2 * d), 0.0f ), // 2
glm::vec3( 1.0f, 0.0f, 0.0f ), // Spring Color
glm::vec3( anchorPosition.x - stretch, anchorPosition.y - (4 * d), 0.0f ), // 3
glm::vec3( 1.0f, 0.0f, 0.0f ), // Spring Color
glm::vec3( anchorPosition.x + stretch, anchorPosition.y - (6 * d), 0.0f ), // 4
glm::vec3( 1.0f, 0.0f, 0.0f ), // Spring Color
glm::vec3( anchorPosition.x - stretch, anchorPosition.y - (8 * d), 0.0f ), // 5
glm::vec3( 1.0f, 0.0f, 0.0f ), // Spring Color
glm::vec3( anchorPosition.x + stretch, anchorPosition.y - (10 * d), 0.0f ), // 6
glm::vec3( 1.0f, 0.0f, 0.0f ), // Spring Color
glm::vec3( anchorPosition.x - stretch, anchorPosition.y - (12 * d), 0.0f ), // 7
glm::vec3( 1.0f, 0.0f, 0.0f ), // Spring Color
glm::vec3( anchorPosition.x + stretch, anchorPosition.y - (14 * d), 0.0f ), // 8
glm::vec3( 1.0f, 0.0f, 0.0f ), // Spring Color
glm::vec3( anchorPosition.x - stretch, anchorPosition.y - (16 * d), 0.0f ), // 9
glm::vec3( 1.0f, 0.0f, 0.0f ), // Spring Color
glm::vec3( anchorPosition.x, anchorPosition.y - (17 * d), 0.0f ), // 10
glm::vec3( 1.0f, 0.0f, 0.0f ), // Spring Color
glm::vec3( anchorPosition.x, anchorPosition.y - (18 * d), 0.0f ), // 11
glm::vec3( 1.0f, 0.0f, 0.0f ), // Spring Color
};
ret.numVertices = NUM_ARRAY_ELEMENTS(vertices);
ret.vertices = new Vertex[ret.numVertices];
memcpy(ret.vertices, vertices, sizeof(vertices)); //memcpy(dest, source, size);

GLushort indices[] = { 0,1 ,1,2, 2,3, 3,4, 4,5, 5,6, 6,7, 7,8, 8,9, 9,10, 10,11 };
ret.numIndices = NUM_ARRAY_ELEMENTS(indices);
ret.indices = new GLushort[ret.numIndices];
memcpy(ret.indices, indices, sizeof(indices));

return ret;
}

ShapeData ObjectGenerator::makeMass(glm::vec3 connectionPoint, GLfloat width, GLfloat height) {
ShapeData ret;
static const Vertex vertices[] = {
//=================Mass==============//
glm::vec3( connectionPoint.x - width, connectionPoint.y, 0.0f ), //top Left 0
glm::vec3( 0.0f, 1.0f, 0.0f ), // Mass Color
glm::vec3(connectionPoint.x + width, connectionPoint.y, 0.0f), //top Right 1
glm::vec3(0.0f, 1.0f, 0.0f), // Mass Color
glm::vec3(connectionPoint.x + width, connectionPoint.y - height, 0.0f), // bottom right 2
glm::vec3(0.0f, 1.0f, 0.0f), // Mass Color
glm::vec3( connectionPoint.x - width, connectionPoint.y - height, 0.0f ), // bottom left 3
glm::vec3( 0.0f, 1.0f, 0.0f ), // Mass Color

};
ret.numVertices = NUM_ARRAY_ELEMENTS(vertices);
ret.vertices = new Vertex[ret.numVertices];
memcpy(ret.vertices, vertices, sizeof(vertices)); //memcpy(dest, source, size);

GLushort indices[] = {0,1,3, 1,2,3 };
ret.numIndices = NUM_ARRAY_ELEMENTS(indices);
ret.indices = new GLushort[ret.numIndices];
memcpy(ret.indices, indices, sizeof(indices));

return ret;
}

最佳答案

您的代码中存在几个相当基本的问题,这些问题似乎是基于对 OpenGL 状态管理的工作方式以及不同类型对象的连接方式的误解。

说明代码中一个基本问题的最清晰方法是代码中的以下序列(省略部分):

//======= Spring Buffer ======//
...
glBindBuffer(GL_ARRAY_BUFFER, mSpringBuffer);
...
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, VERTEX_BYTE_SIZE, 0);

//======= Mass Buffer ======//
...
glBindBuffer(GL_ARRAY_BUFFER, mMassBuffer);
...
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, VERTEX_BYTE_SIZE, 0);

如果您查看两个 glVertexAttribPointer() 调用,它们都会设置相同的顶点属性 (0)。由于它们都设置了相同的状态,因此第二个“获胜”,覆盖了您在第一次调用时设置的状态。调用的参数是相同的,但 glVertexAttribPointer() 还隐含地获取当前绑定(bind)的 GL_ARRAY_BUFFER,数据将从中获取。因此,属性 0 将使用来自 mMassBuffer 的数据,从不 来自 mSpringBuffer

解决此问题的最佳方法是使用两个顶点数组对象 (VAO)。 VAO 使用 glVertexAttribPointer() 跟踪状态集。因此,如果您使用两个不同的 VAO,其中一个可以跟踪用于 Spring 的状态,一个可以跟踪用于质量的状态。删除当前的 VAO 创建/绑定(bind)代码后,执行以下操作:

//======= Spring Buffer ======//
glGenVertexArrays(1, &mSpringVao);
glBindVertexArray(mSpringVao);
...

//======= Spring Buffer ======//
glGenVertexArrays(1, &mMassVao);
glBindVertexArray(mMassVao);
...

然后,在绘制代码中,您不必再绑定(bind)任何缓冲区,因为属性的所有状态设置都在相应的 VAO 中进行跟踪。相反,在每次绘制调用之前绑定(bind)相应的 VAO:

glBindVertexArray(mSpringVao);
glDrawElements(GL_LINES, numSpringIndices, GL_UNSIGNED_SHORT, 0);

glBindVertexArray(mMassVao);
glDrawElements(GL_LINES, numMassIndices, GL_UNSIGNED_SHORT, 0);

当然你还需要设置制服等。

此处的设置代码中还有另一个小但重要的问题:

glEnableVertexAttribArray(2);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, VERTEX_BYTE_SIZE, 0);
glEnableVertexAttribArray(3);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, VERTEX_BYTE_SIZE, (char*)(sizeof(float) * 3));

两个 glEnableVertexAttribArray() 调用的值在这里是错误的。它们需要匹配所有其他调用中使用的顶点属性的位置:

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, VERTEX_BYTE_SIZE, 0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, VERTEX_BYTE_SIZE, (char*)(sizeof(float) * 3));

关于c++ - OpenGL:两个顶点数组 + 两个索引数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35074353/

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