0

My question connected with another my question: How to render large number of similar objects?

I trying to render set of points. For one point I using this code:

glUseProgram(programId);
glUniformMatrix4fv(matrixId, 1, GL_FALSE, &(vp * getModelMatrix(pos, scale))[0][0]);

glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

glDrawArrays(GL_POINTS, 0, 1);
glDisableVertexAttribArray(0);

I slightly puzzled with task about set of points. I allocated more memory for vertices:

glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, totalParticleCount * 3 * sizeof(GLfloat), gVertexBufferData, GL_STATIC_DRAW);

And I using this draw call for rendering:

glDrawArrays(GL_POINTS, 0, totalParticleCount);

But I don't understand which transformation I must use for glUniformMatrix4fv function and how I must set vertices. I'm trying do this:

for (int i = 0; i < totalParticleCount; i++) {
    gVertexBufferData[3 * i + 0] = hState[i].pos.x;
    gVertexBufferData[3 * i + 1] = hState[i].pos.y;
    gVertexBufferData[3 * i + 2] = hState[i].pos.z;
}

And set uniform matrix:

glUniformMatrix4fv(matrixId, 1, GL_FALSE, &vp[0][0]);

In my case I see nothing and I can't understand whats wrong.

Community
  • 1
  • 1
Danil Prokhorenko
  • 1,052
  • 1
  • 10
  • 26
  • I Think it's better to post it in GameDev exchange. gamedev.stackexchange.com – iFarbod Oct 16 '15 at 13:45
  • 4
    @iFarbod Why? It's perfectly OK here. – Bartek Banachewicz Oct 16 '15 at 13:54
  • `&(vp * getModelMatrix(pos, scale))[0][0]` <- looks like taking an address of a temporary. – Bartek Banachewicz Oct 16 '15 at 13:55
  • 2
    @BartekBanachewicz: Pretty sure that temporary should [last for the duration of the expression](http://stackoverflow.com/a/2506800/44729), long enough for `glUniformMatrix4fv()` to copy the information it needs. You might be thinking of `glVertex*Pointer()` using client-side memory: those pointers need to last until `glDrawArrays()`/`glDrawElements()` completes. – genpfault Oct 16 '15 at 14:08
  • @BartekBanachewicz Actually I have a little bit different code but I have the same logic. I checked this code before publishing and it works fine. – Danil Prokhorenko Oct 17 '15 at 02:26

1 Answers1

0

This post (https://gamedev.stackexchange.com/questions/90471/should-unbind-buffers) turned out very helpful in my situation. After updating vertices I must bind VAO and VBO again. Now I have this working code:

for (int i=0; i<totalParticleCount; i++) {
    gVertexBufferData[3 * i + 0] = hState[i].pos.x;
    gVertexBufferData[3 * i + 1] = hState[i].pos.y;
    gVertexBufferData[3 * i + 2] = hState[i].pos.z;
}

glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, totalParticleCount * 3 * sizeof(GLfloat), gVertexBufferData, GL_STATIC_DRAW);

glUseProgram(programId);
glUniformMatrix4fv(matrixId, 1, GL_FALSE, &vp[0][0]);
Community
  • 1
  • 1
Danil Prokhorenko
  • 1,052
  • 1
  • 10
  • 26