grid.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00027 #include "renderstack/buffer.hpp"
00028 #include "renderstack/mesh.hpp"
00029 #include "renderstack/rs_gl.h"
00030 #include "renderstack/grid.hpp"
00031
00032 namespace renderstack {
00033
00034 grid::grid(float cell_width, int positive_cell_count)
00035 {
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 vertex_attribute position(vertex_attribute_usage_position, GL_FLOAT, 0, 3);
00048 vertex_buffer()->vertex_format().append(position);
00049
00050
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
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
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
00078 {
00079
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
00103 {
00104 index_buffer->set_index(vertex_index);
00105 index_buffer->line(2, 3);
00106 vertex_index += 2;
00107 }
00108 }
00109
00110
00111 {
00112
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
00137 {
00138 index_buffer->set_index(vertex_index);
00139 index_buffer->line(1, 3);
00140 vertex_index += 2;
00141 }
00142 }
00143
00144
00145 vertex_buffer->end_edit(index);
00146 index_buffer->end_edit(vertex_index);
00147 }
00148
00149 }
00150