You are on page 1of 15

Vidyavardhini’s college of Engineering & Technology Department

of Computer Engineering

ABSTRACT

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

language, 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. OpenGL is managed by a non-profit technology

consortium, the Khronos Group.

We have OpenGL to create a realistic looking 3D model of a cube who’s parameters

can be easily modified to present a new perspective of a cube to the user.


Vidyavardhini’s college of Engineering & Technology Department
of Computer Engineering

INTRODUCTION:

OpenGL is the premier environment for developing portable, interactive 2D

and 3D graphics applications. Since its introduction in 1992, OpenGL has

become the industry's most widely used and supported 2D and 3D graphics

application programming interface (API), bringing thousands of applications

to a wide variety of computer platforms. OpenGL fosters innovation and

speeds application development by incorporating a broad set of rendering,

texture mapping, special effects, and other powerful visualization functions.

Developers can leverage the power of OpenGL across all popular desktop

and workstation platforms, ensuring wide application deployment.

Developer-Driven Advantages

• Industry standard
An independent consortium, the OpenGL Architecture Review Board,
guides the OpenGL specification. With broad industry support, OpenGL
is the only truly open, vendor-neutral, multiplatform graphics standard.
• Stable
OpenGL implementations have been available for more than seven
years on a wide variety of platforms. Additions to the specification are
well controlled, and proposed updates are announced in time for
developers to adopt changes. Backward compatibility requirements
ensure that existing applications do not become obsolete.
• Reliable and portable
All OpenGL applications produce consistent visual display results on
Vidyavardhini’s college of Engineering & Technology Department
of Computer Engineering

any OpenGL API-compliant hardware, regardless of operating system


or windowing system.
• Evolving
Because of its thorough and forward-looking design, OpenGL allows
new hardware innovations to be accessible through the API via the
OpenGL extension mechanism. In this way, innovations appear in the
API in a timely fashion, letting application developers and hardware
vendors incorporate new features into their normal product release
cycles.
• Scalable
OpenGL API-based applications can run on systems ranging from
consumer electronics to PCs, workstations, and supercomputers. As a
result, applications can scale to any class of machine that the
developer chooses to target.
• Easy to use
OpenGL is well structured with an intuitive design and logical
commands. Efficient OpenGL routines typically result in applications
with fewer lines of code than those that make up programs generated
using other graphics libraries or packages. In addition, OpenGL drivers
encapsulate information about the underlying hardware, freeing the
application developer from having to design for specific hardware
features.
• Well-documented
Numerous books have been published about OpenGL, and a great deal
of sample code is readily available, making information about OpenGL
inexpensive and easy to obtain.

Keeping these powerful advantages of OpenGL in mind, we set out to create


a complex 3D model of a basic geometric figure.

For this purpose we decided to recreate a cube on the digital canvas.

In geometry, a cube is a three-dimensional solid object bounded by six


square faces, facets or sides, with three meeting at each vertex. The cube
can also be called a regular hexahedron and is one of the five Platonic
Vidyavardhini’s college of Engineering & Technology Department
of Computer Engineering

solids. It is a special kind of square prism, of rectangular parallelepiped and


of trigonal trapezohedron. The cube is dual to the octahedron. It has cubical
symmetry (also called octahedral symmetry).

OBJECTIVES:

To study and implement OpenGL and its associated technologies, which are
widely used in todays industries and from games to virtual reality,
mobile phones to supercomputers

• To create a realistic cube using OpenGL.


Vidyavardhini’s college of Engineering & Technology Department
of Computer Engineering

Advantages of OpenGL:

• OpenGL advantages:
o device independent
o Fast! If hardware acceleration exists, then high quality graphics
possible in realtime
o 2D and 3D (3D will be extension of 2D shapes, but in 3D
coordinate space)
o sophisticated features: shadows, fog, textures, lighting effects
(later in course)
o popular standard: OpenGL applications may begin to appear for
PC's
• drawing primitives covered thus far have limitations:
o OpenGL primitives looked at do not retain the geometric shapes
drawn: they simply writes over pixels in its raster display
o If you want to delete a line, rescale a square, or do other re-
editing, the application program has to send additional
commands to do it;
o akin to using MacPaint; compare with MacDraw, in which you can
grab objects (lines, squares, .,..) rescale them, and move them
about screen
o the openGL application should retain model information
o Some graphics libraries will retain data structures that save the
geometric shapes:
Vidyavardhini’s college of Engineering & Technology Department
of Computer Engineering

 eg. VRML
 But also see OpenGL's display lists!
• glut library:
o comprehensive
o Windows-based systems will permit other I/O to be used

Disadvantages of OpenGL:

If you want to use advanced graphics output, you have to decide, whether
you prefer OpenGL or DirectX. Each of them has its specific advantages and
disadvantages. While OpenGL exists on all major platforms (Linux, Windows,
Macintosh,…) DirectX is limited to Windows, but on the other hand DirectX
supports multimedia, which OpenGL is just starting to support (the so called
OpenML). Due to the platform independence of OpenGL, I will use OpenGL to
show how advanced graphics can be implemented.

OpenGL is a software interface that allows you to access the graphics


hardware without taking care of the hardware details or which graphics
adapter is in the system. To become more independent of the platform and
programming language, OpenGL provides it's own data types. In the
following list you can find a summary of the most important data types and
their equivalent in standard C:

