LCG_cg  1.0
Data Structures | Macros | Typedefs | Enumerations | Functions | Variables
torus.cpp File Reference

This program demonstrates the creation of display lists for constructing some quadric surfaces. More...

#include <IL/il.h>
#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <Arcball/Arcball.hpp>
Include dependency graph for torus.cpp:

Go to the source code of this file.

Data Structures

struct  _Point
 3D point structure. More...
 

Macros

#define GL_GLEXT_PROTOTYPES
 
#define M_PI_   3.14159265358979323846
 Π definition. More...
 
#define checkImageWidth   128
 Texture Image width. More...
 
#define checkImageHeight   128
 Texture Image height. More...
 
#define farPlane   (zobs+10*d)
 Far plane distance. More...
 
#define frontPlane   1
 Front plane distance. More...
 
#define posAng(x)   ((x) < 0.0 ? ((x)+TWOPI) : (x))
 Return a positive angle. More...
 
#define TWOPI   (2*M_PI)
 Two PI. More...
 
#define toRad(x)   ((x)*M_PI/180.0)
 Convert an angle to radians. More...
 
#define toDeg(x)   ((x)*180.0/M_PI)
 Convert an angle to degrees. More...
 
#define clamp(x)   (fmin(fmax((x),0),1))
 Clamp to [0,1]. More...
 
#define LEN(x, y, z)   (sqrt((x)*(x)+(y)*(y)+(z)*(z)))
 Vector length. More...
 
#define theAxes   (2*nObjects)
 Axes. More...
 
#define nObjects   (sizeof(objNames)/sizeof(char*))
 Number of objects;. More...
 
#define scale   (0.05*diag)
 Scale for drawing normals. More...
 

Typedefs

typedef struct _Point Point
 3D point structure. More...
 
typedef Point _BBOX[2]
 Axis aligned Bounding box. More...
 
typedef enum _oType oType
 Quadric types. More...
 
typedef enum _rAxes rAxes
 Rotation axes. More...
 

Enumerations

enum  _oType {
  _torus = 0, _cylinder, _cone, _ellipsoid,
  _paraboloid, _hyperbolic_paraboloid, _hyperboloid
}
 Quadric types. More...
 
enum  _rAxes { xAxis, yAxis, zAxis }
 Rotation axes. More...
 

Functions

void updateBox (oType index, double x, double y, double z)
 Add a new point to a bounding box. More...
 
double getBoxSize (oType index, double *dx, double *dy, double *dz)
 Return bounding box dimension. More...
 
void cone (double h, double r, int vs, int rs, int normals)
 Draw a cone, centered at the origin, with a circular base on plane XY. More...
 
void cylinder (double h, double r, int vs, int rs, int normals)
 Draw a cylinder, centered at the origin, with a circular base on plane XY. More...
 
void paraboloid (double h, double r, int vs, int rs, int normals)
 Draw a paraboloid, centered at the origin, with a circular base on plane XY. More...
 
void hyperboloid (double h, double a, double r, int vs, int rs, int normals)
 Draw a one-sheeted hyperboloid of revolution, centered at the origin, with a circular base parallel to plane XY. More...
 
void hyperbolic_paraboloid (double w, double h, int ws, int hs, int normals)
 Draw a hyperbolic paraboloid. More...
 
void torus (double r1, double r2, int numc, int numt, int normals)
 Draw a torus, centered at the origin, and azimuthally symmetric about the z-axis. More...
 
void sphere (double r1, double r2, double r3, int numc, int numt, int normals)
 Draw an ellipsoid or a sphere centered at the origin. More...
 
void drawAxes (double r1, double r2)
 Draw the three coordinate axes. More...
 
void drawBox (const _BBOX BBOX)
 Draw the bounding box as a dashed hexahedron. More...
 
void makeCheckImage (void)
 Create checkerboard texture. More...
 
int loadImage (const char *filename)
 Load an image from a file. More...
 
void initTexture (void)
 Initialize texture stuff. More...
 
void init (void)
 Initialize OpenGL state. More...
 
void display (void)
 Clear window, reset depth buffer and draw torus. More...
 
int dateNow ()
 Return the current Coordinated Universal Time (UTC) in miliseconds. More...
 
void _timer (int value)
 Timer callback function to be triggered in at least delay milliseconds. More...
 
void reshape (int w, int h)
 Handle window resize. More...
 
void keyboard (unsigned char key, int x, int y)
 Keyboard callback interface. More...
 
void mouseFunc (int state, int button, int _x, int _y)
 Mouse callback interface. More...
 
void mouseDrag (int _x, int _y)
 Mouse callback interface. More...
 
int main (int argc, char **argv)
 Main program for testing. More...
 

Variables

double zobs = 10.0
 Camera position. More...
 
double viewingAngle = 45
 Camera viewing angle, also known as opening angle. More...
 
GLuint objects
 Object display list base index. More...
 
double R1 = 1.0
 Distance from the center of the tube to the center of the torus. More...
 
double R2 = 0.3
 Radius of the tube. More...
 
double R3 = 0.5
 Radius in z direction for an ellipsoid. More...
 
