Hey guys,
For the last couple of days I got interested in Instanced Rendering. After some research and examples, I tried to do a little test myself.
I followed this guide: Open GL : Drawing a lot of Geometry Efficiently (part 3) - Getting Your Data Automatically - Tutorials,Articles,Algorithms,Tips,Examples about Multimedia , but I’m using LWJGL, so I ported it to Java:
shader.vert:
precision highp float;
in vec4 position;
in vec4 instance_color;
in vec4 instance_position;
out Fragment
{
vec4 color;
} fragment;
uniform mat4 mvp;
void main(void)
{
gl_Position = mvp * (position + instance_position);
fragment.color = instance_color;
}
Code itself:
int shaderProgram = glCreateProgram();
int vertexShader = glCreateShader(GL_VERTEX_SHADER);
StringBuilder vertexShaderSource = new StringBuilder();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("src/shader.vert"));
String line;
while ((line = reader.readLine()) != null) {
vertexShaderSource.append(line).append('
');
}
} catch (IOException e) {
System.err.println("Vertex shader wasn't loaded properly.");
e.printStackTrace();
Display.destroy();
System.exit(1);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}
glShaderSource(vertexShader, vertexShaderSource);
glCompileShader(vertexShader);
if (glGetShader(vertexShader, GL_COMPILE_STATUS) == GL_FALSE) {
System.err.println("Vertex shader wasn't able to be compiled correctly.");
}
glAttachShader(shaderProgram, vertexShader);
glBindAttribLocation(shaderProgram, 0, "position");
glBindAttribLocation(shaderProgram, 1, "instance_color");
glBindAttribLocation(shaderProgram, 2, "instance_position");
glLinkProgram(shaderProgram);
int vaid;
int bid;
IntBuffer va = BufferUtils.createIntBuffer(1);
IntBuffer b = BufferUtils.createIntBuffer(1);
glGenVertexArrays(va);
glGenBuffers(b);
bid = b.get(0);
vaid = va.get(0);
glBindVertexArray(vaid);
glBindBuffer(GL_ARRAY_BUFFER, bid);
int offset = 0;
FloatBuffer square_vertices_fb = BufferUtils.createFloatBuffer(square_vertices.length);
square_vertices_fb.put(square_vertices);
square_vertices_fb.rewind();
FloatBuffer instance_colors_fb = BufferUtils.createFloatBuffer(instance_colors.length);
instance_colors_fb.put(instance_colors);
instance_colors_fb.rewind();
FloatBuffer instance_positions_fb = BufferUtils.createFloatBuffer(instance_positions.length);
instance_positions_fb.put(instance_positions);
instance_positions_fb.rewind();
glBufferData(GL_ARRAY_BUFFER, square_vertices.length + instance_colors.length + instance_positions.length, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, square_vertices_fb);
glBufferSubData(GL_ARRAY_BUFFER, square_vertices.length, instance_colors_fb);
glBufferSubData(GL_ARRAY_BUFFER, square_vertices.length + instance_colors.length, instance_positions_fb);
glVertexAttribPointer(0, 4, GL_FLOAT, false, 0, 0);
glVertexAttribPointer(1, 4, GL_FLOAT, false, 0, square_vertices.length);
glVertexAttribPointer(2, 4, GL_FLOAT, false, 0, square_vertices.length + instance_colors.length);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glVertexAttribDivisor(1, 1);
glVertexAttribDivisor(2, 1);
while (!Display.isCloseRequested()) {
try {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINES);
glColor3f(1, 0, 0);
glVertex3f(-50, -2, -20);
glVertex3f(50, -2, -20);
glColor3f(0, 1, 0);
glVertex3f(0, -50, -20);
glVertex3f(0, 50, -20);
glColor3f(0, 0, 1);
glVertex3f(0, -2, -100);
glVertex3f(0, -2, 10);
glEnd();
glUseProgram(shaderProgram);
glBindVertexArray(vaid);
GL31.glDrawArraysInstanced(GL_TRIANGLE_FAN, 0, 4, 4);
glUseProgram(0);
Display.update();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
I get no error messages.
The axis drawing works fine, but I get no squares in my view! Any ideas on what’s wrong? It has been driving me crazy for a few days now!
Thanks for the help!
~Basaa