Basic Shape Creation and Rendering  2.0
Tools to create and render common shapes like blocks, cylinders, cones, and spheres
BasicShape.h
Go to the documentation of this file.
1 
11 // This is OPEN SOURCE software developed by James R. Miller (jrmiller@ku.edu)
12 
13 #ifndef BASICSHAPE_H
14 #define BASICSHAPE_H
15 
16 #ifdef __APPLE__
17 #include "GLFW/glfw3.h"
18 #else
19 #include <GL/gl.h>
20 #endif
21 
22 #include "AffPoint.h"
23 #include "AffVector.h"
24 
26 {
27 public:
29  virtual ~BasicShape();
30 
33  enum Caps { CAP_AT_NEITHER, CAP_AT_BOTTOM, CAP_AT_TOP, CAP_AT_BOTH };
34 
76  static BasicShape* makeBlock(
77  const cryph::AffPoint& llCorner,
78  const cryph::AffVector& uEdge, double uLength,
79  const cryph::AffVector& vEdge, double vLength,
80  const cryph::AffVector& wEdge, double wLength);
81 
117  static BasicShape* makeBoundedCone(
118  const cryph::AffPoint& Pbottom, const cryph::AffPoint& Ptop,
119  double radiusAtBottom, double radiusAtTop,
120  int nPointsAroundSide=10, int nPointsAlongAxis=2,
121  Caps capSpec=CAP_AT_BOTH,
122  double sMin=0, double sMax=0, double tMin=0, double tMax=0,
123  const cryph::AffVector& sZero=cryph::AffVector::xu);
124 
158  const cryph::AffPoint& Pbottom, const cryph::AffPoint& Ptop,
159  double radius,
160  int nPointsAroundSide=10, int nPointsAlongAxis=2,
161  Caps capSpec=CAP_AT_BOTH,
162  double sMin=0, double sMax=0, double tMin=0, double tMax=0,
163  const cryph::AffVector& sZero=cryph::AffVector::xu);
164 
199  static BasicShape* makeSphere(
200  const cryph::AffPoint& center, double radius,
201  int nPointsAroundSide=15, int nPointsAlongAxis=15,
202  double sMin=0, double sMax=0, double tMin=0, double tMax=0,
203  const cryph::AffVector& upAxis=cryph::AffVector::yu,
204  const cryph::AffVector& sZero=cryph::AffVector::xu);
205 
234  static BasicShape* makeTorus(
235  const cryph::AffPoint& center, const cryph::AffVector& u, const cryph::AffVector& v,
236  double rMajor, double rMinor,
237  double thetaMinDegrees=0.0, double thetaMaxDegrees=360.0,
238  double phiMinDegrees=0, double phiMaxDegrees=360.0,
239  int nPointsInThetaDirection=60, int nPointsInPhiDirection=40,
240  double sMin=0, double sMax=0, double tMin=0, double tMax=0);
241 
246  void getMCBoundingBox(double* xyzLimits) const;
247 
253  int getNumPoints() const { return nPoints; }
254 
259  const float* getPointCoords() const { return pointCoords; }
260 
265  const float* getNormals() const { return normals; }
266 
271  const float* getTextureCoords() const { return textureCoords; }
272 
276  int getNumDrawArraysCalls() const { return nDrawArraysCalls; }
277 
298  int getDrawArraysData(int i, GLenum& mode, int& thisStartsFace, int& offset,
299  bool& canUsePerVertexTexCoords, bool& canUseNormalArray,
300  cryph::AffVector& fixedN) const;
301 
305  int getNumIndexLists() const { return nIndexLists; }
306 
329  const void* getIndexList(int i, GLenum& mode, int& thisStartsFace, int& nInList,
330  GLenum& type,
331  bool& canUsePerVertexTexCoords, bool& canUsePerVertexNormals,
332  cryph::AffVector& fixedNormal) const;
333 
339  { return alwaysGeneratePerVertexNormals; }
340 
348  static void setAlwaysGeneratePerVertexNormals(bool b);
349 
350 private:
351  BasicShape(); // use public factory methods instead
352  BasicShape(const BasicShape& s) {} // don't make copies
353 
354  // common "worker" routines for cones and cylinders:
355  void addCapsFixedNormal(Caps capSpec, int nPointsInCap,
356  const cryph::AffVector& axis, int nPointsAlongAxis);
357  void addCapsPerVertexNormals(Caps capSpec, int nPointsAlongAxis,
358  int firstAtBottom, int firstAtTop, int nPointsInOneCap);
359  void addDrawArraysDataForConeCylCaps(int nextIndex,
360  int firstAtBottom, int firstAtTop, int nPointsInOneCap);
361  void allocateIndexListsForConeCyl(int nPointsAlongAxis, Caps capSpec);
362  void allocateIndexListsForSphere(int nPointsAlongAxis);
363  void makeBlockData(
364  const cryph::AffPoint& llCorner,
365  const cryph::AffVector& uEdge, double uLength,
366  const cryph::AffVector& vEdge, double vLength,
367  const cryph::AffVector& wEdge, double wLength);
368  void finishBlockDataUsingFixedNormals(
369  const cryph::AffVector& uvNormal, const cryph::AffVector& uwNormal,
370  const cryph::AffVector& vwNormal, const cryph::AffPoint* vertices);
371  void finishBlockDataUsingPerVertexNormals(
372  const cryph::AffVector& uvNormal, const cryph::AffVector& uwNormal,
373  const cryph::AffVector& vwNormal, const cryph::AffPoint* vertices);
374  void makeDrawArraysDataForConeCyl(int nPointsWithoutCaps,
375  int firstAtBottom, int firstAtTop); // if nPointsAlongAxis == 2
376  void makeEightBlockVertices(
377  const cryph::AffPoint& llCorner,
378  const cryph::AffVector& u, double uLength,
379  const cryph::AffVector& v, double vLength,
380  const cryph::AffVector& w, double wLength,
381  cryph::AffPoint* vertices);
382  void makeIndexLists(int nPointsAroundSide, int nPointsAlongAxis);
383  void makeRuledSurfaceBetweenCircles(
384  const cryph::AffPoint& Pbottom, cryph::AffVector& axis,
385  double height, double radiusAtBottom, double radiusAtTop,
386  int nPointsAroundSide, int nPointsAlongAxis,
387  Caps capSpec, double sMin, double sMax, double tMin, double tMax,
388  const cryph::AffVector& sZero);
389  void makeSphere(const cryph::AffPoint& center,
390  const cryph::AffVector& u, const cryph::AffVector& v, const cryph::AffVector& w,
391  double radius,
392  int nPointsAroundSide, int nPointsAlongAxis,
393  double sMin, double sMax, double tMin, double tMax);
394  static void updateOneCoordLimit(double c, double minMax[2]);
395  void updateXYZMinMaxLimits(const cryph::AffPoint& p);
396 
397  // geometry and per-vertex attributes
398  float* pointCoords;
399  float* normals;
400  float* textureCoords;
401  int nPoints;
402  // always generate per-vertex normals, even for flat faces?
403  static bool alwaysGeneratePerVertexNormals;
404  // for bounding box
405  double xyzMinMax[6];
406 
407  struct DrawArraysCallData
408  {
409  GLenum mode;
410  int thisStartsFace; // >= 0 ==> this starts a new face on the original shape
411  int offset;
412  int nPointsInCall;
413  bool usePerVertexTexCoords, usePerVertexNormals;
414  cryph::AffVector useFixedNormal; // if !usePerVertexNormals
415  DrawArraysCallData() : thisStartsFace(-1) {}
416  };
417  DrawArraysCallData* drawArraysCallData;
418  int nDrawArraysCalls;
419 
420  struct IndexListData
421  {
422  GLenum mode;
423  int thisStartsFace; // >= 0 ==> this starts a new face on the original shape
424  int* indices;
425  int nIndices;
426  bool usePerVertexTexCoords, usePerVertexNormals;
427  cryph::AffVector useFixedNormal; // if !usePerVertexNormals
428  IndexListData(): thisStartsFace(-1) {}
429  };
430  IndexListData* indexLists;
431  int nIndexLists, nIndexListsConstructed;
432 };
433 
434 #endif
static BasicShape * makeBoundedCylinder(const cryph::AffPoint &Pbottom, const cryph::AffPoint &Ptop, double radius, int nPointsAroundSide=10, int nPointsAlongAxis=2, Caps capSpec=CAP_AT_BOTH, double sMin=0, double sMax=0, double tMin=0, double tMax=0, const cryph::AffVector &sZero=cryph::AffVector::xu)
Definition: BasicShape.c++:410
virtual ~BasicShape()
Definition: BasicShape.c++:23
static void setAlwaysGeneratePerVertexNormals(bool b)
Definition: BasicShape.c++:862
const void * getIndexList(int i, GLenum &mode, int &thisStartsFace, int &nInList, GLenum &type, bool &canUsePerVertexTexCoords, bool &canUsePerVertexNormals, cryph::AffVector &fixedNormal) const
Definition: BasicShape.c++:300
const float * getTextureCoords() const
Definition: BasicShape.h:271
static BasicShape * makeSphere(const cryph::AffPoint &center, double radius, int nPointsAroundSide=15, int nPointsAlongAxis=15, double sMin=0, double sMax=0, double tMin=0, double tMax=0, const cryph::AffVector &upAxis=cryph::AffVector::yu, const cryph::AffVector &sZero=cryph::AffVector::xu)
Definition: BasicShape.c++:646
void getMCBoundingBox(double *xyzLimits) const
Definition: BasicShape.c++:320
int getNumIndexLists() const
Definition: BasicShape.h:305
static BasicShape * makeTorus(const cryph::AffPoint &center, const cryph::AffVector &u, const cryph::AffVector &v, double rMajor, double rMinor, double thetaMinDegrees=0.0, double thetaMaxDegrees=360.0, double phiMinDegrees=0, double phiMaxDegrees=360.0, int nPointsInThetaDirection=60, int nPointsInPhiDirection=40, double sMin=0, double sMax=0, double tMin=0, double tMax=0)
Definition: BasicShape.c++:732
Definition: BasicShape.h:25
int getNumPoints() const
Definition: BasicShape.h:253
const float * getNormals() const
Definition: BasicShape.h:265
static BasicShape * makeBoundedCone(const cryph::AffPoint &Pbottom, const cryph::AffPoint &Ptop, double radiusAtBottom, double radiusAtTop, int nPointsAroundSide=10, int nPointsAlongAxis=2, Caps capSpec=CAP_AT_BOTH, double sMin=0, double sMax=0, double tMin=0, double tMax=0, const cryph::AffVector &sZero=cryph::AffVector::xu)
Definition: BasicShape.c++:390
static bool getAlwaysGeneratePerVertexNormals()
Definition: BasicShape.h:338
static BasicShape * makeBlock(const cryph::AffPoint &llCorner, const cryph::AffVector &uEdge, double uLength, const cryph::AffVector &vEdge, double vLength, const cryph::AffVector &wEdge, double wLength)
Definition: BasicShape.c++:332
Caps
Definition: BasicShape.h:33
const float * getPointCoords() const
Definition: BasicShape.h:259
int getDrawArraysData(int i, GLenum &mode, int &thisStartsFace, int &offset, bool &canUsePerVertexTexCoords, bool &canUseNormalArray, cryph::AffVector &fixedN) const
Definition: BasicShape.c++:282
int getNumDrawArraysCalls() const
Definition: BasicShape.h:276