Subversion Repositories DIN Is Noise

Rev

Rev 17 | Rev 764 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
* mesh.cc
* DIN Is Noise is copyright (c) 2006-2017 Jagannathan Sampath
* For more information, please visit http://dinisnoise.org/
*/


#include "mesh.h"
#include "drone.h"
#include "dingl.h"
#include <fstream>
using namespace std;

extern ofstream dlog;

mesh::mesh () {
  num_polys = 0;
  r = g = b = 1;
}

void mesh::destroy () {
  if (gl_pts) delete[] gl_pts;
}

void mesh::add_poly (drone* d0, drone* d1, drone* d2, drone* d3) {
  ++num_polys;
  polys.push_back (poly (d0, d1, d2, d3));
  int np = num_polys * 4;
  if (np > n_glpts) {
    if (gl_pts) delete[] gl_pts;
    gl_pts = new int [2 * np];
    n_glpts = np;
  }
  r = d0->r;
  g = d0->g;
  b = d0->b;
}

void mesh::remove_poly (drone* pd) {
  for (poly_iterator i = polys.begin (); i != polys.end ();) {
    poly& pi = *i;
    int erased = 0;
    for (int k = 0; k < 4; ++k) {
      if (pi.drones[k] == pd) {
        i = polys.erase (i);
        erased = 1;
        --num_polys;
        break;
      }
    }
    if (!erased) ++i;
  }
}

void mesh::draw () {
  // draw drone mesh
  //
  int m = 0, n = 0;
  // run thru polygons of the mesh
  for (poly_iterator i = polys.begin (), j = polys.end (); i != j; ++i) {
    poly& p = *i;
    for (int j = 0; j < 4; ++j) { // each polygon contains 4 drones
      drone* pd = p.drones[j];

      // add drone position to draw below
      gl_pts[m++]= pd->x;
      gl_pts[m++]= pd->y;
      ++n;
    }
  }
  // draw the polygons with few opengl calls
  glColor4f (r, g, b, 0.6f);
  glVertexPointer (2, GL_INT, 0, gl_pts);
  glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
  glDrawArrays (GL_QUADS, 0, n); // draw filled polygons
  glColor4f (1, 1, 1, 1);
  glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
  glDrawArrays (GL_QUADS, 0, n); // draw polygon outlines
  glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
}