CS594 - Project 1
Byungil Jeong
- Programming Environment
- OS - linux
- This project is based on the brick shader demo of 3Dlabs
Inc.
- Using OPENGL-2.0, GLSL, GLEW, GLUT, ImageMagick
- 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);
- Side correction - Using both x and z coordinates to
calculate horizontal texture coordinates
<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);
- Corner correction - alternate horizontal offset for each side
<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);
- For top and bottom rows, additional uniform variable rowNum
was defined (rowNum is calculated in the application based on
cube size and brick size)
<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);
- Antialiasing using smoothstep() - only two sides(top
and right) are antialiased
<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);
- Four-side antialiasing result
<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
- The toon shader in the class note is used
- two snapshot with different light positions (arrow keys and
page-up/down keys are used for moving light)
- Add environment and reflection using environment map
- Draw background using an OpenGL texture
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.
- Add reflection to the toon shader - insert the environment map
shader codes into the toon shader codes
- Change the light position
Links to Source codes
proj1.tar.gz
Please refer to README.txt in proj1/brick about user interaction
last revision 2/9/06