Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "OPENGL INTRODUCTION 林宏祥 2014/10/06. 此投影片已被稍微簡化過,跟今日課堂上的內容不太一樣。 如果想要看更詳細說明的人,請參考每頁投影片下面的『備忘稿』"— Presentation transcript:

1 OPENGL INTRODUCTION 林宏祥 2014/10/06

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

3 TODAY WE WILL TALK ABOUT… 1. Rasterization and transformation implementation 2. OpenGL progress

4 RASTERIZATION 3D model 2D image

5 RASTERIZATION transformation clipping scan conversion suited for hardware acceleration

6 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

7 LOADING MODEL FILE Triangle 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

8 Rotation Translation MOVE AND ROTATE OBJECT

9 world coordinate

10 camera coordinate camera parameters in : eye point, reference point, up vector ADD CAMERA

11 NOW WE ENTER CLIPPING SPACE Clipping space: a viewing volume in 3D space. The viewing volume is related to projection model.

12 Orthogonal Projection -Z X x camera x

13 Perspective Projection -Z X x camera x d z

14 Viewing volume of orthogonal projection Near Clipping Plane Far Clipping Plane Left Clipping Plane Right Clipping Plane Bottom Clipping Plane Top Clipping Plane x y -z (camera coordinates)

15 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)

16 What is the transformation from camera space to clipping space? Let’s see normalized device coordinates space.

17 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)

18 enclosed by (in OpenGL system): x NDC = 1, x NDC =-1, y NDC =1, y NDC =-1, z NDC =1, z NDC =-1

19 FROM CLIPPING SPACE TO 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)

20 Viewing volume of orthogonal projection nearVal farVal left right bottom top x y -z (camera coordinates)

21 NDC space

22 A CANONICAL VIEW x NDC 0 1 x camera right left Viewing volume on NDC space Viewing Volume on camera space

23 same mapping in y direction and z direction The z component will multiply (-1) for flipping.

24 zFar zNear Viewing volume of perspective projection (camera coordinates) x y -z

25 fovy: the viewing angle in y direction

26 Perspective Projection -Z X x camer a x d = 1 some scaling -Z X d z

27 A CANONICAL VIEW.. -Z -zNear -zFar y0y0 z camera fovy Y

28 -Z -zNear -zFar y0y0 z camera fovy Y

29 After projection, the boundaries should map to 1, -1 -Z -zNear -zFar y0y0 z camera fovy Y y camera

30 Similarly in x, but consider the aspect.

31 Mapping on z: inverse proportional to camera coordinates.

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

33 NDC space

34 FROM NDC SPACE TO WINDOW SPACE (x 0, y 0 ): window position (w, h): size of the window

35 SCAN CONVERSION Vector to fragments

36 OPENGL PROGRESS Fixed openGL pipeline (openGL 1.x) Programmable openGL pipeline (above openGL 2.x)

37 Fixed openGL pipelineProgrammable openGL pipeline The programming becomes very different since modern hardware (GPU) has changed.

38 ADVANTAGE ON FIXED PIPELINE Easy to learn: regard the pipeline as a black box.

39 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).

40 DEPRECATED 聲明不贊成 & EVIL OPENGL OpenGL 4.2 Reference card: Blue means deprecated www.khronos.org/files/opengl42-quick-reference-card.pdf 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 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)

41 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

42 SHADER DATA Uniform = Shared Constant Vertex Data = ANYTHING YOU WANT! Example? Positions… Normals… Colors… Texture Coordinates… “Per-object constant” (from “progressive openGL” slides, 2012)

43 SHADER DATA AND GLSL OpenGL 4.2 Reference card: Blue means deprecated www.khronos.org/files/opengl42-quick-reference-card.pdf www.khronos.org/files/opengl42-quick-reference-card.pdf “Per-object constant” (from “progressive openGL” slides, 2012)

44 IN VS. OUT Vertex Shader in = Data from Vertex Buffer out = Rasterizer in Fragment Shader in = Rasterizer out out = ANYTHING YOU WANT! http://en.wikibooks.org/wiki/GLSL_Programming/Rasterization Rasterizer GPU Memory … but usually pixel color and depth (from “progressive openGL” slides, 2012)

45 EXAMPLE: DRAW AN TRIANGLE

46 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);

47 CLIENT do{ … // 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)

48 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 shader gl_Position = vec4(vertex_position, 1.0); } note: vertex shader “must” output vertex position (in clipping coordinate space) to let OpenGL system perform scan conversion

49 FRAGMENT SHADER #version 330 //declare an output variable “color” to the image out vec3 color; void main() { // output red color for each segment color = 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

50 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


Download ppt "OPENGL INTRODUCTION 林宏祥 2014/10/06. 此投影片已被稍微簡化過,跟今日課堂上的內容不太一樣。 如果想要看更詳細說明的人,請參考每頁投影片下面的『備忘稿』"

Similar presentations


Ads by Google