OpenGL Data Internal Defined as C C Literal


Type Representation Type Suffix
GLbyte 8-bit integer Signed char b
GLshort 16-bit integer Short s
GLint, GLsizei 32-bit integer Long I
GLfloat, GLclampf 32-bit floating point Float f
GLdouble, 64-bit floating point Double d
GLclampd
Vidyavardhini’s college of Engineering & Technology Department
of Computer Engineering

GLubyte, Unsigned char ub


8-bit unsigned integer
GLboolean
GLushort 16-bit unsigned Unsigned short us
integer
GLuint, GLenum, 32-bit unsigned Unsigned long ui
GLbitfield integer

CODE:

/*Program coded by Apurva Veldurkar, Pritesh Desai and Tarun Maheshwari*/

#include <GL/glut.h>

GLfloat light_diffuse[] = {1.0, 0.0, 0.0, 1.0}; /* Red diffuse light. */


GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0}; /* Infinite light location.
*/
GLfloat n[6][3] = {

/* Normals for the 6 faces of a cube. */

{-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},


{0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0} };
GLint faces[6][4] = {

/* Vertex indices for the 6 faces of a cube. */

{0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},


{4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3} };
GLfloat v[8][3];

/* Will be filled in with X,Y,Z vertexes. */

void
Vidyavardhini’s college of Engineering & Technology Department
of Computer Engineering

drawBox(void)
{
int i;

for (i = 0; i < 6; i++) {


glBegin(GL_QUADS);
glNormal3fv(&n[i][0]);
glVertex3fv(&v[faces[i][0]][0]);
glVertex3fv(&v[faces[i][1]][0]);
glVertex3fv(&v[faces[i][2]][0]);
glVertex3fv(&v[faces[i][3]][0]);
glEnd();
}
}

void
display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawBox();
glutSwapBuffers();
}

void
init(void)
{
/* Setup cube vertex data. */

v[0][0] = v[1][0] = v[2][0] = v[3][0] = -1;


v[4][0] = v[5][0] = v[6][0] = v[7][0] = 1;
v[0][1] = v[1][1] = v[4][1] = v[5][1] = -1;
v[2][1] = v[3][1] = v[6][1] = v[7][1] = 1;
v[0][2] = v[3][2] = v[4][2] = v[7][2] = 1;
v[1][2] = v[2][2] = v[5][2] = v[6][2] = -1;

/* Enable a single OpenGL light. */

glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);


glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);

/* Use depth buffering for hidden surface elimination. */

glEnable(GL_DEPTH_TEST);

/* Setup the view of the cube. */

glMatrixMode(GL_PROJECTION);
gluPerspective(

/* field of view in degree */


Vidyavardhini’s college of Engineering & Technology Department
of Computer Engineering

40.0,

/* aspect ratio */

1.0,

/* Z near */

1.0,

/* Z far */

10.0);
glMatrixMode(GL_MODELVIEW);
gluLookAt(0.0, 0.0, 5.0,

/* eye is at (0,0,5) */

0.0, 0.0, 0.0,

/* center is at (0,0,0) */

0.0, 1.0, 0.);

/* up is in positive Y direction */

/* Adjust cube position to be asthetic angle. */

glTranslatef(0.0, 0.0, -1.0);


glRotatef(60, 1.0, 0.0, 0.0);
glRotatef(-45, 0.0, 0.0, 1.0);
}

int
main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("red 3D lighted cube");
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;

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


}
Vidyavardhini’s college of Engineering & Technology Department
of Computer Engineering

ANALYSIS:

GLfloat light_diffuse[ ]
This will create diffused RED light

GLfloat n[ ][ ]
Creates normals for the 6 faces of a cube.

GLint faces[ ][ ]
Creates vertex indices for the 6 faces of a cube.

drawBox(void)
This function draws a box.

display(void)
This function calls the various functions required to display the3D model.

init(void)
Initializes the current position of the cube.
Vidyavardhini’s college of Engineering & Technology Department
of Computer Engineering

OUTPUT:
Vidyavardhini’s college of Engineering & Technology Department
of Computer Engineering

Result:

We have successfully leveraged the power of OpenGL to create a realistic 3D object.

We can view the model from any angle in a 3D surroundings.


Vidyavardhini’s college of Engineering & Technology Department
of Computer Engineering

CREDIT:

We would like to thank our project guide and class-incharge Prof. Sunil Katkar

without whom this project would not have been possible. We would also like to

thank the non-profit technology consortium, the Khronos Group for creating this

wonderful piece of software which powers leading industries and games.


Vidyavardhini’s college of Engineering & Technology Department
of Computer Engineering

REFERENCES

[1] The official OpenGL site.

http://www.opengl.org

[2] Guide to OpenGL on Windows from Silicon Graphics

http://www.opengl.org/developers/documentation/overviews.h
tml

[3] OpenGL Win32 Tutorial

http://www.nullterminator.net/opengl32.html

http://www.opengl.org/developers/code/tutorials.html
Vidyavardhini’s college of Engineering & Technology Department
of Computer Engineering

[4] Wikipedia page on OpenGL

http://en.wikipedia.org/wiki/OpenGL

You might also like