grid.cpp

Go to the documentation of this file.
00001 /*
00002     RenderStack  Support library for OpenGL 3+
00003     Copyright (C) 2010  Timo Suoranta
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Lesser General Public
00007     License as published by the Free Software Foundation; either
00008     version 2.1 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Lesser General Public License for more details.
00014 
00015     You should have received a copy of the GNU Lesser General Public
00016     License along with this library; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
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     /*    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 }
00148 
00149 }
00150 
Generated on Sun Apr 11 12:23:08 2010 for RenderStack by  doxygen 1.6.3