int nR1 = 25
 Number of samples. More...
 
int nR2 = 10
 Number of samples. More...
 
int drawWire = 0
 Whether draw wireframe or filled polygons. More...
 
int drawNormals = 0
 Whether display vertex normals. More...
 
int applyTexture = 0
 Whether to apply texture. More...
 
int showAxes = 0
 Whether to show the coordinate axes. More...
 
int showBox = 0
 Whether to show the bounding boxes. More...
 
int backG = 0
 Toggle background color. More...
 
int procImage = 1
 Whether to use a procedural texture. More...
 
int useArcball = 0
 Whether to use the arcball paradigm. More...
 
int spin = 1
 Automatic spin. More...
 
rAxes axis = xAxis
 Current axis. More...
 
GLfloat rotAngle = 2.0
 Rotation angle increment. More...
 
unsigned int delay = 60
 Controls the trigger of the timer function. More...
 
GLubyte checkImage [checkImageHeight][checkImageWidth][4]
 Procedural image for a checkerboard. More...
 
GLuint texName
 Texture identifier. More...
 
ILuint ilImage
 IL image identifier. More...
 
const char * objNames [] = {"Torus", "Cylinder", "Cone", "Ellipsoid", "Paraboloid", "Hyperbolic Paraboloid", "Hyperboloid"}
 Object names. More...
 
_BBOX objBoxes [nObjects]
 Object boxes. More...
 
int OBJECT = _torus
 Current object. More...
 
Arcball arcball
 ArcBall. More...
 

Detailed Description

This program demonstrates the creation of display lists for constructing some quadric surfaces.

Quadric surfaces are defined by quadratic equations in two dimensional space.

Display list is a group of OpenGL commands that have been stored (compiled) for later execution. Once a display list is created, all vertex and pixel data are evaluated and copied into the display list memory on the server machine. It is only one time process. After the display list has been prepared (compiled), you can reuse it repeatedly without re-evaluating and re-transmitting data over and over again to draw each frame. Display list is one of the fastest methods to draw static data because vertex data and OpenGL commands are cached in the display list and minimize data transmissions from the client to the server side. It means that it reduces CPU cycles to perform the actual data transfer.

Author
Paulo R Cavalcanti
Date
06/11/2017
See also
https://www.opengl.org/archives/resources/code/samples/redbook/torus.c
http://www.songho.ca/opengl/gl_displaylist.html
https://web.cs.wpi.edu/~matt/courses/cs563/talks/renderman/quadric.html
http://ieeexplore.ieee.org/stamp/stamp.jsp?arnumber=4037402
http://www.nptel.ac.in/courses/Webcourse-contents/IIT-Delhi/Computer%20Aided%20Design%20&%20ManufacturingI/mod3/01.htm
http://openil.sourceforge.net/
http://tutorial.math.lamar.edu/Classes/CalcIII/QuadricSurfaces.aspx
http://www.cs.colby.edu/maxwell/courses/tutorials/maketutor/

Definition in file torus.cpp.

Macro Definition Documentation

◆ checkImageHeight

#define checkImageHeight   128

Texture Image height.

Definition at line 63 of file torus.cpp.

Referenced by initTexture(), and makeCheckImage().

◆ checkImageWidth

#define checkImageWidth   128

Texture Image width.

Definition at line 61 of file torus.cpp.

Referenced by initTexture(), and makeCheckImage().

◆ clamp

#define clamp (   x)    (fmin(fmax((x),0),1))

Clamp to [0,1].

Definition at line 79 of file torus.cpp.

Referenced by cone(), cylinder(), hyperboloid(), paraboloid(), sphere(), and torus().

◆ farPlane

#define farPlane   (zobs+10*d)

Far plane distance.

Near and far clipping planes should always be specified as positive. That's because they represent how far in front of the "camera" the clipping planes will be.

Definition at line 67 of file torus.cpp.

Referenced by reshape().

◆ frontPlane

#define frontPlane   1

Front plane distance.

Definition at line 69 of file torus.cpp.

Referenced by reshape().

◆ GL_GLEXT_PROTOTYPES

#define GL_GLEXT_PROTOTYPES

Definition at line 44 of file torus.cpp.

◆ LEN

#define LEN (   x,
  y,
 
)    (sqrt((x)*(x)+(y)*(y)+(z)*(z)))

Vector length.

Definition at line 81 of file torus.cpp.

Referenced by cone(), cylinder(), getBoxSize(), hyperbolic_paraboloid(), hyperboloid(), paraboloid(), sphere(), and torus().

◆ M_PI_

#define M_PI_   3.14159265358979323846

Π definition.

Unused. We are using M_PI from math.h The difference between const and define is that const has a type and define is trick, because it is the pre-compiler which replaces the value.

Definition at line 59 of file torus.cpp.

◆ nObjects

#define nObjects   (sizeof(objNames)/sizeof(char*))

Number of objects;.

Definition at line 85 of file torus.cpp.

Referenced by display(), and keyboard().

◆ posAng

#define posAng (   x)    ((x) < 0.0 ? ((x)+TWOPI) : (x))

Return a positive angle.

