0% found this document useful (0 votes)
75 views28 pages

Opengl: Introduction To Opengl - Opengl Architecture

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 28

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)
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
•Open Graphics Library

•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 .
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

3
OpenGL architecture
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?
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
Pixel
memory
opns.
From OpenGL Frame
reference manual buffer
“Blue book”
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
From OpenGL reference
buffer
manual “Blue book”
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
From OpenGL reference
buffer
manual “Blue book”
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
From OpenGL reference
buffer
manual “Blue book”
OpenGL Z-buffering, and blending
of incoming pixel colors
Operation with stored colors, and
masking and other logical
Display
operations on pixel values
Lists

Per-
Per-
Eval- Raster- frag-
Vertex
Comm- uator ization ment
opns &
ands opns.
primitive
assembly
Texture
Pixel memory
From OpenGL opns.
Frame
reference manual
buffer
“Blue book”
OpenGL Input data can be in
the form of pixels
Operation (image for texture
mapping) is processed
Display
Lists in the pixel operations
stage.
Per- Per-
Eval- Vertex Raster- frag-
Comm- uator ops & ization ment
ands primitive opns
assembly
Texture
Pixel memory
opns
Frame
buffer
OpenGL architecture
• Commands may either be accumulated 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.
OpenGL API
 Library Organization
 OpenGL (GL)
• Core Library
• OpenGL on Windows.
 OpenGL Utility Library (GLU)
• It uses only GL functions to create common objects.
• It is available in all OpenGL implementations.
 OpenGL Utility Toolkit (GLUT)
• It provides the minimum functionalities expected for
interacting with modern windowing systems.

1
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

1
OpenGL API
 Program Framework
#include <GL/glut.h> includes gl.h

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 OpenGL state

glutMainLoop();
enter event loop
}
1
OpenGL API
 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 an OpenGL context.
1
OpenGL API
 Program Framework
void myInit(){
/* set colors */
glClearColor(1.0, 1.0, 1.0, 0.0);
}/* End of myInit */

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

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);
• Transparency: alpha = 0.0
• Opacity: alpha = 1.0;
 glClear():actually clears the window.
 glColor3f():establishes what color to use for drawing
objects.

Remark: OpenGL is a state machine. You put it into various


states or modes that remain in effect until you change them

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

 The basic ones are specified by a set of vertices.


 The type specifies how OpenGL assembles the
vertices.

1
Primitives and Attributes
 Program Form of Primitives
 Vertex Function: glVertex* ()
• * : can be as the form [nt | ntv]
• n : the number of dimensions (2, 3, 4)
• t : data type (i: integer, f: float, and d: double)
• v : the variables is a pointer.
glVertex2i (GLint x, GLint y);
glVertex3f(GLfloat x, GLfloat y, GLfloat z);
glVertex2fv(p); // int p[2] = {1.0, 1.0}

1
Primitives and Attributes
 Points and Line Segment
 Point: GL_POINTS
 Line Segments: GL_LINES
 Polygons:
• GL_LINE_STRIP
• GL_LINE_LOOP

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

2
Primitives and Attributes
 RGB-Model
 Setting Operations
glColor3f(r value, g value, b value);

2
OpenGL Geometric Drawing Primitives

Fig : Drawing a Polygon or a Set of Points


Geometric Primitive Names and
Meanings
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( );

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy