Simple Animated Raster Painting CS3162 Introduction To Computer Graphics Assignment 1

advertisement
CS3162 Introduction To Computer Graphics
Assignment 1
Semester B, 2000-2001
Simple Animated Raster Painting
Objectives
This assignment aims at introducing various output primitives provided by Visual C++. You will write up
a program which shows painted frames at successive time instances on a window.
Important: Before attempting this assignment, you must finish tutorial exercise 2 first.
Tasks
You are given a sample program which shows painted frames at successive time instances on a
window. You can download this program from the course web page. (This program is different from the
sample program for tutorial exercise 2)
You should try to understand the code in the source file "painting.cpp". There are 2 functions in
“painting.cpp”: “InitializeSettings” and “DoPainting”, which already consist of sample statements, serving
as an example to you.
Afterwards you should create a new design of your own, which will be implemented by re-writing the
“InitializeSettings” and "DoPainting" functions.
Grading Criteria
Elementary requirement of this assignment is to practice a variety of primitives, together with their
properties, provided by Visual C++.
Extra bonus will be given according to the innovative idea of animations or paintings done in the
program.
Submission
Please submit your program source code together with a report, into your lecturer's mailbox, on or before
9 Mar 2001 (Fri) 5:00pm.
Late submissions before 12 Mar 2001 (Mon) 5:00pm are also accepted, with a deduction of 30% of
marks. No submission will be accepted after 12 Mar 2001 (Mon) 5:00pm.
1. Program source code: copy the files in your program directory and subdirectories to a 1.44MB floppy
disk. You should only put essential files into the floppy disk (Please refer to step 15 of tutorial
exercise 2).
2. The report should include an explanation of what you have done in this assignment, plus the printout
of the "painting.cpp" file. The explanation should be more than half page, but not more than 2
pages.
Hints
It is helpful to read the following topics in MSDN (Invoked by the Visual C++ command: Help\Contents):
- BitBlt, StretchBlt
- CDC class members: LineTo, MoveTo, Ellipse, Polygon, Polyline, SetPixel, TextOut, Rectangle,
FillRect, FloodFill, etc.
Original code in "painting.cpp"
// Painting.cpp : implementation file
//
#include "stdafx.h"
#include "resource.h"
#include "asg1.h"
//======================================================
//InitializeSettings - Initialize settings
//
Followings are the global variables of system
//
settings which are needed to be defined:
//
gBackgroundColor, gWidthOfView,gHeightOfView,
//
gNumberOfPicturesPerCycle,gMilliSecondsPerPicture
//======================================================
void InitializeSettings()
{
gBackgroundColor=RGB(0,0,0);
//background color
gWidthOfView=400;
//width(in pixels) of the view
gHeightOfView=300;
//height(in pixels) of the view
gNumberOfPicturesPerCycle=20;
//no. pictures to be painted
//in each animation cycle
gMilliSecondsPerPicture=100;
//time duration for each picture,
//in milliseconds
}
//======================================================
//DoPainting - Paint the picture
//======================================================
void DoPainting(CDC *pDC,int WhichCallInCurrentCycle)
{
//Draw a star by setting pixels. It changes color between white and red
if ((WhichCallInCurrentCycle%2)==0) //Even frames
{
pDC->SetPixel(100,50, RGB(255,255,255));
pDC->SetPixel(100,50-2,RGB(255,255,255));
pDC->SetPixel(100,50+2,RGB(255,255,255));
pDC->SetPixel(100-2,50,RGB(255,255,255));
pDC->SetPixel(100+2,50,RGB(255,255,255));
}
if ((WhichCallInCurrentCycle%2)==1) //Odd frames
{
pDC->SetPixel(100,50 ,RGB(255,0,0));
pDC->SetPixel(100,50-2,RGB(255,0,0));
pDC->SetPixel(100,50+2,RGB(255,0,0));
pDC->SetPixel(100-2,50,RGB(255,0,0));
pDC->SetPixel(100+2,50,RGB(255,0,0));
}
//Draw a star (by using bitmaps) changing its colors, at location:
(300,50)
//The size of the bitmaps of the star is 30x30
if ((WhichCallInCurrentCycle==0)||
(WhichCallInCurrentCycle==1)||
(WhichCallInCurrentCycle==2)||
(WhichCallInCurrentCycle==3)||
(WhichCallInCurrentCycle==4)||
(WhichCallInCurrentCycle==10)||
(WhichCallInCurrentCycle==11)||
{
(WhichCallInCurrentCycle==12)||
(WhichCallInCurrentCycle==13)||
(WhichCallInCurrentCycle==14))
CBitmapDC bm_dc(IDB_BITMAP1,pDC);
BitBlt(pDC->GetSafeHdc(),
300,50,30,30,bm_dc.GetSafeHdc(),0,0,SRCCOPY);
}
else
{
CBitmapDC bm_dc(IDB_BITMAP2,pDC);
BitBlt(pDC->GetSafeHdc(),
300,50,30,30,bm_dc.GetSafeHdc(),0,0,SRCCOPY);
}
BitBlt(pDC->GetSafeHdc(),
prev_x_star,200,30,30,bm_dc.GetSafeHdc(),0,0,BLACKNESS);
BitBlt(pDC->GetSafeHdc(),
x_star,200,30,30,bm_dc.GetSafeHdc(),0,0,SRCCOPY);
}
}
/*
//Other Sample Codes:
//===================
//Use FillRect primitive to draw a filled rectangle:
//
- maybe used to erase background.
{
CRect rect(300,110,350,150);
//Rectangle: upper left: (300,110),
//lower right: (350,150)
CBrush brush(RGB(255,0,0));
pDC->FillRect(&rect,&brush);
}
//Draw a star which appears at frame 4 to frame 9, at location (300,100)
if ((WhichCallInCurrentCycle>=4)&&
(WhichCallInCurrentCycle<=9))
{
CBitmapDC bm_dc(IDB_BITMAP1,pDC);
BitBlt(pDC->GetSafeHdc(),
300,100,30,30,bm_dc.GetSafeHdc(),0,0,SRCCOPY);
}
else
{
CBitmapDC bm_dc(IDB_BITMAP1,pDC);
BitBlt(pDC->GetSafeHdc(),
300,100,30,30,bm_dc.GetSafeHdc(),0,0,BLACKNESS);
}
//Use Polygon primitive to draw a star, outlined with red color,
//filled with green color.
{
CPoint vertex[5];
vertex[0]=CPoint(0,10);
vertex[1]=CPoint(20,10);
vertex[2]=CPoint(4,20);
vertex[3]=CPoint(10,0);
vertex[4]=CPoint(16,20);
CPen pen(PS_SOLID,1,RGB(255,0,0));
CBrush brush(RGB(0,255,0));
CPen *pOldPen=pDC->SelectObject(&pen);
CBrush *pOldBrush=pDC->SelectObject(&brush);
pDC->Polygon(vertex,sizeof(vertex)/sizeof(*vertex));
pDC->SelectObject(pOldPen);
pDC->SelectObject(pOldBrush);
}
//Draw a star, which has a tail, moving horizontally
{
CBitmapDC bm_dc(IDB_BITMAP2,pDC);
int HorizontalMovementOfStarPerPicture=
gWidthOfView/gNumberOfPicturesPerCycle+1;
int x_star=
WhichCallInCurrentCycle*HorizontalMovementOfStarPerPicture;
int prev_x_star=x_star-HorizontalMovementOfStarPerPicture;
//Draw a horizontal line following the moving star
//the line has 256 green dots, in different intensity
//Steps: Draw a black line to erase previous instance,
//
and then draw current dots
int x_line_head=x_star-10;
int x_line_tail=x_line_head-256;
//step1:
{
CPen pen(PS_SOLID,1,RGB(0,0,0));
CPen *pOld=pDC->SelectObject(&pen);
pDC->MoveTo(0,200+10);
pDC->LineTo(gWidthOfView,200+10);
pDC->SelectObject(pOld);
}
//step2:
{
int green=0;
int x;
for (x=x_line_tail;x<x_line_head;x=x+1)
{
pDC->SetPixel(x,200+10,RGB(0,green,0));
green=green+1;
}
}
//Boundary fill a region bounded by red color. Fill it using blue color
{
CBrush brush(RGB(0,0,255));
CBrush *pOldBrush=pDC->SelectObject(&brush);
pDC->FloodFill(10,15,RGB(255,0,0));
pDC->SelectObject(pOldBrush);
}
//Try paint some text
{
CFont font;
font.CreateFont(16,0,0,0,FW_NORMAL,false,false,false,
ANSI_CHARSET,OUT_TT_PRECIS,
CLIP_TT_ALWAYS,DEFAULT_QUALITY,
FF_ROMAN,NULL);
CFont *pOldFont=pDC->SelectObject(&font);
pDC->SetTextColor(RGB(255,0,0));
pDC->SetBkMode(TRANSPARENT);
pDC->TextOut(200,130,"hello");
pDC->SelectObject(pOldFont);
}
//Draw the star. The previous instance of the star should be
//
reset to blackness.
*/
Download