0% found this document useful (0 votes)
89 views61 pages

Open GL

OpenGL is a graphics library for drawing 2D and 3D graphics. It serves as an interface between application programs and graphics hardware. The lecture introduced the OpenGL rendering pipeline which transforms geometric primitives from an application into pixels on the screen. Key components of the pipeline include vertex transformation, primitive assembly, rasterization, and fragment operations. The lecture also discussed OpenGL abstractions for viewing, windowing, primitives, shading, and transformations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views61 pages

Open GL

OpenGL is a graphics library for drawing 2D and 3D graphics. It serves as an interface between application programs and graphics hardware. The lecture introduced the OpenGL rendering pipeline which transforms geometric primitives from an application into pixels on the screen. Key components of the pipeline include vertex transformation, primitive assembly, rasterization, and fragment operations. The lecture also discussed OpenGL abstractions for viewing, windowing, primitives, shading, and transformations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 61

CS552: Computer Graphics

Lecture 2: Introduction to OpenGL


Objective

• After this lecture students will be able to


o Explain the importance of OpenGL
o Install OpenGL in their system
o Write program using OpenGL libraries
The BIG Picture

There really is no such thing as an


“OpenGL program”
The BIG picture

Application Graphics
Hardware
Program Library (API)

OpenGL

A low-level interface to the


graphics hardware
Illustration
Graphics
lights
(photons)

objects image
(triangles) (pixels)

viewer
Physical reality (sort of)
for (each photon)
for (each triangle) lights
(photons)
for (each pixel)
draw;

objects image
(triangles) (pixels)
Ray tracing
for (each pixel)
for (each triangle) lights
(photons)
for (each light)
draw;

objects image
(triangles) (pixels)
Physical reality (sort of)
for (each light)
for (each triangle)
lights
for (each pixel)
draw;

objects image
(triangles) (pixels)
Traditional graphics pipeline (OpenGL)
for (each triangle)
for (each light)
lights
for (each pixel)
draw;

objects image
(triangles) (pixels)
Modern graphics pipeline (OpenGL 2.1)
for (each triangle)
for (each pixel)
lights
for (each light)
draw;

objects image
(triangles) (pixels)
What is OpenGL?

• OpenGL is a software interface to graphics hardware.

• OpenGL is designed as a streamlined, hardware-independent


interface to be implemented
o Runs on many different hardware platforms.

• With OpenGL, you must build your desired model from a small set
of geometric primitives
o Points, lines, and polygons.
What OpenGL doesn’t do?

• The OpenGL API itself is not a programming language like C or C+


+.

• No commands for performing windowing tasks or obtaining user


input are included in OpenGL

• Doesn't provide high-level commands for describing models of


three-dimensional objects.
What is does do?

• Construct shapes from geometric primitives, thereby creating


mathematical descriptions of objects.

• Arrange the objects in three-dimensional space and select the


desired vantage point for viewing the composed scene.

• Calculate the colors of all the objects

• Convert the mathematical description of objects and their


associated color information to pixels on the screen.
OpenGL Rendering Pipeline
Geometry Path

Vertex Vertex Primitive


Data Operation Assembly

Display Texture Fragment Frame


Rasterization
List Memory Operation Buffer

Pixel
Pixel
Transfer
Data
Operation

Image Path
OpenGL Rendering Pipeline
Geometry Path It is a group of OpenGL
commands that have been
stored (compiled) for later
Vertex Vertex Primitive execution.
Data Operation Assembly

Display Texture Fragment Frame


Rasterization
List Memory Operation Buffer

Pixel
Pixel
Transfer
Data
Operation

Image Path
OpenGL Rendering Pipeline
Geometry Path Each vertex and normal coordinates are
transformed by GL_MODELVIEW matrix

Vertex Vertex Primitive


Data Operation Assembly

Display Texture Fragment Frame


Rasterization
List Memory Operation Buffer

Pixel
Pixel
Transfer
Data
Operation

Image Path
OpenGL Rendering Pipeline
• Primitives are transformed eye
Geometry Path coordinates to clip coordinates.
• Viewport transform is applied in
order to map 3D scene to window
Vertex Primitive space coordinates.
Vertex
• Last thing to do in Primitive
Data Operation Assembly
Assembly is culling test if culling is
enabled.

Display Texture Fragment Frame


Rasterization
List Memory Operation Buffer

Pixel
Pixel
Transfer
Data
Operation

Image Path
OpenGL Rendering Pipeline
• After reading the pixels, the data
Geometry Path are performed scaling, bias,
mapping and clamping.

Vertex Vertex Primitive • The transferred data are either


Data Operation Assembly stored in texture memory or
rasterized directly to fragments.

Display Texture Fragment Frame


Rasterization
List Memory Operation Buffer

Pixel
Pixel
Transfer
Data
Operation

Image Path
OpenGL Rendering Pipeline
Geometry Path Texture images are loaded into
texture memory to be applied onto
geometric objects.
Vertex Vertex Primitive
Data Operation Assembly

Display Texture Fragment Frame


Rasterization
List Memory Operation Buffer

Pixel
Pixel
Transfer
Data
Operation

Image Path
OpenGL Rendering Pipeline
• It is the conversion of both
Geometry Path geometric and pixel data into
fragment.

Vertex Vertex Primitive • Fragments are a rectangular


Data Operation Assembly array containing color, depth, line
width, point size and antialiasing
calculations.

Display Texture Fragment Frame


Rasterization
List Memory Operation Buffer

Pixel
Pixel
Transfer
Data
Operation

Image Path
OpenGL Rendering Pipeline
Scissor Test ⇒ Alpha Test ⇒
Geometry Path Stencil Test ⇒ Depth Test.

Blending, dithering, logical


Vertex Vertex Primitive
operation and masking
Data Operation Assembly

Display Texture Fragment Frame


Rasterization
List Memory Operation Buffer

Pixel
Pixel
Transfer
Data
Operation

Image Path
The OpenGL State Machine

• Collection of variables the state of the pipeline

• A state machine is an abstract model of a collection of state


variables
OpenGL Abstractions
•Viewing
–perspective/orthographic
• Image scaling, polygon
•Windowing toolkit (key, mouse
tessellation
handler, window events) • Sphere, cylinders, quadratic
surfaces
OpenGL
Application Graphics
Hardware
Program Library
(API)
GLU

•Primitives - points, line,


GLUTpolygons
• Shading and Color
• Translation, rotation, scaling
• Viewing, Clipping, Texture
• Hidden surface removal
Example
#include <whateverYouNeed.h>
main () {
InitializeAWindowPlease();
glClearColor(0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f(0.25, 0.25, 0.0);
glVertex3f(0.75, 0.25, 0.0);
glVertex3f(0.75, 0.75, 0.0);
glVertex3f(0.25, 0.75, 0.0);
glEnd();
glFlush ();
UpdateTheWindowAndCheckForEvents);
}
Data Types

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
GLubyte, Unsigned char ub
8-bit unsigned integer
GLboolean
GLushort 16-bit unsigned integer Unsigned short us
GLuint, GLenum, 32-bit unsigned integer Unsigned long ui
GLbitfield
Points, Lines, and Polygons

• Homogeneous Co-ordinate system


o four floating-point coordinates (x, y, z, w)

• glVertex2i(Glint xi, Glint yi);

• glVertex3f(Glfloat x, Glfloat y, Glfloat z);


Points, Lines, and Polygons

Lines:

glBegin(GL_LINES);
glVertex2f(x1, y1);
glVertex2f(x2, y2);
glEND();

Define a pair of points as:

glBegin(GL_POINTS);
glVertex2f(x1, y1);
glVertex2f(x2, y2);
glEND();
Transformations

[x y z w] Model View
Matrix

Eye
Projection
coordinates Matrix

Clip Perspective
Coordinates Division

Viewport
Normalized Transformatio Window
Device n
Coordinate
Coordinate
Transformations
Transformations

Local Coordinate System

World Coordinate System


Camera Coordinate System
The Big Picture

Local
Coordinate
System

World
Model Coordinate
Matrix System

Camera
View Coordinate
Matrix System
Projection

Orthographic Perspective
OpenGL Example


glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glLoadIdentity ();/*clear the matrix*/
/* viewing transformation */
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0,
1.0, 0.0);
/* modeling transformation */
glScalef (1.0, 2.0, 1.0);
glutWireCube (1.0);
glFlush ();

OpenGL Example

gluLookAt
(0.0, 0.0, 5.0, // Camera position
0.0, 0.0, 0.0, // Lens aim at
0.0, 1.0, 0.0);// Camera orientation
OpenGL Example


glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glLoadIdentity ();/*clear the matrix*/
/* viewing transformation */
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0,
1.0, 0.0);
/* modeling transformation */
glScalef (1.0, 2.0, 1.0);
glutWireCube (1.0);
glFlush ();

Modeling Transformations

• void glTranslate{fd}(TYPEx, TYPE y, TYPEz);

• void glRotate{fd}(TYPE angle, TYPE x, TYPE y, TYPE z);


o glRotatef(45.0, 0.0, 0.0, 1.0)

• void glScale{fd}(TYPEx, TYPE y, TYPEz);


o glScalef(2.0, -0.5, 1.0).
Coloring

• Widely used methods of describing a color is the RGB color


model.

• OpenGL usually a value between 0 and 1

• We can mix these three colors together to give us a complete


range of colors
Example

• set_color(RED);

• draw_item(A);

• draw_item(B);

• set_color(GREEN);

• set_color(BLUE);

• draw_item(C);
glColor3f

void display() {
glClear(GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT);
glColor3f(0.5f, 0.0f, 1.0f);
glBegin(GL_QUADS);
glVertex2f(-0.75, 0.75);
glVertex2f(-0.75, -0.75);
glVertex2f(0.75, -0.75);
glVertex2f(0.75, 0.75);
glEnd();
glutSwapBuffers();
}
glColor3f (contd.)

void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUADS);
glColor3f(0.5f, 0.0f, 1.0f);
glVertex2f(-0.75, 0.75);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(-0.75, -0.75);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex2f(0.75, -0.75);
glColor3f(1.0f, 1.0f, 0.0f);
glVertex2f(0.75, 0.75);
glEnd();
glutSwapBuffers();
}
Output
Keyboard & Mouse handling

• Write your function to handle key-press event

o void keyPressed
(unsigned char key, int x, int y) {}

• In main invoke your function using GLUT


o glutKeyboardFunc(keyPressed);

// If they ‘a’ key was pressed


If (key == ‘a’) {
// Perform action associated with the ‘a’ key
}
Ubuntu

• Following packages are required

o freeglut3-dev
o mesa-common-dev

• Command
o sudo apt-get install freeglut3 freeglut3-dev mesa-common-dev
Windows

• Following files are required for development


o opengl32.lib
o glu32.lib
o gl.h
o glu.h

• For GLUT (Downloadable)


o glut32.dll
o glut32.lib
o glut.h
Windows

• Runtime libraries:
o C:\Windows\System32\{opengl,glu}32.dll

• On 64-bit Windows:
o C:\Windows\SysWOW64\{opengl,glu}32.dll

• Header files:
o C:\Program Files\Microsoft SDKs\Windows\v7.1A\Include\gl\
{GL,GLU}.h

• Linker library:
o C:\Program Files\Microsoft SDKs\Windows\v7.1A\Lib\OpenGL32.Lib
Windows

• Runtime library:

o C:\Program Files\Microsoft Visual Studio *\VC\bin\glut32.dll

• Header file:
o C:\Program Files\Microsoft Visual Studio *\VC\include\GL\
glut.h

• Linker library:
o C:\Program Files\Microsoft Visual Studio *\VC\lib\glut32.lib
Windows (in VC not there)

• Runtime library:
o C:\Windows\system\glut32.dll

• Header file:
o C:\Program Files\Microsoft SDKs\Windows\v7.0A\Include\GL\
glut.h

• Linker library:
o C:\Program Files\Microsoft SDKs\Windows\v7.0A\Lib\glut32.lib
Linux

• You will have to use the -lglut linker option with gcc/g++ to
compile a program with glut library.

• For example,
o Name of the program to be compiled
cube.c

o Command
gcc -o cube cube.c -lglut -lGLU
Example

Compiler Source File

gcc -o cube cube.c -lglut -lGLU

Output option OpenGL Option


Windows

Start Visual C++ and create a new empty project of type Win32 Console
Application

1. Add a GLUT program to the project

2. Go to Projects Settings C++ tab select Category - Preprocessor.

o In the additional include directory box give the path where


GL/glut.h is present.

3. Go to Projects Settings Link tab, select Category - Input.

o In additional library path give the location of glut32.lib


Thank you
Next Lecture: Introduction to 2D Graphics

You might also like