0% found this document useful (0 votes)
71 views56 pages

Lecture 4 - 3D Drawings

The document discusses 3D graphics rendering and transformations. It includes code for rendering a cube in OpenGL using various transformations. Key points: 1) The code applies viewing, modeling, and projection transformations to position and render a cube. 2) The viewing transformation gluLookAt positions the camera 5 units away from the cube, facing it. 3) Additional transformations include a modeling scale and projection settings to render the cube onto the screen.

Uploaded by

frank musa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views56 pages

Lecture 4 - 3D Drawings

The document discusses 3D graphics rendering and transformations. It includes code for rendering a cube in OpenGL using various transformations. Key points: 1) The code applies viewing, modeling, and projection transformations to position and render a cube. 2) The viewing transformation gluLookAt positions the camera 5 units away from the cube, facing it. 3) Additional transformations include a modeling scale and projection settings to render the cube onto the screen.

Uploaded by

frank musa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

Example

#include <GL/glut.h>

const int A = 500;


const float B = 500;
const float C = 200;

void myinit(void)
{
glClearColor(0.7, 0.7, 0.7, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D( -B/2, B/2, -B/2, B/2);
glMatrixMode(GL_MODELVIEW);
}

void display( void )


{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glBegin(GL_POLYGON) ;
glColor3f ( 1.0, 0.3, 0.2);
glVertex2f( -C/2, -C/2 );
glVertex2f( C/2, -C/2 );
glVertex2f( C/2, C/2 );
glVertex2f( -C/2, C/2 );
glEnd();
glFlush();
}

void main(int argc, char** argv)


{
glutInit(&argc,argv);
glutInitWindowSize( A, A );
glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE);
glutCreateWindow("My Rectangle");
glutDisplayFunc(display);
myinit();
glutMainLoop();
}
Properties of a Graphics API (Application
Programmer Interface):

1) A set of primitives for defining objects.


2) Primitives can take on attributes (Color, fill pattern, etc.)
3) A way to manipulate viewing (Camera position,
orientation, etc.)
4) A way to carry out transformations (Translations,
Rotations)
5) Input mechanisms (for user interface)
6) Control system (for communicating with the window
system, initializing programs, etc.)
3 dimensional drawings
• A series of three computer operations convert an object’s three-
dimensional coordinates to pixel positions on the screen:

• Transformations, which are represented by matrix


multiplication, include modeling, viewing, and projection
operations.
• Such operations include rotation, translation, scaling, reflecting
and projection.
Generally, you use a combination of several transformations to
draw a scene.
• Since the scene is rendered on a rectangular window, objects (or
parts of objects) that lie outside the window must be clipped.
• Finally, a correspondence must be established between the
transformed coordinates and screen pixels.
• This is known as a viewport transformation.
Camera analogy
• Viewing and Modeling Transformations
These transformations orient the model and the
camera relative to each other to obtain the desired
final image.
• Projection Transformations describes how to specify
the shape and orientation of the viewing volume.
The viewing volume determines how a scene is
projected onto the screen (with a perspective or
orthographic projection) and which objects or parts of
objects are clipped out of the scene.
• Viewport Transformation explains how to control the
conversion of three-dimensional model coordinates to
screen coordinates.
• These steps correspond to the order in which you
specify the desired transformations in your program.
• The must precede the
in your code, but you
can specify the projection and viewport
transformations at any point before drawing occurs.
Stages of Vertex Transformation

• The diagram shows the order in which these


operations occur on your computer.
• The viewing and modeling transformations you specify are
combined to form the modelview matrix, which is applied
to the incoming object coordinates to yield eye coordinates.
• Next, if you've specified additional clipping planes to
remove certain objects from the scene or to provide
cutaway views of objects, these clipping planes are applied.
• After that, OpenGL applies the projection matrix to yield clip
coordinates.
• This transformation defines a viewing volume; objects
outside this volume are clipped so that they're not drawn in
the final scene.
• After this point, the perspective division is performed by
dividing coordinate values by w, to produce normalized
device coordinates.
• To specify viewing, modeling, and projection
transformations, you construct a 4 × 4 matrix M,
which is then multiplied by the coordinates of each
vertex v in the scene to accomplish the
transformation v'=Mv.
• Vertices always have four coordinates (x, y, z, w),
though in most cases w is 1 and for two-
dimensional data z is 0.
• Finally, the transformed coordinates are converted to window
coordinates by applying the viewport transformation.
• You can manipulate the dimensions of the viewport to cause
the final image to be enlarged, shrunk, or stretched.
• The z values correctly reflect the depth of a given vertex
(measured in distance away from the screen).
• One use for this depth value is to eliminate unnecessary
drawing.
• OpenGL can use this information to determine which surfaces
are obscured by other surfaces and can then avoid drawing
the hidden surfaces.(Hidden Surface Removal)
Geometric Object Rendering
Functions

