28ARToolKit::PatternTable ARToolKit::_pattern_table;
30static void change_size( ARParam *source,
int xsize,
int ysize, ARParam *newparam )
34 newparam->xsize = xsize;
35 newparam->ysize = ysize;
37 double xscale = (double)xsize / (
double)(source->xsize);
38 double yscale = (double)ysize / (
double)(source->ysize);
39 for( i = 0; i < 4; i++ ) {
40 newparam->mat[0][i] = source->mat[0][i] * xscale;
41 newparam->mat[1][i] = source->mat[1][i] * yscale;
42 newparam->mat[2][i] = source->mat[2][i];
45 newparam->dist_factor[0] = source->dist_factor[0] * xscale;
46 newparam->dist_factor[1] = source->dist_factor[1] * yscale;
47 newparam->dist_factor[2] = source->dist_factor[2] / (xscale*yscale);
48 newparam->dist_factor[3] = source->dist_factor[3];
51static void analyze_fov(
double cparam[3][4],
int width,
int height,
double &xfov,
double &yfov)
57 double p[3][3], q[4][4];
61 if( arParamDecompMat(cparam, icpara, trans) < 0 ) {
62 printf(
"gConvGLcpara: Parameter error!!\n");
66 for( i = 0; i < 3; i++ ) {
67 for( j = 0; j < 3; j++ ) {
68 p[i][j] = icpara[i][j] / icpara[2][2];
71 q[0][0] = (2.0 * p[0][0] / width);
72 q[0][1] = (2.0 * p[0][1] / width);
73 q[0][2] = ((2.0 * p[0][2] / width) - 1.0);
77 q[1][1] = (2.0 * p[1][1] / height);
78 q[1][2] = ((2.0 * p[1][2] / height) - 1.0);
83 q[2][2] = (gfar + gnear)/(gfar - gnear);
84 q[2][3] = -2.0 * gfar * gnear / (gfar - gnear);
92 q[0][0] * trans[0][0] +
93 q[0][1] * trans[1][0] +
94 q[0][2] * trans[2][0];
96 q[1][0] * trans[0][1] +
97 q[1][1] * trans[1][1] +
98 q[1][2] * trans[2][1];
100 xfov = 2.0 * atan(1.0/xval) * (180.0/3.141592654);
101 yfov = 2.0 * atan(1.0/yval) * (180.0/3.141592654);
114ARToolKit *ARToolKit::
117 if (AR_DEFAULT_PIXEL_FORMAT != AR_PIXEL_FORMAT_BGRA &&
118 AR_DEFAULT_PIXEL_FORMAT != AR_PIXEL_FORMAT_RGBA &&
119 AR_DEFAULT_PIXEL_FORMAT != AR_PIXEL_FORMAT_ARGB &&
120 AR_DEFAULT_PIXEL_FORMAT != AR_PIXEL_FORMAT_ABGR &&
121 AR_DEFAULT_PIXEL_FORMAT != AR_PIXEL_FORMAT_RGB &&
122 AR_DEFAULT_PIXEL_FORMAT != AR_PIXEL_FORMAT_BGR) {
123 vision_cat.error() <<
124 "The copy of ARToolKit that you are using is not compiled "
125 "for RGB, BGR, RGBA, BGRA, ARGB or ABGR input. Panda3D cannot "
126 "use this copy of ARToolKit. Please modify the ARToolKit's "
127 "config file and compile it again.\n";
132 vision_cat.error() <<
"ARToolKit: invalid camera nodepath\n";
136 if ((node == 0) || (node->get_type() != Camera::get_class_type())) {
137 vision_cat.error() <<
"ARToolKit: invalid camera nodepath\n";
142 if (lens->get_type() != PerspectiveLens::get_class_type()) {
143 vision_cat.error() <<
"ARToolKit: supplied camera node must be perspective.\n";
149 if( arParamLoad(fn.c_str(), 1, &wparam) < 0 ) {
150 vision_cat.error() <<
"Cannot load ARToolKit camera config\n";
154 arParamDisp(&wparam);
156 analyze_fov(wparam.mat, 640, 480, xfov, yfov);
160 ARToolKit *result =
new ARToolKit();
161 result->_camera = camera;
162 result->_camera_param =
new ARParam;
163 result->_threshold = 0.5;
164 result->_marker_size = marker_size;
165 result->_have_prev_conv =
false;
166 memcpy(result->_camera_param, &wparam,
sizeof(wparam));
177 ARParam *param = (ARParam *)_camera_param;
187ARToolKit() : _have_prev_conv(false) {
203get_pattern(
const Filename &filename) {
204 PatternTable::iterator ptf = _pattern_table.find(filename);
205 if (ptf != _pattern_table.end()) {
206 return (*ptf).second;
210 int id = arLoadPatt(fn.c_str());
212 vision_cat.error() <<
"Could not load AR ToolKit Pattern: " << fn <<
"\n";
215 arDeactivatePatt(
id);
216 _pattern_table[filename] = id;
227 int patt = get_pattern(filename);
228 if (patt < 0)
return;
229 _controls[patt] = path;
248analyze(
Texture *tex,
bool do_flip_texture) {
257 vision_cat.error() <<
"ARToolKit can only analyze RGB and RGBA textures.\n";
266 nassertv((xsize > 0) && (ysize > 0));
272 change_size((ARParam*)_camera_param, xsize, ysize, &cparam);
273 arInitCparam(&cparam);
281 const unsigned char *ram = ri.
p();
283 if (ram ==
nullptr) {
284 vision_cat.warning() <<
"No data in texture!\n";
289 unsigned char *dstrow;
290 const unsigned char *srcrow;
292 if (AR_DEFAULT_PIXEL_FORMAT == AR_PIXEL_FORMAT_RGB ||
293 AR_DEFAULT_PIXEL_FORMAT == AR_PIXEL_FORMAT_BGR) {
294 data =
new unsigned char[xsize * ysize * 3];
295 int dstlen = xsize * 3;
297 if (do_flip_texture) {
298 for (
int y = 0; y < ysize; ++y) {
299 int invy = (ysize - y - 1);
300 memcpy(data + invy * dstlen, ram + y * srclen, dstlen);
302 }
else if (dstlen == srclen) {
303 memcpy(data, ram, ysize * srclen);
305 for (
int y = 0; y < ysize; ++y) {
306 memcpy(data + y * dstlen, ram + y * srclen, dstlen);
311 if (do_flip_texture) {
312 for (
int y = 0; y < ysize; ++y) {
313 dstrow = data + dstlen * (ysize - y - 1);
314 srcrow = ram + srclen * y;
315 for (
int x = 0; x < xsize; ++x) {
316 memcpy(dstrow + x * 3, srcrow + x * 4, 3);
320 for (
int y = 0; y < ysize; y++) {
321 dstrow = data + dstlen * y;
322 srcrow = ram + srclen * y;
323 for (
int x = 0; x < xsize; x++) {
324 memcpy(dstrow + x * 3, srcrow + x * 4, 3);
329 }
else if (AR_DEFAULT_PIXEL_FORMAT == AR_PIXEL_FORMAT_RGBA ||
330 AR_DEFAULT_PIXEL_FORMAT == AR_PIXEL_FORMAT_BGRA) {
331 data =
new unsigned char[xsize * ysize * 4];
332 int dstlen = xsize * 4;
335 if (do_flip_texture) {
336 for (
int y = 0; y < ysize; ++y) {
337 dstrow = data + dstlen * (ysize - y - 1);
338 srcrow = ram + srclen * y;
339 for (
int x = 0; x < xsize; ++x) {
340 memcpy(dstrow + x * 4, srcrow + x * 3, 3);
341 dstrow[x * 4 + 3] = 255;
345 for (
int y = 0; y < ysize; ++y) {
346 dstrow = data + dstlen * y;
347 srcrow = ram + srclen * y;
348 for (
int x = 0; x < xsize; ++x) {
349 memcpy(dstrow + x * 4, srcrow + x * 3, 3);
350 dstrow[x * 4 + 3] = 255;
355 if (do_flip_texture) {
356 for (
int y = 0; y < ysize; ++y) {
357 int invy = (ysize - y - 1);
358 memcpy(data + invy * dstlen, ram + y * srclen, dstlen);
360 }
else if (dstlen == srclen) {
361 memcpy(data, ram, ysize * srclen);
363 for (
int y = 0; y < ysize; ++y) {
364 memcpy(data + y * dstlen, ram + y * srclen, dstlen);
369 data =
new unsigned char[xsize * ysize * 4];
370 int dstlen = xsize * 4;
373 if (do_flip_texture) {
374 for (
int y = 0; y < ysize; ++y) {
375 dstrow = data + dstlen * (ysize - y - 1);
376 srcrow = ram + srclen * y;
377 for (
int x = 0; x < xsize; ++x) {
378 memcpy(dstrow + x * 4 + 1, srcrow + x * 3, 3);
383 for (
int y = 0; y < ysize; ++y) {
384 dstrow = data + dstlen * y;
385 srcrow = ram + srclen * y;
386 for (
int x = 0; x < xsize; ++x) {
387 memcpy(dstrow + x * 4 + 1, srcrow + x * 3, 3);
393 if (do_flip_texture) {
394 for (
int y = 0; y < ysize; ++y) {
395 dstrow = data + dstlen * (ysize - y - 1);
396 srcrow = ram + srclen * y;
397 for (
int x = 0; x < xsize; ++x) {
398 memcpy(dstrow + x * 4 + 1, srcrow + x * 4, 3);
399 dstrow[x * 4] = srcrow[x * 4 + 3];
403 for (
int y = 0; y < ysize; ++y) {
404 dstrow = data + dstlen * y;
405 srcrow = ram + srclen * y;
406 for (
int x = 0; x < xsize; ++x) {
407 memcpy(dstrow + x * 4 + 1, srcrow + x * 4, 3);
408 dstrow[x * 4] = srcrow[x * 4 + 3];
416 Controls::const_iterator ctrli;
417 for (ctrli = _controls.begin(); ctrli != _controls.end(); ++ctrli) {
418 arActivatePatt((*ctrli).first);
421 ARMarkerInfo *marker_info;
424 if (arDetectMarker(data, _threshold * 256, &marker_info, &marker_num) < 0) {
425 vision_cat.error() <<
"ARToolKit detection error.\n";
430 for (ctrli = _controls.begin(); ctrli != _controls.end(); ++ctrli) {
432 int pattern = (*ctrli).first;
433 arDeactivatePatt(pattern);
436 for (
int i = 0; i < marker_num; ++i) {
437 if (marker_info[i].
id == pattern) {
438 if (marker_info[i].cf >= conf) {
439 conf = marker_info[i].cf;
445 ARMarkerInfo *inf = &marker_info[best];
449 if (_have_prev_conv) {
450 arGetTransMatCont(inf, _prev_conv, center, _marker_size, _prev_conv);
452 arGetTransMat(inf, center, _marker_size, _prev_conv);
453 _have_prev_conv =
true;
456 for (
int i = 0; i < 4; ++i) {
457 mat(i, 0) = _prev_conv[0][i];
458 mat(i, 1) = _prev_conv[2][i];
459 mat(i, 2) = -_prev_conv[1][i];
463 LVecBase3 scale, shear, hpr, pos;
464 decompose_matrix(mat, scale, shear, hpr, pos);
467 vision_cat.error() <<
"NodePath must have a parent.\n";
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A node that can be positioned around in the scene graph to represent a point of view for rendering a ...
const Element * p() const
Function p() is similar to the function from ConstPointerTo.
The name of a file, such as a texture file or an Egg file.
std::string to_os_specific() const
Converts the filename from our generic Unix-like convention (forward slashes starting with the root a...
Lens * get_lens(int index=0) const
Returns a pointer to the particular Lens associated with this LensNode, or NULL if there is not yet a...
A base class for any number of different kinds of lenses, linear and otherwise.
set_fov
Sets the horizontal field of view of the lens without changing the aspect ratio.
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
void show()
Undoes the effect of a previous hide() on this node: makes the referenced node (and the entire subgra...
void hide()
Makes the referenced node (and the entire subgraph below this node) invisible to all cameras.
bool is_empty() const
Returns true if the NodePath contains no nodes.
PandaNode * node() const
Returns the referenced node of the path.
get_parent
Returns the NodePath to the parent of the referenced node: that is, this NodePath,...
void set_pos_hpr(PN_stdfloat x, PN_stdfloat y, PN_stdfloat z, PN_stdfloat h, PN_stdfloat p, PN_stdfloat r)
Sets the translation and rotation component of the transform, leaving scale untouched.
void clear()
Sets this NodePath to the empty NodePath.
A basic node of the scene graph or data graph.
Represents a texture object, which is typically a single 2-d image but may also represent a 1-d or 3-...
get_texture_type
Returns the overall interpretation of the texture.
int get_pad_y_size() const
Returns size of the pad region.
CPTA_uchar get_ram_image()
Returns the system-RAM image data associated with the texture.
get_ram_image_compression
Returns the compression mode in which the ram image is already stored pre- compressed.
get_y_size
Returns the height of the texture image in texels.
int get_pad_x_size() const
Returns size of the pad region.
get_num_components
Returns the number of color components for each texel of the texture image.
get_x_size
Returns the width of the texture image in texels.
get_component_type
Returns the numeric interpretation of each component of the texture.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.