c++ - 渲染多个对象的 OpenGL 问题

标签 c++ opengl render

我是 OpenGL 的新手,在渲染多个对象时遇到一些困难。

我有一个 vector ,每个 vector 都有自己的 VertexBuffer。然后,在 while 循环中,我自己绘制每个形状。

当我有许多相同的对象(多个立方体等)时,一切都很好,但是,当我添加一个三角形网格时,一切都变得一团糟。

我可以有很多立方体

enter image description here

我可以有一个三角形网格:

enter image description here

但是,当我尝试创建一个立方体然后创建一个三角形网格时,我得到:

enter image description here

我完全不知道发生了什么。下面提供了我的循环代码。

while (!glfwWindowShouldClose(window))
    {

        // Get the size of the window
        int width, height;
        glfwGetWindowSize(window, &width, &height);

        float aspect_ratio = 1 * float(height)/float(width); // corresponds to the necessary width scaling

        double xpos, ypos;
        glfwGetCursorPos(window, &xpos, &ypos);

        // Clear the framebuffer
        glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        // Enable depth test
        glEnable(GL_DEPTH_TEST);

        glUniform3f(program.uniform("triangleColor"), 1.0f, 1.0f, 1.0f);

        glUniformMatrix4fv(program.uniform("proj"), 1, GL_FALSE, projection.data());
        glUniformMatrix4fv(program.uniform("view"), 1, GL_FALSE, view.data());

        int tally = 0;
    for (int i = 0; i < surfaces.size(); i++) {

        Surface *s = surfaces[i];

        Vector3f color = s->getColor();

        int tempIndex = triangleIndex;
        Matrix4f model = s->getModel();

        // Convert screen position to world coordinates
        double xworld = ((xpos/double(width))*2)-1;
        double yworld = (((height-1-ypos)/double(height))*2)-1; // NOTE: y axis is flipped in glfw

        if (isPressed && mode == "translate") { 
            if(tempIndex == i) {
                Vector4f center = s->getCenter() + model.col(3);
                Vector4f displacement = Vector4f(xworld, yworld, 0, 1) - center;
                Matrix4f translation = translateMatrix(displacement(0), displacement(1), displacement(2));
                model = translation * s->getModel();
                s->setModel(model);
            }
        }
        glUniform3f(program.uniform("triangleColor"), color(0), color(1), color(2));
        glUniformMatrix4fv(program.uniform("model"), 1, GL_FALSE, model.data()); 
        glDrawArrays(GL_TRIANGLES, 0, s->getVertices().size());
    }

我在将对象设为 as 时初始化每个 VBO

VertexBufferObject VBO;
VBO.init();
VBO.update(Vertices);
program.bindVertexAttribArray("position", VBO);

Surface* s = new Surface(VBO, Vertices, percentScale, 0, transformedCenter, SmoothNormals, FlatNormals, color);
s->setModel(model);
surfaces.push_back(s);

Program::bindVertexAttribArray 定义为

GLint Program::bindVertexAttribArray(
        const std::string &name, VertexBufferObject& VBO) const
{
  GLint id = attrib(name);
  if (id < 0)
    return id;
  if (VBO.id == 0)
  {
    glDisableVertexAttribArray(id);
    return id;
  }
  VBO.bind();
  glEnableVertexAttribArray(id);
  glVertexAttribPointer(id, VBO.rows, GL_FLOAT, GL_FALSE, 0, 0);
  check_gl_error();

  return id;
}

最佳答案

在绘制调用之前您没有绑定(bind)任何缓冲区。您可能只是在绘制初始化它们时上次绑定(bind)的任何缓冲区。在 glDrawArrays 之前的循环结束时,您需要这样的东西:

...
program.bindVertexAttribArray("position", VBO); // where VBO is the buffer of surface s
glUniform3f(program.uniform("triangleColor"), color(0), color(1), color(2));
glUniformMatrix4fv(program.uniform("model"), 1, GL_FALSE, model.data()); 
glDrawArrays(GL_TRIANGLES, 0, s->getVertices().size());

关于c++ - 渲染多个对象的 OpenGL 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53494213/

相关文章:

c - X : trigger events at fixed intervals

c - 将 3d 模型文件(3ds/obj/任何开放格式)转换为 C 数组的工具

Java bufferstrategy图形或整型数组

c++ - OpenGL 阴影映射 - 阴影在错误的位置

c++ - 编译程序包含 extern "C"

c++ - OpenGL:GL_QUADS 不绘制正方形

c# - Visual Studio 2012 调试多个项目

c++ - 如何将一个 int 数组发送到我的着色器

razor - Umbraco - 使用 Razor 渲染 .Net 用户控件 (ascx) 宏

对象数组的 C++ 文件输出