• Glut includes a series of routines for generating


easily-recognizable 3-d geometric objects.
• These routines are effectively the same ones that
are included in the GLUT library.
• They are included to allow programmers to create
with a single line of code a three-dimensional object
which can be used to test a variety of OpenGL
functionality. (Mesh models / wireframes)
A Simple Example: Drawing a Cube
#include <GL/glut.h>

void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glLoadIdentity (); /* clear the matrix */
/* viewing transformation */
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glScalef (1.0, 2.0, 1.0); /* modeling transformation */
glutWireCube (1.0);
glFlush ();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
glMatrixMode (GL_MODELVIEW);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
• The following transformations are specified:
Projection transformation - glFrustum()
Viewing transformation - gluLookAt(),
Modeling transformation - glScalef()
Viewport transformation - glViewport()
1. Viewing transformation
• The viewing transformation, gluLookAt(), positions and aims the
camera towards where the cube is drawn.
• The viewing transformation is analogous to positioning and aiming
a camera.
• In the example, before the viewing transformation can be specified,
the current matrix is set to the identity matrix with
glLoadIdentity().
• This step is necessary since most of the transformation commands
multiply the current matrix by the specified matrix and then set
the result to be the current matrix.
• If you don't clear the current matrix by loading it with the identity
matrix, you continue to combine previous transformation matrices
with the new one you supply.
• In some cases, you do want to perform such combinations, but
you also need to clear the matrix sometimes.
#include <GL/glut.h> void reshape (int w, int h)
void init(void) {
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
{
glMatrixMode (GL_PROJECTION);
glClearColor (0.0, 0.0, 0.0, 0.0);
glLoadIdentity ();
glShadeModel (GL_FLAT); glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
} glMatrixMode (GL_MODELVIEW);
void display(void) }
{ int main(int argc, char** argv)
glClear (GL_COLOR_BUFFER_BIT); {
glColor3f (1.0, 1.0, 1.0); glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE |
glLoadIdentity (); /* clear the matrix */
GLUT_RGB);
/* viewing transformation */ glutInitWindowSize (500, 500);
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, glutInitWindowPosition (100, 100);
1.0, 0.0); glutCreateWindow (argv[0]);
glScalef (1.0, 2.0, 1.0); /* modeling init ();
transformation */ glutDisplayFunc(display);
glutWireCube (1.0); glutReshapeFunc(reshape);
glFlush (); glutMainLoop();
} return 0;
}
gluLookAt()
• After the matrix is initialized, the viewing transformation is
specified with gluLookAt().
• The arguments for this command indicate where the camera
(or eye position) is placed, where it is aimed, and which way
is up.
• The arguments used here place the camera at (0, 0, 5), aim
the camera lens towards (0, 0, 0), and specify the up-vector
as (0, 1, 0).
• The up-vector defines a unique orientation for the camera.
• If gluLookAt() was not called, the camera has a default
position and orientation.
• By default, the camera is situated at the origin, points down
the negative z-axis, and has an up-vector of (0, 1, 0).
• The overall effect is that gluLookAt() moves the
camera 5 units along the z-axis.
Syntax of command:

gluLookAt(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX,


upY, upZ)

eyeX, eyeY, eyeZ


Specifies the position of the eye point.

centerX, centerY, centerZ


Specifies the position of the reference point.

upX, upY, upZ


Specifies the direction of the up vector.
Point p in world is:
y
p (MODELVIEW * p)
Camera up = y’
in camera coord-sys!!!
z’

eye center = (0,0,0)


x’ x

z World
• Your image has an 'up' to it that can be separate from the world's up.
• The blue window in this image can be thought of as the 'near-plane' that
your imagery is drawn on: your monitor, if you will.
• If all you supply is the eye-point and the at-point, that window is free to spin
around. You need to give an extra 'up' direction to pin it down.
• OpenGL will normalize the vector that you supply if it isn't unit length.
• OpenGL will also project it down so that it forms a 90 degree angle with the
'z' vector defined by eye and at(unless you give an 'up' vector that is
in exactly the same direction as the line from 'eye' to 'at').
• Once 'in' (z) and 'up' (y) directions are defined, it's easy
to calculate the 'right' or (x) direction from those two.
• In this figure, the 'supplied' up vector is (0,1,0) if the
blue axis is in the y direction.
• If you were to give (1,1,1), it would most likely rotate
the image by 45 degrees because that's saying that the
top of the blue window should be pointed toward that
direction.
• Consequently the image of the guy would appear to be
tipped (in the opposite direction).
Camera analogy

