Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 20

AND VISUALIZATION

02 | Shading Language
COMPUTER GRAPHICS AND VISUALIZATION
Agenda
 Rendering Loop
 VAO, EBO, VBO
 GLSL
 Vertex Shader
 Fragment Shader
 Hello World
COMPUTER GRAPHICS AND VISUALIZATION
Objectives
 Explain Rendering Loop using OpenGL.
 Explain OpenGL Shading Language.
COMPUTER GRAPHICS AND VISUALIZATION
Rendering Loop : Pseudo Code
// initialization commands here
...
// render loop
while(!windowClosed)
{
// input
processInput(window);
// rendering commands here
...
// check and call events and swap the buffers
pollEvents();
swapBuffers(window);
}
COMPUTER GRAPHICS AND VISUALIZATION
Vertex Array Object
 A Vertex Array Object (VAO) is an OpenGL Object that
stores all of the state needed to supply vertex data. It
stores the format of the vertex data as well as the
Buffer Objects providing the vertex data arrays
(khronos, 2018).
 Enable or disable VAO: glEnableVertexAttribArray or
glDisableVertexAttribArray.
 Vertex attribute configurations:
glVertexAttribPointer.
COMPUTER GRAPHICS AND VISUALIZATION
Vertex Array Object (cont.)

VAO and VBO (learnopengl.com, 2017)


COMPUTER GRAPHICS AND VISUALIZATION
Vertex Buffer Object
 A Vertex Buffer Object (VBO) is the common term for a
normal Buffer Object when it is used as a source for
vertex array data. It is no different from any other
buffer object, and a buffer object used for Transform
Feedback or asynchronous pixel transfers can be used
as source values for vertex arrays (khronos, 2018).
COMPUTER GRAPHICS AND VISUALIZATION
Vertex Buffer Object (cont.)

Vertex Buffer Data Format (learnopengl.com, 2018)


COMPUTER GRAPHICS AND VISUALIZATION
Element Buffer Object
 An element buffer object (EBO) is a buffer, just like a
vertex buffer object, that stores indices that OpenGL
uses to decide what vertices to draw (learnopengl.com,
2018).
 Benefit : vertex data sharing.
COMPUTER GRAPHICS AND VISUALIZATION
Element Buffer Object (cont.)

VA, VBO and EBO (learnopengl.com, 2018)


COMPUTER GRAPHICS AND VISUALIZATION
GLSL
#version version_number
in type in_variable_name;
in type in_variable_name;
out type out_variable_name;
uniform type uniform_name;
void main()
{
// process input(s) and do some weird graphics stuff
...
// output processed stuff to output variable
out_variable_name = weird_stuff_we_processed;
}

Shader Common Structure (learnopengl.com, 2017)


COMPUTER GRAPHICS AND VISUALIZATION
GLSL (cont.)
 Basic Types: int, float, double, uint, bool.
 Vectors:
 vecn: the default vector of n floats.
 bvecn: a vector of n booleans.
 ivecn: a vector of n integers.
 uvecn: a vector of n unsigned integers.
 dvecn: a vector of n double components.
 In and Out : define shader input and shader output.
 Uniforms: another way to pass data from our
application on the CPU to the shaders on the GPU
(global variable).
COMPUTER GRAPHICS AND VISUALIZATION
GLSL (cont.)
 To pass data into GPU using glUniform:
 f: the function expects a float as its value.
 i: the function expects an int as its value.
 ui: the function expects an unsigned int as its value.
 3f: the function expects 3 floats as its value.
 fv: the function expects a float vector/array as its value.
COMPUTER GRAPHICS AND VISUALIZATION
Vertex Shader
 The Vertex Shader is the programmable Shader stage in
the rendering pipeline that handles the processing of
individual vertices. (khronos, 2018)
COMPUTER GRAPHICS AND VISUALIZATION
Vertex Shader (cont.)
#version 330 core
layout (location = 0) in vec3 aPos;
void main()
{
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
}
COMPUTER GRAPHICS AND VISUALIZATION
Fragment Shader
 A Fragment Shader is the Shader stage that will process
a Fragment generated by the Rasterization into a set of
colors and a single depth value. (khronos, 2018)
COMPUTER GRAPHICS AND VISUALIZATION
Fragment Shader (cont.)
#version 330 core
out vec4 FragColor;
void main()
{
FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
}
COMPUTER GRAPHICS AND VISUALIZATION
Hello World : Draw a triangle

Index 3 Index 0
(-0.5, 0.5, 0) (0.5, 0.5, 0)

Quad Drawing:
1st Triangle : 0, 1, 3
2nd Triangle : 1, 2, 3

(-0.5, -0.5, 0) (0.5, -0.5, 0)


Index 2 Index 1
COMPUTER GRAPHICS AND VISUALIZATION
Hello World : Draw a triangle (cont.)
Initialization:
Generate and define vertex buffer data using VBO.
Define format vertex buffer data using VAO.
Define data indices using EBO.
Compile Vertex Shader and Fragment Shader.
Create Shader Program.
Render Loop:
Enable VAO.
Render a triangle.
COMPUTER GRAPHICS AND VISUALIZATION
References
 Interactive 3D Graphics, Autodesk, Eric Haines.
 Beginning OpenGL Game Programming, Course
Technology PTR, Luke Benstead, Dave Astle, Kevin
Hawkins.

You might also like