Edited cg-1-2
Edited cg-1-2
Experiments No.1
Theory:
Polygon:
A polygon is a closed planar path composed of a finite number of sequential line
segments. A polygon is a two-dimensional shape formed with more than three
straight lines. When starting point and terminal point is same then it is called
polygon.
Types of Polygons
1. Concave
2. Convex
3. Complex
A convex polygon is a simple polygon whose interior is a convex set. In a convex
polygon, all interior angles are less than 180 degrees.
The following properties of a simple polygon are all equivalent to convexity:
Every line segment between two vertices remains inside or on the boundary of the
polygon.
Convex Polygons: In a convex polygon, any line segment joining any two inside points lies inside
the polygon. A straight line drawn through a convex polygon cross at most two sides. A concave
polygon will always have an interior angle greater than 180 degrees. It is possible to cut a concave
polygon into a set of convex polygons. You can draw at least one straight line through a concave
polygon that crosses more than two sides.
Complex polygon is a polygon whose sides cross over each other one or more times.
3. When one polygon flag is on, and this is for surface S1enter color intensity as I1into refresh buffer
4. When two or image surface flag are on, sort the surfaces according to depth and use intensity value
Sn for the nth surface. This surface will have least z depth value
Experiments No.2
Theory:
Cohen Sutherland Algorithm is a line clipping algorithm that cuts lines to portions which are within a
rectangular area. It eliminates the lines from a given set of lines and rectangle area of interest (view
port) which belongs outside the area of interest and clips those lines which are partially inside the area
of interest. Example:
Algorithm: -
The algorithm divides a two-dimensional space into 9 regions (eight outside regions and one inside
region) and then efficiently determines the lines and portions of lines that are visible in the central
region of interest (the viewport). Following image illustrates the 9 regions:
As you seen each region is denoted by a 4 bit code like 0101 for the bottom right region Four Bit code
is calculated by comparing extreme end point of given line (x,y) by four co-ordinates x_min, x_max,
y_max, y_min which are the coordinates of the area of interest (0000) Calculate the four bit code as
follows:
The more efficient Cohen Sutherland Algorithm performs initial tests on a line to determine whether
intersection calculations can be avoided.
Step 1: Assign a region code for two endpoints of given line
Step 2: If both endpoints have a region code 0000 then given line is completely inside and we will keep
this line.
Step 3: If step 2 fails, perform the logical AND operation for both region codes.
Step 3.1: If the result is not 0000, then given line is completely outside.
Step 3.2: Else line is partially inside.
Step 3.2.a: Choose an endpoint of the line that is outside the given rectangle.
Step 3.2.b: Find the intersection point of the rectangular boundary (based on region code) Step
3.2.c: Replace endpoint with the intersection point and upgrade the region code.
Step 3.2.d: Repeat step 2 until we find a clipped line either trivially accepted or rejected.
Step 4: Repeat step 1 for all lines.
Experiments No.3
Theory:
DDA Line Drawing Algorithm:
Line is a basic element in graphics. To draw a line, you need two end points between which you can
draw a line. Digital Differential Analyzer (DDA) line drawing algorithm is the simplest line drawing
algorithm in computer graphics. It works on incremental method. It plots the points from starting point
of line to end point of line by incrementing in X and Y direction in each iteration.
DDA line drawing algorithm works as follows:
Step 1: Get coordinates of both the end points (X1, Y1) and (X2, Y2) from user.
Step 2: Calculate the difference between two end points in X and Y direction.
dx = X2 X1; dy = Y2 Y1;
Step 3: Based on the calculated difference in step 2, you need to identify the number of steps to put
pixel. If dx > dy, then you need more steps in x coordinate; otherwise in y coordinate.
if (absolute(dx) >
absolute(dy)) Steps =
absolute(dx); else
Steps = absolute(dy);
Step 4: Calculate the increment in x coordinate and y coordinate.
Xincrement = dx / (float) steps;
Yincrement = dy / (float) steps;
Step 5: Plot the pixels by successfully incrementing x and y coordinates accordingly and complete the
drawing of the line.
for(int i=0; i < Steps; i++)
{
X1 = X1 + Xincrement;
Y1 = Y1 + Yincrement;
putpixel(Round(X1), Round(Y1), ColorName);
}
This pattern is made up of one equilateral triangle and two concentric circles. To draw the triangle, we
require coordinates of 3 vertices forming an equilateral triangle. To draw two concentric circles, we
require coordinates of common center and radius of both the circles.
We will take coordinates of circle and radius of one of the circle from user. Then using the properties
of an equilateral triangle, we can find all 3 vertices of equilateral triangle and radius of other circle.
Once we get all these parameters, we can call DDA line drawing and Bresenham’s circle drawing
algorithms by passing appropriate parameters to get required pattern.
OR
To draw this pattern, we require two rectangles and one circle. We can use suitable geometry to get
coordinates to draw rectangles and circle. Then we can call DDA line drawing and Bresenham’s circle
drawing algorithms by passing appropriate parameters to get required pattern.
Experiments No.4
Theory:
Transformation means changing some graphics into something else by applying rules. We can have
various types of transformations such as translation, scaling up or down, rotation, shearing, reflection
etc. When a transformation takes place on a 2D plane, it is called 2D transformation. Transformations
play an important role in computer graphics to reposition the graphics on the screen and change their
size or orientation. Translation, Scaling and Rotation are basic transformations.
1) Translation:
A translation moves an object to a different position on the screen. You can translate a point in 2D by
adding translation coordinate or translation vector (Tx, Ty) to the original coordinates. Consider -
Initial coordinates of the object O = (Xold, Yold) - New coordinates of the object
O after translation = (Xnew, Ynew) - Translation vector or Shift vector = (Tx, Ty)
This translation is achieved by adding the translation coordinates to the old coordinates of the object
as-
Xnew = Xold + Tx (This denotes translation towards X axis)
Ynew = Yold + Ty (This denotes translation towards Y axis)
In Matrix form, the above translation equations may be represented as-
2) Rotation:
In rotation, we rotate the object at angle θ (theta) from its original position. Consider
- Initial coordinates of the object O = (Xold, Yold)
- Initial angle of the object O with respect to origin = Φ - Rotation angle = θ
- New coordinates of the object O after rotation = (Xnew, Ynew)
This anti-clockwise rotation is achieved by using the following rotation equations- Xnew
= Xold x cosθ – Yold x sinθ
Ynew = Xold x sinθ + Yold x cosθ
In Matrix form, the above rotation equations may be represented as-
3) Scaling:
Scaling transformation is used to change the size of an object. In the scaling process, you either expand
or compress the dimensions of the object. Scaling can be achieved by multiplying the original
coordinates of the object with the scaling factor (Sx, Sy). If scaling factor > 1, then the object size is
increased. If scaling factor < 1, then the object size is reduced. Consider - Initial coordinates of the
object O = (Xold, Yold)
- Scaling factor for X-axis = Sx - Scaling factor for Y-axis = Sy
- New coordinates of the object O after scaling = (Xnew, Ynew)
Step 2: In Iteration 1, line is divided into 3 equal parts. Middle part of a line is rotated in 60 0, because it
forms a perfect an equilateral triangle as shown below:
The function presented below (in the "C" language) computes the Hilbert curve. Note that the
curve is symmetrical around the vertical axis. It would therefore be sufficient to draw half of the Hilbert
curve.
Snowflake curve:
Snowflake curve is drawn using koch curve iterations. In koch curve, we just have a single line
in the starting iteration and in snowflake curve, we have an equilateral triangle. Draw an equilateral
triangle and repeat the steps of Koch curve generation for all three segments of an equilateral triangle.
Experiments No.5
Theory:
Koch Curve:
The Koch curve fractal was first introduced in 1904 by Helge von Koch. It was one of the first fractal objects
to be described. To create a Koch curve 1. Create a line and divide it into three parts.
2. The second part is now rotated by 60°.
3. Add another part which goes from the end of part 2 to the beginning of part 3
4. Repeat step 1 to step 3 with each part of the line.
We will get following Koch curve as number of iterations goes on increasing
Experiments No.6
Theory:
OpenGL Basics:
Open Graphics Library (OpenGL) is a cross-language (language independent), cross-platform
(platform independent) API for rendering 2D and 3D Vector Graphics (use of polygons to represent
image). OpenGL is a low-level, widely supported modeling and rendering software package, available
across all platforms. It can be used in a range of graphics applications, such as games, CAD design, or
modeling. OpenGL API is designed mostly in hardware.
Design: This API is defined as a set of functions which may be called by the client program.
Although functions are like those of C language but it is language independent.
Development: It is an evolving API and Khronos Group regularly releases its new version having
some extended feature compare to previous one. GPU vendors may also provide some
additional functionality in the form of extension.
Associated Libraries: The earliest version is released with a companion library called OpenGL
utility library. But since OpenGL is quite a complex process. So in order to make it easier other
library such as OpenGL Utility Toolkit is added which is later superseded by freeglut. Later
included library were GLEE, GLEW and glbinding.
• where filename.c is the name of the file with which this program is saved.
Prerequisites for OpenGL Since OpenGL is a graphics API and not a platform of its own, it requires a
language to operate in and the language of choice is C++.
Getting started with OpenGL Overview of an OpenGL program
Main
Open window and configure frame buffer (using GLUT for example)
Loop
Check for events if window event (resize, unhide, maximize etc.) modify the viewport and
redraw else if input event (keyboard and mouse etc.) handle the event (such as move the camera or
change the state) and usually draw the scene
Redraw
Clear the screen (and buffers e.g., z-buffer) o Change states (if desired) o Render
Use OpenGL to
OpenGL Syntax
All functions have the form: gl* o glVertex3f() – 3 means that this function take three arguments,
and f means that the type of those arguments is float. o glVertex2i() – 2 means that this function take two
arguments, and i means that the type of those arguments is integer
All variable types have the form: GL* o In OpenGL program it is better to use OpenGL variable
types (portability)
GLUT, the OpenGL Utility Toolkit, provides a system for setting up callbacks for interacting with the
user and functions for dealing with the windowing system. This abstraction allows a program to run
on different operating systems with only a recompile. Glut follows the convention of prepending
function names with glut and constants with GLUT.
Writing an OpenGL Program with GLUT
An OpenGL program using the three libraries listed above must include the appropriate headers.
This requires the following three lines:
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
Before OpenGL rendering calls can be made, some initialization has to be done. With GLUT, this
consists of initializing the GLUT library, initializing the display mode, creating the window, and setting
up callback functions. The following lines initialize a full color, double buffered display:
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
Double buffering means that there are two buffers, a front buffer and a back buffer. The front buffer
is displayed to the user, while the back buffer is used for rendering operations. This prevents flickering
that would occur if we rendered directly to the front buffer.
Next, a window is created with GLUT that will contain the viewport which displays the OpenGL front
buffer with the following three lines:
glutInitWindowPosition(px, py);
glutInitWindowSize(sx, sy);
glutCreateWindow(name);
To register callback functions, we simply pass the name of the function that handles the event to the
appropriate GLUT function. glutReshapeFunc(reshape); glutDisplayFunc(display);
Here, the functions should have the following prototypes:
void reshape(int width, int height); void display();
In this example, when the user resizes the window, reshape is called by GLUT, and when the display
needs to be refreshed, the display function is called. For animation, an idle event handler that takes
no arguments can be created to call the display function to constantly redraw the scene with
glutIdleFunc.
Once all the callbacks have been set up, a call to glutMainLoop allows the program to run.
In the display function, typically the image buffer is cleared, primitives are rendered to it, and the
results are presented to the user. The following line clears the image buffer, setting each pixel color to
the clear color, which can be configured to be any color:
glClear(GL_COLOR_BUFFER_BIT);
The next line sets the current rendering color to blue. OpenGL behaves like a state machine, so certain
state such as the rendering color is saved by OpenGL and used automatically later as it is needed.
glColor3f(0.0f, 0.0f, 1.0f);
To render a primitive, such as a point, line, or polygon, OpenGL requires that a call to glBegin is made
to specify the type of primitive being rendered.
glBegin(GL_LINES);
Only a subset of OpenGL commands is available after a call to glBegin. The main command that is used
is glVertex, which specifies a vertex position. In GL LINES mode, each pair of vertices define endpoints
of a line segment. In this case, a line would be drawn from the point at ( x0, y0) to (x1, y1).
glVertex2f(x0, y0); glVertex2f(x1, y1);
A call to glEnd completes rendering of the current primitive. glEnd(); Finally, the back buffer needs to
be swapped to the front buffer that the user will see, which GLUT can handle for us:
glutSwapBuffers();
Developer-Driven Advantages
Industry standard
An independent consortium, the OpenGL Architecture Review Board, guides the OpenGL
specification. With broad industry support, OpenGL is the only truly open, vendor-neutral,
multiplatform graphics standard.
Stable
OpenGL implementations have been available for more than seven years on a wide variety of
platforms. Additions to the specification are well controlled, and proposed updates are
announced in time for developers to adopt changes. Backward compatibility requirements
ensure that existing applications do not become obsolete.
• All OpenGL applications produce consistent visual display results on any OpenGL API-
compliant hardware, regardless of operating system or windowing system.
• Evolving
• Because of its thorough and forward-looking design, OpenGL allows new hardware
innovations to be accessible through the API via the OpenGL extension mechanism. In this way,
innovations appear in the API in a timely fashion, letting application developers and hardware
vendors incorporate new features into their normal product release cycles.
• Scalable
• OpenGL API-based applications can run on systems ranging from consumer electronics to PCs,
workstations, and supercomputers. As a result, applications can scale to any class of machine
that the developer chooses to target.
• Easy to use
• OpenGL is well structured with an intuitive design and logical commands. Efficient OpenGL
routines typically result in applications with fewer lines of code than those that make up
programs generated using other graphics libraries or packages. In addition, OpenGL drivers
encapsulate information about the underlying hardware, freeing the application developer
from having to design for specific hardware features.
• Well-documented:
• Numerous books have been published about OpenGL, and a great deal of sample code is readily
available, making information about OpenGL inexpensive and easy to obtain.