如何在OpenGL中以正确的方式实现实例化。

时间:2014-02-03 22:32:45

标签: c++ opengl gpu vbo gpu-programming

我试图以最有效的方式使用VBO和Instancing机制。我有一个基于体素的世界,我想用尽可能少的绘制调用来绘制它们。下面的代码用VB编写VBO:

void VoxelView::initVBOs() {
/*--------------------- Main OpenGL Program ---------------------*/
/* Vertices of a triangle (counter-clockwise winding) */
float data[6][3] = {
                                            // Left bottom triangle
                                            { -0.5f, 0.5f, 0.0f  },
                                            { -0.5f, -0.5f, 0.0f },
                                            { 0.5f, -0.5f, 0.0f  },
                                            // Right top triangle
                                            { 0.5f, -0.5f, 0.0f  },
                                            { 0.5f, 0.5f, 0.0f   },
                                            { -0.5f, 0.5f, 0.0f  }

                                       };

/*---------------------- Initialise VBO - (Note: do only once, at start of program) ---------------------*/
/* Create a new VBO and use the variable "triangleVBO" to store the VBO id */
glGenBuffers(1, &triangleVBO);

/* Make the new VBO active */
glBindBuffer(GL_ARRAY_BUFFER, triangleVBO);

/* Upload vertex data to the video device */
glBufferData(GL_ARRAY_BUFFER, 6 * 3 * sizeof(float), data, GL_STATIC_DRAW);

/* Specify that our coordinate data is going into attribute index 0(shaderAttribute), and contains three floats per vertex */
glVertexAttribPointer(shaderAttribute, 3, GL_FLOAT, GL_FALSE, 0, 0);

/* Enable attribute index 0(shaderAttribute) as being used */
glEnableVertexAttribArray(shaderAttribute);

/* Make the new VBO active. */
glBindBuffer(GL_ARRAY_BUFFER, triangleVBO);

/*-------------------------------------------------------------------------------------------------------*/

/*--------------------- Load Vertex and Fragment shaders from files and compile them --------------------*/
/* Read our shaders into the appropriate buffers */
vertexSource = filetobuf("Shaders/exampleVertexShader1.vert");
fragmentSource = filetobuf("Shaders/exampleFragmentShader1.frag");

/* Assign our handles a "name" to new shader objects */
vertexShader = glCreateShader(GL_VERTEX_SHADER);
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

/* Associate the source code buffers with each handle */
glShaderSource(vertexShader, 1, (const GLchar**)&vertexSource, 0);
glShaderSource(fragmentShader, 1, (const GLchar**)&fragmentSource, 0);

/* Free the temporary allocated memory */
free(vertexSource);
free(fragmentSource);

/* Compile our shader objects */
glCompileShader(vertexShader);
glCompileShader(fragmentShader);
/*-------------------------------------------------------------------------------------------------------*/

/*-------------------- Create shader program, attach shaders to it and then link it ---------------------*/
/* Assign our program handle a "name" */
shaderProgram = glCreateProgram();

/* Attach our shaders to our program */
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);

/* Bind attribute index 0 (shaderAttribute) to in_Position*/
/* "in_Position" will represent "data" array's contents in the vertex shader */
glBindAttribLocation(shaderProgram, shaderAttribute, "in_Position");

/* Link shader program*/
glLinkProgram(shaderProgram); 

我按以下方式渲染四边形:

void VoxelView::renderVBO()
{
/* Set shader program as being actively used */
glUseProgram(shaderProgram);

/* Set background colour to BLACK */
glClearColor(0.0, 0.0, 0.0, 1.0);

/* Clear background with BLACK colour */
glClear(GL_COLOR_BUFFER_BIT);

/* Actually draw the triangle, giving the number of vertices provided by invoke glDrawArrays
   while telling that our data is a triangle and we want to draw 0-3 vertexes
*/
glDrawArrays(GL_TRIANGLES, 0, 6);
}

我想使用实例化机制多次绘制这个四边形(使用VBO)。我希望它非常简单,因为我想为更复杂的代码实现它。我知道我应该使用 glDrawElementsInstanced 方法来使用实例化,但我不知道该怎么做。有人知道怎么做吗?

1 个答案:

答案 0 :(得分:4)

使用glDrawElementsInstanced时,需要使着色器使用gl_InstanceiD

#version 410

layout (location = 0) in vec3 Position; 
layout (location = 1) in vec2 TexCoord; 
layout (location = 2) in vec3 Normal; 
layout (location = 3) in mat4 WVP; 
layout (location = 7) in mat4 World; 

out vec2 TexCoord0; 
out vec3 Normal0; 
out vec3 WorldPos0; 
flat out int InstanceID; 

void main() 
{ 
    gl_Position = WVP * vec4(Position, 1.0); 
    TexCoord0 = TexCoord; 
    Normal0 = World * vec4(Normal, 0.0)).xyz; 
    WorldPos0 = World * vec4(Position, 1.0)).xyz; 
    InstanceID = gl_InstanceID; 
};

glDrawElementsInstanced使用gl_InstanceID变量,就像它是一个静态整数顶点属性一样。当顶点的第一个副本发送到OpenGL时, gl_InstanceID将为零。然后,对于几何体的每个副本,它将递增一次,最终将达到instanceCount - 1。

表现得像这样

for (int n = 0; n < instancecount; n++)
{
// Set the value of gl_InstanceID
glVertexAttrib1i(gl_InstanceID, n); // except this is internally incremented and not a real vertex attribute
// Make a normal call to glDrawElements
glDrawElements(mode, count, type, indices);
}

除了这在内部发生,您只需要拨打glDrawArraysInstanced()一次。为了对每个实例使用不同的变换,你可以使用gl_InstanceID传递一个统一矩阵数组并使用glDrawElementsInstanced(primitivetype, indices, GL_UNSIGNED_INT, 0, instanceCount-1); 进行索引,或者你可以使用纹理缓冲区对象并在那里存储变换。

所以你的绘图代码应该是这样的,但请注意你仍然需要为数组中的每个intsance传递统一矩阵或者为所有传递纹理缓冲区对象。

uniform mat4 instancematrices[32];

void main()
{
  gl_Position = gl_ModelViewProjectionMatrix * instancematrices[gl_InstanceID] * gl_Vertex;
}

并且您的顶点着色器应该变为

{{1}}