Professional Documents
Culture Documents
Computer Graphics and Visualization 18CS62: Name: Karthik J USN: 1AY18CS047
Computer Graphics and Visualization 18CS62: Name: Karthik J USN: 1AY18CS047
18CS62
NAME : KARTHIK J
USN : 1AY18CS047
Given a set of lines and a rectangular area of interest, the task is to remove lines
which are outside the area of interest and clip the lines which are partially inside the
area.
Rectangular area of interest (coordinates of bottom left and top right) x_min =
4, y_min = 3, x_max =9, y_max = 8
A set of lines (Defined by two corner coordinates) Line 1 :
x1 = 5, y1 = 5, x2 = 7, y2 = 7
Line 2 : x1 = 7, y1 = 9, x2 = 11, y2 = 4
Line 3 : x1 = 1, y1 = 5, x2 = 4, y2 = 1
For Line1 : x1 = 5, y1 = 5, x2 = 7, y2 = 7
i.e. P(x,y) = P(5,5) and Q(x,y) = Q(7,7)
Region Code : P – 0000 and Q – 0000
Both points inside the window.
Y=8
y=8yyyy
Q(7,7)
P(5,5)
Y=3
X=4
X=9
P (7,9)
Y=8
Q(11,4)
Y=3
X=4 X=9
For points within region 0000 let P1 and P2. At point P1 : y =
y_max = 8
m = (y2-y1)/(x2-x1) = (9-4)/(7-11) = -5/4
1/m = -4/5
x = x1 + (1/m)(y_max – y) = 7- (4/5) (8 – 9) = 7+(4/5) = 8 So,
P1(8,8)
. At point P2 :
x = x_max = 9
m = (y2-y1)/(x2-x1) = (9-4)/(7-11) = -5/4
y = y1 + m (x_max – x) = 9 + (-5/4) (9 – 7) = 7 So,
P2(9,7)
P1(8,8) Y=8
P2(9,7)
Y=3
X=4 X=9
For Line3 : x1 = 1, y1 = 5, x2 = 4, y2 = 1
i.e. P(x,y) = P(1,5) and Q(x,y) = Q(4,1)
Region Code : P – 0001 and Q – 0101
Both points outside the window.
Y=8
P(1,5)
Y=3
Q(4,1)
X=4 X=9
glMatrixMode (GL_MODELVIEW);
Viewing parameters are specified with the following GLU function, which is in the
OpenGL Utility library because it invokes the translation and rotation routines in the
basic OpenGL library.
Syntax:
gluLookAt (x0, y0, z0, xref, yref, zref, Vx, Vy, Vz);
The default clipping window is thus a symmetric normalized square, and the default
view volume is a symmetric normalized cube with znear = 1.0 (behind the viewing
position) and zfar = −1.0.
c. OpenGL Symmetric Perspective-Projection
Function There are two functions available for producing a perspective-projection view
of a scene. One of these functions generates a symmetric frustum view volume about the
viewing direction (the negative z view axis). The other function can be used for either a
symmetric-perspective projection or an oblique- perspective projection. For both
functions, the projection reference point is the viewing-coordinate origin and the near
clipping plane is the view plane. A symmetric, perspective-projection, frustum view
volume is set up with the GLU function
Syntax:
gluPerspective (theta, aspect, dnear, dfar);
with each of the four parameters assigned a double-precision, floating-point number. The
first two parameters define the size and position of the clipping window on the near plane,
and the second two parameters specify the distances from the view point (coordinate
origin) to the near and far clipping planes.
Parameter theta represents the field-of-view angle, which is the angle between the top
and bottom clipping planes . This angle can be assigned any value from 0◦ to 180◦.
Parameter aspect is assigned a value for the aspect ratio (width/height) of the clipping
window.
For a perspective projection in OpenGL, the near and far clipping planes must always be
somewhere along the negative zview axis; neither can be “behind” the viewing position.
Example: void perspective()
{
glClearColor( 0, 0, 0, 0 );
glMatrixMode( GL_PROJECTION );
glLoadIdentity(); gluPerspective(45,
1.333, 0, 100); glMatrixMode(
GL_MODELVIEW );
glLoadIdentity();
glClear( GL_COLOR_BUFFER_BIT );
}
This restriction does not apply to an orthogonal projection, but it precludes the inverted
perspective projection of an object when the view plane is behind the view point.
Therefore, both dnear and dfar must be assigned positive numerical values, and the
positions of the near and far planes are calculated as znear =
−dnear and zfar = −dfar. The frustum view volume set up by the gluPerspective function
is symmetric about the negative zview axis. And the description of a scene is converted to
normalized, homogeneous projection coordinates with matrix.
d.OpenGL General Perspective-Projection Function
We can use the following function to specify a perspective projection that has either a
symmetric frustum view volume or an oblique frustum view volume.
Syntax:
glFrustum (xwmin, xwmax, ywmin, ywmax, dnear, dfar);
All parameters in this function are assigned double-precision, floating-point numbers. As in
the other viewing-projection functions, the near plane is the view plane and the projection
reference point is at the viewing position (coordinate origin). This function has the same
parameters as the orthogonal, parallel-projection function, but now the near and far clipping-
plane distances must be positive.
Example : void myReshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h) glFrustum(-2.0, 2.0, -2.0 * (GLfloat) h/ (GLfloat) w,2.0* (GLfloat) h /
(GLfloat) w, 2.0, 20.0);
else glFrustum(-2.0, 2.0, -2.0 * (GLfloat) w/ (GLfloat) h,2.0* (GLfloat) w /
(GLfloat) h, 2.0, 20.0);glMatrixMode(GL_MODELVIEW);
}
The first four parameters set the coordinates for the clipping window on the near plane, and
the last two parameters specify the distances from the coordinate origin to the near and far
clipping planes along the negative zview axis. Locations for the near and far planes are
calculated as znear = −dnear and zfar = −dfar. The clipping window can be specified
anywhere on the near plane. If we select the clipping window coordinates so that xwmin =
−xwmax and ywmin = −ywmax, we obtain a symmetric frustum (about the negative zview
axis as its centerline).
d. OpenGL Clipping Planes
In addition to the six clipping planes enclosing the view volume, OpenGL provides for the
specification of additional clipping planes in a scene. Unlike the view-volume clipping planes,
which are each perpendicular to one of the coordinate axes, these additional planes can have
any orientation. We designate an optional clipping plane and activate clipping against that
plane with the statements:
Syntax:
glClipPlane (id, planeParameters);
glEnable (id);
Parameter id is used as an identifier for a clipping plane. This parameter is assigned one of the
values GL CLIP PLANE0, GL CLIP PLANE1, and so forth, up to a facility- defined
maximum. The plane is then defined using the four-element array planeParameters, whose
elements are the double-precision, floatingpoint values for the four plane-equation parameters
A, B, C, and D. An activated clipping plane that has been assigned the identifier id is turned
off with
glDisable (id);
The plane parameters A, B, C, and D are transformed to viewing coordinates and used to test
viewing-coordinate positions in a scene. Subsequent changes in viewing or geometric-
transformation parameters do not affect the stored plane parameters.
Therefore, if we set up optional clipping planes before specifying any geometric or viewing
transformations, the stored plane parameters are the same as the input parameters. Also,
because the clipping routines for these planes are applied in viewing coordinates, and not in
the normalized coordinate space, the performance of a program can be degraded when
optional clipping planes are activated.
Example : void display(void){
GLdouble eq[4]=(0.0,1.0,0.0,1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glColor3f(1, 1, 1);
glPushMatrix(); glTranslatef
(0.0,0.0,-0.5);
glClipPlane(GL_CLIP_PLANE0,eq);
glEnable(GL_CLIP_PLANE0);
glPopMatrix();
glFlush ();
Six optional clipping planes are available in any OpenGL implementation, but more might
be provided. We can find out how many optional clipping planes are possible for a particular
OpenGL implementation with the inquiry
glGetIntegerv (GL_MAX_CLIP_PLANES, numPlanes);
Parameter numPlanes is the name of an integer array that is to be assigned an integer value
equal to the number of optional clipping planes that we can use.
f.OpenGL Viewports and Display Windows
After the clipping routines have been applied in normalized coordinates, the contents of the
normalized clipping window, along with the depth information, are transferred to three-
dimensional screen coordinates. The color value for each xy position on the viewport is
stored in the refresh buffer (color buffer), and the depth information for each xy position is
stored in the depth buffer. A rectangular viewport is defined with the following functions:
Syntax:
glViewport (xvmin, yvmin, vpWidth, vpHeight);
The first two parameters in this function specify the integer screen position of the lower-left
corner of the viewport relative to the lower-left corner of the display window. And the last
two parameters give the integer width and height of the viewport. To maintain the
proportions of objects in a scene, we set the aspect ratio of the viewport equal to the aspect
ratio of the clipping window. Display windows are created and managed with GLUT
routines. The default viewport in OpenGL is the size and position of the current display
window.