Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualMarkS

Posted 03 January 2013 - 01:17 PM

I'm looking at the tutorial here: http://sol.gfxile.net/instancing.html It seems to be the only and/or best tutorial dealing with ARB_instanced_arrays. However, not much code is presented. I've asked the author, but he seems reluctant to release his code, so I'm hoping I can get an explanation.

int pos = glGetAttribLocation(shader_instancedarrays.program, "transformmatrix");
int pos1 = pos + 0; // <- What is going on here..
int pos2 = pos + 1; // ... and here...
int pos3 = pos + 2; // ... and here...
int pos4 = pos + 3; // ... and here?
glEnableVertexAttribArray(pos1);
glEnableVertexAttribArray(pos2);
glEnableVertexAttribArray(pos3);
glEnableVertexAttribArray(pos4);
glBindBuffer(GL_ARRAY_BUFFER, VBO_containing_matrices); // <- The part I really do not understand
glVertexAttribPointer(pos1, 4, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 4 * 4, (void*)(0));
glVertexAttribPointer(pos2, 4, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 4 * 4, (void*)(sizeof(float) * 4));
glVertexAttribPointer(pos3, 4, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 4 * 4, (void*)(sizeof(float) * 8));
glVertexAttribPointer(pos4, 4, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 4 * 4, (void*)(sizeof(float) * 12));
glVertexAttribDivisor(pos1, 1);
glVertexAttribDivisor(pos2, 1);
glVertexAttribDivisor(pos3, 1);
glVertexAttribDivisor(pos4, 1);


From what I can surmise, the code gets the location of the transformation matrix in the shader program. (Guessing here...) It then creates four positions, corresponding to four instances. Then a VBO is bound. I'm guessing that the VBO contains an array of vertices, each defined by four floats, grouped into four representing a 4x4 matrix, and each representing a position of the corresponding instance. So, the VBO in this example contains 16 vertices, with vertex 1-4 representing a matrix, 5-8 representing another matrix, etc.

Is my understanding correct? The tutorial really isn't a tutorial, but a description of the various methods to accomplish instancing, and thus, is missing important details.

If my understanding of the VBO is correct, it brings up another question. Can I assume that the matrices stored in the VBO are represented as:

|0 4  8 12| = vertex 1, 5, 9, etc.
|1 5  9 13| = vertex 2, 6, 10, etc.
|2 6 10 14| = vertex 3, 7, 11, etc.
|3 7 11 15| = vertex 4, 8, 12, etc.


I'm surprised that there is so little information available on instancing implementation.


#3MarkS

Posted 03 January 2013 - 12:31 PM

I'm looking at the tutorial here: http://sol.gfxile.net/instancing.html It seems to be the only and/or best tutorial dealing with ARB_instanced_arrays. However, not much code is presented. I've asked the author, but he seems reluctant to release his code, so I'm hoping I can get an explanation.

int pos = glGetAttribLocation(shader_instancedarrays.program, "transformmatrix");
int pos1 = pos + 0; // <- What is going on here..
int pos2 = pos + 1; // ... and here...
int pos3 = pos + 2; // ... and here...
int pos4 = pos + 3; // ... and here?
glEnableVertexAttribArray(pos1);
glEnableVertexAttribArray(pos2);
glEnableVertexAttribArray(pos3);
glEnableVertexAttribArray(pos4);
glBindBuffer(GL_ARRAY_BUFFER, VBO_containing_matrices); // <- The part I really do not understand
glVertexAttribPointer(pos1, 4, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 4 * 4, (void*)(0));
glVertexAttribPointer(pos2, 4, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 4 * 4, (void*)(sizeof(float) * 4));
glVertexAttribPointer(pos3, 4, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 4 * 4, (void*)(sizeof(float) * 8));
glVertexAttribPointer(pos4, 4, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 4 * 4, (void*)(sizeof(float) * 12));
glVertexAttribDivisor(pos1, 1);
glVertexAttribDivisor(pos2, 1);
glVertexAttribDivisor(pos3, 1);
glVertexAttribDivisor(pos4, 1);


From what I can surmise, the code gets the location of the transformation matrix in the shader program. (Guessing here...) It then creates four positions, corresponding to four instances. Then a VBO is bound. I'm guessing that the VBO contains an array of vertices, each defined by four floats, grouped into four representing a 4x4 matrix, and each representing a position of the corresponding instance. So, the VBO in this example contains 16 floats, with float 1-4 representing a matrix, 5-8 representing another matrix, etc.

Is my understanding correct? The tutorial really isn't a tutorial, but a description of the various methods to accomplish instancing, and thus, is missing important details.

If my understanding of the VBO is correct, it brings up another question. Can I assume that the matrices stored in the VBO are represented as:

|0 4  8 12| = float 1, 5, 9, etc.
|1 5  9 13| = float 2, 6, 10, etc.
|2 6 10 14| = float 3, 7, 11, etc.
|3 7 11 15| = float 4, 8, 12, etc.


I'm surprised that there is so little information available on instancing implementation.


