Openglfinal

You might also like

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

OpenGL

Introduction to OpenGL - OpenGL architecture,


primitives and attributes, simple modeling and rendering
of two- and three-dimensional geometric objects, GLUT,
interaction, events and call-backs picking. (Simple
Interaction with the Mouse and Keyboard)

Prof. Kajal Vatekar

Prof. Kajal Vatekar


Why OpenGL?
• Device independence
• Platform independence
– SGI Irix, Linux, Windows
• Abstractions (GL, GLU, GLUT)
• Open source
• Hardware-independent software
interface
• Support of client-server protocol

Prof. Kajal Vatekar


OpenGL API

❖ What is OpenGL (Open Graphics Library)


▪ It is a layer between programmer and
graphics hardware.
▪ It is designed as hardware-independent interface
to be implemented on many different hardware
platforms
▪ This interface consists of over 700 distinct
commands.
• Software library
• Several hundred procedures and functions
Prof. Kajal Vatekar
3
•A cross-language, multi-platform API for
rendering 2D and 3D computer graphics.

•The API is used to interact with a Graphics


Processing Unit (GPU)

•It accepts primitives such as points, lines and


polygons and converts them into a pixels via a
graphics pipeline .
Prof. Kajal Vatekar
4 sets of Libraries used in openGL:-
1) Core OpenGL:-
- It include more then 100 commands starting with “gl” prefix(glcolor,glrotate)
- Set of basic geometrics primitive like point, line, polygon
2) OpenGL Utility Library:-
- It provide essential feature and advanced building model(quadric face, polygon)
- It start with “glu” prefix(gluLookAt)
3) OpenGL Utility Toolkit:-
- It required to interact with window system or OS.
- It required to interact with OS (such as window creation, key mangement) and
provide multiple building model
- It start with “glut” prefix (glutcreatewindow)
4) OpenGL Extension Wranger Library:-
- It is cross platform open source c/c++ extension loading library.
- GLEW provide efficient run-time mechanisms for determining which OpenGL
extension are supported on the target platform
- “glewinfo.exe” is used to produce the list of OpenGL function supported by Graphics
system
Prof. Kajal Vatekar
OpenGL architecture

Prof. Kajal Vatekar


OpenGL Operation/ Architecture
From OpenGL reference
Display manual “Blue book”
List
Per- Rasteri-
Vertex zation
Eval- Opns. &
Comm- uator primitive Per-
ands assembly fragment
opns.
Pixel
opns. Texture
memory
What is to be Frame
drawn? buffer
How it is to be
drawn?
Prof. Kajal Vatekar
OpenGL
Can accumulate some
Operation
commands in a display list
for processing at a later
Display time (Batch mode). Or can
Lists proceed immediately
through the pipeline
Per- Per-
Eval- Vertex Rasteri-
Comm- frag-
uator Opns. & zation
ands ment
primitive opns.
assembly
Texture
glNewList() –creation of display list Pixel
memory
glEndList()- end the file opns.
To trigger the execution of list use-
glCallList(), glCallLists() Frame
glDeleteLists()-delete the list buffer
Display lists allow for greater optimization and command reuse, but
not all commands can be put in display lists.
Prof. Kajal Vatekar
OpenGL Provides an efficient means
for approximating curve
Operation and surface geometry by
evaluating polynomial
Display
commands of input values
Lists

Per-
Per-
Eval- Raster- frag-
Vertex
Comm- uator ization ment
opns. &
ands opns.
primitive
assembly
Texture
Pixel memory
opns.
Frame
In This first stage effectively takes any polynomial evaluator commands and buffer
evaluates them into their corresponding vertex
Prof. Kajaland attribute commands.
Vatekar
OpenGL Process geometric primitives -
points, line segments, and
Operation polygons as vertices and are
transformed, lit, and clipped to
Display the viewport in preparation for
Lists the next stage.

