实例化彼此相邻的OpenGL多维数据集 [英] Instancing cubes next to each other OpenGL

查看:70
本文介绍了实例化彼此相邻的OpenGL多维数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试彼此并排创建多个多维数据集.

I tried to instance many cubes side by side each other.

我的理解是,我需要为每个多维数据集创建一个 MVP 矩阵并将其绑定,然后以某种方式更改位置.然后在for循环中运行您想要的多维数据集.

My understanding is that I need to create a MVP matrix for each cube and bind this then somehow change the position. then run this in a for loop for how many cubes you'd want.

这是我尝试过的:

while (!glfwWindowShouldClose(window)) {
    glClearColor(0.0f / 255.0f, 170.0f / 255.0f, 204.0f / 255.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glUseProgram(programID);

    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
    glVertexAttribPointer(
        0,
        3, GL_FLOAT, GL_FALSE,
        0,
        (void*)0
    );

    computeMatricesFromInputs();
    glm::mat4 ProjectionMatrix = getProjectionMatrix();
    glm::mat4 ViewMatrix = getViewMatrix();
    glm::mat4 ModelMatrix = glm::mat4(1.0);
    glm::mat4 MVP = ProjectionMatrix * ViewMatrix * ModelMatrix;

    for (int i = 0; i == cubes; i++) {
        GLuint MatrixID = glGetUniformLocation(programID, "MVP");
        glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
        glDrawArraysInstanced(GL_TRIANGLES, 0, 1, i);
    }

    //GLuint MatrixID = glGetUniformLocation(programID, "MVP");
    //glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
    //glDrawArrays(GL_TRIANGLES, 0, 12*3);
    glDisableVertexAttribArray(0);

    glEnableVertexAttribArray(1);
    glBindBuffer(GL_ARRAY_BUFFER, texturebuffer);
    glVertexAttribPointer(
        1,
        2,
        GL_FLOAT,
        GL_FALSE,
        0,
        (void*)0
    );
    glfwSwapBuffers(window);
    glfwPollEvents();
}

这根本不会绘制任何多维数据集,而且我无法弄清楚如何将每个多维数据集的位置更改为彼此并排.那么,为什么这段代码不绘制实例化的多维数据集,又如何能够将每个多维数据集的位置更改为彼此相邻呢?

This doesn't draw any cubes at all and I can't figure out how I would change the position for each cube to be side by side each other. So why does this code not draw instanced cubes and how would I be able to change the position of each cube to rest next to each other?

推荐答案

glDrawArraysInstanced 是要分别绘制立方体的顶点数(就像

The 3rd parameter of glDrawArraysInstanced is the number of vertices you want to draw respectively of the cube (just like as glDrawArrays), the 4th is the number of instances to be drawn by 1 draw call, not the index of an instance.

无论如何,您不需要 glDrawArraysInstanced ,因为您分别绘制了每个立方体.改用 glDrawArrays :

Anyway You don't need glDrawArraysInstanced at all, because you draw each cube separately. Use glDrawArrays instead:

for (int i = 0; i < cubes; i++) {
    GLuint MatrixID = glGetUniformLocation(programID, "MVP");
    glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
    glDrawArrays(GL_TRIANGLES, 0, noOfVertices);
}

可以用 glDrawArraysInstanced 代替:

glDrawArraysInstanced(GL_TRIANGLES, 0, noOfVertices, cubes); 

这将立即绘制所有多维数据集 cubes .为了使该工作有效,您必须找到一种为每个实例使用不同的模型矩阵的方法.
一种可能性是使用着色器存储缓冲区对象(SSBO),以存储所有数组中的模型矩阵.在顶点着色器中,实例的索引(多维数据集的索引)可以通过内置输入

That will draw all the cubes cubes at once. To make that work, you have to find a way to use a different model matrix for each instance.
One possibility is to use a Shader Storage Buffer Object (SSBO), to store all the model matrices in an array. In the vertex shader the index of the instance (index of the cube) can be get by the built-in input gl_InstanceID. That variable can be used to get the proper model matrix for the instance form the array.

例如:

layout(std430) buffer Matrices
{
    mat4 mvpArray[];
};

void main()
{
    mat4 mvp = mvpArray[gl_InstanceID];

    // [...]
}

这篇关于实例化彼此相邻的OpenGL多维数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