0

我正在尝试使用 . 在不同位置渲染一个三角形 100 次glDrawElementsInstanced()

我没有得到正确的方法。我尝试了不同的教程,但它们都适用于 c++ 而不是 java。最后一个是这个

这是我的示例代码:

float offsets = new float[300];

int i = 0;
float offset = 0.1f;
for (int x = -10; x < 10; x += 2) {
    for (int y = -10; y < 10; y += 2) {
        offsets[i] = (float) x / 10.0f + offset;
        offsets[i + 1] = (float) y / 10.0f + offset;
        offsets[i + 2] = 0.0f;
        i += 3;
    }
}        

FloatBuffer attribData = BufferUtils.createFloatBuffer( offsets.length );
attribData.put( offsets, 0,offsets.length );
attribData.flip();


int bufferID = glGenBuffer();       
glBindBuffer( GL_ARRAY_BUFFER, bufferID );
glBufferData( GL_ARRAY_BUFFER, 0,          m_iUsage );
glBufferData( GL_ARRAY_BUFFER, attribData, m_iUsage );
glEnableVertexAttribArray(index);
glVertexAttribPointer(index,3,GL_FLOAT, false,0,null);
glVertexAttribDivisor(index,1);

glBindVertexArray( m_iVAOid );
glDrawElementsInstanced(GL_TRIANGLES, 3,GL_UNSIGNED_INT,0, 100);
glBindVertexArray( 0 );

这是我的顶点着色器:

#version 330 core

layout(location=0) in vec3 aPosition;
layout(location=1) in vec3 aColor;
layout(location=2) in vec3 aOffset;

out vec3 fColor;

uniform mat4 uModel;
uniform mat4 uView;
uniform mat4 uProjection;

void main(void) 
{
    gl_Position = uProjection * uView * uModel* vec4(aPosition+aOffset, 1.0);
    fColor = aColor;
}

目前我渲染一个正方形,中心在 0,0,0。

4

0 回答 0