移动,翻译vbo中包含的对象

时间:2013-12-09 11:51:35

标签: opengl vbo vao

我无法理解如何实际翻译vbo中包含的单个对象。 所以首先我设置vao和vbo并绑定并输入立方体的顶点...

    glGenVertexArrays(1, &_vertexArray1); //Bind to first VAO
    glBindVertexArray(_vertexArray1);
    glGenBuffers(1, &_vertexBufferCube1);
    glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferCube1);
    glBufferData(GL_ARRAY_BUFFER, g_point_count * 3 * sizeof (float), &g_vp[0], GL_STATIC_DRAW);

glEnableVertexAttribArray(loc1);
glVertexAttribPointer(loc1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(loc2);
glVertexAttribPointer(loc2, 3, GL_FLOAT, GL_FALSE, 0, NULL);`

然后在我的显示功能......

    // tell GL to only draw onto a pixel if the shape is closer to the viewer
glEnable (GL_DEPTH_TEST); // enable depth-testing
glDepthFunc (GL_LESS); // depth-testing interprets a smaller value as "closer"
glClearColor (0.5f, 0.5f, 0.5f, 1.0f);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram (shaderProgramID);


//Declare your uniform variables that will be used in your shader
int matrix_location = glGetUniformLocation (shaderProgramID, "model");
int view_mat_location = glGetUniformLocation (shaderProgramID, "view");
int proj_mat_location = glGetUniformLocation (shaderProgramID, "proj");

mat4 view = identity_mat4 ();
mat4 persp_proj = perspective(45.0, (float)width/(float)height, 0.1, 100.0);
mat4 model = identity_mat4 ();
view = translate (view, vec3 (0.0, 0.0, -5.0f));

glUniformMatrix4fv (proj_mat_location, 1, GL_FALSE, persp_proj.m);
glUniformMatrix4fv (view_mat_location, 1, GL_FALSE, view.m);
glUniformMatrix4fv (matrix_location, 1, GL_FALSE, model.m);


glBindVertexArray(_vertexArray1);
//model = translate (view, vec3 (0.0, -3.0F, 0.0));

glDrawArrays (GL_TRIANGLES, 0, g_point_count);
glBindVertexArray(0);

现在我的问题是我想在一个vao中独立于另一个移动对象。我该怎么做?我是否应该翻译模型矩阵然后取消绑定vao并绑定另一个并再次翻译相同的模型矩阵? 谷歌搜索了一段时间后,我想我应该使用glPushMatrix和glTranslatef,但我推动什么矩阵?

基本上我的vao矩阵需要翻译才能在vao中移动对象?

1 个答案:

答案 0 :(得分:0)

在Reddit上重复我的回答:

转换矩阵和维管组不进行交互。您可以更改任何一个,而无需对另一个做任何事情。 基本经验法则:如果您的模型只是在不变形的情况下四处移动,则修改变换(即模型视图矩阵)。如果你的模型以一种可以用少量制服(骨骼动画)描述的方式变形,你可以改变它们。只有当模型的拓扑发生变化或变形超出骨架的范围时,才能改变VBO中的数据。