![]() |
LCG_cg 1.0
|
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>
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. | |
#define | checkImageWidth 128 |
Texture Image width. | |
#define | checkImageHeight 128 |
Texture Image height. | |
#define | farPlane (zobs+10*d) |
Far plane distance. | |
#define | frontPlane 1 |
Front plane distance. | |
#define | posAng(x) ((x) < 0.0 ? ((x)+TWOPI) : (x)) |
Return a positive angle. | |
#define | TWOPI (2*M_PI) |
Two PI. | |
#define | toRad(x) ((x)*M_PI/180.0) |
Convert an angle to radians. | |
#define | toDeg(x) ((x)*180.0/M_PI) |
Convert an angle to degrees. | |
#define | clamp(x) (fmin(fmax((x),0),1)) |
Clamp to [0,1]. | |
#define | LEN(x, y, z) (sqrt((x)*(x)+(y)*(y)+(z)*(z))) |
Vector length. | |
#define | theAxes (2*nObjects) |
Axes. | |
#define | nObjects (sizeof(objNames)/sizeof(char*)) |
Number of objects;. | |
#define | scale (0.05*diag) |
Scale for drawing normals. | |
Typedefs | |
typedef struct _Point | Point |
3D point structure. | |
typedef Point | _BBOX[2] |
Axis aligned Bounding box. | |
typedef enum _oType | oType |
Quadric types. | |
typedef enum _rAxes | rAxes |
Rotation axes. | |
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. | |
double | getBoxSize (oType index, double *dx, double *dy, double *dz) |
Return bounding box dimension. | |
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. | |
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. | |
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. | |
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. | |
void | hyperbolic_paraboloid (double w, double h, int ws, int hs, int normals) |
Draw a hyperbolic paraboloid. | |
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. | |
void | sphere (double r1, double r2, double r3, int numc, int numt, int normals) |
Draw an ellipsoid or a sphere centered at the origin. | |
void | drawAxes (double r1, double r2) |
Draw the three coordinate axes. | |
void | drawBox (const _BBOX BBOX) |
Draw the bounding box as a dashed hexahedron. | |
void | makeCheckImage (void) |
Create checkerboard texture. | |
int | loadImage (const char *filename) |
Load an image from a file. | |
void | initTexture (void) |
Initialize texture stuff. | |
void | init (void) |
Initialize OpenGL state. | |
void | display (void) |
Clear window, reset depth buffer and draw torus. | |
int | dateNow () |
Return the current Coordinated Universal Time (UTC) in miliseconds. | |
void | _timer (int value) |
Timer callback function to be triggered in at least delay milliseconds. | |
void | reshape (int w, int h) |
Handle window resize. | |
void | keyboard (unsigned char key, int x, int y) |
Keyboard callback interface. | |
void | mouseFunc (int state, int button, int _x, int _y) |
Mouse callback interface. | |
void | mouseDrag (int _x, int _y) |
Mouse callback interface. | |
int | main (int argc, char **argv) |
Main program for testing. | |
Variables | |
double | zobs = 10.0 |
Camera position. | |
double | viewingAngle = 45 |
Camera viewing angle, also known as opening angle. | |
GLuint | objects |
Object display list base index. | |
double | R1 = 1.0 |
Distance from the center of the tube to the center of the torus. | |
double | R2 = 0.3 |
Radius of the tube. | |
double | R3 = 0.5 |
Radius in z direction for an ellipsoid. | |
int | nR1 = 25 |
Number of samples. | |
int | nR2 = 10 |
Number of samples. | |
int | drawWire = 0 |
Whether draw wireframe or filled polygons. | |
int | drawNormals = 0 |
Whether display vertex normals. | |
int | applyTexture = 0 |
Whether to apply texture. | |
int | showAxes = 0 |
Whether to show the coordinate axes. | |
int | showBox = 0 |
Whether to show the bounding boxes. | |
int | backG = 0 |
Toggle background color. | |
int | procImage = 1 |
Whether to use a procedural texture. | |
int | useArcball = 0 |
Whether to use the arcball paradigm. | |
int | spin = 1 |
Automatic spin. | |
rAxes | axis = xAxis |
Current axis. | |
GLfloat | rotAngle = 2.0 |
Rotation angle increment. | |
unsigned int | delay = 60 |
Controls the trigger of the timer function. | |
GLubyte | checkImage [checkImageHeight][checkImageWidth][4] |
Procedural image for a checkerboard. | |
GLuint | texName |
Texture identifier. | |
ILuint | ilImage |
IL image identifier. | |
const char * | objNames [] = {"Torus", "Cylinder", "Cone", "Ellipsoid", "Paraboloid", "Hyperbolic Paraboloid", "Hyperboloid"} |
Object names. | |
_BBOX | objBoxes [nObjects] |
Object boxes. | |
int | OBJECT = _torus |
Current object. | |
Arcball | arcball |
ArcBall. | |
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.
- torus.osx R r r' nR nr - torus.osx 1.0 0.3 0.5 25 10
Definition in file torus.cpp.
#define farPlane (zobs+10*d) |
#define LEN | ( | x, | |
y, | |||
z | |||
) | (sqrt((x)*(x)+(y)*(y)+(z)*(z))) |
#define M_PI_ 3.14159265358979323846 |
#define nObjects (sizeof(objNames)/sizeof(char*)) |
#define posAng | ( | x | ) | ((x) < 0.0 ? ((x)+TWOPI) : (x)) |
#define toDeg | ( | x | ) | ((x)*180.0/M_PI) |
#define toRad | ( | x | ) | ((x)*M_PI/180.0) |
enum _oType |
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.
value | a parameter passed to this callback function. |
Definition at line 1219 of file torus.cpp.
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:
Parametric Equation:
Inverse parametrization:
Note: atan2 returns values ∈ [-Π, Π]
h | cone height. |
r | cone base radius. |
vs | number of divisions in z direction. |
rs | number of angular divisions. |
normals | whether to draw normals or polygons. |
Definition at line 242 of file torus.cpp.
References _cone, clamp, getBoxSize(), LEN, scale, TWOPI, and updateBox().
Referenced by init().
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:
Parametric Equation:
Inverse parametrization:
h | cylinder height. |
r | cylinder radius. |
vs | number of divisions in z direction. |
rs | number of angular divisions. |
normals | whether to draw normals or polygons. |
Definition at line 313 of file torus.cpp.
References _cylinder, clamp, getBoxSize(), LEN, scale, TWOPI, and updateBox().
Referenced by init().
int dateNow | ( | ) |
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 applyTexture, arcball, axis, drawBox(), drawNormals, drawWire, nObjects, objBoxes, OBJECT, objects, rotAngle, showAxes, showBox, spin, theAxes, useArcball, xAxis, yAxis, zAxis, and zobs.
Referenced by main().
void drawAxes | ( | double | r1, |
double | r2 | ||
) |
void drawBox | ( | const _BBOX | BBOX | ) |
double getBoxSize | ( | oType | index, |
double * | dx, | ||
double * | dy, | ||
double * | dz | ||
) |
Return bounding box dimension.
index | Position into objBoxes array. |
dx | Bounding box length. |
dy | Bounding box width. |
dz | Bounding box height. |
Definition at line 199 of file torus.cpp.
Referenced by cone(), cylinder(), hyperbolic_paraboloid(), hyperboloid(), paraboloid(), reshape(), sphere(), and torus().
void hyperbolic_paraboloid | ( | double | w, |
double | h, | ||
int | ws, | ||
int | hs, | ||
int | normals | ||
) |
Draw a hyperbolic paraboloid.
Implicit Equation:
Parametric Equation:
Inverse parametrization:
w | hyperbolic paraboloid width. |
h | hyperbolic paraboloid length. |
ws | number of divisions in x direction. |
hs | number of divisions in y direction. |
normals | whether to draw normals or polygons. |
Definition at line 539 of file torus.cpp.
References _hyperbolic_paraboloid, getBoxSize(), LEN, scale, and updateBox().
Referenced by init().
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:
Parametric Equation:
Inverse parametrization:
h | hyperboloid height. |
a | hyperboloid skirt radius. |
r | hyperboloid base radius. |
vs | number of divisions in z direction. |
rs | number of angular divisions. |
normals | whether to draw normals or polygons. |
Definition at line 463 of file torus.cpp.
References _hyperboloid, clamp, getBoxSize(), LEN, scale, TWOPI, and updateBox().
Referenced by 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.
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().
void initTexture | ( | void | ) |
Initialize texture stuff.
Create a single texture called texName.
Definition at line 912 of file torus.cpp.
References checkImage, checkImageHeight, checkImageWidth, makeCheckImage(), procImage, and texName.
Referenced by init(), and 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.
key | key pressed. |
x | coordinate of the mouse cursor. |
y | coordinate of the mouse cursor. |
Definition at line 1289 of file torus.cpp.
References applyTexture, arcball, axis, backG, drawNormals, drawWire, ilImage, initTexture(), nObjects, OBJECT, objNames, procImage, reshape(), rotAngle, showAxes, showBox, spin, texName, useArcball, xAxis, yAxis, zAxis, and zobs.
Referenced by main().
int loadImage | ( | const char * | filename | ) |
int main | ( | int | argc, |
char ** | argv | ||
) |
Main program for testing.
Arguments:
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().
void makeCheckImage | ( | void | ) |
Create checkerboard texture.
- i -> i & 1000 - 0-7 -> 0 - 8-15 -> 8 - 16-23 -> 0 - 24-31 -> 8 - ....
Definition at line 860 of file torus.cpp.
References checkImage, checkImageHeight, and checkImageWidth.
Referenced by initTexture().
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.
_x | coordinate of the mouse cursor. |
_y | coordinate of the mouse cursor. |
Definition at line 1468 of file torus.cpp.
References arcball, and useArcball.
Referenced by main().
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.
state | mouse button status. |
button | mouse button pressed. |
_x | coordinate of the mouse cursor. |
_y | coordinate of the mouse cursor. |
Definition at line 1441 of file torus.cpp.
References arcball, and useArcball.
Referenced by main().
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:
Parametric Equation:
Inverse parametrization:
h | paraboloid height. |
r | paraboloid radius. |
vs | number of divisions in z direction. |
rs | number of angular divisions. |
normals | whether to draw normals or polygons. |
Definition at line 387 of file torus.cpp.
References _paraboloid, clamp, getBoxSize(), LEN, scale, TWOPI, and updateBox().
Referenced by init().
void reshape | ( | int | w, |
int | h | ||
) |
Handle window resize.
Sets the viewport to its new size.
w | viewport width. |
h | viewport height. |
Definition at line 1254 of file torus.cpp.
References arcball, farPlane, frontPlane, getBoxSize(), OBJECT, toDeg, toRad, viewingAngle, and zobs.
Referenced by keyboard(), and main().
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:
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:
Inverse parametrization:
r1 | x radius of the ellipsoid. |
r2 | y radius of the ellipsoid. |
r3 | z radius of the ellipsoid. |
numc | number of divisions along the zero degree longitude (number of latitude divisions). |
numt | number of divisions along the zero degree latitude - equator (number of longitude divisions). |
normals | whether to draw normals or polygons. |
Definition at line 722 of file torus.cpp.
References _ellipsoid, clamp, getBoxSize(), LEN, scale, TWOPI, and updateBox().
Referenced by init().
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.
Implicit Equation:
Parametric Equation:
Inverse parametrization:
When:
r1 | distance from the center of the tube to the center of the torus. |
r2 | radius of the tube. |
numc | number of divisions of the circle with radius r2. |
numt | number of divisions of the circle with radius r1. |
normals | whether to draw normals or polygons. |
Definition at line 622 of file torus.cpp.
References _torus, clamp, getBoxSize(), LEN, scale, TWOPI, and updateBox().
Referenced by init().
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.
index | Position into objBoxes array. |
x | Point coordinate. |
y | Point coordinate. |
z | Point coordinate. |
Definition at line 180 of file torus.cpp.
References objBoxes.
Referenced by cone(), cylinder(), hyperbolic_paraboloid(), hyperboloid(), paraboloid(), sphere(), and torus().
int applyTexture = 0 |
Whether to apply texture.
Definition at line 110 of file torus.cpp.
Referenced by display(), and keyboard().
Arcball arcball |
ArcBall.
Definition at line 158 of file torus.cpp.
Referenced by display(), keyboard(), main(), mouseDrag(), mouseFunc(), and reshape().
int backG = 0 |
GLubyte checkImage[checkImageHeight][checkImageWidth][4] |
Procedural image for a checkerboard.
Definition at line 146 of file torus.cpp.
Referenced by initTexture(), and makeCheckImage().
unsigned int delay = 60 |
int drawNormals = 0 |
Whether display vertex normals.
Definition at line 108 of file torus.cpp.
Referenced by display(), and keyboard().
int drawWire = 0 |
Whether draw wireframe or filled polygons.
Definition at line 106 of file torus.cpp.
Referenced by display(), and keyboard().
ILuint ilImage |
IL image identifier.
Definition at line 150 of file torus.cpp.
Referenced by keyboard(), and main().
int nR1 = 25 |
int nR2 = 10 |
Object boxes.
Definition at line 154 of file torus.cpp.
Referenced by display(), getBoxSize(), and updateBox().
int OBJECT = _torus |
Current object.
Definition at line 156 of file torus.cpp.
Referenced by display(), keyboard(), and reshape().
GLuint objects |
const char* objNames[] = {"Torus", "Cylinder", "Cone", "Ellipsoid", "Paraboloid", "Hyperbolic Paraboloid", "Hyperboloid"} |
int procImage = 1 |
Whether to use a procedural texture.
Definition at line 118 of file torus.cpp.
Referenced by initTexture(), and keyboard().
double R1 = 1.0 |
double R2 = 0.3 |
double R3 = 0.5 |
GLfloat rotAngle = 2.0 |
Rotation angle increment.
Definition at line 140 of file torus.cpp.
Referenced by display(), and keyboard().
int showAxes = 0 |
Whether to show the coordinate axes.
Definition at line 112 of file torus.cpp.
Referenced by display(), and keyboard().
int showBox = 0 |
Whether to show the bounding boxes.
Definition at line 114 of file torus.cpp.
Referenced by display(), and keyboard().
int spin = 1 |
GLuint texName |
Texture identifier.
Definition at line 148 of file torus.cpp.
Referenced by initTexture(), and keyboard().
int useArcball = 0 |
Whether to use the arcball paradigm.
Definition at line 120 of file torus.cpp.
Referenced by display(), keyboard(), mouseDrag(), and mouseFunc().
double viewingAngle = 45 |