Vous êtes sur la page 1sur 45

Session : Jan09-May09

UNIT II Graphics Programming

Manjunath CR

06CS65 CG&V - Unit - II Graphics programming

Session : Jan09-May09

Programming 2D

Point can be in
2D : p(x,y) 3D : p(x,y,z)

3D can represented as
Z=0 -> p(x,y,0) Triplet p=(x,y,z) or a column matrix

Vertex is a position in space 2,3 or 4D

Manjunath CR

06CS65 CG&V - Unit - II Graphics programming

Session : : Jan09-May09 Jan09-May09

OpenGL

OpenGL is a software interface that allows the programmer to create 2D and 3D graphics images OpenGL is independent of the hardware, operating, and windowing systems in use OpenGL functions in a client/server environment OpenGL functions (which are called commands) are designed to provide 2D and 3D graphics with the emphasis on 3D

Manjunath CR

06CS65 CG&V - Unit - II Graphics programming

Session : Jan09-May09

OpenGL

OpenGLs Atoms: Vertices a point is called a vertex user coordinates: possibly infinite drawing pad vertices (plural of vertex) are always 3D can also be used as 2D general form: glVertex* examples: glVertex2i(GLint x, GLint y) glVertex3f(GLfloat x, GLfloat y, GLfloat z) glVertex3fv(GLfloat[] vertex)
Manjunath CR
06CS65 CG&V - Unit - II Graphics programming 4

Session : Jan09-May09

OpenGL

Manjunath CR

06CS65 CG&V - Unit - II Graphics programming

Session : Jan09-May09

OpenGL

Manjunath CR

06CS65 CG&V - Unit - II Graphics programming

Session : Jan09-May09

OpenGl

glBegin(GL POINTS); glVertex2f(x1,y1); glVertex2f(x2,y2); glEnd(); glBegin(GL LINES); glVertex2f(x1,y1); glVertex2f(x2,y2); glEnd();
Manjunath CR
06CS65 CG&V - Unit - II Graphics programming 7

Session : Jan09-May09

Graphics function

Categories or Major gruops


primitive functions (objects: what) attribute functions (how) viewing functions (camera) transformation functions (e.g., rotation . . . ) input functions(GLUT) control functions(GLUT) Query function

Manjunath CR

06CS65 CG&V - Unit - II Graphics programming

Session : Jan09-May09

OpenGL is a state machine OpenGL functions are of two types


Primitive generating
Can cause output if primitive is visible How vertices are processed and appearance of primitive are controlled by the state

State changing
Transformation functions Attribute functions

Manjunath CR

06CS65 CG&V - Unit - II Graphics programming

Session : Jan09-May09

OpenGL Interface

Access OpenGL by 3 libraries


GL :gl as prefix OpenGL utility libraries (GLU) : use GL function , contains code for creating common objects & viewing OpenGL utility toolkit (GLUT) : control & window system. to interface the GL with external devices for major window system

Manjunath CR

06CS65 CG&V - Unit - II Graphics programming

10

Session : Jan09-May09

OpenGL Library Structure

#include <GL/glut.h> or: #include <glut.h> glFunction() gluFunction() glutFunction()

Manjunath CR

06CS65 CG&V - Unit - II Graphics programming

11

Session : Jan09-May09

Primitives & Attributes

OpenGL support 2 classes of primitives


Geometric Image or raster

Manjunath CR

06CS65 CG&V - Unit - II Graphics programming

12

Session : Jan09-May09

Geometric primitives
are specified in the problem domain : points, lines polygons, curves and surfaces

glBegin( ... ); glVertex*( ... ); . . glEnd();

Manjunath CR

06CS65 CG&V - Unit - II Graphics programming

13

Session : Jan09-May09

Line segment

Polylines

Manjunath CR

06CS65 CG&V - Unit - II Graphics programming

14

Session : Jan09-May09

polygon

