0

I'm having a problem with moving my camera with glOrtho. I have a small quad in the center and i want to try to move the camera using glOrtho, but it just doesn't seem to be working. The quad doesn't move at all, so the camera isn't moving too i guess. maybe i miss understand how glOrtho works? here is my code so far.

void Camera::updateCamera(float x, float y, float zoom)
{
    camX = x;
    camY = y;
    this->zoom = zoom;
    viewWidth = 320;
    viewHeight = 240;
    //viewWidth = tan(60) * this->zoom;
    //viewHeight = tan(45) * this->zoom;    
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(camX - viewWidth,
        camX + viewWidth,
        camY - viewHeight,
        camY + viewHeight,
        -1,
        1);
    glMatrixMode(GL_MODELVIEW);
}

and here is where i apply it. i tried to move it along the x axis for 25 points.

void Engine::renderAll()
{
    x += 25;
    glClear(GL_COLOR_BUFFER_BIT);
    shader->use();
    camera.updateCamera(x, y, 1.0);
    //shader->setUniform4fv("view", camera.getView());
    batchManager->renderBatches();
    SDL_GL_SwapWindow(window);
}
GroniumArgor
  • 103
  • 1
  • 11

1 Answers1

1

Yes, I guess you misundertood how glOrtho works. The role of glOrtho is transform a 3D view in a 2D view, using an orthographic projection.

If you want to work with positioning camera in a 3D space, the correct function is normally called LookAt. Once, it seems me that you are using old OpenGL, you can try the glu function gluLookAt

PS. In moderm openGL, this functions is now deprecated. I suggest you to try to learn the modern way.

Amadeus
  • 10,199
  • 3
  • 25
  • 31
  • oh i forgot to mention, i'm working in 2D, that's why i tried to use the glOrtho. – GroniumArgor Jul 21 '16 at 17:32
  • Not a problema. OpenGL is a 3D library/framework. Just keep one dimension constant – Amadeus Jul 21 '16 at 17:33
  • so, i can't use glOrtho for my camera? – GroniumArgor Jul 21 '16 at 17:36
  • If you want to do an orthographic projection, you not can, you must use it. But remember, camera has 2 roles: projection and positioning. Projection is done with glOrtho, for example, and position is done via vertex transformation, or in old OpenGL, gluLookAt – Amadeus Jul 21 '16 at 17:41
  • You can look at http://stackoverflow.com/questions/2571402/explain-the-usage-of-glortho?rq=1 for a better undestand about projection – Amadeus Jul 21 '16 at 17:43