OpenGL Programming/Quadrics

From Wikibooks, open books for an open world
Jump to navigation Jump to search


Quadrics are a way to create simple shapes, such as spheres or cylinders, from their maths equations.

Quadrics are usually drawn using the GLU (OpenGL Utility) library. In this tutorial though, we target portability, in particular mobile devices, where GLU may just not be available, or only for OpenGL 1.x, so we'll implement it ourselves. In addition we've get a better understanding than merely call of few functions :)

We can draw quadrics differently:

  • solid / wireframe
  • front-facing on the outside / on the inside

Solving the equation[edit]

So you might ask "How on earth am I going to solve a three variables equations?!". Well, the answer is to define x and y, using a grid, and compute z.

There are two main ways to draw a grid for x and y values:

  • square grid: make N points along the x axis, and for each of them, make M points along the y axis. This is a classic double-for loop as used with 2-dimensional C arrays.
  • disk grid: define a circle radius N times, and compute M points on the circle (using cos/sin).

Both methods will produce different results visually.

When solving the equation, we'll meet two forms:

  •  : z is computed directly
  •  : there are 3 cases:
    • EQUATION is 0, and there's one solution (0)
    • EQUATION is negative, and there's no solution
    • otherwise, z has two solution, and


Equation :

So :

So we have two z solutions for each (x,y) point (except when z = 0).


< OpenGL Programming

Browse & download complete code Download.svg