Monday, February 1, 2010

OpenGL Display List

OpenGL Display List


Display list is a group of OpenGL commands that have been stored (compiled) for later execution. Once a display list is created, all vertex and pixel data are evaluated and copied into the display list memory on the server machine. It is only one time process. After the display list has been prepared (compiled), you can reuse it repeatedly without re-evaluating and re-transmitting data over and over again to draw each frame. Display list is one of the fastest methods to draw static data because vertex data and OpenGL commands are cached in the display list and minimize data transmissions from the client to the server side. It means that it reduces CPU cycles to perform the actual data transfer.
Another important capability of display list is that display list can be shared with many clients, since it is server state.
For optimal performance, put matrix transforms, lighting and material calculation inside display list if possible, so OpenGL processes these expensive computations only once while display list is created, and stores the last results into the display list.
However, display list has a disadvantage. Once a display list is compiled, it cannot be modified later. If you need frequent changes or dynamic dataset, use Vertex Array or Vertex Buffer Object instead. Vertex Buffer Object can handle both static and dynamic dataset.
Note that not all OpenGL commands can be stored into the display lists. Since display list is a part of server state, any client state related commands cannot be placed in display list, for example, glFlush(), glFinish(), glRenderMode(), glEnableClientState(), glVertexPointer(), etc. And any command that returns a value cannot be stored in display list either, because the value should be returned to client side, not in display list, for example, glIsEnabled(), glGet*(), glReadPixels(), glFeedbackBuffer(), etc. If these commands are called in a display list, they are executed immediately.

Implementation

Using display list is quite simple. First thing to do is creating one or more of new display list objects with glGenLists(). The parameter is the number of lists to create, then it returns the index of the beginning of contiguous display list blocks. For instance, glGenLists() returns 1 and the number of display lists you requested was 3, then index 1, 2 and 3 are available to you. If OpenGL failed to create new display lists, it returns 0.
The display lists can be deleted by glDeleteLists() if they are not used anymore.
Second, you need to store OpenGL commands into the display list in between glNewList() and glEndList() block, prior to rendering. This process is called compilation. glNewList() requires 2 arguments; first one is the index of the display list that was returned by glGenLists(). The second argument is mode which specifies compile only or compile and also execute(render); GL_COMPILE, GL_COMPILE_AND_EXECUTE.
Up to now, preparation of display list has been done. Only thing you need to do is executing the display list with glCallList() or multiple display lists with glCallLists() every frame.
Take a look at the following code for a single display list first.
// create one display list
GLuint index = glGenLists(1);

// compile the display list, store a triangle in it
glNewList(index, GL_COMPILE);
    glBegin(GL_TRIANGLES);
    glVertex3fv(v0);
    glVertex3fv(v1);
    glVertex3fv(v2);
    glEnd();
glEndList();
...

// draw the display list
glCallList(index);
...

// delete it if it is not used any more
glDeleteLists(index, 1);
Note that only OpenGL commands between glNewList() and glEndList() will be recorded into a display list once and cached multiple times whenever it is called by glCallList().
In order to execute multiple display lists with glCallLists(), you need extra work to do. You need an array to store indices of display list that are only to be rendered. In other words, you can select and render only limited number of display lists from whole lists. glCallLists() requires 3 parameters; the number of list to draw, data type of index array, and pointer to the array.
Note that you don't have to specify the exact index of the display list into the array. You may specify the offset instead, then set the base offset with glListBase(). When glCallLists() is called, the actual index will be computed by adding the base and offset. The following code shows using glCallLists() and the detail explanation follows.
GLuint index = glGenLists(10);  // create 10 display lists
GLubyte lists[10];              // allow maximum 10 lists to be rendered

glNewList(index, GL_COMPILE);   // compile the first one
...
glEndList();

...// compile more display lists

glNewList(index+9, GL_COMPILE); // compile the last (10th)
...
glEndList();
...

// draw odd placed display lists only (1st, 3rd, 5th, 7th, 9th)
lists[0]=0; lists[1]=2; lists[2]=4; lists[3]=6; lists[4]=8;
glListBase(index);              // set base offset
glCallLists(5, GL_UNSIGNED_BYTE, lists);
For convenience, OpenGL provides glListBase() to specify the offset that is added to the display list indices when glCallLists() is executed.
In the above example, only 5 display lists are rendered; 1st, 3rd, 5th, 7th and 9th of display lists. Therefore the offset from index value is index+0, index+2, index+4, index+6, and index+8. These offset values are stored in the index array to be rendered. If the index value is 3 returned by glGenLists(), then the actual display lists to be rendered by glCallLists() are, 3+0, 3+2, 3+4, 3+6 and 3+8.
glCallLists() is very useful to display texts with the offsets corresponding ASCII value in the font.

MacLaurin series of Exponential function

MacLaurin series of Exponential function, e^x

The MacLaulin series (Taylor series at x=0) representation of a function f(x) is
Definition of MacLaurin series
The derivatives of the exponential function e^x and their values at x=0 are:
derivatives of e^x
Note that the derivative of e^x is also e^x and e^0 = 1. We substitute this value of f^{(n)}(0) in the above MacLaurin series:
MacLaurin series of e^x
We can also get the MacLaurin series of e^{ix} by replacing x to ix:
MacLaurin series of e^{ix}

