PDA

View Full Version : Using matrix stack - my code seems to modify the whole stack



chromosom
06-03-2014, 06:20 PM
I want to place objects using matrix stack, but the following code seems to modify the whole stack, so that using stack becomes a nonsense. What would you do in order to use stack? I need it to create a simple tree.


MatrixStack store;

void DrawTree1(glutil::MatrixStack &modelMatrix)
{
int branches=3;
float bl=14.0f;
float ratio=0.55f;
float bl1=bl*ratio;
float bl2=bl1*ratio;
float bl3=bl2*ratio;
float r=0.9f;
float x=20.0f;
float y=20.0f;
float z=0.5f;
float z1=1.0f;
float z2=1.5f;
float z3=2.0f;
float alpha=30.0f;

modelMatrix.Translate(glm::vec3(x, bl/2, y));
glutil::PushStack push(modelMatrix);
modelMatrix.Scale(glm::vec3(r, bl, r));
glUseProgram(UniformColorTint.theProgram);
glUniformMatrix4fv(UniformColorTint.modelToWorldMa trixUnif, 1, GL_FALSE, glm::value_ptr(modelMatrix.Top()));
glUniform4f(UniformColorTint.baseColorUnif, 0.5f, 0.5f, 0.5f, 0.0f);
g_pCubeTintMesh->Render();
glUseProgram(0);

for(int i=0;i<=branches;i++)
{
// z=1.0f;
glutil::PushStack pop(modelMatrix);
glutil::PushStack push(modelMatrix);
modelMatrix.Translate(glm::vec3(0, bl/2, 0));
modelMatrix.RotateZ(alpha+sin(i*3.0f)*10.0f);
modelMatrix.Translate(glm::vec3(0, bl1/2, 0));
modelMatrix.Scale(1.0f,ratio,1.0f);
glUseProgram(UniformColorTint.theProgram);
glUniformMatrix4fv(UniformColorTint.modelToWorldMa trixUnif, 1, GL_FALSE, glm::value_ptr(modelMatrix.Top()));
glUniform4f(UniformColorTint.baseColorUnif, 0.5f, 0.5f, 0.5f, 0.0f);
g_pCubeTintMesh->Render();
glUseProgram(0);
}
}

reto.koradi
06-03-2014, 10:06 PM
Your push and pop calls are unbalanced. Your stack will be 1 item bigger after every time you call this function, and will eventually overflow.

chromosom
06-04-2014, 02:12 AM
Okay I can fix it easily, but why the whole stack is modified?