Game Development Reference
In-Depth Information
Thus you have two threads running simultaneously and accessing the same image buffer.
When the onPreviewFrame fires on the camera surface, as shown in the following fragment,
the image buffer can be sent via JNI to the ARTK for further work:
camera.setPreviewCallback(new Camera.PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera cam) {
// Here we have a frame image in NV21 format
}
});
As shown in Listing 9-1, with the image buffer, the ARTK can then detect any markers on the
image, get a transformation matrix between the marker and the real camera, and draw the
virtual object (as seen in the following code):
/* detect the markers in the video frame */
if( arDetectMarker(dataPtr, thresh, &marker_info, &marker_num) < 0 ) {
cleanup();
exit(0);
}
/* check for marker visibility */
k = -1;
for( j = 0; j < marker_num; j++ ) {
if( patt_id == marker_info[j].id ) {
if( k == -1 ) k = j;
else if( marker_info[k].cf < marker_info[j].cf ) k = j;
}
}
/* no markers ? */
if( k == -1 ) {
return;
}
/* get the transformation between the marker and the real camera */
arGetTransMat(&marker_info[k], patt_center, patt_width, patt_trans);
/* draw GL stuff */
/* Swap buffers */
This technique of using two surfaces to draw and letting the ARTK handle the marker
detection and the transformation matrix is pretty clever. Nevertheless, the following issues
should be carefully considered:
Concurrency : You have two threads accessing the same image buffer.
Special care should be taken to lock access to shared data so only one
thread can touch the image buffer at a given time. AndAR does this
using the Java built-in concurrency mechanism.
OpenGL drawing in Java vs. the native side : AndAR preforms all
OpenGL ES drawing at the Java side (using the GL Surface). This may
not be the best choice for a game that requires access to resources in
the native side (for example, models, textures, etc.). It would be better
to move all GL drawing to the native side, especially if you plan to reuse
vast amounts of code from an existing game engine.
 
Search WWH ::




Custom Search