Task:
• Manipulate the values of the gluLookAt
function’s arguments, to view the object
from different positions:

e.g
gluLookAt (1.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0,
0.0);

gluLookAt (0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0,


0.0);
2. The Modeling Transformation
• You use the modeling transformation to position and orient the model.
• For example, you can rotate, translate, or scale the model - or perform some
combination of these operations.
• In this example, glScalef() is the modeling transformation that is used.
• The arguments for this command specify how scaling should occur along the three
axes.
• If all the arguments are 1.0, this command has no effect.
• The cube is drawn twice as large in the y direction.
• Thus, if one corner of the cube had originally been at (3.0, 3.0, 3.0), that corner
would wind up being drawn at (3.0, 6.0, 3.0).
• The effect of this modeling transformation is to transform the cube so that it isn't a
cube but a rectangular box.

Task:
• Replace the gluLookAt() call with the modeling transformation glTranslatef() with
parameters (0.0, 0.0, -5.0).

• Why are the effects similar?


Task 2

Add the following call after the glScalef() command:


glTranslatef(2.0, 0.0, 0.0);
• Note that instead of moving the camera (with a viewing transformation) so
that the cube could be viewed, you could have moved the cube away from
the camera (with a modeling transformation).
• This duality in the nature of viewing and modeling transformations is why
you need to think about the effect of both types of transformations
simultaneously.
• This is why modeling and viewing transformations are combined into the
modelview matrix before the transformations are applied.
• Also note that the modeling and viewing transformations are included in
the display() routine, along with the call that's used to draw the cube,
glutWireCube().
• This way, display() can be used repeatedly to draw the contents of the
window if, for example, the window is moved or uncovered, and you've
ensured that each time, the cube is drawn in the desired way, with the
appropriate transformations.
• The potential repeated use of display() underscores the need to load the
i d e n t i t y m a t r i x b e fo r e p e r fo r m i n g t h e v i e w i n g a n d m o d e l i n g
transformations, especially when other transformations might be
performed between calls to display().
Modeling transformations(Summary)

• glScalef(2.0f, 2.0f, 2.0f); // twice as big parameters: sx, sy, sz


• glTranslatef(2.0f, 3.5f, 1.8f); // move object parameters: tx, ty, tz
• glRotatef(30.0f, 0.0f, 0.0f, 1.0f); // 30 degrees about z-axis
3. The Projection Transformation
• Specifying the projection transformation is like
choosing a lens for a camera.
• You can think of this transformation as determining
what the field of view or viewing volume is and
therefore what objects are inside it and to some extent
how they look.
• This is equivalent to choosing among wide-angle,
normal, and telephoto lenses, for example.
• With a wide-angle lens, you can include a wider scene
in the final photograph than with a telephoto lens, but
a telephoto lens allows you to photograph objects as
though they're closer to you than they actually are.
• In addition to the field-of-view considerations, the
projection transformation determines how objects are
projected onto the screen, as its name suggests.
• Two basic types of projections are provided for you by
OpenGL, along with several corresponding commands for
describing the relevant parameters in different ways.
• One type is the perspective projection, which matches how
you see things in daily life.
• Perspective makes objects that are farther away appear
smaller; for example, it makes railroad tracks appear to
converge in the distance.
• If you're trying to make realistic pictures, you'll want to
choose perspective projection, which is specified with the
glFrustum() command in this code example.
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glLoadIdentity (); /* clear the matrix */
/* viewing transformation */
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glScalef (1.0, 2.0, 1.0); /* modeling transformation */
glutWireCube (1.0);
glFlush ();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
glMatrixMode (GL_MODELVIEW);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
• The other type of projection is orthographic, which maps
objects directly onto the screen without affecting their
relative size.
• Orthographic projection is used in architectural and
computer-aided design applications where the final image
needs to reflect the measurements of objects rather than
how they might look.
• Architects create perspective drawings to show how
particular buildings or interior spaces look when viewed
from various vantage points; the need for orthographic
projection arises when blueprint plans or elevations are
generated, which are used in the construction of buildings.
• Before glFrustum() can be called to set the projection
transformation, some preparation needs to happen.
• As shown in the reshape() routine, the command called
glMatrixMode() is used first, with the argument
GL_PROJECTION.
• This indicates that the current matrix specifies the projection
transformation; the following transformation calls then affect
the projection matrix.
• As you can see, a few lines later glMatrixMode() is called
again, this time with GL_MODELVIEW as the argument.
• This indicates that succeeding transformations now affect the
modelview matrix instead of the projection matrix.
• Note that glLoadIdentity() is used to initialize the current
projection matrix so that only the specified projection
transformation has an effect.
• Now glFrustum() can be called, with arguments that define
the parameters of the projection transformation.
• In this example, both the projection transformation and the
viewport transformation are contained in the reshape()
routine, which is called when the window is first created
and whenever the window is moved or reshaped.
• This makes sense, since both projecting (the width to height
aspect ratio of the projection viewing volume) and applying
the viewport relate directly to the screen, and specifically to
the size or aspect ratio of the window on the screen.
Command syntax:

