basic grid mesh (low level)
#include <grid.hpp>
Inherits renderstack::mesh.
Public Member Functions | |
grid (float cell_width, int positive_cell_count) |
Definition at line 36 of file grid.hpp.
renderstack::grid::grid | ( | float | cell_width, | |
int | positive_cell_count | |||
) |
Definition at line 34 of file grid.cpp.
00035 { 00036 /* 2 9 11 13 3 0.. 3 corners */ 00037 /* 2.. 7 min_x...max_x lines */ 00038 /* 6 . . . 7 8..13 min_z..,max_z lines */ 00039 /* */ 00040 /* 4 . x . 5 positive_cell_count = 2 */ 00041 /* */ 00042 /* 2 . . . 3 */ 00043 /* */ 00044 /* 0 8 10 12 1 */ 00045 00046 /* Setup vertex format */ 00047 vertex_attribute position(vertex_attribute_usage_position, GL_FLOAT, 0, 3); 00048 vertex_buffer()->vertex_format().append(position); 00049 00050 /* Initialize buffers */ 00051 buffer *vertex_buffer = mesh::vertex_buffer(); 00052 buffer *index_buffer = mesh::find_or_create_index_buffer( 00053 mesh_mode_edge_lines, 00054 GL_ELEMENT_ARRAY_BUFFER, 00055 GL_STATIC_DRAW, 00056 GL_UNSIGNED_INT, 00057 GL_LINES 00058 ); 00059 00060 /* Prepare writing to buffers */ 00061 int vertex_index = 0; 00062 int index = 0; 00063 00064 vertex_buffer->begin_edit(); 00065 index_buffer->begin_edit(); 00066 00067 float extreme = (float)(positive_cell_count) * cell_width; 00068 00069 /* Corner vertices */ 00070 { 00071 vertex_buffer->set_index(index++); vertex_buffer->put_position(-extreme, -extreme, 0.0f); 00072 vertex_buffer->set_index(index++); vertex_buffer->put_position( extreme, -extreme, 0.0f); 00073 vertex_buffer->set_index(index++); vertex_buffer->put_position(-extreme, extreme, 0.0f); 00074 vertex_buffer->set_index(index++); vertex_buffer->put_position( extreme, extreme, 0.0f); 00075 } 00076 00077 /* min x...max x vertices */ 00078 { 00079 /* min z : min_x...max_x */ 00080 { 00081 index_buffer->set_index(vertex_index); 00082 index_buffer->line(0, 1); 00083 vertex_index += 2; 00084 } 00085 { 00086 int z; 00087 int range = (positive_cell_count - 1); 00088 00089 for(z = -range; z <= range; ++z) 00090 { 00091 float z_position = (float)(z) * cell_width; 00092 int v0 = index++; 00093 int v1 = index++; 00094 vertex_buffer->set_index(v0); vertex_buffer->put_position(-extreme, z_position, 0.0f); 00095 vertex_buffer->set_index(v1); vertex_buffer->put_position( extreme, z_position, 0.0f); 00096 index_buffer->set_index(vertex_index); 00097 index_buffer->line(v0, v1); 00098 vertex_index += 2; 00099 } 00100 } 00101 00102 /* max z : min_x...max_x */ 00103 { 00104 index_buffer->set_index(vertex_index); 00105 index_buffer->line(2, 3); 00106 vertex_index += 2; 00107 } 00108 } 00109 00110 /* min z...max z vertices */ 00111 { 00112 /* min x : min_z...max_z */ 00113 { 00114 index_buffer->set_index(vertex_index); 00115 index_buffer->line(0, 2); 00116 vertex_index += 2; 00117 } 00118 00119 { 00120 int x; 00121 int range = (positive_cell_count - 1); 00122 00123 for(x = -range; x <= range; ++x) 00124 { 00125 float x_position = (float)(x) * cell_width; 00126 int v0 = index++; 00127 int v1 = index++; 00128 vertex_buffer->set_index(v0); vertex_buffer->put_position(x_position, -extreme, 0.0f); 00129 vertex_buffer->set_index(v1); vertex_buffer->put_position(x_position, extreme, 0.0f); 00130 index_buffer->set_index(vertex_index); 00131 index_buffer->line(v0, v1); 00132 vertex_index += 2; 00133 } 00134 } 00135 00136 /* max x : min_z...max_z */ 00137 { 00138 index_buffer->set_index(vertex_index); 00139 index_buffer->line(1, 3); 00140 vertex_index += 2; 00141 } 00142 } 00143 00144 /* Done */ 00145 vertex_buffer->end_edit(index); 00146 index_buffer->end_edit(vertex_index); 00147 }