GameProg_lect4 2dArt
GameProg_lect4 2dArt
GAME PROGRAMMING
write up a design documentation
for your upcoming game
• describe the unique character of the
game
• Genre: what games does your game
draw from and expand upon?
• What is the story of the game? Name
basic elements - a character, a place, a
mission. Describe the scenes. Does
sound play a role in your game? If so, in
what way (e.g. dramatizing effects)?
• Control scheme: how will the user
interact with the game? Mouse?
Keyboard? Other (e.g. line-in,
microphone)? What button will do what?
If there is any type of GUI or Heads-
upDisplay, please provide a sample
screenshot of the GUI (hint: you can use
a mockup of your screen,
Elements of Graphic Design
Q
• Line
• Shape
Q
• Space - + or -
Line Shape Space Texture Size
Y M
Green
Black
(0, 0,
0)
Red
Blue
rgb color cube
in another view
HSV color space
• HSV
– hue,
– Saturation,
– Value,
HSV UI
Pixel-based primitives
•Bitmaps
– 2D array of bit masks for pixels
•update pixel color based on current
color
•Images
– 2D array of pixel color information
•complete color information for each
pixel
Game Models
• Geometry
– Position / vertex normals / vertex colors / texture
coordinates
• Topology
– Primitive
18
•Lines / triangles / surfaces / …
• Property
– Materials
– Textures
• Motion / Animation
Geometry Data
Vertex position
– (x, y, z, w)
– In model space or screen spane
Vertex normal
– (nx, ny, nz)
19
Vertex color
– (r, g, b) or (diffuse, specular)
Texture coordinates on vertex
– (u1, v1), (u2, v2), …
Skin weights
– (bone1, w1, bone2, w2, …)
DRAWING LINE/PLYLINES
Bresenhem’s Algorithm Drawing line
• Starts at x0, y0
• Plots x0, y0
• xi = xi + 1
25
Draw_Line( )
// compute horizontal and vertical deltas
dx = x1-x0;
dy = y1-y0;
// test which direction the line is going in i.e. slope angle
if (dx>=0)
{
x_inc = 1;
} // end if line is moving right
else
{
x_inc = -1;
dx = -dx; // need absolute value
} // end else moving left
26
DrawLine( )
// now based on which delta is greater we can draw the line
if (dx > dy)
{
error = dy2 - dx; // initialize error term
for (index=0; index <= dx; index++) // draw the line
{
*vb_start = color; // set the pixel
// test if error has overflowed
if (error >= 0)
{
error-=dx2; // move to next line
vb_start+=y_inc;
} // end if error overflowed
error+=dy2; // adjust the error term
vb_start+=x_inc; // move to next pixel
} // end for
} // end if |slope| <= 1
27
DrawLine( )
else
{
error = dx2 - dy; // initialize error term
for (index=0; index <= dy; index++) // draw the line
{
*vb_start = color; // set the pixel
if (error >= 0) // test if error overflowed
{
error-=dy2;
vb_start+=x_inc; // move to next line
} // end if error overflowed
error+=dx2; // adjust the error term
vb_start+=y_inc; // move to next pixel
} // end for
} // end else |slope| > 1
return(1); // return success
} // end Draw_Line
28
DrawLine( )
// test y component of slope
if (dy>=0)
{
y_inc = lpitch;
} // end if line is moving down
else
{
y_inc = -lpitch;
dy = -dy; // need absolute value
} // end else moving up
// compute (dx,dy) * 2
dx2 = dx << 1;
dy2 = dy << 1;
29
Polygons
•Defined by vertices
•Closed (all line connected)
•Simply draw them one line at a time
•Can be convex or concave
•Attributes:
Number of vertices, color, position,
list of vertices (x, y) form
31
Draw_Polygon2D( )
41
Filling Polygons
P0 (x0 , y0)
P2 (x2 , y 2) P1 (x1 , y 1)
44
Triangle Filling
void Draw_Bottom_TriFP(int x1,int y1, int x2,int y2, int x3,int y3,
int color, UCHAR *dest_buffer, int mempitch)
// function draws triangle that has flat bottom using fixed point math
void Draw_Top_Tri(int x1,int y1, int x2, int y2, int x3,int y3,
int color, UCHAR *dest_buffer, int mempitch)
// this function draws a triangle that has a flat top
void Draw_Bottom_Tri(int x1,int y1, int x2, int y2, int x3,int y3,
int color, UCHAR *dest_buffer, int mempitch)
// this function draws a triangle that has a flat bottom
48
Drawing Filled Triangles
void Draw_TriangleFP_2D(int x1,int y1,int x2, int y2, int x3,int y3,
int color, UCHAR *dest_buffer, int mempitch)
// function draws triangle on the destination buffer using fixed point
// it decomposes all triangles into a pair of flat top, flat bottom
void Draw_Triangle_2D(int x1,int y1, int x2, int y2, int x3,int y3,
int color, UCHAR *dest_buffer, int mempitch)
// this function draws a triangle on the destination buffer
// it decomposes all triangles into a pair of flat top, flat bottom
49
Example Player Art
Clipping
P2
53
Cohen Sutherland
55
Clip_Line( )
int Clip_Line(int &x1,int &y1,int &x2, int &y2)
{
// function clips the line using globally defined clipping region
// internal clipping codes
#define CLIP_CODE_C 0x0000
#define CLIP_CODE_N 0x0008
#define CLIP_CODE_S 0x0004
#define CLIP_CODE_E 0x0002
#define CLIP_CODE_W 0x0001
#define CLIP_CODE_NE 0x000a
#define CLIP_CODE_SE 0x0006
#define CLIP_CODE_NW 0x0009
#define CLIP_CODE_SW 0x0005
56
Clip_Line( )
// determine codes for p1 and p2
if (y1 < min_clip_y)
p1_code|=CLIP_CODE_N;
else if (y1 > max_clip_y)
p1_code|=CLIP_CODE_S;
if (x1 < min_clip_x)
p1_code|=CLIP_CODE_W;
else if (x1 > max_clip_x)
p1_code|=CLIP_CODE_E;
if (y2 < min_clip_y)
p2_code|=CLIP_CODE_N;
else if (y2 > max_clip_y)
p2_code|=CLIP_CODE_S;
if (x2 < min_clip_x)
p2_code|=CLIP_CODE_W;
else if (x2 > max_clip_x)
p2_code|=CLIP_CODE_E;
57
Clip_Line( )
// try and trivially reject
if ((p1_code & p2_code))
return(0);
// test for totally visible, if so leave points untouched
if (p1_code==0 && p2_code==0)
return(1);
58
Clip_Line( )
// do bounds check
if ((xc1 < min_clip_x) || (xc1 > max_clip_x) ||
(yc1 < min_clip_y) || (yc1 > max_clip_y) ||
(xc2 < min_clip_x) || (xc2 > max_clip_x) ||
(yc2 < min_clip_y) || (yc2 > max_clip_y) )
{
return(0);
} // end if
return(1);
} // end Clip_Line
59
Example Enemy Art
Moving Objects
64
Scaling (Changing Size)
65
Scale_Polygon2D( )
int Scale_Polygon2D(POLYGON2D_PTR poly, float sx, float sy)
{
// this function scalesthe local coordinates of the polygon
// test for valid pointer
if (!poly)
return(0);
// loop and scale each point
for (int curr_vert = 0; curr_vert < poly->num_verts; curr_vert++)
{
// scale and store result back
poly->vlist[curr_vert].x *= sx;
poly->vlist[curr_vert].y *= sy;
66
Game_Main( )
// do the graphics
Draw_Polygon2D(&asteroid, (UCHAR *)ddsd.lpSurface,
ddsd.lPitch);
67
Rotation (Turning)
68
Rotate_Poloygon2D( )
int Rotate_Polygon2D(POLYGON2D_PTR poly, int theta)
{
// function rotates the local coordinates of the polygon – no matrices
// test for valid pointer
if (!poly)
return(0);
// loop and rotate each point, very crude, no lookup!!!
for (int curr_vert = 0; curr_vert < poly->num_verts; curr_vert++)
{
// perform rotation
float xr = (float)poly->vlist[curr_vert].x*cos_look[theta] -
(float)poly->vlist[curr_vert].y*sin_look[theta];
float yr = (float)poly->vlist[curr_vert].x*sin_look[theta] +
(float)poly->vlist[curr_vert].y*cos_look[theta];
// store result back
poly->vlist[curr_vert].x = xr;
poly->vlist[curr_vert].y = yr;
} // end for curr_vert
// return success
return(1);
} // end Rotate_Polygon2D
69
Matrix Operations
• Translation
| 1 0 0|
|x y 1| * | 0 1 0|
|dx dy 1|
• Scaling
|sx 0 0|
|x y 1| * | 0 sy 0|
| 0 0 1|
70
Matrix Operations
• (S * R) * T
| sx*cos(a) –sx*sin(a) 0|
|x y 1| * | sy*sin(a) sy*cos(a) 0|
| dx dy 1|
71
Mat_Init_3x2( )
inline int Mat_Init_3X2(MATRIX3X2_PTR ma,
float m00, float m01,
float m10, float m11,
float m20, float m21)
{
// fills a 3x2 matrix with the sent data in row major form
ma->M[0][0] = m00; ma->M[0][1] = m01;
ma->M[1][0] = m10; ma->M[1][1] = m11;
ma->M[2][0] = m20; ma->M[2][1] = m21;
// return success
return(1);
} // end Mat_Init_3X2
72
Mat_Mul1x2_3x2( )
int Mat_Mul1X2_3X2(MATRIX1X2_PTR ma, MATRIX3X2_PTR mb, MATRIX1X2_PTR mprod)
{
// function multiplies a 1x2 matrix against a 3x2 matrix - ma*mb and
// stores result using a dummy element for the 3rd element of the 1x2
for (int col=0; col<2; col++)
{
// compute dot product from row of ma and column of mb
float sum = 0; // used to hold result
for (int index=0; index<2; index++)
{
sum+=(ma->M[index]*mb->M[index][col]); // add next product pair
} // end for index
sum += mb->M[index][col]; // add in last element * 1
mprod->M[col] = sum; // insert resulting col element
} // end for col
return(1);
} // end Mat_Mul_1X2_3X2
73
Matrix Translation
75
Matrix Rotation
int Rotate_Polygon2D_Mat(POLYGON2D_PTR poly, int theta)
{
// function rotates the local coordinates of the polygon
if (!poly) return(0); // test for valid pointer
Mat_Init_3X2(&mr,cos_look[theta],sin_look[theta],
-sin_look[theta],cos_look[theta], 0, 0);
76
Matrix Rotation
77
Title Screen Art
Transformation Example 1
79
Transformation Example 2
80
PROJECTION
Projection
World space
Orthographic Perspective
Classical Projections
First-Person perspective
• A graphical perspective rendered from the
viewpoint of the player character
P*(x*,y*,0)
X-axis
View-plane
Eye of viewer
Z-axis (0,0,D) D = distance of eye from view-plane
(0, 0) (W-1, 0) (Xmin, Ymax) (Xmax, Yma
Projection
Screen Plane
(0, W-1) (W-1, H-1) (Xmin, Ymin) (Xmax, Ymi
Screen Dimension
W x H
Screen Projection Plane
(0, 0) (W-1, 0) (Xmin, Ymax) (Xmax, Yma
(x, y) (X, Y)
(X, Y)
(x, y)
C
c A
a
b B
By similar triangles:
y* / y = D / (D – z)
P(x,y,z)
So y* = y / ( 1 – z / D )
P*(x*,y*,0) y
y*
Eye
Z-axis
D z
View-plane
Projection: top-view
D z
Z-axis
x*
x
P*( x*, y*, 0 )
By similar triangles:
x* / x = D / ( D – z )
P( x, y, z )
So: x* = x / ( 1 – z / D )
The projection equations
• PointP( x, y, z ) in 3D-world is
“mapped” to pixel P*( x*, y* ) in the 2D-
viewplane:
x* = x / ( 1 –
z/D) y* = y /
(1–z/D)