This is a report that I have prepared during my Computer Graphics Lab course. This contains the theoretical information that we learned in our introduction class. It also contains information on different computer graphics tools and software. It contains codes to create different and also the procedure.
1. Information on GLUT
2. Flag drawing with GLUT
3. DDA Algorithm
4. Midpoint Line Drawing Algorithm
5. Tansformation
Lab Practices and Works Documentation / Report on Computer Graphics
1. 1
Notre Dame University Bangladesh
Department of Computer Science and Engineering
(CSE)
Lab Report
Content:
Submitted to:
Name: Mondira Chakraborty
Designation: Lecturer
Dept: Computer Science and
Engineering
Submitted by:
Name: Rup Chowdhury
Id: 201120010
Semester: Fall
Year: 2023
Course Name: Computer Graphics Lab
Course Code: CSE 4204
2. 2
Contents
1. Introduction to OpenGL and GLUT. The reasons behind using GLUT -------------- (3 - 7)
2. Drawing a flag using GLUT --------------------------------------------------------------- (8 - 12)
3. DDA Algorithm ---------------------------------------------------------------------------- (13 - 17)
4. Midpoint Line Drawing Algorithm ------------------------------------------------------ (18 - 21)
5. Transformation ----------------------------------------------------------------------------- (22 - 31)
3. 3
Experiment No: 1
Experiment Name: Introduction to OpenGL and GLUT. The reasons behind using GLUT.
Aim: To learn about OpenGL, GLUT and why we use GLUT.
Procedure: Read books on OpenGL and GLUT.
Code:
What is OpenGL?
It is a window system independent, operating system independent graphics rendering API which
can render 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.
OpenGL is window and operating system independent. As such, the part of your application which
does rendering is platform independent. However, 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.
Figure: Architecture of OpenGL
4. 4
What is GLUT?
GLUT 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 sized
OpenGL 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 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.
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
GLUT functions and data types:
glutInit : Initializes the GLUT library and negotiates a session with the window system.
glutInitWindowSize: Sets the initial window size. Normally it is written as, “void
glutInitWindowSize(int width, int height)”. Where “int width” represents the width in
pixels and “int Height” represents the height in pixels.
glutInitWindowPosition: Sets the initial window position on the screen. Normally it is
written as, “void glutInitWindowPosition(int x, int y)”. Where “int x” represents the
position in x axis and “int y” represents the position in y axis.
glutCreateWindow: Requests to create window.
glutInitDisplayMode: Sets the initial display mode.
5. 5
GLUT drawing primitives:
GL_POINTS : It draws individual points on the screen.
GL_LINES : Draws line between two different points.
GL_LINE_LOOP : Draws line but first and last point is connected.
Gl_TRIANGLE_STRIP : Draws linked strip of triangles.
GL_TRIANGLE_FAN : Draws fan of triangles.
GL_QUADS: Draws 4 sided polygon taking 4 different points.
GL_QUAD_STRIP: Draws linked 4 sided polygons.
GL_POLYGON: Draws multiple sided polygon taking different multiple points.
glVertex
The main function is function named glVertex. This function defines a point in our 3D world,
and it can vary from receiving 2 up to 4 coordinates.
glBegin and glEnd
glVertex alone won't draw anything on the screen, it merely defines a vertex, usually
of a more complex object. To really start displaying something on the screen we will have
to use two additional functions. These functions are represented as
glBegin(int mode) and glEnd( void );
6. 6
glVertex2f(100.0f, 150.0f); defines a point at x = 100, y = 150, z = 0; this function takes
only 2 parameters, z is always 0.
glVertex3f(100.0f, 150.0f, -25.0f); defines a point at x = 100, y = 150, z = -25.0f; this
function takes 3 parameters, defining a fully 3D point in your world.
glVertex3f(0, 0, 0);
glVertex3f(200, 0, 0);
glVertex3f(200, 0, 0);
glVertex3f(200, 100, 0);
glVertex3f(200, 100, 0);
glVertex3f(100, 200, 0);
glVertex3f(100, 200, 0);
glVertex3f(0, 100, 0);
glVertex3f(0, 100, 0);
glVertex3f(0, 0, 0);
(100, 200)
(200, 100)
(0, 100)
(200, 0)
(0, 0)
7. 7
The gluOrtho2D function sets up a two-dimensional orthographic viewing region.
Discussion:
From this lab I learned about OpenGL and GLUT theoretically. And we learned about the
different functions and data types of GLUT. How these functions work and properties of the
function, how these codes are written.
8. 8
Experiment No: 2
Experiment Name: Drawing a flag using GLUT
Aim: To draw a flag using different shapes and functions.
Procedure:
1. Draw circle using circle equation.
2. Draw the background, stick and base using glQuads and glVertex.
Code:
#include<windows.h>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <stdlib.h>
#include <math.h>
void init()
{
glClearColor(0.0f,0.0f,0.0f,0.0f);
glOrtho(-20,60,-25,25,-3,3);
}
void DrawCircle(float cx, float cy, float rx,float ry, int num_segments)
{
glBegin(GL_TRIANGLE_FAN);
for(int ii = 0; ii < num_segments; ii++)
9. 9
{
float theta = 2.0f * 3.1415926f * float(ii) / float(num_segments);//get the current angle
float x = rx * cosf(theta);//calculate the x component
float y = ry * sinf(theta);//calculate the y component
glVertex2f(x + cx, y + cy);//output vertex
}
glEnd();
}
void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
//big square
glColor3f(0.0f,0.6f,0.1f);
glBegin(GL_QUADS);
glVertex3d(35,25,0);
glVertex3d(35,4,0);
glVertex3d(1.0,4.0,0.0);
glVertex3f(1.0,25.0,0.0);
glEnd();
//circle
glColor3f(1.0f,0.0f,0.0f);
glBegin(GL_QUADS);
12. 12
Discussion: From this lab, I have learned to draw different shapes like rectangle and circle. Then
I learned how to merge different shapes to draw an image. This lab helped me learn coordination
system more practically.
13. 13
Experiment No: 3
Experiment Name: DDA Algorithm
Aim: To draw line using DDA Algorithm
Procedure:
1. Create DDA function.
2. Take coordinates for 2 different points ((x1, y1) (x2, y2))
3. Put these coordinates into the DDA algorithm with a loop.
Code:
#include<stdio.h>
#include<GL/gl.h>
#include<GL/glut.h>
#include<iostream>
using namespace std;
int x0, x1, y0, y1;
void DDA(int x0, int x1, int y0, int y1)
{
float ys = (y1 - y0);
float xd = (x1 - x0);
float m = ys/xd;
if(m <= 1 && m > 0)
{
printf("%d %d n", xd, ys);
printf("m = %f n", m);
float x=0.0, y=y0;
for(int i = x0; i < x1; i++)
14. 14
{
x = i + 1;
y = y + m;
printf("x = %f and y = %f ", x, y);
printf("n");
glVertex2f(x, y);
}
}
else if(m >= -1 && m <= 0)
{
printf("%d %d n", xd, ys);
printf("m = %f n", m);
float x=x1, y=y1;
for(int i = x1; i > x0; i--)
{
x = i - 1;
y = y - m;
printf("x = %f and y = %f ", x, y);
printf("n");
glVertex2f(x, y);
}
}
else if( m > 1)
15. 15
{
printf("%d %d n", xd, ys);
printf("m = %f n", m);
float x=0.0, y=y0;
for(int i = x0; i < x1; i++)
{
x = i + (1/m);
y = y + 1;
printf("x = %f and y = %f ", x, y);
printf("n");
glVertex2f(x, y);
}
}
else if(m < -1)
{
printf("%d %d n", xd, ys);
printf("m = %f n", m);
float x=x1, y=y1;
for(int i = x1; i > x0; i--)
{
x = i - (1/m);
y = y - 1;
printf("x = %f and y = %f ", x, y);
printf("n");
glVertex2f(x, y);
17. 17
}
int main(int argc, char** argv)
{
cout << "Input 1st point: ";
cin >> x0;
cin >> y0;
cout << "Input 1st point: ";
cin >> x1;
cin >> y1;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(250, 280);
glutInitWindowPosition(100, 100);
glutCreateWindow("DDA line");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Discussion: From this lab I have learned about drawing lines using DDA algorithm. And learned
taking co-ordinates as input from the user and use them to draw line between these two
coordinates.
18. 18
Experiment No: 4
Experiment Name: Midpoint Line Algorithm
Aim: To draw a line using midpoint line algorithm.
Procedure:
1. Create MidPoint function.
2. Take coordinates for 2 different points ((x1, y1) (x2, y2))
3. Put these coordinates into the midpoint line algorithm with a loop.
Code:
#include<stdio.h>
#include<GL/gl.h>
#include<GL/glut.h>
#include<iostream>
using namespace std;
int x0, x1, y0, y1;
void MidPoint(int x0, int x1, int y0, int y1)
{
int p = 0, q = 0;
int dx = x1 -x0;
int dy = y1 - y0;
int d = 2*dy - dx;
int cnt = 0;
int incrE = 2*dy;
int incrNE = 2*(dy-dx);
int x = x0;
int y = y0;
glBegin(GL_POINTS);
22. 22
Experiment No: 5
Experiment Name: Transformation
Aim: To use translation, scaling, rotation on an object to transform it.
Procedure:
1. Create 3 different functions for translation, scaling and rotation.
2. Take input from the user.
3. Take opinion from the user and give desire result.
Code:
#include<stdio.h>
#include<GL/gl.h>
#include<GL/glut.h>
#include<math.h>
float T[3][3];
int P1[3][1];
int P2[3][1];
int NT[3][1];
int choice,Dx,Dy,Sx,Sy;
typedef GLfloat colorType[3];
float angle;
void drawPxl(GLint x, GLint y, GLfloat r, GLfloat g, GLfloat b)
{
glColor3f(r,g,b);
glBegin (GL_POINTS);
glVertex2i (x,y);
glEnd();
}
void matCal(int p[3][1])
{
for(int i=0;i<3;i++)
{
for(int k=0;k<3;k++)
{
NT[i][1]+=T[i][k]*p[k][1];
// printf("%f ",T[i][k]);
}
//printf("n");
}