Polygon : object has a border that can be described by a line loop The performance of the GP can characterized by no of polygons per second that can be rendered
Rendering in 2 ways
Edges Its interior with solid color or a pattern

3 properties of polygon
Simple, convex & flat
Manjunath CR
06CS65 CG&V - Unit - II Graphics programming 15

Session : Jan09-May09

Polygons can be Filled

Filling the Polygon Interior (2D) To be filled, polygons have to be: simple and convex. A simple polygon has a well-defined interior

Convex polygon: All points on the line segment between any 2 points inside the polygon are inside the polygon.

Manjunath CR

06CS65 CG&V - Unit - II Graphics programming

16

Session : Jan09-May09

Filling Polygons (3D)

Polygon Types The appearance of polygons depends on the attributes that have been set before

Manjunath CR

06CS65 CG&V - Unit - II Graphics programming

17

Session : Jan09-May09

Polygon Strips

Manjunath CR

06CS65 CG&V - Unit - II Graphics programming

18

Session : Jan09-May09

Manjunath CR

06CS65 CG&V - Unit - II Graphics programming

19

Session : Jan09-May09

Text

Text are of 2 form


Stroke Raster

Manjunath CR

06CS65 CG&V - Unit - II Graphics programming

20

Session : Jan09-May09

Stroke text
can be treated like all other graphics objects. Computer Graphics If the character define by a closed boundary we can fill it Advantages,
it can be defined to have all the details of any other objects It can be manipulated by standard transformation Viewed like any other graphical primitives

Manjunath CR

06CS65 CG&V - Unit - II Graphics programming

21

Session : Jan09-May09

Defining a full 128 or 256 character stroke, can be complex & font can take more memory , processing time Standard postscript fonts are defined by polynomial curves
Postscript can be used for high or low resolution application

Manjunath CR

06CS65 CG&V - Unit - II Graphics programming

22

Session : Jan09-May09

Raster text
Simple & fast

Manjunath CR

06CS65 CG&V - Unit - II Graphics programming

23

Session : Jan09-May09

Characters are defined as rectangles of bits called bit block Each block defines a single character by a pattern of 0 and 1 bits in the block Raster character can be placed in the frame buffer rapidly by a bit-block-transfer (bitblt) operation

Manjunath CR

06CS65 CG&V - Unit - II Graphics programming

24

Session : Jan09-May09

To increase the size of the character by replicating or duplicating pixels Transformation is not possible

Manjunath CR

06CS65 CG&V - Unit - II Graphics programming

25

Session : Jan09-May09

Stroke and bitmap character are created from other primitives openGl does not have a text a primitive
GLUT library provides few predefined bitmap and stroke Placed at present raster position on the display

glRasterPos2i(rx, ry);
glutBitmapCharacter(GLUT BITMAP 8 BY 13, k); rx += glutBitmapWidth(GLUT BITMAP 8 BY 13, k) K = no of ascii character
Manjunath CR
06CS65 CG&V - Unit - II Graphics programming 26

Session : Jan09-May09

Attributes
Is a property that determines how a geometrics primitives is rendered
EX: Color , thickness of the line, pattern filling the polygons

Manjunath CR

06CS65 CG&V - Unit - II Graphics programming

27

Session : Jan09-May09

Color
Tristimulus values
Additive & subtractive color
Color solid

Manjunath CR

06CS65 CG&V - Unit - II Graphics programming

28

Session : Jan09-May09

Manjunath CR

06CS65 CG&V - Unit - II Graphics programming

29

Session : Jan09-May09

RGB color From programmer view, 2 approaches RGB color model Indexed color model

Manjunath CR

06CS65 CG&V - Unit - II Graphics programming

30

Session : Jan09-May09

RGB color
Each color component stored separately in the frame buffer Usually 8 bits per component in buffer Note in glColor3f the color values range from 0.0 (none) to 1.0 (all), while in glColor3ub the values range from 0 to 255
3byte 1 for each color

