Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

C:/temp/src/j2k/Beta/Math/Vector/NotDone/jtriangle.hpp

Go to the documentation of this file.
00001 /*
00002 This class is intended to be a triangle which is used in opengl. Although 
00003 it may be possible to use it for some other purpose, there may be some 
00004 functions that don't work correctly, or at all.
00005 
00006 I am not sure how to go about the problem that different programs use 
00007 different vertexes. The best solution I see at the moment is to allow
00008 the creation of vertex classes that fit the following parameters.
00009 
00010 required:
00011    below members are accessable to the triangle class
00012 
00013    copy constructor
00014 
00015    pos of type vect3f
00016 
00017    function draw() which makes necessary commands to draw
00018    itself in opengl.
00019 
00020    function tform() which makes necessary commands to
00021    transform itself with <jmatrix.h>.
00022 
00023 optional:
00024    normal of type vect3f, to draw normals
00025 
00026 To use a specific type of vertex, you must use a typedef command
00027 like this. I am not sure if it is possible to have multiple vertex
00028 types in one program. If you can, make sure you dont mix triangle types:
00029 
00030    typedef nameOfVertexClass myVertexType;
00031 */
00032 
00033 #ifndef __triangle_h_
00034 #define __triangle_h_
00035 
00036 #include <list>
00037 #include <joe/vect3f.h>
00038 #include <joe/old/jmatrix.h>
00039 
00040 using namespace std;
00041 
00042 //////////////////////////////////////////////////////////////
00043 //*********************************************************///
00044 //////////////////////////////////////////////////////////////
00045 
00046 class simpleVertex
00047 {
00048 public:
00049    vect3f pos;
00050    
00051    simpleVertex(const simpleVertex& obj)
00052    {
00053       pos = obj.pos;
00054    }
00055 
00056    void draw() 
00057    {
00058       glVertex3fv(pos.v);
00059    }
00060 
00061    void tform()
00062    {
00063       mstack.tform(pos);
00064    }
00065 };
00066 
00067 //////////////////////////////////////////////////////////////
00068 //*********************************************************///
00069 //////////////////////////////////////////////////////////////
00070 
00071 class normalVertex
00072 {
00073 public:
00074    vect3f pos;
00075    vect3f normal;
00076    vect3f color;
00077    
00078    normalVertex() 
00079    {
00080       color(1,1,1);
00081    }
00082 
00083    normalVertex(const normalVertex& obj)
00084    {
00085       color = obj.color;
00086       normal = obj.normal;
00087       pos = obj.pos;
00088    }
00089 
00090 
00091 
00092    void draw() 
00093    {
00094       glColor3fv(color.v);
00095       glNormal3fv(normal.v);
00096       glVertex3fv(pos.v);
00097    }
00098 
00099    void tform()
00100    {
00101       normal += pos;
00102       mstack.tform(normal);
00103       mstack.tform(pos);
00104       normal -= pos;
00105    }
00106 };
00107 
00108 //////////////////////////////////////////////////////////////
00109 //*********************************************************///
00110 //////////////////////////////////////////////////////////////
00111 
00112 typedef normalVertex myVertexType;
00113 
00114 class triangle
00115 {
00116 public:
00117    // constructors
00118    triangle() {}
00119    triangle(const triangle& obj)
00120    {
00121       for (int i = 0; i < 3; i++){
00122          vert[i] = obj.vert[i];
00123          center = obj.center;
00124          normal = obj.normal;
00125       }
00126    }
00127    ~triangle() {}
00128 
00129    // variables
00130    vect3f normal;
00131    vect3f center;
00132 
00133    myVertexType vert[3];
00134 
00135    // functions
00136    void draw();
00137    void drawmesh();
00138    void drawnormals();
00139 
00140    void tform();
00141    void normalize();
00142    void syncnormals();
00143 
00144    float angle(vect3f&);
00145    float distance(vect3f&);
00146 };
00147 
00148 //---------------------------------------------------------//
00149 
00150 void triangle::draw()
00151 {
00152    myVertexType tempvert[3];
00153 
00154    glBegin(GL_TRIANGLES);
00155       for (int i = 0; i < 3; i++){
00156             tempvert[i] = vert[i];
00157             tempvert[i].tform();
00158             tempvert[i].draw();
00159       }
00160    glEnd();
00161 }
00162 
00163 //---------------------------------------------------------//
00164 
00165 void triangle::drawmesh()
00166 {
00167    myVertexType tempvert[3];
00168 
00169    glBegin(GL_LINE_LOOP);
00170       for (int i = 0; i < 3; i++){
00171             tempvert[i] = vert[i];
00172             tempvert[i].tform();
00173             tempvert[i].draw();
00174       }
00175    glEnd();
00176 }
00177 
00178 //---------------------------------------------------------//
00179 
00180 void triangle::drawnormals()
00181 {
00182    myVertexType tempvert[3];
00183 
00184    glBegin(GL_LINES);
00185 
00186       for (int i = 0; i < 3; i++){
00187             tempvert[i] = vert[i];
00188             tempvert[i].tform();
00189             tempvert[i].draw();
00190             tempvert[i].pos += tempvert[i].normal;
00191             tempvert[i].draw();
00192       }
00193 
00194       tempvert[0].pos = center;
00195       tempvert[0].normal = normal;
00196       tempvert[0].normal += tempvert[0].pos;
00197       mstack.tform(tempvert[0].pos);
00198       tempvert[0].draw();
00199       mstack.tform(tempvert[0].normal);
00200       tempvert[0].pos = tempvert[0].normal;
00201       tempvert[0].draw();
00202    glEnd();
00203 }
00204 
00205 //---------------------------------------------------------//
00206 
00207 void triangle::tform()
00208 {
00209    for (int i = 0; i < 3; i++)
00210       vert[i].tform();
00211 
00212    normal += center;
00213    mstack.tform(center);
00214    mstack.tform(normal);
00215    normal -= center;
00216 }
00217 
00218 //---------------------------------------------------------//
00219 
00220 void triangle::normalize()
00221 {
00222    vect3f edge1, edge2;
00223    edge1 = vert[1].pos - vert[0].pos;
00224    edge2 = vert[2].pos - vert[1].pos;
00225    normal = edge1 % edge2;
00226    normal.unit();
00227    center = (vert[0].pos + vert[1].pos + vert[2].pos) / 3;
00228 }
00229 
00230 //---------------------------------------------------------//
00231 
00232 void triangle::syncnormals()
00233 {
00234    for (int i = 0; i < 3; i++)
00235       vert[i].normal = normal;
00236 }
00237 
00238 //---------------------------------------------------------//
00239 
00240 float triangle::angle(vect3f& point)
00241 {
00242    vect3f vtop = point - center;
00243    return acos((normal * vtop) / (!normal * !vtop));
00244 }
00245 
00246 //---------------------------------------------------------//
00247 
00248 float triangle::distance(vect3f& point)
00249 {
00250    vect3f vtop = point - center;
00251    return normal.comp(vtop);
00252 
00253 }
00254 
00255 //////////////////////////////////////////////////////////////
00256 //*********************************************************///
00257 //////////////////////////////////////////////////////////////
00258 
00259 class model
00260 {
00261 public:
00262    // variables
00263    vector<triangle> triList;
00264    vector<triangle>::iterator iter;
00265 
00266    unsigned int texture;
00267 
00268    // functions
00269    void put(triangle);
00270    triangle get();
00271 
00272    void begin();
00273    void next();
00274    void prev();
00275    int size();
00276 
00277    void draw();
00278    void drawmesh();
00279    void drawnormals();
00280    void tform();
00281 };
00282 
00283 //---------------------------------------------------------//
00284 
00285 void model::put(triangle obj)
00286 {
00287    if (triList.empty()){
00288       triList.push_back(obj);
00289       iter = triList.begin();
00290    }
00291    else
00292       triList.push_back(obj);
00293 }
00294 
00295 //---------------------------------------------------------//
00296 
00297 triangle model::get()
00298 {
00299    return *iter;
00300 }
00301 
00302 //---------------------------------------------------------//
00303 
00304 void model::begin()
00305 {
00306    iter = triList.begin();
00307 }
00308 
00309 //---------------------------------------------------------//
00310 
00311 void model::next()
00312 {
00313    if (iter < triList.end() - 1)
00314       iter++;
00315 }
00316 
00317 //---------------------------------------------------------//
00318 
00319 void model::prev()
00320 {
00321    if (iter > triList.begin())
00322       iter--;
00323 }
00324 
00325 //---------------------------------------------------------//
00326 
00327 int model::size()
00328 {
00329    return triList.size();
00330 }
00331 
00332 //---------------------------------------------------------//
00333 
00334 void model::draw()
00335 {
00336    for (vector<triangle>::iterator i = triList.begin(); i < triList.end(); i++)
00337       (*i).draw();
00338 }
00339 
00340 //---------------------------------------------------------//
00341 
00342 void model::drawmesh()
00343 {
00344    for (vector<triangle>::iterator i = triList.begin(); i < triList.end(); i++)
00345       (*i).drawmesh();
00346 }
00347 
00348 //---------------------------------------------------------//
00349 
00350 void model::drawnormals()
00351 {
00352    for (vector<triangle>::iterator i = triList.begin(); i < triList.end(); i++)
00353       (*i).drawnormals();
00354 }
00355 
00356 //---------------------------------------------------------//
00357 
00358 void model::tform()
00359 {
00360    for (vector<triangle>::iterator i = triList.begin(); i < triList.end(); i++)
00361       (*i).tform();
00362 }
00363 
00364 //////////////////////////////////////////////////////////////
00365 //*********************************************************///
00366 //////////////////////////////////////////////////////////////
00367 
00368 #endif // __triangle_h_

Generated on Sun Oct 14 18:46:15 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001