CG Final
CG Final
CG Final
Bachelor of Engineering
In
Computer Science and Engineering
Submitted by,
LAVANYA K V 20CSE039
LEKHANA H L 20CSE040
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.
External Viva
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.
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
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
iii
LIST OF FIGURES
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.
• Z buffering.
• Texture Mapping
• Alpha.
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
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.
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.
CHAPTER 3
REQUIREMENTS SPECIFICATION
3.3 HARDWARE REQUIREMENTS
• Processor : Pentium PC
• RAM : 512MB
• Language : Open GL
CHAPTER 4
SOFTWARE DESIGN
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.
CHAPTER 5
PROJECT DESCRIPTION
KEYBOARD
FUNCTION
case '1':
case ' ':
if (light2) {
glDisable(GL_LIGHT1); if (anim) {
DISPLAY FUNCTION
glPushMatrix();
if (transparent) {
glEnable(GL_ALPHA_TEST); draw_engine_pole();
pass = 2; glPushMatrix();
} 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--;
RESHAPE FUNCTION
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
CHAPTER 6
IMPLEMENTATION
6.1 SOURCE CODE
#include <stdio.h>
#include <GL/glut.h>
#include <math.h>
#define TRUE 1
#define FALSE 0
#define IMAGE_WIDTH 64
#define IMAGE_HEIGHT 64
#define ANGLE_STEP 10
#define FREQ_DIV 58
/* Rotation angles */
GLint crank_angle = 0;
GLdouble crank_step = 5;
/* Toggles */
/* Storage for the angle look up table and the texture map */
GLdouble head_look_up_table[361];
GLubyte image[IMAGE_WIDTH][IMAGE_HEIGHT][3];
GLint list_piston_shaded = 1;
GLint list_piston_texture = 2;
GLint list_flywheel_shaded = 4;
GLint list_flywheel_texture = 8;
GLUquadricObj *obj;
shaded
void
glPushMatrix();
glutSolidCube(1);
else
glutWireCube(1);
glPopMatrix();
end,
void
glPushMatrix();
glPushMatrix();
glPopMatrix();
glPopMatrix();
/* Draws a piston. */
void
draw_piston(void)
glPushMatrix();
glPushMatrix();
glPopMatrix();
glPopMatrix();
/* Draws the engine pole and the pivot pole for the cylinder head. */
void
draw_engine_pole(void){
glPushMatrix();
glPopMatrix();
necesary
void
draw_cylinder_head(void)
glPushMatrix();
glRotatef(head_angle, 1, 0, 0);
glPopMatrix();
void
draw_flywheel(void)
glPushMatrix();
glPopMatrix();
/* Draws the crank bell, and the pivot pin for the piston. Also calls
the
before
hand. */
void
draw_crankbell(void){
glPushMatrix();
if (shaded) {
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();
glPushMatrix();
glPopMatrix();
glPushMatrix();
draw_crankbell();
glPopMatrix();
glPushMatrix();
if (shaded) {
if (texture)
glCallList(list_flywheel_texture);
else
glCallList(list_flywheel_shaded);} else
draw_flywheel();
glPopMatrix();
glPopMatrix();
is
set, displays the model twice, 1st time accepting those fragments
with
for
void
display(void)
int pass;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
if (transparent) {
glEnable(GL_ALPHA_TEST);
pass = 2;
} else {
glDisable(GL_ALPHA_TEST);
pass = 0;
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();
draw_cylinder_head();
glPopMatrix();
draw_crank();
glPopMatrix();
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
void
animation(void)
crank_angle = 0;
head_angle = head_look_up_table[crank_angle];
glutPostRedisplay();
so
/* ARGSUSED1 */
void
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);
gluQuadricTexture(obj, GL_FALSE);
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':
crank_angle = 0;
head_angle = head_look_up_table[crank_angle];
break;
case 'z':
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;
glDisable(GL_LIGHT1);
light2 = FALSE;
} else {
glEnable(GL_LIGHT1);
light2 = TRUE;
break;
case '4':
view_h = 360;
break;
case '6':
view_h = 0;
break;
case '8':
view_v = 0;
break;
case '2':
view_v = 360;
break;
if (anim) {
glutIdleFunc(0);
anim = FALSE;
} else {
glutIdleFunc(animation);
anim = TRUE;
break;
case '+':
void
create_menu(void)
glutCreateMenu(menu);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutAddMenuEntry("Shaded", 1);
glutAddMenuEntry("Animation", 2);
glutAddMenuEntry("Texture", 3);
glutAddMenuEntry("Transparency", 4);
example
"checker.c".) */
void
make_image(void)
int i, j, c;
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;
head_look_up_table[i] =
MAGNITUDE * atan(
void
myinit(void)
obj = gluNewQuadric();
make_table();
make_image();
/* Set up Texturing */
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, 3, IMAGE_WIDTH,
image);
gluQuadricTexture(obj, GL_FALSE);
void
myReshape(int w, int h)
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
Sets system in Double Buffered mode and initialises all the call-
back
functions. */
int
puts("Steam Engine\n");
glutInitWindowSize(400, 400);
glutInit(&argc, argv);
GLUT_MULTISAMPLE);
glutCreateWindow("Steam Engine");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutSpecialFunc(special);
create_menu();
myinit();
glutReshapeFunc(myReshape);
glutMainLoop();
CHAPTER 7
SNAPSHOTS
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.
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