0% found this document useful (0 votes)
24 views87 pages

Overview CG

The document provides an overview of graphics systems and programming, detailing hardware and software components involved in graphics operations. It discusses display devices, color representation, and the graphics programming process, including the use of OpenGL as a low-level interface for graphics hardware. Additionally, it covers various mapping techniques, OpenGL functionalities, and examples of geometry creation using OpenGL commands.

Uploaded by

kohdohdohdddd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
0% found this document useful (0 votes)
24 views87 pages

Overview CG

The document provides an overview of graphics systems and programming, detailing hardware and software components involved in graphics operations. It discusses display devices, color representation, and the graphics programming process, including the use of OpenGL as a low-level interface for graphics hardware. Additionally, it covers various mapping techniques, OpenGL functionalities, and examples of geometry creation using OpenGL commands.

Uploaded by

kohdohdohdddd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
You are on page 1/ 87

Overview of a graphics system and programming

Jayanta Mukhopadhyay

Department of Computer Science & Engineering


Indian Institute of Technology Kharagpur
[email protected]

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 1 / 87


Graphics system
Hardware for I/P & O/P pictorial information.
Software routines for performing the basic graphics operations.

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 2 / 87


Display Devices
A hardware for displaying graphics primitives.

Display processor operates with a basic set of instructions.

Operates on discrete grids bounded by a rectangle with a predefined


coordinate convention.

Sets display attributes of operations, such as Color, Line style, Line


width, Brightness or Intensity, Font, Text height etc.
Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 3 / 87
Two types of Display Devices

Vector graphics device: e.g. Plotter


Maintains the current position of a drawing pen.
Basic Commands: Move to a point, Draw a line to a point, Draw Text
(Character).

Raster graphics device: e.g. CRT Terminal


Maps the discrete grids to a 2-D array of pixels, called Frame Buffer.
Basic commands: Clear frame buffer, Set a pixel ON (OFF) for display
(no-display state).
Periodically displays the pixels of a frame buffer.
May use double buffering to update and display alternately.
Implements vector graphics operations by setting necessary pixels ON
or OFF.

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 4 / 87


Display of colors in a Raster Device

A device may have independent but coherent control of displaying


three primary colors, namely Red, Green and Blue.

Use three separate color planes (2-D arrays) for a frame buffer.

For gray level display only one plane is sufficient.

The number of bits (usually 8) used to represent a pixel of a plane


decides the number of levels.

Use of Look Up Table (LUT) enables to display colors with a single


plane frame buffer.

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 5 / 87


Display of colors in a Vector Device

Chooses a color for a single intensity / ink level.

Only a few colors are available for display.

Special computational algorithms exist to bring variation of ink /


brightness density over a region to produce gray / color shades.

Halftoning algorithms.

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 6 / 87


Graphics programming: broad steps

Initialize graphics environment: Devices, 2-D / 3-D graphics, Open a


canvas, Single / Double buffers, Z-buffer, Color scheme ..

Set viewport: Rectangular area of the canvas for display


Set coordinate convention
Otherwise uses default coordinate system.

Display and other commands including choice of attributes for display.

Close graphics environment.

Specialized environments
Client-server computing.
Event driven computing.

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 7 / 87


Different coordinate systems
Device coordinates

Normalized device
coordinates.

User defined coordinates

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 8 / 87


OpenGL

Outline

Basics, functionalities, data types

Input-Output

Projection

Viewing

Transformation

Camera metaphor

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 9 / 87


What is OpenGL?
A low level software interface to graphics hardware.

You can access graphics hardware directly

Independent to window system, cross platform

High level enough, so it’s independent to graphics hardware

Industry standard

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 10 / 87


What is OpenGL?

Hardware and Operating System independent.

No commands for performing windowing tasks

No commands for obtaining input

A State Machine

Put OpenGL into a state (or mode) & that state remains in effect until
you change it

Each OpenGL window has its own separate state

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 11 / 87


What is OpenGL?
An open standard

The OpenGL language is not controlled by a single company but rather


steered by the Architecture Review Board (ARB).

Extensible

OpenGL language has an extension mechanism defined in it.

Venders can expose non-standard OpenGL features this way

Courtesy: From this slide onwards, the rest are from Prof. Ayan Chaudhury, CSE, IIT Kharagpur.

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 12 / 87


What is OpenGL?

Is often called as API

