Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology &...

60
Computer Graphics Computer Graphics Prepared By: Bahram Taheri Prepared By: Bahram Taheri Sept. 2006 Sept. 2006 Amirkabir University of Technology Amirkabir University of Technology & & Birmingham University Birmingham University

Transcript of Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology &...

Page 1: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

Computer GraphicsComputer Graphics

Prepared By: Bahram TaheriPrepared By: Bahram TaheriSept. 2006Sept. 2006

Amirkabir University of TechnologyAmirkabir University of Technology&&

Birmingham UniversityBirmingham University

Page 2: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

Geometric TransformationsGeometric Transformations

2D and 3D Transformations2D and 3D Transformations Translation (Deals with position)Translation (Deals with position) Scaling (Deals with size)Scaling (Deals with size) Rotation (Deals with orientation)Rotation (Deals with orientation)

Page 3: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

2D Transformation2D Transformation

Translating Points Translating Points in the (x, y) Planein the (x, y) Plane

Moving point p(x,y) Moving point p(x,y) to p’(x’,y’) so thatto p’(x’,y’) so that

y

x

dyy

dxx

'

'

Consider the following column vectors:

y

x

d

dT

y

xp

y

xp that So ,

'

'' ,

Page 4: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

Example of TranslationExample of Translation Translate the following figure by T(3, -Translate the following figure by T(3, -

4)4)

2 5

3

7

A

B

C

x

y

1

5'

4

3

3

2ATA

This can be simply shown as A’=A+T

Page 5: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

TranslationTranslationTranslated FigureTranslated Figure

5

3

A

B

C

x8

-1

Page 6: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

ScalingScaling

Points Can be Stretched or Shrunken into Points Can be Stretched or Shrunken into New Points along the x and y directions New Points along the x and y directions by the Following Multiplications:by the Following Multiplications:

PSPy

x

s

s

y

x

ysyxsx

y

x

yx

.'or .0

0

'

'

:FormMatrix in the And

.' and .'

Page 7: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

ScalingScaling

Scaling is about the size as well as Scaling is about the size as well as distance from the Origindistance from the Origin

If the Scaling Factors Are Smaller If the Scaling Factors Are Smaller than 1 then the Figure will become than 1 then the Figure will become Smaller as Well as Closer to the Smaller as Well as Closer to the OriginOrigin

If the Scaling Factor is Larger than If the Scaling Factor is Larger than One, then the Figure will become One, then the Figure will become Larger and Further from the Origin.Larger and Further from the Origin.

Page 8: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

ScalingScaling

Let’s scale the following triangle by Let’s scale the following triangle by ½ in x direction and by 1/3 in y ½ in x direction and by 1/3 in y direction.direction.

2 5

3

7

A

B

C

x

y

3.5

Page 9: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

Matrix Transformation of a Matrix Transformation of a Scaled FigureScaled Figure

PSPy

x

s

s

y

x

ysyxsx

y

x

yx

.'or .0

0

'

'

:FormMatrix in the And

.' and .'

1

1

3

2.

310

021

'

''

:A(2,3)Point For e.g. FormMatrix in the And

13.3

1' and 12.

2

1'

y

xA

yx

Page 10: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

Scaled ShapeScaled Shape

2 5

3

7

A

B

C

x

