forked from BSVino/docs.gl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A bunch more examples, focused in drawing and VBOs.
- Loading branch information
Showing
6 changed files
with
107 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// This definition is needed for type safety. | ||
#define BUFFER_OFFSET(i) ((char *)NULL + (i)) | ||
|
||
{%glBindBuffer}(GL_ARRAY_BUFFER, vertex_buffer); // vertex_buffer is retrieved from {%glGenBuffers} | ||
{%glBindBuffer}(GL_ELEMENT_ARRAY_BUFFER, index_buffer); // index_buffer is retrieved from {%glGenBuffers} | ||
|
||
{%glEnableVertexAttribArray}(texcoord_attrib_index); // Attribute indexes were received from calls to {%glGetAttribLocation}, or passed into {%glBindAttribLocation}. | ||
{%glEnableVertexAttribArray}(normal_attrib_index); | ||
{%glEnableVertexAttribArray}(position_attrib_index); | ||
|
||
// vertex_stride is the size of bytes of each vertex in the buffer object | ||
// vertex_position_offset and kin are the offset in bytes of the position data | ||
// in each vertex. For example if your vertex structure is | ||
// [ position, texcoord, normal ] then position vertex_position_offset will | ||
// have offset 0, vertex_texcoord_offset is 12 (position is 3 * sizeof(float) | ||
// bytes large, and texcoord comes just after) and vertex_normal_offset is | ||
// 20 = 5 * sizeof(float). | ||
{%glVertexAttribPointer}(texcoord_attrib_index, 2, GL_FLOAT, false, vertex_stride, BUFFER_OFFSET(vertex_texcoord_offset)); | ||
{%glVertexAttribPointer}(normal_attrib_index, 3, GL_FLOAT, false, vertex_stride, BUFFER_OFFSET(vertex_normal_offset)); | ||
{%glVertexAttribPointer}(position_attrib_index, 3, GL_FLOAT, false, vertex_stride, BUFFER_OFFSET(vertex_position_offset)); | ||
|
||
// num_vertices is the number of verts in your vertex_data. | ||
// index_data is an array of unsigned int offsets into vertex_data. | ||
{%glDrawElements}(GL_TRIANGLES, num_vertices, GL_UNSIGNED_INT, NULL); | ||
|
||
{%glDisableVertexAttribArray}(position_attrib_index); | ||
{%glDisableVertexAttribArray}(texcoord_attrib_index); | ||
{%glDisableVertexAttribArray}(normal_attrib_index); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// data_size_in_bytes is the size in bytes of your vertex data. | ||
// data_vertices is your actual vertex data, probably a huge array of floats | ||
|
||
GLuint vertex_buffer; // Save this for later rendering | ||
{%glGenBuffers}(1, &vertex_buffer); | ||
{%glBindBuffer}(GL_ARRAY_BUFFER, vertex_buffer); | ||
{%glBufferData}(GL_ARRAY_BUFFER, data_size_in_bytes, 0, GL_STATIC_DRAW); | ||
{%glBufferSubData}(GL_ARRAY_BUFFER, 0, data_size_in_bytes, data_vertices); | ||
|
||
GLint size = 0; | ||
{%glGetBufferParameteriv}(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &size); | ||
if(data_size_in_bytes != size) | ||
{ | ||
{%glDeleteBuffers}(1, &vertex_buffer); | ||
// Log the error | ||
return; | ||
} | ||
|
||
// Success |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// data_size_in_bytes is the size in bytes of your vertex data. | ||
// data_indices is an array of integer offsets into your vertex data. | ||
|
||
GLuint index_buffer; // Save this for later rendering | ||
{%glGenBuffers}(1, &index_buffer); | ||
{%glBindBuffer}(GL_ELEMENT_ARRAY_BUFFER, index_buffer); | ||
{%glBufferData}(GL_ELEMENT_ARRAY_BUFFER, data_size_in_bytes, 0, GL_STATIC_DRAW); | ||
{%glBufferSubData}(GL_ELEMENT_ARRAY_BUFFER, 0, data_size_in_bytes, data_indices); | ||
|
||
GLint size = 0; | ||
{%glGetBufferParameteriv}(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size); | ||
if(data_size_in_bytes != size) | ||
{ | ||
{%glDeleteBuffers}(1, &index_buffer); | ||
// Log the error | ||
return; | ||
} | ||
|
||
// Success | ||
|
13 changes: 6 additions & 7 deletions
13
html/examples/vertex_array.htm → html/examples/vertexarray_draw.htm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,16 @@ | ||
{%glEnableVertexAttribArray}(texcoord_attrib_index); // Attribute indexes were received from calls to {%glGetAttribLocation}, or passed into {%glBindAttribLocation}. | ||
{%glVertexAttribPointer}(texcoord_attrib_index, 2, GL_FLOAT, false, 0, texcoords_data); // texcoords_data is a float*, 2 per vertex, representing UV coordinates. | ||
|
||
{%glEnableVertexAttribArray}(normal_attrib_index); | ||
{%glVertexAttribPointer}(normal_attrib_index, 3, GL_FLOAT, false, 0, normals_data); // normals_data is a float*, 3 per vertex, representing normal vectors. | ||
|
||
{%glEnableVertexAttribArray}(color_attrib_index); | ||
{%glVertexAttribPointer}(color_attrib_index, 3, GL_UNSIGNED_BYTE, true, sizeof(unsigned char)*3, colors_data); // colors_data is a unsigned char*, 3 per vertex, representing the color of each vertex. | ||
|
||
{%glEnableVertexAttribArray}(position_attrib_index); | ||
|
||
{%glVertexAttribPointer}(texcoord_attrib_index, 2, GL_FLOAT, false, 0, texcoords_data); // texcoords_data is a float*, 2 per vertex, representing UV coordinates. | ||
{%glVertexAttribPointer}(normal_attrib_index, 3, GL_FLOAT, false, 0, normals_data); // normals_data is a float*, 3 per vertex, representing normal vectors. | ||
{%glVertexAttribPointer}(color_attrib_index, 3, GL_UNSIGNED_BYTE, true, sizeof(unsigned char)*3, colors_data); // colors_data is a unsigned char*, 3 per vertex, representing the color of each vertex. | ||
{%glVertexAttribPointer}(position_attrib_index, 3, GL_FLOAT, false, 0, vertex_data); // vertex_data is a float*, 3 per vertex, representing the position of each vertex | ||
|
||
{%glDrawArrays}(GL_TRIANGLES, 0, vertex_data); | ||
{%glDisableVertexAttribArray}(position_attrib_index); | ||
|
||
{%glDisableVertexAttribArray}(position_attrib_index); | ||
{%glDisableVertexAttribArray}(texcoord_attrib_index); | ||
{%glDisableVertexAttribArray}(normal_attrib_index); | ||
{%glDisableVertexAttribArray}(color_attrib_index); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{%glEnableVertexAttribArray}(texcoord_attrib_index); // Attribute indexes were received from calls to {%glGetAttribLocation}, or passed into {%glBindAttribLocation}. | ||
{%glEnableVertexAttribArray}(normal_attrib_index); | ||
{%glEnableVertexAttribArray}(position_attrib_index); | ||
|
||
{%glVertexAttribPointer}(texcoord_attrib_index, 2, GL_FLOAT, false, 0, texcoords_data); // texcoords_data is a float*, 2 per vertex, representing UV coordinates. | ||
{%glVertexAttribPointer}(normal_attrib_index, 3, GL_FLOAT, false, 0, normals_data); // normals_data is a float*, 3 per vertex, representing normal vectors. | ||
{%glVertexAttribPointer}(position_attrib_index, 3, GL_FLOAT, false, 0, vertex_data); // vertex_data is a float*, 3 per vertex, representing the position of each vertex | ||
|
||
// num_vertices is the number of verts in your vertex_data. | ||
// index_data is an array of unsigned int offsets into vertex_data. | ||
{%glDrawElements}(GL_TRIANGLES, num_vertices, GL_UNSIGNED_INT, index_data); | ||
|
||
{%glDisableVertexAttribArray}(position_attrib_index); | ||
{%glDisableVertexAttribArray}(texcoord_attrib_index); | ||
{%glDisableVertexAttribArray}(normal_attrib_index); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters