Simple Animated Raster Painting CS3162 Introduction To Computer Graphics Assignment 1

advertisement
CS3162 Introduction To Computer Graphics
Assignment 1
Semester 1, 2000-2001
Simple Animated Raster Painting
Objectives
This assignment aims at introducing the various output primitives provided by Visual C++. In this
assignment you will write up a program which shows painted frames at successive time instances on a
window.
Tasks
You are given a sample program which shows painted frames at successive time instances on a
window. You can download this program from your lecturer's web page.
You should try to understand the code in the source file "painting.cpp", which you will write up in this
assignment. In this "painting.cpp" file, the "DoPainting" function already consists a sequence of
statements of output primitives, which can serve as a good example to you.
Afterwards you should create a new design of your own, which will be implemented by re-writing this
"DoPainting" function.
You should change the "Initialization Settings" session in this "painting.cpp" file, according to your
design.
You should re-write the "DoPainting function" to implement your design. You may have to create subfunctions to be called by "DoPainting".
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
22 Oct 2000.
1. Program source code: copy the files in your program directory and subdirectories to a 1.44MB floopy
disk. Note that you can use the 'build\clean' command, for both "release" and "debug"
configurations, to remove intermediate files in your program directory, in order to minimize the size
of the program directory and subdirectories.
2. The report should include an explanation of what you have done in this assignment, in not more than
2 pages, plus the printout of the "painting.cpp" file.
Hints
Please 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"
//Initialization settings:
int gNumberOfPicturesPerCycle=20;
int gMilliSecondsPerPicture=100;
COLORREF gBackgroundColor=RGB(0,0,0);
int gWidthOfView=400;
int gHeightOfView=300;
//no. pictures to be painted in each animation cycle
//time duration for each picture, in milliseconds
//background color: whiteness or blackness are best
//width(in pixels) of the view
//height(in pixels) of the view
void DoPainting(CDC *pDC,int WhichCallInCurrentCycle)
{
//Draw a star by setting pixels. It changes color between white and red
if ((WhichCallInCurrentCycle%2)==0)
{
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)
{
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%8)==0||(WhichCallInCurrentCycle%8)==1||
(WhichCallInCurrentCycle%8)==2||(WhichCallInCurrentCycle%8)==3)
{
CBitmapDC bm_dc(IDB_BITMAP1,pDC);
BitBlt(pDC->GetSafeHdc(),300,50,30,30,bm_dc.GetSafeHdc(),0,0,SRCCOPY);
}
if ((WhichCallInCurrentCycle%8)==4||(WhichCallInCurrentCycle%8)==5||
(WhichCallInCurrentCycle%8)==6||(WhichCallInCurrentCycle%8)==7)
{
CBitmapDC bm_dc(IDB_BITMAP2,pDC);
BitBlt(pDC->GetSafeHdc(),300,50,30,30,bm_dc.GetSafeHdc(),0,0,SRCCOPY);
}
//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;
}
}
//Draw the star. The previous instance of the star should be reset to blackness.
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 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);
}
//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(12,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);
}
Download