y

)3/7,75.1('

)1,5.2('

)1,1('

B

C

A

Page 11: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

Some Points About ScalingSome Points About Scaling

Scaling Can be Performed around a Point Scaling Can be Performed around a Point Other than the OriginOther than the Origin

In Differential Scaling,In Differential Scaling, In Uniform Scaling, In Uniform Scaling,

yx SS

yx SS

Page 12: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

Study on Study on originorigin of 2 methods of of 2 methods of scalingscaling

Prepared By:Prepared By:Hadi KhayyamianHadi KhayyamianVala NamdarpourVala Namdarpour

Mechanical Eng. 1Mechanical Eng. 1stst Year YearBirmingham-AUTBirmingham-AUT

Feb. 2008Feb. 2008

Page 13: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

First method : Scaling with identical proportionalityFirst method : Scaling with identical proportionality

Here, x-axis coefficient ( Here, x-axis coefficient ( ss11) & y-axis ) & y-axis

coefficient (coefficient (ss22) are the same. ) are the same.

d 1

d 2

d 3

a1c1b1 a1c1 b1

a2

b2

c2

a2

b2

c2 )(1

1: 1

11

2222 cx

sc

sccyd

)(1

1: 1

11

2222 bx

sb

sbbyd

)(1

1: 1

11

2223 ax

sa

saayd

Page 14: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

And point via intersecting dAnd point via intersecting d11 and d and d22 ,when s ,when s11= = ss22 ,is: ,is: origin (0 , 0)origin (0 , 0)

point via intersecting dpoint via intersecting d33 and d and d22 ,when s ,when s11= s= s22 ,is: ,is: origin (0 , 0)origin (0 , 0)

point via intersecting dpoint via intersecting d11 and d and d33 ,when s ,when s11= s= s22 ,is: ,is: origin (0 , 0)origin (0 , 0)

So they have common So they have common originsorigins

Page 15: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

second method : differential scaling ( Ssecond method : differential scaling ( S11≠≠ S S22))

(x2 ,y2)

)x3 ,y3(

)x1,y1(

))1()1(

)1()1(

(

))1()1(

)1()1(

(

11

22

11

22

221

22

1

22

2

SbSb

ScSc

cbSSc

SSb

x

))1()1(

)1()1(

(

))1()1(

)1()1(

(

11

22

11

22

221

22

1

22

1

SaSa

ScSc

caSSc

SSa

x

))1()1(

)1()1(

(

))1()1(

)1()1(

(

11

22

11

22

221

22

1

22

3

SaSa

SbSb

baSSb

SSa

x

Page 16: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

In this case you can see that In this case you can see that xx1 1 ≠ ≠ xx2 2 ≠ ≠

xx33 so their points are different. so their points are different.

So they have different So they have different originsorigins

Page 17: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.
Page 18: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

Rotation of A FigureRotation of A Figure A Figure Can Be Usually Rotated About a A Figure Can Be Usually Rotated About a

Point Through an Angle in a Specific Point Through an Angle in a Specific DirectionDirection

The Mathematical Definition of Rotation is:The Mathematical Definition of Rotation is:

PRPy

x

y

x

yxy

yxx

.'or .cossin

sincos

'

'

:have weformmatrix In

cos.sin.'

sin.cos.'

Page 19: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

RotationRotation

The Given Rotation Formulas Are The Given Rotation Formulas Are About the OriginAbout the Origin

Rotation Can be About Any Arbitrary Rotation Can be About Any Arbitrary PointPoint

Counterclockwise Rotations Are Counterclockwise Rotations Are PositivePositive

Clockwise Rotations Are Negative Clockwise Rotations Are Negative

Page 20: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

Deriving the Rotation Deriving the Rotation EquationEquation

r

x

y

),( yxP

)','(' yxP

)cos(.)sin(.)cos().sin(.)sin().cos(.)sin(.'

)sin(.)cos(.)sin().sin(.)cos().cos(.)cos(.'

)sin(.),cos(.

yxrrry

yxrrrx

while

ryrx

Page 21: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

Homogeneous Coordinates Homogeneous Coordinates and Matrix Representation of and Matrix Representation of

2D Transformations2D Transformations The matrix representation for translation, The matrix representation for translation,

scaling and rotation are: scaling and rotation are:

P’ = T+PP’ = T+P

P’ = S*PP’ = S*P

P’ = R*PP’ = R*P

If we express the points in homogeneous If we express the points in homogeneous coordinates, then we can treat all three coordinates, then we can treat all three transformations as multiplication.transformations as multiplication.

In homogeneous coordinates we represent a In homogeneous coordinates we represent a point by a triple (x,y,W) rather than a pair point by a triple (x,y,W) rather than a pair (x,y). (x,y).

Page 22: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

Homogeneous Coordinates Homogeneous Coordinates ContinuedContinued

We say that two sets of triples (x,y,W) and We say that two sets of triples (x,y,W) and (x’,y’,W’) are the same points, if and only if one (x’,y’,W’) are the same points, if and only if one set is a multiple of the other set. For example, set is a multiple of the other set. For example, (2,3,5) and (4,6,10) represent the same points.(2,3,5) and (4,6,10) represent the same points.

At least one of the homogeneous coordinates At least one of the homogeneous coordinates must be nonzero.must be nonzero.

(0,0,0) is not allowed.(0,0,0) is not allowed. If W coordinate is nonzero, we can divide the If W coordinate is nonzero, we can divide the

other coordinates by W to obtain the Cartesian other coordinates by W to obtain the Cartesian coordinates of the point:coordinates of the point:(x,y,W) (x,y,W) (x/W, y/W,1) (x/W, y/W,1) The Cartesian The Cartesian coordinates!coordinates!

Points with W=0 are called points at infinity.Points with W=0 are called points at infinity.

Page 23: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

The Plane Represented by The Plane Represented by W=1W=1

Each triple is usually used to show a point in 3-D space. Each triple is usually used to show a point in 3-D space. However, here we use it to represent points in 2-D space. However, here we use it to represent points in 2-D space. Each homogeneous point, by all its representations, creates Each homogeneous point, by all its representations, creates a line in the xyW coordinates. Here, W=1 represents the a line in the xyW coordinates. Here, W=1 represents the

Cartesian representation of the homogenized points:Cartesian representation of the homogenized points:

Y

X

WP

W=1 Plane

The line representing Homogenized points (x,y,W)

Page 24: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

Translation in Homogenous Translation in Homogenous CoordinatesCoordinates

The new equation becomes:The new equation becomes:

Where the translation matrix is: Where the translation matrix is:

1

.

100

10

01

1

'

'

y

x

d

d

y

x

y

x

100

10

01

),( y

x

yx d

d

ddT

Page 25: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

Translation + Translation in Translation + Translation in Homogeneous CoordinatesHomogeneous Coordinates

ons. translatiofn Compositioor ion,Concatenat

,Catenation g,Compoundin as toreferred ismatrix This

),(

:indeed isation net transl The

100

10

01

100

10

01

.

100

10

01

:Where

)).,().,(("

)).,().(,("

:Hence

').,("

).,('

2121

21

21

1

1

2

2

1122

1122

22

11

yyxx

yy

xx

y

x

y

x

yxyx

yxyx

yx

yx

ddddT

dd

dd

d

d

d

d

PddTddTP

PddTddTP

PddTP

PddTP

Page 26: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

Composition of Scalings in Composition of Scalings in Homogeneous CoordinatesHomogeneous Coordinates

').,("

).,('

:tivemultiplica be should scalings

successive additive, are ons translatisuccessive theasJust

).,('

1

.

100

00

00

1

'

'

22

11

PSSSP

PSSSP

PSSSP

y

x

S

S

y

x

yx

yx

yx

y

x

Page 27: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

Combination of ScalingsCombination of Scalings

1

.

100

0.0

00.

1

"

"

1

.

100

00

00

.

100

00

00

1

"

"

)).,().,(("

Hence

').,("

).,('

21

21

1

1

2

2

1122

22

11

y

x

SS

SS

y

x

y

x

S

S

S

S

y

x

PSSSSSSP

PSSSP

PSSSP

yy

xx

y

x

y

x

yxyx

yx

yx

Page 28: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

Rotation in Homogeneous Rotation in Homogeneous CoordinatesCoordinates

The rotation equation The rotation equation is:is:

tivemultiplica as wellas additive are rotations successive that prove will weNow

).('

100

0cossin

0sincos

)( where

1

.

100

0cossin

0sincos

1

'

'

PRP

R

y

x

y

x

Page 29: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

Successive RotationsSuccessive Rotations

1

.

100

0coscossinsinsincoscossin

0cossinsincossinsincoscos

1

"

"

1

.

100

0cossin

0sincos

.

100

0cossin

0sincos

1

"

"

).().("

').("

).('

y

x

y

x

y

x

y

x

PRRP

PRP

PRP

Page 30: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

Borland GraphicsBorland Graphics

Video Adapters:Video Adapters: Monochrome Display Adapter (MDA) for text display Monochrome Display Adapter (MDA) for text display

onlyonly Color/Graphics Adapter (CGA)Color/Graphics Adapter (CGA) Enhanced Graphics Adapter (EGA)Enhanced Graphics Adapter (EGA) Video Graphics Array adapter (VGA)Video Graphics Array adapter (VGA)

Each adapter can operate in a variety of modes:Each adapter can operate in a variety of modes: Screen display of 40 or 80 columns (text mode only)Screen display of 40 or 80 columns (text mode only) Display resolution (Graphics mode only)Display resolution (Graphics mode only) Display type (Color or black & white)Display type (Color or black & white)

Page 31: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

Screen ModeScreen Mode

The screen operating mode is The screen operating mode is defined when your program calls one defined when your program calls one of the mode-defining functions of the mode-defining functions TextmodeTextmode InitgraphInitgraph setgraphmodesetgraphmode

Page 32: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

Text modeText mode: Screen is divided into : Screen is divided into 40 or 80 columns (wide) and 25, 43, 40 or 80 columns (wide) and 25, 43, or 50 lines (high). or 50 lines (high). Each cell will consist of an attribute and Each cell will consist of an attribute and

a character. The character will be the a character. The character will be the displayed ASCII character; the attribute displayed ASCII character; the attribute specifies how the character is displayed specifies how the character is displayed (its color, intensity, …). The top left (its color, intensity, …). The top left corner of the display screen is position corner of the display screen is position (1,1), (1,1),

x-coordinate increasing from left to right x-coordinate increasing from left to right Y-coordinate increasing from top to Y-coordinate increasing from top to

bottombottom

Page 33: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

Graphics Mode: Screen is divided into Graphics Mode: Screen is divided into pixels (what’s a pixel anyway?)pixels (what’s a pixel anyway?) Each pixel displays a dot onscreen.Each pixel displays a dot onscreen. The number of pixels (the resolution) The number of pixels (the resolution)

depends on the type of video adapter depends on the type of video adapter connected to your system and the mode connected to your system and the mode that adapter is in.that adapter is in.

You can control the color of each pixel.You can control the color of each pixel. The top left corner of the screen is The top left corner of the screen is

position (1,1). With x-coordinate position (1,1). With x-coordinate increasing from left to right and the y-increasing from left to right and the y-coordinate increasing from top to bottom.coordinate increasing from top to bottom.

Page 34: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

Windows and ViewportsWindows and Viewports Borland provides functions for Borland provides functions for

creating and managing windows on creating and managing windows on your screen in the text mode.your screen in the text mode.

It also provides the same for It also provides the same for managing viewports in the graphics managing viewports in the graphics mode.mode.

Page 35: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

What is a Window?What is a Window? A window is a rectangular area defined on A window is a rectangular area defined on

your computer’s video screen while it is in your computer’s video screen while it is in the text mode.the text mode.

When your program writes to the screen, When your program writes to the screen, its output is restricted to the active its output is restricted to the active window. The rest of the screen (outside window. The rest of the screen (outside the window) remains untouched.the window) remains untouched.

The default window is a full-screen text The default window is a full-screen text window. However, we may create smaller window. However, we may create smaller windows (with a call to window function) windows (with a call to window function) This function specifies the window’s This function specifies the window’s position in terms of screen coordinates.position in terms of screen coordinates.

Page 36: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

What is a View Port?What is a View Port? In graphics mode, we can define a In graphics mode, we can define a

rectangular area on the computer’s video rectangular area on the computer’s video screen, which is called a viewport.screen, which is called a viewport.

The graphic output of our programs will be The graphic output of our programs will be displayed on the viewport as a virtual displayed on the viewport as a virtual screen. screen.

The rest of the screen remains untouched The rest of the screen remains untouched (outside the viewport)(outside the viewport)

We may define a viewport in terms of the We may define a viewport in terms of the screen coordinates with a call to screen coordinates with a call to setviewportsetviewport function. function.

Page 37: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

Important!Important!

Only for defining the windows or Only for defining the windows or viewports we will refer to the main viewports we will refer to the main screen’s coordinates!screen’s coordinates!

Otherwise for every other application Otherwise for every other application we will refer relative to the window’s we will refer relative to the window’s (texts) or viewport’s (graphics) (texts) or viewport’s (graphics) coordinates.coordinates.

Page 38: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

Programming in the text Programming in the text modemode

The modes available on your system The modes available on your system depend on the type of video adapter and depend on the type of video adapter and monitor you have. You can specify the monitor you have. You can specify the current text mode with a call to current text mode with a call to textmodetextmode function.function.

Text mode functions are divided into five Text mode functions are divided into five separate groups:separate groups: Text output and manipulationText output and manipulation Window and mode controlWindow and mode control Attribute controlAttribute control State queryState query Cursor shapeCursor shape

Page 39: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

Text output and manipulation Text output and manipulation functionsfunctions

cprintfcprintf Sends formatted output to the screenSends formatted output to the screen cputscputs Sends a string to the screenSends a string to the screen getchegetche Reads a char and echoes it to the scr.Reads a char and echoes it to the scr. putchputch Sends a single char to the screenSends a single char to the screen clreolclreol Clears from the cursor to the end of the lineClears from the cursor to the end of the line clrscrclrscr Clears the text windowClears the text window dellinedelline Deletes the line where the cursor restsDeletes the line where the cursor rests gotoxygotoxy positions the cursorpositions the cursor inslineinsline Inserts a blank line below cursorInserts a blank line below cursor movetextmovetext Copies text from an area onscreen to anotherCopies text from an area onscreen to another gettextgettext Copies text from an area onscreen to memoryCopies text from an area onscreen to memory puttextputtext Copies text from memory to an area onscreenCopies text from memory to an area onscreen

Page 40: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

Window and mode control Window and mode control functionsfunctions

textmodetextmode sets the screen to a sets the screen to a text modetext mode

windowwindow defines a text-mode defines a text-mode window window

Page 41: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

Text-mode Attribute Text-mode Attribute FunctionsFunctions

textattrtextattr Sets the background and Sets the background and foreground colors at foreground colors at

the the same timesame time textbackgroundtextbackground sets the background sets the background

colorcolor textcolortextcolorSets the foreground colorSets the foreground color highvideohighvideo Sets text to high intensitySets text to high intensity lowvideolowvideoSets text to low intensitySets text to low intensity normvideonormvideo Sets text to original intensitySets text to original intensity

Page 42: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

Some PointsSome Points

The attribute control function uses an The attribute control function uses an 8-bit value to set the background, 8-bit value to set the background, foreground and blink attributes.foreground and blink attributes.

The first 4 lowest bits represent the The first 4 lowest bits represent the foreground color, the next 3 bits foreground color, the next 3 bits represent the background color and represent the background color and the 8the 8thth bit (the highest one = 128) is bit (the highest one = 128) is for blinking.for blinking.

Page 43: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

State-Query FunctionsState-Query Functions

gettextinfogettextinfo Fills in a Fills in a text_infotext_info structure structure with information about the with information about the current text windowcurrent text window

wherexwherex Gives the x-coordinate Gives the x-coordinate of of the cell containing the cursorthe cell containing the cursor

whereywherey Gives the y coordinate of the Gives the y coordinate of the cell containing the cursorcell containing the cursor

Page 44: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

About gettextinfo functionAbout gettextinfo function

This function fills a text_info structure This function fills a text_info structure (defined in conio.h) with several details (defined in conio.h) with several details about the text window, including:about the text window, including: The current video modeThe current video mode The window’s position in absolute screen The window’s position in absolute screen

coordinatescoordinates The window’s dimensionsThe window’s dimensions The current foreground and background colorsThe current foreground and background colors The cursor’s current positionThe cursor’s current position

Page 45: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

_setcursortype_setcursortype

This function enables you to change This function enables you to change the appearance of your cursor. The the appearance of your cursor. The values are:values are: _NOCURSOR, which turns off the cursor_NOCURSOR, which turns off the cursor _SOLIDCURSOR, which gives you a solid _SOLIDCURSOR, which gives you a solid

block (large) cursorblock (large) cursor And _NORMALCURSOR, which gives you And _NORMALCURSOR, which gives you

the normal underscore cursorthe normal underscore cursor

Page 46: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

Text ModesText Modes

Computer may operate in one of the Computer may operate in one of the several text modes available on your several text modes available on your computer. The enumeration type computer. The enumeration type text_modes, defined in conio.h, text_modes, defined in conio.h, enables you to use symbolic names for enables you to use symbolic names for for the mode argument rather than the for the mode argument rather than the raw mode numbers. However, if you raw mode numbers. However, if you use the symbolic constants, you must use the symbolic constants, you must include conio.h in your source code.include conio.h in your source code.

Page 47: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

Numeric and Symbolic Values Numeric and Symbolic Values for text_modesfor text_modes

Symbolic Symbolic ConstantConstant

NumeriNumeric Valuec Value

Video Text ModeVideo Text Mode

LASTMODELASTMODE -1-1 Previous text mode enabledPrevious text mode enabled

BW40BW40 00 Black and white, 40 columnsBlack and white, 40 columns

C40C40 11 16-color, 40 columns16-color, 40 columns

BW80BW80 22 Black and white 80 columnsBlack and white 80 columns

C80C80 33 16-color, 80 columns16-color, 80 columns

MONOMONO 77 Monochrome, 80 columnsMonochrome, 80 columns

C4350C4350 6464 EGA, 80X43; VGA, 80X50EGA, 80X43; VGA, 80X50

Page 48: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

Examples of Using Symbolic Examples of Using Symbolic Constants and Numeric ValuesConstants and Numeric Values

SyntaxSyntax Operating ModeOperating Mode LibraryLibraryTextmode(0)Textmode(0) Black and white, 40 columnsBlack and white, 40 columns

Textmode(BW8Textmode(BW80)0)

Black and white, 80 columnsBlack and white, 80 columns conio.hconio.h

Textmode(C40)Textmode(C40) 16-color, 40 columns16-color, 40 columns conio.hconio.h

Textmode(3)Textmode(3) 16-color, 80 columns16-color, 80 columns

Textmode(7)Textmode(7) Monochrome, 80 columnsMonochrome, 80 columns

Textmode(C435Textmode(C4350)0)

EGA, 80X43; VGA, 80X50EGA, 80X43; VGA, 80X50 conio.hconio.h

Page 49: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

ColorsColorsSymbolic ConstantsSymbolic Constants Numeric ValueNumeric Value Foreground or backgroundForeground or background

BLACKBLACK 00 BothBoth

BLUEBLUE 11 BothBoth

GREENGREEN 22 BothBoth

CYANCYAN 33 BothBoth

REDRED 44 BothBoth

MAGENTAMAGENTA 55 BothBoth

BROWNBROWN 66 BothBoth

LIGHTGRAYLIGHTGRAY 77 BothBoth

DARKGRAYDARKGRAY 88 Foreground onlyForeground only

LIGHTBLUELIGHTBLUE 99 Foreground onlyForeground only

LIGHTGREENLIGHTGREEN 1010 Foreground onlyForeground only

LIGHTCYANLIGHTCYAN 1111 Foreground onlyForeground only

LIGHTREDLIGHTRED 1212 Foreground onlyForeground only

LIGHTMAGENTALIGHTMAGENTA 1313 Foreground onlyForeground only

YELLOWYELLOW 1414 Foreground onlyForeground only

WHITEWHITE 1515 Foreground onlyForeground only

BLINKBLINK 128128 Foreground onlyForeground only

Page 50: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

initgraphinitgraph

SyntaxSyntax #include <graphics.h>#include <graphics.h> void initgraph(int *graphdriver, int *graphmode, char void initgraph(int *graphdriver, int *graphmode, char

*pathtodriver);*pathtodriver); DescriptionDescription initgraph initializes the graphics system by loading a initgraph initializes the graphics system by loading a

