1 / 27

O penGL Introduction

O penGL Introduction. 林宏祥 2014.04.10. Today We will talk about…. 1. Rasterization and transformation implementation 2. OpenGL progress. rasterization. 2 D image. 3D model. RASterization. transformation clipping scan conversion. The pipeline is suited for hardware acceleration.

latona
Télécharger la présentation

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.04.10

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

  3. rasterization 2D image 3D model

  4. RASterization • transformation • clipping • scan conversion The pipeline is suited for hardware acceleration

  5. transformation • => matrix vector multiplication • => from object space to clipping space Space transition happens when a vector is multiplied by a corresponding transformation matrix. The series of space is just a definition: to better describe the process from 3D world to 2D image

  6. 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

  7. Move and rotate object Translation Rotation

  8. world coordinate

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

  10. Clipping Clipping space denotes a viewing volume in 3D space. The viewing volume is related to projection model.

  11. Orthogonal Projection X xcamera x -Z

  12. Perspective Projection X xcamera x -Z d z

  13. scan conversion • Vector to fragments • (There may be multiple fragments within one pixel)

  14. OpenGL PROGRESS • Fixed openGL pipeline (openGL 1.x) • Programmable openGL pipeline (above openGL 2.x)

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

  16. Advantage on fixed pipeline • Easy to learn: regard the pipeline as a black box.

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

  18. 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 What’s the big deal?! GPUs have changed! and many manymany more…

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

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

  21. 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

  22. example: draw an triangle

  23. 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);

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

  25. 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

  26. 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

  27. 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