Window based programming

Event driven programming

callback function

event loop

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 13 / 87


OpenGL rendering pipeline

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 14 / 87


OpenGL pipeline (simplified)

OpenGL API calls → OpenGL command buffer → Transform &


lighting → Rasterization → Frame buffer

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 15 / 87


What OpenGL provides?

Draw with points, lines, and polygons

Matrix Operations (Transformations)

Hidden Surface Removal (Z-Buffer)

Lighting (Phong model)

Shading

Texture mapping

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 16 / 87


What OpenGL provides?

Basic drawing

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 17 / 87


What OpenGL provides?
Transformations

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 18 / 87


What OpenGL provides?
Z-buffer

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 19 / 87


What OpenGL provides?

Lighting

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 20 / 87


What OpenGL provides?

Shading

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 21 / 87


What OpenGL provides?

Texture mapping
There are limitations of geometric modelling
Many natural phenomena and objects are hard to model
E.g. Cloud, grass, terrain, skin,..

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 22 / 87


OpenGL Texture mapping

Consider modelling of an orange

Start with an orange-colored sphere: too simple


Replace sphere with a more complex shape
Does not capture surface characteristics (small dimples)
Takes too many polygons to model all the dimples

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 23 / 87


Modelling of an orange (contd.)

Take a picture of a real orange, scan it, and “paste” onto simple
geometric model
This process is called texture mapping

Still might not be sufficient because resulting surface will be smooth


Need to change local shape
Bump mapping
Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 24 / 87
3 types of mapping
1. Texture Mapping
Uses images to fill inside of polygons

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 25 / 87


3 types of mapping

2. Environment Mapping
Uses a picture of the environment for texture maps

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 26 / 87


3 types of mapping

3. Bump Mapping
Emulates altering normal vectors during the rendering process

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 27 / 87


Where does mapping take place?

Mapping techniques are implemented at the end of the rendering


pipeline
Very efficient because few polygons pass down the geometric pipeline

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 28 / 87


Texture mapping: is it simple?

Although the idea is simple - map an image to a surface - there are 3


or 4 coordinate systems involved

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 29 / 87


Texture mapping: is it simple?

(we will see these coordinate systems in the viewing lectures)


Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 30 / 87
Libraries

Basic GL

Fundamental OpenGL library

OpenGL32 on Windows

GL on most UNIX/Linux systems

GLUT
GL utility toolkit

Open a window, get input, etc.

GLU
GL utility library

Provides high level routines

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 31 / 87


OpenGL data types

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 32 / 87


OpenGL primitives

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 33 / 87


OpenGL primitives

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 34 / 87


OpenGL function format

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 35 / 87


OpenGL syntax

All OpenGL commands are prefixed by the letters gl

Defined constants begin with GL

Commands may be postfixed by

A number {2, 3, 4} indicating the number of arguments (e.g.


glVertex2i())

A letter {i, f , d, ub} indicating the data type of the arguments (e.g.
glRectf())

A letter v indicating the argument is a vector (array) (e.g.


glVertex3fv())

Most constants are defined in the include files gl.h, glu.h and glut.h

# include<glut.h> should automatically include the others

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 36 / 87


Some syntax examples

Example: Setting the current color using glColor:

Colors may have 3 components: RGB, or 4 components: RGBA.


(Think of A as opacity)

Floating point - color component values range from 0 to 1

glColor3f(0.0,0.5,1.0);
(0% Red, 50% Green, 100% Blue)

glColor4f(0.0,0.5,1.0,0.3);
(0% Red, 50% Green, 100% Blue, 30% opacity)

GLfloat color[4] = {0.0,0.5,1.0,0.3};


glColor4fv(color);
(0% Red, 50% Green, 100% Blue, 30% opacity)

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 37 / 87


Some syntax examples

Unsigned byte: color component values range from 0 to 255:

glColor3ub(0,127,255);
(0% Red, 50% Green, 100% Blue)

glColor4f(0,127,255,76);
(0% Red, 50% Green, 100% Blue, 30% opacity)

GLubyte color[4] = {0,127,255,76};


glColor4ubv(color);
(0% Red, 50% Green, 100% Blue, 30% opacity)

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 38 / 87


OpenGL clear colour

The background is specified using glClearColor();

Set blue colour: glClearColor(0.0,0.0,1.0,1.0);

Notice that the colours are always floating point

