OpenGL Examples

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 73

OpenGL Examples

https://www.youtube.com/watch?v=PgIYzCLetAc
Overview
1. Get objects and textures
2. Load object
3. Load texture
4. Render
5. Key event
Find objects and textures
1. Using existed program: Blender, 3DMAX, ……
2. Online: CGTrader, Free3D……
Load object
Common object format: .obj, .stl, .3ds
Load object
Load object
Load object
Load object
Load object --- VAO, VBO
VAO
VBO

VBO

VBO
Load object --- VAO, VBO
Load object --- VAO, VBO
VAO
VBO
Load object --- VAO, VBO
Load object --- VAO, VBO
VAO
VBO

VBO
Load object --- VAO, VBO
Load object --- VAO, VBO
VAO
VBO

VBO

VBO
Texture
Texture https://www.youtube.com/watch?v=scPSP_U858k&t=1284s
Load texture
Load texture
Load texture
Load texture
VAO
VBO

VBO

VBO

VBO
Load texture
Load texture
Load texture
Load texture

64*64
69*69

256*256

https://forum.unity.com/threads/what-is-the-purpose-of-the-mip-maps-and-how-they-work.490622/
Load texture --- Mipmap
Load texture

64*64
69*69
256*256

https://forum.unity.com/threads/what-is-the-purpose-of-the-mip-maps-and-how-they-work.490622/
Sprite
Sprite
Render
glfwSwapBuffer
Render

……
Render

……
Y
Key Event
Key Event
Example2
Example2
Example2
Example2
Example2

𝑅 𝑅 𝑅
2 , 2 ,2
Example2

𝑅 𝑅 𝑅

2, 2 ,2
Example2

𝑅 𝑅 𝑅

2 , 2 ,2
Example2

−𝑅 𝑅 𝑅

2 , 2 ,2
Example2

𝑆𝑡𝑎𝑟𝑡 = 𝑔𝑙𝑓𝑤𝐺𝑒𝑡𝑇𝑖𝑚𝑒()
𝑇 = 𝑔𝑙𝑓𝑤𝐺𝑒𝑡𝑇𝑖𝑚𝑒() − 𝑆𝑡𝑎𝑟𝑡

𝜃 = 90 ∗ 𝑇 / Animation_Duration
Example2
1.

Rotate -> Translate?

2.

Translate -> Rotate?


Example2
1.

Rotate -> Translate

2.

Translate -> Rotate


Example2
1.
𝜃
Y

2.
Y
𝜃
Example2
1.
𝜃
Y

2.
Y
𝜃
Shader and GLSL
Application GPU Data Flow Framebuffer

Vertices Vertices Fragments Pixels


Vertex Fragment
Rasterizer
Processing Processing

Vertex Fragment
Shader Shader
Create
Program unsigned int program = glCreateProgram();

Create GLuint vs = glCreateShader(GL_VERTEX_SHADER);


Shader GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);

Load Shader glShaderSource(vs, 1, (const GLchar**)&vertex_shader, nullptr);


Source glShaderSource(fs, 1, (const GLchar**)&fragment_shader, nullptr);

Compile glCompileShader(vs);
Shader glCompileShader(fs);

Attach Shader glAttachShader(program, vs);


to Program glAttachShader(program, fs);

Link Program glLinkProgram(program);

Use Program glUseProgram()


Shader and GLSL --- Simplest example
Shader and GLSL
Shader and GLSL --- Texture
Vertex Shader Fragment Shader
Shader and GLSL --- Texture
Shader and GLSL --- Render in 3D
1. Model
◦ Translation
◦ Rotation
◦ Scaling

2. View
◦ glm::lookAt( eye , center , up)

3. Projection
◦ glm::perspective(FOVY , aspect_ratio, zNear, zFar)
◦ ortho (left, right, bottom, top, zNear, zFar)
Shader and GLSL --- Render in 3D
Vertex Shader
Shader and GLSL --- Render in 3D
Vertex Shader Main.cpp
Shader and GLSL --- Render in 3D
Vertex Shader Main.cpp

Projection = glm::perspective(glm::radians(45.0f),
640.0f / 480, 1.0f, 100.f);

View = glm::lookAt(glm::vec3(40.0f), glm::vec3(),


glm::vec3(0, 1, 0));

setUniformMat4(program, “vp”, Projection * View);

setUniformMat4(program, “model”,
translation*rotation*scale);
Why we see what we see
Factors that affect the “color” of a pixel:
◦ Light sources
◦ Color of Light source
◦ Position and direction
◦ Attenuation
◦ Object’s surface property
◦ Color of object
◦ Position, micro-structure
◦ Absorption
Light-Object interactions
Ambient
Environment lighting
◦ even when it is dark there is usually still some light somewhere in the world
◦ so objects are almost never completely dark.
◦ To simulate this we use an ambient lighting constant that always gives the object some color.
Diffuse
Simulates the directional impact a light object has on an object.
◦ This is the most visually significant component of the lighting model
◦ The more a part of an object faces the light source, the brighter it becomes.
Diffuse
Simulates the directional impact a light object has on an object.
◦ This is the most visually significant component of the lighting model
◦ The more a part of an object faces the light source, the brighter it becomes.

DiffuseStrength =
LightDirection·Normal
Specular(reflection)
Simulates the bright spot of a light that appears on shiny objects.
Specular highlights are often more inclined to the color of the light than the color of the object.
Specular(reflection)
Specular(reflection)

SpecularStrength = a*(R·V)P
Light-Object interactions
Ambient
◦ Constant

Diffuse
◦ DiffuseStrength = LightDirection·Normal

Specular
◦ SpecularStrength = a*(R·V) P
Shader and GLSL --- Shading
Flat shading

Gouraud shading

Phong shading

Blinn-Phong shading
Shader and GLSL --- Blinn-Phong Shading
The calculation of R makes the program slow.
So replace (R·V) by (N·H)

H=(L+V)/|L+V|
Shader and GLSL --- Advance Shading
Ocean:
◦ https://www.shadertoy.com/view/lsBSWm

Global Illumination:
◦ https://www.shadertoy.com/results?query=Global+Illumination

Physically based rendering (PBR):


◦ https://www.youtube.com/watch?v=HaOOWK14pgs
Reference
https://github.com/syoyo/tinyobjloader
https://learnopengl.com/Getting-started/Shaders
https://learnopengl.com/Getting-started/Transformations
http://ogldev.atspace.co.uk/www/tutorial19/tutorial19.html
http://math.hws.edu/graphicsbook/c4/s1.html

You might also like