52
O GL GLUT GLEW GLSL OpenGL, GLUT, GLEW, GLSL An Overview GLUT GLEW GLSL GLEW GLSL

Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

  • Upload
    others

  • View
    48

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

O GL GLUT GLEW GLSLOpenGL, GLUT, GLEW, GLSLAn Overview

GLUT

GLEW GLSLGLEW GLSL

Page 2: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

Objectives

Give you an overview of the software that you will be using this semesterthat you will be using this semester OpenGL, GLUT, GLEW, GLSL

Wh t th ? What are they? How do you use them?

What does the code look like? What does the code look like? Evolution

All of them are required to write modern All of them are required to write modern graphics code, although alternatives exist

Page 3: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

What is OpenGL?

An application programming interface (API) A (low level) Graphics rendering API A (low-level) Graphics rendering API

Generate high-quality images from geometric and image primitivesand image primitives

Page 4: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

Maximal Portability

Display device independent Window system independent Window system independent

Operating system independent

(100 50) Line(100 50 150 80) device/lib 1

Without a standard API (such as OpenGL) - impossible to port

(100, 50)

(150 100)

Line(100,50,150,80) - device/lib 1

Moveto(100 50) - device/lib 2(150, 100) Moveto(100,50) device/lib 2Lineto(150,100)

Page 5: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

Brief History of OpenGL Originated from a proprietary API called Iris GL from Silicon

Graphics, Inc. Provide access to graphics hardware capabilities at the

lowest possible level that still provides hardware independence p

The evolution is controlled by OpenGL Architecture Review Board, or ARB.

OpenGL 1.0 API finalized in 1992, first implementation in 1993

In 2006 OpenGL ARB became a workgroup of the Khronos In 2006, OpenGL ARB became a workgroup of the Khronos Group

10+ revisions since 1992

Page 6: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

OpenGL Evolution 1.1 (1997): vertex arrays and texture objects 1.2 (1998): 3D textures ( ) 1.3 (2001): cubemap textures, compressed

textures, multitextures , 1.4 (2002): mipmap generation, shadow map

textures, etc 1.5 (2003): vertex buffer object, shadow

comparison functions, occlusion queries, non-power-of-2 textures

Page 7: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

OpenGL Evolution 2.0 (2004): vertex and fragment shading (GLSL

1.1), multiple render targets, etc 2.1 (2006): GLSL 1.2, pixel buffer objects, etc 3.0 (2008): GLSL 1.3, deprecation model, etc ( ) , p , 3.1 (2009): GLSL 1.4, texture buffer objects, move

much of deprecated functions to ARB compatible extension

3.2 (2009) 4.3 (August 2012)

Page 8: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

OpenGL Extensions New features/functions are marked with prefix Supported only by one vendor pp y y

NV_float_buffer (by nvidia) Supported by multiple vendors Supported by multiple vendors

EXT_framebuffer_object Reviewed by ARB Reviewed by ARB

ARB_depth_texture Promoted to standard OpenGL API Promoted to standard OpenGL API

Page 9: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

Deprecation Model, Contexts, p , ,and Profiles

Redundant and In-efficient functions are deprecated – to be removed in the futuredeprecated to be removed in the future glBegin(), glEnd()

OpenGL Contexts – data structures where OpenGL OpenGL Contexts data structures where OpenGL stores the state information used for rendering Textures, buffer objects, etc, j ,

Profile – A subset of OpenGL functionality used by an application program Core vs. Compatibility

Page 10: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

OpenGL Basics

OpenGL’s primary function – Renderingp p y g

Rendering? – converting geometric/mathematical object descriptions into frame buffer valuesobject descriptions into frame buffer values

OpenGL can render: Geometric primitives Bitmaps and Images (Raster primitives)

Page 11: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

OpenGL Programming Main Steps