Definition at line 71 of file torus.cpp.

◆ scale

#define scale   (0.05*diag)

Scale for drawing normals.

Definition at line 87 of file torus.cpp.

Referenced by cone(), cylinder(), hyperbolic_paraboloid(), hyperboloid(), paraboloid(), sphere(), and torus().

◆ theAxes

#define theAxes   (2*nObjects)

Axes.

Definition at line 83 of file torus.cpp.

Referenced by display(), and init().

◆ toDeg

#define toDeg (   x)    ((x)*180.0/M_PI)

Convert an angle to degrees.

Definition at line 77 of file torus.cpp.

Referenced by reshape().

◆ toRad

#define toRad (   x)    ((x)*M_PI/180.0)

Convert an angle to radians.

Definition at line 75 of file torus.cpp.

Referenced by reshape().

◆ TWOPI

#define TWOPI   (2*M_PI)

Two PI.

Definition at line 73 of file torus.cpp.

Referenced by cone(), cylinder(), hyperboloid(), paraboloid(), sphere(), and torus().

Typedef Documentation

◆ _BBOX

typedef Point _BBOX[2]

Axis aligned Bounding box.

Definition at line 128 of file torus.cpp.

◆ oType

typedef enum _oType oType

Quadric types.

◆ Point

typedef struct _Point Point

3D point structure.

◆ rAxes

typedef enum _rAxes rAxes

Rotation axes.

Enumeration Type Documentation

◆ _oType

enum _oType

Quadric types.

Enumerator
_torus 
_cylinder 
_cone 
_ellipsoid 
_paraboloid 
_hyperbolic_paraboloid 
_hyperboloid 

Definition at line 131 of file torus.cpp.

◆ _rAxes

enum _rAxes

Rotation axes.

Enumerator
xAxis 
yAxis 
zAxis 

Definition at line 134 of file torus.cpp.

Function Documentation

◆ _timer()

void _timer ( int  value)

Timer callback function to be triggered in at least delay milliseconds.

The number of milliseconds is a lower bound on the time before the callback is generated. GLUT attempts to deliver the timer callback as soon as possible after the expiration of the callback's time interval.

There is no support for canceling a registered callback. Instead, ignore a callback based on its value parameter when it is triggered.

Parameters
valuea parameter passed to this callback function.
See also
https://www.opengl.org/resources/libraries/glut/spec3/node64.html

Definition at line 1219 of file torus.cpp.

References dateNow(), and delay.

Referenced by main().

◆ cone()

void cone ( double  h,
double  r,
int  vs,
int  rs,
int  normals 
)

Draw a cone, centered at the origin, with a circular base on plane XY.

Implicit Equation:

  • \({x^{2} \over c^{2}}+{y^{2} \over c^{2}} = {z^{2}},\ c = \frac{r}{h}\)
  • \({f(x,y,z) = \frac{h}{r} x^{2}}+{\frac{h}{r} y^{2}}-{\frac{r}{h} z^{2} = 0}\)
  • \(\nabla f(x,y,z) = (\frac{h}{r} x,\ \frac{h}{r} y,\ -{\frac{r}{h} z})\)

Parametric Equation:

  • x(u,v) = u cos(v),
  • y(u,v) = u sin(v),
  • z(u,v) = u * h / r,
  • u ∈ [0,h], v ∈ [0,2Π]
  • df/du = (cos(v), sin(v), h/r),
  • df/dv = (-u sin(v), u cos(v) , 0)
  • df/du x df/dv = (h/r * u cos(v), h/r * u sin(v), -u) = (h/r * x(u,v), h/r * y(u,v), -r/h * z(u,v))

Inverse parametrization:

  • u = (r/h) * z, or u = (r/h) * (h-z), if cone is upside down.
  • v = atan2(y,x), if v < 0 then v += 2Π

Note: atan2 returns values ∈ [-Π, Π]

Parameters
hcone height.
rcone base radius.
vsnumber of divisions in z direction.
rsnumber of angular divisions.
normalswhether to draw normals or polygons.
See also
http://mathinsight.org/parametrized_surface_examples
http://mathworld.wolfram.com/Cone.html
https://en.wikipedia.org/wiki/Atan2

Definition at line 242 of file torus.cpp.

References _cone, clamp, getBoxSize(), LEN, scale, TWOPI, and updateBox().

Referenced by init().

◆ cylinder()

void cylinder ( double  h,
double  r,
int  vs,
int  rs,
int  normals 
)

Draw a cylinder, centered at the origin, with a circular base on plane XY.

Implicit Equation:

  • \({x^{2}+y^{2}} = {r^{2}},\) any z
  • \({f(x,y,z) = x^{2}+y^{2}}-{r^{2} = 0}\)
  • \({\nabla f(x,y,z) = (x, y, 0)}\)

Parametric Equation:

  • x(u,v) = r cos(v),
  • y(u,v) = r sin(v),
  • z(u,v) = u,
  • u ∈ [0,h], v ∈ [0,2Π]
  • df/du = (0, 0, 1),
  • df/dv = (-r sin(v), r cos(v) , 0)
  • df/dv x df/du = (r cos(v), r sin(v), 0) = (x(u,v), y(u,v), 0)

