PDA

View Full Version : Instancing code doesn't work



Basaaa
12-18-2012, 04:03 PM
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: http://programming4.us/multimedia/8306.aspx , 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('\n');
}
} 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.leng th);
square_vertices_fb.put(square_vertices);
square_vertices_fb.rewind();

FloatBuffer instance_colors_fb = BufferUtils.createFloatBuffer(instance_colors.leng th);
instance_colors_fb.put(instance_colors);
instance_colors_fb.rewind();

FloatBuffer instance_positions_fb = BufferUtils.createFloatBuffer(instance_positions.l ength);
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

menzel
12-18-2012, 11:53 PM
Hello,

you need a vertex and a fragment shader in your shader program, I only see a vertex shader.

Basaaa
12-19-2012, 02:48 AM
Thank you for the answer. As I'm completely new to shaders, and the example didn't provide a fragment shader, could you give me a basic fragment shader?

Thanks, I appreciate it!

Edit:
I tried this, without any success:


void main(void)
{
gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
}

menzel
12-19-2012, 06:05 AM
Can you render anything without instancing with your shaders? How did you set up the shaders? Have you checked for shader compile errors, shader linker errors and OpenGL errors (glGetError) ?

Basaaa
12-19-2012, 06:15 AM
Can you render anything without instancing with your shaders? How did you set up the shaders? Have you checked for shader compile errors, shader linker errors and OpenGL errors (glGetError) ?
Compiling works fine the status said it's compiled successfully. I dont know how to check if there are link errors. Could you provide me with an example?

Thanks again for your help :)

Edit:
I tried to get the error code, and I get a 1281 code just after the first glBufferSubData. Any ideas why?'

Edit 2:
Error is fixed, seems that LWJGL wants the offset in bytes, so *4.

However, now I get no error anywhere, but I don't see anything on my screen.
Any ideas?

menzel
12-19-2012, 10:44 AM
Compiling works fine the status said it's compiled successfully. I dont know how to check if there are link errors. Could you provide me with an example?


See the example code here: http://www.opengl.org/wiki/OpenGL_Shading_Language#Error_Checking

Basaaa
12-19-2012, 11:58 AM
See the example code here: http://www.opengl.org/wiki/OpenGL_Shading_Language#Error_Checking

Thanks for the answer, but I already fixed the error.
I placed it in my last posts. I now simply don't get anything on my screen.

Thanks!

menzel
12-19-2012, 02:00 PM
And I have still open questions: Are there any linking errors (shader linker, not c++ linker)? You asked to to query those, that's why i provided the link with code. Second question is how your code looks like now (as you have added a fragment shader and other fixes).

Basaaa
12-21-2012, 08:17 AM
Hi guys,


I still have not found the solution, and I really want this to work. I packed the eclipse project in a rar. Could anyone please take a look at the code, and see if anything is wrong?

*Click* (http://planowebdesign.nl/project.rar)

If you want to help me, you can also add me on Skype: Masterleik.

Thanks for your time, I really do appreciate it!


~Basaa