Skip to content

Commit

Permalink
A bunch more examples, focused in drawing and VBOs.
Browse files Browse the repository at this point in the history
  • Loading branch information
BSVino committed Aug 18, 2014
1 parent ac5c487 commit 94827f3
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 10 deletions.
28 changes: 28 additions & 0 deletions html/examples/vbo_draw_indexed.htm
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);
19 changes: 19 additions & 0 deletions html/examples/vbo_load.htm
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
20 changes: 20 additions & 0 deletions html/examples/vbo_load_index.htm
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

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);
15 changes: 15 additions & 0 deletions html/examples/vertexarray_draw_indexed.htm
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);
22 changes: 19 additions & 3 deletions opengl.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,25 @@
example_functions = {}

examples = {
'vertex_array': {
'description': 'Render a mesh with glDrawArrays using texture UV, normal, and color vertex attributes.',
'commands': [ 'glEnableVertexAttribArray', 'glVertexAttribPointer', 'glDrawArrays' ],
'vertexarray_draw': {
'description': 'Render a vertex array (not loaded into OpenGL) using texture UV, color, and normal vertex attributes.',
'commands': [ 'glDrawArrays' ],
},
'vertexarray_draw_indexed': {
'description': 'Render an indexed vertex array (not loaded into OpenGL) using texture UV and normal vertex attributes.',
'commands': [ 'glVertexAttribPointer', 'glDrawElements' ],
},
'vbo_load': {
'description': 'Load a vertex buffer into OpenGL for later rendering.',
'commands': [ 'glGenBuffers', 'glBindBuffer', 'glBufferData', 'glBufferSubData', 'glGetBufferParameteriv', 'glDeleteBuffers' ],
},
'vbo_load_index': {
'description': 'Load an index buffer into OpenGL for later rendering.',
'commands': [ 'glGenBuffers', 'glBindBuffer', 'glBufferData', 'glBufferSubData', 'glGetBufferParameteriv', 'glDeleteBuffers' ],
},
'vbo_draw_indexed': {
'description': 'Render an indexed buffer object using texture UV and normal vertex attributes.',
'commands': [ 'glBindBuffer', 'glEnableVertexAttribArray', 'glVertexAttribPointer', 'glDrawElements' ],
},
'shader_compile': {
'description': 'Compile a program from a vertex shader and a fragment shader.',
Expand Down

0 comments on commit 94827f3

Please sign in to comment.