Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

vuGrid.cpp

Go to the documentation of this file.
00001 /*
00002   Copyright (c) 2002 Tai Meng (tmeng@sfu.ca). All Rights Reserved
00003 */ 
00004 
00005 /*============================================================*/
00006 
00007 #include <GL/glut.h> //OpenGL / GLUT
00008 #include <iostream> //iostream.h is an older library
00009 #include <string> //for debugging and unit testing; string.h is c-style
00010 
00011 #include "vuVector.h"
00012 
00013 #include "vuGrid.h"
00014 
00015 /*============================================================*/
00016 
00017 using namespace std;
00018 
00019 /*============================================================*/
00020 /* CONSTRUCTORS */
00021 /*============================================================*/
00022 
00023 vuGrid::vuGrid()
00024   //All components will be initialized to 0
00025 {
00026 }
00027 
00028 /*============================================================*/
00029 
00030 vuGrid::vuGrid(const vuGrid& v)
00031   : xWidth(v.xWidth), yHeight(v.yHeight), zDepth(v.zDepth), 
00032     isDrawGrid(v.isDrawGrid)
00033 {
00034 }
00035 
00036 /*============================================================*/
00037 
00038 vuGrid::vuGrid(float width, float height, float depth)
00039   : xWidth(width), yHeight(height), zDepth(depth), isDrawGrid(true)
00040 {
00041 }
00042 
00043 /*============================================================*/
00044 
00045 vuGrid::vuGrid(float width, float height, float depth, bool isGrid)
00046   : xWidth(width), yHeight(height), zDepth(depth), isDrawGrid(isGrid)
00047 {
00048 }
00049 
00050 /*============================================================*/
00051 
00052 vuGrid::~vuGrid()
00053 {
00054 }
00055 
00056 /*============================================================*/
00057 /* MUTATORS */
00058 /*============================================================*/
00059   
00060 vuGrid& vuGrid::operator=(const vuGrid& v)
00061 {
00062  //if LHS is not referencing the same object as the right hand side
00063  if(this != &v)
00064    {
00065      //do the copying
00066      xWidth = v.xWidth;
00067      yHeight = v.yHeight;
00068      zDepth = v.zDepth;
00069      isDrawGrid = v.isDrawGrid;
00070    }
00071  return *this;
00072 }
00073 
00074 /*============================================================*/
00075 
00076 void vuGrid::disable()
00077 {
00078   isDrawGrid = false;
00079 }
00080 
00081 /*============================================================*/
00082 
00083 void vuGrid::enable()
00084 {
00085   isDrawGrid = true;
00086 }
00087 
00088 /*============================================================*/
00089 
00090 void vuGrid::toggle()
00091 {
00092   isDrawGrid = !isDrawGrid;
00093 }
00094 
00095 /*============================================================*/
00096 /* ACCESSORS */
00097 /*============================================================*/
00098 
00099 float vuGrid::getWidth() const
00100 {
00101   return xWidth;
00102 }
00103 
00104 /*============================================================*/
00105 
00106 float vuGrid::getHeight() const
00107 {
00108   return yHeight;
00109 }
00110 
00111 /*============================================================*/
00112 
00113 float vuGrid::getDepth() const
00114 {
00115   return zDepth;
00116 }
00117 
00118 /*============================================================*/
00119 
00120 bool vuGrid::getStatus() const
00121 {
00122   return isDrawGrid;
00123 }
00124 
00125 /*============================================================*/
00126 
00127 float vuGrid::getCenterX() const
00128 {
00129   return xWidth / 2.0f;
00130 }
00131 
00132 /*============================================================*/
00133 
00134 float vuGrid::getCenterY() const
00135 {
00136   return yHeight / 2.0f;
00137 }
00138 /*============================================================*/
00139 
00140 float vuGrid::getCenterZ() const
00141 {
00142   return zDepth / 2.0f;
00143 }
00144 /*============================================================*/
00145 
00146 bool vuGrid::operator==(const vuGrid& v) const
00147 { 
00148   return (xWidth == v.xWidth &&
00149           yHeight == v.yHeight &&
00150           zDepth == v.zDepth);
00151 }
00152 
00153 /*============================================================*/
00154 
00155 bool vuGrid::operator!=(const vuGrid& v) const
00156 {
00157   return !vuGrid::operator==(v);
00158 }
00159 
00160 /*============================================================*/
00161 
00162 void vuGrid::drawLine(vuVector& start, vuVector& end) const
00163 {
00164   //the line has to be opaque else it won't draw in blend mode
00165   float c1 = 0.5f;
00166   float c2 = 0.75f;
00167   
00168   glColor4f(c1, c2, 0.0f, 1.0f);
00169   glVertex3fv(start.getData());
00170   glColor4f(0.0f, c1, c2, 1.0f);
00171   glVertex3fv(end.getData());
00172 }
00173 
00174 /*============================================================*/
00175 
00176 void vuGrid::labelAxes() const
00177 {
00178   //static so that the variables need not be reinitilized every
00179   //time this function is called
00180 
00181   //but the variables here are not member attribs since they are
00182   //only used here
00183 
00184   static const float GRID_ORIGIN_X = 0.0f;
00185   static const float GRID_ORIGIN_Y = 0.0f;
00186   static const float GRID_ORIGIN_Z = 0.0f;
00187 
00188   const float GRID_WIDTH = xWidth;
00189   const float GRID_HEIGHT = yHeight;
00190   const float GRID_DEPTH = zDepth;
00191 
00192   float offset = (xWidth + yHeight + zDepth) / 30.0f;
00193 
00194   glBegin(GL_LINES);
00195 
00196   //the 'X'
00197   vuVector x1(GRID_WIDTH, GRID_ORIGIN_Y+offset, GRID_ORIGIN_Z);
00198   vuVector x2(GRID_WIDTH+offset, GRID_ORIGIN_Y-offset, GRID_ORIGIN_Z);
00199   drawLine(x1, x2);
00200   vuVector x3(GRID_WIDTH+offset, GRID_ORIGIN_Y+offset, GRID_ORIGIN_Z);
00201   vuVector x4(GRID_WIDTH, GRID_ORIGIN_Y-offset, GRID_ORIGIN_Z);
00202   drawLine(x3, x4);
00203  
00204   //the 'Y'
00205   vuVector y1(GRID_ORIGIN_X-(offset/2.0f), GRID_HEIGHT+(offset*2.0f), GRID_ORIGIN_Z);
00206   vuVector y2(GRID_ORIGIN_X, GRID_HEIGHT+offset, GRID_ORIGIN_Z);
00207   drawLine(y1, y2);
00208   vuVector y3(GRID_ORIGIN_X+(offset/2.0f), GRID_HEIGHT+(offset*2.0f), GRID_ORIGIN_Z);
00209   vuVector y4(GRID_ORIGIN_X-(offset/2.0f), GRID_HEIGHT, GRID_ORIGIN_Z);
00210   drawLine(y3, y4);
00211 
00212   //the 'Z'
00213   vuVector z1(GRID_ORIGIN_X, GRID_ORIGIN_Y+offset, GRID_DEPTH+offset);
00214   vuVector z2(GRID_ORIGIN_X, GRID_ORIGIN_Y+offset, GRID_DEPTH);
00215   drawLine(z1, z2);
00216   vuVector z3(GRID_ORIGIN_X, GRID_ORIGIN_Y-offset, GRID_DEPTH+offset);
00217   vuVector z4(GRID_ORIGIN_X, GRID_ORIGIN_Y-offset, GRID_DEPTH);
00218   drawLine(z3, z4);
00219   vuVector z5(GRID_ORIGIN_X, GRID_ORIGIN_Y+offset, GRID_DEPTH);
00220   vuVector z6(GRID_ORIGIN_X, GRID_ORIGIN_Y-offset, GRID_DEPTH+offset);
00221   drawLine(z5, z6);
00222 
00223   glEnd();
00224 }
00225 
00226 /*============================================================*/
00227 
00228 void vuGrid::drawAxes() const
00229 {
00230   const float GRID_ORIGIN_X = 0.0f;
00231   const float GRID_ORIGIN_Y = 0.0f;
00232   const float GRID_ORIGIN_Z = 0.0f;
00233 
00234   vuVector origin(GRID_ORIGIN_X, GRID_ORIGIN_Y, GRID_ORIGIN_Z);
00235   vuVector yAxis(GRID_ORIGIN_X, yHeight, GRID_ORIGIN_Z);
00236   vuVector xAxis(xWidth, GRID_ORIGIN_Y, GRID_ORIGIN_Z);
00237   vuVector zAxis(GRID_ORIGIN_X, GRID_ORIGIN_Y, zDepth);
00238 
00239   glBegin(GL_LINES);
00240   //first draw 3 axes, regardless of whether or not grid is enabled
00241   drawLine(origin, xAxis);
00242   drawLine(origin, yAxis);
00243   drawLine(origin, zAxis);
00244   glEnd();
00245 }
00246 
00247 /*============================================================*/
00248 
00249 void vuGrid::drawInOpenGL() const
00250 {
00251   if (isDrawGrid)
00252     {
00253       labelAxes();
00254       drawAxes();
00255 
00256       //could also draw grid walls, but that is optional
00257     }
00258 }
00259 
00260 /*============================================================*/
00261 /* Friend Functions */
00262 /*============================================================*/
00263 
00264 ostream& operator<<(ostream& out, const vuGrid& v)
00265 {
00266   out<<"Width: "<<v.xWidth<<endl
00267      <<"Height: "<<v.yHeight<<endl
00268      <<"Depth: "<<v.zDepth<<endl
00269      <<"Is grid enabled? "<<v.isDrawGrid<<endl;
00270   return out;
00271 }

Generated on Wed Dec 15 21:20:34 2004 for vuVolume by  doxygen 1.3.9.1