Graphics hardware and software SzirmayKalos Lszl Interactive graphics

  • Slides: 24
Download presentation
Graphics hardware and software Szirmay-Kalos László

Graphics hardware and software Szirmay-Kalos László

Interactive graphics systems Input pipeline CPU: op sys Device To Screen Inv. Viewport transf

Interactive graphics systems Input pipeline CPU: op sys Device To Screen Inv. Viewport transf Inv. Proj. transf Inv. View transf Upside down! camera Screen refresh GPU hw Frame buffer Pixel proc Rasterization Inv. Model transf. Viewport transf vectorization CPU: app Clip Output pipeline: GPU Virtual world Proj. transf View transf Model transf Shader processors

Software architecture glut…( ) Application (CPU) on. Display() on. Mouse() on. Keyboard() freeglut Graphics

Software architecture glut…( ) Application (CPU) on. Display() on. Mouse() on. Keyboard() freeglut Graphics library: Open. GL Operating System (Windows) gl… 3 dv( ) Frame buffer Drawing (GPU)

Event handling (GLUT) App. Window setup Callback registration main Display. Func Operating system Keyboad.

Event handling (GLUT) App. Window setup Callback registration main Display. Func Operating system Keyboad. Func freeglut Keyboad. Up. Func Special. Func callbacks Reshape Mouse. Func Graphics hardware Open. GL Motion. Func Idle. Func application

Open. GL 3. 3 … 4. 6 Normalized screen gl. Tex. Parameter gl. Uniform

Open. GL 3. 3 … 4. 6 Normalized screen gl. Tex. Parameter gl. Uniform Pixel szín shader Screen Pixel address frame buffer reg 1 reg 2 reg 3 reg 4 … gl_Frag. Coord Pixel color Compositing Vertex shader reg 1 reg 2 reg 3 reg 4 … gl_Position Texture memory Sampler Raszterizáció reg 1, reg 2, reg 3 …: Attrib. Array Clip, homdiv gl. Viewporttr. Viewport transzform gl. Point. Size, gl. Line. Width Rasterization VAO Vágás, Homogén osztás VBO Vertex Buffer Object gl. Uniform CPU program

Vertex data streaming interleaved VAO VB 0 0 x 1, y 1 x 2,

Vertex data streaming interleaved VAO VB 0 0 x 1, y 1 x 2, y 2 x 3, y 3 VBO 0 x 1, y 1 R 1, G 1, B 1 x 2, y 2 R 2, G 2, B 2 x 3, y 3 R 3, G 3, B 3 Vertex shader Input registers VBO 1 R 1, G 1, B 1 R 2, G 2, B 2 R 3, G 3, B 3 gl. Vertex. Attrib. Pointer Attrib. Array 0 Attrib. Array 1 in var 2 Vertex Shader Attrib. Array …

My first Open. GL program (100, 100) #include <windows. h> // Only in Ms.

My first Open. GL program (100, 100) #include <windows. h> // Only in Ms. Win #include <GL/glew. h> // download #include <GL/freeglut. h> // download 600 int main(int argc, char * argv[]) { glut. Init(&argc, argv); // init glut. Init. Context. Version(3, 3); glut. Init. Window. Size(600, 600); glut. Init. Window. Position(100, 100); glut. Init. Display. Mode(GLUT_RGBA|GLUT_DOUBLE); 600 glut. Create. Window(“Hi Graphics"); //show App. Window glew. Experimental = true; // magic glew. Init(); // init glew gl. Viewport(0, 0, 600); //photo on. Initialization(); // next slide (-0. 6, 1) glut. Display. Func(on. Display); //event handler glut. Main. Loop(); return 1; } (1, 1) (-0. 8, -0. 8) (-1, -1) (0. 8, -0. 2)

on. Initialization() unsigned int shader. Program; unsigned int vao; // virtual world on the

on. Initialization() unsigned int shader. Program; unsigned int vao; // virtual world on the GPU void on. Initialization() { gl. Gen. Vertex. Arrays(1, &vao); gl. Bind. Vertex. Array(vao); // make it active vao vbo 324 vertices bytes 2 floats/vertex Attrib. Array 0 unsigned int vbo; // vertex buffer object gl. Gen. Buffers(1, &vbo); // Generate 1 buffer gl. Bind. Buffer(GL_ARRAY_BUFFER, vbo); // Geometry with 24 bytes (6 floats or 3 x 2 coordinates) float vertices[] = {-0. 8, -0. 6, 1. 0, 0. 8, -0. 2}; gl. Buffer. Data(GL_ARRAY_BUFFER, // Copy to GPU target sizeof(vertices), // # bytes vertices, // address GL_STATIC_DRAW); // we do not change later gl. Enable. Vertex. Attrib. Array(0); // Attrib. Array 0 gl. Vertex. Attrib. Pointer(0, // vbo -> Attrib. Array 0 2, GL_FLOAT, GL_FALSE, // two floats/attrib, not fixed-point 0, NULL); // stride, offset: tightly packed

#version 330 precision highp float; uniform mat 4 MVP; layout(location = 0) in vec

#version 330 precision highp float; uniform mat 4 MVP; layout(location = 0) in vec 2 vp; //Attrib. Array 0 #version 330 precision highp float; uniform vec 3 color; out vec 4 out. Color; void main() { gl_Position = vec 4(vp. x, vp. y, 0, 1) * MVP; } void main() { out. Color = vec 4(color, 1); } static const char * vertex. Source = R"( … )“; static const char * fragment. Source = R"( … )“; unsigned int vertex. Shader = gl. Create. Shader(GL_VERTEX_SHADER); gl. Shader. Source(vertex. Shader, 1, &vertex. Source, NULL); gl. Compile. Shader(vertex. Shader); unsigned int fragment. Shader=gl. Create. Shader(GL_FRAGMENT_SHADER); gl. Shader. Source(fragment. Shader, 1, &fragment. Source, NULL); gl. Compile. Shader(fragment. Shader); shader. Program = gl. Create. Program(); gl. Attach. Shader(shader. Program, vertex. Shader); gl. Attach. Shader(shader. Program, fragment. Shader); gl. Bind. Frag. Data. Location(shader. Program, 0, "out. Color"); gl. Link. Program(shader. Program); gl. Use. Program(shader. Program); }

uniform mat 4 MVP; layout(location = 0) in vec 2 vp; uniform vec 3

uniform mat 4 MVP; layout(location = 0) in vec 2 vp; uniform vec 3 color; out vec 4 out. Color; void main() { gl_Position = vec 4(vp. x, vp. y, 0, 1) * MVP; } void main() { out. Color = vec 4(color, 1); } void on. Display( ) { gl. Clear. Color(0, 0, 0, 0); // background color gl. Clear(GL_COLOR_BUFFER_BIT); // clear frame buffer // Set vertex. Color to (0, 1, 0) = green int location = gl. Get. Uniform. Location(shader. Program, “color"); gl. Uniform 3 f(location, 0. 0 f, 1. 0 f, 0. 0 f); // 3 floats float MVPtransf[4][4] = { 1, 0, 0, 0, 1, 0, 0, 0, 1 }; // MVP matrix, // row-major! row-major location = gl. Get. Uniform. Location(shader. Program, "MVP"); gl. Uniform. Matrix 4 fv(location, 1, GL_TRUE, &MVPtransf[0][0]); gl. Bind. Vertex. Array(vao); // Draw call gl. Draw. Arrays(GL_TRIANGLES, 0 /*start. Idx*/, 3 /*# Elements*/); glut. Swap. Buffers( ); // exchange buffers for double buffering }

Open. GL primitives gl. Draw. Arrays( primitive. Type, start. Idx, num. Of. Elements); Vectorized

Open. GL primitives gl. Draw. Arrays( primitive. Type, start. Idx, num. Of. Elements); Vectorized parametric curve GL_POINTS GL_LINES Output of Ear clipping GL_TRIANGLES GL_LINE_STRIP Tessellated parametric surface GL_TRIANGLE_STRIP GL_LINE_LOOP „Convex” polygon GL_TRIANGLE_FAN

Green triangle versus Victor Vasarely

Green triangle versus Victor Vasarely

Vertex attributes, dynamic world, world and image spaces, interaction

Vertex attributes, dynamic world, world and image spaces, interaction

Texturing 1: Upload to GPU VAO Vertex Shader gl. Tex. Parameter CPU gl. Tex.

Texturing 1: Upload to GPU VAO Vertex Shader gl. Tex. Parameter CPU gl. Tex. Image 2 D Texture texture. Id Sampler GL_TEXTURE 0 unsigned int texture. Id; Fragment Shader Sampler. Unit Your responsibility void Upload. Texture(int width, int height, vector<vec 4>& image) { gl. Gen. Textures(1, &texture. Id); gl. Bind. Texture(GL_TEXTURE_2 D, texture. Id); // binding Mip-map level Border gl. Tex. Image 2 D(GL_TEXTURE_2 D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_FLOAT, &image[0]); //Texture -> GPU gl. Tex. Parameteri(GL_TEXTURE_2 D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); gl. Tex. Parameteri(GL_TEXTURE_2 D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); }

Texturing 2: Equip objects with texture coordinates gl. Gen. Vertex. Arrays(1, &vao); gl. Bind.

Texturing 2: Equip objects with texture coordinates gl. Gen. Vertex. Arrays(1, &vao); gl. Bind. Vertex. Array(vao); gl. Gen. Buffers(2, vbo); // Generate 2 vertex buffer objects // vertex coordinates: vbo[0] -> Attrib Array 0 -> vertices gl. Bind. Buffer(GL_ARRAY_BUFFER, vbo[0]); float vtxs[] = {x 1, y 1, x 2, y 2, …}; gl. Buffer. Data(GL_ARRAY_BUFFER, sizeof(vtxs), vtxs, GL_STATIC_DRAW); gl. Enable. Vertex. Attrib. Array(0); gl. Vertex. Attrib. Pointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL); // vertex coordinates: vbo[1] -> Attrib Array 1 -> uvs gl. Bind. Buffer(GL_ARRAY_BUFFER, vbo[1]); float uvs[] = {u 1, v 1, u 2, v 2, …}; gl. Buffer. Data(GL_ARRAY_BUFFER, sizeof(uvs), uvs, GL_STATIC_DRAW); gl. Enable. Vertex. Attrib. Array(1); gl. Vertex. Attrib. Pointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL);

Texturing 3: Vertex és Pixel Shader layout(location = 0) in vec 2 vtx. Pos;

Texturing 3: Vertex és Pixel Shader layout(location = 0) in vec 2 vtx. Pos; layout(location = 1) in vec 2 vtx. UV; out vec 2 texcoord; void main() { gl_Position = vec 4(vtx. Pos, 0, 1) * MVP; texcoord = vtx. UV; … } uniform sampler 2 D sampler. Unit; in vec 2 texcoord; out vec 4 fragment. Color; void main() { fragment. Color = texture(sampler. Unit, texcoord); }

Texturing 4: Active texture and sampler CPU Texture texture. Id Sampler GL_TEXTURE 0 Shader

Texturing 4: Active texture and sampler CPU Texture texture. Id Sampler GL_TEXTURE 0 Shader Processor sampler. Unit unsigned int texture. Id; void Draw( ) { int sampler = 0; // which sampler unit should be used int location = gl. Get. Uniform. Location(shader. Prog, "sampler. Unit"); gl. Uniform 1 i(location, sampler); gl. Active. Texture(GL_TEXTURE 0 + sampler); // = GL_TEXTURE 0 gl. Bind. Texture(GL_TEXTURE_2 D, texture. Id); gl. Bind. Vertex. Array(vao); gl. Draw. Arrays(GL_TRIANGLES, 0, n. Vtx); }

float vtx = {-1, 1, 1, -1, 1}; gl. Buffer. Data(GL_ARRAY_BUFFER, sizeof(vtx), vtx, GL_STATIC_DRAW);

float vtx = {-1, 1, 1, -1, 1}; gl. Buffer. Data(GL_ARRAY_BUFFER, sizeof(vtx), vtx, GL_STATIC_DRAW); (vr, vt) Image viewer NDC: (+1, +1) Tex: (1, 1) (u, v) (vl, vb) Screen: pixels NDC: (-1, -1) Tex: (0, 0) Full screen Textured quad (u, v) uniform sampler 2 D texture. Unit; in vec 2 uv; out vec 4 fragment. Color; (0, 0) Interpolated tex. Coord void main() { fragment. Color = texture(texture. Unit, uv); Texture } layout(location = 0) in vec 2 vp; // Attrib Array 0 out vec 2 uv; // output attribute void main() { uv = (vp + vec 2(1, 1)) / 2; // clipping to texture space gl_Position = vec 4(vp. x, vp. y, 0, 1); }

float vtx = {-1, 1, 1, -1, 1}; gl. Buffer. Data(GL_ARRAY_BUFFER, sizeof(vtx), vtx, GL_STATIC_DRAW);

float vtx = {-1, 1, 1, -1, 1}; gl. Buffer. Data(GL_ARRAY_BUFFER, sizeof(vtx), vtx, GL_STATIC_DRAW); (vr, vt) Image viewer NDC: (+1, +1) Tex: (1, 1) (u, v) (vl, vb) Screen: pixels NDC: (-1, -1) Tex: (0, 0) Full screen Textured quad (u*, v*) (0, 0) Interpolated tex. Coord Texture layout(location = 0) in vec 2 vp; // Attrib Array 0 out vec 2 uv; // output attribute void main() { uv = (vp + vec 2(1, 1)) / 2; // clipping to texture space gl_Position = vec 4(vp. x, vp. y, 0, 1); }

u* texels Magic lense pixels r uc r u uniform sampler 2 D texture.

u* texels Magic lense pixels r uc r u uniform sampler 2 D texture. Unit; uniform vec 2 uvc; // cursor position in texture space in vec 2 uv; // interpolated texture coordinates out vec 4 fragment. Color; void main() { const float r 2 = 0. 05 f; float d 2 = dot(uv - uvc, uv - uvc); vec 2 tuv = (d 2 < r 2) ? (uv - uvc) * d 2 / r 2 + uvc : uv; fragment. Color = texture(texture. Unit, tuv); }

Swirl uniform sampler 2 D texture. Unit; uniform vec 2 uvc; // cursor position

Swirl uniform sampler 2 D texture. Unit; uniform vec 2 uvc; // cursor position in texture space in vec 2 uv; // interpolated texture coordinates out vec 4 fragment. Color; void main() { const float a = 8, alpha = 15; float ang = a * exp( -alpha * length(uv - uvc) ); mat 2 rot. Mat = mat 2( cos(ang), sin(ang), -sin(ang), cos(ang) ); vec 2 tuv = (uv - uvc) * rot. Mat + uvc; fragment. Color = texture(texture. Unit, tuv); }

Gravity (black hole) z=0 z=1 (1, 1) (u, v) (u*, v*) (0, 0) Interpolated

Gravity (black hole) z=0 z=1 (1, 1) (u, v) (u*, v*) (0, 0) Interpolated tex. Coord Texture

= Equivalence principle p dir void main() { const float r 0 = 0.

= Equivalence principle p dir void main() { const float r 0 = 0. 09 f, ds = 0. 001 f; vec 3 p = vec 3(uv, 0), dir = vec 3(0, 0, 1), blackhole = vec 3(uvc, 0. 5 f); float r 2 = dot(blackhole - p, blackhole - p); while (p. z < 1 && r 2 > r 0 * r 0) { p += dir * ds; r 2 = dot(blackhole - p, blackhole - p); vec 3 g. Dir = (blackhole - p)/sqrt(r 2); // gravity direction dir = normalize(dir * ds + g. Dir * r 0 / r 2 / 4 * ds); } if (p. z >= 1) fragment. Color = texture(texture. Unit, vec 2(p. x, p. y)); else fragment. Color = vec 4(0, 0, 0, 1); }

Wave wave. Dist=c*time wave. Width=0. 03 ang. In-ang. Refr d texture uvc water. Depth=1

Wave wave. Dist=c*time wave. Width=0. 03 ang. In-ang. Refr d texture uvc water. Depth=1 uv tuv uniform float time; const float PI = 3. 14159265, n = 1. 33, c = 0. 1, a. Max = 0. 1; void main() { float d = length(uv - uvc), wave. Dist = c * time; if (abs(d - wave. Dist) < wave. Width) { float ang. In = a. Max/wave. Dist * sin((wave. Dist-d)/wave. Width*PI); float ang. Refr = asin(ang. In)/n); vec 2 dir = (uv - uvc)/d; vec 2 tuv = uv + dir * tan(ang. In - ang. Refr) * water. Depth; fragment. Color = texture(texture. Unit, tuv); } else { fragment. Color = texture(texture. Unit, uv); } }