| 1 | // | 
|---|
| 2 | // polygons.cc | 
|---|
| 3 | // | 
|---|
| 4 | // Copyright (C) 1996 Limit Point Systems, Inc. | 
|---|
| 5 | // | 
|---|
| 6 | // Author: Curtis Janssen <cljanss@limitpt.com> | 
|---|
| 7 | // Maintainer: LPS | 
|---|
| 8 | // | 
|---|
| 9 | // This file is part of the SC Toolkit. | 
|---|
| 10 | // | 
|---|
| 11 | // The SC Toolkit is free software; you can redistribute it and/or modify | 
|---|
| 12 | // it under the terms of the GNU Library General Public License as published by | 
|---|
| 13 | // the Free Software Foundation; either version 2, or (at your option) | 
|---|
| 14 | // any later version. | 
|---|
| 15 | // | 
|---|
| 16 | // The SC Toolkit is distributed in the hope that it will be useful, | 
|---|
| 17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 19 | // GNU Library General Public License for more details. | 
|---|
| 20 | // | 
|---|
| 21 | // You should have received a copy of the GNU Library General Public License | 
|---|
| 22 | // along with the SC Toolkit; see the file COPYING.LIB.  If not, write to | 
|---|
| 23 | // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | 
|---|
| 24 | // | 
|---|
| 25 | // The U.S. Government is granted a limited license as per AL 91-7. | 
|---|
| 26 | // | 
|---|
| 27 |  | 
|---|
| 28 | #include <stdlib.h> | 
|---|
| 29 |  | 
|---|
| 30 | #include <util/render/render.h> | 
|---|
| 31 | #include <util/render/object.h> | 
|---|
| 32 | #include <util/render/polygons.h> | 
|---|
| 33 | #include <util/render/color.h> | 
|---|
| 34 |  | 
|---|
| 35 | using namespace sc; | 
|---|
| 36 |  | 
|---|
| 37 | static ClassDesc RenderedPolygons_cd( | 
|---|
| 38 | typeid(RenderedPolygons),"RenderedPolygons",1,"public RenderedObject", | 
|---|
| 39 | 0, create<RenderedPolygons>, 0); | 
|---|
| 40 |  | 
|---|
| 41 | RenderedPolygons::RenderedPolygons() | 
|---|
| 42 | { | 
|---|
| 43 | nvertex_ = 0; | 
|---|
| 44 | nface_ = 0; | 
|---|
| 45 | vertices_ = 0; | 
|---|
| 46 | vertex_rgb_ = 0; | 
|---|
| 47 | faces_ = 0; | 
|---|
| 48 | nvertex_in_face_ = 0; | 
|---|
| 49 | } | 
|---|
| 50 |  | 
|---|
| 51 | RenderedPolygons::RenderedPolygons(const Ref<KeyVal>& keyval): | 
|---|
| 52 | RenderedObject(keyval) | 
|---|
| 53 | { | 
|---|
| 54 | int nvertex = keyval->count("vertices"); | 
|---|
| 55 | int nface = keyval->count("faces"); | 
|---|
| 56 | Coloring coloring = None; | 
|---|
| 57 | if (keyval->count("vertex_color_list")) { | 
|---|
| 58 | coloring = Vertex; | 
|---|
| 59 | } | 
|---|
| 60 | initialize(nvertex, nface, coloring); | 
|---|
| 61 |  | 
|---|
| 62 | int i; | 
|---|
| 63 | for (i=0; i<nvertex; i++) { | 
|---|
| 64 | set_vertex(i, | 
|---|
| 65 | keyval->doublevalue("vertices", i, 0), | 
|---|
| 66 | keyval->doublevalue("vertices", i, 1), | 
|---|
| 67 | keyval->doublevalue("vertices", i, 2)); | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 | if (coloring == Vertex) { | 
|---|
| 71 | for (i=0; i<nvertex; i++) { | 
|---|
| 72 | set_vertex_rgb(i, | 
|---|
| 73 | keyval->doublevalue("vertex_color_list", i, 0), | 
|---|
| 74 | keyval->doublevalue("vertex_color_list", i, 1), | 
|---|
| 75 | keyval->doublevalue("vertex_color_list", i, 2)); | 
|---|
| 76 | } | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | for (i=0; i<nface; i++) { | 
|---|
| 80 | nvertex_in_face_[i] = keyval->count("faces", i); | 
|---|
| 81 | faces_[i] = new int[nvertex_in_face_[i]]; | 
|---|
| 82 | for (int j=0; j<nvertex_in_face_[i]; j++) { | 
|---|
| 83 | faces_[i][j] = keyval->intvalue("faces", i, j); | 
|---|
| 84 | } | 
|---|
| 85 | } | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | RenderedPolygons::~RenderedPolygons() | 
|---|
| 89 | { | 
|---|
| 90 | if (vertices_ && vertices_[0]) delete[] vertices_[0]; | 
|---|
| 91 | if (vertices_) delete[] vertices_; | 
|---|
| 92 | if (vertex_rgb_ && vertex_rgb_[0]) delete[] vertex_rgb_[0]; | 
|---|
| 93 | if (vertex_rgb_) delete[] vertex_rgb_; | 
|---|
| 94 | if (faces_) { | 
|---|
| 95 | for (int i=0; i<nface_; i++) { | 
|---|
| 96 | if (faces_[i]) delete[] faces_[i]; | 
|---|
| 97 | } | 
|---|
| 98 | delete[] faces_; | 
|---|
| 99 | } | 
|---|
| 100 | if (nvertex_in_face_) { | 
|---|
| 101 | delete[] nvertex_in_face_; | 
|---|
| 102 | } | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | void | 
|---|
| 106 | RenderedPolygons::render(const Ref<Render>& render) | 
|---|
| 107 | { | 
|---|
| 108 | render->polygons(this); | 
|---|
| 109 | } | 
|---|
| 110 |  | 
|---|
| 111 | void | 
|---|
| 112 | RenderedPolygons::initialize(int nvertex, int nface, | 
|---|
| 113 | RenderedPolygons::Coloring coloring) | 
|---|
| 114 | { | 
|---|
| 115 | coloring_ = coloring; | 
|---|
| 116 | nvertex_ = nvertex; | 
|---|
| 117 | nface_ = nface; | 
|---|
| 118 |  | 
|---|
| 119 | vertices_ = new double*[nvertex]; | 
|---|
| 120 | double* tmp = vertices_[0] = new double[3*nvertex]; | 
|---|
| 121 | int i; | 
|---|
| 122 | for (i=1; i<nvertex; i++) { | 
|---|
| 123 | tmp += 3; | 
|---|
| 124 | vertices_[i] = tmp; | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | if (coloring == Vertex) { | 
|---|
| 128 | vertex_rgb_ = new double*[nvertex]; | 
|---|
| 129 | double*tmp = vertex_rgb_[0] = new double[3*nvertex]; | 
|---|
| 130 | for (i=1; i<nvertex; i++) { | 
|---|
| 131 | tmp += 3; | 
|---|
| 132 | vertex_rgb_[i] = tmp; | 
|---|
| 133 | } | 
|---|
| 134 | } | 
|---|
| 135 | else { | 
|---|
| 136 | vertex_rgb_ = 0; | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | faces_ = new int*[nface]; | 
|---|
| 140 | nvertex_in_face_ = new int[nface]; | 
|---|
| 141 | for (i=0; i<nface; i++) { | 
|---|
| 142 | faces_[i] = 0; | 
|---|
| 143 | nvertex_in_face_[i] = 0; | 
|---|
| 144 | } | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 | void | 
|---|
| 148 | RenderedPolygons::set_vertex(int i, double x, double y, double z) | 
|---|
| 149 | { | 
|---|
| 150 | vertices_[i][0] = x; | 
|---|
| 151 | vertices_[i][1] = y; | 
|---|
| 152 | vertices_[i][2] = z; | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 | void | 
|---|
| 156 | RenderedPolygons::set_vertex_rgb(int i, double r, double g, double b) | 
|---|
| 157 | { | 
|---|
| 158 | vertex_rgb_[i][0] = r; | 
|---|
| 159 | vertex_rgb_[i][1] = g; | 
|---|
| 160 | vertex_rgb_[i][2] = b; | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | void | 
|---|
| 164 | RenderedPolygons::set_face(int iface, int v1, int v2, int v3) | 
|---|
| 165 | { | 
|---|
| 166 | if (faces_[iface]) delete[] faces_[iface]; | 
|---|
| 167 | faces_[iface] = new int[3]; | 
|---|
| 168 | faces_[iface][0] = v1; | 
|---|
| 169 | faces_[iface][1] = v2; | 
|---|
| 170 | faces_[iface][2] = v3; | 
|---|
| 171 |  | 
|---|
| 172 | nvertex_in_face_[iface] = 3; | 
|---|
| 173 | } | 
|---|
| 174 |  | 
|---|
| 175 | void | 
|---|
| 176 | RenderedPolygons::set_face(int iface, int v1, int v2, int v3, int v4) | 
|---|
| 177 | { | 
|---|
| 178 | if (faces_[iface]) delete[] faces_[iface]; | 
|---|
| 179 | faces_[iface] = new int[4]; | 
|---|
| 180 | faces_[iface][0] = v1; | 
|---|
| 181 | faces_[iface][1] = v2; | 
|---|
| 182 | faces_[iface][2] = v3; | 
|---|
| 183 | faces_[iface][3] = v4; | 
|---|
| 184 |  | 
|---|
| 185 | nvertex_in_face_[iface] = 4; | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|
| 188 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 189 |  | 
|---|
| 190 | // Local Variables: | 
|---|
| 191 | // mode: c++ | 
|---|
| 192 | // c-file-style: "CLJ" | 
|---|
| 193 | // End: | 
|---|