Upload
dinhtuong
View
289
Download
19
Embed Size (px)
Citation preview
P a g e | 1
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
INDEX
S.NO TOPICS PAGE NO.
1. Write the steps to Setting up the VC++ environment for Open
GL.
1-3
2. Briefly describe architecture of OpenGL. 3-4
3. Describe the features supported by GLUT (OpenGL Utility
Toolkit).
5
4. WAP to draw a Red Triangle on a Blue background. 6-7
5. WAP to show various OpenGL primitives with the help of
menu e.g. GL_POINTS, GL_LINES,
GL_LINE_STRIP,GL_LINE_LOOP GL_TRIANGLES
GL_TRIANGLE_STRIP GL_TRIANGLE_FAN,
GL_QUADS, GL_QUAD_STRIP. (use glutCreateMenu
Function).
8-19
6. Write a program to draw the following points: (0.0,0.0),
(20.0,0.0), (20.0,20.0), (0.0,20.0) and (10.0,25.0). For this
purpose, use the GL_POINTS primitive.
20-22
7. Rewrite the previous program in order to draw a house. The
house consists of two figures: a square and a triangle. The
first four points given above define the square, while the last
three points define the triangle. For this purpose, use the
GL_QUADS and GL_TRIANGLES primitives. Afterwards
construct two windows and the door of the house.
23-25
8. WAP to create multiple viewport on screen. 26-29
9. WAP to draw line using DDA line drawing algorithm
30-33
10. WAP to draw line using Bresenham Line drawing algorithm. 34-37
11. WAP to draw circle using Mid-point circle drawing
algorithm.
38-41
12. Write a program to draw the Sin and Cos curve using menu. 42-46
13. 1. Write a program to draw a circle centered at (cx,cy)
with a given radius r, whose points are given by the
following equations
47-49
P a g e | 2
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
x=cx+r.cos(θ)
y=cy+r.sin(θ)
14. WAP Program to implement basic transformations (
Translation, scaling, rotation) using Menu driven program.
50-56
15. WAP to show 3D shapes available in OpenGL. 57-72
16. WAP to draw the chess board. 73-76
17. WAP to perform point and line clipping in OpenGL. 77-84
P a g e | 3
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
#1.Write the steps to Setting up the VC++ environment for
Open GL.
Setting up OpenGL environment for Microsoft Visual C++
Before starting the setting up OpenGL environment, please do the following
first:
1. Install Microsoft Visual C++ 6.0 (Comes with Ms Visual Studio 6.0)
2. Download the file called “opengl95.exe” and “glutdlls.zip” from internet.
[Just open browser-> navigatehttp://www.google.com -> type upper mentioned
file name and search -> you will definitely find the appropriate link to
download.]
3. Run opengl95.exe file and it will be extracted and will be generated some
files. Copy those files and
4. Extract the file “glutdlls.zip” and it will also generate some more files.
5. Now, combine all extracted files from two sources (opengl95.exe and
glutdlls.zip) and place in one location.
Setting up OpenGL environment for Microsoft Visual C++
With any system, you can start with a C\C++ compiler and install appropriate
OpenGL header files (.h) and libraries. Three libraries associated with header
files are required for the use of OpenGL:
OpenGL (The basic API tool)
GLU (the OpenGL Utility Library)
GLUT (the OpenGL utility Toolkit)
Typically, files are associated with each library are:
Header Files (.h)
Library Files: Static Library Files (.lib) and Dynamic Linked
Library (.dll).
Adding Header Files: We can place (add) the three header files: Gl.h,
Glu.h, Glut.h in a subdirectory of the include directory of your
compiler. Thus, in each application program we may write the
following include statements:
#include<gl/Gl.h>
#include<gl/Glu.h>
#include<gl/Glut.h>
P a g e | 4
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
Adding Library Files: Now, you have to add the appropriate OpenGL
library (.lib and .dll) files to your project so that the linker can find
them, in order to run your program successfully.
Ms. Visual C++ (5.0 or later) is a suitable environment for using OpenGL. If
you are working with Ms Visual C++ i.e., “win32 console application”, there is
no need to build a Graphical User Interface (GUI): as the input from the
keyboard and output of text take place through the separate console window.
When upper mentioned files have been downloaded, copy all extracted files and
place those into following:
1. C:\Program files\Microsoft Visual Studio\VC98\lib\
2. C:\Windows\System32\
Now, Restart your pc….
NOW CREATING A PROJECT IN VC++ 6.0 AND EXECUTE AN
OPENGL APPLICATION:
Steps:
Open Ms. Visual C++ 6.0
Choose: File -> New
In the project tab choose: Win32 Console Application
Enter a unique project name eg. Myfirstopengl
Click on ok
Now Choose option “An empty project”
Click finish then “Ok”.
Now, go to “Project” menu -> Settings
Go to “Link” tab menu.
In the “Object/ Library modules” enter the names of the three .lib files
as
Glu32.lib glut32.lib opengl32.lib
Press Ok
Now, go to “File” -> “New”.
Choose “C++ Source File”
Name it. Eg. Firstgl
Press “Ok”
P a g e | 5
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
Write your code
Press “F7” to built
Press Ctrl+f5 to run
Another Simple method for Setting up the VC++ environment for
Open GL.
1. Check whether VC++ (Microsoft Visual Studio 6.0) is setup on your
machine/system.
2. Open the glut folder from the server.
3. Copy glut.dll, glu32.dll and glut32.dll into C:\WINDOWS\system32.
4. Copy glut32.lib,glut.lib into C:\Program Files\Microsoft Visual Studio\VC98\
into the lib folder.
5. Copy glut.h into C:\Program Files\Microsoft Visual Studio\VC98\ into the
INCLUDE folder.
#2.Briefly describe architecture of OpenGL.
It is a window system independent, operating system independent graphics
rendering API which is capable of rendering high-quality color images
composed of geometric and image primitives.
OpenGL is a library for doing computer graphics. By using it, you can create
interactive applications which render high-quality color images composed of 3D
geometric objects and images. As OpenGL is window and operating system
independent. As such, the part of your application which does rendering is
platform independent.
However, in order for OpenGL to be able to render, it needs a window to draw
into. Generally, this is controlled by the windowing system on whatever
platform you’re working on. Summarizing the above discussion, we can say
OpenGL is a software API to graphics hardware.
• Designed as a streamlined, hardware-independent interface to be implemented
on many different hardware platforms.
P a g e | 6
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
• Procedural interface.
• No windowing commands.
• No high-level commands.
P a g e | 7
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
#3.Describe the features supported by GLUT (OpenGL
Utility Toolkit).
GLUT (pronounced like the glut in gluttony) is the OpenGL Utility Toolkit, a
window system independent toolkit for writing OpenGL programs. It
implements a simple windowing application programming interface (API) for
OpenGL.
GLUT makes it considerably easier to learn about and
explore OpenGL programming. GLUT provides a portable API so you can write
a single OpenGL program that works across all PC and workstation OS
platforms.
GLUT is designed for constructing small to medium
SizedOpenGL programs. While GLUT is well-suited to learning OpenGL and
developing simple OpenGL applications, GLUT is not a full-featured toolkit so
large applications requiring sophisticated user interfaces are better off using
native window system toolkits. GLUT is simple, easy, and small.
The GLUT library has both C, C++ (same as C),
FORTRAN, and Ada programming bindings. The GLUT source code
distribution is portable to nearly all OpenGL implementations and platforms.
The current version is 3.7. Additional releases of the
library are not anticipated.
GLUT is not open source. Mark Kilgard maintains the
copyright. There are a number of newer and open source alternatives.
The current version of the GLUT API is 3.
The current source code distribution is GLUT 3.7.
The toolkit supports:
Multiple windows for OpenGL rendering
Callback driven event processing
Sophisticated input devices
An 'idle' routine and timers
A simple, cascading pop-up menu facility
Utility routines to generate various solid and wire frame objects
Support for bitmap and stroke fonts
Miscellaneous window management functions.
P a g e | 8
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
#4.WAP to draw a Red Triangle on a Blue background.
#include <windows.h> // for MS Windows
#include <GL/glut.h> // GLUT, include glu.h and gl.h
/* Handler for window-repaint event. Call back when the window first appears
and
whenever the window needs to be re-painted. */
void display() {
glClearColor(0.0f, 0.0f, 1.0f, 1.0f); // Set background color to blue
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer
(background)
// Draw a Red 1x1 triangle centered at origin
glBegin(GL_TRIANGLES);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f( 1.0f, 0.0f, 0.0f);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(0.0f,1.0f, 0.0f);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f( 0.0f,0.0f, 1.0f);
glEnd();
glFlush(); // Render now
}
P a g e | 9
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
/* Main function: GLUT runs as a console application starting at main() */
int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutCreateWindow("OpenGL Setup Test"); // Create a window with the given
title
glutInitWindowSize(320, 320); // Set the window's initial width & height
glutInitWindowPosition(50, 50); // Position the window's initial top-leftcorner
glutDisplayFunc(display); // Register display callback handler for window re-
paint
glutMainLoop(); // Enter the event-processing loop
return 0;
}
OUTPUT:
P a g e | 10
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
#5.WAP to show various OpenGL primitives with the help of
menu
e.g.GL_POINTS,GL_LINES,GL_LINE_STRIP,GL_LINE_LOO
P,GL_TRIANGLES,GL_TRIANGLE_STRIP,GL_TRIANGLE_
FAN, GL_QUADS, GL_QUAD_STRIP. (use glutCreateMenu
Function).
#include <GL/glut.h>
# include <iostream.h>
# include<stdlib.h>
void display()
{
/* clear window */
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
void OptionsMenu (GLint selectedOpt)
{
switch (selectedOpt)
{
case 1:
/* draw unit square polygon */
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glVertex2f(-0.5, -0.5);
P a g e | 11
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
/* flush GL buffers */
glFlush();
break;
case 2:
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINES);
glVertex2f(0.75,0.25);
glVertex2f(0.75,0.75);
glEnd();
/* flush GL buffers */
glFlush();
break;
case 3:
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(6);
glBegin(GL_POINTS);
glVertex2f(0.25,0.25);
glVertex2f(0.75,0.25);
glVertex2f(0.75,0.75);
P a g e | 12
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
glVertex2f(0.25,0.50);
glVertex2f(0.50,0.75);
glEnd();
/* flush GL buffers */
glFlush();
break;
case 4:
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINE_STRIP);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glEnd();
/* flush GL buffers */
glFlush();
break;
case 5:
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINE_LOOP);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.25,0.25);
glVertex2f(0.75,0.25);
glVertex2f(0.75,0.75);
glVertex2f(0.25,0.50);
P a g e | 13
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
glVertex2f(0.50,0.75);
glEnd();
/* flush GL buffers */
glFlush();
break;
case 6:
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glVertex2f(0.25,0.25);
glVertex2f(0.75,0.25);
glVertex2f(0.75,0.75);
glVertex2f(0.25,0.50);
glVertex2f(0.50,0.75);
glEnd();
/* flush GL buffers */
glFlush();
break;
case 7:
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLE_STRIP);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glColor3f(0.50, 0.25,0.70);
P a g e | 14
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
glVertex2f(0.25,0.25);
glVertex2f(0.75,0.25);
glVertex2f(0.75,0.75);
glColor3f(0.0, 0.25,0.0);
glVertex2f(0.25,0.50);
glVertex2f(0.50,0.75);
glEnd();
/* flush GL buffers */
glFlush();
break;
case 8:
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLE_FAN);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.25,0.25);
glVertex2f(0.75,0.25);
glEnd();
/* flush GL buffers */
glFlush();
break;
case 9:
P a g e | 15
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_QUADS);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
/* flush GL buffers */
glFlush();
break;
case 10:
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_QUAD_STRIP);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
/* flush GL buffers */
glFlush();
break;
default: break;
}
//glutPostRedisplay();
P a g e | 16
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
}
void init()
{
/* set clear color to black */
glClearColor(1.0, 0.0, 1.0, 1.50);
/* set fill color to white */
glColor3f(0.25, 0.25,0.50);
}
void main()
{
/* Initialize mode and open a window in upper left
corner of
/* screen */
/* Window title is name of program (arg[0]) */
//glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(20, 20);
glutCreateWindow("Different primitives");
init();
glutDisplayFunc(display);
glutCreateMenu (OptionsMenu);
glutAddMenuEntry("Polygon ",1);
P a g e | 17
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
glutAddMenuEntry("Line ",2);
glutAddMenuEntry("point ",3);
glutAddMenuEntry("Linestrip ",4);
glutAddMenuEntry("Lineloop ",5);
glutAddMenuEntry("Triangle ",6);
glutAddMenuEntry("Trianglestrip",7);
glutAddMenuEntry("Trianglefan",8);
glutAddMenuEntry("Quard ",9);
glutAddMenuEntry("Quardstrip",10);
glutAttachMenu (GLUT_RIGHT_BUTTON);
glutMainLoop();
}
P a g e | 18
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
OUTPUT:
P a g e | 19
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
P a g e | 20
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
P a g e | 21
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
P a g e | 22
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
#6.Write a program to draw the following points: (0.0,0.0),
(20.0,0.0), (20.0,20.0), (0.0,20.0) and (10.0,25.0). For this
purpose, use the GL_POINTS primitive.
#include <GL/glut.h>
#include <stdlib.h>
void draw()
{
glClear ( GL_COLOR_BUFFER_BIT );
glColor3f(1,0,0);
glBegin(GL_POINTS);
glVertex2f(0.0f,0.0f);
glVertex2f(20.0,0.0);
glVertex2f(20.0,20.0);
glVertex2f(0.0,20.0);
glVertex2f(10.0,25.50);
glEnd();
glFlush();
}
void initialize ()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glClearColor(0.0, 0.0, 1.0, 0.0);
P a g e | 23
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
glPointSize(5);
gluOrtho2D(0,500,0,500);
}
int main()
{
glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE);
glutInitWindowSize(500,500);
glutInitWindowPosition(0, 0);
glutCreateWindow("Program to draw Points");
initialize();
glutDisplayFunc(draw);
glutMainLoop();
return 0;
}
P a g e | 24
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
OUTPUT:
P a g e | 25
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
#7.Rewrite the previous program in order to draw a
house. The house consists of two figures: a square and a
triangle. The first four points given above define the
square, while the last three points define the triangle. For
this purpose, use the GL_QUADS and GL_TRIANGLES
primitives. Afterwards construct two windows and the
door of the house.
#include <GL/glut.h>
# include <iostream.h>
# include<stdlib.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_QUADS);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.45, -0.5);
glVertex2f(-0.45, 0.5);
glVertex2f(-0.5, 0.5);
glEnd();
glBegin(GL_QUADS);
glVertex2f(-0.5, -0.5);
glVertex2f(0.5,-0.5);
glVertex2f(0.5, -0.45);
glVertex2f(-0.5, -0.45);
glBegin(GL_QUADS);
glVertex2f(0.45,-0.5);
glVertex2f(0.45,0.5);
glVertex2f(0.5,0.5);
glVertex2f(0.5,-0.5);
glEnd();
glBegin(GL_QUADS);
glVertex2f(0,0.9);
glVertex2f(0.05,0.85);
glVertex2f(-0.45,0.45);
glVertex2f(-0.5,0.5);
P a g e | 26
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
glEnd();
glBegin(GL_QUADS);
glVertex2f(0,0.9);
glVertex2f(-0.05,0.85);
glVertex2f(0.45,0.45);
glVertex2f(0.5,0.5);
glEnd();
glBegin(GL_QUADS);
glVertex2f(-0.3,0.8);
glVertex2f(-0.35,0.8);
glVertex2f(-0.35,0.6);
glVertex2f(-0.3,0.65);
glEnd();
glBegin(GL_QUADS);
glVertex2f(0,0.2);
glVertex2f(0.3,0.2);
glVertex2f(0.3,0.5);
glVertex2f(0,0.5);
glEnd();
glBegin(GL_QUADS);
glVertex2f(0,0);
glVertex2f(-0.3,0);
glVertex2f(-0.3,-0.5);
glVertex2f(0,-0.5);
glEnd();
glFlush();
}
void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glColor3f(1.0, 1.0, 1.0);
}
void main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
P a g e | 27
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
glutCreateWindow( "HUT.cpp" );
glutDisplayFunc(display);
init();
glutMainLoop();
}
OUTPUT:
P a g e | 28
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
#8.WAP to create multiple viewport on screen.
#include <GL/glut.h>
//Header File ForTheOpenGLLibrary
//#include <GL/glu.h>
# include <iostream.h>
//#include <gl/ glut.h>
#include <stdlib.h>
void draw(){
// Make background colour yellow
glClearColor(100,100,100,0);
glClear(GL_COLOR_BUFFER_BIT);
//Sets upFIRSTviewportspanningtheleft-bottomquarterof the interface window
glViewport(0,0,250,250);
// Sets up the PROJECTION matrix
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
//gluOrtho2D(0.0,50.0,-10.0,70.0);
//also sets up worldwindow
// Draw BLUE rectangle
glColor3f(0,0,1);
glRectf(0.0,0.0,10.0,30.0);
//Sets up SECOND viewport spanning the right-top quarter of the interface
window
glViewport(250,0,250,250);
P a g e | 29
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
//Sets up the PROJECTION matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//gluOrtho2D(0.0,60.0,-10.0,40.0);
//also sets up world window // Draw RED rectangle
glColor3f(1,0,0);
glRectf(0.0,0.0,10.0,30.0);
//displayrectangles
glViewport(0,250,250,250);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//gluOrtho2D(0.0,60.0,-10.0,40.0);
//also sets up world window // Draw RED rectangle
glColor3f(0,1,0);
glRectf(0.0,0.0,10.0,30.0);
glutSwapBuffers();
}
//Keyboard method to allow ESC key to quit
void keyboard( unsigned char key,int x, int y)
{
if (key== 27)
P a g e | 30
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
exit(0);
}
int main( int argc , char ** argv )
{
glutInit(&argc, argv);
//DoubleBuffered RGB display
glutInitDisplayMode (GLUT_RGB|GLUT_DOUBLE);
//Set window size
glutInitWindowSize( 500 , 500);
glutCreateWindow("Three viewports spanning the left-bottom and right-top
quarters");
//Declare the display and keyboard functions
glutDisplayFunc(draw);
/*glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0,0.0,-50);
glRotatef(90.0,1.0,0.0,0.0);
glutDisplayFunc(draw);*/
glutKeyboardFunc(keyboard);
//Start the Main Loop
glutMainLoop();
return 0 ;
}
P a g e | 31
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
OUTPUT:
P a g e | 32
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
#9. Program on Digital Differential Analyzer Algorithm
#include<stdio.h>
#include<math.h>
#include<GL/glut.h>
void init(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,300.0,0.0,300.0);
}
void setPixel(int xCoordinate, int yCoordinate)
{
// glEnable(GL_LINE_SMOOTH);
glBegin(GL_POINTS);
glVertex2i(xCoordinate,yCoordinate);
glEnd();
glFlush();
}
void drawLine(int x1, int y1, int x2, int y2)
{
int dx=x2-x1;
int dy=y2-y1;
int steps=0;
P a g e | 33
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
if (abs(dy) > abs(dx))
steps= dy;
else
steps= dx;
float xx= dx/(float)steps;
float yy= dy/(float)steps;
float x= x1; float y= y1;
setPixel (int(x+0.5), int(y+0.5));
for (int j=0;j<steps;j++)
{
x=x+xx;
y=y+yy;
setPixel (int(x+0.5), int(y+0.5));
}
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
P a g e | 34
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
glColor3f(1.0,0.0,0.0);
glPointSize(5.0);
int x1 = 50;
int y1 = 20;
int x2 = 80;
int y2 = 200;
drawLine(x1,y1,x2,y2);
}
void main()
{
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("Program on Digital Differential Analyzer Algorithm");
init();
glutDisplayFunc(display);
glutMainLoop();
}
OUTPUT:
P a g e | 35
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
P a g e | 36
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
#10. Program on Bresenham Line drawing Algorithm
#include<stdio.h>
#include<math.h>
#include<GL/glut.h>
void init(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,300.0,0.0,300.0);
}
void setPixel(int xCoordinate, int yCoordinate)
{
glEnable(GL_LINE_SMOOTH);
glBegin(GL_POINTS);
glVertex2i(xCoordinate,yCoordinate);
glEnd();
glFlush();
}
void drawLineBres(int x1, int y1, int x2, int y2)
{
int dx=x2-x1;
int dy=y2-y1;
int p=2*dy-dx;
P a g e | 37
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
int x,y;
x = x1;
y=y1;
setPixel(x,y);
for(;x<=x2;)
{
if (p<0)
{
p+=2*dy;
}
else
{
p+=2*dy-2*dx;
y++;
}
x++;
setPixel(x,y);
}
}
void display(void)
P a g e | 38
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glPointSize(4.0);
int x1 = 100;
int y1 = 70;
int x2 = 160;
int y2 = 100;
drawLineBres(x1,y1,x2,y2);
}
void main()
{
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("Program on Bresenham Line drawing Algorithm");
init();
glutDisplayFunc(display);
glutMainLoop();
}
OUTPUT:
P a g e | 39
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
P a g e | 40
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
#11.WAP to draw circle using Mid-point circle drawing
algorithm.
#include<stdio.h>
#include<math.h>
#include<GL/glut.h>
void init(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,300.0,0.0,300.0);
}
void setPixel(int xCoordinate, int yCoordinate)
{
glEnable(GL_LINE_SMOOTH);
glBegin(GL_POINTS);
glVertex2i(xCoordinate,yCoordinate);
glEnd();
glFlush();
}
void symmetry_points(int xc,int yc,int x,int y)
{
setPixel (xc+x, yc+y);
glColor3f(0.5,0.0,0.0);
setPixel (xc-x, yc+y);
P a g e | 41
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
glColor3f(1.0,0.8,0.3);
setPixel (xc-x, yc-y);
glColor3f(0.3,0.1,0.2);
setPixel (xc+x, yc-y);
glColor3f(0.0,0.0,0.0);
setPixel (xc+y, yc+x);
glColor3f(0.4,0.5,0.6);
setPixel (xc-y, yc+x);
glColor3f(0.4,0.3,0.1);
setPixel (xc-y, yc-x);
glColor3f(1.0,0.0,0.0);
setPixel (xc+y, yc-x);
}
void drawCirBres(int xc, int yc, int r)
{
int p=1-r;
int x=0;
int y=r;
symmetry_points(xc,yc,x,y);
while (x<y)
{
if (p<=0)
{
x++;
P a g e | 42
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
p=p+2*x +1;
}
else
{
x++;
y--;
p=p + 2*x - 2*y +1;
}
symmetry_points(xc,yc,x,y);
}
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glPointSize(4.0);
int x1 = 220;
int y1 = 180;
int r = 70;
drawCirBres(x1,y1,r);
}
void main()
{ glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500,500);
P a g e | 43
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
glutInitWindowPosition(0,0);
glutCreateWindow("Program on Bresenham Line drawing Algorithm");
init();
glutDisplayFunc(display);
glutMainLoop();
}
OUTPUT:
P a g e | 44
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
#12.Write a program to draw the Sin and Cos curve using
menu.
#include <GL/glut.h>
# include <iostream.h>
# include<stdlib.h>
#include<math.h>
void display()
{
/* clear window */
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
void OptionsMenu (GLint selectedOpt)
{
int i;
switch (selectedOpt)
{
case 1: //cos curve
glColor3f(1.0,0.0,0.0);
glBegin(GL_POINTS);
for(i=0;i<361;i=i+5)
{
float x = (float)i;
float y = 60.0 * cos(i *(6.284/360.0));
P a g e | 45
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
glVertex2f(x,y);
}
glEnd();
/* flush GL buffers */
glFlush();
break;
case 2: // sine curve
glColor3f(1.0,0.0,0.0);
glBegin(GL_POINTS);
for(i=0;i<361;i=i+5)
{
float x = (float)i;
float y = 100.0 * sin(i *(6.284/360.0));
glVertex2f(x,y);
}
glEnd();
glFlush();/* flush GL buffers */
break;
default: break;
}
}
void init()
{
glClearColor(0.0, 0.0, 0.0, 1.0); // clear background with black
P a g e | 46
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
double w = glutGet( GLUT_WINDOW_WIDTH );
double h = glutGet( GLUT_WINDOW_HEIGHT );
double ar = w / h;
glOrtho( -360 * ar, 360 * ar, -120, 120, -1, 1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glPointSize(5);
}
void main()
{
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(20, 20);
glutCreateWindow("menu for sine cos");
init();
glutDisplayFunc(display);
glutCreateMenu (OptionsMenu);
glutAddMenuEntry("cos curve ",1);
glutAddMenuEntry("sine curve ",2);
glutAttachMenu (GLUT_RIGHT_BUTTON);
glutMainLoop(); }
P a g e | 47
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
OUTPUT:
P a g e | 48
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
P a g e | 49
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
#13.Write a program to draw a circle centered at (cx,cy)
with a given radius r, whose points are given by the
following equations
x=cx+r.cos(θ)
y=cy+r.sin(θ)
#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>
void draw()
{int xc=250, int yc=200, int r=50;
glClear ( GL_COLOR_BUFFER_BIT );
glColor3f(1,0,0);
glBegin(GL_POINTS);
for(int i= 0; i < 360; i++)
{
float theta = 2.0 * 3.1415926 * float(i) / float(360);
float x = r * cos (theta);
float y = r * sin (theta);
glVertex2f(float(xc) + x, y + float(yc));
}
glEnd();
glFlush();
P a g e | 50
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
}
void initialize ()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glClearColor(1.0, 1.0, 1.0, 0.0);
glPointSize(5);
gluOrtho2D(0,500,0,500);
}
//MAIN FUNCTION
int main()
{
glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE);
glutInitWindowSize(500,500);
glutInitWindowPosition(0, 0);
glutCreateWindow("Program to draw Points");
initialize();
glutDisplayFunc(draw);
glutMainLoop();
return 0;
P a g e | 51
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
}
OUTPUT:
P a g e | 52
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
#14.WAP Program to implement basic transformations (
Translation, scaling, rotation) using Menu driven program
#include <GL/glut.h>
# include <iostream.h>
# include<stdlib.h>
#include<math.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
void OptionsMenu (GLint selectedOpt)
{
int i;
switch (selectedOpt) {
case 1: //translation
glBegin(GL_QUADS); // Each set of 4 vertices form a quad
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex2f(-0.3f, -0.3f); // Define vertices in counter-clockwise (CCW)
order
glVertex2f( 0.3f, -0.3f); // so that the normal (front-face) is facing you
glVertex2f( 0.3f, 0.3f);
glVertex2f(-0.3f, 0.3f);
glEnd();
P a g e | 53
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
glTranslatef(0.1f, -0.7f, 0.0f); // Translate right and down
glBegin(GL_QUADS); // Each set of 4 vertices form a quad
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex2f(-0.3f, -0.3f);
glVertex2f( 0.3f, -0.3f);
glVertex2f( 0.3f, 0.3f);
glVertex2f(-0.3f, 0.3f);
glEnd();
glFlush();
break;
case 2: //rotation
glBegin(GL_TRIANGLES); // Each set of 3 vertices form a triangle
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex2f(-0.3f, -0.2f);
glVertex2f( 0.3f, -0.2f);
glVertex2f( 0.0f, 0.3f);
glEnd();
glTranslatef(0.2f, -0.3f, 0.0f); // Translate right and down
glRotatef(180.0f, 0.0f, 0.0f, 1.0f); // Rotate 180 degree
glBegin(GL_TRIANGLES); // Each set of 3 vertices form a
triangle
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex2f(-0.3f, -0.2f);
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex2f( 0.3f, -0.2f);
P a g e | 54
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex2f( 0.0f, 0.3f);
glEnd();
glFlush();
break;
case 3: //scaling
glBegin(GL_TRIANGLES); // Each set of 3 vertices form a triangle
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex2f(-0.3f, -0.2f);
glVertex2f( 0.3f, -0.2f);
glVertex2f( 0.0f, 0.3f);
glEnd();
glTranslatef(0.2f, -0.3f, 0.0f); // Translate right and down
glScaled(0.5f,0.5f,1.0f); // Rotate 180 degree
glBegin(GL_TRIANGLES); // Each set of 3 vertices form a
triangle
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex2f(-0.3f, -0.2f);
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex2f( 0.3f, -0.2f);
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex2f( 0.0f, 0.3f);
glEnd();
glFlush();
break;
P a g e | 55
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
default: break;
}
}
void init()
{
glClearColor(0.0, 0.0, 0.0, 1.0); // clear background with black
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glPointSize(5);
}
void main()
{
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(700, 700); // Set the window's initial width & height -
non-square
glutInitWindowPosition(50, 50); // Position the window's initial top-left
corner
glutCreateWindow("menu for transformation");
init();
glutDisplayFunc(display);
glutCreateMenu (OptionsMenu);
glutAddMenuEntry("translation ",1);
P a g e | 56
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
glutAddMenuEntry("rotation ",2);
glutAddMenuEntry("scaling ",3);
glutAttachMenu (GLUT_RIGHT_BUTTON);
glutMainLoop();
}
OUTPUT:
P a g e | 57
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
P a g e | 58
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
P a g e | 59
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
#15.WAP to show 3D shapes available in OpenGL
#include <stdio.h>
#include <windows.h> // Standard header for MS Windows applications
#include <GL/gl.h> // Open Graphics Library (OpenGL) header
#include <GL/glut.h> // The GL Utility Toolkit (GLUT) Header
#define KEY_ESCAPE 27
typedef struct {
int width;
int height;
char* title;
float field_of_view_angle;
float z_near;
float z_far;
} glutWindow;
glutWindow win;
float Rotation;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Clear Screen and Depth Buffer
glLoadIdentity();
P a g e | 60
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
gluLookAt( 10,3,0, 0,0,0, 0,1,0); //
Define a viewing transformation
glPushMatrix();
// Push the current matrix stack
glColor3f(1,0,0);
glTranslatef(0,0,-2); //
Multiply the current matrix by a translation matrix
glRotatef(Rotation,0,1,0); //
Multiply the current matrix by a rotation matrix
glRotatef(90,0,1,0); //
Multiply the current matrix by a rotation matrix
glutWireTeapot(1); //
render a wireframe teapot respectively.
glPopMatrix();
// Pop the current matrix stack
glPushMatrix();
// Push the current matrix stack
glColor3f(0,1,0);
glTranslatef(0,0,2); //
Multiply the current matrix by a translation matrix
glRotatef(Rotation,0,1,0);
glRotatef(90,0,1,0);
glutSolidTeapot(1);
glPopMatrix();
// Pop the current matrix stack
P a g e | 61
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
glPushMatrix();
// Push the current matrix stack
glColor3f(0,0,1);
glRotatef(-Rotation,0,1,0);
glRotatef(90,0,1,0); //
Multiply the current matrix by a rotation matrix
glTranslatef(0,2,0); //
Multiply the current matrix by a translation matrix
glutSolidCube (1.3);
glPopMatrix();
// Pop the current matrix stack
glPushMatrix();
// Push the current matrix stack
glColor3f(1,1,1);
glTranslatef(0,-2.5,0); //
Multiply the current matrix by a translation matrix
glRotatef(-Rotation,1,1,0); //
Multiply the current matrix by a rotation matrix
glRotatef(90,0,1,0); //
Multiply the current matrix by a rotation matrix
glutSolidSphere (1 , 32 , 32 );
glPopMatrix();
// Pop the current matrix stack
Rotation+=15;
P a g e | 62
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
glutSwapBuffers();
}
void initialize ()
{
GLfloat aspect = (GLfloat) win.width / win.height;
GLfloat amb_light[] = { 0.1, 0.1, 0.1, 1.0 };
GLfloat diffuse[] = { 0.6, 0.6, 0.6, 1 };
GLfloat specular[] = { 0.7, 0.7, 0.3, 1 };
glMatrixMode(GL_PROJECTION);
// select projection matrix
glViewport(0, 0, win.width, win.height);
// set the viewport
glMatrixMode(GL_PROJECTION);
// set matrix mode
glLoadIdentity();
// reset projection matrix
gluPerspective(win.field_of_view_angle, aspect, win.z_near, win.z_far);
// set up a perspective projection matrix
glMatrixMode(GL_MODELVIEW);
// specify which matrix is the
current matrix
glShadeModel( GL_SMOOTH );
glClearDepth( 1.0f );
// specify the clear value for the depth buffer
P a g e | 63
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
glEnable( GL_DEPTH_TEST );
glDepthFunc( GL_LEQUAL );
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
// specify implementation-specific hints
glLightModelfv( GL_LIGHT_MODEL_AMBIENT, amb_light );
glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuse );
glLightfv( GL_LIGHT0, GL_SPECULAR, specular );
glEnable( GL_LIGHT0 );
glEnable( GL_COLOR_MATERIAL );
glShadeModel( GL_SMOOTH );
glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE );
glDepthFunc( GL_LEQUAL );
glEnable( GL_DEPTH_TEST );
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glClearColor(0.0, 0.0, 1.0, 1.0);
}
void keyboard ( unsigned char key, int mousePositionX, int mousePositionY )
{
P a g e | 64
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
switch ( key )
{
case KEY_ESCAPE:
exit ( 0 );
break;
default:
break;
}
}
int main(int argc, char **argv)
{
// set window values
win.width = 640;
win.height = 480;
win.title = "OpenGL/GLUT Window.";
win.field_of_view_angle = 45;
win.z_near = 1.0f;
win.z_far = 500.0f;
// initialize and run program
glutInit(&argc, argv); // GLUT initialization
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH
); // Display Mode
P a g e | 65
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
glutInitWindowSize(win.width,win.height); //
set window size
glutCreateWindow(win.title);
// create Window
glutDisplayFunc(display);
// register Display Function
glutIdleFunc( display );
// register Idle Function
glutKeyboardFunc( keyboard );
// register Keyboard Handler
initialize();
glutMainLoop();
// run GLUT mainloop
return 0;
}
P a g e | 66
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
OUTPUT:
OTHER SHAPES:
#include <windows.h> // for MS Windows
#include <GL/glut.h> // GLUT, include glu.h and gl.h
/* Global variables */
char title[] = "3D Shapes with animation";
GLfloat anglePyramid = 0.0f; // Rotational angle for pyramid [NEW]
P a g e | 67
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
GLfloat angleCube = 0.0f; // Rotational angle for cube [NEW]
int refreshMills = 15; // refresh interval in milliseconds [NEW]
/* Initialize OpenGL Graphics */
void initGL() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and
opaque
glClearDepth(1.0f); // Set background depth to farthest
glEnable(GL_DEPTH_TEST); // Enable depth testing for z-culling
glDepthFunc(GL_LEQUAL); // Set the type of depth-test
glShadeModel(GL_SMOOTH); // Enable smooth shading
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Nice
perspective corrections
}
/* Handler for window-repaint event. Called back when the window first
appears and
whenever the window needs to be re-painted. */
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear
color and depth buffers
glMatrixMode(GL_MODELVIEW); // To operate on model-view matrix
// Render a color-cube consisting of 6 quads with different colors
glLoadIdentity(); // Reset the model-view matrix
glTranslatef(1.5f, 0.0f, -7.0f); // Move right and into the screen
P a g e | 68
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
glRotatef(angleCube, 1.0f, 1.0f, 1.0f); // Rotate about (1,1,1)-axis [NEW]
glBegin(GL_QUADS); // Begin drawing the color cube with 6 quads
// Top face (y = 1.0f)
// Define vertices in counter-clockwise (CCW) order with normal pointing
out
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f( 1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
// Bottom face (y = -1.0f)
glColor3f(1.0f, 0.5f, 0.0f); // Orange
glVertex3f( 1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);
// Front face (z = 1.0f)
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
P a g e | 69
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
// Back face (z = -1.0f)
glColor3f(1.0f, 1.0f, 0.0f); // Yellow
glVertex3f( 1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f( 1.0f, 1.0f, -1.0f);
// Left face (x = -1.0f)
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
// Right face (x = 1.0f)
glColor3f(1.0f, 0.0f, 1.0f); // Magenta
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glEnd(); // End of drawing color-cube
// Render a pyramid consists of 4 triangles
P a g e | 70
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
glLoadIdentity(); // Reset the model-view matrix
glTranslatef(-1.5f, 0.0f, -6.0f); // Move left and into the screen
glRotatef(anglePyramid, 1.0f, 1.0f, 0.0f); // Rotate about the (1,1,0)-axis
[NEW]
glBegin(GL_TRIANGLES); // Begin drawing the pyramid with 4
triangles
// Front
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(-1.0f, -1.0f, 1.0f);
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(1.0f, -1.0f, 1.0f);
// Right
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(1.0f, -1.0f, 1.0f);
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(1.0f, -1.0f, -1.0f);
// Back
glColor3f(1.0f, 0.0f, 0.0f); // Red
P a g e | 71
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(1.0f, -1.0f, -1.0f);
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(-1.0f, -1.0f, -1.0f);
// Left
glColor3f(1.0f,0.0f,0.0f); // Red
glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3f(0.0f,0.0f,1.0f); // Blue
glVertex3f(-1.0f,-1.0f,-1.0f);
glColor3f(0.0f,1.0f,0.0f); // Green
glVertex3f(-1.0f,-1.0f, 1.0f);
glEnd(); // Done drawing the pyramid
glutSwapBuffers(); // Swap the front and back frame buffers (double
buffering)
// Update the rotational angle after each refresh [NEW]
anglePyramid += 0.2f;
angleCube -= 0.15f;
}
/* Called back when timer expired [NEW] */
void timer(int value) {
P a g e | 72
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
glutPostRedisplay(); // Post re-paint request to activate display()
glutTimerFunc(refreshMills, timer, 0); // next timer call milliseconds later
}
/* Handler for window re-size event. Called back when the window first
appears and
whenever the window is re-sized with its new width and height */
void reshape(GLsizei width, GLsizei height) { // GLsizei for non-negative
integer
// Compute aspect ratio of the new window
if (height == 0) height = 1; // To prevent divide by 0
GLfloat aspect = (GLfloat)width / (GLfloat)height;
// Set the viewport to cover the new window
glViewport(0, 0, width, height);
// Set the aspect ratio of the clipping volume to match the viewport
glMatrixMode(GL_PROJECTION); // To operate on the Projection matrix
glLoadIdentity(); // Reset
// Enable perspective projection with fovy, aspect, zNear and zFar
gluPerspective(45.0f, aspect, 0.1f, 100.0f);
}
/* Main function: GLUT runs as a console application starting at main() */
int main(int argc, char** argv) {
P a g e | 73
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_DOUBLE); // Enable double buffered mode
glutInitWindowSize(640, 480); // Set the window's initial width & height
glutInitWindowPosition(50, 50); // Position the window's initial top-left
corner
glutCreateWindow(title); // Create window with the given title
glutDisplayFunc(display); // Register callback handler for window re-paint
event
glutReshapeFunc(reshape); // Register callback handler for window re-size
event
initGL(); // Our own OpenGL initialization
glutTimerFunc(0, timer, 0); // First timer call immediately [NEW]
glutMainLoop(); // Enter the infinite event-processing loop
return 0;
}
OUTPUT:
P a g e | 74
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
P a g e | 75
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
#16.WAP to draw the chess board
#include <gl/glut.h>
#include <stdio.h>
void init(void)
{
glClearColor(0.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 400,0,400);
}
void draw_BlackArea()
{
int i,j,r,g,b;
glColor3f(1,0,0);
for (j=1;j<=8;j++)
{
for (i=1;i<=8;i++)
{
if((j%2)==1)
{
r=i%2;
g=r;
b=r;
P a g e | 76
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
glColor3f(r,g,b);
glRecti((i-1)*50,(j-1)*50,(i)*50,j*50);
}
else
{
r=(i+1)%2;
g=r;
b=r;
glColor3f(r,g,b);
glRecti((i-1)*50,(j-1)*50,(i)*50,j*50);
}
}
}
glFlush();
glutSwapBuffers();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0, 0, 0);
P a g e | 77
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
draw_BlackArea();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(400,400);
glutInitWindowPosition(10,10);
glutCreateWindow("Triangle Drawing!");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
P a g e | 78
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
Output:
P a g e | 79
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
#17.WAP to perform point and line clipping in OpenGL
#include<stdio.h>
#include<GL/glut.h>
#define outcode int
double xmin=50,ymin=50,xmax=100,ymax=100;// Windows boundaries
double xvmin=200,yvmin =200, xvmax=300,yvmax=300; // Viewport
boundaries
const int RIGHT= 8; // bit codes for the right
const int LEFT =2; //bit codes for the left
const int TOP=4; // bit codes for the top
const int BOTTOM=1; //bit codes for the bottom
outcode ComputeOutCode(double x,double y); // used to compute bit codes of a
point
// Cohen -Sutherland clipping algorithm clips a line from
// p0=(x0,y0) to p1 =(x1,y1) against a rectangle with.
// diagonal from (xmin,ymin)to (xmax,ymax)
void CohenSutherlandLineClipAnddraw(double x0,double y0,double x1,double
y1)
{
// OutCodes for P0 ,P1 and Whatever point(among P0 & P1) lies outside the
P a g e | 80
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
// clip rectangle
outcode outcode0,outcode1,outcodeOut;
int accept =0,done =0;// These are two bits to indicate trivial accept and/or
// done with clipping
//compute outcodes
outcode0= ComputeOutCode(x0,y0);
outcode1= ComputeOutCode(x1,y1);
do
{
if(!(outcode0|outcode1)) // logical or is 0 trivially accept and exit
{ accept=1;
done=1;
}
else
if(outcode0 & outcode1) // logical and is 0 trivially reject and exit
done=1;
else
{
//failed both tests , so calculate the line segment clip;
// from an outside point to an intersection with clip edge
P a g e | 81
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
double x,y;
// at least one endpoint is outside the clip rectangle ; pick it.
outcodeOut= outcode0?outcode0:outcode1;
//now find the intersection point ; slope m= (y1-y0)/(x1-x0)
// use formula
/// y=y0+slope*(x-x0), //either x is xmin or xmax
/// x=x0+(1/slope)*(y-y0) // y is ymin or ymax
if(outcodeOut & TOP) //point is above the clip rectangle
{
x= x0+(x1-x0)*(ymax-y0)/(y1-y0);
y=ymax;
}
else
if(outcodeOut & BOTTOM) //point is below the clip rectangle
{
x= x0+(x1-x0)*(ymin-y0)/(y1-y0);
y=ymin;
}
else
if(outcodeOut & RIGHT) //point is to the right of clip rectangle
{
y= y0+(y1-y0)*(xmax-x0)/(x1-x0);
x=xmax;
P a g e | 82
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
}
else //point is to the left of the clip rectangle
{
y= y0+(y1-y0)*(xmin-x0)/(x1-x0);
x=xmin;
}
// now we move outside point to intersection point to clip
// and get ready for next pass.
if(outcodeOut == outcode0) // If the outside point was p0 update x0,y0 to
x,y
{ x0=x; // so x,y become the new x0,y0
y0=y;
outcode0 = ComputeOutCode(x0,y0);
//compute outcode of new endpoint
}
else // If the outside point was p1 update x1,y1 to x,y
{ // so x,y becomes the new x1,y1
x1=x;
y1=y;
outcode1 = ComputeOutCode(x1,y1);
// compute outcode of new endpoint
}
}
}while(!done);
if(accept) //If line was trivial reject no need to draw viewport
P a g e | 83
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
{
// window to viewport mapping
double sx=(xvmax-xvmin)/(xmax-xmin);// scale parameter in x direction
double sy=(yvmax-yvmin)/(ymax-ymin);// scale parameter in y direction
double vx0 = xvmin+(x0-xmin)*sx;
double vy0 = yvmin+(y0-ymin)*sy;
double vx1 = xvmin+(x1-xmin)*sx;
double vy1 = yvmin+(y1-ymin)*sy;
//draw a red color viewport
glColor3f(1.0,0.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);
glVertex2d(vx0,vy0);
glVertex2d(vx1,vy1);
glEnd();
}
}
// compute the bit code for a point (x,y) using the clip rectangle
P a g e | 84
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
// bounded diagonally by (xmin,ymin) and (xmax,ymax)
outcode ComputeOutCode(double x,double y)
{
outcode code =0;
if(y>ymax) //above the clip window
code |=TOP;
if(y<ymin) //below the clip window
code |=BOTTOM;
if(x>xmax) //to the right of the clip window
code |=RIGHT;
if(x<xmin) //to the left of the clip window
code |=TOP;
return code;
}
void display()
{
double x0=120,y0=10,x1=40,y1=130;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0); // draw red color lines
glBegin(GL_LINES);
glVertex2d(x0,y0);
glVertex2d(x1,y1);
glVertex2d(60,20);
glVertex2d(80,120);
P a g e | 85
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
glEnd();
glColor3f(0.0,0.0,1.0); // draw a blue colored window
glBegin(GL_LINE_LOOP);
glVertex2f(xmin,ymin);
glVertex2f(xmax,ymin);
glVertex2f(xmax,ymax);
glVertex2f(xmin,ymax);
glEnd();
CohenSutherlandLineClipAnddraw(x0,y0,x1,y1);
CohenSutherlandLineClipAnddraw(60,20,80,120);
glFlush();
}
void myinit()
{
glClearColor(1.0,1.0,1.0,1.0);
glColor3f(1.0,0.0,0.0);
glPointSize(1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,499.0,0.0,499.0);
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
P a g e | 86
VISIT www.educationjockey.com for old papers, practical files samples. This file is for reference purpose only.
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("cohen Sutherland line clipping algorithm");
glutDisplayFunc(display);
myinit();
glutMainLoop();
return 0;
}
Output: