42
OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson C OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

Embed Size (px)

Citation preview

Page 1: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 20141 Photo credit :Johnson Cameraface

OpenGL Basics

Page 2: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 20142

OpenGL-ES Basics

How to draw stuffHow to draw stuff• Use the OS to create the display

How to draw stuff• Use the OS to create the display

• Connect the display to OpenGL

How to draw stuff• Use the OS to create the display

• Connect the display to OpenGL

• Initialization Steps– Load resources

– Models, images, shaders, etc.

How to draw stuff• Use the OS to create the display

• Connect the display to OpenGL

• Initialization Steps– Load resources

– Models, images, shaders, etc.

• Rendering Steps– Iterate through each visible object, making OpenGL

state and rendering calls as needed

How to draw stuff• Use the OS to create the display

• Connect the display to OpenGL

• Initialization Steps– Load resources

– Models, images, shaders, etc.

• Rendering Steps– Iterate through each visible object, making OpenGL

state and rendering calls as needed

• Process Any Input

How to draw stuff• Use the OS to create the display

• Connect the display to OpenGL

• Initialization Steps– Load resources

– Models, images, shaders, etc.

• Rendering Steps– Iterate through each visible object, making OpenGL

state and rendering calls as needed

• Process Any Input

• Update object state based upon input or elapsed time

How to draw stuff• Use the OS to create the display

• Connect the display to OpenGL

• Initialization Steps– Load resources

– Models, images, shaders, etc.

• Rendering Steps– Iterate through each visible object, making OpenGL

state and rendering calls as needed

• Process Any Input

• Update object state based upon input or elapsed time

• Cleanup - release resources, disconnect OpenGL

Page 3: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 20143

OpenGL-ES Basics

OpenGL is a State Machine

OpenGL is a state machine – in other words, it takes direct action by the user to change state.

State changes are serial.

The state is stored in the OpenGL context.

State is: Active textures, active shaders, current vertex and index data, active render targets, shader uniforms, culling settings, winding order, raster operations, active stencil operations, blending, texture filtering, depth testing, etc. etc. etc.

When you make a render call, what gets rendered depends upon what state was active at that time.

Page 4: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 20144

OpenGL is a State Machine

OpenGL-ES Basics

OpenGL is a State Machine

OpenGL is a state machine – in other words, it takes direct action by the user to change state.

State changes are serial.

The state is stored in the OpenGL context.

State is: Active textures, active shaders, current vertex and index data, active render targets, shader uniforms, culling settings, winding order, raster operations, active stencil operations, blending, texture filtering, depth testing, etc. etc. etc.

When you make a render call, what gets rendered depends upon what state was active at that time.

Do not forget this….

Page 5: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 20145

OpenGL-ES Basics

Rendering an object

Assuming that all vertex, texture data is loaded:

1. Set the state for the object.a) Position and Projection

– Set the model transformation and projection matricies– (vertex shader)

b) Shading– Lighting parameters, materials, blending– (fragment shader)

c) Misc. state– Winding order, stencil, viewport, etc.– (glEnable/glDisable, etc.)

2. Invoke a render call

Page 6: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 20146

OpenGL-ES 2.0/3.0 Pipeline

PrimitiveDrawProcessing

VertexProcessing Vertex

PrimitiveProcessing

RasterizationFragment

ProcessingFragmentPixel

ProcessingPixel

Image

VertexShader

FragmentShader

no stream/discard

fail/discard no write

AP

I C

ALL

Page 7: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 20147

3D Math overview

Page 8: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 20148

World Coordinates

Each object starts off in it’s own 3D coordinate system

Object Coordinates Eye Coordinates

View MatrixModel Matrix

Down the pipeline - Viewing the scene

Page 9: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 20149

For multiple objects, they need to be set into world coordinates through the use of the Model Matrix

Object Coordinates World Coordinates Eye Coordinates

View MatrixModel Matrix

Down the pipeline - Viewing the scene

Page 10: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 201410

After the transformation, the are all in the same coordinate system – World Coordinates

World CoordinatesObject Coordinates Eye Coordinates

View MatrixModel Matrix

Down the pipeline - Viewing the scene

Page 11: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 201411

Eye Coordinates

Then we use the View Matrix to put everything relative to the eye

World CoordinatesObject Coordinates World Coordinates Eye Coordinates

View MatrixModel Matrix

Down the pipeline - Viewing the scene

Page 12: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 201412

Eye Coordinates

Then we use the View Matrix to put everything relative to the eye

World CoordinatesObject Coordinates World Coordinates Eye Coordinates

View MatrixModel Matrix

Down the pipeline - Viewing the scene

Page 13: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 201414

Shader Overview

OpenGL ES: 2 programmable stages - “shaders”.• Vertex Shader: Convert vertex position into “eye

space” – called for each vertex in a primitive

• Fragment Shader: Compute color of fragment – called for each fragment (i.e. pixel)

A primitive is created from vertices in 3D space

A Fragment is aligned to

the pixel grid.

Page 14: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 201415

Vertex Shaders

The Vertex Shader is the programmable shader stage in the rendering pipeline that handles the processing of individual vertices.

The Vertex Shader takes Vertex Attribute data, process one vertex at a time and generates one output vertices.

Vertex shaders typically perform transformations to post-projection space, for consumption later rendering stages.

They can also be used to do per-vertex lighting, or to perform setup work for later shader stages. Any per-vertex calculations can be done in a vertex shader.

Page 15: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 201416

Vertex Shader (OpenGL-ES-3.0)

#version 300 es

layout (location = 0) in highp vec3 a_Position;layout (location = 1) in mediump vec4 a_Color;

out mediump vec4 v_color;uniform mat4 u_MVP;

void main() { gl_Position = u_MVP * vec4(a_Position, 1.0); v_color = a_Color;}

Page 16: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 201417

Vertex Shader

#version 300 es

layout (location = 0) in highp vec3 a_Position;layout (location = 1) in mediump vec4 a_Color;

out mediump vec4 v_color;uniform mat4 u_MVP;

void main() { gl_Position = u_MVP * vec4(a_Position, 1.0); v_color = a_Color;}

Page 17: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 201418

Vertex Shader

#version 300 es

layout (location = 0) in highp vec3 a_Position;layout (location = 1) in mediump vec4 a_Color;

out mediump vec4 v_color;uniform mat4 u_MVP;

void main() { gl_Position = u_MVP * vec4(a_Position, 1.0); v_color = a_Color;}

Page 18: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 201419

Vertex Shader

#version 300 es

layout (location = 0) in highp vec3 a_Position;layout (location = 1) in mediump vec4 a_Color;

out mediump vec4 v_color;uniform mat4 u_MVP;

void main() { gl_Position = u_MVP * vec4(a_Position, 1.0); v_color = a_Color;}

Page 19: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 201420

Vertex Shader

Application code

Shader code

glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,i,vdata);glVertexAttribPointer(1,4,GL_FLOAT,GL_FALSE,i,vdata+3);

glEnableVertexAttribArray( 0 );glEnableVertexAttribArray( 1 );

#version 300 es

layout (location = 0) in highp vec3 a_Position;layout (location = 1) in mediump vec4 a_Color;

Page 20: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 201421

Vertex Shader

glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,i,vdata);glVertexAttribPointer(1,4,GL_FLOAT,GL_FALSE,i,vdata+3);

glEnableVertexAttribArray( 0 );glEnableVertexAttribArray( 1 );

#version 300 es

layout (location = 0) in highp vec3 a_Position;layout (location = 1) in mediump vec4 a_Color;

Application code

Shader code

Page 21: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 201422

Vertex Shader

glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,i,vdata);glVertexAttribPointer(1,4,GL_FLOAT,GL_FALSE,i,vdata+3);

glEnableVertexAttribArray( 0 );glEnableVertexAttribArray( 1 );

#version 300 es

layout (location = 0) in highp vec3 a_Position;layout (location = 1) in mediump vec4 a_Color;

Application code

Shader code

Page 22: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 201423

Vertex Shader

#version 300 es

layout (location = 0) in highp vec3 a_Position;layout (location = 1) in mediump vec4 a_Color;

out mediump vec4 v_color;uniform mat4 u_MVP;

void main() { gl_Position = u_MVP * vec4(a_Position, 1.0); v_color = a_Color;}

Page 23: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 201424

Vertex Shader

#version 300 es

layout (location = 0) in highp vec3 a_Position;layout (location = 1) in mediump vec4 a_Color;

out mediump vec4 v_color;uniform mat4 u_MVP;

void main() { gl_Position = u_MVP * vec4(a_Position, 1.0); v_color = a_Color;}

Page 24: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 201425

Vertex Shader

#version 300 es

layout (location = 0) in highp vec3 a_Position;layout (location = 1) in mediump vec4 a_Color;

out mediump vec4 v_color;uniform mat4 u_MVP;

void main() { gl_Position = u_MVP * vec4(a_Position, 1.0); v_color = a_Color;}

Page 25: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 201426

Vertex Shader

#version 300 es

layout (location = 0) in highp vec3 a_Position;layout (location = 1) in mediump vec4 a_Color;

out mediump vec4 v_color;uniform mat4 u_MVP;