Reset the colour buffer: glClear(GL COLOR BUFFER BIT); at the


top of display() function

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 39 / 87


Getting started

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 40 / 87


Inputs

Keyboard input: glutKeyboardFunc(keyboard)

Ex: keyboard(unsigned char key, int x, int y);

Mouse input: glutMouseFunc(mouse)

Ex: mouse(int button, int state, int mousex, int mousey);

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 41 / 87


OpenGL geometry

The fundamental primitive is the point. Points are also called


vertices, a single point is a vertex

Points are used to build more complex geometry

There are many ways to specify points:

2D: glVertex2f(1.0, 2.0);

2D: glVertex2i(4, -1);

3D: glVertex3f(10.5, 20.1, 30,7);

glVertex*() must be within glBegin(constant) and glEnd() commands

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 42 / 87


OpenGL geometry examples

glBegin(GL POINTS);
glVertex2i(0,0);
glVertex2i(1,0) ;
glVertex2i(1,1) ;
glVertex2i(0,1) ;
glEnd() ;

This code draws 4 points

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 43 / 87


OpenGL geometry examples

glBegin(GL LINES);
glVertex2i(0,0);
glVertex2i(1,0) ;
glVertex2i(1,1) ;
glVertex2i(0,1) ;
glEnd() ;

This code draws 2 lines: The first one from (0,0) to (1,0) and the second
one from (1,1) to (0,1)

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 44 / 87


OpenGL geometry examples

glBegin(GL LINE LOOP);


glVertex2i(0,0);
glVertex2i(1,0) ;
glVertex2i(1,1) ;
glVertex2i(0,1) ;
glEnd() ;

This code draws a square

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 45 / 87


OpenGL geometry examples

glBegin(GL QUADS);
glVertex2i(0,0);
glVertex2i(1,0) ;
glVertex2i(1,1) ;
glVertex2i(0,1) ;
glEnd() ;

This code draws a quadrilateral

So how will the display() function look like for drawing a black
quadrilateral?

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 46 / 87


OpenGL geometry

The order that vertices is important

Each glBegin(constant) expects a certain ordering of vertices in order


to produce the expected result

Polygon (e.g. triangle) vertices are usually specified counter


clockwise.

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 47 / 87


OpenGL camera

OpenGL places a camera at the origin in object space pointing in the


negative z direction

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 48 / 87


Orthographic projection

Representing 3D object into 2D

By default orthographic view, points are projected forward along the z


axis onto the plane z=0

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 49 / 87


Viewport
Viewport is a rectangular area of the window in which OpenGL
drawing takes place.

Syntax: glViewport(lowerx, lowery, width, height);

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 50 / 87


Viewport

Values in pixels (window coordinates)

Usually put in glutReshapeFunc();

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 51 / 87


OpenGL pipeline

Transformation: Vertices are rotated & translated into place

Projection: The projection of vertices onto the image plane is


calculated

Clipping: Vertices outside the image plane are clipped away

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 52 / 87


OpenGL: Drawing a rectangle

int main(int & argc, char** argv)


{
glutInit(argc, argv); //initialize toolkit
//set display mode: single bufferring, RGBA model
glutInitDisplayMode (GLUT SINGLE | GLUT RGB );
glutInitWindowSize(500, 500); //set window size on screen
glutInitWindowPosition( 100, 150 ); //set window position on screen
glutCreateWindow(argv[0]); //open screen window
init();
glutDisplayFunc (display); //points to display function
glutMainLoop(); //go into perpetual loop
return 0;
}
Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 53 / 87
OpenGL: Drawing a rectangle

//initialization
void init( void )
{
glClearColor( 1.0, 1.0, 1.0, 0.0 ); //get white background color
glColor3f( 0.0f, 0.0f, 0.0f ); //set drawing color
glPointSize( 6.0 ); //a dot is 4x4
glMatrixMode( GL PROJECTION );
glLoadIdentity(); //replace current matrix with identity matrix
gluOrtho2D( 0.0, 500.0, 0.0, 500.0 );
}

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 54 / 87


OpenGL: Drawing a rectangle

void display( void )


{
glClear( GL COLOR BUFFER BIT ); //clear screen
glColor3f( 1.0, 0.0, 0.0 ); //red
glRecti( 250, 250, 350, 350 );
glFlush(); //send all output to screen
}

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 55 / 87


