Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 9 of 9

Thread: Instancing code doesn't work

Hybrid View

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2012
    Posts
    17

    Instancing code doesn't work

    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:
    Code :
    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:

    Code :
    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.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

  2. #2
    Member Regular Contributor
    Join Date
    Jan 2012
    Location
    Germany
    Posts
    325
    Hello,

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

  3. #3
    Junior Member Newbie
    Join Date
    Nov 2012
    Posts
    17
    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:
    Code :
    void main(void)
    {
    	gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
    }
    Last edited by Basaaa; 12-19-2012 at 04:07 AM. Reason: Added code

  4. #4
    Member Regular Contributor
    Join Date
    Jan 2012
    Location
    Germany
    Posts
    325
    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) ?

  5. #5
    Junior Member Newbie
    Join Date
    Nov 2012
    Posts
    17
    Quote Originally Posted by menzel View Post
    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?
    Last edited by Basaaa; 12-19-2012 at 09:50 AM.

  6. #6
    Member Regular Contributor
    Join Date
    Jan 2012
    Location
    Germany
    Posts
    325
    Quote Originally Posted by Basaaa View Post
    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_Sh...Error_Checking

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •