Overview of OpenGL

boringtarpΛογισμικό & κατασκευή λογ/κού

13 Δεκ 2013 (πριν από 3 χρόνια και 9 μήνες)

132 εμφανίσεις

CSc 155 Lecture Note Slides

Overview of
OpenGL

CSc

155 Lecture Note Slides

OpenGL overview

2

The Main Players

Two major “Graphics Libraries”:


DirectX

o
Powerful

o
Proprietary

o
Runs
only
under Windows


OpenGL

o
Powerful

o
Open Standard

o
Runs on: Windows, Solaris, Linux, Mac OS, …

o
Bindings for several languages available

o
Drivers provided by most graphics cards

o
Many cards provide hardware support

CSc

155 Lecture Note Slides

OpenGL overview

3

OpenGL ARB: Khronos WG


The Khronos Group: an “Open Standard API” industry consortium


OpenGL “ARB” is a “Khronos Working Group”

CSc

155 Lecture Note Slides

OpenGL overview

OpenGL Versions

4

GL 1.0

GL 1.1

GL 1.2

GL 1.3

GL 1.4

1992

GL 1.5

ES 1.0

ES 1.1

GL 2.0

ES 2.0

GL 2.1

GL 3.0

2008

Deprecation
introduced

GL 3.1

Core

Compatibility

Last Microsoft
-
supported version

GL 3.2

2003

Programmable
pipeline introduced

GL 3.3

GL 4.0, 4.1,
4.2, 4.3

1997

2001

2004

2006

2009

2009

2010

2010, 2010,
2011, 2012

1998

2002

CSc

155 Lecture Note Slides

OpenGL overview

5

Accessing OpenGL in Java

JOGL: the
J
ava
O
pen
GL

binding

o
Originally a joint project between Sun & SGI

o
Now an Open
-
Source community project

User
Program

GL

Object

GLU
Object

GLUT
Object

JOGL
package

Method
calls

GL
Library

GLU
Library

GLUT
Library

Native OS/Driver
Routines

CSc

155 Lecture Note Slides

OpenGL overview

6

JOGL Program Structure

JFrame

<<interface>>

GLEventListener

<<interface>>

GLAutoDrawable

GL

1)
Extend JFrame, implement

GLEventListener

2)
Create a GLCanvas

3)
Add canvas to frame

4)
Make frame a listener

for canvas GLEvents

5)
Invoke canvas display()

to repaint

GLCanvas

private Listeners

public display();



GLContext

uses


MyDisplayFrame

java.awt.

Canvas

Standard Java

JOGL

CSc

155 Lecture Note Slides

OpenGL overview

7

JOGL Interface Methods

/** This interface defines the methods which must be implemented by a


* component (e.g. a
JFrame
) that wishes to respond to “GLEvents” (for


* example, a “display callback” from a GLCanvas’s display() method.)


*/

public interface GLEventListener {


/** Called when OpenGL is initialized */


void init(GLAutoDrawable drawable) ;


/** Called when the drawable object is repainted */


void display (GLAutoDrawable drawable) ;


/** Called when the drawable object’s container is resized */


void reshape(GLAutoDrawable

d, int

x, int

y, int

width, int height) ;


/** Called to notify the listener to release any OpenGL resources




* which it may have associated with the specified drawable */


void dispose(GLAutoDrawable

d) ;

}

CSc

155 Lecture Note Slides

OpenGL overview

8

JOGL Interface Methods
(cont.)

/** This interface defines the methods which are implemented by


* “GLAutoDrawable”s (for example, JOGL “GLCanvas” objects). These


* methods allow a listener to register to be “called back” when the


* GLAutoDrawable’s display() method has been called.


*/

public interface GLAutoDrawable {


/** Adds a “callback” listener to a drawable object */


public void addGLEventListener(GLEventListener

listener);



/** Called by the application or system to repaint the drawable


* object; invokes display(this) in all registered listeners . */


public void display() ;



/** Returns the GL object which knows how to draw on this drawable */


public GL getGL() ;



//... other interface methods ...

}

CSc

155 Lecture Note Slides

OpenGL overview

9

The Graphics “Pipeline”

Vertex

Processing

Primitive

Assembly

Rasterization


Fragment

(“pixel”)

Processing

Fragment

Testing

(HSR, etc.)

Frame Buffer

(Memory)

Display

Primitive
Processing

(Projection,
Clipping)

Graphics Card


Implemented by the
combination of the graphics driver
and

graphics (hardware) card


Can be FIXED
-
FUNCTION or PROGRAMMABLE,

depending on implementation
version

Vertices
(e.g. from
glVertex()
)

Application Code

OpenGL API

Other Data

“Client Side”

“Server Side”

CSc

155 Lecture Note Slides

OpenGL overview

10

Hardware Architecture


Early structure:

Main
Memory

CPU

Frame Buffer

(Memory)

Application code

OpenGL Driver
(software)

Vertex Processing

Primitive Assembly

Projection

Clipping/Culling/etc.

Rasterization

Fragment Processing

CSc

155 Lecture Note Slides

OpenGL overview

11

Hardware Architecture
(cont.)


First evolution: simple processors

Memory

CPU

Frame
Buffer

Application code

OpenGL Driver
(software)

Most (not all)
pipeline steps

Registers

Processor

Graphics Card

I/O Bus

Memory Bus

CSc

155 Lecture Note Slides

OpenGL overview

12

Hardware Architecture
(cont.)


Next stage: graphics processors (GPU’s)

Memory

CPU

Frame Buffer

Application code