void main() { gl_Position = u_MVP * vec4(a_Position, 1.0); v_color = a_Color;}

Page 26: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 201427

Vertex Shader

#version 300 es

layout (location = 0) in highp vec3 a_Position;layout (location = 1) in mediump vec4 a_Color;

out mediump vec4 v_color;uniform mat4 u_MVP;

void main() { gl_Position = u_MVP * vec4(a_Position, 1.0); v_color = a_Color;}

Page 27: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 201428

Vertex Shader

#version 300 es

layout (location = 0) in highp vec3 a_Position;layout (location = 1) in mediump vec4 a_Color;

out mediump vec4 v_color;uniform mat4 u_MVP;

void main() { gl_Position = u_MVP * vec4(a_Position, 1.0); v_color = a_Color;}

Page 28: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 201429

Vertex Shader

#version 300 es

layout (location = 0) in highp vec3 a_Position;layout (location = 1) in mediump vec4 a_Color;

out mediump vec4 v_color;uniform mat4 u_MVP;

void main() { gl_Position = u_MVP * vec4(a_Position, 1.0); v_color = a_Color;}

Page 29: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 201430

Vertex Shader Fragment Shader

Overview

The vertex shader is responsible for converting input vertices into projected out vertices.

Eventually the rasterization engine will convert these vertices into interpolated pixel locations for processing by the fragment shader.

A primitive is created from vertices in 3D space

A Fragment is aligned to the pixel grid.

Page 30: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 201431

Vertex Shader Fragment Shader

Interpolation

The rasterization step is how the pipeline gets from vertices to pixels.

The vertex shader converts input to output vertices.

The rasterizer converts the transformed primitives to pixel-grid fragments.

The fragment shader get run – ONCE FOR EACH FRAGMENT. Potentially millions of times!

Page 31: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 201432

Rasterization & Interpolation

Page 32: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 201433

Fragment Shader

#version 300 es

precision mediump float;

in mediump vec4 v_color;

layout (location = 0) out lowp vec4 o_Color;

void main() { o_Color = v_color;}

Page 33: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 201434

Vertex Shader

#version 300 es

layout (location = 0) in highp vec3 a_Position;layout (location = 1) in mediump vec4 a_Color;

out mediump vec4 v_color;uniform mat4 u_MVP;

void main() { gl_Position = u_MVP * vec4(a_Position, 1.0); v_color = a_Color;}

Page 34: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 201435

Fragment Shader

#version 300 es

precision mediump float;

in mediump vec4 v_color;

layout (location = 0) out lowp vec4 o_Color;

void main() { o_Color = v_color;}

Page 35: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 201436

Fragment Shader

#version 300 es

precision mediump float;

in mediump vec4 v_color;

layout (location = 0) out lowp vec4 o_Color;

void main() { o_Color = v_color;}

Page 36: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 201437

Fragment Shader

#version 300 es

precision mediump float;

in mediump vec4 v_color;

layout (location = 0) out lowp vec4 o_Color;

void main() { o_Color = v_color;}

Page 37: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 201438

Fragment Shader

#version 300 es

precision mediump float;

in mediump vec4 v_color;

layout (location = 0) out lowp vec4 o_Color;

void main() { o_Color = v_color;}

Page 38: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 201439

Fragment Shader

#version 300 es

precision mediump float;

in mediump vec4 v_color;

layout (location = 0) out lowp vec4 o_Color;

void main() { o_Color = v_color;}

Page 39: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 201440

Fragment Shader

#version 300 es

precision mediump float;

in mediump vec4 v_color;

layout (location = 0) out lowp vec4 o_Color;

void main() { o_Color = v_color;}

Page 40: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 201441

Vertex Shader

#version 300 es

layout (location = 0) in highp vec3 a_Position;layout (location = 1) in mediump vec4 a_Color;

out mediump vec4 v_color;uniform mat4 u_MVP;

void main() { gl_Position = u_MVP * vec4(a_Position, 1.0); v_color = a_Color;}

Page 41: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 201442

Vertex Shader

#version 300 es

layout (location = 0) in highp vec3 a_Position;layout (location = 1) in mediump vec4 a_Color;

out mediump vec4 v_color;uniform mat4 u_MVP;

void main() { gl_Position = u_MVP * vec4(a_Position, 1.0); v_color = a_Color;}

smoothsmoothflat

Page 42: OpenGL-ES 3.0 And Beyond Boston 2014 1 Photo credit :Johnson Cameraface OpenGL Basics

OpenGL-ES 3.0 And Beyond Boston 201443

Fragment Shader

#version 300 es

precision mediump float;

flat in mediump vec4 v_color;

layout (location = 0) out lowp vec4 o_Color;

void main() { o_Color = v_color;}