graphics driver from disk (or validating a registered driver), graphics driver from disk (or validating a registered driver), and putting the system into graphics mode. and putting the system into graphics mode.

To start the graphics system, first call the initgraph To start the graphics system, first call the initgraph function. initgraph loads the graphics driver and puts the function. initgraph loads the graphics driver and puts the system into graphics mode. You can tell initgraph to use a system into graphics mode. You can tell initgraph to use a particular graphics driver and mode, or to autodetect the particular graphics driver and mode, or to autodetect the attached video adapter at run time and pick the attached video adapter at run time and pick the corresponding driver. corresponding driver.

Page 51: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

initgraphinitgraph

If you tell initgraph to autodetect, it calls detectgraph to If you tell initgraph to autodetect, it calls detectgraph to select a graphics driver and mode. initgraph also resets all select a graphics driver and mode. initgraph also resets all graphics settings to their defaults (current position, palette, graphics settings to their defaults (current position, palette, color, viewport, and so on) and resets graphresult to 0. color, viewport, and so on) and resets graphresult to 0.

Normally, initgraph loads a graphics driver by allocating Normally, initgraph loads a graphics driver by allocating memory for the driver (through _graphgetmem), then loading memory for the driver (through _graphgetmem), then loading the appropriate BGI file from disk. As an alternative to this the appropriate BGI file from disk. As an alternative to this dynamic loading scheme, you can link a graphics driver file dynamic loading scheme, you can link a graphics driver file (or several of them) directly into your executable program (or several of them) directly into your executable program file. file.