OpenGL Driver
Interface

Registers

GPU

Graphics Card

PCI, AGP, or PCI
-
Express Bus

VRAM

Vertex Processing

Primitive Assembly

Projection

Clipping/Culling/etc.

Rasterization

Fragment Processing

“Firmware”

CSc

155 Lecture Note Slides

OpenGL overview

13

THE
PROGRAMMABLE

PIPELINE


Application

Program


Assembly


Projection


Clipping/Culling


Persp. Divide


MapToViewport


Rasterizer

Fragment

Testing

(HSR, etc.)

State
Variables


Vertex

Processor





Compiler



Linker


Fragment

Processor



Shader

Source

Code

Vertex
Program

Fragment
Program

Graphics card

Frame
Buffer

Client Side

Server Side

CSc

155 Lecture Note Slides

OpenGL overview

14

Hardware Programming


Assembly

o
Tedious, vendor/chip specific


High
-
level languages

o
HLSL

(“
H
igh
-
L
evel
S
hading
L
anguage”)


Proprietary (Microsoft)


Powerful


Specific to DirectX

o
Cg

(“
C

for
g
raphics”)


Proprietary (nVidia)


Supports both DirectX and OpenGL APIs (more complex)

o
GLSL

(“Open
GL

S
hading
L
anguage”)


Open standard


Compiles to all common vendor chips


Can run “on top of” DirectX, or directly on hardware

CSc

155 Lecture Note Slides

OpenGL overview

GLSL Versions

OpenGL Version

GLSL Version

Notes

1.5

1.0

2003; Partial

GLSL

2.0

1.1

2004

2.1

1.2

2006; ≈ Direct3D

9

3.0

1.3

2008

3.1

1.4

2009

3.2

1.5

2009

3.3

3.3

2010; ≈ D3D 10 (“Shader

Model 4.0”)

4.0

4.0

2010

4.1

4.1

2010

4.2

4.2

2011; ≈ D3D

11 (“Shader Model 5.0”)

4.3

4.3

August 2012

15

CSc

155 Lecture Note Slides

OpenGL overview

16

GLSL Overview


Based on C; looks much like C/C++/Java


Every Shader has a “main”:

void main () { ... }


Other similarities:


Constants, identifiers, most operators, expression syntax


Statement forms ( assignment,

if
/
then
/
else
;
for
,
while
;






switch

)


Function calls

o
Parameter passing is “Call By Value
-
Return”:


Input parameters are copied in at call time


Output parameters are copied back to caller at exit

o
Parameters have a “mode qualifier” :
in
,
out
, or
inout



int myFunc (in float a, out float b) { … }

CSc

155 Lecture Note Slides

OpenGL overview

17

Features
NOT
In GLSL


No automatic type promotion

float f = 0 ;

//compiler error!


No pointers


No Strings or Chars


No unions


No enumerated types


No bitfield structures or bitwise operators


No references to files

o
no
#include

o
(but there
is

a preprocessor;
#define

works…)

CSc

155 Lecture Note Slides

OpenGL overview

18

GLSL Variables


Functions can declare “local variables”

of any type


Variables can also be declared

outside the scope of any function

o
Used for “input/output communication” with

shaders

o
Must have a
qualifier

indicating how the

variable is used
:
const
,
in
,
or

out


“Use qualifier” is
in addition to “type”

CSc

155 Lecture Note Slides

OpenGL overview

Example: “
Identity”
Vertex Shader

//A simple GLSL "Identity" vertex shader
--

simply copies the input vertex

// position and color to the output


#version 330
//indicate this shader requires GLSL Version 3.3 or higher


in vec3 vertPos ;
//the vertex geometric position

in vec4 vertColor;
//the vertex color


out vec4 varyingColor;
//the varying (interpolated) color passed to the frag shader


void main(void) {


varyingColor = vertColor ;
//pass the input color to the output


gl_Position = vec4(vertPos,1) ;
//pass the input position to the output

}

19

CSc

155 Lecture Note Slides

OpenGL overview

“Identity”
Fragment Shader

//A simple GLSL "identity" fragment shader. All it does is pass the

// input color from the rasterizer to the shader color output.


#version 330


in vec4 varyingColor ;
//the interpolated color from the rasterizer


out vec4 fragColor ;
//the color assigned to the fragment


void main (void) {


gl_FragColor = varyingColor ;

}

20

CSc

155 Lecture Note Slides

OpenGL overview

21

Shader Program Organization

Application
Program

Shader1
source code

Shader2 source
code

OpenGL API

Shader
object

Shader
object

Compiler

Program
object

Linker

OpenGL
Driver

Compiled Code

Executable
Code

Graphics
Hardware

Shader
Source Code

Logs

CSc

155 Lecture Note Slides

OpenGL overview

22

Creating & Installing Shaders

1.
Create one or more (empty)
shader objects
:







shaderID =
glCreateShader
(
shaderType
)

2.
Provide source code (an array of Strings) for


each shader:







glShaderSource
(shaderID, numStrings,










arrayOfStrings, arrayOfStringLengths)

3.
Compile each shader:







glCompileShader
(shaderID)

CSc

155 Lecture Note Slides

OpenGL overview

23

Creating & Installing Shaders
(cont.)

4.
Create one or more (empty) program objects:





programID =
glCreateProgram
()

5.
Attach shader object(s) to program objects:





glAttachShader
(programID, shaderID)

6.
Link program objects:





glLinkProgram
(programID)

7.
Install a program object into OpenGL pipeline:





glUseProgram
(programID)