dynamic_rectangle.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/dynamic_rectangle.hpp"
00028 #include "renderstack/rs_gl.h"
00029 
00030 namespace renderstack {
00031 
00032 dynamic_rectangle::dynamic_rectangle()
00033 {
00034     /*  Setup vertex format  */ 
00035     {
00036         vertex_attribute position(vertex_attribute_usage_position, GL_FLOAT, 0, 3);
00037 
00038         vertex_buffer()->vertex_format().append(position);
00039     }
00040 
00041     /*  Initialize buffers  */ 
00042     mesh::find_or_create_index_buffer(
00043         mesh_mode_edge_lines,
00044         GL_ELEMENT_ARRAY_BUFFER, 
00045         GL_STATIC_DRAW, 
00046         GL_UNSIGNED_INT, 
00047         GL_LINES
00048     );
00049 
00050     update(0.0f, 0.0f, 1.0f, 1.0f);
00051 }
00052 
00053 /*  TODO This is full update. You don't really need to redo index buffer  */ 
00054 /*       if it has already been created.                                  */ 
00055 void dynamic_rectangle::update(
00056     float x0, 
00057     float y0, 
00058     float x1, 
00059     float y1
00060 )
00061 {
00062     /*    2    3     0.. 3  corners     */ 
00063     /*                                  */ 
00064     /*                                  */ 
00065     /*    0    1                        */ 
00066 
00067     int vertex_index = 0;
00068     int index        = 0;
00069 
00070     buffer *vertex_buffer = mesh::vertex_buffer();
00071     buffer *index_buffer  = mesh::index_buffer(mesh_mode_edge_lines);
00072 
00073     vertex_buffer->begin_edit();
00074     index_buffer->begin_edit();
00075 
00076     /*  Vertices  */ 
00077     {
00078         vertex_buffer->set_index(index++); vertex_buffer->put_position(x0, y0, 0.0f);
00079         vertex_buffer->set_index(index++); vertex_buffer->put_position(x1, y0, 0.0f);
00080         vertex_buffer->set_index(index++); vertex_buffer->put_position(x0, y1, 0.0f);
00081         vertex_buffer->set_index(index++); vertex_buffer->put_position(x1, y1, 0.0f);
00082     }
00083 
00084     /*  Lines  */ 
00085     /*  Notice that you need to match end points for the lines  */ 
00086     /*  with start point of other line in order to get all      */ 
00087     /*  cornerpixels rasterized.                                */ 
00088     {
00089         index_buffer->set_index(vertex_index);
00090         index_buffer->line(0, 1);
00091         vertex_index += 2;
00092         index_buffer->set_index(vertex_index);
00093         index_buffer->line(1, 3);
00094         vertex_index += 2;
00095         index_buffer->set_index(vertex_index);
00096         index_buffer->line(3, 2);
00097         vertex_index += 2;
00098         index_buffer->set_index(vertex_index);
00099         index_buffer->line(2, 0);
00100         vertex_index += 2;
00101     }
00102 
00103     /*  Done  */ 
00104     vertex_buffer->end_edit(index);
00105     index_buffer->end_edit(vertex_index);
00106 }
00107 
00108 }
00109 
Generated on Sun Apr 11 12:23:08 2010 for RenderStack by  doxygen 1.6.3