OpenGL Programming/Quadrics

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

Concept[edit]

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 = EQUATION : z is computed directly
  • z^2 = EQUATION : 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, \scriptstyle\sqrt{EQUATION} and \scriptstyle-\sqrt{EQUATION}

Sphere[edit]

Equation : {x^2 \over a^2} + {y^2 \over a^2} + {z^2 \over a^2} = 1

So : z^2 = a^2 - {x^2} - {y^2}

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

Links[edit]

< OpenGL Programming

Browse & download complete code