pathtodriver specifies the directory path where initgraph pathtodriver specifies the directory path where initgraph looks for graphics drivers. initgraph first looks in the path looks for graphics drivers. initgraph first looks in the path specified in pathtodriver, then (if they are not there) in the specified in pathtodriver, then (if they are not there) in the current directory. Accordingly, if pathtodriver is null, the current directory. Accordingly, if pathtodriver is null, the driver files (*.BGI) must be in the current directory. This is driver files (*.BGI) must be in the current directory. This is also the path settextstyle searches for the stroked character also the path settextstyle searches for the stroked character font files (*.CHR). font files (*.CHR).

Page 52: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

graphdrivergraphdriver

*graphdriver is an integer that *graphdriver is an integer that specifies the graphics driver to be specifies the graphics driver to be used. You can give it a value using a used. You can give it a value using a constant of the graphics_drivers constant of the graphics_drivers enumeration type, which is defined in enumeration type, which is defined in graphics.h and listed below. graphics.h and listed below.

Page 53: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

Graphics_deiver constantsGraphics_deiver constants

graphics_drivers constant Numeric value DETECT 0 (requests autodetect)

CGA 1

MCGA 2

EGA 3

EGA64 4

EGAMONO 5

IBM8514 6

HERCMONO 7

ATT400 8

