PDA

View Full Version : second UBO doesn't work



mlkn_mlkn
05-11-2015, 12:34 PM
Hello, everyone.

I'm using two uniform blocks in my shader program and it works well as soon as "//glDrawArrays(GL_POINTS, 0, bulletsMaxCount);" call is commented.
I suspect i'm doing something wrong with some kind of pointer (glVertexPointer maybe), but I don't know what.

Here is init code:


//buffers init
glGenBuffers(1, &g_GlobalValuesUBO);
glBindBuffer(GL_UNIFORM_BUFFER, g_GlobalValuesUBO);
glBufferData(GL_UNIFORM_BUFFER, sizeof(glm::mat4) * 2, NULL, GL_STREAM_DRAW);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
glBindBufferBase(GL_UNIFORM_BUFFER, g_iGlobalValuesBindingIndex, g_GlobalValuesUBO);

glGenBuffers(1, &g_LightBlockUBO);
glBindBuffer(GL_UNIFORM_BUFFER, g_LightBlockUBO);
glBufferData(GL_UNIFORM_BUFFER, sizeof(glm::vec4) * 3, NULL, GL_STREAM_DRAW); //4 instead of bulletsMaxCount so far
glBindBuffer(GL_UNIFORM_BUFFER, 0);
glBindBufferBase(GL_UNIFORM_BUFFER, g_iLightBlockBindingIndex, g_LightBlockUBO);

glGenBuffers(1, &shipVertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, shipVertexBuffer);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

glGenBuffers(1, &shipUVBuffer);
glBindBuffer(GL_ARRAY_BUFFER, shipUVBuffer);
glBufferData(GL_ARRAY_BUFFER, uvs.size() * sizeof(glm::vec2), &uvs[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

glGenBuffers(1, &shipNormalBuffer);
glBindBuffer(GL_ARRAY_BUFFER, shipNormalBuffer);
glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(glm::vec3), &normals[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

glGenBuffers(1, &g_imposterVBO);
glBindBuffer(GL_ARRAY_BUFFER, g_imposterVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(myBullets), myBullets, GL_STREAM_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

//VAO's init

//spaceship VAO
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

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

glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, shipUVBuffer);
glVertexAttribPointer( 1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0 );

glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, shipNormalBuffer);
glVertexAttribPointer( 2, 3, GL_FLOAT, GL_FALSE, 0, (void*)0 );
glBindVertexArray(0);

//bullets VAO
glGenVertexArrays(1, &g_imposterVAO);
glBindVertexArray(g_imposterVAO);

glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, g_imposterVBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 28, (void*)(0));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 28, (void*)(12));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, 28, (void*)(24));

glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);


and there is code from draw() call:


glBindBuffer(GL_UNIFORM_BUFFER, g_LightBlockUBO);
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(glm::vec4), &ambientIntensity[0]);
glBufferSubData(GL_UNIFORM_BUFFER, sizeof(glm::vec4), sizeof(glm::vec4), &sunPosition[0]);
glBufferSubData(GL_UNIFORM_BUFFER, sizeof(glm::vec4) * 2, sizeof(glm::vec4), &sunIntensity[0]);
glBindBuffer(GL_UNIFORM_BUFFER, 0);

glBindBuffer(GL_UNIFORM_BUFFER, g_GlobalValuesUBO);
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(glm::mat4), &View[0][0]);
glBindBuffer(GL_UNIFORM_BUFFER, 0);

glBindBuffer(GL_ARRAY_BUFFER, g_imposterVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(myBullets), myBullets, GL_STREAM_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

glUseProgram(theProgram);

glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]); //to be deleted

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, Texture);
glUniform1i(TextureID, 0);

glBindVertexArray(vao);

glDrawArrays(GL_TRIANGLES, 0, vertices.size());

glBindVertexArray(0);
glUseProgram(0);

glUseProgram(bulletsProgram);
glUniform1f(bulletsTimeUnif, float(currentTime));

glBindVertexArray(g_imposterVAO);

// glDrawArrays(GL_POINTS, 0, bulletsMaxCount);

glBindVertexArray(0);
glUseProgram(0);


It works correct with just one UBO.
Sorry if this question is kind of wrong and thanks in advance.