Now and Get: Best VTU Student Companion App You Can Get
Now and Get: Best VTU Student Companion App You Can Get
Now and Get: Best VTU Student Companion App You Can Get
* Visit https://vtuconnect.in for more info. For any queries or questions wrt our
platform contact us at: [email protected]
Download & Share VTU Connect App Now From Google Play Store
(EVEN SEMESTER)
LABORATORY MANUAL
SEMESTER: VI
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
Objectives
Vision
Mission.
To keep pace with advancements in knowledge and make the students competitive
and capable at the global level.
To create an environment for the students to acquire the right physical,
intellectual, emotional and moral foundations and shine as torch bearers of
tomorrow's society.
To strive to attain ever-higher benchmarks of educational excellence.
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
PO6. The engineer and society: Apply reasoning informed by the contextual
knowledge to assess societal, health, safety, legal and cultural issues and the
consequent responsibilities relevant to the professional engineering practice
PO7. Environment and sustainability: Understand the impact of the
professional engineering solutions in societal and environmental contexts, and
demonstrate the knowledge of, and need for sustainable development.
PO8. Ethics: Apply ethical principles and commit to professional ethics and
responsibilities and norms of the engineering practice.
PO9. Individual and team work: Function effectively as an individual, and as a
member or leader in diverse teams, and in multidisciplinary settings.
PO10. Communication: Communicate effectively on complex engineering
activities with the engineering community and with society at large, such as, being
able to comprehend and write effective reports and design documentation, make
effective presentations, and give and receive clear instructions.
PO11. Project management and finance: Demonstrate knowledge and
understanding of the engineering and management principles and apply these to
one’s own work, as a member and leader in a team, to manage projects and in
multidisciplinary environments.
PO12. Life-long learning: Recognize the need for, and have the preparation and
ability to engage in independent and life-long learning in the broadest context of
technological change.
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
1. Ability to apply skills in the field of algorithms, database design, web design, cloud
computing and data analytics..
2. Apply knowledge in the field of computer networks for building network and
internet based applications.
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
Credits - 2
Course objectives: This course (18CSL67) will enable students to:
Demonstrate simple algorithms using OpenGL Graphics Primitives and attributes.
Implementation of line drawing and clipping algorithms using OpenGL functions
Design and implementation of algorithms Geometric transformations on both 2D
and 3D objects.
Descriptions (if any): --
Installation procedure of the required software must be demonstrated, carried
out in groups and documented in the journal.
Programs List:
PART A
Design, develop, and implement the following programs using OpenGL API
1. Implement Brenham's line drawing algorithm for all types of slope.
Refer: Text-1: Chapter 3.5
Refer: Text-2: Chapter 8
2. Create and rotate a triangle about the origin and a fixed point.
Refer: Text-1: Chapter 5-4
3. Draw a colour cube and spin it using OpenGL transformation matrices.
Refer: Text-2: Modelling a Coloured Cube
4. Draw a color cube and allow the user to move the camera suitably to experiment with
perspective viewing.
Refer: Text-2: Topic: Positioning of Camera
5. Clip a line using Cohen-Sutherland algorithm.
Refer: Text-1: Chapter 6.7
Refer: Text-2: Chapter 8
6. To draw a simple shaded scene consisting of a tea pot on a table. Define suitably the
position and properties of the light source along with the properties of the surfaces of
the solid object used in the scene.
Refer: Text-2: Topic: Lighting and Shading
7. Design, develop and implement recursively subdivide a tetrahedron to form 3D
sierpinski gasket. The number of recursive steps is to be specified by the user.
Refer: Text-2: Topic: sierpinski gasket.
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
8. Develop a menu driven program to animate a flag using Bezier Curve algorithm.
Refer: Text-1: Chapter 8-10
9. Develop a menu driven program to fill the polygon using scan line algorithm.
PART –B (MINI-PROJECT)
Student should develop mini project on the topics mentioned below or similar
applications using Open GL API. Consider all types of attributes like color, thickness,
styles, font, background, speed etc., while doing mini project.
(During the practical exam: the students should demonstrate and answer Viva-
Voce)
Sample Topics:
Simulation of concepts of OS, Data structures, algorithms etc.
Laboratory outcomes: The students should be able to:
Apply the concepts of computer graphics
Implement computer graphics applications using OpenGL
Animate real world problems using OpenGL
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
CONTENT LIST
Download & Share VTU Connect App Now From Google Play Store
Computer Graphics Laboratory
Download & ShareWith Mini App
VTU Connect Project
Now From Google Play Store 18CSL67
OpenGL
Introduction
OpenGL (Open Graphics Library) is an application program interface (API) that is used to
define 2D and 3D computer graphics.
The interface consists of over 250 different function calls which can be used to draw
complex three-dimensional scenes from simple primitives. OpenGL was developed by Silicon
Graphics Inc. (SGI) in 1992 and is widely used in CAD, virtual reality, scientific visualization,
information visualization, and flight simulation.
OpenGL's basic operation is to accept primitives such as points, lines and polygons, and
convert them into pixels. This is done by a graphics pipeline known as the OpenGL state
machine.
Features of OpenGL:-
GLUT gives you the ability to create a window, handle input and render to the screen
without being Operating System dependent.
The first things you will need are the OpenGL and GLUT header files and libraries for your
current Operating System.
Once you have them setup on your system correctly, open your first C file and include them at
the start of your program:
Now, just double check that your system is setup correctly, and try compiling your current file.
If you get no errors, you can proceed. If you have any errors, try your best to fix them. Once you
are ready to move onto the next step, create a main() method in your current file.
Inside this is where all of your main GLUT calls will go.
The first call we are going to make will initialize GLUT and is done like so:
glutInit(&argc, argv); //initialize the program.
Keep in mind for this, that argc and argv are passed as parameters to your main method. You
can see how to do this below.
Once we have GLUT initialized, we need to tell GLUT how we want to draw. There are several
parameters we can pass here, but we are going to stick them with most basic GLUT_SINGLE,
which will give use a single buffered window.
glutInitDisplayMode(GLUT_SINGLE);
//set up a basic display buffer (only singular for now)
The next two methods we are going to use, simply set the size and position of the GLUT window
on our screen:
glutInitWindowSize (500, 500); //set the width and height of the window
glutInitWindowPosition (100, 100); // set the position of the window
glutCreateWindow ("A basic OpenGL Window"); //set the caption for the window
We now have a window of the size and position that we want. But we need to be able to draw to
it. We do this, by telling GLUT which method will be our main drawing method. In this case, it
is a void method called display ()
So thus far, we have a window. But the display method that I mentioned is needed.
Let’s take a look at this and dissect it.
void display(void)
{
The next thing we want to do, is erase everything currently stored by OpenGL. We need to do
this at the start of the method, as the method keeps looping over itself and if we draw something,
we need to erase it before we draw our next frame. If we don't do this, we can end up with a big
mess inside our buffer where frames have been drawn over each other.
The third method, glLoadIdentity resets our model view matrix to the identity matrix, so that our
drawing transformation matrix is reset. From then, we will set the 'camera' for our scene. I am
placing it 5 units back into the user so that anything we draw at 0, 0, 0 will be seen in front of us.
The final thing we need to do is then user the current buffer to the screen so we can see it. This
can be done with glFlush() as we are only using a single buffer.
OpenGL is an API. OpenGL is nothing more than a set of functions you call from your program
(think of as collection of .h les.
OpenGL Libraries
GLU
Helper functions for shapes, transformations
gluPerspective(fovy, aspect, near, far )
gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);
GLUT
Highest level: Window and interface management
glutSwapBuers()
glutInitWindowSize(500, 500);
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
Windows, Linux, UNIX, etc. all provide a platform specific implementation.
Ten gl Primitives
Projections in OpenGL
Perspective projection
Orthographic projection
void glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdou-ble
near, GLdouble far)
GLUT
The OpenGL Utility Toolkit (GLUT) is a library of utilities for OpenGL programs, which
primarily perform system-level I/O with the host operating system. Functions performed include
window defnition, window control, and monitoring of keyboard and mouse input. Routines for
drawing a number of geometric primitives (both in solid and wireframe mode) are also provided,
including cubes, spheres, and cylinders. GLUT even has some limited support for creating pop-
up menus. The two aims of GLUT are to allow the creation of rather portable code between
operating systems (GLUT is cross-platform) and to make learning OpenGL easier. All GLUT
functions start with the glut prefex (for example, glutPostRedisplay marks the current window as
needing to be redrawn).
All data, whether it describes geometry or pixels, can be saved in a display list for current or
later use. (The alternative to retaining data in a display list is processing the data immediately -
also known as immediate mode.) When a display list is executed, the retained data is sent from
the display list just as if it were sent by the application in immediate mode.
Evaluators
All geometric primitives are eventually described by vertices. Parametric curves and surfaces
may be initially described by control points and polynomial functions called basis functions.
Evaluators provide a method to derive the vertices used to represent the surface from the control
points. The method is a polynomial mapping, which can produce surface normal, texture
coordinates, colors, and spatial coordinate values from the control points.
Per-Vertex Operations
For vertex data, next is the "per-vertex operations" stage, which converts the vertices into
primitives. Some vertex data (for example, spatial coordinates) are transformed by 4 x 4 oating-
point matrices. Spatial coordinates are projected from a position in the 3D world to a position on
your screen. If advanced features are enabled, this stage is even busier. If texturing is used,
texture coordinates may be generated and transformed here. If lighting is enabled, the lighting
calculations are performed using the transformed vertex, surface normal, light source position,
material properties, and other lighting information to produce a color value.
Primitive Assembly
Clipping, a major part of primitive assembly, is the elimination of portions of geome-try which
fall outside a half-space, dened by a plane. Point clipping simply passes or rejects vertices; line
or polygon clipping can add additional vertices depending upon how the line or polygon is
clipped. In some cases, this is followed by perspective division, which makes distant geometric
objects appear smaller than closer objects. Then viewport and depth (z coordinate) operations
are applied. If culling is enabled and the primitive is a polygon, it then may be rejected by a
culling test. Depending upon the polygon mode, a polygon may be drawn as points or lines.
The results of this stage are complete geometric primitives, which are the transformed and
clipped vertices with related color, depth, and sometimes texture-coordinate values and
guidelines for the rasterization step.
Pixel Operations
While geometric data takes one path through the OpenGL rendering pipeline, pixel data takes a
different route. Pixels from an array in system memory are rst unpacked from one of a variety of
formats into the proper number of components. Next the data is scaled, biased, and processed by
a pixel map. The results are clamped and then either written into texture memory or sent to the
rasterization step If pixel data is read from the frame bufer, pixel-transfer operations (scale, bias,
mapping, and clamping) are performed. Then these results are packed into an appropriate format
and returned to an array in system memory
.
There are special pixel copy operations to copy data in the frame buffer to other parts of the
frame buffer or to the texture memory. A single pass is made through the pixel transfer
operations before the data is written to the texture memory or back to the frame buffer.
Texture Assembly
An OpenGL application may wish to apply texture images onto geometric objects to make them
look more realistic. If several texture images are used, it's wise to put them into texture objects
so that you can easily switch among them.
Some OpenGL implementations may have special resources to accelerate texture performance.
There may be specialized, high-performance texture memory. If this memory is available, the
texture objects may be prioritized to control the use of this limited and valuable resource.
Rasterization
Rasterization is the conversion of both geometric and pixel data into fragments. Each fragment
square corresponds to a pixel in the frame buffer. Line and polygon stipples, line width, point
size, shading model, and coverage calculations to support antialiasing are taken into
consideration as vertices are connected into lines or the interior pixels are calculated for a filled
polygon. Color and depth values are assigned for each fragment square.
Fragment Operations
Before values are actually stored into the frame buffer, a series of operations are performed that
may alter or even throw out fragments. All these operations can be enabled or disabled.
The first operation which may be encountered is texturing, where a texel (texture element) is
generated from texture memory for each fragment and applied to the fragment. Then fog
calculations may be applied, followed by the scissor test, the alpha test, the stencil test, and the
depth-buffer test (the depth buffer is for hidden-surface removal). Failing an enabled test may
end the continued processing of a fragment's square. Then, blending, dithering, logical
operation, and masking by a bitmask may be performed. Finally, the thoroughly processed
fragment is drawn into the appropriate buffer, where it has finally advanced to be a pixel and
achieved its final resting place.
OpenGL-Related Libraries
OpenGL provides a powerful but primitive set of rendering commands, and all higher-level
drawing must be done in terms of these commands. Also, OpenGL programs have to use the
underlying mechanisms of the windowing system. A number of libraries exist to allow you to
simplify your programming tasks, including the following:
The OpenGL Utility Library (GLU) contains several routines that use lower-level OpenGL
commands to perform such tasks as setting up matrices for specific viewing orientations and
projections, performing polygon tessellation, and rendering surfaces. This library is provided as
part of every OpenGL implementation. Portions of the GLU are described in the OpenGL.
For every window system, there is a library that extends the functionality of that window system
to support OpenGL rendering. For machines that use the X Window System, the OpenGL
Extension to the X Window System (GLX) is provided as an adjunct to OpenGL. GLX routines
use the prefix glX. For Microsoft Windows, the WGL routines provide the Windows to OpenGL
interface. All WGL routines use the prefix wgl. For IBM OS/2, the PGL is the Presentation
Manager to OpenGL interface, and its routines use the prefix pgl.
The OpenGL Utility Toolkit (GLUT) is a window system-independent toolkit, written by Mark
Kilgard, to hide the complexities of differing window system APIs. Open Inventor is an object-
oriented toolkit based on OpenGL which provides objects and methods for creating interactive
three-dimensional graphics applications. Open Inventor, which is written in C++, provides
prebuilt objects and a built-in event model for user interaction, high-level application
components for creating and editing three-dimensional scenes, and the ability to print objects
and exchange data in other graphics formats. Open Inventor is separate from OpenGL.
As you know, OpenGL contains rendering commands but is designed to be independent of any
window system or operating system. Consequently, it contains no commands for opening
windows or reading events from the keyboard or mouse. Un-fortunately, it's impossible to write
a complete graphics program without at least opening a window, and most interesting programs
require a bit of user input or other services from the operating system or window system. In
many cases, complete programs make the most interesting examples, so this book uses GLUT to
simplify opening windows, detecting input, and so on. If you have an implementation of
OpenGL and GLUT on your system, the examples in this book should run without change when
linked with them.
In addition, since OpenGL drawing commands are limited to those that generate simple
geometric primitives (points, lines, and polygons), GLUT includes several routines that create
more complicated three-dimensional objects such as a sphere, a torus, and a teapot. This way,
snapshots of program output can be interesting to look at. (Note that the OpenGL Utility
Library, GLU, also has quadrics routines that create some of the same three-dimensional objects
as GLUT, such as a sphere, cylinder, or cone.)
GLUT may not be satisfactory for full-featured OpenGL applications, but you may find it a
useful starting point for learning OpenGL. The rest of this section briefly describes a small
subset of GLUT routines so that you can follow the programming examples in the rest of this
book.
glutInit: initializes GLUT, must be called before other GL/GLUT functions. It takes the same
arguments as the main(). void glutInit(int *argc, char **argv)
glutCreateWindow: creates a window with the given title. int glutCreateWindow(char *title)
glutInitWindowSize: species the initial window width and height, in pixels. void
glutInitWindowSize(int width, int height)
glutInitWindowPosition: positions the top-left corner of the initial window at (x, y). The
coordinates (x, y), in term of pixels, is measured in window coordinates, i.e., origin (0, 0) is at
the top-left corner of the screen; x-axis pointing right and y-axis pointing down. void
glutInitWindowPosition(int x, int y)
glutDisplayFunc: registers the callback function (or event handler) for handling
window-paint event. The OpenGL graphic system calls back this handler when it receives a
window re-paint request. In the example, we register the function display() as the handler. void
glutDisplayFunc(void (*func)(void))
glutMainLoop: enters the innite event-processing loop, i.e, put the OpenGL graphics system
to wait for events (such as re-paint), and trigger respective event handlers (such as display()).
void glutMainLoop()
void glMatrixMode (GLenum mode); The glMatrixMode function species which matrix is
the current matrix.
void glPointSize (GLoat size); The glPointSize function species the diameter of rasterized
points.
void glPushMatrix (void); void glPopMatrix (void); The glPushMatrix and glPopMatrix
functions push and pop the current matrix stack.
void glRotatef (GLoat angle, GLoat x, GLoat y, GLoat); The glRotatef functions multiply the
current matrix by a rotation matrix.
void glScalef (GLoat x, GLoat y, GLoat z); The glScalef functions multiply the current matrix
by a general scaling matrix.
void glTranslatef (GLoat x, GLoat y, GLoat z); The glTranslatef functions multiply the current
matrix by a translation matrix.
void glViewport (GLint x, GLint y, GLsizei width, GLsizei height); The glView-port function
sets the viewport.
void glEnable, glDisable(); The glEnable and glDisable functions enable or dis-able OpenGL
capabilities.
Usage
void glutInit(int *argcp, char **argv);
argcp
A pointer to the program's unmodied argc variable from main. Upon return, the value
pointed to by argcp will be updated, because glutInit extracts any command line options
intended for the GLUT library.
argv
The program's unmodied argv variable from main. Like argcp, the data for argv will be
updated because glutInit extracts any command line options understood by the GLUT
library.
Description
glutInit will initialize the GLUT library. During this process, glutInit may cause the
termination of the GLUT program with an error message to the user if GLUT cannot be
properly initialized.
glutInitWindowPosition, glutInitWindowSize
glutInitWindowPosition and glutInitWindowSize set the initial window position and size
respectively.
Usage
void glutInitWindowSize(int width, int height);
void glutInitWindowPosition(int x, int y);
Width
Width in pixels.
Height
Height in pixels.
x
Window X location in pixels.
y
Window Y location in pixels.
Description
ˆ
Windows created by glutCreateWindow will be requested to be created with the current initial
window position and size.
glutInitDisplayMode
mode
Display mode, normally the bitwise OR-ing of GLUT display mode bit masks. See
values below:
GLUT_RGB
An alias for GLUT_RGBA.
GLUT_INDEX
Bit mask to select a color index mode window. This overrides GLUT RGBA if it is also
specified.
GLUT_SINGLE
Bit mask to select a single buffered window. This is the default if neither GLUT_DOUBLE or
GLUT_SINGLE are specified.
GLUT_DOUBLE
Bit mask to select a double buffered window. This overrides GLUT_SINGLE if it is also
specified.
GLUT_DEPTH
Bit mask to select a window with a depth buffer.
Description
The initial display mode is used when creating top-level windows.
glutMainLoop
glutMainLoop enters the GLUT event processing loop. This routine should be called at
most once in a GLUT program. Once called, this routine will never return. It will call as
necessary any callbacks that have been registered.
glutCreateWindow
glutCreateWindow creates a top-level window.
Usage
int glutCreateWindow(char *name);
name
ASCII character string for use as window name.
Description
glutCreateWindow creates a top-level window. The name will be provided to the
window system as the window's name. The intent is that the window system will label
the window with the name.
glutPostRedisplay
glutPostRedisplay marks the current window as needing to be redisplayed.
Usage
void glutPostRedisplay(void);
Description
Mark the normal plane of current window as needing to be redisplayed. The next
iteration through glutMainLoop, the window's display callback will be called to redisplay
the window's normal plane. Multiple calls to glutPostRedisplay before the next display
callback opportunity generates only a single redisplay callback.
glutReshapeWindow
glutReshapeWindow requests a change to the size of the current window.
Usage
void glutReshapeWindow(int width, int height);
width
New width of window in pixels.
Height
New height of window in pixels.
Description
glutReshapeWindow requests a change in the size of the current window. The width
and height parameters are size extents in pixels. The width and height must be positive
values. The requests by glutReshapeWindow are not processed immediately. The request is
executed after returning to the main event loop. This allows multiple glutReshapeWindow,
glutPositionWindow, and glut-FullScreen requests to the same window to be coalesced.
glutDisplayFunc
glutDisplayFunc sets the display callback for the current window.
Usage
void glutDisplayFunc(void (*func)(void));
func
The new display callback function.
ˆ
Description
glutDisplayFunc sets the display callback for the current window. When GLUT
determines that the normal plane for the window needs to be redisplayed, the display
callback for the window is called. Before the callback, the current win-dow is set to the
window needing to be redisplayed and (if no overlay display callback is registered) the layer
in use is set to the normal plane. The display callback is called with no parameters. The
entire normal plane region should be redisplayed in response to the callback (this includes
ancillary buers if your program depends on their state).
glutReshapeFunc
glutReshapeFunc sets the reshape callback for the current window.
Usage
void glutReshapeFunc(void (*func)(int width, int height));
func
The new reshape callback function.
Description
glutReshapeFunc sets the reshape callback for the current window. The reshape
callback is triggered when a window is reshaped. A reshape callback is also triggered
immediately before a window's rst display callback after a window is created or whenever
an overlay for the window is established. The width and height parameters of the callback
specify the new window size in pixels. Before the callback, the current window is set to the
window that has been reshaped.
glFlush
glFlush - force execution of GL commands in finite time
ˆ
Description
glMatrixMode
glMatrixMode - specify which matrix is the current matrix
Usage
void glMatrixMode(GLenum mode)
Parameters
Mode- Species which matrix stack is the target for subsequent matrix oper-ations.
Three values are accepted: GL_MODELVIEW, GL_PROJECTION, and GL_TEXTURE.
The default value is GL_MODELVIEW.
Description
glMatrixMode sets the current matrix mode. Mode can assume one of three values:
GL_MODELVIEW - Applies subsequent matrix operations to the mode matrix stack.
GL_PROJECTION - Applies subsequent matrix operations to the projection matrix stack
gluOrtho2D
Usage
gluOrtho2D(left, right, bottom, top)
Species the 2D region to be projected into the viewport. Any drawing outside the region
will be automatically clipped away
Sample Programs
Creating a Window:
#include<GL/glut.h>
void display () /* callback function which is called when OpenGL needs to update the display */
Activity 1:
Change the window size to 500, 500
#include<GL/glut.h>
glClearColor (0.0,1.0,1.0,0.0);
glClear (GL_COLOR_BUFFER_BIT);
glFlush();
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
Drawing pixels/points :
#include<GL/glut.h>
void display()
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
glVertex2i(100,300);
glVertex2i(201,300);
glEnd();
glFlush();
void myinit()
glutInit(&argc,argv);
glutInitWindowSize(300,300);
glutInitWindowPosition(0,0);
glutCreateWindow("Example2");
glutDisplayFunc(display);
myinit();
glutMainLoop();
Activity 2
Change the window color to BLUE
Draw FIVE points: at 4 corners of the window and one more at the Centre of the window.
#include<GL/glut.h>
void display()
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
glVertex2i(10,10);
glVertex2i(250,250);
glVertex2i(10,490);
glVertex2i(490,490);
glVertex2i(490,10);
glEnd();
glFlush();
void myinit()
glPointSize(10.0);
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("Activity2");
glutDisplayFunc(display);
myinit();
glutMainLoop();
Drawing lines :
#include<GL/glut.h>
void display()
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINES);
glVertex2d (150,50);
glVertex2d (200,300);
glEnd();
glFlush();
void myinit()
glClearColor(1.0,1.0,1.0,1.0);
glColor3f(1.0,0.0,0.0);
glPointSize(1.0);
gluOrtho2D(0.0,500.0,0.0,500.0);
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("LINE");
glutDisplayFunc(display);
myinit();
glutMainLoop();
Activity 3
#include<GL/glut.h>
void display()
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,1.0,0.0);
glLineWidth(4.0);
glBegin(GL_LINES);
glEnd();
glFlush();}
void myinit()
glClearColor(1.0,0.0,1.0,1.0);
gluOrtho2D(0.0,200.0,0.0,200.0);
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(10,100);
glutCreateWindow("Square");
glutDisplayFunc(display);
myinit();
glutMainLoop();
#include<GL/glut.h>
void display()
glClear(GL_COLOR_BUFFER_BIT);
glLineWidth(3.0);
glVertex2f(50, 50);
glVertex2f(200, 50);
glVertex2f(200, 200);
glVertex2f(50, 200);
glEnd();
glFlush();
void myinit()
glClearColor(1.0,1.0,0.0,1.0);
gluOrtho2D(0.0,499.0,0.0,499.0);
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(300,300);
glutInitWindowPosition(0,0);
glutCreateWindow("LINE LOOP");
glutDisplayFunc(display);
myinit();
glutMainLoop();
Activity 4
Change the window color to PURPLE
#include<GL/glut.h>
void display()
glClear(GL_COLOR_BUFFER_BIT);
glLineWidth(3.0);
glBegin(GL_POLYGON);
glVertex2f(50, 50);
glVertex2f(200, 50);
glVertex2f(200, 200);
glVertex2f(50, 200);
glEnd();
void myinit()
gluOrtho2D(0.0,499.0,0.0,499.0);
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(300,300);
glutInitWindowPosition(0,0);
glutCreateWindow("POLYGON");
glutDisplayFunc(display);
myinit();
glutMainLoop();
glClear(GL_COLOR_BUFFER_BIT);
glLineWidth(3.0);
glBegin(GL_POLYGON);
glVertex2f(50, 50);
glVertex2f(200, 50);
glVertex2f(200, 200);
glVertex2f(50, 200);
glEnd();
glFlush();
void myinit()
{
glClearColor(0.0,0.0,0.0,0.0);
gluOrtho2D(0.0,499.0,0.0,499.0);
}
void main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(300,300);
glutInitWindowPosition(0,0);
glutCreateWindow("TRIANGLE");
glutDisplayFunc(display);
myinit();
glutMainLoop();
Writing Text
#include<GL/glut.h>
char *p;
glPushMatrix();
glTranslatef(x,y,0);
glScaled(0.2,0.2,0);
for(p=text;*p;p++)
glutStrokeCharacter(GLUT_STROKE_ROMAN,*p);
glPopMatrix();
void display()
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
void myinit()
glClearColor(1.0,1.0,1.0,1.0);
glColor3f(1.0,0.0,0.0);
gluOrtho2D(0.0,499.0,0.0,499.0);
glutInit(&2wargc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("ATME");
glutDisplayFunc(display);
myinit();
glutMainLoop();
#include<string.h>
void display()
int i;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glLineWidth(10.0);
glBegin(GL_LINES);
glVertex2f(0.0,0.0);
glColor3f(0.0,1.0,0.0);
glVertex2f(0.0,0.8);
glEnd();
glColor3f(0.0,1.0,1.0);
glRasterPos2f(-0.2,-0.1);
for(i=0;i<strlen(str);i++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,str[i]);
glFlush();
void myinit()
glClearColor(0.0,0.0,0.0,0.0);
gluOrtho2D(-1.0,1.0,-1.0,1.0);
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("Coloured Line");
myinit();
glutDisplayFunc(display);
glutMainLoop();
Activity 5
#include<GL/glut.h>
char *p;
glPushMatrix();
glTranslatef(x,y,0);
glScaled(0.2,0.2,0);
for(p=text;*p;p++)
glutStrokeCharacter( GLUT_STROKE_MONO_ROMAN,*p);
glPopMatrix();
void display()
glClear(GL_COLOR_BUFFER_BIT);
output(70,300,"GRAPHICS IS FUN!");
glColor3f(1.0,0.0,0.0);
output(120,250,"REALLY FUN!");
glFlush();
void myinit()
glClearColor(0.0,0.0,0.0,0.0);
glColor3f(1.0,1.0,0.0);
gluOrtho2D(0.0,499.0,0.0,499.0);
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("STROKE TEXT”);
glutDisplayFunc(display);
myinit();
glutMainLoop();
void display()
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glVertex2f(100, 100);
glColor3f(1.0,0.0,0.0); // Red
glVertex2f(300, 100);
glColor3f(0.0,0.0,1.0); // Blue
glVertex2f(300, 300);
glColor3f(1.0,1.0,0.0); // Yellow
glVertex2f(100, 300);
glEnd();
glFlush();
void myinit()
glClearColor(0.0,0.0,0.0,1.0);
glColor3f(1.0,0.0,0.0);// Red
gluOrtho2D(0.0,499.0,0.0,499.0);
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("COLORED SQUARE");
glutDisplayFunc(display);
myinit();
glutMainLoop();
void display()
glClear(GL_COLOR_BUFFER_BIT);
glViewport (5,-150,400,400);
glBegin(GL_POLYGON);
glColor3f(1.0,0.0,0.0);
glVertex2f(90,250);
glColor3f(0.0,1.0,0.0);
glVertex2f(250,250);
glColor3f(0.0,0.0,1.0);
glVertex2f(175,400);
glEnd();
glViewport (300,300,400,400);
glBegin(GL_POLYGON);
glColor3f(1.0,0.0,0.0);
glVertex2f(50,50);
glColor3f(0.0,1.0,0.0);
glVertex2f(250,50);
glColor3f(0.0,0.0,1.0);
glVertex2f(250,250);
glColor3f(0.0,1.0,1.0);
glVertex2f(50,250);
glEnd();
glFlush();
voidmyinit()
glClearColor(0.0,0.0,0.0,1.0);
gluOrtho2D(0.0,499.0,0.0,499.0);
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutDisplayFunc(display);
myinit();
glutMainLoop();
1. PART A
Design, develop, and implement the following programs in C/C++ using OpenGL API.
1.1.1 PREAMBLE
Bresenham's line algorithm is an algorithm that determines the points of an n-dimensional raster
that should be selected in order to form a close approximation to a straight line between two
points. It is commonly used to draw line primitives in a bitmap image (e.g. on a computer
screen), as it uses only integer addition, sub-traction and bit shifting, all of which are very cheap
operations in standard computer architectures.
It is an incremental error algorithm. It is one of the earliest algorithms developed in the field of
computer graphics. An extension to the original algorithm may be used for drawing circles.
While algorithms such as Wu's algorithm are also frequently used in modern computer graphics
because they can support antialiasing, the speed and simplicity of Bresenham's line algorithm
means that it is still important.
The algorithm is used in hardware such as plotters and in the graphics chips of modern graphics
cards. It can also be found in many software graphics libraries. Because the algorithm is very
simple, it is often implemented in either the rmware or the graphics hardware of modern
graphics cards.
Concepts Used:
Line equations
Interger arithmetic
Algorithm:
Pseudo Code OpenGL commands Familiarised :
glutInit
glutInitDisplayMode
glClearColor
glColor
glPointSize
glOrtho
Program 1: Implement Brenham’s line drawing algorithm for all types of slope.
Program Objective:
creation of 2D objects
Implement GLU and GLUT functions
To have the detailed knowledge of the graphics Brenham's line algorithm.
/* Brenham's line */
#include<GL/glut.h>
#include<stdio.h>
int x1,x2,y1,y2;
void myInit()
{
glClearColor(0.0,0.0,0.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,500,0,500);
}
inc1=2*(dy-dx);
inc2=2*dy;
for(i=0;i<dx;i++)
{
if(e>=0)
{
y+=incy;
e+=inc1;
}
else
e+=inc2;
x+=incx;
draw_pixel(x,y);
}
}
else
{
draw_pixel(x,y);
e=2*dx-dy;
inc1=2*(dx-dy);
inc2=2*dx;
for(i=0;i<dy;i++)
{
if(e>=0)
{
x+=incx;
e+=inc1;
}
else
e+=inc2;
y+=incy;
draw_pixel(x,y);
}
}
}
void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
draw_line(x1,x2,y1,y2);
glFlush();
}
int main(int argc,char **argv)
{
printf("enter x1,x2,y1,y2\n");
scanf("%d%d%d%d",&x1,&x2,&y1,&y2);
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("win");
glutDisplayFunc(myDisplay);
myInit();
glEnable(GL_DEPTH_TEST);
glClearColor(0.0,0.0,0.0,0.0);
glutMainLoop();
}
RUN:
gcc 1.c -lglut -lGL –lGLU
./a.out
SAMPLE OUTPUT:
Program Outcome:
1.2.1 PREAMBLE
In linear algebra, a rotation matrix is a matrix that is used to perform a rotation in Euclidean
space. For example the matrix rotates points in the xy-Cartesian plane counterclockwise
through an angle about the origin of the Cartesian coordinate system. To perform the rotation
using a rotation matrix R, the position of each point must be represented by a column vector
v, containing the coordinates of the point.
A rotated vector is obtained by using the matrix multiplication Rv. Since matrix
multiplication has no effect on the zero vector (i.e., on the coordinates of the origin), rotation
matrices can only be used to describe rotations about the origin of the coordinate system.
Rotation matrices provide a simple algebraic description of such rotations, and are used
extensively for computations in geometry, physics, and computer graphics. In 2-dimensional
space, a rotation can be simply described by an angle of rotation, but it can be also
represented by the 4 entries of a rotation matrix with 2 rows and 2 columns. In 3-dimensional
space, every rotation can be interpreted as a rotation by a given angle about a single fixed
axis of rotation (see Euler's rotation theorem), and hence it can be simply described by an
angle and a vector with 3 entries. However, it can also be represented by the 9 entries of a
rotation matrix with 3 rows and 3 columns.
The notion of rotation is not commonly used in dimensions higher than 3; there is a notion of
a rotational displacement, which can be represented by a matrix, but no associated single axis
or angle.
Program 2: Create and rotate a triangle about the origin and a fixed point.
Program Objective:
creation of 2D objects
Create 2D Triangle with basic transformation operations like rotation.
Use mathematical and theoretical principles i.e transformation matrices of computer
graphics to draw and rotate Triangle object.
/* Triangle Rotation */
#include<GL/glut.h>
#include<stdio.h>
#include<math.h>
GLfloat triangle[3][3]={{200.0,300.0,250.0},{200.0,200.0,300.0},{0.0,0.0,0.0}};
float matRot[3][3]={{0.0},{0.0},{0.0}};
GLfloat res[3][3]={{0.0},{0.0},{0.0}};
GLfloat theta=0.0;
void Rotate_point()
{
int i,j,l;
matRot[0][0]=cos(theta);
matRot[0][1]=-sin(theta);
matRot[0][2]=0;
matRot[1][0]=sin(theta);
matRot[1][1]=cos(theta);
matRot[1][2]=0;
matRot[2][0]=0;
matRot[2][1]=0;
matRot[2][2]=1;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
res[i][j]=0;
for(l=0;l<3;l++)
{
res[i][j]+=matRot[i][l]*triangle[l][j];
}
}
}
void inp_triangle()
{
glBegin(GL_LINE_LOOP);
glVertex2f(triangle[0][0],triangle[1][0]);
glVertex2f(triangle[0][1],triangle[1][1]);
glVertex2f(triangle[0][2],triangle[1][2]);
glEnd();
glFlush();
}
void Out_triangle()
{
glBegin(GL_LINE_LOOP);
glVertex2f(res[0][0],res[1][0]);
glVertex2f(res[0][1],res[1][1]);
glVertex2f(res[0][2],res[1][2]);
glEnd();
glFlush();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
inp_triangle();
glColor3f(0.0,1.0,0.0);
glTranslatef(50.0,0.0,0.0);
Rotate_point();
Out_triangle();
glPushMatrix();
glColor3f(0.0,0.0,1.0);
glTranslatef(300.0,100.0,0.0);
Rotate_point();
glTranslatef(-200.0,-200.0,0.0);
Out_triangle();
glPopMatrix();
glFlush();
}
void reshape(GLint w,GLint h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,500,0,500);
glutPostRedisplay();
glFlush();
}
int main(int argc,char **argv)
{
printf("enter the angle of rotation\n");
scanf("%f",&theta);
theta=theta*3.14/180;
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(800,800);
glutInitWindowPosition(0,0);
glutCreateWindow("2d");
glutDisplayFunc(display);
glEnable(GL_DEPTH_TEST);
glutReshapeFunc(reshape);
glClearColor(0.0,0.0,0.0,0.0);
glutMainLoop();
}
RUN:
SAMPLE OUTPUT:
Program Outcome:
1.3.1 PREAMBLE
Concepts Used:
Data structures for representing a cube
Rotation Transformation
Algorithm:
Modeling a color cube with simple data structures
Define vertices with centre of cube as origin
Define normals to identify faces
Define colors
2. Draw a polygon from a list of indices into the array vertices and use color corresponding to first
index
polygon(1,2,6,5);
polygon(4,5,6,7);
polygon(0,1,5,4);
}
4. Define the Display function to clear frame buffer, Z-buffer, rotate cube and draw, swap buffers.
void display(void)
{
/* display callback, clear frame buffer and z buffer,
rotate cube and draw, swap buffers */
glLoadIdentity();
glRotatef(theta[0], 1.0, 0.0, 0.0);
glRotatef(theta[1], 0.0, 1.0, 0.0);
glRotatef(theta[2], 0.0, 0.0, 1.0);
colorcube();
glutSwapBuffers();
5. Define the spin cube function to spin cube 2 degrees about selected axis.
6. Define mouse callback to select axis about which to rotate.
7. Define reshape function to maintain aspect ratio.
Program 3: Draw a color cube and spin it using OpenGL transformation matrices.
Program Objective:
creation of 3D objects
Create 3D-color cube with basic transformation operations like rotation.
Use mathematical and theoretical principles i.e transformation matrices of computer graphics
to draw the color cube object.
#include<GL/glut.h>
#include<stdio.h>
GLfloat vertices[][3]={{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0},{1.0,1.0,-1.0},{-1.0,1.0,-1.0},
{-1.0,-1.0,1.0},{1.0,-1.0,1.0},{1.0,1.0,1.0},{-1.0,1.0,1.0}};
GLfloat colors[][3]={{0.0,0.0,0.0},{0.0,0.0,1.0},{0.0,1.0,0.0},{0.0,1.0,1.0},{1.0,0.0,0.0},
{1.0,0.0,1.0},{1.0,1.0,0.0},{1.0,1.0,1.0}};
static GLfloat theta[]={0.0,0.0,0.0};
static GLint axis=2;
static GLdouble viewer[]={0,0,5};
void polygon(int a,int b,int c,int d)
{
glBegin(GL_POLYGON);
glColor3fv(colors[a]);
glVertex3fv(vertices[a]);
glColor3fv(colors[b]);
glVertex3fv(vertices[b]);
glColor3fv(colors[c]);
glVertex3fv(vertices[c]);
glColor3fv(colors[d]);
glVertex3fv(vertices[d]);
glEnd();
}
void colorcube()
{
polygon(0,3,2,1);
polygon(2,3,7,6);
polygon(1,2,6,5);
polygon(0,4,5,1);
polygon(4,5,6,7);
polygon(0,3,7,4);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef(theta[0],1.0,0.0,0.0);
glRotatef(theta[1],0.0,1.0,0.0);
glRotatef(theta[2],0.0,0.0,1.0);
colorcube();
glFlush();
glutSwapBuffers();
}
void spincube()
{
theta[axis]+=2.0;
if(theta[axis]>360.0)
theta[axis]-=360.0;
glutPostRedisplay();
}
void mouse(int btn,int state,int x,int y)
{
if(btn==GLUT_LEFT_BUTTON&&state==GLUT_DOWN)
axis=0;
if(btn==GLUT_MIDDLE_BUTTON&&state==GLUT_DOWN)
axis=1;
if(btn==GLUT_RIGHT_BUTTON&&state==GLUT_DOWN)
axis=2;
spincube();
}
void myreshape(int w,int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h)
glOrtho(-2.0,2.0,-2.0*(GLfloat)h/(GLfloat)w,2.0*(GLfloat)h/(GLfloat)w,-10.0,10.0);
else
glOrtho(-2.0*(GLfloat)w/(GLfloat)h,2.0*(GLfloat)w/(GLfloat)h,-2.0,2.0,-10.0,10.0);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutCreateWindow("Color cube");
glutReshapeFunc(myreshape);
glutDisplayFunc(display);
glutIdleFunc(spincube);
glutMouseFunc(mouse);
glEnable(GL_DEPTH_TEST);
glClearColor(0.0,0.0,0.0,0.0);
glutMainLoop();
}
RUN:
gcc 3.c -lglut -lGL -lGLU
./a.out
SAMPLE OUTPUT:
Program Outcome:
1.4.1PREAMBLE
Concepts Used:
Data structures for representation of a cube
Perspective viewing
Defining and moving the camera
Input functions using Keyboard and mouse
Algorithm:
1. Modeling a color cube with simple data structures
Define vertices with centre of cube as origin
Define normals to identify faces
Define colors
2. Camera Manipulations
Define initial camera position - take care to define outside the cube.
3. Callback Function
Handling mouse inputs - use the 3 mouse buttons to define the 3 axes of rotation
Handling Keyboard Inputs - use the 3 pairs of keys to move the viewer along +ive and
-ive directions of the X and Y axes respectively.
Pseudo Code
1. Define global arrays for vertices and colors
GLfloat vertices[][3] = {{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0}, {1.0,1.0,-1.0},
{-1.0,1.0,-1.0}, {-1.0,-1.0,1.0}, {1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-
1.0,1.0,1.0}};
GLfloat colors[][3] = {{0.0,0.0,0.0},{1.0,0.0,0.0},
{1.0,1.0,0.0}, {0.0,1.0,0.0}, {0.0,0.0,1.0}, {1.0,0.0,1.0},
{1.0,1.0,1.0}, {0.0,1.0,1.0}};
2. Draw a polygon from a list of indices into the array vertices and use color corresponding to first
index
polygon(2,3,7,6);
polygon(0,4,7,3);
polygon(1,2,6,5);
polygon(4,5,6,7);
polygo n(0,1,5,4);
}
4. Intialize the theta value and initial viewer location and axis static GLfloat theta[]={0.0,0.0,0.0}
static Glint axis =2;
static GLdouble viewer[]={0.0,0.0,5,0}
Program 4: Draw a color cube and allow the user to move the camera suitably to experiment
with perspective viewing.
Program Objective:
creation of 3D objects
Create 3D-color cube with basic transformation operations like rotation.
Use mathematical and theoretical principles i.e transformation matrices of computer graphics
to draw and rotate the color cube object.
Use matrix algebra in computer graphics and implement fundamental algorithms and
transformations involved in viewing models.
#include<GL/glut.h>
#include<stdio.h>
GLfloat vertices[][3]={{-1.0,-1.0,-1.0},
{1.0,-1.0,-1.0},
{1.0,1.0,-1.0},
{-1.0,1.0,-1.0},
{-1.0,-1.0,1.0},
{1.0,-1.0,1.0},
{1.0,1.0,1.0},
{-1.0,1.0,1.0}};
GLfloat colors[][3]={{0.0,0.0,0.0},
{0.0,0.0,1.0},
{0.0,1.0,0.0},
{0.0,1.0,1.0},
{1.0,0.0,0.0},
{1.0,0.0,1.0},
{1.0,1.0,0.0},
{1.0,1.0,1.0}};
static GLfloat theta[]={0.0,0.0,0.0};
static GLint axis=2;
static GLdouble viewer[]={0,0,5};
void polygon(int a,int b,int c,int d)
{
glBegin(GL_POLYGON);
glColor3fv(colors[a]);
glVertex3fv(vertices[a]);
glColor3fv(colors[b]);
glVertex3fv(vertices[b]);
glColor3fv(colors[c]);
glVertex3fv(vertices[c]);
glColor3fv(colors[d]);
glVertex3fv(vertices[d]);
glEnd();
}
void colorcube()
{
polygon(0,3,2,1);
polygon(2,3,7,6);
polygon(1,2,6,5);
polygon(0,4,5,1);
polygon(4,5,6,7);
polygon(0,3,7,4);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(viewer[0],viewer[1],viewer[2],0,0,0,0,1,0);
glRotatef(theta[0],1.0,0.0,0.0);
glRotatef(theta[1],0.0,1.0,0.0);
glRotatef(theta[2],0.0,0.0,1.0);
colorcube();
glFlush();
glutSwapBuffers();
}
void myreshape(int w,int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h)
glFrustum(-2,2,-2*(GLfloat)h/(GLfloat)w,2*(GLfloat)h/(GLfloat)w,2,20);
else
glFrustum(-2*(GLfloat)w/(GLfloat)h,2*(GLfloat)w/(GLfloat)h,-2,2,2,20);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
}
void myMouse(int btn,int state,int x,int y)
{
if(btn==GLUT_LEFT_BUTTON&&state==GLUT_DOWN)
axis=0;
if(btn==GLUT_RIGHT_BUTTON&&state==GLUT_DOWN)
axis=1;
if(btn==GLUT_MIDDLE_BUTTON&&state==GLUT_DOWN)
axis=2;
theta[axis]+=2.0;
if(theta[axis]>360.0)
theta[axis]-=360.0;
display();
}
void keys(unsigned char key,int x,int y)
{
if(key=='x') viewer[0]-=1;
if(key=='X') viewer[0]+=1;
if(key=='y') viewer[1]-=1;
if(key=='Y') viewer[1]+=1;
if(key=='z') viewer[2]-=1;
if(key=='Z') viewer[2]+=1;
display();
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutCreateWindow("colorcube");
glutReshapeFunc(myreshape);
glutDisplayFunc(display);
glutMouseFunc(myMouse);
glutKeyboardFunc(keys);
glEnable(GL_DEPTH_TEST);
glClearColor(0.0,0.0,0.0,0.0);
glutMainLoop();
}
RUN:
gcc 4.c -lglut -lGL -lGLU
./a.out
SAMPLE OUTPUT:
Program Outcome:
1.4 Cohen-Sutherland
Clip a line using Cohen-Sutherland algorithm.
1.5.1 PREAMBLE
The Cohen-Sutherland algorithm uses a divide-and-conquer strategy. The line segment's endpoints
are tested to see if the line can be trivially accepted or rejected. If the line cannot be trivally
accepted or rejected, an intersection of the line with a window edge is determined and the trivial
reject/accept test is repeated.
This process is continued until the line is accepted. To perform the trivial acceptance and rejection
tests, we extend the edges of the window to divide the plane of the window into the nine regions.
Each end point of the line segment is then assigned the code of the region in which it lies.
Concepts Used:
Data structures for representing a square and line
Algorithm:
Every line endpoint is assigned a 4 bit Region code. The appropriate bit is set depending on the
location of the endpoint with respect to that window component as shown below:
Endpoint Left of window then set bit 1 Endpoint Right of window then set bit 2 Endpoint Below
window then set bit 3 Endpoint Above window then set bit 4
2. Compute the 4-bit codes for each endpoint. If both codes are 0000,(bitwise OR of the codes
yields 0000 ) line lies completely inside the window: pass the endpoints to the draw routine.
If both codes have a 1 in the same bit position (bitwise AND of the codes is not 0000), the line lies
outside the window. It can be trivially rejected.
3. If a line cannot be trivially accepted or rejected, at least one of the two endpoints must lie outside
the window and the line segment crosses a window edge. This line must be clipped at the window
edge before being passed to the drawing routine.
4. Examine one of the endpoints, say. Read’s 4-bit code in order: Left-to-Right, Bottom-to-Top.
5. When a set bit (1) is found, compute the intersection I of the corresponding window edge with
the line from to. Replace with i and repeat the algorithm.
1.6
Example1
Can determine the bit code by testing the endpoints with window as follows:
If x is less than Xwmin then set bit 1
If x is greater than Xwmax then set bit 2
If y is less than Ywmin then set bit 3
If y is greater than Ywmax then set bit 4
Note: can use 4 element Boolean matrix and set C [Left] = true / false (1/0). If both endpoints
= 0000 (in window) then display line. If both endpoints have a bit set in same position (P7,
P8) then the line is completely outside the window and is rejected.So: can do logical AND of
region codes and reject if result is 0000 Can do logical OR of region codes and accept if
result = 0000
For the rest of the lines we must check for intersection with window. May still be outside, e.g.
P3 - P4 in the above image. If point is to Left of window then compute intersection with Left
window boundary. Do the same for Right, Bottom, and Top.
Example2:
Program Objective:
creation of 2D objects
To have the detailed knowledge of the graphics Cohen-Sutherland line-clipping
algorithm.
Use mathematical and theoretical principles of computer graphics to draw the and clip the
line object.
Implement GLU and GLUT functions
/* Cohen-Sutherland */
#include<GL/glut.h>
#include<stdio.h>
#define outcode int
const int L=1,R=2,B=4,T=8;
float x0,y00,x1,y11;
float xmin=50.0,ymin=50.0,xmax=150.0,ymax=150.0,xvmin=200,yvmin=200,xvmax=300,
yvmax=300;
x=x0+(ymax-y00)*(x1-x0)/(y11-y00);
y=ymax;
}
else if(outcodeout&B)
{
x=x0+(ymin-y00)*(x1-x0)/(y11-y00);
y=ymin;
}
else if(outcodeout&R)
{
y=y00+(xmax-x0)*(y11-y00)/(x1-x0);
x=xmax;
}
else
{
y=y00+(xmin-x0)*(y11-y00)/(x1-x0);
x=xmin;
}
if(outcodeout==outcode0)
{
x0=x;
y00=y;
outcode0=compute(x0,y00);
}
else
{
x1=x,y11=y;
outcode1=compute(x1,y11);
}
}
}
while(!done);
if(accept)
{
float sx,sy,vx0,vx1,vy00,vy11;
sx=(xvmax-xvmin)/(xmax-xmin);
sy=(yvmax-yvmin)/(ymax-ymin);
vx0=xvmin+(x0-xmin)*sx;
vy00=yvmin+(y00-ymin)*sy;
vx1=xvmin+(x1-xmin)*sx;
vy11=yvmin+(y11-ymin)*sy;
glColor3f(0.0,1.0,0.0);
glBegin(GL_LINE_LOOP);
glVertex2f(xvmin,yvmin);
glVertex2f(xvmax,yvmin);
glVertex2f(xvmax,yvmax);
glVertex2f(xvmin,yvmax);
glEnd();
glColor3f(0.0,0.0,1.0);
glBegin(GL_LINES);
glVertex2f(vx0,vy00);
glVertex2f(vx1,vy11);
glEnd();
}
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glBegin(GL_LINE_LOOP);
glVertex2f(xmin,ymin);
glVertex2f(xmax,ymin);
glVertex2f(xmax,ymax);
glVertex2f(xmin,ymax);
glEnd();
glColor3f(0.0,0.0,1.0);
glBegin(GL_LINES);
glVertex2f(x0,y00);
glVertex2f(x1,y11);
glEnd();
cohen(x0,y00,x1,y11);
glFlush();
}
void init(){
glClearColor(0.0,0.0,0.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,499.0,0.0,499.0);
}
RUN:
gcc 5.c -lglut -lGL -lGLU
./a.out
Program Outcome:
To draw a simple shaded scene consisting of a tea pot on a table. Define suitably the position
and properties of the light source along with the properties of the surfaces of the solid object
used in the scene.
1.6.1 PREAMBLE
Concepts Used:
Translation, Scaling
Material Properties, Light Properties
Depth in 3D Viewing
Using predefined GLU library routines
Algorithm:
Algorithm[High Level]:
1. Recognise the objects in the problem domain and their components
Teapot
Table
Table top
4Legs
Walls
Left wall
Right wall
Floor
2. If depth testing is NOT used ensure that the background is drawn first
3. Enable lighting first - since lighting is applied at the time objects are drawn.
4. Draw the objects.
Algorithm[Low Level]:
Main Function:
Initialization for Display, Mode , Window
Enable lighting, shading, depth test
Register display and callback function
Define viewport appropriately
Call mainloop
Display function:
Define Material properties
Define lighting properties
Set the camera by defining
projection parameters
camera parameters
Plan the required centre position for each of the components
Draw each component using translation, scaling and rotation as required
Pseudo Code
6. Define the main function to create window and enable lighting function using
glEnable(GL_LIGHTING)
Program 6: To draw a simple shaded scene consisting of a tea pot on a table. Define
suitably the position and properties of the light source along with the properties of the
surfaces of the solid object used in the scene.
Program Objective:
creation of 3D objects
Use 3D teapot objects to draw a simple shaded scene consisting of a tea pot on a table.
Use matrix algebra in computer graphics and implement fundamental algorithms and
transformations involved in viewing models.
/* Tea pot */
#include<GL/glut.h>
void wall(double thickness)
{
glPushMatrix();
glTranslated(0.5,0.5*thickness,0.5);
glScaled(1.0,thickness,1.0);
glutSolidCube(1.0);
glPopMatrix();
}
void tableleg(double thick,double len)
{
glPushMatrix();
glTranslated(0,len/2.0,0);
glScaled(thick,len,thick);
glutSolidCube(1.0);
glPopMatrix();
}
void table(double topwid,double topthick,double legthick,double leglen)
{
glPushMatrix();
glTranslated(0,leglen,0);
glScaled(topwid,topthick,topwid);
glutSolidCube(1.0);
glPopMatrix();
double dist=0.95*topwid/2-legthick/2;
glPushMatrix();
glTranslated(dist,0,dist);
tableleg(legthick,leglen);
glTranslated(0,0,-2*dist);
tableleg(legthick,leglen);
glTranslated(-2*dist,0,2*dist);
tableleg(legthick,leglen);
glTranslated(0,0,-2*dist);
tableleg(legthick,leglen);
glPopMatrix();
}
void display()
{
GLfloat mat_ambient[]={0.7f,0.7f,0.7f,1.0f};
GLfloat mat_diffuse[]={0.5f,0.5f,0.5f,1.0f};
GLfloat mat_specular[]={1.0f,1.0f,1.0f,1.0f};
GLfloat mat_shininess[]={50.0f};
glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambient);
glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
GLfloat Lightintensity[]={1.0f,1.0f,1.0f,1.0f};
GLfloat Lightposition[]={2.0f,6.0f,3.0f,0.0f};
glLightfv(GL_LIGHT0,GL_POSITION,Lightposition);
glLightfv(GL_LIGHT0,GL_DIFFUSE,Lightintensity);
double winht=1.0;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-winht*64/48.0,winht*64/48.0,-winht,winht,0.1,100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(2.3,1.3,2.0,0.0,0.25,0.0,0.0,1.0,0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslated(0.5,0.38,0.5);
glRotated(60,0,1,0);
glutSolidTeapot(0.08);
glPopMatrix();
glPushMatrix();
glTranslated(0.4,0,0.4);
table(0.6,0.02,0.02,0.3);
glPopMatrix();
wall(0.02);
glPushMatrix();
glRotated(90.0,0.0,0.0,1.0);
wall(0.02);
glPopMatrix();
glPushMatrix();
glRotated(-90.0,1.0,0.0,0.0);
wall(0.02);
glPopMatrix();
glFlush();
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB |GLUT_DEPTH);
glutCreateWindow("Tea pot");
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutDisplayFunc(display);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glClearColor(0.0,0.0,0.0,0.0);
glutMainLoop();
}
RUN:
gcc 6.c -lglut -lGL -lGLU
./a.out
SAMPLE OUTPUT:
Program Outcome:
1.7.1 PREAMBLE
Sierpinski's Triangle is a very famous fractal that's been seen by most advanced math students.
This fractal consists of one large triangle, which contains an infinite amount of smaller triangles
within. The infinite amount of triangles is easily understood if the fractal is zoomed in many
levels. Each zoom will show yet more previously unseen triangles embedded in the visible ones.
Creating the fractal requires little computational power. Even simple graphing calculators can
easily make this image. The fractal is created pixel by pixel, using random numbers; the fractal
will be slightly different each time due to this. Although, if you were to run the program
repeatedly, and allow each to use an infinite amount of time, the results would be always
identical. No one has an infinite amount of time, but the differences in the finite versions are
very small.
A fractal is generally "a rough or fragmented geometric shape that can be split into parts, each
of which is (at least approximately) a reduced-size copy of the whole".
We begin with a triangle in the plane and then apply a repetitive scheme of operations to it
(when we say triangle here, we mean a blackened, filled-in' triangle). Pick the midpoints of its
three sides. Together with the old vertices of the original triangle, these midpoints define four
congruent triangles of which we drop the center one. This completes the basic construction
step. In other words, after the first step we have three congruent triangles whose sides have
exactly half the size of the original triangle and which touch at three points which are common
vertices of two contiguous triangles. Now we follow the same procedure with the three
remaining triangles and repeat the basic step as often as desired. That is, we start with one
triangle and then produce 3, 9, 27, 81, 243, triangles, each of which is an exact scaled down
version of the triangles in the preceding step.
Concepts Used:
Data structures for representing 3D vertices
Tetrahedron sub-division using mid-points
Algorithm:
Input: Four 3D vertices of tetrahedron, no. of divisions
Recursively sub-divide the each triangle by finding the mid-point Pseudo Code
2. Define and initialize initial location inside tetrahedron. GLfloat p[3] = {25.0,10.0,25.0};
3. Define Triangle function that uses points in three dimensions to display one triangle
Use glBegin(GL_POLYGON)
glVertex3fv(a) glVertex3fv(b)
glVertex3fv(c) to display points. glEnd();
4. Subdivide a tetrahedron using divide_triangle function
Program Objective:
creation of 3D objects
Implement GLU and GLUT functions
Create Sierpinski gasket
Implement the concepts of recursion to create Sierpinski gasket
/* 3 D Sierpinski gasket */
#include <GL/glut.h>
#include <stdio.h>
typedef float point[3];
point v[] = {{0.0,0.0,1.0},{0.0,1.0,0.0},{-1.0,-0.5,0.0},{1.0,-0.5,0.0}};
int n;
void triangle(point a, point b, point c)
{
glBegin(GL_POLYGON);
glVertex3fv(a);
glVertex3fv(b);
glVertex3fv(c);
glEnd();
}
void divide(point a, point b, point c,int m)
{
point v1, v2, v3;
int j;
if(m > 0)
{
for(j = 0; j < 3; j++)
v1[j] = (a[j] + b[j]) / 2;
for(j = 0; j < 3; j++)
v2[j] = (a[j] + c[j]) / 2;
for(j=0; j<3; j++)
v3[j] = (b[j] + c[j]) / 2;
divide(a, v1, v2, m - 1);
divide(c, v2, v3, m - 1);
divide(b, v3, v1, m - 1);
}
else(triangle(a, b, c));
}
void tetra(int m)
{
glColor3f(1.0, 0.0, 0.0);
divide(v[0], v[1], v[2], m);
glColor3f(0.0, 1.0, 0.0);
divide(v[3], v[2], v[1], m);
glColor3f(0.0, 0.0, 1.0);
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
tetra(n);
glFlush();
}
RUN:
gcc 7.c -lglut -lGL -lGLU
./a.out
SAMPLE OUTPUT:
Program Outcome:
1.8.1 PREAMBLE
Bezier curve is discovered by the French engineer Pierre Bezier. These curves can be
generated under the control of other points. Approximate tangents by using control points are
used to generate curve. The Bezier curve can be represented mathematically as
Where pi is the set of points and Bni(t) represents the Bernstein polynomials which
are given by
Bezier curves generally follow the shape of the control polygon, which consists of the
segments joining the control points and always pass through the first and last control points.
They are contained in the convex hull of their defining control points. The degree of the
polynomial defining the curve segment is one less that the number of defining polygon point.
Therefore, for 4 control points, the degree of the polynomial is 3, i.e. cubic polynomial. A
Bezier curve generally follows the shape of the defining polygon. The direction of the
tangent vector at the end points is same as that of the vector determined by first and last
segments. The convex hull property for a Bezier curve ensures that the polynomial smoothly
follows the control points. No straight line intersects a Bezier curve more times than it
intersects its control polygon. They are invariant under an affine transformation. Bezier
curves exhibit global control means moving a control point alters the shape of the whole
curve. A given Bezier curve can be subdivided at a point t=t0 into two Bezier segments
which join together at the point corresponding to the parameter value t=t0.
Concepts Used: Algorithm: OpenGL Commands Familiarized:
glMatrixMode
glLoadIdentity
gluOrtho2D
glFlush
glColor3f
glBegin
Program 8: Develop a menu driven program to animate a flag using Bezier Curve
Program Objective:
creation of 2D objects
Use mathematical and theoretical principles of computer graphics to animate a flag using
Bezier Curve algorithm
Implement GLU and GLUT functions
/* Bezier Curve */
#include<GL/glut.h>
#include<stdio.h>
#include<math.h>
#define pi 3.14
struct
{
GLfloat x,y,z;
}
typedef wcpt;
void display()
{
GLint nctrpt=4,nBezcurpts=20;
static float theta=0;
wcpt ctrlpt[4]={{20,100,0},{30,110,0},{50,90,0},{60,100,0}};
ctrlpt[1].x+=10*sin(theta*pi/180);
ctrlpt[1].y+=5*sin(theta*pi/180);
ctrlpt[2].x-=10*sin((theta+30)*pi/180);
ctrlpt[2].y-=10*sin((theta+30)*pi/180);
ctrlpt[3].x-=4*sin(theta*pi/180);
ctrlpt[3].y-=sin((theta+30)*pi/180);
theta+=3;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,1.0,1.0);
glPushMatrix();
glLineWidth(5);
glColor3f(1,0,0.15);
for(int i=0;i<8;i++)
{
glTranslatef(0,-0.8,0);
bezier(ctrlpt,nctrpt,nBezcurpts);
}
glColor3f(1,1,1);
for(int i=0;i<8;i++)
{
glTranslatef(0,-0.8,0);
bezier(ctrlpt,nctrpt,nBezcurpts);
}
glColor3f(0,1,0);
for(int i=0;i<8;i++)
{
glTranslatef(0,-0.8,0);
bezier(ctrlpt,nctrpt,nBezcurpts);
}
glPopMatrix();
glColor3f(0.7,0.5,0.3);
glLineWidth(5);
glBegin(GL_LINES);
glVertex2f(20,100);
glVertex2f(20,40);
glEnd();
glFlush();
glutPostRedisplay();
glutSwapBuffers();
}
SAMPLE OUTPUT
Program Outcome:
1.9 Scan-line
Develop a menu driven program to fill the polygon using scan line algorithm.
1.9.1 PREAMBLE
Special cases to be handled: i. Horizontal edges should be excluded ii. For vertices lying on
scanlines, i. count twice for a change in slope. ii. Shorten edge by one scanline for no change
in slope
Algorithm:
1. Set y to the smallest y coordinate that has an entry in the ET; i.e, y for the first nonempty
bucket.
2. Initialize the AET to be empty.
3. Repeat until the AET and ET are empty:
3.1 Move from ET bucket y to the AET those edges whose y_min = y (entering edges).
3.2 Remove from the AET those entries for which y = y_max (edges not
involved in the next scanline), the sort the AET on x (made easier because ET is
presorted).
Extensions:
1. Multiple overlapping polygons - priorities
2. Color, patterns Z for visibility
// Function scanfill
Inputs: vertices of the polygon.
Output: filled polygon.
Processing :
1. Initialize array LE to 500 and RE to 0 for all values of y(0-->499)
2. Call function EDGEDETECT for each edge of the polygon one by one to set the value of x
for each value of y within that line.
3. For each value of y in the screen draw the pixels for every value of x provided. It is greater
than right edge value of x.
//function Display
Inputs: Globally defined vertices of Polygon
Output: Filled polygon display
Processing:
1. Draw the polygon using LINE,LOOP
2. Fill the polygon using SCANFILL
//Function EDGEDETECT:
Inputs:
1. End co-ordinates of edge
2. Address of LE and RE
3. Output: The updated value of x for left edge of the polygon and the right edge of the
Polygon.
Processing:
1. Find the inverse of the slope.
2. Starting from the lesser integer value of y to the greater integer value of y.
3. Compute the value of x for left edge and right edge and update the value of
x for both the edges.
Program 9: Develop a menu driven program to fill the polygon using scan line
algorithm
Program Objective:
creation of 2D objects
To have the detailed knowledge of the graphics scan-line area filling algorithm
Use mathematical and theoretical principles of computer graphics to draw and fill colors
to the object.
Implement GLU and GLUT functions
/* Scan-line */
#include<GL/glut.h>
float x1,x2,x3,x4,y1,y2,y3,y4;
void draw_pixel(int x,int y)
{
glColor3f(1.0,0.0,1.0);
glPointSize(1.0);
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
}
void edgedetect(float x1,float y1,float x2,float y2,int *le,int *re)
{
float temp,x,mx;
int i;
if(y1>y2)
{
temp=x1,x1=x2,x2=temp;
temp=y1,y1=y2,y2=temp;
}
if(y1==y2)
mx=x2-x1;
else
mx=(x2-x1)/(y2-y1);
x=x1;
for(i=(int)y1;i<(int)y2;i++)
{
if(x<(float)le[i])
le[i]=(int)x;
if(x>(float)re[i])
re[i]=(int)x;
x+=mx;
}
}
void Scanfill(float x1,float y1,float x2,float y2,float x3,float y3,float x4,float y4)
{
int le[500],re[500],i,j;
for(i=0;i<500;i++)
le[i]=500,re[i]=0;
edgedetect(x1,y1,x2,y2,le,re);
edgedetect(x2,y2,x3,y3,le,re);
edgedetect(x3,y3,x4,y4,le,re);
edgedetect(x4,y4,x1,y1,le,re);
for(j=0;j<500;j++)
{
if(le[j]<=re[j])
for(i=le[j];i<re[j];i++)
draw_pixel(i,j);
}
}
void filloption(GLint selectedoption)
{
switch(selectedoption)
{
case 1:
Scanfill(x1,y1,x2,y2,x3,y3,x4,y4);
break;
case 2:
exit(0);
break;
}
}
void Reshape(GLint w,GLint h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,500,0,500);
glutPostRedisplay();
glFlush();
}
void display()
{
x1=250.0,y1=200.0,x2=150.0,y2=300.0,x3=250.0,y3=400.0,x4=350.0,y4=300.0;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,0.0,1.0);
glBegin(GL_LINE_LOOP);
glVertex2f(x1,y1);
glVertex2f(x2,y2);
glVertex2f(x3,y3);
glVertex2f(x4,y4);
glEnd();
glFlush();
}
glutDisplayFunc(display);
glutCreateMenu(filloption);
glutAddMenuEntry("Scanfill",1);
glutAddMenuEntry("exit",2);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutReshapeFunc(Reshape);
glClearColor(0.0,0.0,0.0,0.0);
glutMainLoop();
}
RUN:
gcc 9.c -lglut -lGL -lGLU
./a.out
SAMPLE OUTPUT
Program Outcome:
One Three-Dimensional OpenGL Graphics Project using features from at least THREE
CATEGORIES listed below:
Category I
Input and Interaction
Menus, Display Lists
Category II
Transformations
Camera Movement
Category III
Coloring
Texturing
Lighting/ Shading
Category IV
Animation
Hidden Surface Removal
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
Computer Graphics Laboratory With Mini Project 15CSL68
2. What is OpenGL?
Answer: OpenGL is the most extensively documented 3D graphics API (Application Pro-gram
Interface) to date. It is used to create Graphics.
3. What is GLUT?
Answer: The OpenGL Utility Toolkit (GLUT) is a library of utilities for OpenGL programs,
which primarily perform system-level I/O with the host operating system.
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
Computer Graphics Laboratory With Mini Project 15CSL68
scanline upward. For each line, any pixels that contain an intersection between this scanline and
an edge of the polygon are filled in. Then, the algorithm progresses along the scanline, turning on
when it reaches a polygon pixel and turning o when it reaches another one, all the way across the
scanline.
handle_resize
{
glViewport(...);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Set projection transform with glOrtho, glFrustum, gluOrtho2D, gluPerspective, etc
handle_refresh
{
glClear(...);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
Computer Graphics Laboratory With Mini Project 15CSL68
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
Computer Graphics Laboratory With Mini Project 15CSL68
appropriate eld of view, and keeps the zNear and zFar clipping planes at an appropriate range. An
application that displays molecules in micron scale, for example, would probably not want to
place the viewer at a distance of 10 feet with a 60 degree eld of view.
19. What does the .gl or .GL le format have to do with OpenGL?
Answer: .gl les have nothing to do with OpenGL, but are sometimes confused with it. .gl is a le
format for images, which has no relationship to OpenGL.
20. Who needs to license OpenGL? Who doesn't? Is OpenGL free software?
Answer: Companies which will be creating or selling binaries of the OpenGL library will need to
license OpenGL. Typical examples of licensees include hardware vendors, such as Digital
Equipment, and IBM who would distribute OpenGL with the system software on their
workstations or PCs. Also, some software vendors, such as Portable Graphics and Template
Graphics, have a business in creating and distributing versions of OpenGL, and they need to
license OpenGL. Applications developers do NOT need to license OpenGL. If a developer wants
to use OpenGL that developer needs to obtain copies of a linkable OpenGL library for a
particular machine. Those OpenGL libraries may be bundled in with the development and/or
run-time options or may be purchased from a third-party software vendor, without licensing the
source code or use of the OpenGLtrademark.
Windows created by glutCreateWindow will be requested to be created with the current initial
window position and size. The intent of the initial window position and size values is to provide a
suggestion to the window system for a window's initial size and position. The window system is
not obligated to use this information. Therefore, GLUT programs should not assume the window
was created at the specied size or position. A GLUT program should use the window's reshape
callback to determine the true size of the window.
Download & Share VTU Connect App Now From Google Play Store
Download & Share VTU Connect App Now From Google Play Store
Computer Graphics Laboratory With Mini Project 15CSL68
24. Describe the usage of glutMainLoop?
Answer: void glutMainLoop(void);
glutMainLoop enters the GLUT event processing loop. This routine should be called at most once
in a GLUT program. Once called, this routine will never return. It will call as necessary any
callbacks that have been registered.
Download & Share VTU Connect App Now From Google Play Store