Inverse parametrization:

  • u = z
  • v = acos(x/r) = atan2(y,x), if v < 0 then v += 2Π
Parameters
hcylinder height.
rcylinder radius.
vsnumber of divisions in z direction.
rsnumber of angular divisions.
normalswhether to draw normals or polygons.
See also
http://mathworld.wolfram.com/Cylinder.html
https://en.wikipedia.org/wiki/Quadric
http://mathworld.wolfram.com/QuadraticSurface.html

Definition at line 313 of file torus.cpp.

References _cylinder, clamp, getBoxSize(), LEN, scale, TWOPI, and updateBox().

Referenced by init().

◆ dateNow()

int dateNow ( )

Return the current Coordinated Universal Time (UTC) in miliseconds.

Returns
time since the Epoch (00:00:00 UTC, January 1, 1970) measured in miliseconds.

Definition at line 1201 of file torus.cpp.

Referenced by _timer().

◆ display()

void display ( void  )

Clear window, reset depth buffer and draw torus.

Single buffer does not work, in general.

Definition at line 1141 of file torus.cpp.

References Arcball::applyRotationMatrix(), applyTexture, arcball, axis, drawBox(), drawNormals, drawWire, nObjects, objBoxes, OBJECT, objects, rotAngle, showAxes, showBox, spin, theAxes, useArcball, xAxis, yAxis, zAxis, and zobs.

Referenced by main().

◆ drawAxes()

void drawAxes ( double  r1,
double  r2 
)

Draw the three coordinate axes.

The length of the axes is r1 + r2.

Parameters
r1axis length.
r2axis additional length.

Definition at line 785 of file torus.cpp.

Referenced by init().

◆ drawBox()

void drawBox ( const _BBOX  BBOX)

Draw the bounding box as a dashed hexahedron.

Parameters
BBOXgiven bounding box.

Definition at line 805 of file torus.cpp.

Referenced by display().

◆ getBoxSize()

double getBoxSize ( oType  index,
double *  dx,
double *  dy,
double *  dz 
)

Return bounding box dimension.

Parameters
indexPosition into objBoxes array.
dxBounding box length.
dyBounding box width.
dzBounding box height.
Returns
Bounding box diameter.

Definition at line 199 of file torus.cpp.

References LEN, and objBoxes.

Referenced by cone(), cylinder(), hyperbolic_paraboloid(), hyperboloid(), paraboloid(), reshape(), sphere(), and torus().

◆ hyperbolic_paraboloid()

void hyperbolic_paraboloid ( double  w,
double  h,
int  ws,
int  hs,
int  normals 
)

Draw a hyperbolic paraboloid.

Implicit Equation:

  • \({xy = z}\)
  • \({f(x,y,z) = xy - z = 0}\)
  • \({\nabla f(x,y,z) = (y, x, -1)}\)

Parametric Equation:

  • x(u,v) = u,
  • y(u,v) = v,
  • z(u,v) = uv
  • u ∈ [0,w], v ∈ [0,h]
  • df/du = (1, 0, v),
  • df/dv = (0, 1, u)
  • df/du x df/dv = (y, x, -1)

Inverse parametrization:

  • u = x
  • v = y
Parameters
whyperbolic paraboloid width.
hhyperbolic paraboloid length.
wsnumber of divisions in x direction.
hsnumber of divisions in y direction.
normalswhether to draw normals or polygons.
See also
http://mathworld.wolfram.com/HyperbolicParaboloid.html
https://en.wikipedia.org/wiki/Quadric
http://www.geom.uiuc.edu/docs/reference/CRC-formulas/node61.html

Definition at line 539 of file torus.cpp.

References _hyperbolic_paraboloid, getBoxSize(), LEN, scale, and updateBox().

Referenced by init().

◆ hyperboloid()

void hyperboloid ( double  h,
double  a,
double  r,
int  vs,
int  rs,
int  normals 
)

Draw a one-sheeted hyperboloid of revolution, centered at the origin, with a circular base parallel to plane XY.

Implicit Equation:

  • \({{x^{2} \over a^{2}} + {y^{2} \over a^{2}} - {z^{2} \over c^{2}}} = 1\)
  • \(r = a * \sqrt{1 + \frac{h^2}{4c^2}}\)

  • - \(c = \sqrt{h^2 / (4 (\frac{r^2}{a^2} - 1))},\ r \ge a\)
  • \({f(x,y,z) = {c^2} x^{2} + {c^2} y^{2} - {a^2} z^{2} - {a^2} {c^2} = 0}\)
  • \({\nabla f(x,y,z) = (2 \frac{x}{a^2},\ 2 \frac{y}{a^2},\ -2 \frac{z}{c^2})}\)

Parametric Equation:

  • x(u,v) = a sqrt(1+u²) cos(v),
  • y(u,v) = a sqrt(1+u²) sin(v),
  • z(u,v) = c u
  • u ∈ [-h,h], v ∈ [0,2Π]
  • df/du = (-a u/srqt(1+u²) cos(v), -a u/srqt(1+u²) sin(v), c),
  • df/dv = (-a sqrt(1+u²) sin(v), a sqrt(1+u²) cos(v) , 0)
  • df/dv x df/du = (c x, c y, -a²/c z)