#2MarkS

Posted 03 January 2013 - 12:28 PM

I'm looking at the tutorial here: http://sol.gfxile.net/instancing.html It seems to be the only and/or best tutorial dealing with ARB_instanced_arrays. However, not much code is presented. I've asked the author, but he seems reluctant to release his code, so I'm hoping I can get an explanation.

int pos = glGetAttribLocation(shader_instancedarrays.program, "transformmatrix");
int pos1 = pos + 0; // <- What is going on here..
int pos2 = pos + 1; // ... and here...
int pos3 = pos + 2; // ... and here...
int pos4 = pos + 3; // ... and here?
glEnableVertexAttribArray(pos1);
glEnableVertexAttribArray(pos2);
glEnableVertexAttribArray(pos3);
glEnableVertexAttribArray(pos4);
glBindBuffer(GL_ARRAY_BUFFER, VBO_containing_matrices); // <- The part I really do not understand
glVertexAttribPointer(pos1, 4, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 4 * 4, (void*)(0));
glVertexAttribPointer(pos2, 4, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 4 * 4, (void*)(sizeof(float) * 4));
glVertexAttribPointer(pos3, 4, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 4 * 4, (void*)(sizeof(float) * 8));
glVertexAttribPointer(pos4, 4, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 4 * 4, (void*)(sizeof(float) * 12));
glVertexAttribDivisor(pos1, 1);
glVertexAttribDivisor(pos2, 1);
glVertexAttribDivisor(pos3, 1);
glVertexAttribDivisor(pos4, 1);


From what I can surmise, the code gets the location of the transformation matrix in the shader program. (Guessing here...) It then creates four positions, corresponding to four instances. Then a VBO is bound. I'm guessing that the VBO contains an array of vertices, each defined by four floats, grouped into four representing a 4x4 matrix, and each representing a position of the corresponding instance. So, the VBO contains 16 floats, with float 1-4 representing a matrix, 5-8 representing another matrix, etc.

Is my understanding correct? The tutorial really isn't a tutorial, but a description of the various methods to accomplish instancing, and thus, is missing important details.

If my understanding of the VBO is correct, it brings up another question. Can I assume that the matrices stored in the VBO are represented as:

|0 4  8 12| = float 1, 5, 9, etc.
|1 5  9 13| = float 2, 6, 10, etc.
|2 6 10 14| = float 3, 7, 11, etc.
|3 7 11 15| = float 4, 8, 12, etc.


I'm surprised that there is so little information available on instancing implementation.


#1MarkS

Posted 03 January 2013 - 12:26 PM

I'm looking at the tutorial here: http://sol.gfxile.net/instancing.html it seems to be the only and/or best tutorial dealing with ARB_instanced_arrays. However, not much code is presented. I've asked the author, but he seems reluctant to release his code, so I'm hoping I can get an explanation.
int pos = glGetAttribLocation(shader_instancedarrays.program, "transformmatrix");
int pos1 = pos + 0; // <- What is going on here..
int pos2 = pos + 1; // ... and here...
int pos3 = pos + 2; // ... and here...
int pos4 = pos + 3; // ... and here?
glEnableVertexAttribArray(pos1);
glEnableVertexAttribArray(pos2);
glEnableVertexAttribArray(pos3);
glEnableVertexAttribArray(pos4);
glBindBuffer(GL_ARRAY_BUFFER, VBO_containing_matrices); // <- The part I really do not understand
glVertexAttribPointer(pos1, 4, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 4 * 4, (void*)(0));
glVertexAttribPointer(pos2, 4, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 4 * 4, (void*)(sizeof(float) * 4));
glVertexAttribPointer(pos3, 4, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 4 * 4, (void*)(sizeof(float) * 8));
glVertexAttribPointer(pos4, 4, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 4 * 4, (void*)(sizeof(float) * 12));
glVertexAttribDivisor(pos1, 1);
glVertexAttribDivisor(pos2, 1);
glVertexAttribDivisor(pos3, 1);
glVertexAttribDivisor(pos4, 1);

From what I can surmise, the code gets the location of the transformation matrix in the shader program. (Guessing) It then creates four positions, corresponding to four instances. Then a VBO is bound. I'm guessing that the VBO contains an array of vertices, each defined by four floats, grouped into four representing a 4x4 matrix, and each representing a position of the corresponding instance. So, the VBO contains 16 floats, with float 1-4 representing a matrix, 5-8 representing another matrix, etc.

Is my understanding correct? The tutorial really isn't a tutorial, but a description of the various methods to accomplish instancing, and thus, is missing important details.

If my understanding of the VBO is correct, it brings up another question:

Can I assume that the matrices stored in the VBO are represented as:
|0 4  8 12| = float 1, 5, 9, etc.
|1 5  9 13| = float 2, 6, 10, etc.
|2 6 10 14| = float 3, 7, 11, etc.
|3 7 11 15| = float 4, 8, 12, etc.

I'm surprised that there is so little information available on instancing implementation.

PARTNERS