Translating primitives in OpenGL

40 Views Asked by At

I want move several squares by a specified distance in different directions. To begin with I used glTranslatef(0.4f, 0.4f, 0.0f); where 0.4f is the square's side length, but it seems to have stopped working when I switched machines. I have also tried glVertex3f(-0.4f, 0.4f, 0.0f); but without success. I initialized vertices:

float vertices[] = {
    -0.2f, -0.2f, 0.0f,
    -0.2f, 0.2f, 0.0f,
    0.2f, 0.2f, 0.0f,
    0.2f, -0.2f, 0.0f,
};

I initialized buffers in main():

unsigned int VBO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glEnableVertexAttribArray(0);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(0);
// Colors
unsigned int VBO2;
glGenBuffers(1, &VBO2);
glBindBuffer(GL_ARRAY_BUFFER, VBO2);
glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, VBO2);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

Then I went on to draw the shapes:

glDrawArrays(GL_POLYGON, 0, 4);
glTranslatef(0.4f, 0.4f, 0.0f);
glDrawArrays(GL_POLYGON, 1, 4);
glTranslatef(-0.4f, -0.4f, 0.0f);

So how to move these primitives along X and Y axis respectively?

0

There are 0 best solutions below