0% found this document useful (0 votes)
65 views40 pages

Introduction To Opengl

OpenGL is an industry standard API for 2D and 3D graphics. It consists of a core specification and extensions. Rendering in OpenGL uses a client-server model where the client issues commands that the server/GPU executes. Immediate mode rendering specifies geometry by enclosing glVertex calls between glBegin and glEnd calls to define shapes like points, lines, triangles. This defines the vertices and rendering mode for the graphics hardware to rasterize into pixels on the screen.

Uploaded by

Sorin Secan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views40 pages

Introduction To Opengl

OpenGL is an industry standard API for 2D and 3D graphics. It consists of a core specification and extensions. Rendering in OpenGL uses a client-server model where the client issues commands that the server/GPU executes. Immediate mode rendering specifies geometry by enclosing glVertex calls between glBegin and glEnd calls to define shapes like points, lines, triangles. This defines the vertices and rendering mode for the graphics hardware to rasterize into pixels on the screen.

Uploaded by

Sorin Secan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 40

2.

Introduction to OpenGL

Lecture Plan
1. Introduction to OpenGL

2. How it Works Drawing in OpenGL


3. Setting up OpenGL in Windows 4. Introduction to Immediate Mode rendering

1. Introduction to OpenGL

1. Introduction to OpenGL
OpenGL is a platform independent, industry standard API (Application Programming Interface) OpenGL is used in many application areas - Games - Scientific & Medical Visualisation - Architectural rendering - Engineering & CAD OpenGL was originally released by SGI in 1992 as an Open Standard. Today, OpenGL is managed by the ARB (Architecture Review Board) which is part of the Khronos Group and currently stands at version 3.2

1. Introduction to OpenGL
OpenGL consists of a core specification and a number of extensions written by members of the ARB (including graphics card manufacturers)
Other extensions

Apple extensions

nVidia extensions

SGI extensions

OpenGL (3.2)

Other extensions

Microsoft extensions

Other extensions

ATI extensions

1. Introduction to OpenGL
Rendering in OpenGL is done in a client-server type environment. The client part of OpenGL issues rendering commands to the server, which performs the actual rendering operations.
Standalone system The client-server model exists in software only
OpenGL commands

Client(application)
Server (Rendering)

Network system The client machines send OpenGL commands to the rendering server.

OpenGL commands
6

1. Introduction to OpenGL
Typical PC graphics architecture

CPU
- Graphics card / Graphics Processing Unit (GPU) - Perform Transform and Lighting (T&L) calculations

Memory Northbridge

AGP or PCI-E
USB Firewire

Southbridge
Disk I/O
7

Audio

1. Introduction to OpenGL
Where does OpenGL live on your system? The implementation of OpenGL on your system is already installed! You dont have to install anything to actually use OpenGL (be it a CAD application or game for example). OpenGL actually lives in your graphics card driver the software that drives your graphics card (otherwise known as the Graphics Processing Unit GPU). When you update your graphics driver you often get improved features and performance in OpenGL.

1. Introduction to OpenGL
How does OpenGL talk to the GPU?

Your application will make calls to OpenGL. These are passed through the HAL (Hardware Abstraction Layer) in the graphics driver which in turn talks directly to the hardware
Main OpenGL libraries to support OpenGL function calls (<gl.h>, <glu.h>)

Your Application
Other windows libraries GDI/GDI+ & WPF

OpenGL Libraries

Hardware Abstraction Layer (HAL) Hardware (GPU)

Video card driver provides implementation of OpenGL functionality for card. If a feature is not supported, software rendering is used!

Video card hardware performs main rendering operations.

2. How it Works Drawing in OpenGL

10

2. How it Works Drawing in OpenGL


Were going to focus on a type of rendering called Rasterisation This takes geometrical models and converts them to pixels that we see on-screen Geometrical models are built up from vertices. Vertices are simply points in 2D (or 3D) space. Vertices are connected to describe the shape we want to draw (using edges and where relevant, faces).

11

2. How it Works Drawing in OpenGL


The OpenGL rendering pipeline consists of a series of processes that convert the OpenGL commands we specify in our code into the images we see on screen.

Vertex Data

Per-vertex Operations Transform and lighting

Rasterisation

Per-fragment Operations

Frame Buffer

Move, scale and rotate vertex data

Convert geometry into actual pixels, or fragments

Per-pixel lighting and testing

Collection of pixels that make up the screen This is what you see on the monitor

Older versions of OpenGL were based on the Fixed Function Pipeline. In this model, we cannot add or change functionality, but we can control pre-defined functionality by setting OpenGL variables (state variables).
12

2. How it Works Drawing in OpenGL


The lastest version of OpenGL is based on the Programmable Pipeline. Here key points in the pipeline can be replaced with our own functions (called shaders). This allows us to define our own custom rendering code and data model which gives us far more control over the rendering process on the GPU.

Vertex Data

Per-vertex Operations

Geometry Operations

Rasterisation

Per-fragment Operations

Frame Buffer

Vertex shader applied to each vertex we specify one vertex in, process, one vertex out.

The geometry shader is a new stage in the pipeline this allows the GPU to actually generate new geometry on-the-fly rather than the application having to provide all of the geometry data.

Fragment (pixel) shader allows us to control lighting at the pixel level.

Well look at shader development later in the course.

2. How it Works Drawing in OpenGL


The Framebuffer
but the framebuffer contains more information for each pixel than just colour It also contains

Accumulation data - Accumulation buffer Stencil Information Stencil buffer Depth Information Depth (Z) buffer Pixel colour Colour buffer (directly visible)

The Framebuffer contains the pixel colours that make up the image that we see

In computer graphics we often refer to a pixel as a fragment because a fragment contains much more data than just colour information.
14

2. How it Works Drawing in OpenGL

All modern GPUs are built around this pipeline concept and are designed to carry out the rendering operations very quickly!
Quick note: Intels new Larrabee card proposes moving away from the pipeline architecture and using a hardware accelerated software renderer. The advantages of this are more flexibility in the type of rendering you can do.

Modern GPUs offer two board ways of specifying geometry


Immediate mode (where we specify the geometry to render one part at a time in code) (this week) Retained mode (where we specify the whole geometry in a buffer). This is more efficient than immediate mode and well look at this later in the course. (look at in week 5)
15

3. Setting up OpenGL in Windows

16

3. Setting up OpenGL in Windows

To use OpenGL in Windows we need to do the following


Include relevant headers
gl.h glu.h glext.h (defines OpenGL core types and functions) (defines useful functions to perform common tasks) (defines more recent extension types and functions)

Create a Window (which acts as our drawing canvas)


Setup a Window Class Register the Window Class with the Windows OS Create a window based on the new Window Class Show the new window

Create an OpenGL Rendering Context. A rendering context contains information about the current colour to draw in, the current texture to use, the current shape / geometry to draw etc.

17

3. Setting up OpenGL in Windows


Your rendering code Platform independent

Platform specific

Rendering Context

Device Context

OS Hardware rendering (HAL)

Software rendering

18

3. Setting up OpenGL in Windows

Creating a rendering Context

PIXEL FORMAT DESCRIPTOR

Device Context (hDC) ChoosePixelFormat Valid system pixel format

SetPixelFormat
wglCreateContext New rendering context MakeCurrent
19

Once a context has been created were ready to start drawing into our window.

4. Introduction to Immediate Mode Rendering

20

4. Introduction to Immediate Mode Rendering


(Immediate mode)rendering in OpenGL is based on the following algorithm 1) Specify the rendering mode to use with glBegin() command 2) Specify shape/geometry with glVertex commands 3) End geometry with glEnd() command 4) Flush command queue and display Lets look at each of these stages in turn

21

4. Introduction to Immediate Mode Rendering


Use glBegin() to specify the rendering mode. This tells OpenGL what type of geometry you want to draw. The rendering modes provided by OpenGL are GL_POINTS GL_LINES GL_LINE_STRIP GL_LINE_LOOP GL_QUADS GL_QUAD_STRIP GL_POLYGON GL_TRIANGLES GL_TRIANGLE_STRIP GL_TRIANGLE_FAN
22

4. Introduction to Immediate Mode Rendering


When we describe our geometry, properties (OpenGL state) are specified on a per-vertex basis. For a given vertex we usually specify the following information - Colour - Texture Coordinate - Normal vector - Position (Quick note: for a vertex the position is the last thing to specify) Over the next few weeks were going to look at colour, texture and position information. Well revisit the vertex normal when we look at lighting later in the course!

23

4. Introduction to Immediate Mode Rendering


Specifying vertex position

OpenGL provides the glVertex command for specifying position information. The syntax of the command is
glVertex<number of coordinates><type>(parameters)

<number of coordinates> tells OpenGL how many parameters your passing. <type> tells OpenGL if the coordinate parameters are integers, floating point, short etc.
Some examples glVertex2i(10,4); means we specify two co-ordinates in integer format (used for 2D rendering). glVertex3f(10.2, 4.3, -3.0); means we specify three co-ordinates in floating point format (used to render 3D models).
24

4. Introduction to Immediate Mode Rendering


GL_POINTS

glBegin (GL_POINTS); glVertex2f(0.0, 0.0); glVertex2f(0.5, 0.2);

glVertex2f(-1.0, 0.7);
glVertex2f(-0.3, -0.5); glEnd();

25

4. Introduction to Immediate Mode Rendering


GL_LINES

glBegin (GL_LINES); glVertex2f(0.0, 0.0); glVertex2f(0.5, 0.0);

glVertex2f(0.4, 0.7);
glVertex2f(-0.1, 1.0); glEnd();

26

4. Introduction to Immediate Mode Rendering


GL_LINE_STRIP

glBegin (GL_LINE_STRIP); glVertex(0.0, 0.0); glVertex(0.5, 0.0); glVertex(0.4, 0.7); glVertex(-0.1, 1.0); glEnd();

27

4. Introduction to Immediate Mode Rendering


GL_LINE_LOOP

glBegin (GL_LINE_LOOP); glVertex(0.0, 0.0); glVertex(0.5, 0.0); glVertex(0.4, 0.7); glVertex(-0.1, 1.0); glEnd();

28

4. Introduction to Immediate Mode Rendering


GL_QUADS
glBegin (GL_QUADS); glVertex2f( -0.7, 0.0); glVertex2f( -0.8, 0.4); glVertex2f( -0.3, 0.4); glVertex2f( -0.4, 0.0);

glVertex2f( -0.4, 0.0); glVertex2f( -0.3, 0.4); glVertex2f( 0.1, 0.25);

glVertex2f( -0.05, -0.1);


glEnd();

29

4. Introduction to Immediate Mode Rendering


GL_QUAD_STRIP
glBegin (GL_QUAD_STRIP); glVertex2f( -0.7, 0.0); glVertex2f( -0.8, 0.4); glVertex2f( -0.4, 0.0); glVertex2f( -0.3, 0.4); glVertex2f(-0.05, -0.1); glVertex2f(0.1, 0.25); glVertex2f(0.45, -0.04); glVertex2f(0.55, 0.33); glEnd();

30

4. Introduction to Immediate Mode Rendering


GL_POLYGON

glBegin (GL_POLYGON); glVertex2f(-0.35, 0.05); glVertex2f(-0.2, 0.4); glVertex2f(0.0, 0.4); glVertex2f(0.15, 0.8); glVertex2f(0.45, -0.05); glVertex2f(0.33, -0.33); glVertex2f(0.05, -0.25); glVertex2f(-0.25, -0.25); glEnd();

31

4. Introduction to Immediate Mode Rendering


GL_TRIANGLES

glBegin (GL_TRIANGLES); glVertex2f(0.0, 0.0); glVertex2f(0.25, 0.5); glVertex2f(0.5, 0.0); glVertex2f(0.5, 0.0); glVertex2f(0.25, 0.5);

glVertex2f(0.75, 0.5);
glEnd();

32

4. Introduction to Immediate Mode Rendering


Why Triangles?
GL_POLYGON offers a powerful tool for modelling arbitrary polygon shapes, but most graphical environments use only triangles (and the OpenGL ARB recommends using triangles). Why?
concave

Firstly, triangles are not concave ie.cannot have corners that point inwards.

concave

They are convex ie. corners extend outwards only


convex

33

4. Introduction to Immediate Mode Rendering


Why Triangles?
Secondly, because triangles are filled in (with colour or texture), the convexity of triangles simplifies this process...

For convex shapes, each row of pixels (scan line) has a clear start and end point

For concave shapes, multiple scan line regions need to be identified. This complicates the filling process and adds unnecessary processor overhead

34

4. Introduction to Immediate Mode Rendering


Why Triangles?
Often, arbitrary polygons are converted into a triangular tessellation (arrangement / pattern)

35

4. Introduction to Immediate Mode Rendering


GL_TRIANGLE_STRIP
glBegin (GL_TRIANGLE_STRIP); glVertex2f(0.0, 0.0); glVertex2f(0.25, 0.5); glVertex2f(0.5, 0.0); glVertex2f(0.75, 0.5);

glVertex( 1.0, 0.0);


glEnd();

36

4. Introduction to Immediate Mode Rendering


GL_TRIANGLE_FAN

glBegin (GL_TRIANGLE_FAN); glVertex2f(0.5, 0.0); glVertex2f(0.0, 0.0); glVertex2f(0.25, 0.5); glVertex2f(0.75, 0.5); glVertex( 1.0, 0.0); glEnd();

37

4. Introduction to Immediate Mode Rendering


Why Strips and Fans?
For each vertex we specify in glVertex() the transformations and operations in the rendering pipeline are applied. This can be wasteful when we duplicate vertices as shown in some of the above examples.

glBegin (GL_TRIANGLES); glVertex2f(0.0, 0.0); glVertex2f(0.25, 0.5); glVertex2f(0.5, 0.0); glVertex2f(0.25, 0.5); glVertex2f(0.5, 0.0); glVertex2f(0.75, 0.5); glEnd(); Here two triangles are presented, each defined by 3 vertices. Because there is a shared edge, two vertices must be repeated. This can be very inefficient for larger, complex models with hundreds of triangular faces!!!

38

4. Introduction to Immediate Mode Rendering


Why Strips and Fans?
Strips and Fans allow us to arrange vertices and faces so we minimise this duplication. Commercial graphics engines use triangle strips extensively for efficient rendering.

Polygon shape with triangles tessellated for strip and fan rendering

The same shape but tessellated for rendering with two triangle strips

39

To sum up

1. OpenGL is based around a rasterisation rendering pipeline


2. Rendering code is platform independent, but an OpenGL rendering context must be attached to a platform specific structure so we can render through the OS 3. OpenGL rendering is based on a state-based model. All rendering is affected by the current state.

40

You might also like