Separator




duminică, 8 septembrie 2013

Programming with shaders - part 001 .

If you want to working with shaders then you must know something about GLSL data types.
The GLSL data types it's like variables or any types of data from programming language.
Uniform variables
Let's show you this types versus C data types:

bool -- int conditional get true -- or false. 
int -- int signed integer. 
float -- float single floating-point scalar. 
vec2 -- float[2] Two component floating-point vector. 
vect3 -- float[3] Three component floating-point vector. 
vec4 -- float[4] Four component floating-point vector. 
bvec2 -- int[2] Two component Boolean vector. 
bvec3 -- int[3] Three component Boolean vector. 
bvec4 -- int[4] Four component Boolean vector. 
ivec2 -- int[2] Two component signed integer vector. 
ivec3 -- int[3] Three component signed integer vector. 
ivec4 -- int[4] Four component signed integer vector. 
mat2 -- float[4] 2x2 floating-point matrix. 
mat3 float[9] 3x3 floating-point matrix. 
mat4 -- float[16] 4x4 floating-point matrix. 
sampler1D int for accessing a 1D texture. 
sampler2D -- int for accessing a 2D texture. 
sampler3D -- int for accessing a 3D texture. 
samplerCube -- int for accessing a cubemap texture. 
sampler1DShadow -- int for accessing a 1D depth texture. 
sampler2DShadow -- int for accessing a 2D depth texture..
This are the uniform variables and can be treated as constants... so cannot be changed.
The uniform variables are used when is passed from OpenGL app to the shader.
Vertex Attributes
This can only be used with vertex shaders.
The atributes are position, normals, texture coordinates all you can use with GL_ARB-vertex_program.
The GL_ARB-vertex_program holds slots and after overwritten the attribute is lost.
The attributes can be:

arrays, structures,
float,
vec[2],vec[3],vec[4],
mat[2],mat[3],mat[4]
Let's see one simple example
In OpenGL you used something like this:
...
glBegin(GL_TRIANGLES);
...
glVertex3f(0,1,0);
glNormal3f(1,0,1);
glVertex3f(0,1,0);
glNormal3f(1,0,0);
...
glEnd();
...
The link between shader and opengl source code using slot is:
...
int any_slot= 7;
...
glBindAttribLocationARB(my_program, any_slot, "my_attribute");
...
glBegin(GL_TRIANGLES);
...
glVertex3f(0,1,0);
glNormal3f(1,0,1);
glVertex3f(0,1,0);
glNormal3f(1,0,0);
...
glEnd();
...
this will pass the my_attribute to shader program (my_program) using a slot (any_slot).
To working ( access ) with the attribute in shader source code , just use this:
attribute vec3 my_attribute;
Can you see I sent with vec3 type because the glVertex3f has 3 float so is need to be three component floating-point vector.
Varying variables.
This are used most to send data from vertex shader to fragment shader.
The most common example is texture coordinates from vertex shader to fragment shader.
But about how it's vertex and fragment shader in the next tutorial.

Niciun comentariu:

Trimiteți un comentariu