I try program a simple industrie-roboter arm in C + OpenGl 3.3. I have 3 simple models set up in a single buffer element (Vertex, Index and ColorBuffer) and i can rotate/translate the whole thing around. However, i am unable to translate/rotate a single section.
The Vertex Data is saved in an HEADER file. The Red segment is called "base", blue is "seg1" and green is "seg2". I loaded them into the buffer like so.
size = sizeof(base_verticies) + sizeof(seg1_verticies) + sizeof(seg2_verticies);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, size, 0, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(base_verticies), base_verticies);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(base_verticies), sizeof(seg1_verticies), seg1_verticies);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(base_verticies)+sizeof(seg1_verticies), sizeof(seg2_verticies), seg2_verticies);
size = sizeof(base_indices) + sizeof(seg1_indices) + sizeof(seg2_indices);
glGenBuffers(1, &IBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, 0, GL_STATIC_DRAW);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, sizeof(base_indices), base_indices);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, sizeof(base_indices), sizeof(seg1_indices), seg1_indices);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, sizeof(base_indices)+sizeof(seg1_indices), sizeof(seg2_indices), seg2_indices);
size = sizeof(base_colors) + sizeof(seg1_colors) + sizeof(seg2_colors);
glGenBuffers(1, &CBO);
glBindBuffer(GL_ARRAY_BUFFER, CBO);
glBufferData(GL_ARRAY_BUFFER, size, 0, GL_STATIC_DRAW);
glBufferSubData( ... ); ...; // same for colors
I can now draw those section seperatly like so
//base (red)
glDrawElements(GL_TRIANGLES, sizeof(base_indices)/sizeof(GLushort), GL_UNSIGNED_SHORT, 0);
//seg1 (blue)
glDrawElements(GL_TRIANGLES, sizeof(base_indices)/sizeof(GLushort), GL_UNSIGNED_SHORT, (void*)sizeof(base_indices));
//seg2 (green)
glDrawElements(GL_TRIANGLES, sizeof(base_indices)/sizeof(GLushort), GL_UNSIGNED_SHORT, (void*)(sizeof(base_indices)+sizeof(seg1_indices)));
But my Problem is, that I can only rotate and translate the whole thing, but not single sections. I do rotate the whole model in the onIdle()
function like so:
float angle = 1;
float RotationMatrixAnim[16];
SetRotationY(angle, RotationMatrixAnim);
/* Apply model rotation */
MultiplyMatrix(RotationMatrixAnim, InitialTransform, ModelMatrix);
MultiplyMatrix(TranslateDown, ModelMatrix, ModelMatrix);
Is there a way to modify the position,translation,... for single segments? The only tutorials I find are from legacy OpenGl with glPushMatrix()
and glPopMatrix
, which are deprecated.
Is it a good idea to store those Segments in the same buffer?
Thanks in advance for any help & precious time of yours.
Thanks to @Rabbid76 suggestion i was able to solve my issue.
I made 3 ModelMatrices, that is one for each section
float ModelMatrix[3][16]; /* Model matrix */
I rotate/translate the section i want like so
float RotationMatrixSeg2[16];
SetRotationY(angle, RotationMatrixSeg2);
MultiplyMatrix(RotationMatrixSeg2, InitialTransform, ModelMatrix[2]);
MultiplyMatrix(TranslateDown, ModelMatrix[2], ModelMatrix[2]);
I set the corresbonding ModelMatrix
befor drawing the section.
/* Draw each section */
glUniformMatrix4fv(RotationUniform, 1, GL_TRUE, ModelMatrix[0]);
glDrawElements(GL_TRIANGLES, sizeof(base_indices)/sizeof(GLushort), GL_UNSIGNED_SHORT, 0);
glUniformMatrix4fv(RotationUniform, 1, GL_TRUE, ModelMatrix[1]);
glDrawElements(GL_TRIANGLES, sizeof(base_indices)/sizeof(GLushort), GL_UNSIGNED_SHORT, (void*)sizeof(base_indices));
glUniformMatrix4fv(RotationUniform, 1, GL_TRUE, ModelMatrix[2]);
glDrawElements(GL_TRIANGLES, sizeof(base_indices)/sizeof(GLushort), GL_UNSIGNED_SHORT, (void*)(sizeof(base_indices)+sizeof(seg1_indices)));