apresentacao_glsl.odp

Download Report

Transcript apresentacao_glsl.odp

Introduction to OpenGL
Shading Language (GLSL)
from little examples
Example 1 – Source Code
// Vertex Shader Code
void main(void)
{
vec4 a = gl_Vertex;
a.x = a.x * 0.5;
gl_Position = gl_ModelViewProjectionMatrix * a;
}
// Fragment Shader Code
void main (void)
{
gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
}
Example 1 – Loading Shader
unsigned long getFileLength(ifstream& file)
{
if(!file.good()) return 0; // The function returns true if
none of the stream's
// error flags
(eofbit, failbit and badbit) are set
unsigned long pos=file.tellg(); // Returns the absolute
position
// of the get pointer
file.seekg(0,ios::end); // Sets the position of the get
pointer
unsigned long len = file.tellg();
file.seekg(ios::beg);
return len;
}
int loadshader(char* filename, GLchar** ShaderSource, unsigned
long* len)
{
ifstream file;
file.open(filename, ios::in); // opens as ASCII!
if(!file) return -1;
len = getFileLength(file);
if (len==0) return -2;
// Error: Empty File
*ShaderSource = (GLubyte*) new char[len+1];
Example 1 – Loading Shader
}
if (*ShaderSource == 0) return -3; // can't reserve memory
*ShaderSource[len] = 0;
unsigned int i=0;
while (file.good())
{
*ShaderSource[i] = file.get();
// get character from file.
if (!file.eof())
i++;
}
*ShaderSource[i] = 0; // 0-terminate it at the correct position
file.close();
return 0; // No Error
int unloadshader(GLubyte** ShaderSource)
{
if (*ShaderSource != 0)
delete[] *ShaderSource;
*ShaderSource = 0;
}
Example 1 – Compiling,
Linking and Using Shader
GLuint vertexShader, fragmentShader, ProgramObject ;
vertexShaderObject = glCreateShader(GL_VERTEX_SHADER);
fragmentShaderObject = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSourceARB(vertexShaderObject, 1, &VertexShaderSource,
&vlength);
glShaderSourceARB(fragmentShaderObject, 1, &FragmentShaderSource,
&flength);
glCompileShaderARB(vertexShaderObject);
glCompileShaderARB(fragmentShaderObject);
ProgramObject = glCreateProgram();
glAttachShader(ProgramObject, vertexShaderObject);
glAttachShader(ProgramObject, fragmentShaderObject);
glLinkProgram(ProgramObject);
glUseProgram(ProgramObject);
Example 1 – OpenGL Context
void initializeGL ()
{
shader = SM.loadfromFile("vertexshader.txt","fragmentshader.txt"); // load,
compile, link from file
if (shader==0)
std::cout << "Error Loading, compiling or linking shader\n";
else
std::cout << "Success Loading, compiling or linking shader\n";
}
void paintGL()
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if(shader) shader->begin(); // equivalente a glUseProgram(ProgramObject);
glBegin (GL_TRIANGLES);
glVertex3f (0, 0, 0);
glVertex3f (0, 1, 0);
glVertex3f (1, 0, 0);
glEnd ();
if (shader) shader->end(); //equivalente a glUseProgram(0);
}
Example 1 – Graphic
Example 2 - Uniform Variables
- used to communicate with your vertex or fragment shader from "outside" ;
// Vertex Shader Code
// Fragment Shader Code
uniform float Scale;
uniform vec4 uColor;
void main(void)
{
vec4 a = gl_Vertex;
a.x = a.x * Scale;
a.y = a.y * Scale;
gl_Position = gl_ModelViewProjectionMatrix * a;
}
void main (void)
{
gl_FragColor = uColor;
}
// Changing the Uniform Value in C++
GLint loc = glGetUniformLocation(ProgramObject, "Scale");
if (loc != -1)
{
glUniform1f(loc, 0.432);
}
Example 2 - Uniform Variables
void MyOpenGLWidget::initializeGL ()
{
shader = SM.loadfromFile("vertexshader.txt","fragmentshader.txt"); // load (and
compile, link) from file
if (shader==0)
std::cout << "Error Loading, compiling or linking shader\n";
else
{
std::cout << "Success Loading, compiling or linking shader\n";
cont = 0;
ProgramObject = shader->GetProgramObject();
locScale = glGetUniformLocation(ProgramObject, "uScale");
locColor = glGetUniformLocation(ProgramObject, "uColor");
if (locScale == -1)
std::cout << "Warning: can't find uniform variable uScale!\n";
if (locColor == -1)
std::cout << "Warning: can't find uniform variable uColor!\n";
}
}
Example 2 - Uniform Variables
void MyOpenGLWidget::paintGL()
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if (shader)
{
shader->begin();
glUniform1f(locScale, cont/100.0f);
glUniform4f(locColor, cont/100.0f, 0.0, 0.0, 1.0);
cont = cont + 10;
}
glBegin (GL_TRIANGLES);
glVertex3f (0, 0, 0);
glVertex3f (0, 1, 0);
glVertex3f (1, 0, 0);
glEnd ();
if (shader) shader->end();
}
Example 2 - Graphic
Example 3 - Vertex Attributes
// Vertex Shader Code
void main(void)
{
// vec4 a = gl_Vertex + gl_Color;
//gl_Position = gl_ModelViewProjectionMatrix * a;
vec3 a = gl_Vertex.xyz + gl_Color.rgb;
gl_Position = gl_ModelViewProjectionMatrix * vec4(a, 1.0);
}
// Fragment Shader Code
void main (void)
{
gl_FragColor = vec4(0.0,0.0,1.0,1.0);
}
Example 3 - Graphic
Vertex Attributes
gl_Vertex
gl_Normal
gl_Color
gl_MultiTexCoord0
gl_MultiTexCoord1
gl_MultiTexCoord2
gl_MultiTexCoord3
gl_MultiTexCoord4
gl_MultiTexCoord5
gl_MultiTexCoord6
gl_MultiTexCoord7
gl_FogCoord
Position (vec4)
Normal (vec4)
Primary color of vertex (vec4)
Texture coordinate of texture
Texture coordinate of texture
Texture coordinate of texture
Texture coordinate of texture
Texture coordinate of texture
Texture coordinate of texture
Texture coordinate of texture
Texture coordinate of texture
Fog Coord (float)
unit
unit
unit
unit
unit
unit
unit
unit
0
1
2
3
4
5
6
7
(vec4)
(vec4)
(vec4)
(vec4)
(vec4)
(vec4)
(vec4)
(vec4)
Example 4 - Varying Variables
- Varying variables provide an interface between Vertex and
Fragment Shader
// Vertex Shader Source Code
varying vec4 vColor;
void main(void)
{
vColor = gl_Color;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
// Fragment Shader Code
varying vec4 vColor;
void main (void)
{
gl_FragColor = vColor;
}
Example 4 - Graphic
Texturing
-
image data
whatever
uniform variable
type:
sampler1D
sampler2D
sampler3D
samplerCube
sampler1DShadow
sampler2DShadow
sampler2DRect
1D texture
2D texture
3D texture
Cubemap texture
1D texture for shadow map
2D texture for shadow map
for rectanguar textures (width, height non
power of 2) (GL_ARB_texture_rectangle)
sampler2DRectShadow for shadow map, rectanguar textures (width,
height non power of 2)
(GL_ARB_texture_rectangle
Example 5 - Texturing
-
image data
whatever
uniform variable
type:
sampler1D
sampler2D
sampler3D
samplerCube
sampler1DShadow
sampler2DShadow
sampler2DRect
1D texture
2D texture
3D texture
Cubemap texture
1D texture for shadow map
2D texture for shadow map
for rectanguar textures (width, height non
power of 2) (GL_ARB_texture_rectangle)
sampler2DRectShadow for shadow map, rectanguar textures (width,
height non power of 2)
(GL_ARB_texture_rectangle
OpenGL Extensions
OpenGL Extensions are usually made available to access
new features of 3D graphics hardware:
- vendor-specific:
APPLE : Extension from Apple Computer, Inc.
ATI : Extension from ATI Technologies, Inc. (AMD)
HP : Extension from Hewlett Packard.
NV : Extension from NVIDIA Corporation
SGIS : Extension from Silicon Graphics, Inc.
-
multivendor:
EXT : Multivendor Extension
- ARB approved
Detecting Extension
OpenGL Extensions are usually made available to
access new features of 3D graphics hardware:
- vendor-specific:
APPLE : Extension from Apple Computer, Inc.
ATI : Extension from ATI Technologies, Inc. (AMD)
HP : Extension from Hewlett Packard.
NV : Extension from NVIDIA Corporation
SGIS : Extension from Silicon Graphics, Inc.
-
multivendor:
EXT : Multivendor Extension
- ARB approved