RRokas

12-06-2016, 04:12 PM

The Matrix Stack functionality is deprecated and I want to draw multiple objects with different transformations.

I imagine I can accomplish different object transformations by manually sending in a matrix via a uniform to a shader. Here is what I have now:

glUseProgram(shaderProgram);

glBindBuffer(GL_ARRAY_BUFFER, vbo);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);

GLint posAttrib = glGetAttribLocation(shaderProgram, "vertexPosition");

glEnableVertexAttribArray(posAttrib);

GLint colorAttrib = glGetAttribLocation(shaderProgram, "vertexColor");

glEnableVertexAttribArray(colorAttrib);

GLfloat ortho = glGetUniformLocation(shaderProgram, "projection");

float r = 1.875;

float l = -1.875;

float b = -1.5;

float t = 1.5;

float n = 0;

float f = 5;

float orthoElements[] =

{

2 / (r - l), 0, 0, -(r + l) / (r - l),

0, 2 / (t - b), 0, -(t + b) / (t - b),

0, 0, -2 / (f - n), (f + n) / (f - n),

0, 0, 0, 1,

};

glUniformMatrix4fv(ortho, 1, GL_TRUE, orthoElements);

//Draw AXES

glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(sizeof(vertices) + sizeof(colors) + sizeof(edgeColors)));

glVertexAttribPointer(colorAttrib, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(sizeof(vertices) + sizeof(colors) + sizeof(edgeColors) + 3 * sizeof(float)));

glDrawArrays(GL_LINES, 0, sizeof(axes) / (6 * sizeof(axes[0])));

GLfloat rot = glGetUniformLocation(shaderProgram, "rotation_y");

float angle = -45 + persp;

//rotates "angle" degrees about the y-axis

float rotElements[] =

{

cosf(angle), 0, sinf(angle), 0,

0, 1, 0, 0,

-sinf(angle), 0, cosf(angle), 0,

0, 0, 0, 1,

};

glUniformMatrix4fv(rot, 1, GL_TRUE, rotElements);

//draw other stuff

To sum up the code above, I first apply orthographic projection to the whole scene, then I draw the coordinate axes. After that I apply an extra rotation about the y-axis and then draw extra stuff. Because I have axes' draw calls after the projection transformation and before the rotation transformation, I expect the axes be affected only by the projection transformation. But it seems that all transformations whose matrices I send to the shader accumulate and affect everything, be it above or below the transformation code.

Why is that happening? And how can I "protect" my draw calls from transformations below affecting them? In other words, I want to have control over which objects are affected by which transformations.

I imagine I can accomplish different object transformations by manually sending in a matrix via a uniform to a shader. Here is what I have now:

glUseProgram(shaderProgram);

glBindBuffer(GL_ARRAY_BUFFER, vbo);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);

GLint posAttrib = glGetAttribLocation(shaderProgram, "vertexPosition");

glEnableVertexAttribArray(posAttrib);

GLint colorAttrib = glGetAttribLocation(shaderProgram, "vertexColor");

glEnableVertexAttribArray(colorAttrib);

GLfloat ortho = glGetUniformLocation(shaderProgram, "projection");

float r = 1.875;

float l = -1.875;

float b = -1.5;

float t = 1.5;

float n = 0;

float f = 5;

float orthoElements[] =

{

2 / (r - l), 0, 0, -(r + l) / (r - l),

0, 2 / (t - b), 0, -(t + b) / (t - b),

0, 0, -2 / (f - n), (f + n) / (f - n),

0, 0, 0, 1,

};

glUniformMatrix4fv(ortho, 1, GL_TRUE, orthoElements);

//Draw AXES

glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(sizeof(vertices) + sizeof(colors) + sizeof(edgeColors)));

glVertexAttribPointer(colorAttrib, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(sizeof(vertices) + sizeof(colors) + sizeof(edgeColors) + 3 * sizeof(float)));

glDrawArrays(GL_LINES, 0, sizeof(axes) / (6 * sizeof(axes[0])));

GLfloat rot = glGetUniformLocation(shaderProgram, "rotation_y");

float angle = -45 + persp;

//rotates "angle" degrees about the y-axis

float rotElements[] =

{

cosf(angle), 0, sinf(angle), 0,

0, 1, 0, 0,

-sinf(angle), 0, cosf(angle), 0,

0, 0, 0, 1,

};

glUniformMatrix4fv(rot, 1, GL_TRUE, rotElements);

//draw other stuff

To sum up the code above, I first apply orthographic projection to the whole scene, then I draw the coordinate axes. After that I apply an extra rotation about the y-axis and then draw extra stuff. Because I have axes' draw calls after the projection transformation and before the rotation transformation, I expect the axes be affected only by the projection transformation. But it seems that all transformations whose matrices I send to the shader accumulate and affect everything, be it above or below the transformation code.

Why is that happening? And how can I "protect" my draw calls from transformations below affecting them? In other words, I want to have control over which objects are affected by which transformations.