Inverse parametrization:

  • u = sqrt( x² / a² + y² / a² - 1 )
  • v = atan2(y,x), if v < 0 then v += 2Π
Parameters
hhyperboloid height.
ahyperboloid skirt radius.
rhyperboloid base radius.
vsnumber of divisions in z direction.
rsnumber of angular divisions.
normalswhether to draw normals or polygons.
See also
http://mathworld.wolfram.com/One-SheetedHyperboloid.html
http://www.geom.uiuc.edu/docs/reference/CRC-formulas/node61.html
https://rechneronline.de/pi/hyperboloid-e.php

Definition at line 463 of file torus.cpp.

References _hyperboloid, clamp, getBoxSize(), LEN, scale, TWOPI, and updateBox().

Referenced by init().

◆ init()

void init ( void  )

Initialize OpenGL state.

Create fifteen display lists for the seven objects, and their normal vectors, plus another one for the coordinate axes.

Use Gouraud shading, and a single light source at the camera position.

OpenGL uses a right-handed coordinate system until the perspective projection, where OpenGL switches to a left-handed coordinate space for the depth buffer (z-buffer) test.

  • The camera at the origin is looking along -Z axis in eye space,
  • but it is looking along +Z axis in NDC.
  • glDepthRange is by default [0, 1] (near, far).
  • Making the +Z axis point into the screen,
  • and with +X to the right and +Y up.
  • It is a left-handed system.
  • Changing the depth range to [1, 0] will make the system right-handed.
See also
https://www.youtube.com/watch?v=Ck1SH7oYRFM
https://webserver2.tecgraf.puc-rio.br/ftp_pub/lfm/OpenGL_Transformation.pdf
https://www.ics.uci.edu/~gopi/CS211B/opengl_programming_guide_8th_edition.pdf
http://www.songho.ca/opengl/gl_projectionmatrix.html
http://devernay.free.fr/cours/opengl/materials.html
http://math.hws.edu/graphicsbook/c4/s2.html

Definition at line 975 of file torus.cpp.

References cone(), cylinder(), drawAxes(), hyperbolic_paraboloid(), hyperboloid(), initTexture(), nR1, nR2, objects, paraboloid(), R1, R2, R3, sphere(), theAxes, torus(), and zobs.

Referenced by main().

◆ initTexture()

void initTexture ( void  )

Initialize texture stuff.

Create a single texture called texName.

See also
https://gregs-blog.com/2008/01/17/opengl-texture-filter-parameters-explained/
https://open.gl/textures

Definition at line 912 of file torus.cpp.

References checkImage, checkImageHeight, checkImageWidth, makeCheckImage(), procImage, and texName.

Referenced by init(), and keyboard().

◆ keyboard()

void keyboard ( unsigned char  key,
int  x,
int  y 
)

Keyboard callback interface.

glutPostRedisplay() marks the current window as needing to be redisplayed. The next iteration through glutMainLoop, the window's display callback will be called to redisplay the window's normal plane.

Parameters
keykey pressed.
xcoordinate of the mouse cursor.
ycoordinate of the mouse cursor.
See also
https://www.opengl.org/resources/libraries/glut/spec3/node49.html

Definition at line 1289 of file torus.cpp.

References applyTexture, arcball, axis, backG, drawNormals, drawWire, ilImage, initTexture(), nObjects, OBJECT, objNames, procImage, Arcball::reset(), reshape(), rotAngle, Arcball::setViewMatrix(), showAxes, showBox, spin, texName, useArcball, xAxis, yAxis, zAxis, and zobs.

Referenced by main().

◆ loadImage()

int loadImage ( const char *  filename)

Load an image from a file.

Parameters
filenamefile name.
Returns
-1 on fail, or image name on success.
See also
http://openil.sourceforge.net/tuts/tut_4/index.htm

Definition at line 887 of file torus.cpp.

Referenced by main().

◆ main()

int main ( int  argc,
char **  argv 
)

Main program for testing.

Arguments:

  • argc - number of arguments.
  • argv[0] - Program name.
  • argv[1] - r1: Distance from the center of the tube to the center of the torus.
  • argv[2] - r2: Radius of the tube.
  • argv[3] - r3: Radius in z direction for an ellipsoid.
  • argv[4] - nr1: number of samples (divisions) on circle r1.
  • argv[5] - nr2: number of samples (divisions) on circle r2.
  • argv[6] - fname: texture file name.

Definition at line 1487 of file torus.cpp.

References _timer(), arcball, delay, display(), ilImage, init(), keyboard(), loadImage(), mouseDrag(), mouseFunc(), nR1, nR2, R1, R2, R3, and reshape().

◆ makeCheckImage()

void makeCheckImage ( void  )

Create checkerboard texture.

  • If i's 4th bit is set, i.e., every 8 iterations of i, i & 0x8 yields nonzero.
  • The same for j. If either is nonzero, but the other is zero, then XOR will yield 1.
  • This is multiplied by 255, i.e., the maximum value of a channel.
    • i -> i & 1000
    • 0-7 -> 0
    • 8-15 -> 8
    • 16-23 -> 0
    • 24-31 -> 8
    • ....
See also
https://www.tutorialspoint.com/cprogramming/c_bitwise_operators.htm
https://www.miniwebtool.com/bitwise-calculator/

Definition at line 860 of file torus.cpp.

References checkImage, checkImageHeight, and checkImageWidth.

Referenced by initTexture().

◆ mouseDrag()

void mouseDrag ( int  _x,
int  _y 
)

Mouse callback interface.

Set the motion callback for the current window. The motion callback for a window is called when the mouse moves within the window while one or more mouse buttons are pressed.

glutPostRedisplay() marks the current window as needing to be redisplayed. The next iteration through glutMainLoop, the window's display callback will be called to redisplay the window's normal plane.

Parameters
_xcoordinate of the mouse cursor.
_ycoordinate of the mouse cursor.
See also
https://www.opengl.org/resources/libraries/glut/spec3/node51.html

Definition at line 1468 of file torus.cpp.

References arcball, Arcball::updateRotation(), and useArcball.

Referenced by main().

◆ mouseFunc()

void mouseFunc ( int  state,
int  button,
int  _x,
int  _y 
)

Mouse callback interface.

glutPostRedisplay() marks the current window as needing to be redisplayed. The next iteration through glutMainLoop, the window's display callback will be called to redisplay the window's normal plane.

Parameters
statemouse button status.
buttonmouse button pressed.
_xcoordinate of the mouse cursor.
_ycoordinate of the mouse cursor.
See also
https://www.opengl.org/resources/libraries/glut/spec3/node50.html

Definition at line 1441 of file torus.cpp.

References arcball, Arcball::startRotation(), Arcball::stopRotation(), and useArcball.

Referenced by main().

◆ paraboloid()

void paraboloid ( double  h,
double  r,
int  vs,
int  rs,
int  normals 
)

Draw a paraboloid, centered at the origin, with a circular base on plane XY.

Implicit Equation:

  • \({x^{2}+y^{2}} = {c\ z},\ c = \frac{r^2}{h}\)
  • \({f(x,y,z) = \frac{h}{r^2} x^{2} + \frac{h}{r^2} y^{2} - z = 0}\)
  • \({\nabla f(x,y,z) = (2 \frac{h}{r^2} x,\ 2 \frac{h}{r^2} y,\ - 1)}\)

Parametric Equation:

  • x(u,v) = u cos(v),
  • y(u,v) = u sin(v),
  • z(u,v) = h * (u/r)², (z = h -> u = r)
  • u ∈ [0,h], v ∈ [0,2Π]
  • df/du = (cos(v), sin(v), 2*u h/r²),
  • df/dv = (-u sin(v), u cos(v) , 0)
  • df/dv x df/du = (2*u h/r² u cos(v), 2*u h/r² u sin(v), -u) = (2h/r² x(u,v), 2h/r² y(u,v), -1)

Inverse parametrization:

  • u = sqrt(z * r² / h ) or u = sqrt( (h-z) * r² / h ), if paraboloid is upside down.
  • v = atan2(y,x), if v < 0 then v += 2Π
Parameters
hparaboloid height.
rparaboloid radius.
vsnumber of divisions in z direction.
rsnumber of angular divisions.
normalswhether to draw normals or polygons.
See also
http://mathworld.wolfram.com/Paraboloid.html
https://en.wikipedia.org/wiki/Quadric
http://www.geom.uiuc.edu/docs/reference/CRC-formulas/node61.html

Definition at line 387 of file torus.cpp.

References _paraboloid, clamp, getBoxSize(), LEN, scale, TWOPI, and updateBox().

Referenced by init().

◆ reshape()

void reshape ( int  w,
int  h 
)

Handle window resize.

Sets the viewport to its new size.

Parameters
wviewport width.
hviewport height.

Definition at line 1254 of file torus.cpp.

References arcball, farPlane, frontPlane, getBoxSize(), OBJECT, Arcball::setWidthHeight(), toDeg, toRad, viewingAngle, and zobs.

Referenced by keyboard(), and main().

◆ sphere()

void sphere ( double  r1,
double  r2,
double  r3,
int  numc,
int  numt,
int  normals 
)

Draw an ellipsoid or a sphere centered at the origin.

Implicit Equation:

  • \({x^{2} \over a^{2}}+{y^{2} \over b^{2}}+{z^{2} \over c^{2}}=1,\ a= r_1,\ b = r_2,\ c = r_3\)
  • \({f(x,y,z) = r_2^2 r_3^2\ x^{2}+r_1^2 r_3^2\ y^{2}+r_1^2 r_2^2\ z^{2}} - (r_1 r_2 r_3)^2 = 0\)
  • \({\nabla f(x,y,z) = (r_2^2 r_3^2\ x,\ r_1^2 r_3^2\ y,\ r_1^2 r_2^2\ z)}\)

A sphere is defined as the set of all points in three-dimensional Euclidean space R³ that are located at a distance r (the "radius") from a given point (the "center").

An ellipsoid is a surface that may be obtained from a sphere by deforming it by means of directional scalings, or more generally, of an affine transformation.

Parametric Equation:

  • x(u,v) = r1 cos(u) sin(v),
  • y(u,v) = r2 sin(u) sin(v),
  • z(u,v) = r3 cos(v),
  • u ∈ [0,2Π], v ∈ [0,Π]
  • df/du = (-r1 sin(u) sin(v), r2 cos(u) sin(v), 0),
  • df/dv = (r1 cos(u) cos(v), r2 sin(u) cos (v), -r3 sin(v))
  • df/dv x df/du = (r2 r3/r1 x(u,v), r1 r3/r2 y(u,v), r1 r2/r3 z(u,v)) sin v = (r2² r3² x(u,v), r1² r3² y(u,v), r1² r2² z(u,v))

Inverse parametrization:

  • u = atan2((y,x) * (r1/r2)), if u < 0 then u += 2Π
  • v = acos(z/r3)
Parameters
r1x radius of the ellipsoid.
r2y radius of the ellipsoid.
r3z radius of the ellipsoid.
numcnumber of divisions along the zero degree longitude (number of latitude divisions).
numtnumber of divisions along the zero degree latitude - equator (number of longitude divisions).
normalswhether to draw normals or polygons.
See also
http://mathworld.wolfram.com/Ellipsoid.html
http://mathworld.wolfram.com/Sphere.html
https://en.wikipedia.org/wiki/Ellipsoid
https://sciencing.com/equators-latitude-6314100.html

Definition at line 722 of file torus.cpp.

References _ellipsoid, clamp, getBoxSize(), LEN, scale, TWOPI, and updateBox().

Referenced by init().

◆ torus()

void torus ( double  r1,
double  r2,
int  numc,
int  numt,
int  normals 
)

Draw a torus, centered at the origin, and azimuthally symmetric about the z-axis.

In geometry, a torus is a surface of revolution generated by revolving a circle in three-dimensional space about an axis coplanar with the circle.

  • Volume: 2 × π² × R × r²
  • Surface area: 4 × π² × R × r
  • Common objects with this shape: Doughnut, Ring, Lifebuoy

Implicit Equation:

  • \((r_1-\sqrt{(x^2+y^2)})^2+z^2=r_2^2,\)
  • \(f(x,y,z) = (x^2+y^2+z^2+r_1^2-r_2^2)^2 - 4 r_1^2 (x^2+y^2) = 0\), a quartic equation