Initialize OpenGL (using GLUT, discussed later) Define the geometry (points lines Define the geometry (points, lines,

triangles/polygons) Define the vertex attributes (color normal etc) Define the vertex attributes (color, normal, etc) Transform the geometry (translate, rotate, scale) Set up the camera (position direction angle etc) Set up the camera (position, direction, angle, etc) Set up lighting (light position/color etc)

Set up textures Set up textures Draw

Page 12: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

Primitive Types

Page 13: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

Specifying Geometric primitives

OpenGL 1.x

glBegin(primType); //e g GL TRIANGLESglBegin(primType); //e.g. GL_TRIANGLES glVertex3f(x,y,z);

…glEnd();

OpenGL 3.x and up: use Vertex Buffer Objects (VBO) Much more efficient since it allows the geometry to be stored in the

graphics card and reduce the number of function callsg p

glGenBuffers(1, &vboHandle); // create a VBO handle

glBindBuffer(GL_ARRAY_BUFFER, vboHandle); // bind the handle to the current VBO

glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); // allocate space and copy the data over

Page 14: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

OpenGL 1.x Code Examplevoid Display(){

glClear(GL_COLOR_BUFFER_BIT);glColor4f(1,1,0,1);glBegin(GL_POLYGON);

glVertex2f(-0.5, -0.5);glVertex2f(-0.5, 0.5);

vertex attribute

glVertex2f(0.5, 0.5);glVertex2f(0.5, -0.5);

glEnd();glFlush();

}

While glBegin/glEnd and glVertex*/glColor* have been deprecated it is still important to know them since there is adeprecated, it is still important to know them since there is a large amount of legacy code that uses them

I will introduce OpenGL VBO in a separate lecture

Page 15: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

glVertex* Command Formats

glVertex2f(x, y)g ( y)

Add ‘v’ for vectorform

number of Components/Dimensions

B – byteub – unsigned bytes – short

glVertex2fv(v)

Dimensions

2 – (x,y)3 – (x,y,z)

s shortus – unsigned shorti – intui – unsigned int ( ,y, )

4 – (x,y,z,w) or (r,g,b,a)

gf – floatd – double

Page 16: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

Rest of an OpenGL program

After the geometry is provided, OpenGL will control the GPUs to perform vertex and fragmentcontrol the GPUs to perform vertex and fragment processing Vertex processing: transformation and lighting (T&L) Fragment processing: shading and texture mapping

Your job is to control the processing through OpenGL/GLSL API, the focus of this course OpenGL 1.x: use the fixed function pipeline

O GL 3 d id d il d l OpenGL 3.x and up: provide more detailed control to the shaders

Page 17: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

Window-based programming

Most of the modern graphics systems are window basedwindow-based

Non-window based environmentWindow based environment

Non window based environment

Page 18: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

Window system independent

OpenGL is window system independentp y p No window management functions – create

windows, resize windows, event handling, tetc

This is to ensure the application’s portabilityportability

Create some headache though – just a pure OpenGL program won’t work pu Op G p og a o oanywhere.

Page 19: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

More APIs are needed

Microsoft Windows: WGL l h Apple Macintosh: AGL

X window system: GLX

These libraries provide complete functionality to create the drawing area as well as graphics iser interface (GUI) such as d a g a ea as e as g ap cs se te ace (GU ) suc assliders, buttons, , menus etc.

Problem – you need to learn them all to write a platform independent program

Page 20: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

Use GLUT (OpenGL Utility Toolkit)

For fast prototyping, you can use GLUT to interface with different window systems

GLUT is a window independent API – programs itt i O GL d GLUT b t d t Xwritten using OpenGL and GLUT can be ported to X

windows, MS windows, and Macintosh with no effort

GLUT does not contain all the bells and whistles though (no sliders, no dialog boxes, no menu bar, t )etc)

Page 21: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

GLUT Basics GLUT

Program Structure1. Configure OpenGL frame buffer2. Create a drawing area (window)3. Register input callback functions

Render Resize Input: keyboard, mouse, etc

4. Enter event processing loop

Page 22: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

Sample Program GLUT

#include <GL/glut.h>#include <GL/gl.h>

Void main(int argc, char** argv) {

int mode = GLUT_RGB|GLUT_SINGLE; _ | _glutInitDisplayMode(mode);glutInitWindowSize(500,500); glutCreateWindow(argv[0]); init(); glutDisplayFunc(display); glutKeyboardFunc(key); gl tMainLoop()glutMainLoop();

}