OpenGL camera metaphor

Imagine that OpenGL has a (virtual) camera which is taking a picture of


the (virtual) world

Must determine the part of the world that the camera will see.

Can’t fit the entire world inside the computer

Can’t fit the entire world on the screen

Must specify how the camera sees the world

Cameras can have different lenses

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 56 / 87


OpenGL camera metaphor

Must specify the position and orientation of the camera

Must also specify position and orientation of objects in the world


that the camera will be “photographing”

OpenGL projection transformation is concerned with:

determining the part of the world that the camera will see

How the camera sees the world

OpenGL modelview transformation is concerned with:

The position and orientation of the camera

The position and orientation of objects in the world

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 57 / 87


OpenGL projection transformation

Defines how a point, or vertex, is transformed from world space to the


2D plane of the screen, or screen space.

World space could be an 5x10 sheet of paper in a 2D drawing program,


or the solar system in a space flight simulator

World space is the two- or three-dimension space which you are


modelling in the computer

World coordinate numbers (vertices) are unitless.

glVertex2i(2,3) purposely does not say whether the 2 is in mm, cm,


inch, m, km, miles, or light years

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 58 / 87


OpenGL projection transformation
OpenGL represents the projection transform as a 4x4 matrix (we’ll see
details later in the course):
 
x11 x12 x13 x14
x
 21 x22 x23 x24 

x31 x32 x33 x34 
 

x41 x42 x43 x44

There is special support for two commonly used transformations


Orthographic

Perspective

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 59 / 87


OpenGL Orthographic transformation

An orthographic viewing volume:

void glOrtho(GLdouble left, GLdouble right,


GLdouble bottom, GLdouble top,
GLdouble zNear, GLdouble zFar ) ;

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 60 / 87


OpenGL Perspective transformation

An perspective viewing volume:

void gluPerspective(GLdouble fovy, GLdouble aspect,


GLdouble zNear, GLdouble zFar ) ;

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 61 / 87


OpenGL general transformation

An general viewing volume:

void glFrustum(GLdouble left, GLdouble right,


GLdouble bottom, GLdouble top
GLdouble zNear, GLdouble zFar ) ;

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 62 / 87


OpenGL projection transformation

What happens when glOrtho() gluPerspective() or glFrustum() is


executed?

A matrix which produces a projection transformation for the requested


viewing volume is generated

That matrix is multiplied into the current OpenGL state

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 63 / 87


OpenGL modelview transformation

Recall that the modelview transformation is concerned with:

The position and orientation of the camera

The position and orientation of objects in the world

Like the projection transformation, the modelview transformation is a 4x4


matrix

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 64 / 87


OpenGL modelview transformation

Why not use two separate transformation matrices, one for the camera (a
view transformation) and one for the objects in the world (a model
transformation) instead of one composite modelview transformation?

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 65 / 87


OpenGL modelview transformation

Try this thought experiment:

Suppose the camera, or eye, and the object the camera is to view are
in the same location, the origin.

In order to view the object either the camera or the object must be
moved

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 66 / 87


OpenGL modelview transformation

Moving the camera away from the origin:

Moving the object away from the origin:

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 67 / 87


OpenGL modelview transformation

No Difference !!!

The programmer can think of the modelview matrix as transforming


the view or the models that are being viewed

Hence the name modelview

OpenGL’s default state is modelview mode

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 68 / 87


gluLookAt()

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 69 / 87


gluLookAt()

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 70 / 87


gluLookAt()

Example 1: void gluLookAt( 0,0,-5, 0,0,0, 0,1,0 ) ;

Camera is at -5 on z-axis

looking at the origin

coincident with the positive y-axis

Example 2: void gluLookAt( 10,0,0, 0,0,0, 0,0,1 ) ;

Camera is at 10 on x-axis

looking at the origin

coincident with the positive z-axis

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 71 / 87


Specifying the view

In order to specify a view to OpenGL:

1 Set the viewport

2 Set the projection transformation

3 Set the modelview transformation

In order to do these tasks:

Set the transformation state for the projection mode and the
modelview mode back to a default ’no transformation’ state.

Change between the projection and modelview states

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 72 / 87


Specifying the view

glMatrixMode(GL PROJECTION) ;

Tells OpenGL we want to set the projection transformation

glMatrixMode(GL MODELVIEW) ;

