Skip to content

Instantly share code, notes, and snippets.

@AndreaCrotti
Created January 24, 2010 15:12
Show Gist options
  • Select an option

  • Save AndreaCrotti/285248 to your computer and use it in GitHub Desktop.

Select an option

Save AndreaCrotti/285248 to your computer and use it in GitHub Desktop.

Compiling and running

Under linux/osx simply run the script test.sh in the main directory, it will compile everything and run one server and one client. For example this is the output of a possible execution:

Example

73-084:final project andrea$ ./test.sh
making server and client
executing the server and putting it in background
executing one client, press 'q' to exit
waiting for client to connect
OpenGL Version: 2.1
GL_FRAMEBUFFER_COMPLETE_EXT
Client 0 has connected
Connected to Server as Client 0
73-084:final project andrea$ Received quit action from client
all clients disconnected, closing the server

How to use

Once the client is started you can use the following keystrokes or mouse gestures:

  • W, S
    move forward and backwards
  • A and D
    strafe left and right
  • MouseWheel
    Zoom In/Out
  • Right MouseButton + Moving
    change the camera
  • G
    change Geometry
  • H
    change texture
  • J
    (de)activate Light 1
  • K
    (de)activate Light 2
  • L
    (de)activate Light 3
  • Q
    quit

3d Techniques used

Bump mapping

The vertex color is: $Vertex Color = emission + globalAmbient + sum(attenuation * spotlight * [lightAmbient + (max {L.N, 0} * diffuse) + (max {H.N, 0} ^ shininess)*specular])$

Normal Mapping

Normal Mapping - Is one possible implementation of a technique known as bump mapping.
While bump mapping perturbs the existing normal (the direction the surface is facing) of a model, normal mapping replaces the normal in its entirety. Normal maps are images that store a direction, the direction of normals directly in the RGB values of an image. They can simulate that pixel being moved at any direction, in an arbitrary way. \

Normal maps are normal maps usually have to be generated in some way, often from higher resolution geometry than the geometry you’re applying the map to.

Applying the normal mapping will replace completely the normals of our mesh.

In our implementation we work on large triangles, so we need to perform per-pixel computations, so normal mapping and Phong shading.
Using for example Goraud shading and bump mapping otherwise we would get a non realistic spotlight effect around the center of the triangle.

The cube’s shading is smooth because the lighting is calculated at every pixel instead of every vertex. Per-pixel lighting uses an RBG texture to encode the data needed to create surface normals in a regular texture map.

This texture containing surface normal data is called a normal map.
The red, green, and blue channels of the normal map represent the X, Y, and Z values of the normal vector.

Tangent space

For the normal mapping we need to compute the tangent space. Tangent space is generally used on triangular surfaces and is composed by 3 vectors (T,B,N):

  • tangent
  • binormal
  • normal

The equation representing the situation is: $pi = ui.T + vi.B$

Implementation

The java code to load the normal map is equal to the one for the other shaders. We don’t compute the normal map but we load it from the file normalMap.png

In plus for the normal mapping we need to compute also the tangent and binormal, which are computed as:

The full computation of needed parameters is in Main.java

tangent = new Vector3f((c3c1_B * v2v1.x - c2c1_B * v3v1.x) * fScale1,
                       (c3c1_B * v2v1.y - c2c1_B * v3v1.y) * fScale1,
                       (c3c1_B * v2v1.z - c2c1_B * v3v1.z) * fScale1);

binormal = new Vector3f((-c3c1_T * v2v1.x + c2c1_T * v3v1.x) * fScale1,
                        (-c3c1_T * v2v1.y + c2c1_T * v3v1.y) * fScale1,
                        (-c3c1_T * v2v1.z + c2c1_T * v3v1.z) * fScale1);

In our case we don’t need to compute also the inverse of the matrix in the java code (which is always an expansive operation) because it’s done in the normal vertex file.

The rest of the normal mapping implementation is in normal.vert and normal.frag.

Parallax Mapping

We downloaded the parallax map from this web site, and applied to our image.

Links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment