270 likes | 423 Views
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.
E N D
OpenGL Introduction 林宏祥 2014.04.10
Today We will talk about… • 1. Rasterizationand transformation implementation • 2. OpenGL progress
rasterization 2D image 3D model
RASterization • transformation • clipping • scan conversion The pipeline is suited for hardware acceleration
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
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
Move and rotate object Translation Rotation
ADD camera camera coordinate camera parameters in : eye point, reference point, up vector
Clipping Clipping space denotes a viewing volume in 3D space. The viewing volume is related to projection model.
Orthogonal Projection X xcamera x -Z
Perspective Projection X xcamera x -Z d z
scan conversion • Vector to fragments • (There may be multiple fragments within one pixel)
OpenGL PROGRESS • Fixed openGL pipeline (openGL 1.x) • Programmable openGL pipeline (above openGL 2.x)
The programming becomes very different since modern hardware (GPU) has changed. Fixed openGL pipeline Programmable openGL pipeline
Advantage on fixed pipeline • Easy to learn: regard the pipeline as a black box.
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).
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…
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
Shader Data (from “progressive openGL” slides, 2012) “Per-object constant” = Shared Constant Uniform Vertex Data = ANYTHING YOU WANT! Example? Positions… Normals… Colors… Texture Coordinates…
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
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);
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)
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
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
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