50
OPENGL INTRODUCTION 林林林 2014/10/06

O penGL Introduction

Embed Size (px)

DESCRIPTION

O penGL Introduction. 林 宏 祥 2014/10/06. 此投影片已被稍微簡化過,跟今日課堂上的內容不太一樣。 如果想要看更詳細說明的人,請參考每頁投影片下面的 『 備忘稿 』. Today We will talk about…. 1. Rasterization and transformation implementation 2. OpenGL progress. rasterization. 2 D image. 3D model. RASterization. transformation clipping - PowerPoint PPT Presentation

Citation preview

Page 1: O penGL Introduction

OPENGL INTRODUCTION

林宏祥 2014/10/06

Page 2: O penGL Introduction

此投影片已被稍微簡化過,跟今日課堂上的內容不太一樣。如果想要看更詳細說明的人,請參考每頁投影片下面的『備忘稿』

Page 3: O penGL Introduction

TODAY WE WILL TALK ABOUT…

1. Rasterization and transformation implementation

2. OpenGL progress

Page 4: O penGL Introduction

RASTERIZATION

3D model

2D image

Page 5: O penGL Introduction

RASTERIZATION

transformation

clipping

scan conversion

suited for hardware acceleration

Page 6: O penGL Introduction

TRANSFORMATION

=> matrix vector multiplication

=> from object space to clipping space

Space transition: a vector is multiplied by a corresponding transformation matrix.

The series of space: a definition describe the rendering process

Page 7: O penGL Introduction

LOADING MODEL FILETriangle

126.000000 202.500000 0.000000 -0.902861 -0.429933 -0.00000012

89.459999 202.500000 89.459999 -0.637936 -0.431364 -0.63793612

88.211967 209.144516 88.211967 -0.703896 0.095197 -0.70389512

Triangle

88.211967 209.144516 88.211967 -0.703896 0.095197 -0.70389512

124.242210 209.144516 0.000000 -0.995496 0.094807 -0.00000012

126.000000 202.500000 0.000000 -0.902861 -0.429933 -0.00000012

Triangle

89.459999 202.500000 89.459999 -0.637936 -0.431364 -0.63793612

0.000000 202.500000 126.000000 -0.000000 -0.429933 -0.90286112

0.000000 209.144516 124.242210 -0.000000 0.094807 -0.99549612

Triangle

0.000000 209.144516 124.242210 -0.000000 0.094807 -0.99549612

88.211967 209.144516 88.211967 -0.703895 0.095197 -0.70389612

89.459999 202.500000 89.459999 -0.637936 -0.431364 -0.63793612

object coordinate

Page 8: O penGL Introduction

Rotation

Translation

MOVE AND ROTATE OBJECT

Page 9: O penGL Introduction

world coordinate

Page 10: O penGL Introduction

camera coordinate

camera parameters in : eye point, reference point, up vector

ADD CAMERA

Page 11: O penGL Introduction

NOW WE ENTER CLIPPING SPACE

Clipping space: a viewing volume in 3D space.

The viewing volume is related to projection model.

Page 12: O penGL Introduction

Orthogonal Projection

-Z

X

xcamerax

cameraxx

Page 13: O penGL Introduction

Perspective Projection

-Z

X

xcamera

x

d

z

dz

xx camera

Page 14: O penGL Introduction

Viewing volume of orthogonal projection

Near Clipping Plane

Far Clipping PlaneLeft Clipping Plane

Right Clipping Plane

Bottom Clipping Plane

Top Clipping Plane

x

y

-z

(camera coordinates)

Page 15: O penGL Introduction

Viewing volume of perspective projection

Far Clipping Plane

Near Clipping Plane

Left Clipping Plane

Right Clipping Plane

Bottom Clipping Plane

Top Clipping Plane

x

y

-z

(camera coordinates)

Page 16: O penGL Introduction

What is the transformation from camera space to clipping space?

Let’s see normalized device coordinates space.

Page 17: O penGL Introduction

NDC SPACE

NDC space: a viewing volume in 3D space (cuboid).

x

y

z

Note: NDC is a left-hand coordinate system (because of z-buffer)

Page 18: O penGL Introduction

enclosed by (in OpenGL system): xNDC = 1, xNDC=-1, yNDC=1, yNDC=-1, zNDC =1, zNDC=-1

Page 19: O penGL Introduction

FROM CLIPPING SPACETO NDC SPACE

viewing volume in clipping space == viewing volume in NDC space after the division operation.

(Clipping coordinates is also a left-hand coordinate system)

Page 20: O penGL Introduction

Viewing volume of orthogonal projection

nearVal

farValleft

right

bottom

top

x

y

-z

(camera coordinates)

Page 21: O penGL Introduction

1

1000

200

02

0

002

1camera

camera

camera

clipping

clipping

clipping

z

y

x

nearValfarVal

nearValfarVal

nearValfarVal

bottomtop

bottomtop

bottomtop

leftright

leftright

leftright

z

y

x

1NDC

NDC

NDC

z

y

x

NDC space

Page 22: O penGL Introduction

A CANONICAL VIEW

xNDC0

1-1

xcamera

rightleft

Viewing volume on NDC space

Viewing Volume on camera space

2

rightleft

Page 23: O penGL Introduction

1)(1

0x

leftright2

leftrightx

clippingcamera

same mapping in y direction and z direction

The z component will multiply (-1) for flipping.

Page 24: O penGL Introduction

zFar

zNear

Viewing volume of perspective projection

(camera coordinates)

x

y

-z

Page 25: O penGL Introduction

)2/tan(

1

fovyf fovy: the viewing angle in y direction

Page 26: O penGL Introduction

Perspective Projection

-Z

X

xcamera

x

d = 1

some scaling

-Z

X

d

z

dz

xx camera

NDC

Page 27: O penGL Introduction

A CANONICAL VIEW..

-Z

-zNear

-zFar

y0

zcamerafovy

Y

Page 28: O penGL Introduction

f

1

z-

y)

2

fovytan(

camera

0

-Z

-zNear

-zFar

y0

zcamerafovy

Y

Page 29: O penGL Introduction

After projection, the boundaries should map to 1, -1 )

fz-

(

yy

camera

cameraNDC

-Z

-zNear

-zFar

y0

zcamerafovy

Y

ycamera

Page 30: O penGL Introduction

Similarly in x, but consider the aspect.

)f

z-(aspect

xx

camera

cameraNDC

)f

z-(

yy

camera

cameraNDC

Page 31: O penGL Introduction

cz

m

camera

NDCz

cameraz

1NDCz

Mapping on z: inverse proportional to camera coordinates.

Page 32: O penGL Introduction

cz

m

camera

NDCz

-zFar is mapping to 1, -zNear is mapping to -1, then

czFar-

m1

czNear-

m1

zFar-zNear

zNearzFar c ,

zFar)-(zNear

zNearzFar2m

Page 33: O penGL Introduction

zFar-zNear

zNearzFar

)zFar)(-z-(zNear

zNearzFar2z

cameraNDC

)f

z-(

yy

camera

cameraNDC

)f

z-(aspect

xx

camera

cameraNDC

1

0100

200

100

100

camera

camera

camera

camera

clipping

clipping

clipping

z

y

x

zFarzNear

zNearzFar

zFarzNear

zNearzFarf

aspect

f

z

z

y

x

1

)/(

)/(

)/(

cameraclipping

cameraclipping

cameraclipping

zz

zy

zx

NDC space

Page 34: O penGL Introduction

FROM NDC SPACE TO WINDOW SPACE

02)1( xw

xx NDCwindow

02)1( yh

yy NDCwindow

(x0, y0): window position

(w, h): size of the window

Page 35: O penGL Introduction

SCAN CONVERSION

Vector to fragments

Page 36: O penGL Introduction

OPENGL PROGRESS

Fixed openGL pipeline (openGL 1.x)

Programmable openGL pipeline (above openGL 2.x)

Page 37: O penGL Introduction

Fixed openGL pipeline Programmable openGL pipeline

The programming becomes very different since modern hardware (GPU) has changed.

Page 38: O penGL Introduction

ADVANTAGE ON FIXED PIPELINE

Easy to learn: regard the pipeline as a black box.

Page 39: O penGL Introduction

DIS-ADVANTAGE ON FIXED PIPELINE PROGRAMMING

Debugging is hard if you do not know openGL pipeline.

(In modern graphic programming, we “must” understand the programmable system or we cannot programming easily)

Only provide specific functions, thus limiting the creativity and problem solving.

The programming on modern programmable GPU is totally different from that of fixed pipeline. (You must learn from the start).

Page 40: O penGL Introduction

DEPRECATED聲明不贊成 & EVIL OPENGL

OpenGL 4.2 Reference card: Blue means deprecated www.khronos.org/files/opengl42-quick-reference-card.pdf

Current version: OpenGL & GLSL 4.3

OpenGL 4.3 Reference card: Deprecated functions are gone!

http://www.khronos.org/files/opengl43-quick-reference-card.pdf

and many many many more…

…since 2008!(GLSL since 2004)

GPUs have changed!

(from “progressive openGL” 2012 slides)

Page 41: O penGL Introduction

RENDER LOOP(CLIENT)

initialize window

load shader program

clear frame buffer

Update transformation

Update Objects

Draw Object

SwapBuffers

while (running):

a linear array on GPU memory

Page 42: O penGL Introduction

SHADER DATA Uniform= Shared Constant

Vertex Data= ANYTHING YOU WANT!

Example?Positions…Normals…Colors…

Texture Coordinates…

“Per-object constant”(from “progressive openGL” slides, 2012)

Page 43: O penGL Introduction

SHADER DATA AND GLSL

OpenGL 4.2 Reference card: Blue means deprecated www.khronos.org/files/opengl42-quick-reference-card.pdf

“Per-object constant”

(from “progressive openGL” slides, 2012)

Page 44: O penGL Introduction

IN VS. OUT

Vertex Shader

in = Data from Vertex Buffer

out = Rasterizer in

FragmentShader

in = Rasterizer out

out = ANYTHING YOU WANT!

http://en.wikibooks.org/wiki/GLSL_Programming/Rasterization

http://en.wikibooks.org/wiki/GLSL_Programming/Rasterization

Rasterizer

GPU Memory

Fragment

Pixel… but usually pixel color and depth

(from “progressive openGL” slides, 2012)

Page 45: O penGL Introduction

EXAMPLE: DRAW AN TRIANGLE

Page 46: O penGL Introduction

CLIENT//declare a linear array on CPU memory

static const GLfloat g_vertex_buffer_data[] = {

-1.0f, -1.0f, 0.0f, (use xyz vector to represent a vertex)

1.0f, -1.0f, 0.0f,

0.0f, 1.0f, 0.0f, };

GLuint vertexbuffer;

// generate the vertex buffer object(VBO)

glGenBuffers(1, &vertexbuffer);

// bind VBO to GL_ARRAY_BUFFER, a state in OpenGL context

glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);

// allocate GPU memory and copy VBO content to the memory

glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);

Page 47: O penGL Introduction

CLIENTdo{

// Use shader program

glUseProgram(programID);

// Give an attribute id to the VBO ( a VBO can be assigned many attribute ids)

glEnableVertexAttribArray(0);

//bind VBO vertexbuffer again (in other application we may have many VBOs)

glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0 );//describe the content in VBO

// Draw the triangle

glDrawArrays(GL_TRIANGLES, 0, 3); // 3 indices starting at 0 -> 1 triangle

….

// Swap buffers

glfwSwapBuffers();

} while( ……);

(3 floats for a point) (offset) (padding)

(read 3 vertices once)

Page 48: O penGL Introduction

VERTEX SHADER

//specify GLSL version#version 330 core// Get vertex data from the VBO according to the vertex attribute id. The vertex data will stored in declared variable “vertex_position”layout(location = 0) in vec3 vertex_position;

void main(){// gl_Position is a built-in variable in GLSL, which is an output variable of the vertex shadergl_Position = vec4(vertex_position, 1.0);

}

note: vertex shader “must” output vertex position (in clipping coordinate space) to let OpenGL system perform scan conversion

Page 49: O penGL Introduction

FRAGMENT SHADER

#version 330//declare an output variable “color” to the imageout vec3 color;void main(){// output red color for each segmentcolor = vec3(1,0,0);}

Note:

In fragment shader, it receives a fragment in a “triangle” when vertex shader finish processing three vertices (remember GL_TRIANGLE in client code?)

The fragment is already in window coordinate here. We can derive the coordinate from the built-in variable for other application

Page 50: O penGL Introduction

REFERENCE

Dominik Seifert, “Progressive OpenGL” slides, 2012 ICG course.

Hong-Shiang Lin, “ICG clipping” slides, 2012 ICG course.

Jason L.McKesson, “Learning Modern 3D Graphics Programming” website. 2012