OpenGL Examples
OpenGL Examples
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.
2.
2.
2.
Y
𝜃
Example2
1.
𝜃
Y
2.
Y
𝜃
Shader and GLSL
Application GPU Data Flow Framebuffer
Vertex Fragment
Shader Shader
Create
Program unsigned int program = glCreateProgram();
Compile glCompileShader(vs);
Shader glCompileShader(fs);
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);
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