Per-
Comm- Per-
Eval- Raster- frag-
Vertex
uator ization ment
opns. &
opns.
primitive
assembly
Texture
Pixel memory
opns.
Frame
The second stage is the per-vertex operations, including transformations, buffer
lighting, primitive assembly, clipping, projection, and viewport mapping.
Prof. Kajal Vatekar
OpenGL Produces a series of
frame buffer addresses
Operation and associated values
using a two-dimensional
Display description of a point,
Lists line segment, or polygon
Per-
Per-
Eval- Raster- frag-
Comm- Vertex
uator ization ment
ands opns &
opns.
primitive
assembly
Texture
Pixel memory
opns.
Frame
In This 3rd stage produces fragments, which are series of framebuffer buffer
addresses and values, from the viewport-mapped primitives as well as bitmaps
Prof. Kajal Vatekar
and pixel rectangles
OpenGL Z-buffering, and blending of
incoming pixel colors with
Operation stored colors, and masking
and other logical operations
Display
on pixel values are done
Lists Solve
Error
Per-
Per-
Eval- Raster- frag-
Vertex
Comm- uator ization ment
opns &
ands opns.
primitive
assembly
Texture
Pixel memory
opns.
Frame
The fourth stage is the per-fragment operations. Before fragments go to the buffer
framebuffer, they may be subjected to Prof.
a series of Vatekar
Kajal conditional tests and
modifications, such as blending or z-buffering.
OpenGL Input data can be in
the form of pixels
Operation (image for texture
mapping) is processed
Display
in the pixel operations
Lists
stage.
Per- Per-
Eval- Vertex Raster- frag-
Comm- uator ops & ization ment
ands primitive opns
assembly
Texture
Pixel memory
opns
Frame
buffer
Prof. Kajal Vatekar
OpenGL architecture
• Commands may either be build up in display lists, or processed
immediately through the pipeline. Display lists allow for greater
optimization and command reuse, but not all commands can be put in
display lists.
• The first stage in the pipeline is the evaluator. This stage effectively takes
any polynomial evaluator commands and evaluates them into their
corresponding vertex and attribute commands.
• The second stage is the per-vertex operations, including transformations,
lighting, primitive assembly, clipping, projection, and viewport mapping.
• The third stage is rasterization. This stage produces fragments, which are
series of framebuffer addresses and values, from the viewport-mapped
primitives as well as bitmaps and pixel rectangles.
• The fourth stage is the per-fragment operations. Before fragments go to the
framebuffer, they may be subjected to a series of conditional tests and
modifications, such as blending or z-buffering.
• Parts of the framebuffer may be fed back into the pipeline as pixel
rectangles. Texture memory may be used in the rasterization process
when texture mapping is enabled.
Prof. Kajal Vatekar
OpenGL API

❖ Program Structure
▪ Step 1: Initialize the interaction between windows
and OpenGL.
▪ Step 2: Specify the window properties and further
create window.
▪ Step 3: Set the callback functions
▪ Step 4: Initialize the program attributes
▪ Step 5: Start to run the program

Prof. Kajal Vatekar


❖ Program Framework
#include <GL/glut.h> includes gl.h
argc (argument count):-how many arguments were entered on the command line
argv (argument vector):-it’s an array of pointers to arrays of character objects.

int main(int argc, char** argv)


{
glutInit(&argc,argv); interaction initialization
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("simple"); define window properties

glutDisplayFunc(myDisplay);
display callback
myInit(); set OpenGLstate

glutMainLoop();
enter event loop
} Prof. Kajal Vatekar
Act like agent which hold the output, if not putting this line your output doesnot hold your output
GLUT Function

❖ Program Framework: Window Management


▪ glutInit():initializes GLUT and should be called
before any other GLUT routine.
▪ glutInitDisplayMode():specifies the color model
(RGB or color-index color model)
▪ glutInitWindowSize():specifies the size, in pixels,
of your window.
▪ glutInitWindowPosition():specifies the screen
location for the upper-left corner
▪ glutCreateWindow():creates a window with
Prof. Kajal Vatekar
an OpenGL context. 1
OpenGL API
❖ Program Framework
void myInit(){ RGB=0(Black) 1(White)
/* set colors */ Alpha=0(Opaque)1(Trasparent)
glClearColor(1.0, 1.0, 1.0, 0.0); RGBA
}/* End of myInit */

void myDisplay(){
/* clear the display */
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}/* End of GasketDisplay */

Prof. Kajal Vatekar


1
OpenGL API

❖ Program Framework: Color Manipulation