Page 23: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

Sample Program GLUT

#include <GL/glut.h>#include <GL/gl.h>

Void main(int argc, char** argv) {

int mode = GLUT_RGB|GLUT_SINGLE;glutInitDisplayMode(mode);glutInitWindowSize(500,500);glutCreateWindow(“Simple”); init();

Specify the display Mode – RGB or color Index, single or doubleBufferinit();

glutDisplayFunc(display); glutKeyboardFunc(key); glutMainLoop();

Buffer

glutMainLoop(); }

Page 24: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

Sample Program GLUT

#include <GL/glut.h>#include <GL/gl.h>

Void main(int argc, char** argv) {

int mode = GLUT_RGB|GLUT_SINGLE; glutInitDisplayMode(mode);glutInitWindowSize(500,500);glutCreateWindow(“Simple”);init();

Create a window Named “simple” with resolutioninit();

glutDisplayFunc(display); glutKeyboardFunc(key); glutMainLoop();

with resolution 500 x 500

glutMainLoop(); }

Page 25: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

Sample Program GLUT

#include <GL/glut.h>#include <GL/gl.h>

Void main(int argc, char** argv) {

int mode = GLUT_RGB|GLUT_SINGLE; glutInitDisplayMode(mode);glutInitWindowSize(500,500); glutCreateWindow(“Simple”); glutDisplayFunc(display);glutDisplayFunc(display); glutKeyboardFunc(key); glutMainLoop();

}}

Page 26: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

Sample Program GLUT

#include <GL/glut.h>#include <GL/gl.h>

Void main(int argc, char** argv) {

int mode = GLUT_RGB|GLUT_SINGLE; glutInitDisplayMode(mode);glutInitWindowSize(500,500); glutCreateWindow(“Simple”); init();init(); glutDisplayFunc(display); glutMainLoop();

}

Register your call back functions

}

Page 27: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

Callback functions? GLUT

Most of window-based programs are event-drivenevent driven – which means do nothing until an event happens, and then execute some pre-defined pp , pfunctions

Events – key press, mouse button press and release, window resize, etc.

Page 28: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

GLUTglutDisplayFunc(void (*func)(void) )

Void main(int argc, char** argv) {

…glutDisplayFunc(display); …

}}void display() – a functionwritten by you. It contains allthe OpenGL drawing functionthe OpenGL drawing functioncalls and will be calledwhen pixels in the window need to be refreshed.

Page 29: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

Event Queue GLUT

Event queue Keyboard

….

Mouse

Window

MainLoop()

Window

