You are on page 1of 28

Lecture No.

5: OpenGL (Open Graphics Library)


OpenGL terrain generator using fractals

Terrain is the vertical and horizontal dimension of land surface


I REPRESENTATION OF OBJECTS ON THE SCREEN

- Points, Lines, Curves

- Geometrical Objects
- Circle
- Ellipse
- Rectangle

- Polygons, Polylines

- Transformations – 2D
- Windowing and Viewport
- Mapping of Objects

- Curves and Surfaces


- Bezier Curves

- Projections

- Hidden Surface Removal

- Rendering
- Light Effects
- Shades and Shadows
- Textures
- Ray Tracing
- Illumination
III SOFTWARE STANDARDS

Goal of standardized software is portability (ie, one thing


designed for one system can be used on other system). 4. Open Inventor
Also re-writing of code is not required.
5. Virtual Reality Modelling Language (VRML)
Number of Graphics software standards came into
existence. Some of these are as follows: 6. Java 2D : Create 2D scenes within Java applets

1. GKS (Graphics Kernel System) 1984 : First software 7. Java 3D : Produce 3D web displays
standard accepted by ISO including ANSI --- First 2D
then extended to 3D 8. RenderMan Interface by Pixar Corporation:
Produces scenes by using lightning models
2. PHIGS (Programmer’s Hierarchical Interactive
Graphics System): 9. Mathematica (provides Graphics Libraries)
• It was extension of GKS.
• New features like rendering, colour 10. Matlab (provides Graphics Libraries)
specifications, picture manipulation were
added. 11. Maple (provides Graphics Libraries)

3. GL (Graphics Libraries) by Silicon Graphics Inc. (SGI)


• SGI’s Graphics became increasingly popular
• They came up with set of routines called GL
(Graphics Libraries)
• Became popular in Graphics Community
• Disadvantage: ….These were platform
dependent (On the other hand, OpenGL is
platform independent)
What is OpenGL ?
OpenGL (Open Graphics Library) is the computer industry's standard application program
interface ( API ) for defining 2-D and 3-D graphic images.

OpenGL is a cross-language, multi-platform application programming interface (API) for


rendering 2D and 3D vector graphics. OpenGL is designed as a streamlined, hardware-
independent interface to be implemented on many different hardware platforms.

• This interface consists of about 150 distinct


commands that we use to specify the objects and
operations needed to produce interactive three-
dimensional applications.

• With OpenGL, we can build up any desired model


from a small set of geometric primitives - points,
lines, and polygons.

Tip: An application programming interface (API) is a set of routines, protocols, and tools for building software applications.
OpenGL Architecture
OpenGL Hierarchy
• GL (Graphic Library)
• Lowest level: vertex, matrix manipulation
• Eg:- glVertex3f(point.x, point.y, point.z)

• GLU (Graphic Library Utility)


• Provides functions/routines for setting up:
• Viewing and Projection Matrices
• Displaying quadrics and B-Splines
• Processing Surface-rendering operations
• Helper functions for shapes, transformations
• Eg:- gluPerspective( fovy, aspect, near, far )

• GLUT (Graphic Library Utility Toolkit)


• Highest level: Window and interface management
• Provides library of functions for interacting with any scree-windowing system.
• Contains functions for describing quadric curves and surfaces.
• Eg:- glutInitWindowSize()
Things used in OpenGL Program:
OpenGL program is case sensitive. Eg: We need to write GL in capitals
✓ Basic Operations:
• Primitive functions – it includes main functions (like - line drawing,
polygon, triangle, etc.)
• Symbolic Constants
• Built-in Data types

Several system libraries are there…Depends upon the type of computer


✓ OpenGL Libraries
we are using. Eg:
- AppleGL (AGL) – For Apple Systems – Prefix agl
- Windows-to-OpenGL (WGL) – For Windows systems
- Presentation Manager to OpenGL (PGL) – for IBM OS/2 - Prefix pgl
✓ Header Files
For Windows,
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h> For Apple OSX systems,
#include <GL/glut.h> #include <GLUT/glut.h>
✓ Need to create a Window
PRIMITIVE FUNCTIONS

Function names are prefixed with gl and each Component name with a Capital letter
Eg:
glBegin(with set of primitives)
glBegin(GL_LINES);
glClear()
glVertex2i(180,15);
glEnd()
glVertex2i(10,145);
glCopyPixel()
glEnd();
glPolygonMode()

Some Primitives are:


• GL_POINTS
• GL_LINES
• GL_TRIANGLES
• GL_QUADS
SYMBOLIC CONSTANTS
• Hundreds of symbolic constants are available in OpenGL
• Written in CAPITAL letters

Eg:
GL_2D
GL_RGB
GL_POLYGON

DATA TYPES
Special built-in data types are there in OpenGL

Eg:
GLbyte
GLshort
GLint
GLfloat
GLdouble
GLboolean
OpenGL WINDOW

To create any graphics display/object using OpenGL, we first need to setup a DISPLAY WINDOW
(rectangular area where our picture will be displayed)

100 Monitor Screen

50

300

400

OpenGL Display Window


Writing OpenGL Program:
To initialize GLUT:
glutInit(&argc, argv);
To create Display Window:

glutCreateWindow(“My First OpenGL Window”);

What a Display Window is to contain?

glutDisplayFunc(lineSegment);

Display Window is not yet on the screen

All Display Windows we have created are activated by using the following function:

glutMainLoop();
It must be put at the last in the program
Positioning a Window
glutInitWindowPosition(50,100);
initial
Specifying the Window Size:
glutInitWindowSize(400, 300);

For Display Window, we can choose a background colour (eg. White) :


glClearColor(1.0, 1.0, 1.0, 0);
R G B Alpha Value
To actually put display 0 = Totally Transparent Object
window on screen with 1 = Opaque Object
the colour values, we
need to invoke one more
function

glClearColor(GL_COLOR_BUFFER_BIT);
Setting Object Color:
glColor3f(0.0, 0.4, 0.2);
indicates that we are specifying 3 RGB color components using floating point values

OpenGL works for 3D

To Display Line Segment (say) in 2D, we still have to use 3D viewing operations.

For this, we need following two functions:

glMatrixMode(GL_PROJECTION);

gluOrtho2D(0.0, 200.0, 0.0, 150. 0);

X Values Y Values
OpenGL Program to plot a line

#include<iostream>
#include<GL/glut.h>

void init()
{ glClearColor(0.0, 0.0, 0.0, 0.0); // Set display window colour to white
glMatrixMode(GL_PROJECTION); // Set Projection Parameters
gluOrtho2D(0.0, 250.0, 0.0, 150.0); //Values of xMin, yMin, xMax, yMax
}

void Line()
{ glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINES);
glVertex2i(180,15);
glVertex2i(10,145);
glEnd();
glFlush();
} Output
int main(int agc, char** argv)
{ glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize(400, 300);
glutInitWindowPosition(50, 100);

glutCreateWindow(“Line Drawing");
init();
glutDisplayFunc(Line);
glutMainLoop();
return 0;
}
Anoder Code Example
void Display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 0.0);
glBegin(GL_POLYGON);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
glFlush();
}
….

You might also like