void glFrustum(GLdouble left, GLdouble right, GLdouble bottom,


GLdouble top, GLdouble near, GLdouble far);

• The frustum's viewing volume is defined by the parameters: (left,


bottom, -near) and (right, top, -near) specify the (x, y, z)
coordinates of the lower-left and upper-right corners of the near
clipping plane;
• near and far give the distances from the viewpoint to the near
and far clipping planes.
• They should always be positive.
Task
• Change the glFrustum() call in the Example to the
more commonly used Utility Library routine
gluPerspective() with parameters (60.0, 1.0, 1.5,
20.0).
• Then experiment with different values, especially
for fovy and aspect.
gluPerspective

• Alternatively, you might try the routine gluPerspective().


• This routine creates a viewing volume of the same shape as
glFrustum() does, but you specify it in a different way.
• Rather than specifying corners of the near clipping plane, you
specify the angle of the field of view in the y direction and
the aspect ratio of the width to height (x/y).
• For a square portion of the screen, the aspect ratio is 1.0.
• You also specify the distance between the viewpoint and the
near and far clipping planes, thereby truncating the pyramid.
• Note that gluPerspective() is limited to creating frustums that
are symmetric in both the x- and y-axes along the line of sight,
but this is usually what you want.
#include <GL/glut.h>
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glLoadIdentity (); /* clear the matrix */
/* viewing transformation */
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glScalef (1.0, 2.0, 1.0); /* modeling transformation */
glutWireCube (1.0);
glFlush ();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
glMatrixMode (GL_MODELVIEW);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
Command syntax:

void gluPerspective(GLdouble fovy, GLdouble aspect,


GLdouble near, GLdouble far);

• fovy is the angle of the field of view in the x-z plane; its value must be in the
range [0.0,180.0].
• Aspect is the aspect ratio of the frustum, its width divided by its height.
• Near and far values the distances between the viewpoint and the clipping
planes, along the negative z-axis.
• They should always be positive.
Orthographic Projection
• With an orthographic projection, the viewing volume is
a rectangular parallelepiped, or more informally, a box.
• Unlike perspective projection, the size of the viewing
volume doesn't change from one end to the other, so
distance from the camera doesn't affect how large an
object appears.
• This type of projection is used for applications such as
creating architectural blueprints and computer-aided
design, where it's crucial to maintain the actual sizes of
objects and angles between them as they're projected.
Orthographic Projection

• The command glOrtho() creates an orthographic parallel viewing volume.


• As with glFrustum(), you specify the corners of the near clipping plane and the distance to
the far clipping plane.

void glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top,


GLdouble near, GLdouble far);

• (left, bottom, -near) and (right, top, -near) are points on the near clipping plane that are
mapped to the lower-left and upper-right corners of the viewport window, respectively.
• (left, bottom, -far) and (right, top, -far) are points on the far clipping plane that are mapped
to the same respective corners of the viewport.
• Both near and far can be positive or negative.
• With no other transformations, the direction of projection is parallel to
the z-axis, and the viewpoint faces toward the negative z-axis.
• Note that this means that the values passed in for far and near are used as
negative z values if these planes are in front of the viewpoint, and positive if
they're behind the viewpoint.
• For the special case of projecting a two-dimensional image onto a two-
dimensional screen, use the Utility Library routine gluOrtho2D().
• This routine is identical to the three-dimensional version, glOrtho(), except
that all the z coordinates for objects in the scene are assumed to lie
between -1.0 and 1.0.
• If you're drawing two-dimensional objects using the two-dimensional vertex
commands, all the z coordinates are zero; thus, none of the objects are
clipped because of their z values.

