// Shapes.h : class heirarchy for all OpenGL shape primitives // // (C) COPYRIGHT John Henckel, mailto:henckel@iname.com Aug 1998 // // Permission to use, copy, modify, distribute and sell this software // and its documentation for any purpose is hereby granted without fee, // provided that the above copyright notice appear in all copies. // // Visit my website! http://www.GeoCities.com/Paris/6502 #if !defined(shapes_h__INCLUDED_) #define shapes_h__INCLUDED_ #include #include "BasicTypes.h" //--------------------------------------------------------------------------- // These are used for flags in Draw and PreDraw #define DRAW_MONO 1 #define DRAW_WIRE 2 enum ShapeType { t_Sphere = 1, t_Teapot, t_Icosahedron, t_Octahedron, t_Tetrahedron, t_Dodecahedron, t_Box, t_Torus, t_Cylinder, t_Cone, t_Camera, t_ConvexSolid, t_Text }; //--------------------------------------------------------------------------- // Shape class Shape { public: Shape() // set default values { radius = 1.0; shine = 0.08; mass = 1.0; moment = Vect(1,1,1); } static Shape* CreateFromString(CString& data, int linenum, CString& errmsg); virtual void ParseAttr(CString& data, int linenum, CString& errmsg); virtual void PreDraw(int flags); virtual void LookAt(const Vect& v, double tilt, double alpha); virtual void Draw(int flags) = 0; virtual int Step(); virtual void FinishInit() {} // after all parseAttr are done virtual ShapeType Type() = 0; virtual CString TypeString() = 0; virtual CString Title(); // public common attributes Vect location; Quat rotation; Vect speed; Quat spin; double mass; Vect moment; // moment of inertia about the principal axes Color color; double shine; double radius; CString title; int linenum; }; //--------------------------------------------------------------------------- #define DECLSHAPE1(ClassName) \ class ClassName : public Shape { public: \ void Draw(int flags); \ ShapeType Type(); \ CString TypeString(); \ } #define IMPLSHAPE1(ClassName) \ ShapeType ClassName::Type() { return t_##ClassName; } \ CString ClassName::TypeString() { return #ClassName; } \ void ClassName::Draw(int flags) { \ PreDraw(flags); \ if (flags & DRAW_WIRE) auxWire##ClassName(radius); \ else auxSolid##ClassName(radius); \ } #define IMPLSHAPE2(ClassName) \ ShapeType ClassName::Type() { return t_##ClassName; } \ CString ClassName::TypeString() { return #ClassName; } DECLSHAPE1(Sphere); DECLSHAPE1(Teapot); DECLSHAPE1(Icosahedron); DECLSHAPE1(Octahedron); DECLSHAPE1(Tetrahedron); DECLSHAPE1(Dodecahedron); //--------------------------------------------------------------------------- class Box : public Shape { public: Box(); void ParseAttr(CString& data, int linenum, CString& errmsg); void Draw(int flags); void FinishInit(); ShapeType Type(); CString TypeString(); Vect size; }; //--------------------------------------------------------------------------- class Torus : public Shape { public: Torus(); void ParseAttr(CString& data, int linenum, CString& errmsg); void Draw(int flags); void FinishInit(); ShapeType Type(); CString TypeString(); double rad1, rad2; }; //--------------------------------------------------------------------------- class Cylinder : public Shape { public: Cylinder(); void ParseAttr(CString& data, int linenum, CString& errmsg); void Draw(int flags); void FinishInit(); ShapeType Type(); CString TypeString(); double len, rad; }; //--------------------------------------------------------------------------- class Cone : public Shape { public: Cone(); void ParseAttr(CString& data, int linenum, CString& errmsg); void Draw(int flags); void FinishInit(); ShapeType Type(); CString TypeString(); double len, rad; }; //--------------------------------------------------------------------------- class Camera : public Shape { public: Camera(); void ParseAttr(CString& data, int linenum, CString& errmsg); void Draw(int flags); void FinishInit(); ShapeType Type(); CString TypeString(); int Step(); double fov, clipnear, clipfar; Vect lookat; double tilt; BOOL look; Color background; }; //--------------------------------------------------------------------------- // These are used by ConvexSolid class Edge { public: Edge(): p1(0), p2(0) {} Edge(int a, int b): p1(a), p2(b) {} int operator == (const Edge& e) { return p1==e.p1 && p2==e.p2; } int p1, p2; }; class Face { public: Vect normal; CArray vert; Face& operator = (const Face& f) { normal=f.normal; vert.Copy(f.vert); return *this; } }; typedef CArray VectArray; typedef CArray EdgeArray; typedef CArray FaceArray; //--------------------------------------------------------------------------- class ConvexSolid : public Shape { public: ConvexSolid(); void ParseAttr(CString& data, int linenum, CString& errmsg); void Draw(int flags); void FinishInit(); ShapeType Type(); CString TypeString(); Vect center; // this is only used during parsing VectArray vert; EdgeArray edge; FaceArray face; double scale; private: int NextVert(int p1, int p2); }; //--------------------------------------------------------------------------- class Text : public Shape { public: Text(); void ParseAttr(CString& data, int linenum, CString& errmsg); void Draw(int flags); void FinishInit(); ShapeType Type(); CString TypeString(); CString text; }; #endif