Euler's Equation

Euler's Equation

Euler's equation

Euler's equation is one of most remarkable and mysterious discoveries in Mathematics. Euler's equation (formula) shows a deep relationship between the trigonometric function and complex exponential function.

Discovery of Euler's Equation

First, take a look the Taylor series representation of exponential function, e^x and trigonometric functions, sine, sin(x) and cosine, cos(x).
Taylor series of e^x, sin(x), cos(x) Les't compare cos(x) + sin(x) with e^x. Notice cos(x) + sin(x) is almost identical to Taylor series of e^x ; all terms in the series are exactly same except signs. As you know, the exponential function, e^x increases exponentially as input x grows. But, what does this exponential function have to do with periodic (oscillating) functions, cos(x) and sin(x)?
Mathematicians had tried to figure out this weird relationship between the exponential function and the sum of 2 oscillating functions. Finally, Leonhard Euler completed this relation by bringing the imaginary number, i into the above Taylor series; e^{ix} instead of e^x and cos(x) + isin(x) instead of cos(x) + sin(x).
Taylor series of e^{ix}, cos(x)+isin(x) Now, we find out e^{ix} equals to cos(x) + isin(x), which is known as Euler's Equation.

Graph of Euler's Equation

We know that the exponential function, e^x is increasing exponentially as x grows. But, what does the function e^{ix} look like?
The following images show the graph of the complex exponential function, complex exponential function, e^{ix}, by plotting the Taylor series of e^{ix} in the 3D complex space (x - real - imaginary axis). Surprisingly, it is a spiral spring (coil) shape, rotating around a unit circle. And, when it is projected to the real number (top view) and imaginary number axis (side view), it becomes a trigonometric function, respectively cosine and sine.
Graph of complex exponential function, e^{ix}
Graph of complex exponential function, e^{ix}
Projection of e^{ix} in real number axis
Real number part of Real part of complex exponential function, e^{ix}
Projection of e^{ix} in imaginary number axis
Imaginary number part of Imaginary part of complex exponential function, e^{ix}
This graphical representation of the complex exponential function, complex exponential function, e^{ix} clearly shows the relation to the trigonometric function; the real number part of complex exponential function, e^{ix} is cosine and the imaginary part of complex exponential function, e^{ix} is sine function with the period of 2pi in radians.
Graph of e^{ix}
Example of Complex Exponential Function, e^{ix}
This is an interactive OpenGL application to draw the complex exponential function, complex exponential function, e^{ix} in 3D.


Use the mouse cursor to change the view and use 'd' key to change drawing modes.

Meaning of Euler's Equation

Projection onto complex plane of complex exponential function, e^{ix}
Graph of complex exponential function, e^{ix} on the complex plane
When the graph of complex exponential function, e^{ix} is projected to the complex plane, the function complex exponential function, e^{ix} is tracing on the unit circle. It is a periodic function with the period 2pi.
It means that raising mathematical constant, e to an imaginary power ix produces the complex number with the angle x in radians. This polar form of complex exponential function, e^{ix} is very convenient to represent rotating objects or periodic signals because it can represent a point in the complex plane with only single term instead of two terms, a + ib. Plus, it simplifies the mathematics when used in multiplication, for example, .
The complex exponential forms are frequently used in electrical engineering and physics. For example, A periodic signal can be represented the sum of sine and cosine functions in Fourier analysis, and the movement of a mass attached to a string is also sinusodial. These sinusodial functions can be replaced with the complex exponential forms for simpler computation.
Polar form of a complex number
Polar form of a complex number
We can extend this polar form representation for any complex number.
Polar form of a complex number The magnitude (norm), |z| of the complex number is a scalar value and can be rewrtten by using the laws of exponent and logarithm:
Polar form of a complex number

2D Rotation with Euler's Equation

The multiplication of two complex numbers implies a rotation in 2D space.

Multiplication implies rotation
Take a look the following figure showing 2 complex numbers on the unit circle of the complex plane.
When we compare these two complex numbers, we notice that the sum of angles in the imaginary exponential form equals to the product of two complex numbers, . It tells us multiplying by performs rotating with angle Φ.
We can extend it to any arbitary complex number. In order to rotate a complex number z = x + iy with a certain angle Φ, we simply multiply to the number. Then, the rotated result z' becomes .

2D rotation of a complex number

If we re-write it as a matrix form, it becomes a 2x2 rotation matrix that we are familiar with.

Euler Identity

If we substitute the value into Euler's equation, then we get:
Euler Identity This equation is called Euler Identity showing the link between 5 fundamental mathematical constants; 0, 1, pi, e, and i.
Logarithmic function is only defined for the domain x > 0. But, Euler Identity allows to define the logarithm of negative x by converting exponent to logarithm form:
If we substitute to Euler's equation, then we get:
Then raise both sides to the power :
The above equation tells us that is actually a real number (not an imaginary number).

Proof of Euler's Equation

This is a proof using calculus. Let's start with the right-hand side only and its differentiate is:

Modify the above equation:

Move t to left-hand side, then apply integral:

To find constant C, substitute x = 0:

Now, we know C = 0, so the above equation is:

Finally, convert this logarithm form to the exponential form: