Introduction To Opengl
Introduction To Opengl
Introduction to OpenGL
Lecture Plan
1. Introduction to OpenGL
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
Video card driver provides implementation of OpenGL functionality for card. If a feature is not supported, software rendering is used!
10
11
Vertex Data
Rasterisation
Per-fragment Operations
Frame Buffer
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
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.
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
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.
16
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
Platform specific
Rendering Context
Device Context
Software rendering
18
SetPixelFormat
wglCreateContext New rendering context MakeCurrent
19
Once a context has been created were ready to start drawing into our window.
20
21
23
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
glVertex2f(-1.0, 0.7);
glVertex2f(-0.3, -0.5); glEnd();
25
glVertex2f(0.4, 0.7);
glVertex2f(-0.1, 1.0); glEnd();
26
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
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
29
30
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
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
Firstly, triangles are not concave ie.cannot have corners that point inwards.
concave
33
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
35
36
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
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
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
40