Opengl Translate/Rotate 来自同一缓冲区的单个对象

Opengl Translate/Rotate single Objects from same Buffer

我尝试在 C + OpenGl 3.3 中编写一个简单的工业机器人手臂。我在单个缓冲区元素(Vertex、Index 和 ColorBuffer)中设置了 3 个简单模型,我可以 rotate/translate 解决所有问题。但是,我无法 translate/rotate 一个部分。

顶点数据保存在 HEADER 文件中。红色段称为 "base",蓝色段为 "seg1",绿色段为 "seg2"。我像这样将它们加载到缓冲区中。

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

我现在可以像这样单独绘制那些部分了

//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)));

但我的问题是,我只能旋转和平移整个内容,而不是单个部分。我确实在 onIdle() 函数中旋转整个模型,如下所示:

float angle = 1;
float RotationMatrixAnim[16];
SetRotationY(angle, RotationMatrixAnim);

/* Apply model rotation */
MultiplyMatrix(RotationMatrixAnim, InitialTransform, ModelMatrix);
MultiplyMatrix(TranslateDown, ModelMatrix, ModelMatrix);
  1. 有没有办法修改单个片段的位置、翻译...?我找到的唯一教程来自带有 glPushMatrix()glPopMatrix 的遗留 OpenGl,它们已被弃用。

  2. 将这些段存储在同一个缓冲区中是个好主意吗?

在此先感谢您的帮助和宝贵的时间。

感谢 @Rabbid76 的建议,我得以解决我的问题。

  1. 我制作了 3 个模型矩阵,每个部分一个

    float ModelMatrix[3][16]; /* Model matrix */
    
  2. 我rotate/translate我想要的部分是这样的

    float RotationMatrixSeg2[16];
    SetRotationY(angle, RotationMatrixSeg2);
    
    MultiplyMatrix(RotationMatrixSeg2, InitialTransform, ModelMatrix[2]);
    MultiplyMatrix(TranslateDown, ModelMatrix[2], ModelMatrix[2]);
    
  3. 我设置了相应的ModelMatrix然后绘制剖面。

    /* 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)));