VGA 9

PC3270 10

Page 54: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

graphmodegraphmode

*graphmode is an integer that specifies the initial graphics *graphmode is an integer that specifies the initial graphics mode (unless *graphdriver equals DETECT; in which case, mode (unless *graphdriver equals DETECT; in which case, *graphmode is set by initgraph to the highest resolution *graphmode is set by initgraph to the highest resolution available for the detected driver). You can give *graphmode available for the detected driver). You can give *graphmode a value using a constant of the graphics_modes a value using a constant of the graphics_modes enumeration type, which is defined in graphics.h and listed enumeration type, which is defined in graphics.h and listed below. below.

graphdriver and graphmode must be set to valid values graphdriver and graphmode must be set to valid values from the following tables, or you will get unpredictable from the following tables, or you will get unpredictable results. The exception is graphdriver = DETECT. results. The exception is graphdriver = DETECT.

Palette listings C0, C1, C2, and C3 refer to the four Palette listings C0, C1, C2, and C3 refer to the four predefined four-color palettes available on CGA (and predefined four-color palettes available on CGA (and compatible) systems. You can select the background color compatible) systems. You can select the background color (entry #0) in each of these palettes, but the other colors (entry #0) in each of these palettes, but the other colors are fixed. are fixed.

Page 55: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

Pallete ColorsPallete Colors

Palette Number Three Colors 0 LIGHTGREEN LIGHTRED YELLOW

1 LIGHTCYAN LIGHTMAGENTA WHITE

2 GREEN RED BROWN

3 CYAN MAGENTA LIGHTGRAY

Page 56: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

Return ValueReturn Value

initgraph always sets the internal initgraph always sets the internal error code; on success, it sets the error code; on success, it sets the code to 0. If an error occurred, code to 0. If an error occurred, *graphdriver is set to -2, -3, -4, or -5, *graphdriver is set to -2, -3, -4, or -5, and graphresult returns the same and graphresult returns the same value as listed below: value as listed below:

Page 57: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

Error codesError codes

Constant Name Number Meaning grNotDetected -2 Cannot detect a graphics card

grFileNotFound -3 Cannot find driver file

grInvalidDriver -4 Invalid driver

grNoLoadMem -5 Insufficient memory to load driver

Page 58: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

Graph error messagesGraph error messages

SyntaxSyntax #include <graphics.h> //In DOS mode#include <graphics.h> //In DOS mode char * grapherrormsg(int errorcode);char * grapherrormsg(int errorcode); DescriptionDescription grapherrormsg returns a pointer to the error grapherrormsg returns a pointer to the error

message string associated with errorcode, message string associated with errorcode, the value returned by graphresult. the value returned by graphresult.

Return ValueReturn Value grapherrormsg returns a pointer to an error grapherrormsg returns a pointer to an error

message string. message string.

Page 59: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

Set colorSet color

SyntaxSyntax #include <graphics.h>#include <graphics.h> void setcolor(int color);void setcolor(int color); DescriptionDescription setcolor sets the current drawing color to setcolor sets the current drawing color to

color, which can range from 0 to color, which can range from 0 to getmaxcolor. The current drawing color is getmaxcolor. The current drawing color is the value to which pixels are set when the value to which pixels are set when lines, and so on are drawn. The drawing lines, and so on are drawn. The drawing colors shown below are available for the colors shown below are available for the CGA and EGA, respectively. CGA and EGA, respectively.

Page 60: Computer Graphics Prepared By: Bahram Taheri Sept. 2006 Amirkabir University of Technology & Birmingham University.

Palette Number

Three Colors

0 LIGHTGREEN LIGHTRED YELLOW

1 LIGHTCYAN LIGHTMAGENTA WHITE

2 GREEN RED BROWN

3 CYAN MAGENTA LIGHTGRAY