PDA

View Full Version : Using glTranslatef for zoom moves my object left and right instead of zooming



LarrySellers
09-27-2014, 09:53 PM
Hey guys,

I am trying to use the up and down keys to zoom in and out on an object I've drawn.

From what I understand I can use glTranslatef(0.0f, 0.0f, zoomValue) to zoom in/out on the object. But when I use this in my code it just moves the object left and right instead of zooming in and out. Can anyone point out what the problem is? Here is the code for my display function:



void Bunny::displayBunny()
{
GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
do{
float translateValue = 0.0f;
float yRotate = 0.0f;
if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS)
yRotate = 1.0f;
if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS)
yRotate = -1.0f;
if (glfwGetKey(window, GLFW_KEY_UP))
translateValue += .001;
if (glfwGetKey(window, GLFW_KEY_DOWN))
translateValue -= .001;

// Clear the screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// 1rst attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
0, // attribute
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);

// Draw the triangle !
glDrawArrays(GL_TRIANGLES, 0, vertices.size());

glDisableVertexAttribArray(0);

glTranslatef(0.0f, 0.0f, translateValue);
glRotatef(0.1f, 0.0f, yRotate, 0.0f);

// Swap buffers
glfwSwapBuffers(window);
glfwPollEvents();

} // Check if the ESC key was pressed or the window was closed
while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0);
}


Thanks!

Cornix
09-28-2014, 11:26 AM
Maybe you should look up what the word "translation" means. Its movement. With glTranslate you can move your coordinate system on the x, y and z-axis. You are currently moving everything on the z-axis.
Perhaps have a look at glScale which allows you to scale your axes.

LarrySellers
09-28-2014, 11:44 AM
Maybe you should look up what the word "translation" means.

That was quite helpful despite the unnecessary condescension. Thanks! <3

Cornix
09-28-2014, 01:25 PM
Not everybody speaks english as their native language, its a big world, it might very well be that you do not know what translation means.