1 / 50

O penGL Introduction

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

Download Presentation

O penGL Introduction

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. OpenGL Introduction 林宏祥 2014/10/06

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

  3. Today We will talk about… • 1. Rasterizationand transformation implementation • 2. OpenGL progress

  4. rasterization 2D image 3D model

  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. Move and rotate object Translation Rotation

  9. world coordinate

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

  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 X xcamera x -Z

  13. Perspective Projection X xcamera x -Z d z

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

  15. Viewing volumeof perspective projection y (camera coordinates) -z x Left Clipping Plane Far Clipping Plane Top Clipping Plane Near Clipping Plane Bottom Clipping Plane Right Clipping Plane

  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). Note: NDC is a left-hand coordinate system (because of z-buffer) y z x

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

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

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

  21. NDC space

  22. A canonical view Viewing volume on NDC space Viewing Volume on camera space 1 left right -1 xNDC 0 xcamera

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

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

  25. fovy: the viewing angle in y direction

  26. Perspective Projection X X xcamera x -Z -Z d d = 1 z some scaling

  27. A canonical view.. Y -zFar -zNear y0 fovy zcamera -Z

  28. Y -zFar -zNear y0 fovy zcamera -Z

  29. Y -zFar -zNear y0 ycamera fovy zcamera -Z After projection, the boundaries should map to 1, -1

  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 (x0, y0): 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. The programming becomes very different since modern hardware (GPU) has changed. Fixed openGL pipeline Programmable openGL pipeline

  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 …since 2008! (GLSL since 2004) (from “progressive openGL” 2012 slides) Current version: OpenGL & GLSL 4.3 OpenGL 4.2 Reference card: Blue means deprecatedwww.khronos.org/files/opengl42-quick-reference-card.pdf OpenGL 4.3 Reference card: Deprecated functions are gone!http://www.khronos.org/files/opengl43-quick-reference-card.pdf GPUs have changed! and many manymany more…

  41. Render Loop(client) while (running): a linear array on GPU memory initialize window load shader program clear frame buffer Update transformation Update Objects Draw Object SwapBuffers

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

  43. Shader Data and GLSL (from “progressive openGL” slides, 2012) OpenGL 4.2 Reference card: Blue means deprecatedwww.khronos.org/files/opengl42-quick-reference-card.pdf “Per-object constant”

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

  45. example: draw an triangle

  46. client • //declare a linear array on CPU memory • static constGLfloatg_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, }; • GLuintvertexbuffer; • // 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) (padding) (offset) (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

More Related