OPENGL BUFFER FUNCTIONALITY Slide Set 3 02 void
OPENGL BUFFER FUNCTIONALITY Slide Set 3. 02
void gl. Create. Buffers( GLsizei n, GLuint *buffers); ■ Creates one or more buffers ■ n is the number of buffers to create ■ buffers is the address of an array of integers that will be filled with the names of the created buffers – If n is 1, then buffers can be the address of a simple int variable ■ Note that buffer names are simply integers, it is up to the programmer to keep straight what they represent 2
void gl. Bind. Buffer(GLenum target, GLuint buffer) ■ Some operations used in working with buffers require that the buffer be bound to a binding point – Parameter target specifies the binding point ■ The binding point used depends on the function of the buffer – We will probably only be using GL_ARRAY_BUFFER in this course ■ The buffer parameter is the name of the buffer to bind ■ To operate on a different buffer, we must bind that buffer and then operate on it ■ Programmers need to take care since some functions do not explicitly mention the buffer being operated on 3
void gl. Buffer. Storage(GLenum target, GLsizeiptr size, const void * data, GLbitfield flags) ■ This function allocates storage for a buffer and populates the buffer with data ■ target is the binding point which our buffer is bound to, not the name of the buffer (even though both are integers) ■ size is the number of bytes of data to put into the buffer ■ data is a pointer to the data ■ Notice the lack of data types: data is a pointer to an array of bytes ■ Open. GL expects that the data for the buffer will be laid out in order in memory with no gaps – An array of vec 4 will satisfy this condition since each vec 4 is stored as a sequence of four float values in memory – A std: : vector of vec 4 will also work since such a std: : vector object stores its content as an array of vec 4 4
GLint gl. Get. Attrib. Location(GLuint program, const GLchar *name) ■ For a buffer to be useful, we must associate it with an in variable in the vertex shader – Such a variable is called as an attribute ■ This function gets the location of an attribute (in variable) in the vertex shader in the currently active program ■ program is the value that represents the program in Open. Gl functions ■ name is a string (char *) that contains the name of the variable ■ Many points of care needed here – The program reference is just an integer, just like the attribute location being returned – There’s a good chance all these various names and references are values like 1 and 2. There’s nothing in the value to indicate what it is being used for – The name of the variable must be spelled correctly to match the vertex shader 5
void gl. Vertex. Attrib. Pointer(GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void * pointer); ■ Associates the currently bound array buffer to an attribute (in variable) in the vertex shader ■ The function also describes the structure of the data as an array of arrays of a basic type – Similar to describing it as an ‘array of vec 4’ – A parameter in the function call specifies the basic type of the elements – A parameter in the function call specify the number of these basic values in each component array – For example, ‘array of vec 4’ would have a basic type of float and 4 values per component array 6
gl. Vertex. Attrib. Pointer parameters ■ index is the index or name of the variable, as returned by gl. Get. Attrib. Location. ■ size is the number of basic values per component array ■ type is a symbolic constant that specifies the basic type of data ■ normalized specifies if the basic values in the array should be divided by the max possible before use in the vertex shader – For example, if colors are represented by 4 -tuples of integers in the range 0 to 255 then normalization would convert those to floats in the range from 0 to 1. 0. – For an array of vec 4 components, this parameter would be false – The value of the parameter should be one of the symbolic constants GL_TRUE or GL_FALSE rather than the boolean values ■ stride specifies that the actual data does not fill the buffer consecutively but with gaps between component arrays. – stride specifies the number of bytes between the beginning of consecutive component arrays – A value of 0 indicates that the component arrays are packed with no space in between ■ pointer is the offset to the first component array in the buffer 7
Take Care! ■ If the data put into the buffer using gl. Buffer. Storage is an array of vec 4, but a call to gl. Vertex. Attrib. Pointer describes it as an array of triples of unsigned bytes (single bytes interpreted as integers in the range 0 to 255 inclusive), the system will interpret the data the way gl. Vertex. Attrib. Pointer describes. 8
void gl. Enable. Vertex. Attrib. Array(GLuint index) ■ Instructs Open. GL to use the buffer ■ index is the index or name of the variable, as returned by gl. Get. Attrib. Location. 9
- Slides: 9