¿Cómo sabe glDrawArrays qué dibujar?


Estoy siguiendo algunos tutoriales de OpenGL principiantes, y estoy un poco confundido acerca de este fragmento de código:

glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject); //Bind GL_ARRAY_BUFFER to our handle
glEnableVertexAttribArray(0); //?
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); //Information about the array, 3 points for each vertex, using the float type, don't normalize, no stepping, and an offset of 0. I don't know what the first parameter does however, and how does this function know which array to deal with (does it always assume we're talking about GL_ARRAY_BUFFER?

glDrawArrays(GL_POINTS, 0, 1); //Draw the vertices, once again how does this know which vertices to draw? (Does it always use the ones in GL_ARRAY_BUFFER)

glDisableVertexAttribArray(0); //?
glBindBuffer(GL_ARRAY_BUFFER, 0); //Unbind

No entiendo cómo glDrawArrays sabe qué vértices dibujar, y qué es todo lo que tiene que ver con glEnableVertexAttribArray. ¿Podría alguien arrojar algo de luz sobre la situación?

Author: Devid Farinelli, 2013-09-30

2 answers

La llamada a glBindBuffer le dice a OpenGL que use vertexBufferObject siempre que necesite el GL_ARRAY_BUFFER.

glEnableVertexAttribArray significa que desea que OpenGL use matrices de atributos de vértices; sin esta llamada, los datos que suministró serán ignorados.

glVertexAttribPointer, como has dicho, le dice a OpenGL qué hacer con los datos de la matriz suministrados, ya que OpenGL no sabe inherentemente en qué formato estarán esos datos.

glDrawArrays utiliza todos los datos anteriores para dibujar puntos.

Recuerde que OpenGL es una gran máquina de estados. La mayoría de las llamadas las funciones de OpenGL modifican un estado global al que no se puede acceder directamente. Es por eso que el código termina con glDisableVertexAttribArray y glBindBuffer(..., 0): tienes que volver a poner ese estado global cuando hayas terminado de usarlo.

 37
Author: Steve Howard,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-09-30 19:33:24

DrawArrays toma datos de ARRAY_BUFFER.

Los datos se 'mapean' de acuerdo con su configuración en glVertexAttribPointer que dice cuál es la definición de su vértice.

En tu ejemplo tienes un vértice attrib (glEnableVertexAttribArray) en la posición 0 (normalmente puedes tener 16 vértices attribs, cada uno con 4 flotadores). Luego le dices que cada atributo se obtendrá leyendo 3 GL_FLOATS del búfer a partir de la posición 0.

Gran tutorial aquí: http://www.arcsynthesis.org/gltut/Basics/Tut02%20Vertex%20Attributes.html

 3
Author: fen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-09-30 19:36:27