– void gluOrtho2D(GLdouble left, GLdouble right, GLdouble bottom,


GLdouble top);

• The clipping region is a rectangle with the lower-left corner at (left, bottom)
and the upper-right corner at (right, top).
#include <GL/glut.h>
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glLoadIdentity (); /* clear the matrix */
/* viewing transformation */
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glScalef (1.0, 2.0, 1.0); /* modeling transformation */
glutWireCube (1.0);
glFlush ();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
glMatrixMode (GL_MODELVIEW);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
• As shown in the reshape() routine in the example,
the command called glMatrixMode() is used first,
with the argument GL_PROJECTION.
• This indicates that the current matrix specifies the
projection transformation;
• A few lines later glMatrixMode() is called again, this
time with GL_MODELVIEW as the argument.
• This indicates that succeeding transformations now
affect the modelview matrix instead of the
projection matrix.
4. The Viewport Transformation
• Together, the projection transformation and the viewport
transformation determine how a scene gets mapped onto the
computer screen.
• The projection transformation specifies the mechanics of how
the mapping should occur, and the viewport indicates the
shape of the available screen area into which the scene is
mapped.
• Since the viewport specifies the region the image occupies on
the computer screen, you can think of the viewport
transformation as defining the size and location of the final
processed photograph - for example, whether the
photograph should be enlarged or shrunk.
• The arguments to glViewport() describe the origin of
the available screen space within the window - (0,
• 0) in this example - and the width and height of the
available screen area, all measured in pixels on the
screen.
• This is why this command needs to be called within
reshape() - if the window changes size, the viewport
needs to change accordingly.
• Note that the width and height are specified using the
actual width and height of the window; often, you want
to specify the viewport this way rather than giving an
absolute Size.
Viewport Transformation
Screen Image

Viewport

47
Window vs. Viewport
• Window
– World-coordinate area selected for display
– What is to be viewed
• Viewport
– Area on the display device to which a window is
mapped
– Where it is to be displayed

48
Viewport Transformation
• Window-to-Viewport Mapping
Window Viewport
wy2 vy2

(wx, wy) (vx, vy)


wy1 vy1
wx1 wx2 vx1 vx2
Screen Coordinates Image Coordinates

49
Reshape Function

Takes 2 parameters, which define the width and the height


of the display window.
void glViewport(GLint x, GLint y, GLsizei width, GLsizei
height)

PARAMETERS
•x, y: Specify the lower left corner of the viewport rectangle,
in pixels. The default is (0, 0).
•width, height: Specify the width and height, respectively, of
the viewport.
Manipulating the viewport
void display() {
#include <GL/glut.h> glClear(GL_COLOR_BUFFER_BIT);
glViewport((int) (0.0*deviceWindowWidth), (int) (0.0*deviceWindowHeight),
(int) (0.5*deviceWindowWidth), (int) (1.0*deviceWindowHeight));
int deviceWindowWidth; lineLoop();
int deviceWindowHeight; glViewport((int) (0.5*deviceWindowWidth), (int) (0.0*deviceWindowHeight),
(int) (0.5*deviceWindowWidth), (int) (1.0*deviceWindowHeight));
lineLoop()
void init() { glFlush();
glMatrixMode(GL_PROJECTION); }
glLoadIdentity();
void reshape(int width, int height)
gluOrtho2D(0.0, 1.0, 0.0, 1.0); {
deviceWindowWidth = width;
glClearColor(0.0, 0.0, 0.6, 1.0); deviceWindowHeight = height;
}
glColor3f(1.0, 1.0, 0.0);
glLineWidth(5.0); int main(int argc, char **argv)
} {
glutInit(&argc, argv);
glutInitWindowPosition(0, 0);
void lineLoop() { glutInitWindowSize(500, 500);
glBegin(GL_LINE_LOOP); glutCreateWindow("Hello (Viewports) World");
glVertex2d(0.05, 0.50); glutDisplayFunc(display);
glVertex2d(0.50, 0.95); glutReshapeFunc(reshape);
glVertex2d(0.95, 0.50); init();
glutMainLoop();
glVertex2d(0.50, 0.05); return 0;
glEnd(); }
}
Drawing the Scene
Output
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glLoadIdentity (); /* clear the matrix */
/* viewing transformation */
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glScalef (1.0, 2.0, 1.0); /* modeling transformation */
glutWireCube (1.0);
glFlush ();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
glMatrixMode (GL_MODELVIEW);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
Exercise
• Alter the code in the example above, so that the
cube may appear as an orthographic projection
as shown in the diagram below:

You might also like