Mouse_callback() {

Keypress_callback() {

window_callback() {….{

{….{

{….{

Page 30: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

And many more … GLUT

glutKeyboardFunc() – register the callback that will be called when a key is pressedwill be called when a key is pressed

glutMouseFunc() – register the callback that will be called when a mouse button is pressedbe ca ed e a ouse butto s p essed

glutMotionFunc() – register the callback that will be called when the mouse is in motion while a buton is pressed

glutIdleFunc() – register the callback that will be lled hen nothing i going on (no e ent)called when nothing is going on (no event)

Page 31: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

GLUTglutMainLoop() #include <GL/glut.h>#include <GL/gl.h>

Void main(int argc, char** argv) {

int mode = GLUT_RGB|GLUT_SINGLE; glutInitDisplayMode(mode);glutInitWindowSize(500,500); glutCreateWindow(“Simple”); glutDisplayFunc(display);glutDisplayFunc(display); glutReshapeFunc(resize); glutKeyboardFunc(key); glutMainLoop();glutMainLoop();

} The program goes into a infinite loop waiting for events

Page 32: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

GLEW: The OpenGL ExtensionGLEW: The OpenGL Extension Wrangler Library

A cross-platform open-source C/C++ extension loading library ( i d OS X Li F BSD)loading library (windows, OS X, Linux, FreeBSD)

Why GLEW is needed? It is not possible to link directly to functions that are It is not possible to link directly to functions that are provided in newer version of OpenGL. In windows, this means OpenGL 1.2 and up

GLEW does the tedious work to helps you find the function pointers (addresses of the functions) for OpenGL extensions

GLEW can also help you to check if a particular OpenGL p y p pextension is available on your machine

Page 33: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

GLEW usage

Make sure you have GLEW on your machine Include the glew header file Include the glew header file

#include <GL/glew.h>

Initialize glew before calling any opengl functionsg g y p gGLenum err = glewInit();

if ( err != GLEW_OK) printf(" Error initializing GLEW! \n"); else printf("Initializing GLEW succeeded!\n");else printf( Initializing GLEW succeeded!\n );

Check OpenGL features, for example, shadersif (! GLEW_ARB_vertex_program)

printf(" ARB vertex program is not supported!!\n"); else printf(" ARB vertex program is supported!!\n");

Page 34: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

OpenGL Shading Language p g g g(GLSL) A C-like language and incorporated into

OpenGL 2.0OpenGL 2.0 Used to write vertex program and

fragment programfragment program No distinction in the syntax between a

vertex program and a fragmentvertex program and a fragment program l f d d d C Platform independent compared to Cg

Page 35: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

Shader Objects Shaders are defined as an array of strings Four steps to using a shader Four steps to using a shader

Send shader source to OpenGL Compile the shaderp Create an executable (i.e., link compiled shaders

together) Install the executable as part of current state

Goal was to mimic C/C++ source code development model

Page 36: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

Sequence

Page 37: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

The Programmable GPU

Vertex Shader

Fragment Shader

vertices

Transformd h Clipping

PrimitiveAssembly

dTexture Fragment

primitives

And Lighting Clipping AndRasterization

Stages Testing

GPU = vertex shader (vertex program) + fragment shader (fragment program, pixel program)

Vertex shader replaces per-vertex transform & lighting Fragment shader replaces texture stages Fragment shader replaces texture stages Fragment testing after the fragment shader Flexibility to do framebuffer pixel blending

Page 38: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

GPU programming model“Stream programming” “Stream programming” Process each vertex or fragment

independentlyindependently

Vertex Shaderv1 v2 v v1 v2 vShaderv1 v2 … vn v1 v2 … vn

Fragment Shaderf1 f2 … fn f1 f2 … fn

Page 39: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

The idea

You specify vertices as usual Vertex positions, texture coordinates, etc. Vertex positions, texture coordinates, etc. And some user variables if you want

The vertex shader modifies/calculates these variables.

Each fragment gets the interpolated values, which i ht h b difi dmight have been modified.

The fragment shader can now work on the interpolated values including the user definedinterpolated values, including the user defined variables.

Page 40: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

Vertex Program

Replace the fixed-function operations performed by the vertex processor p

A vertex program is executed on each vertex Each vertex program must output the information that

the rasterizer needsthe rasterizer needs At a minimum – transforms the vertex position

The program can access all OpenGL statesC t l t t di t t i l ti Current color, texture coordinates, material properties, transformation matrices, etc

The application can also supply additional input a iables to the e te p og amvariables to the vertex program

Page 41: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

A very simple vertex programvoid main(void) {

l P iti l P j ti M t i * l M d lVi M t i * l V tgl_Position = gl_ProjectionMatrix*gl_ModelViewMatrix*gl_Vertex; }

Just a passing-through shader: convert a vertex from local space to clip space No color is assigned here so the fragment program will No color is assigned here, so the fragment program will need to decide the fragment colors

All variables starts with ‘gl ’ are part of OpenGL state so g _ p pno need to declare

Page 42: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

GLSL Data Types Supported data types are very similar to

C/C++: float, int, bool, etcC/C++: float, int, bool, etc Additional types examples: vec2, vec3,

vec4 mat2 mat3 mat4vec4, mat2, mat3, mat4 Can use C++ style constructor

3 3(1 0 2 1 1 2) vec3 a = vec3(1.0, 2.1, -1.2); vec3 b = vec2(a); //conversion

Page 43: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

GLSL Qualifiers Three types of variables: Attributes,

Uniform, VaryingUniform, Varying Attribute: used by vertex shaders for variables

that can change once per vertex g p Build-in attributes: gl_Vertex, gl_FrontColor User-definted attributes (example): temperature,

velocity

Uniform: variables set for the entire primitive, i i d t id lB i ()/ lE d()i.e., assigned outside glBegin()/glEnd(); Also include build-in and user-definted

Page 44: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

GLSL Qualifiers (cont’d) Varying variables: the mechanism for

conveying data from a vertex programconveying data from a vertex program to a fragment program

Defined on a per vertex basis but Defined on a per vertex basis but interpolated over the primitive for the fragment programfragment program.

Include build-in and user defined varying variablesvarying variables

Page 45: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

Vertex Processor Input

Vertex shader is executed once each time a vertex position is specified

Vi lV lD A h ll Via glVertex or glDrawArrays or other vertex array calls Per-vertex input values are called “attributes”

Change every vertexg y Passed through normal OpenGL mechanisms (per-vertex

API or vertex arrays) More persistent input values are called “uniforms”p p

Can come from OpenGL state or from the application Constant across at least one primitive, typically constant

for many primitives Passed through new OpenGL API calls

Page 46: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

Vertex Processor Output

Vertex shader uses input values to compute p poutput values

Vertex shader must compute gl_Position Mandatory, needed by the rasterizer Can use built-in function ftransform() to get

invariance with fixed functionalityinvariance with fixed functionality

Page 47: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

Vertex Processor Output

Other output values are called “varying” variablesE l t t di t bit d t E.g., color, texture coordinates, arbitrary data

Will be interpolated in a perspective-correct fashion across the primitivesDefined by the vertex shader Defined by the vertex shader

Can be of type float, vec2, vec3, vec4, mat2, mat3, mat4, or arrays of these

Output of vertex processor feeds into OpenGL fixed functionality Output of vertex processor feeds into OpenGL fixed functionality If a fragment shader is active, output of vertex shader must

match input of fragment shader If no fragment shader is active output of vertex shader must If no fragment shader is active, output of vertex shader must

match the needs of fixed functionality fragment processing

Page 48: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

Fragment Program The fragment program is executed after rasterizer

and operate on each fragment Vertex attributes (colors, positions, texture

coordinates, etc) are interpolated across a primitive automatically as the input to the fragment programF O GL Fragment program can access OpenGL state, (interpolated) output from vertex program, and user defined variablesuser defined variables

Page 49: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

A very simple fragment program

void main(void) {

l F C l l F tC lgl_FragColor = gl_FrontColor; }

Just a passing-through fragment shader

Page 50: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

Fragment Program Input

Output of vertex shader is the input to the fragment shadershader Compatibility is checked when linking occurs Compatibility between the two is based on varying

variables that are defined in both shaders and that matchvariables that are defined in both shaders and that match in type and name

Fragment shader is executed for each fragment p od ed b te i tionproduced by rasterization

For each fragment, fragment shader has access to the interpolated value for each varying variablet e te po ated a ue o eac a y g a ab e Color, normal, texture coordinates, arbitrary values

Page 51: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

Fragment Processor Input

Fragment shader may access: gl FrontFacing contains “facingness” of primitive that gl_FrontFacing – contains facingness of primitive that

produced the fragment gl_FragCoord – contains computed window relative

di 1/coordinates x, y, z, 1/w

Uniform variables are also available OpenGL state or supplied by the application same as for OpenGL state or supplied by the application, same as for

vertex shader

If no vertex shader is active, fragment shader get the results of OpenGL fixed functionality

Page 52: Opengl intro 5542web.cse.ohio-state.edu/~wang.3602/courses/cse5542... · OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Definethegeometry(pointslinesDefine

Fragment Processor Output

Output of the fragment processor goes on to the fixed function fragment operations and frame bufferfixed function fragment operations and frame buffer operations using built-in variables gl_FragColor – computed R, G, B, A for the fragment

l h d d h l f h f gl_FragDepth – computed depth value for the fragment gl_FragData[n] – arbitrary data per fragment, stored in

multiple render targets Values are destined for writing into the frame buffer if all

back end tests (stencil, depth etc.) pass Clamping or format conversion to the target buffer Clamping or format conversion to the target buffer

is done automatically outside of the fragment shader