MINI P Suraj

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

ASSIGNMENT NO : 08

(Mini Project)

Title :
Implementation of openGL functions in mini project.

Aim :
Design and implement game / animation clip / Graphics Editor using open source

graphics library. Make use of maximum features of Object Oriented Programming.

CO Mapped : CO 4, Co 5

Pre-requisite :
1. Basic programming skills of C++ and OpenGL

2. 64-bit Open source Linux

3. Open Source C++ Programming tool like G++/GCC, OpenGL

Learning Objective : To implement OpenGL functions.

Theory:
OpenGL Basics:

Open Graphics Library (OpenGL) is a cross-language (language independent), cross-

platform (platform independent) API for rendering 2D and 3D Vector Graphics (use of

polygons to represent image). OpenGL is a low-level, widely supported modeling and

rendering software package, available across all platforms. It can be used in a range of

graphics applications, such as games,

CAD design, or modeling. OpenGL API is designed mostly in hardware.

Design:
This API is defined as a set of functions which may be called by the client program.
Although functions are similar to those of C language but it is language independent.

Development:
It is an evolving API and Khronos Group regularly releases its new version having some

extended feature compare to previous one. GPU vendors may also provide some additional

functionality in the form of extension.

Associated Libraries:
The earliest version is released with a companion library called

OpenGL utility library. But since OpenGL is quite a complex process. So in order to make it

easier other library such as OpenGL Utility Toolkit is added which is later superseded by

freeglut. Later included library were GLEE, GLEW and glbinding.

Implementation:
Mesa 3D is an open source implementation of OpenGL. It can do pure software

rendering and it may also use hardware acceleration on BSD, Linux, and other platforms

by taking advantage of Direct Rendering Infrastructure.

Installation of OpenGL on Ubuntu

We need the following sets of libraries in programming OpenGL: 1.

Core OpenGL (GL):


consists of hundreds of functions, which begin with a prefix "gl"

(e.g., glColor, glVertex, glTranslate, glRotate). The Core OpenGL models an object via a set of

geometric primitives, such as point, line, and polygon. 2.

OpenGL Utility Library (GLU):


built on-top of the core OpenGL to provide important utilities and more building models

(such as qradric surfaces). GLU functions start with a prefix "glu" (e.g., gluLookAt,

gluPerspective)3.
OpenGL Utilities Toolkit (GLUT):
provides support to interact with the Operating System

(such as creating a window, handling key and mouse inputs); and more building models
(such as sphere and torus). GLUT functions start with a prefix of

"glut"

(e.g., glutCreatewindow, glutMouseFunc). GLUT is designed for constructing small to

medium sudo apt-get install freeglut3-dev

For working on Ubuntu operating system:

gcc filename.c -lGL -lGLU -lglut where filename.c is the name of the file

with which this program is saved.

Prerequisites for OpenGL


Since OpenGL is a graphics API and not a platform of its own, it requires a language to

operate in and the language of choice is C++.

Getting started with

OpenGL Overview of an

OpenGL program
• Main

• Open window and configure frame buffer (using GLUT for example)

• Initialize GL states and display (Double buffer, color mode, etc.)

• Loop

• Check for events if window event (resize, unhide, maximize etc.) modify the viewport and

Redraw else if input event (keyboard and mouse etc.) handle the event (such as move the

camera or change the state) and usually draw the scene

Redraw

• Clear the screen (and buffers e.g., z-buffer)


• Change states (if desired)

• Render

• Swap buffers (if double buffer)

OpenGL order of operations


• Construct shapes (geometric descriptions of objects – vertices, edges, polygons etc.)

• Use OpenGL to

• Arrange shape in 3D (using transformations)

• Select your vantage point (and perhaps lights)

• Calculate color and texture properties of each object

• Convert shapes into pixels on screen

OpenGL Syntax
• All functions have the form: gl*

• glVertex3f() – 3 means that this function take three arguments, and f means that the type

of those arguments is float.

• glVertex2i() – 2 means that this function take two arguments, and i means that the type of

those arguments is integer

• All variable types have the form: GL*

• In OpenGL program it is better to use OpenGL variable types (portability)

• Glfloat instead of float

• Glint instead of int

OpenGL states
▪ On/off (e.g., depth buffer test)

▪ glEnable( GLenum ) ▪

glDisable( GLenum )

Examples:
▪ glEnable(GL_DEPTH_TEST);

▪ glDisable(GL_LIGHTING);

• Mode States

• Once the mode is set the effectstays until reset

Examples: glShadeModel(GL_FLAT) or

glShadeModel(GL_SMOOTH) glLightModel(…) etc.

Drawing in 3D
➢ Depth buffer (or z-buffer) allows scene to remove hidden surfaces. Use

➢ glEnable(GL_DEPTH_TEST) to enable it.

➢ glPolygonMode( Face, Mode )

➢ Face: GL_FRONT, GL_BACK, GL_FRONT_AND_BACKMode: GL_LINE,

➢ GL_POINT, GL_FILL

➢ glCullFace( Mode )

➢ Mode: GL_FRONT, GL_BACK, GL_FRONT_AND_BACK

➢ glFrontFace( Vertex_Ordering )

➢ Vertex Ordering: GL_CW or GL_CCW

Viewing transformation
• glMatrixMode ( Mode )

• Mode: GL_MODELVIEW, GL_PROJECTION, or GL_TEXTURE

• glLoadIdentity()

• glTranslate3f(x,y,z)

• glRotate3f(angle,x,y,z)

