gpt4 book ai didi

c++ - OpenGL - 缓冲区更新下一个渲染对象

转载 作者:行者123 更新时间:2023-11-28 04:52:09 25 4
gpt4 key购买 nike

Okej,所以我遇到了一个非常奇怪的情况,我有三个(或两个,无关紧要)对象,其中一个正在将其缓冲区更新为“动画”。换句话说,一个物体在移动而另一个物体静止不动。当移动对象正在移动但在移动对象之后渲染的对象实际上是动画时,奇怪的部分就出现了。移动对象移动但不动画。希望它有意义。

现在是一些代码,因为我已经解决了它与缓冲区相关的问题,所以我将这些部分:

Object::Object() {
...
glGenVertexArrays(1, &VAO_);
glGenBuffers(2, VBO_);
glGenBuffers(1, &EBO_);

glBindVertexArray(VAO_);

glBindBuffer(GL_ARRAY_BUFFER, VBO_[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 4 * 3, vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(0);

glBindBuffer(GL_ARRAY_BUFFER, VBO_[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 4 * 2, tex_vert, GL_STREAM_DRAW);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(1);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO_);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * 6, indices, GL_STATIC_DRAW);

glBindVertexArray(0); // unbind vertex
glBindBuffer(GL_ARRAY_BUFFER, 0); // unbind buffer
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); // unbind buffer
}
void Object::Update() {
...
glBindBuffer(GL_ARRAY_BUFFER, VBO_[1]);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float) * 4 * 2, tex_vert);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void Object::Render() {
...
glUseProgram(Shader_.GetProgramID());
glBindVertexArray(VAO_);
glBindTexture(GL_TEXTURE_2D, Texture_);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}

int Main() {
Object m();
Object m2();
Object m3();
while (!glfwWindowShouldClose(engine.window)) {
...
m.Update();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
m.Render();
m3.Render();
m2.Render();
glfwSwapBuffers(window);
...
}
}

所以 m3 得到动画,但 m 正在移动,如果 m3 和 m2 改变位置,m2 将得到动画。如果我不渲染 m2/m3,m 将获得动画和移动。值得一提的是,所有非动画对象都会获得纹理,但会保持初始状态,因此一开始一切看起来都很好。

VAO、VBO 和 EBO 为每个对象获得了不同的值,但是“错误的”缓冲区 (VBO_[1]) 得到了更新。

认为我尝试了大多数错误的方法来解决这个问题,所以如果有人能指出我正确的方向,我将不胜感激。

编辑:

关于如何填充缓冲区等的附加信息

void Object::Update() {
if (Keyboard::KeyPressed(GLFW_KEY_W)) {
if (MovementY_ < 0) {
MovementY_ = 0;
}
MovementY_ += MovementSpeed_ * Engine::GetDeltaTime();

TextureTopLeft_ = glm::vec2((MALE_TEXTURE_SIZE_S * SpriteFrame_) - MALE_TEXTURE_SIZE_S, MALE_UP_STAND_T - MALE_TEXTURE_SIZE_T);
TextureTopRight_ = glm::vec2((MALE_TEXTURE_SIZE_S * SpriteFrame_), MALE_UP_STAND_T - MALE_TEXTURE_SIZE_T);
TextureBottomLeft_ = glm::vec2((MALE_TEXTURE_SIZE_S * SpriteFrame_) - MALE_TEXTURE_SIZE_S, MALE_UP_STAND_T);
TextureBottomRight_ = glm::vec2((MALE_TEXTURE_SIZE_S * SpriteFrame_), MALE_UP_STAND_T);
}
float tex_vert[4][2] = {
{TextureTopRight_.s, TextureTopRight_.t},
{TextureBottomRight_.s, TextureBottomRight_.t},
{TextureBottomLeft_.s, TextureBottomLeft_.t},
{TextureTopLeft_.s, TextureTopLeft_.t},
};
glBindBuffer(GL_ARRAY_BUFFER, VBO_[1]);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float) * 4 * 2, tex_vert);
glBindBuffer(GL_ARRAY_BUFFER, 0);

PositionX_ += MovementX_;
PositionY_ += MovementY_;
}
void Object::Render() {
glm::mat4 Projection, Model;
Projection = glm::orthoLH(0.0f, float(Engine::GetFrameBufferWidth()), 0.0f, float(Engine::GetFrameBufferHeight()), 0.0f, 1000.0f);

Model = glm::translate(Model, glm::vec3(PositionX_, PositionY_, PositionY_));
glm::mat4 mvp = Projection * Model;
GLint MVP = glGetUniformLocation(Shader_.GetProgramID(), "MVP");
glUniformMatrix4fv(MVP, 1, GL_FALSE, glm::value_ptr(mvp));

glUseProgram(Shader_.GetProgramID());
glBindVertexArray(VAO_);
glBindTexture(GL_TEXTURE_2D, Texture_);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}

# These two never change, assigned in object-creation.
float vertices[4][3] = {
{+SpriteWidth_, +SpriteHeight_, 0.0f},
{+SpriteWidth_, -SpriteHeight_, 0.0f},
{-SpriteWidth_, -SpriteHeight_, 0.0f},
{-SpriteWidth_, +SpriteHeight_, 0.0f},
};

unsigned int indices[] = {
2, 1, 0,
2, 0, 3,
};

片段着色器:

out vec4 FragColor;
in vec2 TexCoord;
uniform sampler2D ourTexture;

void main() {
FragColor = texture(ourTexture, TexCoord);
}

顶点着色器:

layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoord;
out vec2 TexCoord;
uniform mat4 MVP;

void main() {
gl_Position = MVP * vec4(aPos, 1.0);
TexCoord = aTexCoord;
}

最佳答案

问题不在于 VBO_[1] 的更新,也就是应该渲染的纹理位置。

# Correct
glBindBuffer(GL_ARRAY_BUFFER, VBO_[1]);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float) * 4 * 2, tex_vert);
glBindBuffer(GL_ARRAY_BUFFER, 0);

问题是由于 glUseProgram 的错误放置,移动/位置(又名 MVP)没有更新正确的对象。第一个对象得到一个空的 MVP,第二个对象得到第一个对象 MVP,第三个对象得到第二个对象 MVP,第一个对象得到第三个对象 MVP 等等。如果在渲染函数的最后使用了 glUseProgram(0),则没有对象将被正确放置,因为 MVP 从未获得真正的值(value)。

# Updated with correct position of glUseProgram()

glm::mat4 Projection, Model;
Projection = glm::orthoLH(0.0f, float(Engine::GetFrameBufferWidth()), 0.0f, float(Engine::GetFrameBufferHeight()), 0.0f, 1000.0f);

Model = glm::translate(Model, glm::vec3(PositionX_, PositionY_, PositionY_));
glm::mat4 mvp = Projection * Model;
GLint MVP = glGetUniformLocation(Shader_.GetProgramID(), "MVP");

glUseProgram(Shader_.GetProgramID()); // This must be loaded before UniformMatrix4fv
glUniformMatrix4fv(MVP, 1, GL_FALSE, glm::value_ptr(mvp));
glBindVertexArray(VAO_);
glBindTexture(GL_TEXTURE_2D, Texture_);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);

因此,请注意给其他感到困惑的人:

glUseProgram()glUniformMatrix4fv()

之前

感谢找到解决方案的 Rabbid76!

关于c++ - OpenGL - 缓冲区更新下一个渲染对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47942099/

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