1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
| #include <stdio.h>
#include <stdlib.h>
#if defined(WIN32)
# include <windows.h>
#endif
#if defined(WIN32) || defined(linux)
# include <GL/glut.h>
#elif defined(__APPLE__)
# include <GLUT/glut.h>
#endif
// Header files for OpenHaptics.
#include <HL/hl.h>
#include <HDU/hduError.h>
// id needed for haptic shape.
HLuint gMyShapeId;
void display(void)
{
// Start a haptic frame.
hlBeginFrame();
// Start the haptic shape.
/* hlBeginShape(HL_SHAPE_DEPTH_BUFFER, gMyShapeId);
GLfloat ctrlpoints[4][3] = {
{ -4.0, -4.0, 0.0}, { -2.0, 4.0, 0.0},
{2.0, -4.0, 0.0}, {4.0, 4.0, 0.0}};*/
typedef struct point_3d { // Structure for a 3-dimensional point (NEW)
double x, y, z;
} POINT_3D;
typedef struct bpatch { // Structure for a 3rd degree bezier patch (NEW)
POINT_3D anchors[4][4]; // 4x4 grid of anchor points
GLuint dlBPatch; // Display List for Bezier Patch
GLuint texture; // Texture for the patch
}
BEZIER_PATCH;
HDC hDC=NULL; // Private GDI Device Context
HGLRC hRC=NULL; // Permanent Rendering Context
HWND hWnd=NULL; // Holds Our Window Handle
HINSTANCE hInstance; // Holds The Instance Of The Application
DEVMODE DMsaved; // Saves the previous screen settings (NEW)
//bool keys[256]; // Array Used For The Keyboard Routine
//bool active=TRUE; // Window Active Flag Set To TRUE By Default
//bool fullscreen=TRUE; // Fullscreen Flag Set To Fullscreen Mode By Default
GLfloat rotz = 0.0f; // Rotation about the Z axis
BEZIER_PATCH mybezier; // The bezier patch we're going to use (NEW)
BOOL showCPoints=TRUE; // Toggles displaying the control point grid (NEW)
int divs = 7; // Number of intrapolations (conrols poly resolution) (NEW)
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc
// Adds 2 points. Don't just use '+' ;)
POINT_3D pointAdd(POINT_3D p,POINT_3D q) {
p.x += q.x; p.y += q.y; p.z += q.z;
return p;
}
// Multiplies a point and a constant. Don't just use '*'
POINT_3D pointTimes(POINT_3D c, POINT_3D p) {
p.x *= c; p.y *= c; p.z *= c;
return p;
}
// Function for quick point creation
POINT_3D makePoint(double a, double b, double c) {
POINT_3D p;
p.x = a; p.y = b; p.z = c;
return p;
}
// Calculates 3rd degree polynomial based on array of 4 points
// and a single variable (u) which is generally between 0 and 1
POINT_3D Bernstein(float u, POINT_3D *p) {
POINT_3D a, b, c, d, r;
a = pointTimes(pow(u,3), p[0]);
b = pointTimes(3*pow(u,2)*(1-u), p[1]);
c = pointTimes(3*u*pow((1-u),2), p[2]);
d = pointTimes(pow((1-u),3), p[3]);
r = pointAdd(pointAdd(a, b), pointAdd(c, d));
return r;
}
glEnd();
// End the haptic shape.
hlEndShape();
//End the haptic frame.
hlEndFrame();
}
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
// Enable depth buffering to provide depth information for OpenHaptics.
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
// OpenHaptics setup follows:
// Create a haptic device instance.
HDErrorInfo error;
HHD hHD = hdInitDevice(HD_DEFAULT_DEVICE);
if (HD_DEVICE_ERROR(error = hdGetError()))
{
hduPrintError(stderr, &error, "Failed to initialize haptic device");
fprintf(stderr, "Press any key to exit");
getchar();
exit(-1);
}
if (HD_SUCCESS != hdGetError().errorCode)
{
fprintf(stderr, "Erorr initializing haptic device.\nPress any key to exit");
getchar();
exit(-1);
}
// Create a haptic rendering context and activate it.
HHLRC hHLRC = hlCreateContext(hHD);
hlMakeCurrent(hHLRC);
// Reserve an id for the shape
gMyShapeId = hlGenShapes(1);
// Specify the boundaries for the workspace of the haptic device
// in millimeters in the cordinates of the haptic device.
// The haptics engine will map the view volume to this workspace
hlWorkspace (-80, -80, -70, 80, 80, 20);
// Specify the haptic view volume (in this case it will be
// the same as the graphic view volume).
hlMatrixMode(HL_TOUCHWORKSPACE);
hlLoadIdentity();
hlOrtho (0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
void glutMenu(int ID)
{
switch(ID) {
case 0:
exit(0);
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(250, 250);
glutInitWindowPosition(100, 100);
glutCreateWindow("Hello Haptics");
glutCreateMenu(glutMenu);
glutAddMenuEntry("Quit", 0);
glutAttachMenu(GLUT_RIGHT_BUTTON);
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
} |
Partager