• glScale3f(x,y,z)
OpenGL provides a consistent interface to the underlying graphics hardware. This
abstraction allows a single program to run a different graphics hardware easily. A program
written with

OpenGL can even be run in software (slowly) on machines with no graphics acceleration.

OpenGL function names always begin with gl, such as glClear(), and they may end with

characters that indicate the types of the parameters, for example glColor3f(GLfloat red,

GLfloat green, GLfloat blue) takes three floating-point color parameters and

glColor4dv(const GLdouble *v) takes a pointer to an array that contains 4 double-precision

floating-point values. OpenGL constants begin with GL, such as GL DEPTH. OpenGL also

uses special names for types that are passed to its functions, such as GLfloat or GLint, the

corresponding C types are compatible, that is float and int respectively. GLU is the OpenGL

utility library. It contains useful functions at a higher level than those provided by OpenGL,

for example, to draw complex shapes or set up cameras. All GLU functions are written on

top of OpenGL. Like OpenGL, GLU function names begin with glu, and constants begin with

GLU. GLUT, the OpenGL Utility Toolkit, provides a system for setting up callbacks for

interacting with the user and functions for dealing with the windowing system. This

abstraction allows a program to run on different operating systems with only a recompile.

Glut follows the convention of prepending function names with glut and constants with

Glut.

Graphics Mode:
Screen is divided into pixels. While working in C programming language,

default output mode is text mode. To draw graphics objects on screen display

mode must be changed from text mode to graphics mode. To change from text

mode to graphics mode, use functions below.


void initgraph(int *graphdriver, int graph mode, char *pathtodriver);

Some of the basic graphic mode functions are

#include<graphics.h>: The header file must be included for every graphics

program

1. void initgraph(int *graphdriver, int graph mode, char *pathtodriver) ;

initgraph() function is used to change from text mode to graphics mode. The

graphics, driver and graphics mode are the parameters to this function,

detectgraph() fiction is used to find out the graphics driver and graphics

mode.

2. void closegraph() ; closegraph() function closes the graphics mode,

deallocates all memory allocated by graphics system and restores the

screen to text mode.

3. putpixel(int x, int y, color) ; putpixel() function is used to draw pixel at

given position(x,y) and with given color.

4. line (int x1.int y1int x2,int y2) ; line function is used to draw a line from

point(x1,y1)-Starting point to point (x2 y2)-Ending point

5. circle (int x, int y, int radius) ;

Circle function is used to draw a circle with canter (x, y) and radius of a circle.

6. rectangle(int left, int top, int right, int bottom) ;

Rectangle function is used to draw a rectangle. Coordinates of left top and right

bottom corner are required to draw the rectangle.

7. ellipse (int x, int y, start angle, end angle, int x radius, int y radius);

Ellipse function is used to draw ellipse with center(x, y) starting angle, end

Angle, radius of x and y axis.


8. onttextxy(int x, int y, "text") ; outtextxy function is used to insert text at

the given position(x,y).

Algorithm :
1. Start

2. Declare all required variable.

3. Initialize the graphics mode

4 . Draw the lines for bonnet and line drawing function.

6. . Draw the circle for left wheel of car using circle function

7. Draw the circle for right wheel of car using circle function

8.draw the line Line left of left wheel using line function

9. draw the line Line left of Line middle of both wheel

10 draw other parts of car and increase the x coordinates of both wheel to run the car.

11. Stop
Assignment No. 8: Car Animation Mini project

Title : Moving Car

Batch: B3

Name: Yasir Patel

Roll No: COSB58

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<dos.h> void

main()
{

int gd=DETECT,gm,i;

clrscr();

initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");

//setbgcolor(RED); //road line(0,347,700,347);

outtextxy(175,150,"); outtextxy(175,170,"*********

MICROPROJECT ***********"); outtextxy(175,190,"Subject :

Computer Graphics "); for (i = 0; i <= 420; i = i + 10)

// Set color of car as red setcolor(RED);

// These lines for bonnet

and // body of car line(0 + i,

300, 210 + i, 300); line(50 + i,

300, 75 + i, 270); line(75 + i,

270, 150 + i, 270); line(150 + i,

270, 165 + i, 300); line(0 + i,

300, 0 + i, 330); line(210 + i,

300, 210 + i, 330); // For left

wheel of car circle(65 + i, 330,

15); circle(65 + i, 330, 2); //

For right wheel of car

circle(145 + i, 330, 15);

circle(145 + i, 330, 2); // Line

left of left wheel line(0 + i,

330, 50 + i, 330);
// Line middle of both wheel

line(80 + i, 330, 130 + i, 330); //

Line right of right wheel

line(210 + i, 330, 160 + i, 330);

delay(100);

// To erase previous drawn car, draw

// the whole car at same position //

but colour using black

setcolour(BLACK);

// Lines for bonnet and body of car

line(0 + i, 300, 210 + i, 300);

line(50 + i, 300, 75 + i, 270);

line(75 + i, 270, 150 + i, 270);

line(150 + i, 270, 165 + i, 300);

line(0 + i, 300, 0 + i, 330); line(210

+ i, 300, 210 + i, 330); // For left

wheel of car circle(65 + i, 330, 15);

circle(65 + i, 330, 2); // For right

wheel of car circle(145 + i, 330,

15); circle(145 + i, 330, 2); // Line

left of left wheel line(0 + i, 330, 50

+ i, 330);

// Line middle of both wheel

line(80 + i, 330, 130 + i, 330); //


Line right of right wheel

line(210 + i, 330, 160 + i, 330);

getch();

You might also like