0

I don't quite understand how you do movement in OpenGL without using glPopMatrix and glPushMatrix. I can only get it to work by calling glDrawArray on several VAOs.

In this example below, I can move one triangle by using the shaders to do matrix multiplication. What I don't understand is say I have several points array I.e. points2[], points3[]. How would I go about being able to do translation without having to draw several VAOs? So how do I put everything into one VAO and still be able to say just multiple matrix3[] with points3[]?

float points[] = {
        0.0f,  0.0f,  0.0f,
        0.0f, 0.5f,  0.0f,
        0.5f, 0.5f,  0.0f,
        0.0f,  0.0f,  0.0f,
        0.5f, 0.0f,  0.0f,
            0.5f, 0.5f,  0.0f
        };
float matrix[] = {
      1.0f, 0.0f, 0.0f, 0.0f, // first column
      0.0f, 1.0f, 0.0f, 0.0f, // second column
      0.0f, 0.0f, 1.0f, 0.0f, // third column
      0.5f, 0.0f, 0.0f, 1.0f // fourth column
    };

    unsigned int points_vbo = 0;
    glGenBuffers (1, &points_vbo);
    glBindBuffer (GL_ARRAY_BUFFER, points_vbo);
    glBufferData (GL_ARRAY_BUFFER, 18 * sizeof (float), &points, GL_STATIC_DRAW);
unsigned int vao = 0;
glGenVertexArrays (1, &vao);
glBindVertexArray (vao);
glBindBuffer (GL_ARRAY_BUFFER, points_vbo);
glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, (GLubyte*)NULL);
glBindBuffer (GL_ARRAY_BUFFER, colours_vbo);
glVertexAttribPointer (1, 3, GL_FLOAT, GL_FALSE, 0, (GLubyte*)NULL);

glEnableVertexAttribArray (0);
glEnableVertexAttribArray (1);

//Setup shaders
std::string vertex_shader = loadshaders("test_vs.txt");
std::string fragment_shader = loadshaders("test_fs.txt");

unsigned int vs = glCreateShader (GL_VERTEX_SHADER);
const char* str = vertex_shader.c_str ();
glShaderSource (vs, 1, &str, NULL);
glCompileShader (vs);
unsigned int fs = glCreateShader (GL_FRAGMENT_SHADER);
const char* strb = fragment_shader.c_str ();
glShaderSource (fs, 1, &strb, NULL);
glCompileShader (fs);

unsigned int shader_programme = glCreateProgram ();
glAttachShader (shader_programme, fs);
glAttachShader (shader_programme, vs);
glLinkProgram (shader_programme);

int matrix_location = glGetUniformLocation (shader_programme, "matrix");
glUseProgram (shader_programme);
glUniformMatrix4fv (matrix_location, 1, GL_FALSE, matrix);
4

1 回答 1

1

从 GL 固定管道迁移到现代“核心”openGL 时,这是一个常见问题。

通常,每次要“移动”几何时,您都必须自行更新矩阵。

glUseProgram(shader_id);
bind_textures();
bind_vaos_and_buffers();

matrix = math_library::createTranslationMatrix(x, y, z);
glUniformMatrix4fv (matrix_location, 1, GL_FALSE, matrix);
glDraw*(...);

matrix = math_library::createTranslationMatrix(x1, y1, z1);
glUniformMatrix4fv (matrix_location, 1, GL_FALSE, matrix);
glDraw*(...); 

一般来说:每次你想画一些东西时:创建一个适当的矩阵,将它发送到活动着色器,然后调用一个“绘制”命令。请注意,这种方式使用一种几何数据,我们只需使用几种不同的变换多次绘制它。

math_library-例如可以是glm 库

你可以在这里阅读关于定位和运动的整章

于 2013-07-24T16:02:15.543 回答