Parametric Equation:

  • x(u,v) = (r1 + r2 cos(v)) cos(u),
  • y(u,v) = (r1 + r2 cos(v)) sin(u),
  • z(u,v) = r2 sin(v),
  • u ∈ [0,2Π], v ∈ [0,2Π]
  • df/du = (-y(u,v), x(u,v), 0),
  • df/dv = (-z(u,v) cos(u), -z(u,v) sin(u), r2 cos(v))
  • df/du x df/dv = (r2 x(u,v) cos(v), r2 y(u,v) cos(v), z(u,v) (y(u,v) sin(u) + x(u,v) (cos(u))) = (r2 cos(v) x(u,v), r2 cos(v) y(u,v), (r1 + r2 cos(v)) z(u,v))
  • df/du x df/dv = (cos(v) cos(u), cos(v) sin(u), sin(v))

Inverse parametrization:

  • u = atan2(y,x), if u < 0 then u += 2Π
  • v = asin(z/r2)

When:

  • r1 > r2, the surface will be the familiar ring torus.
  • r1 = r2 corresponds to the horn torus, which in effect is a torus with no "hole".
  • r1 < r2 describes the self-intersecting spindle torus.
  • r1 = 0, the torus degenerates to the sphere.
Parameters
r1distance from the center of the tube to the center of the torus.
r2radius of the tube.
numcnumber of divisions of the circle with radius r2.
numtnumber of divisions of the circle with radius r1.
normalswhether to draw normals or polygons.
See also
http://mathworld.wolfram.com/Torus.html
https://en.wikipedia.org/wiki/Torus
https://blogs.scientificamerican.com/roots-of-unity/a-few-of-my-favorite-spaces-the-torus/
https://www.mathsisfun.com/geometry/torus.html
http://trecs.se/torus.php
http://mathworld.wolfram.com/QuarticEquation.html

Definition at line 622 of file torus.cpp.

References _torus, clamp, getBoxSize(), LEN, scale, TWOPI, and updateBox().

Referenced by init().

◆ updateBox()

void updateBox ( oType  index,
double  x,
double  y,
double  z 
)

Add a new point to a bounding box.

The axis-aligned minimum bounding box (or AABB) for a given point set is its minimum bounding box subject to the constraint that the edges of the box are parallel to the (Cartesian) coordinate axes. It is simply the Cartesian product of N intervals each of which is defined by the minimal and maximal value of the corresponding coordinate for the points in S.

Axis-aligned minimal bounding boxes are used to an approximate location of an object in question and as a very simple descriptor of its shape. For example, in computational geometry and its applications when it is required to find intersections in the set of objects, the initial check is the intersections between their MBBs. Since it is usually a much less expensive operation than the check of the actual intersection (because it only requires comparisons of coordinates), it allows to quickly exclude from checks the pairs that are far apart.

Parameters
indexPosition into objBoxes array.
xPoint coordinate.
yPoint coordinate.
zPoint coordinate.
See also
https://en.wikipedia.org/wiki/Minimum_bounding_box

Definition at line 180 of file torus.cpp.

References objBoxes.

Referenced by cone(), cylinder(), hyperbolic_paraboloid(), hyperboloid(), paraboloid(), sphere(), and torus().

Variable Documentation

◆ applyTexture

int applyTexture = 0

Whether to apply texture.

Definition at line 110 of file torus.cpp.

Referenced by display(), and keyboard().

◆ arcball

Arcball arcball

ArcBall.

Definition at line 158 of file torus.cpp.

Referenced by display(), keyboard(), main(), mouseDrag(), mouseFunc(), and reshape().

◆ axis

rAxes axis = xAxis

Current axis.

Definition at line 137 of file torus.cpp.

Referenced by display(), and keyboard().

◆ backG

int backG = 0

Toggle background color.

Definition at line 116 of file torus.cpp.

Referenced by keyboard().

◆ checkImage

GLubyte checkImage[checkImageHeight][checkImageWidth][4]

Procedural image for a checkerboard.

Definition at line 146 of file torus.cpp.

Referenced by initTexture(), and makeCheckImage().

◆ delay

unsigned int delay = 60

Controls the trigger of the timer function.

Definition at line 143 of file torus.cpp.

Referenced by _timer(), and main().

◆ drawNormals

int drawNormals = 0

Whether display vertex normals.

Definition at line 108 of file torus.cpp.

Referenced by display(), and keyboard().

◆ drawWire

int drawWire = 0

Whether draw wireframe or filled polygons.

Definition at line 106 of file torus.cpp.

Referenced by display(), and keyboard().

◆ ilImage

ILuint ilImage

IL image identifier.

Definition at line 150 of file torus.cpp.

Referenced by keyboard(), and main().

◆ nR1

int nR1 = 25

Number of samples.

Definition at line 102 of file torus.cpp.

Referenced by init(), and main().

◆ nR2

int nR2 = 10

Number of samples.

Definition at line 104 of file torus.cpp.

Referenced by init(), and main().

◆ objBoxes

_BBOX objBoxes[nObjects]

Object boxes.

Definition at line 154 of file torus.cpp.

Referenced by display(), getBoxSize(), and updateBox().

◆ OBJECT

int OBJECT = _torus

Current object.

Definition at line 156 of file torus.cpp.

Referenced by display(), keyboard(), and reshape().

◆ objects

GLuint objects

Object display list base index.

Definition at line 94 of file torus.cpp.

Referenced by display(), and init().

◆ objNames

const char* objNames[] = {"Torus", "Cylinder", "Cone", "Ellipsoid", "Paraboloid", "Hyperbolic Paraboloid", "Hyperboloid"}

Object names.

Definition at line 152 of file torus.cpp.

Referenced by keyboard().

◆ procImage

int procImage = 1

Whether to use a procedural texture.

Definition at line 118 of file torus.cpp.

Referenced by initTexture(), and keyboard().

◆ R1

double R1 = 1.0

Distance from the center of the tube to the center of the torus.

Definition at line 96 of file torus.cpp.

Referenced by init(), and main().

◆ R2

double R2 = 0.3

Radius of the tube.

Definition at line 98 of file torus.cpp.

Referenced by init(), and main().

◆ R3

double R3 = 0.5

Radius in z direction for an ellipsoid.

Definition at line 100 of file torus.cpp.

Referenced by init(), and main().

◆ rotAngle

GLfloat rotAngle = 2.0

Rotation angle increment.

Definition at line 140 of file torus.cpp.

Referenced by display(), and keyboard().

◆ showAxes

int showAxes = 0

Whether to show the coordinate axes.

Definition at line 112 of file torus.cpp.

Referenced by display(), and keyboard().

◆ showBox

int showBox = 0

Whether to show the bounding boxes.

Definition at line 114 of file torus.cpp.

Referenced by display(), and keyboard().

◆ spin

int spin = 1

Automatic spin.

Definition at line 122 of file torus.cpp.

Referenced by display(), and keyboard().

◆ texName

GLuint texName

Texture identifier.

Definition at line 148 of file torus.cpp.

Referenced by initTexture(), and keyboard().

◆ useArcball

int useArcball = 0

Whether to use the arcball paradigm.

Definition at line 120 of file torus.cpp.

Referenced by display(), keyboard(), mouseDrag(), and mouseFunc().

◆ viewingAngle

double viewingAngle = 45

Camera viewing angle, also known as opening angle.

Definition at line 92 of file torus.cpp.

Referenced by reshape().

◆ zobs

double zobs = 10.0

Camera position.

Definition at line 90 of file torus.cpp.

Referenced by display(), init(), keyboard(), and reshape().