-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.cpp
More file actions
326 lines (281 loc) · 9.29 KB
/
Source.cpp
File metadata and controls
326 lines (281 loc) · 9.29 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// ============================================================================
// Includes
// ============================================================================
#include "physSimu.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h> // malloc(), free()
#ifdef __APPLE__
# include <GLUT/glut.h>
#else
# include <GL/glut.h>
#endif
#include <AR/config.h>
#include <AR/video.h>
#include <AR/param.h> // arParamDisp()
#include <AR/ar.h>
#include <AR/gsub_lite.h>
// ============================================================================
// Constants
// ============================================================================
#define VIEW_SCALEFACTOR 0.025 // 1.0 ARToolKit unit becomes 0.025 of my OpenGL units.
#define VIEW_DISTANCE_MIN 0.1 // Objects closer to the camera than this will not be displayed.
#define VIEW_DISTANCE_MAX 100.0 // Objects further away from the camera than this will not be displayed.
// ============================================================================
// Global variables
// ============================================================================
// Preferences.
static int prefWindowed = TRUE;
static int prefWidth = 640; // Fullscreen mode width.
static int prefHeight = 480; // Fullscreen mode height.
static int prefDepth = 32; // Fullscreen mode bit depth.
static int prefRefresh = 0; // Fullscreen mode refresh rate. Set to 0 to use default rate.
// Image acquisition.
static ARUint8 *gARTImage = NULL;
// Marker detection.
static int gARTThreshhold = 100;
static long gCallCountMarkerDetect = 0;
// Transformation matrix retrieval.
static double gPatt_width = 80.0; // Per-marker, but we are using only 1 marker.
static double gPatt_centre[2] = {0.0, 0.0}; // Per-marker, but we are using only 1 marker.
static double gPatt_trans[3][4]; // Per-marker, but we are using only 1 marker.
static int gPatt_found = FALSE; // Per-marker, but we are using only 1 marker.
static int gPatt_id; // Per-marker, but we are using only 1 marker.
// Drawing.
static ARParam gARTCparam;
static ARGL_CONTEXT_SETTINGS_REF gArglSettings = NULL;
static int gDrawRotate = FALSE;
static float gDrawRotateAngle = 0; // For use in drawing.
// ============================================================================
// Functions
// ============================================================================
static int setupCamera(const char *cparam_name, char *vconf, ARParam *cparam)
{
ARParam wparam;
int xsize, ysize;
// Open the video path.
if (arVideoOpen(vconf) < 0) {
fprintf(stderr, "setupCamera(): Unable to open connection to camera.\n");
return (FALSE);
}
// Find the size of the window.
if (arVideoInqSize(&xsize, &ysize) < 0) return (FALSE);
fprintf(stdout, "Camera image size (x,y) = (%d,%d)\n", xsize, ysize);
// Load the camera parameters, resize for the window and init.
if (arParamLoad(cparam_name, 1, &wparam) < 0) {
fprintf(stderr, "setupCamera(): Error loading parameter file %s for camera.\n", cparam_name);
return (FALSE);
}
arParamChangeSize(&wparam, xsize, ysize, cparam);
fprintf(stdout, "*** Camera Parameter ***\n");
arParamDisp(cparam);
arInitCparam(cparam);
if (arVideoCapStart() != 0) {
fprintf(stderr, "setupCamera(): Unable to begin camera data capture.\n");
return (FALSE);
}
return (TRUE);
}
static int setupMarker(const char *patt_name, int *patt_id)
{
if((*patt_id = arLoadPatt(patt_name)) < 0) {
fprintf(stderr, "setupMarker(): pattern load error !!\n");
return (FALSE);
}
return (TRUE);
}
static void Quit(void)
{
arglCleanup(gArglSettings);
arVideoCapStop();
arVideoClose();
exit(0);
}
static void Idle(void)
{
static int ms_prev;
int ms;
float s_elapsed;
ARUint8 *image;
ARMarkerInfo *marker_info; // Pointer to array holding the details of detected markers.
int marker_num; // Count of number of markers detected.
int j, k;
// Find out how long since Idle() last ran.
ms = glutGet(GLUT_ELAPSED_TIME);
s_elapsed = (float)(ms - ms_prev) * 0.001;
if (s_elapsed < 0.01f) return; // Don't update more often than 100 Hz.
ms_prev = ms;
// Update drawing.
// Grab a video frame.
if ((image = arVideoGetImage()) != NULL) {
gARTImage = image; // Save the fetched image.
gPatt_found = FALSE; // Invalidate any previous detected markers.
gCallCountMarkerDetect++; // Increment ARToolKit FPS counter.
// Detect the markers in the video frame.
if (arDetectMarker(gARTImage, gARTThreshhold, &marker_info, &marker_num) < 0) {
exit(-1);
}
printf("marker_num: %d\n", marker_num);
// Check through the marker_info array for highest confidence
// visible marker matching our preferred pattern.
k = -1;
for (j = 0; j < marker_num; j++) {
if (marker_info[j].id == gPatt_id) {
if (k == -1) k = j; // First marker detected.
else if(marker_info[j].cf > marker_info[k].cf) k = j; // Higher confidence marker detected.
}
}
if (k != -1) {
// Get the transformation between the marker and the real camera into gPatt_trans.
arGetTransMat(&(marker_info[k]), gPatt_centre, gPatt_width, gPatt_trans);
gPatt_found = TRUE;
}
// Tell GLUT the display has changed.
glutPostRedisplay();
}
}
//
// This function is called on events when the visibility of the
// GLUT window changes (including when it first becomes visible).
//
static void Visibility(int visible)
{
if (visible == GLUT_VISIBLE) {
glutIdleFunc(Idle);
} else {
glutIdleFunc(NULL);
}
}
//
// This function is called when the
// GLUT window is resized.
//
static void Reshape(int w, int h)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Call through to anyone else who needs to know about window sizing here.
}
//
// This function is called when the window needs redrawing.
//
static void Display(void)
{
GLdouble p[16];
GLdouble m[16];
// Select correct buffer for this context.
glDrawBuffer(GL_BACK);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the buffers for new frame.
arglDispImage(gARTImage, &gARTCparam, 1.0, gArglSettings); // zoom = 1.0.
arVideoCapNext();
gARTImage = NULL; // Image data is no longer valid after calling arVideoCapNext().
if (gPatt_found) {
// Projection transformation.
arglCameraFrustumRH(&gARTCparam, VIEW_DISTANCE_MIN, VIEW_DISTANCE_MAX, p);
glMatrixMode(GL_PROJECTION);
glLoadMatrixd(p);
glMatrixMode(GL_MODELVIEW);
// Viewing transformation.
glLoadIdentity();
// Lighting and geometry that moves with the camera should go here.
// (I.e. must be specified before viewing transformations.)
//none
// ARToolKit supplied distance in millimetres, but I want OpenGL to work in my units.
arglCameraViewRH(gPatt_trans, m, VIEW_SCALEFACTOR);
glLoadMatrixd(m);
// All other lighting and geometry goes here.
/*
glTranslatef(0.0, 0.0, 0.9);
static double angle=0;
glRotatef(90.0, 1.0, 0.0, 0.0);
glRotatef(angle, 0.0, 1.0, 0.0);
angle=angle+2.5;
glColor3f(1.0, 0.0, 0.0);
*/
glutWireTeapot(1.0);
} // gPatt_found
// Any 2D overlays go here.
//none
glutSwapBuffers();
}
int main(int argc, char** argv)
{
// char glutGamemode[32];
// const char *cparam_name = "Data/camera_para.dat";
// //
// // Camera configuration.
// //
//#ifdef _WIN32
// char *vconf = "Data\\WDM_camera_flipV.xml";
//#else
// char *vconf = "";
//#endif
// const char *patt_name = "Data/patt.hiro";
//
// // ----------------------------------------------------------------------------
// // Library inits.
// //
//
// glutInit(&argc, argv);
//
// // ----------------------------------------------------------------------------
// // Hardware setup.
// //
//
// if (!setupCamera(cparam_name, vconf, &gARTCparam)) {
// fprintf(stderr, "main(): Unable to set up AR camera.\n");
// exit(-1);
// }
// if (!setupMarker(patt_name, &gPatt_id)) {
// fprintf(stderr, "main(): Unable to set up AR marker.\n");
// exit(-1);
// }
//
// // ----------------------------------------------------------------------------
// // Library setup.
// //
//
// // Set up GL context(s) for OpenGL to draw into.
// glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
// if (!prefWindowed) {
// if (prefRefresh) sprintf_s(glutGamemode, "%ix%i:%i@%i", prefWidth, prefHeight, prefDepth, prefRefresh);
// else sprintf_s(glutGamemode, "%ix%i:%i", prefWidth, prefHeight, prefDepth);
// glutGameModeString(glutGamemode);
// glutEnterGameMode();
// } else {
// glutInitWindowSize(prefWidth, prefHeight);
// glutCreateWindow(argv[0]);
// }
//
// // Setup argl library for current context.
// if ((gArglSettings = arglSetupForCurrentContext()) == NULL) {
// fprintf(stderr, "main(): arglSetupForCurrentContext() returned error.\n");
// exit(-1);
// }
// glEnable(GL_DEPTH_TEST);
// arUtilTimerReset();
//
// // Register GLUT event-handling callbacks.
// // NB: Idle() is registered by Visibility.
// glutDisplayFunc(Display);
// glutReshapeFunc(Reshape);
// glutVisibilityFunc(Visibility);
//
// glutMainLoop();
//
physSimu ps;
ps.t = 0;
ps.circle.r = 0;
Field field;
ps.changeState(0, 0, 5, 5);
for (int i = 0; i < 100; i++) {
ps.simulate(field, 0.01*i);
ps.print();
}
while (1);
return (0);
}