CG Final

Download as pdf or txt
Download as pdf or txt
You are on page 1of 39

ADICHUNCHANAGIRI UNIVERSITY

BG Nagara -571448, KARNATAKA.

A Mini Project Report


On
“PISTON USING OPENGL”
Submitted in partial fulfilment for the academic year 2022-23

Bachelor of Engineering
In
Computer Science and Engineering
Submitted by,
LAVANYA K V 20CSE039
LEKHANA H L 20CSE040

Under the guidance of:


Dr. VANI N
Asst. Professor, Dept., of CS&E
BGSIT, B G Nagara

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


B G S INSTITUTE OF TECHNOLOGY
B G NAGARA-571448
2022-23
ADICHUNCHANAGIRI UNIVERSITY
B G S INSTITUTE OF TECHNOLOGY,
Department of Computer Science and Engineering
BG Nagara-571448, MANDYA

CERTIFICATE

This is to certify that the mini project entitled “PISTON USING OPENGL” is
a bonafide work carried by Ms. LAVANYA K V, bearing USN: 20CSE039, and
Ms.LEKHANA H L, bearing USN: 20CSE040 the students of B G S INSTITUTE OF
TECHNOLOGY,B G Nagara in partial fulfillment of Bachelor of Engineering in
Computer Science and Engineering, of Adichunchanagiri University, B G Nagara during
the academic year 2022 - 2023. It is certified that all correction / suggestions indicated
for internal assessment have been incorporated in the report deposited in the department
library.

Signature of Guide: Signature of HOD:

Dr. VANI N Dr. SHASHIKALA S V


Asst. Professor, Dept. of CS&E Professor & HOD, Dept. of CS&E
BGSIT, B G Nagara BGSIT, B G Nagara

External Viva

Name of the Examiners Signature with Date

1.

2.
ACKNOWLEDGEMENT
We sincerely convey my regards and thanks to Dr. B K Narendra, Principal,
BGSIT, BG Nagar, Mandya, for giving me a chance to carry out and present my mini
project work.

Our sincere thanks to Dr. Shashikala S V, Prof. and Head of Department, CS&E,
BGSIT, B G Nagar, Mandya, for giving me a chance to carry out and present my project
work with all the support and facilities.

We would like to thank Dr. Vani N, Assistant Professor, Department of CS&E,


BGSIT, BG Nagar, Mandya our honorable guide who stood as an excellent guide to carry
out my work has been always available as an expressive evaluator for the creation and
correction of the report towards our work. They have taken pain and time to go through my
work when needed.

Our heartfelt gratitude to all the teaching and non-teaching faculties of Computer
Science and Engineering Department, BGSIT, BG Nagar, Mandya, for their support
and guidance towards the completion of my mini project work.

Finally, we would also extend our heartfelt thanks to our family members,
classmates, friends and well-wishers for their support and encouragement throughout this
effort.

LAVANYA K V(20CSE039)

LEKHANA H L(20CSE040)

i
ABSTRACT

In this project, we have graphically implemented 'Piston', where it demonstrate working of

piston in a steam engine. The project simulates a working of a piston in a steam engine. It
illustrates how the linear motion of the piston is converted into rotary motion. The engine is
initially at rest. On right clicking, the user is provided with a menu which provides five options-

shaded, animate, increase speed, decrease speed, transparent. A sample code, followed by the
main code, describes the implementation of Piston in 2D graphics, where animation of the
piston is shown to demonstrate the working of a steam engine's piston. The texture of the piston
can be changed by shaded option and there are two textures. We finally conclude our report

with the conclusion and references, which gives an insight as to what all we gained from this
project work.

ii
CONTENTS

Title Page No.


ACKNOWLEDGMENT i
ABSTRACT ii
CONTENTS iii
LIST OF FIGURES iv
CHAPTER 1 INTRODUCTION 1-2
1.1 Computer Graphics 1
1.2 Introduction to Piston 2
CHAPTER 2 LITERATURE SURVEY 3
2.1 Literature Survey 3
CHAPTER 3 REQUIREMENT SPECIFICATION 4
3.1 Hardware Requirements 4
3.2 Software Requirements 4
CHAPTER 4 SOFTWARE DESIGN 5
4.1 Flow Chart 5
CHAPTER 5 Project Description 6-9
5.1 Keyboard Function 6-7
5.2 Display Function 8
5.3 Reshape Function 9
CHAPTER 6 Implementation 10-28
6.1 Source Code 10-28
CHAPTER 7 SNAPSHOTS 29-31
CONCLUSION AND FUTURE SCOPE 32
REFRENCES 33