▪ glClearColor():establishes what color the window will be cleared to.
▪ Clear Color Setting
glClearColor(r value, g value, b value,alpha);
▪ glClear():actually clears the window.
▪ glColor3f():establishes what color to use for drawing objects.

Remark: OpenGL is a state machine. You put it intovarious


states or modes that remain in effect until you change them

Prof. Kajal Vatekar


1
Primitives and Attributes
❖ Program Form of Primitives
glBegin(type);
glVertex*(…);
glVertex*(…);
.
.
glVertex*(…);
glEnd();

▪ In OpenGL, term ‘primitive’ refered as two


similar but separate concept.
▪ The basic ones are specified by a set of vertices.
▪ The type specifies how OpenGL assembles the
Prof. Kajal Vatekar
vertices. 1
•OpenGL supports several basic primitive
types including points and general polygon
•All of these primitive are specified using
sequence of vertices.

Prof. Kajal Vatekar


Primitives and Attributes
❖ Points and Line Segment
▪ Point: GL_POINTS
▪ Line Segments: GL_LINES
▪ Polygons:
• GL_LINE_STRIP
• GL_LINE_LOOP

Prof. Kajal Vatekar


1
Primitives and Attributes
❖ Polygon Primitives
▪ Polygons: GL_POLYGON
▪ Triangles: GL_TRIANGLES
▪ Quadrilaterals: GL_QUADS
▪ Stripes: GL_TRIANGLE_STRIP
▪ Fans: GL_TRIANGLE_FAN

Prof. Kajal Vatekar


2
Geometric Primitive Names and
Meanings

Prof. Kajal Vatekar


Primitives and Attributes
❖ RGB-Model
▪ Setting Operations
glColor3f(r value, g value, b value);

Prof. Kajal Vatekar


2
Prof. Kajal Vatekar
OpenGL Geometric Drawing Primitives

Fig : Drawing a Polygon or a Set of Points


Prof. Kajal Vatekar
Examples
• //the following code plots three dots
glBegin(GL_POINTS);
glVertex2i(100, 50);
glVertex2i(100, 130);
glVertex2i(150, 130);
glEnd( );
• // the following code draws a triangle
glBegin(GL_TRIANGLES);
glVertex3f(100.0f, 100.0f, 0.0f);
glVertex3f(150.0f, 100.0f, 0.0f);
glVertex3f(125.0f, 50.0f, 0.0f);
glEnd( );
• // the following code draw a lines
glBegin(GL_LINES);
glVertex3f(100.0f, 100.0f, 0.0f); // origin of the line
glVertex3f(200.0f, 140.0f, 5.0f); // ending point of the line
glEnd( );

Prof. Kajal Vatekar


Transformation in OpenGL

•Viewing Transformation is the mapping of


coordinate of points and lines that form the
picture into appropriate coordinate on the
display device.
•The conversion of 3D into 2D coordinate on
the screen is done using matrix transformation

Prof. Kajal Vatekar


Model Matrix:- The matrix that contain every translation, rotation or
scaling applied to an object as the matrix in OpenGL.
Matrix transformation transform a position in model to world
coordinate.
View Matrix:- In real life we re used to moving the camera to change
the view of certain scene, but in OpenGL camera cannot move.
Instead of moving and rotating the camera, the world is moved and
rotated around the camera to construct the appropriate view.
The view matrix in OpenGL control the way we look at scene
Projection Matrix:- Clipping volume can be increased or decreased
with the projection matrix.
It enhance the feeling of real world is the perceptive projection matrix

Prof. Kajal Vatekar


GLUT

•GLUT stands for OpenGL


Utility Toolkit.
•Most of system dependent
actions required to display
a window, Put OpenGL
graphics in it and accept
mouse and keyboard input
are handled by GLUT

Prof. Kajal Vatekar


Callback Function
A callback function is call when it need to know how to
process something.
Number of callback is supported by GLUT
3 types of callback:-
1) Window callback:- It indicates when to redisplay or
reshape a window.
2) Menu callback:- It is set by glutCreateMenu
3) Callback Handler Display:- The function Display() is
known as callback event handler.
An event handler provide the response to a particular
events (key press, mouse click)

Prof. Kajal Vatekar


Prof. Kajal Vatekar

You might also like