Difference between glOrthof and glViewport

Difference between glOrthof and glViewport

In OpenGL, glOrthof and glViewport are two distinct functions that play crucial roles in defining the viewing volume and the rendering area on the screen.

glOrthof

Description

glOrthof is a function used to define a 2D orthogonal viewing volume. It specifies a rectangular region in the world coordinate system that will be mapped to the screen. The viewing volume is defined by six parameters:

  • left: The x-coordinate of the left clipping plane.
  • right: The x-coordinate of the right clipping plane.
  • bottom: The y-coordinate of the bottom clipping plane.
  • top: The y-coordinate of the top clipping plane.
  • near: The z-coordinate of the near clipping plane.
  • far: The z-coordinate of the far clipping plane.

Example

glOrthof(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);

This code defines a viewing volume that encompasses a square region from -1.0 to 1.0 in both x and y directions. The near and far clipping planes are at -1.0 and 1.0, respectively.

glViewport

Description

glViewport is a function used to define the rendering area, or viewport, on the screen. It specifies a rectangular region within the window where the scene will be rendered.

  • x: The x-coordinate of the lower left corner of the viewport.
  • y: The y-coordinate of the lower left corner of the viewport.
  • width: The width of the viewport in pixels.
  • height: The height of the viewport in pixels.

Example

glViewport(0, 0, 800, 600);

This code defines a viewport that spans the entire window, from (0, 0) to (800, 600) pixels.

Comparison

Feature glOrthof glViewport
Purpose Defines a 2D orthogonal viewing volume Defines the rendering area on the screen
Input Parameters Left, right, bottom, top, near, far x, y, width, height
Units World coordinates Pixels
Effect Determines what portion of the world coordinate system is visible Specifies the region of the window where the scene is rendered

Relationship

glOrthof and glViewport work together to define the final rendered image. glOrthof establishes the viewing volume, while glViewport defines the region of the screen that the viewing volume will be mapped to.

Key Points

  • glOrthof defines the viewing volume in world coordinates.
  • glViewport defines the rendering area in pixels.
  • Both functions are essential for setting up the scene for rendering.
  • The order in which these functions are called is important. glOrthof should generally be called before glViewport.


Leave a Reply

Your email address will not be published. Required fields are marked *