iii
LIST OF FIGURES

Figure no. Figure name Page no.

4.1 Flowchart of Piston 5


5.1 Keyboard function 6-7
5.2 Display function 8
5.3 Reduce function 9
7.1 Initial view 36
7.2 Menu associated with Right Mouse Button 36
7.3 Rotating Steam Engine 37
7.4 Shading 37
7.5 Texture 38
7.6 No Light (Transparency) 38

iv
Piston Using OpenGL 2022-2023

CHAPTER 1
INTRODUCTION
1.1 COMPUTER GRAPHICS

Graphics provides one of the most natural means of communicating with a computer, since
our highly developed 2D and 3D pattern-recognition abilities allow us to perceive and process
pictorial data rapidly and efficiently. Interactive computer graphics is the most important means of
producing pictures since the invention of photography and television. It has the added advantage that,
with the computer, we can make pictures not only of concrete real world objects but also of abstract,
synthetic objects, such as mathematical surfaces and of data that have no inherent geometry, such as
survey results.

Using this editor you can draw and paint using the mouse. It can also perform ahost of
other functions like drawing lines, circles, polygons and so on. Interactive picture construction
techniques such as basic positioning methods, rubber-band methods, dragging and drawing are used.
Block operations like cut, copy and paste are supported to edit large areas of the workspace
simultaneously. It is user friendly and intuitive to use.

OpenGL serves two main purpose :

• To hide the complexities of interfacing with different 3D accelerators, by presenting


programmer with a single, uniform API
• To hide the differing capabilities of hardware platforms, by requiring that all
Implementations support the full openGL, feature set.
OpenGL has historically been influential on the development of 3D accelerator, promotinga base
level of functionality that is now common in consumer level hardware:

• Rasterized points, lines and polygons are basic primitives.

• A transform and lighting pipeline.

• Z buffering.

• Texture Mapping

• Alpha.

Dept of CSE, BGSIT, B G Nagara Page 1


Piston Using OpenGL 2022-2023

1.2 INTRODUCTION TO PISTON


The aim of this project is to create a PISTON. The Engine is made up of a Piston, Engine
Pole, Cylinder Head, Flywheel, Crank Bell and a Crank. The viewer is allowed to rotate the Engine’s
Crank either in clock wise or in anti-clock wise direction. The viewer can also slow up or slow down
the Crank speed.

First a Piston, Engine Pole, Cylinder Head, Flywheel, Crank Bell and a Crank is created using
myCylinder( ) function. The main primitives used inside the myCylinder( ) function to create a
Cylinder is gluCylinder( ) and gluDisk( ) . So every time, myCylinder( ) function is called inside the
functions used to create Piston, Engine Pole, Cylinder Head, Flywheel, Crank Bell and Crank. The
parts mentioned above are combined to form a Steam Engine image. We can make Steam Engine
transparent and display. In display function, at first it clears the drawing buffer and if transparency is
set, displays the model twice, first time accepting those fragments with a ALPHA value of 1 only,
then with DEPTH_BUFFER writing disabled for those with other values. Initially when the
animation is not called, the crank angle will not change and the window is idle. When called
increments the crank angle by ANGLE_STEP, updates the head angle and notifies the system that the
screen needs to be updated. When a menu option has been selected, it translates the menu item
identifier into a keystroke, then calls the keyboard function. A menu will be associated with the mouse
too. The viewer can also see the shaded and textured steam engine

The controls functions are :-


1. ‘a’ - > To rotate crank anti-clock wise.

2. ‘z’ - > To rotate crank clock wise.

3. ‘+’ and ‘-‘ - > To speed up and speed down

4. ‘o’ - > Transparency.

5. ‘0’ and 1’ - > Right light and Left light respectively

Dept of CSE, BGSIT, B G Nagara Page 2


Piston Using OpenGL 2022-2023

CHAPTER 2

