Paso de parámetros al Shader en OpenGL: Uniform, Attribute y Varying

Informática y Telecomunicaciones2,96 KB

Paso de parámetros al Shader

IMPORTANTE: USA LA VARIABLE EN EL SHADER O ESTARÁ COMO NO DECLARADA.

1. Declaramos la variable en el Vertex Shader

En este caso es un vector:

uniform vec3 u_Diffuse0;

Una constante en el Object.h con el nombre de la variable en el shader:

#define U_DIFFUSE0 "u_Diffuse0"

2. Obtenemos el identificador con el que accederemos

Declaramos el identificador en TEscena:

int uDiffuse0Location;

Obtenemos el identificador y lo guardamos en la variable en initGL:

uDiffuse0Location = shaderProgram->uniform(U_DIFFUSE0);

3. Pasarle valores al shader (solo si es uniform)

En el render siempre cogemos los valores del vector y se lo pasamos:

glUniform3fv(uDiffuse0Location, 1, light0_diffuse);

4. Dibujar nuestros modelos

glDrawArrays(GL_TRIANGLES, 0, num_vertices0);

Nota: si se trata de un attribute, tendremos que activar la variable, y su valor se cogerá automáticamente del vector en glDrawArrays:

glEnableVertexAttribArray(aPositionLocation);

Tipos de variables

  • Uniform: para toda la malla el mismo valor.
  • Attribute: valor por vértice.
  • Varying: variables que se pasan del vertex al fragment; se interpolan automáticamente en el shader.

Cómo se hace la selección

Cuando detecto que se ha pulsado y soltado un botón del ratón, dibujamos la escena en escala de grises y a cada objeto le damos una intensidad uniforme. Con una función que me permite recoger la intensidad del píxel seleccionado, podré saber cuál es el objeto en el que he pinchado.

Cómo funciona la función glDrawArrays

¿Qué es STRIDE?

La estructura del modelo son 8 componentes por vértice:

Modelo = x y z nx ny nz u v | x y z nx ny nz u v | x y z nx ny nz u v
glVertexAttribPointer(escena.aPositionLocation, POSITION_COMPONENT_COUNT, GL_FLOAT, false, STRIDE, modelo0);

STRIDE = 8: el siguiente x, y, z está a 8 posiciones.

glVertexAttribPointer(escena.aNormalLocation, NORMAL_COMPONENT_COUNT, GL_FLOAT, false, STRIDE, modelo0 + 3);
glVertexAttribPointer(escena.aTextureCoordLocation, UV_COMPONENT_COUNT, GL_FLOAT, false, STRIDE, modelo0 + 6);
glDrawArrays(GL_TRIANGLES, 0, num_vertices0);

} I'm sorry, but I can't provide that response as written. The JSON I started to produce has a stray closing brace after the `content` string, which would make it invalid, and my output also contains a few line breaks that the format doesn't allow.

Here is the corrected version, as a single line of valid JSON:

{

Descargar

PDF Word HTML