# Computer graphics/2013-2014/Laboratory 3

Quick links: front; laboratories agenda, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, JOGL template.

## (J)OGL BASICS (PART 2)

NOTE: Grab the application template from the following link: Computer graphics -- 2013-2014 -- info.uvt.ro/JOGL-Template

### Antialising

Aliasing effects are induced due to discretization of images or to effects that cause different continuous signals to become indistinguishable.
Several antialiasing techniques ca be used in (J)OGL:
* Color blending -- means that two colors can be mixed together using various criteria. The next example shows how we can use it to obtain an antialiasing effect:
We could play with the previous code by disabling GL_BLEND and/or GL_LINE_SMOOTH and notice the difference.
* Handling the accumulation buffer -- is the most simple OGL buffer and is handled by only one function (method) glAccum. It is an extended-range color buffer but instead of rendering images in it they are added to the contents of the accumulation buffer after rendering. It is used for creating antialiasing effects, motion blur, depth of field etc. This approach will be detailed in a later laboratory when will deal with buffers in more detail.
* multisampling -- more efficient than the first technique (done by the fragment shader) but cannot be restricted to just anti-alias a subset of polygons in the scene (optimization!?)
Links:
* Aliasing

### Polygons

Are made of a single line loop between several points.

#### Validity

Only convex polygons are valid in (J)OGL. If we want to draw concave polygons we should draw them by using lines with GL_LINE_LOOP. Alternatively we could divide our concave polygon into smaller convex ones by triangulation or tessallation (See bellow).
Link:
* Convex and Concave Polygons
* Valid Polygons

#### The building blocks of polyons

Polygons are made of smaller polygons. These building blocks can be either other triangles (triangulation) or other smaller polygons (tessellation).
The simplest polygon is a triangle.

#### Dimensionality

Polygons may lie in the same plane or not depending on whether all their vertices lie in the same plane or not.

#### Primitives

In (J)OGL we can create polygons by interpreting vertices in a broad way:
* polygons -- by using GL.GL_POLYGON
* quads -- by using GL.GL_QUADS
** quad strip -- by using GL.GL_QUAD_STRIP
* triangles -- by using GL.GL_TRIANGLES
** triangle strip -- by using GL.GL_TRIANGLE_STRIP
** triangle fan -- by using GL.GL_TRIANGLE_FAN
A particular polygon which has its own function (method) is the rectangle (by using glRect).
Links:
* Polygon Primitives

### Polygon filling and orientation

(J)OGL polygons can also be filled with a given color or just draw their contours or display the points (vertices) they are made of. The function (method) which enables you this operation is glPolygonMode(face, mode), where face could take the GL_FRONT, GL_BACK, GL_FRONT_AND_BACK values and mode can take the GL_POINT, GL_LINE or GL_FILL values. Polygons could be rendered differently depending on which side is facing the viewer.
An important feature when drawing polygons is the decision to draw only front or/and back faced ones. This action is specified by the glCullFace(face) where face is specified by GL_FRONT, GL_BACK, GL_FRONT_AND_BACK. This feature is called culling and is used for optimization reasons mostly.
Polygons having their vertices specified in clockwise order are back-faced, while polygons having vertices defined in trigonometric order are front-faced. You could change this default behavior by using the glFrontFace function (method). It receives two parameters: GL_CW (ClockWise) and GL_CCW (CounterClockWise - Trigonometric).
For example the following code will display the polygon:
while the next one will not:

### Polygon stipple

As for lines polygons can be filled with a stipple pattern by using the glPolygonStipple function (method):

### Polygon normals

Normals are perpendicular lines on surfaces. They are important when dealing with lights as their effects are computed with regard to the angle between the light direction and the each vertex normal. More on them will be discussed when we will address lighting issues.
Normals can be specified by using the glNormal* family functions (methods). The values of the arguments should be in the [-1,1] range.
Normals are mostly used for determining the light influence over a vertex.

#### Bump mapping primer

Bump mapping take full advantage of this technique. It is used to create the illusion of 3D surfaces on flat textures.
Two kind of maps are usually used for bump mapping: bump and normal maps. Both texture-types are used to generate fairly realistic shadows on the surface. To achieve this, bump maps store the height of each point on the surface, while normal maps store the surface normals. You typically get smoother results with a normal map than with a bump map.
The idea behind bump mapping is to perturb the normals so that vertices don't lit uniformly. The perturbation depends on either the value of the bump pixel (bump map) or the value of the normal (normal map).
Links:
* Tutorial on vectors, normals and lighting
* About OGL normals
* Bump mapping

### Building Display Lists

Display lists can be seen (at first glance) as a C function which once defined can be used as many times we want in our code. Simply put they store batches of OGL commands which can then be used repeatedly in our code. They are also used for their performance.
OGL commands within them are precompiled and, if possible, stored in the graphics card memory. Therefore, in general the execution of a Display List is faster than the execution of the commands contained in it.
Display Lists IDs are generated by using the glGenLists function (method). All the OGL commands that we want to store in a Display List must be placed inside a function (method) block starting with glNewList(int id, int mode) (where mode can be GL_COMPILE - creates the list, or GL_COMPILE_AND_EXECUTE - creates the list and executes the commands contained inside of it) and ending with glEndList.
Links:

## API

* java.media.opengl;
** GL;
** glPolygonMode(int, int);
** glRect(double, double, double, double);
** glPolygonStipple(ByteBuffer);
** glCullFace(int);
** glFrontFace(int);
** glNormal3f(float, float, float);
** glGenLists(int);
** glNewList(int, int);
** glEndList;

## Exercises

* Draw a house by using GL_TRIANGLES. The Sun should be a filled yellow circle and should move through the scene from left to right. Fill each section of the house with a different color. Use display lists
* Draw the Mandelbrot set (fractal). You can find the algorithm here http://en.wikipedia.org/wiki/Mandelbrot_set#For_programmers
* Draw a chess board made out of polygons with squares of white and black. Squares should alternate with one being black and one white. It is prohibited to draw a polygon using the black colour. Instead you should draw them as back-faced and enable culling for them.
* Gabriel Iuhasz, iuhasz.gabriel@info.uvt.ro