LITERATURE SURVEY
Computer graphics started with the display of data on hardcopy plotters and cathode ray tube
(CRT) screens soon after the introduction of computers.

Computer graphics today largely interactive, the user controls the contents, structure, and
appearance of objects and of displayed images by using input devices, such as keyboard, mouse, or
touch-sensitive panel on the screen. Graphics based user interfaces allow millions of new users to
control simple, low-cost application programs, such as spreadsheets, word processors, and drawing
programs.

OpenGL (Open Graphics Library) is a standard specification defining a crosslanguage, cross-


platform API for writing applications that produce 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. It is also used in video games, where it competes with Direct3D on Microsoft Windows
platforms (see Direct3D vs. OpenGL). OpenGL is managed by the non-profit technology consortium,
the Khronos Group.

In the 1980s, developing software that could function with a wide range of graphics hardware
was a real challenge. By the early 1990s, Silicon Graphics (SGI) was a leader in 3D graphics for
workstations. SGI's competitors (including Sun Microsystems, Hewlett- Packard and IBM) were also
able. In addition, SGI had a large number of software customers; by changing to the OpenGL API
they planned to keep their customers locked onto SGI (and IBM) hardware for a few years while
market support for OpenGL maturedto bring to market 3D hardware, supported by extensions made
to the PHIGS standard.

Dept of CSE, BGSIT, B G Nagara Page 3


Piston Using OpenGL 2022-2023

CHAPTER 3

REQUIREMENTS SPECIFICATION
3.3 HARDWARE REQUIREMENTS

• Processor : Pentium PC

• RAM : 512MB

• Hard Disk : 20GB(approx)

• Display : VGA Color Monitor

3.2 SOFTWARE REQUIREMENTS

• Operating System : Windows 98SE/2000/XP/Vista/UBUNTU

• Language : Open GL

• Complier: Microsoft visual studio 2010

Dept of CSE, BGSIT, B G Nagara Page 4


Piston Using OpenGL 2022-2023

CHAPTER 4

SOFTWARE DESIGN

4.1 FLOW CHART

Fig.4.1 Flowchart of Piston

The dataflow diagram. Which shows the flow of data within the program from the start tothe different
functions within the program, where each function will perform its specified actions for the proper
display of the graphics and functionality defined within the program.

Dept of CSE, BGSIT, B G Nagara Page 5


Piston Using OpenGL 2022-2023

CHAPTER 5

PROJECT DESCRIPTION

5.1 KEYBOARD FUNCTION:

KEYBOARD
FUNCTION

case 's': case 't':


case 'a':
if (shaded == FALSE) { if (texture == FALSE)
shaded = TRUE; if ((crank_angle +=
glShadeModel(GL_SMOO {
crank_step) >= 360)
TH); texture = TRUE;
crank_angle = 0;
glEnable(GL_LIGHTING); glEnable(GL_TEXTURE_2
glEnable(GL_DEPTH_TES D); gluQuadricTexture(obj, head_angle=head_look_up
T); GL_TRUE); _table[crank_angle];
glEnable(GL_COLOR_MA } break;
TERIAL);
gluQuadricNormals(obj, else { texture = FALSE; case 'z': if ((crank_angle -=
GLU_SMOOTH); glDisable(GL_TEXTURE_ crank_step) <= 0)
gluQuadricDrawStyle(obj, 2D);
crank_angle = 360;
GLU_FILL); } gluQuadricTexture(obj,
head_angle =
else { shaded = FALSE; GL_FALSE);
head_look_up_table[crank_
glShadeModel(GL_FLAT); } angle];
glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TE break; break;
ST);
case 'o': case '0': if (light1) {
glDisable(GL_COLOR_M
glDisable(GL_LIGHT0);
ATERIAL); if (transparent == FALSE) light1 = FALSE; }
gluQuadricNormals(obj, { transparent = TRUE;
GLU_NONE); else {
}
gluQuadricDrawStyle(obj,
glEnable(GL_LIGHT0); light1
GLU_LINE); else { transparent = FALSE; = TRUE; }
gluQuadricTexture(obj, } break;
GL_FALSE); }

Fig 5.1 Keyboard function

Dept of CSE, BGSIT, B G Nagara Page 6


Piston Using OpenGL 2022-2023

