CS594 - Project 1 

Byungil Jeong

- Programming Environment

- Improving Brick Shader

        <vertex shader>          
          MCposition  = gl_Vertex.xy;
       
       <fragment shader>
         position = MCposition / BrickSize;
         if (fract(position.y * 0.5) > 0.5)
             position.x += 0.5;
         position = fract(position);
         useBrick = step(position, BrickPct);
         color  = mix(MortarColor, BrickColor, useBrick.x * useBrick.y);

            Initial state of bricks
        <vertex shader>          
        // calculate tangential vector of vertex normal in XZ plane
         vec2 tanVec = normalize(vec2(gl_Normal.z, -gl_Normal.x));
        // horizontal texture coordinate is the projection of (x, z) to the tangential surface
         MCposition = vec2(dot(gl_Vertex.xz, tanVec), gl_Vertex.y);                

brick2

 <vertex shader>          
varying int   NormDir;
  :
// need absolute value of x and z component to classify surfaces
vec2 absNormal  = abs(gl_Normal.xz);
// classifying surfaces
if (absNormal[0] < absNormal[1])
    NormDir = 1;
else
    NormDir = 0;

 <fragment shader>
position = MCposition / BrickSize;
if (fract(position.y * 0.5) > 0.5 && NormDir == 0)
        position.x += 0.5;
if (fract(position.y * 0.5) <= 0.5 && NormDir == 1)
        position.x += 0.5;
position = fract(position);
useBrick = step(position, BrickPct);
color  = mix(MortarColor, BrickColor, useBrick.x * useBrick.y);

 <fragment shader>
uniform int   rowNum;
   :
vec2  zero2 = vec2(0.0, 0.0);
position = MCposition / BrickSize;
if (fract(position.y * 0.5) > 0.5 && NormDir == 0)
        position.x += 0.5;
if (fract(position.y * 0.5) <= 0.5 && NormDir == 1)
        position.x += 0.5;

if (position.y > rowNum || position.y < -rowNum) {
    useBrick = zero2; // usr mortar color for top and bottom rows
}     
else {
    position = fract(position);
    useBrick = step(position, BrickPct);
}
color  = mix(MortarColor, BrickColor, useBrick.x * useBrick.y);

brick2


         <fragment shader>
          vec2  delta = vec2(0.05, 0.05);
             :
          position = fract(position);
          // fail to antialias when position becomes zero
          useBrick = smoothstep(position-delta, position, BrickPct);
         

   

 

        <fragment shader>
          position = fract(position);
          useBrick = smoothstep(position-delta, position, BrickPct)
                        - smoothstep(position-delta, position, zero2);  // antialias left and bottom edges

   


 
- Add toon shaded teapot

 
- Add environment and reflection using environment map

 

        I used an environment map shader for the reflective surface. I modified the environment map shader in the class note.

<vertex shader>
    gl_Position    = ftransform();
    Normal         = normalize(gl_NormalMatrix * gl_Normal);
    vec4 pos       = gl_ModelViewMatrix * gl_Vertex;
    EyeDir         = pos.xyz;
   
    // to use the same light source as used for other shaders
    vec3 LightPos  = vec3(gl_LightSource[0].position); //get light position in eye coordinates
    LightIntensity = max(dot(normalize(LightPos - EyeDir), Normal), 0.0);

          <fragment shader>
    // compute reflection vector
    vec3 reflectDir = normalize(reflect(EyeDir, Normal));
   
    // compute texture index
    vec2 index;
    index.y = 1 - (reflectDir.y + 1) * 0.5; // because the row order of Jpeg image and OpenGL texture are opposite
    index.x = (reflectDir.x + 1) * 0.5;

    // Do a lookup into the environment map
    vec3 envColor = vec3 (texture2D(envMap, index));

This shader uses normalized reflection vector as texture coordinates with little transformation.
This method is much simpler than the one in the class note, but it works fine.

 

- Snap shots to show the final results of this project.

 



Links to Source codes

proj1.tar.gz

Please refer to README.txt in proj1/brick about user interaction


last revision 2/9/06