Tells OpenGL we want to set the modelview transformation

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 73 / 87


Specifying the view

Return to the default state: glLoadIdentity() ;


 
1 0 0 0
0 1 0 0
 
0 0 1 0
 

0 0 0 1

Which sets the transformation to the identity matrix

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 74 / 87


Specifying the view

The default projection is


glMatrixMode(GL PROJECTION) ;
glLoadIdentity() ;

which is equivalent to:


glMatrixMode(GL PROJECTION) ;
glOrtho(-1,1, -1,1, 1,1) ;

A common example:

glMatrixMode(GL PROJECTION) ;
glLoadIdentity() ;
gluPerspective(45,1,5,100) ;

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 75 / 87


Specifying the view

The default modelview is


glMatrixMode(GL MODELVIEW) ;
glLoadIdentity() ;

which is equivalent to:


glMatrixMode(GL MODELVIEW) ;
gluLookAt(0,0,0, 0,0,-1, 0,1,0) ;

A common example:

glMatrixMode(GL MODELVIEW) ;
glLoadIdentity() ;
gluLookAt(0,0,0, 0,0,-1, 0,1,0) ;

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 76 / 87


Specifying the view

Putting everything together in a reshape callback example:

void reshape(int x, int y){


glViewPort(x,y) ;

glMatrixMode(GL PROJECTION) ;
glLoadIdentity() ;
gluPerspective(45,1,5,100) ;

glMatrixMode(GL MODELVIEW) ;
glLoadIdentity() ;
gluLookAt(0,0,10, 0,0,-1, 0,1,0) ;
}

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 77 / 87


Modelview Transformations

Transformations in OpenGL are not drawing commands. They are


retained as part of the graphics state

Transformation calls in OpenGL multiply a matrix which represents


the transformation into the current OpenGL state

This approach could easily get out of hand if there were no way to
save and restore the modelview matrix state. Fortunately, there is.

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 78 / 87


Modelview Transformations

Translation: moves the origin of the coordinate system:


x ′ = x + tx , y ′ = y + ty , z ′ = z + tz
glTranslatef(tx,ty,tz);

Rotation: rotates by angle θ (in degrees) about an axis direction


(x,y,z)
glRotatef(theta,x,y,z);
glRotatef(30.0, 0.0, 1.0, 0.0) rotates by 30 degree about y-axis

Scaling: multiply coordinates by scale factor s


x ′ = sx ∗ x , y ′ = sy ∗ y , z ′ = sz ∗ z
glScalef(sx,sy,sz);

The order of transform is important!

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 79 / 87


Modelview Transformations

glPushMatrix(): saves the modelview state


pushes the current matrix on to a stack, duplicating the current
matrix. That is, after a glPushMatrix call, the matrix on top of the
stack is identical to the one below it

glPopMatrix(): restores the modelview state


pops the current matrix stack, replacing the current matrix with the
one below it on the stack

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 80 / 87


Modelview Transformations
Example:

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 81 / 87


Modelview Transformations

Translation: glTranslatef(GLfloat x, GLfloat y, GLfloat z)


(using gluLookAt(0,0,7, 0,0,0, 0,1,0))

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 82 / 87


Modelview Transformations

Rotation: glRotatef(GLfloat angle, GLfloat x, GLfloat y, GLfloat z)


(using gluLookAt(0,0,7, 0,0,0, 0,1,0))

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 83 / 87


Modelview Transformations

Translation followed by Rotation:


(using gluLookAt(0,0,7, 0,0,0, 0,1,0))

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 84 / 87


Modelview Transformations

Rotation followed by Translation:


(using gluLookAt(0,0,7, 0,0,0, 0,1,0))

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 85 / 87


Modelview Transformations

Translation and Rotation are order dependent!

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 86 / 87


Summary

A graphics system provides a computing environment for visualization


of objects on a 2-D plane.

Two types of display devices: vector and raster display devices.

Three core agenda of computer graphics: object representation,


computation (for object manipulation and display) and modelling (of
physical and dynamic processes).

Flexible environment for defining the coordinate system: Device,


Normalized device and User defined coordinate systems.

Programming involves initialization, defining display zone, setting


coordinate convention, display commands, and closing.

OpenGL graphics programming paradigm briefly introduced.

Jayanta Mukhopadhyay (CSE, IIT Kharagpur)Overview of a graphics system and programming 87 / 87

You might also like