KEYBOARD FUNC (contd)

case '1':
case ' ':
if (light2) {
glDisable(GL_LIGHT1); if (anim) {

light2 = FALSE; } glutIdleFunc(0);

else { glEnable(GL_LIGHT1); anim = FALSE;


light2 = TRUE; } }
break; case '4': else {
if ((view_h -= ANGLE_STEP) <= glutIdleFunc(animation); anim
0) view_h = 360; = TRUE; }
break; break;
case '6': case '+': if ((++crank_step) >
if ((view_h += ANGLE_STEP) >= 45)
360) view_h = 0; crank_step = 45;
break; break;
case '8': if ((view_v += case '-':
ANGLE_STEP) >= 360) view_v =
0; break; if ((--crank_step) <= 0)
crank_step = 0;
case '2': if ((view_v -=
ANGLE_STEP) <= 0) view_v = break;
360; default: return;
break;

Dept of CSE, BGSIT, B G Nagara Page 7


Piston Using OpenGL 2022-2023

5.2 DISPLAY FUNCTION:

DISPLAY FUNCTION

glPushMatrix();

if (transparent) {
glEnable(GL_ALPHA_TEST); draw_engine_pole();

pass = 2; glPushMatrix();

} else { glTranslatef(0.5, 1.4, 0.0);


glDisable(GL_ALPHA_TEST); draw_cylinder_head();
pass = 0; glPopMatrix();

} glPushMatrix();
glRotatef(view_h, 0, 1, 0);
glRotatef(view_v, 1, 0, 0); glTranslatef(0.0, -0.8, 0.0);
draw_crank();
do {

if(pass==2) { glPopMatrix();

glAlphaFunc(GL_EQUAL,1); }
glDepthMask(GL_TRUE);
while (pass > 0);
pass--;
glDepthMask(GL_TRUE);
}
glutSwapBuffers();
else if (pass != 0){
glAlphaFunc(GL_NOTEQUAL, 1); glPopMatrix();
glDepthMask(GL_FALSE);

pass--;

Fig 5.2 Display function

Dept of CSE, BGSIT, B G Nagara Page 8


Piston Using OpenGL 2022-2023

5.3 RESHAPE FUNCTION:

RESHAPE FUNCTION

glViewport(0, 0, w, h);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluPerspective(65.0, (GLfloat) w / (GLfloat) h, 1.0,


20.0);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glTranslatef(0.0, 0.0, -5.0);

glScalef(1.5, 1.5, 1.5);

Fig 5.3 Reduce function

Dept of CSE, BGSIT, B G Nagara Page 9


Piston Using OpenGL 2022-2023

CHAPTER 6
IMPLEMENTATION
6.1 SOURCE CODE

#include <stdio.h>

#include <GL/glut.h>

#include <math.h>

#define TRUE 1

#define FALSE 0

/* Dimensions of texture image. */

#define IMAGE_WIDTH 64

#define IMAGE_HEIGHT 64

/* Step to be taken for each rotation. */

#define ANGLE_STEP 10

/* Magic numbers for relationship b/w cylinder head and crankshaft. */

#define MAGNITUDE 120

#define PHASE 270.112

#define FREQ_DIV 58

#define ARC_LENGHT 2.7

#define ARC_RADIUS 0.15

/* Rotation angles */

GLdouble view_h = 270, view_v = 0, head_angle = 0;

GLint crank_angle = 0;

/* Crank rotation step. */

GLdouble crank_step = 5;

Dept of CSE, BGSIT, B G Nagara Page 10


Piston Using OpenGL 2022-2023

/* Toggles */

GLshort shaded = TRUE, anim = FALSE;

GLshort texture = FALSE, transparent = FALSE;

GLshort light1 = TRUE, light2 = FALSE;

/* Storage for the angle look up table and the texture map */

GLdouble head_look_up_table[361];

GLubyte image[IMAGE_WIDTH][IMAGE_HEIGHT][3];

/* Indentifiers for each Display list */

GLint list_piston_shaded = 1;

GLint list_piston_texture = 2;

GLint list_flywheel_shaded = 4;

GLint list_flywheel_texture = 8;

/* Variable used in the creaton of glu objects */

GLUquadricObj *obj;

/* Draws a box by scaling a glut cube of size 1. Also checks the

shaded

toggle to see which rendering style to use. NB Texture doesn't work

correctly due to the cube being scaled. */

void

myBox(GLdouble x, GLdouble y, GLdouble z)

glPushMatrix();

glScalef(x, y, z);if (shaded)

glutSolidCube(1);

Dept of CSE, BGSIT, B G Nagara Page 11


Piston Using OpenGL 2022-2023

else

glutWireCube(1);

glPopMatrix();

/* Draws a cylinder using glu function, drawing flat disc's at each

end,

to give the appearence of it being solid. */

void

myCylinder(GLUquadricObj * object, GLdouble outerRadius,

GLdouble innerRadius, GLdouble lenght)

glPushMatrix();

gluCylinder(object, outerRadius, outerRadius, lenght, 20, 1);

glPushMatrix();

glRotatef(180, 0.0, 1.0, 0.0);

gluDisk(object, innerRadius, outerRadius, 20, 1);

glPopMatrix();

glTranslatef(0.0, 0.0, lenght);

gluDisk(object, innerRadius, outerRadius, 20, 1);

glPopMatrix();

/* Draws a piston. */

void

draw_piston(void)

Dept of CSE, BGSIT, B G Nagara Page 12


Piston Using OpenGL 2022-2023

glPushMatrix();

glColor4f(0.3, 0.6, 0.9, 1.0);

glPushMatrix();

glRotatef(90, 0.0, 1.0, 0.0);

glTranslatef(0.0, 0.0, -0.07);

myCylinder(obj, 0.125, 0.06, 0.12);

glPopMatrix();

glRotatef(-90, 1.0, 0.0, 0.0);

glTranslatef(0.0, 0.0, 0.05);

myCylinder(obj, 0.06, 0.0, 0.6);

glTranslatef(0.0, 0.0, 0.6);

myCylinder(obj, 0.2, 0.0, 0.5);

glPopMatrix();

/* Draws the engine pole and the pivot pole for the cylinder head. */

void

draw_engine_pole(void){

glPushMatrix();

glColor4f(0.9, 0.9, 0.9, 1.0);

myBox(0.5, 3.0, 0.5);

glColor3f(0.5, 0.1, 0.5);

glRotatef(90, 0.0, 1.0, 0.0);

glTranslatef(0.0, 0.9, -0.4);

Dept of CSE, BGSIT, B G Nagara Page 13


Piston Using OpenGL 2022-2023

myCylinder(obj, 0.1, 0.0, 2);

glPopMatrix();

/* Draws the cylinder head at the appropreate angle, doing the

necesary

translations for the rotation. */

void

draw_cylinder_head(void)

glPushMatrix();

glColor4f(0.5, 1.0, 0.5, 0.1);

glRotatef(90, 1.0, 0.0, 0.0);

glTranslatef(0, 0.0, 0.4);

glRotatef(head_angle, 1, 0, 0);

glTranslatef(0, 0.0, -0.4);

myCylinder(obj, 0.23, 0.21, 1.6);

glRotatef(180, 1.0, 0.0, 0.0);

gluDisk(obj, 0, 0.23, 20, 1);

glPopMatrix();

/* Draws the flywheel. */

void

draw_flywheel(void)

Dept of CSE, BGSIT, B G Nagara Page 14


Piston Using OpenGL 2022-2023

glPushMatrix();

glColor4f(0.5, 0.5, 1.0, 1.0);

glRotatef(90, 0.0, 1.0, 0.0);

myCylinder(obj, 0.625, 0.08, 0.5);

glPopMatrix();

/* Draws the crank bell, and the pivot pin for the piston. Also calls

the

appropreate display list of a piston doing the nesacary rotations

before

hand. */

void

draw_crankbell(void){

glPushMatrix();

glColor4f(1.0, 0.5, 0.5, 1.0);

glRotatef(90, 0.0, 1.0, 0.0);

myCylinder(obj, 0.3, 0.08, 0.12);

glColor4f(0.5, 0.1, 0.5, 1.0);

glTranslatef(0.0, 0.2, 0.0);

myCylinder(obj, 0.06, 0.0, 0.34);

glTranslatef(0.0, 0.0, 0.22);

glRotatef(90, 0.0, 1.0, 0.0);

glRotatef(crank_angle - head_angle, 1.0, 0.0, 0.0);

if (shaded) {

Dept of CSE, BGSIT, B G Nagara Page 15


Piston Using OpenGL 2022-2023

if (texture)

glCallList(list_piston_texture);

else

glCallList(list_piston_shaded);

} else

draw_piston();

glPopMatrix();

/* Draws the complete crank. Piston also gets drawn through the crank

bell

function. */

void

draw_crank(void)

glPushMatrix();

glRotatef(crank_angle, 1.0, 0.0, 0.0);

glPushMatrix();

glRotatef(90, 0.0, 1.0, 0.0);

glTranslatef(0.0, 0.0, -1.0);

myCylinder(obj, 0.08, 0.0, 1.4);

glPopMatrix();

glPushMatrix();

glTranslatef(0.28, 0.0, 0.0);

draw_crankbell();

Dept of CSE, BGSIT, B G Nagara Page 16


Piston Using OpenGL 2022-2023

glPopMatrix();

glPushMatrix();

glTranslatef(-0.77, 0.0, 0.0);

if (shaded) {

if (texture)

glCallList(list_flywheel_texture);

else

glCallList(list_flywheel_shaded);} else

draw_flywheel();

glPopMatrix();

glPopMatrix();

/* Main display routine. Clears the drawing buffer and if transparency

is

set, displays the model twice, 1st time accepting those fragments

with

a ALPHA value of 1 only, then with DEPTH_BUFFER writing disabled

for

those with other values. */

void

display(void)

int pass;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

Dept of CSE, BGSIT, B G Nagara Page 17


Piston Using OpenGL 2022-2023

glPushMatrix();

if (transparent) {

glEnable(GL_ALPHA_TEST);

pass = 2;

} else {

glDisable(GL_ALPHA_TEST);

pass = 0;

/* Rotate the whole model */

glRotatef(view_h, 0, 1, 0);

glRotatef(view_v, 1, 0, 0);

do {

if (pass == 2) {

glAlphaFunc(GL_EQUAL, 1);

glDepthMask(GL_TRUE);

pass--;

} else if (pass != 0) {

glAlphaFunc(GL_NOTEQUAL, 1);

glDepthMask(GL_FALSE);

pass--;

draw_engine_pole();

glPushMatrix();

glTranslatef(0.5, 1.4, 0.0);

Dept of CSE, BGSIT, B G Nagara Page 18


Piston Using OpenGL 2022-2023

draw_cylinder_head();

glPopMatrix();

glPushMatrix();glTranslatef(0.0, -0.8, 0.0);

draw_crank();

glPopMatrix();

} while (pass > 0);

glDepthMask(GL_TRUE);

glutSwapBuffers();

glPopMatrix();

/* Called when the window is idle. When called increments the crank

angle

by ANGLE_STEP, updates the head angle and notifies the system that

the screen needs to be updated. */

void

animation(void)

if ((crank_angle += crank_step) >= 360)

crank_angle = 0;

head_angle = head_look_up_table[crank_angle];

glutPostRedisplay();

/* Called when a key is pressed. Checks if it reconises the key and if

so

Dept of CSE, BGSIT, B G Nagara Page 19


Piston Using OpenGL 2022-2023

acts on it, updateing the screen. */

/* ARGSUSED1 */

void

keyboard(unsigned char key, int x, int y)

switch (key) {

case 's':

if (shaded == FALSE) {

shaded = TRUE;

glShadeModel(GL_SMOOTH);

glEnable(GL_LIGHTING);

glEnable(GL_DEPTH_TEST);

glEnable(GL_COLOR_MATERIAL);

gluQuadricNormals(obj, GLU_SMOOTH);

gluQuadricDrawStyle(obj, GLU_FILL);

} else {

shaded = FALSE;

glShadeModel(GL_FLAT);

glDisable(GL_LIGHTING);

glDisable(GL_DEPTH_TEST);

glDisable(GL_COLOR_MATERIAL);

gluQuadricNormals(obj, GLU_NONE);gluQuadricDrawStyle(obj, GLU_LINE);

gluQuadricTexture(obj, GL_FALSE);

Dept of CSE, BGSIT, B G Nagara Page 20


Piston Using OpenGL 2022-2023

if (texture && !shaded);

else

break;

case 't':

if (texture == FALSE) {

texture = TRUE;

glEnable(GL_TEXTURE_2D);

gluQuadricTexture(obj, GL_TRUE);

} else {

texture = FALSE;

glDisable(GL_TEXTURE_2D);

gluQuadricTexture(obj, GL_FALSE);

break;

case 'o':

if (transparent == FALSE) {

transparent = TRUE;

} else {

transparent = FALSE;

break;

case 'a':

if ((crank_angle += crank_step) >= 360)

crank_angle = 0;

Dept of CSE, BGSIT, B G Nagara Page 21


Piston Using OpenGL 2022-2023

head_angle = head_look_up_table[crank_angle];

break;

case 'z':

if ((crank_angle -= crank_step) <= 0)

crank_angle = 360;

head_angle = head_look_up_table[crank_angle];

break;

case '0':

if (light1) {

glDisable(GL_LIGHT0);

light1 = FALSE;

} else {

glEnable(GL_LIGHT0);

light1 = TRUE;

break;

case '1':if (light2) {

glDisable(GL_LIGHT1);

light2 = FALSE;

} else {

glEnable(GL_LIGHT1);

light2 = TRUE;

break;

Dept of CSE, BGSIT, B G Nagara Page 22


Piston Using OpenGL 2022-2023

case '4':

if ((view_h -= ANGLE_STEP) <= 0)

view_h = 360;

break;

case '6':

if ((view_h += ANGLE_STEP) >= 360)

view_h = 0;

break;

case '8':

if ((view_v += ANGLE_STEP) >= 360)

view_v = 0;

break;

case '2':

if ((view_v -= ANGLE_STEP) <= 0)

view_v = 360;

break;

case ' ':

if (anim) {

glutIdleFunc(0);

anim = FALSE;

} else {

glutIdleFunc(animation);

anim = TRUE;

Dept of CSE, BGSIT, B G Nagara Page 23


Piston Using OpenGL 2022-2023

break;

case '+':

/* Initialises the menu of toggles. */

void

create_menu(void)

glutCreateMenu(menu);

glutAttachMenu(GLUT_RIGHT_BUTTON);

glutAddMenuEntry("Shaded", 1);

glutAddMenuEntry("Animation", 2);

glutAddMenuEntry("Texture", 3);

glutAddMenuEntry("Transparency", 4);

glutAddMenuEntry("Right Light (0)", 5);

glutAddMenuEntry("Left Light (1)", 6);

glutAddMenuEntry("Speed UP", 7);

glutAddMenuEntry("Slow Down", 8);

/* Makes a simple check pattern image. (Copied from the redbook

example

"checker.c".) */

void

make_image(void)

Dept of CSE, BGSIT, B G Nagara Page 24


Piston Using OpenGL 2022-2023

int i, j, c;

for (i = 0; i < IMAGE_WIDTH; i++) {for (j = 0; j < IMAGE_HEIGHT; j++) {

c = (((i & 0x8) == 0) ^ ((j & 0x8) == 0)) * 255;

image[i][j][0] = (GLubyte) c;

image[i][j][1] = (GLubyte) c;

image[i][j][2] = (GLubyte) c;

/* Makes the head look up table for all possible crank angles. */

void

make_table(void)

GLint i;

GLdouble k;

for (i = 0, k = 0.0; i < 360; i++, k++) {

head_look_up_table[i] =

MAGNITUDE * atan(

(ARC_RADIUS * sin(PHASE - k / FREQ_DIV)) /

((ARC_LENGHT - ARC_RADIUS * cos(PHASE - k / FREQ_DIV))));

/* Initialises texturing, lighting, display lists, and everything else

associated with the model. */

Dept of CSE, BGSIT, B G Nagara Page 25


Piston Using OpenGL 2022-2023

void

myinit(void)

GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0};

GLfloat mat_shininess[] = {50.0};

GLfloat light_position1[] = {1.0, 1.0, 1.0, 0.0};

GLfloat light_position2[] = {-1.0, 1.0, 1.0, 0.0};

glClearColor(0.0, 0.0, 0.0, 0.0);

obj = gluNewQuadric();

make_table();

make_image();

/* Set up Texturing */

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

glTexImage2D(GL_TEXTURE_2D, 0, 3, IMAGE_WIDTH,

IMAGE_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE,

image);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);/* Set up


Lighting */

glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);

glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);

Dept of CSE, BGSIT, B G Nagara Page 26


Piston Using OpenGL 2022-2023

glLightfv(GL_LIGHT0, GL_POSITION, light_position1);

glLightfv(GL_LIGHT1, GL_POSITION, light_position2);

gluQuadricTexture(obj, GL_FALSE);

/* Called when the model's window has been reshaped. */

void

myReshape(int w, int h)

glViewport(0, 0, w, h);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluPerspective(65.0, (GLfloat) w / (GLfloat) h, 1.0, 20.0);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glTranslatef(0.0, 0.0, -5.0); /* viewing transform */

glScalef(1.5, 1.5, 1.5);

}/* Main program. An interactive model of a miniture steam engine.

Sets system in Double Buffered mode and initialises all the call-

back

functions. */

int

main(int argc, char **argv)

puts("Steam Engine\n");

Dept of CSE, BGSIT, B G Nagara Page 27


Piston Using OpenGL 2022-2023

puts("Keypad Arrow keys (with NUM_LOCK on) rotates object.");

puts("Rotate crank: 'a' = anti-clock wise 'z' = clock wise");

puts("Crank Speed : '+' = Speed up by 1 '-' = Slow Down by 1");

puts("Toggle : 's' = Shading 't' = Texture");

puts(" : ' ' = Animation 'o' = Transparency");

puts(" : '0' = Right Light '1' = Left Light");

puts(" Alternatively a pop up menu with all toggles is attached");

puts(" to the left mouse button.\n");

glutInitWindowSize(400, 400);

glutInit(&argc, argv);

/* Transperancy won't work properly without GLUT_ALPHA */

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH |

GLUT_MULTISAMPLE);

glutCreateWindow("Steam Engine");

glutDisplayFunc(display);

glutKeyboardFunc(keyboard);

glutSpecialFunc(special);

create_menu();

myinit();

glutReshapeFunc(myReshape);

glutMainLoop();

return 0; /* ANSI C requires main to return int. */

Dept of CSE, BGSIT, B G Nagara Page 28


Piston Using OpenGL 2022-2023

CHAPTER 7

SNAPSHOTS

Fig 7.1: Initial View

Fig 7.2: Menu associated with Right Mouse Button


Dept of CSE, BGSIT, B G Nagara Page 29
Piston Using OpenGL 2022-2023

Fig 7.3: Rotating Steam Engine

Fig 7.4: Shading

Dept of CSE, BGSIT, B G Nagara Page 30


Piston Using OpenGL 2022-2023

Fig 7.5: Texture

Fig 7.6: No Light (Transparency)

Dept of CSE, BGSIT, B G Nagara Page 31


Piston Using OpenGL 2022-2023

CONCLUSION AND FUTURE SCOPE


This project allows the user to rotate the piston in a Steam Engine. Its like a Miniature Steam
EngineSimulation.

FUTURE SCOPE
1.SIMULATOR
Not only the movement of piston, we can make the whole parts in the steam engine
working so that it will be a simulator of steam engine. By modifying this project we can construct
a fully fledged simulator. Students who are studying about the steam engine can work through
this andit will be very helpful for them. Almost a complete picturization of a steam can be done
through this.

2.DESIGN OF PISTON
Engineers who build Steam Engines can design their model by looking this project.
They get a good picturization by seeing this and it will be helpful for them in building pistons.So
this project will be benifited by engineers.

Dept of CSE, BGSIT, B G Nagara Page 32


Piston Using OpenGL 2022-2023

REFERENCES
[1] Interactive Computer Graphics A Top-Down Approach with OpenGL - Edward Angel,
5th Edition, Addison-Wesley, 2008

[2] The Official Guide to Learning OpenGL, by Jackie Neider, Tom Davis, Mason
Woo(THE RED BOOK)

[3] OpenGL Super Bible by Richard S. Wright, Jr. and Michael Sweet

[4] http://www.opengl.org/ online man pages.

[5] And last GOOGLE.

Dept of CSE, BGSIT, B G Nagara Page 33

You might also like