Manjunath CR

06CS65 CG&V - Unit - II Graphics programming

31

Session : Jan09-May09

Four color : RGBA transparency(A=0)

- A-alpha:- opacity(A=1) or

Ex : creating fog effects or combining images

Manjunath CR

06CS65 CG&V - Unit - II Graphics programming

32

Session : Jan09-May09

Indexed color
Colors are indices into tables of RGB value Requires less memory
indices usually 8 bits not as important now
Memory inexpensive Need more colors for shading

Manjunath CR

06CS65 CG&V - Unit - II Graphics programming

33

Session : Jan09-May09

Viewing

Orthographic view default openGLs view

Projection vectors are orthogonal to the projection plane

Default camera: also sees what is behind it.

Manjunath CR

06CS65 CG&V - Unit - II Graphics programming

34

Session : Jan09-May09

OpenGL places a camera at the origin pointing in the negative z direction The default viewing volume is a box centered at the origin with sides of length 2

Manjunath CR

06CS65 CG&V - Unit - II Graphics programming

35

Session : Jan09-May09

Viewing rectangle is = 0. OpenGls default clipping volume is the 2 2 2 volume (-1 -1 1) to (1 1 1)

Manjunath CR

06CS65 CG&V - Unit - II Graphics programming

36

Session : Jan09-May09

Defining a relation between objects and camera Projection 2D-viewing (just clipping): viewing/clipping rectangle

Manjunath CR

06CS65 CG&V - Unit - II Graphics programming

37

Session : Jan09-May09

Using glOrtho
void glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far) void gluOrtho2D(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top)

Manjunath CR

06CS65 CG&V - Unit - II Graphics programming

38

Session : Jan09-May09

Matrix Mode

2 important matrices
Model-view projection

glMatrixMode(GL PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, 500.0, 0.0, 500.0); glMatrixMode(GL MODELVIEW);

Manjunath CR

06CS65 CG&V - Unit - II Graphics programming

39

Session : Jan09-May09

Matrix Mode

Two- and Three-dimensional Viewing In glOrtho(left, right, bottom, top,near, far) the near and far distances are measured from the camera
Two-dimensional vertex commands place all vertices in the plane z=0 If the application is in two dimensions, we can use the function

gluOrtho2D(left,right,bottom,top)
In two dimensions, the view or clipping volume becomes a clipping window
Manjunath CR
06CS65 CG&V - Unit - II Graphics programming 40

Session : Jan09-May09

Control Functions

GLUT functions
glutInit allows application to get command line arguments and initializes system glutInitDisplayMode requests properties for the window (the rendering context)
RGB color Single buffering Properties logically ORed together

glutWindowSize in pixels glutWindowPosition from top-left corner of display


Manjunath CR
06CS65 CG&V - Unit - II Graphics programming 41

Session : Jan09-May09

glutCreateWindow create window simple glutDisplayFunc display callback glutMainLoop enter infinite event loop

with

title

Manjunath CR

06CS65 CG&V - Unit - II Graphics programming

42

Session : Jan09-May09

#include <GL/glut.h> int main(int argc, char** argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(500,500); glutInitWindowPosition(0,0); glutCreateWindow("simple"); glutDisplayFunc(mydisplay); init(); glutMainLoop(); }

Manjunath CR

06CS65 CG&V - Unit - II Graphics programming

43

Session : Jan09-May09

Aspect ratio & View port

Manjunath CR

06CS65 CG&V - Unit - II Graphics programming

44

Session : Jan09-May09

Program structure
Most OpenGL programs have a similar structure that consists of the following functions

main():
defines the callback functions opens one or more windows with the required properties enters event loop (last executable statement)

init(): sets the state variables


Viewing Attributes

callbacks
Display function Input and window functions
Manjunath CR
06CS65 CG&V - Unit - II Graphics programming 45

Vous aimerez peut-être aussi