www.jpct.net

jPCT-AE - a 3d engine for Android => Support => Topic started by: kelmer on January 17, 2013, 04:00:39 pm

Title: Vuforia + jPCT-AE
Post by: kelmer on January 17, 2013, 04:00:39 pm
I know there are a few posts regarding this integration already, but they are all of a point much more advanced that the one I am :P

I am trying to integrate the basic ImageTargets from Vuforia with JPCT-AE's simple HelloWorld demo, but I am facing some problems.

First I started from Vuforia's code, using their extension of GlSurfaceView. Then on ImageRendered I pasted the code from JPCT's hello world, like this:

Code: [Select]
public ImageTargetsRenderer(ImageTargets activity) {

this.mActivity = activity;

world = new World();
world.setAmbientLight(20, 20, 20);
sun = new Light(world);
sun.setIntensity(250, 250, 250);

// Create a texture out of the icon...:-)
Texture texture = new Texture(BitmapHelper.rescale(BitmapHelper.convert(mActivity.getResources().getDrawable(R.drawable.ic_launcher)), 64, 64));
TextureManager.getInstance().addTexture("texture", texture);

cube = Primitives.getCube(10);
cube.calcTextureWrapSpherical();
cube.setTexture("texture");
cube.strip();
cube.build();

world.addObject(cube);

Camera cam = world.getCamera();
cam.moveCamera(Camera.CAMERA_MOVEOUT, 50);
cam.lookAt(cube.getTransformedCenter());

SimpleVector sv = new SimpleVector();
sv.set(cube.getTransformedCenter());
sv.y -= 100;
sv.z -= 100;
sun.setPosition(sv);
MemoryHelper.compact();
}

/** Called when the surface is created or recreated. */
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
DebugLog.LOGD("GLRenderer::onSurfaceCreated");

// Call native function to initialize rendering:
initRendering();

// Call QCAR function to (re)initialize rendering after first use
// or after OpenGL ES context was lost (e.g. after onPause/onResume):
QCAR.onSurfaceCreated();
}

/** Called when the surface changed size. */
public void onSurfaceChanged(GL10 gl, int width, int height) {
DebugLog.LOGD("GLRenderer::onSurfaceChanged");

// Call native function to update rendering when render surface
// parameters have changed:
updateRendering(width, height);

// Call QCAR function to handle render surface size changes:
QCAR.onSurfaceChanged(width, height);

if (fb != null) {
fb.dispose();
}
fb = new FrameBuffer(gl, width, height);

}

/** The native render function. */
public native void renderFrame();

/** Called to draw the current frame. */
public void onDrawFrame(GL10 gl) {

fb.clear(back);
world.renderScene(fb);
world.draw(fb);
fb.display();

if (!mIsActive)
return;

// Update render view (projection matrix and viewport) if needed:
mActivity.updateRenderView();
// Call our native function to render content
renderFrame();

}
:
But when I launch my app I get an ArrayIndexOutOfBounds error on the world.renderScene method. If I comment vuforia's mGlView.init() code:

Code: [Select]
public void init(int flags, boolean translucent, int depth, int stencil) {
// By default GLSurfaceView tries to find a surface that is as close
// as possible to a 16-bit RGB frame buffer with a 16-bit depth buffer.
// This function can override the default values and set custom values.

// Extract OpenGL ES version from flags
mUseOpenGLES2 = (flags & QCAR.GL_20) != 0;

// By default, GLSurfaceView() creates a RGB_565 opaque surface.
// If we want a translucent one, we should change the surface's
// format here, using PixelFormat.TRANSLUCENT for GL Surfaces
// is interpreted as any 32-bit surface with alpha by SurfaceFlinger.

DebugLog.LOGI("Using OpenGL ES " + (mUseOpenGLES2 ? "2.0" : "1.x"));
DebugLog.LOGI("Using " + (translucent ? "translucent" : "opaque") + " GLView, depth buffer size: " + depth + ", stencil size: " + stencil);

// If required set translucent format to allow camera image to
// show through in the background
if (translucent) {
this.getHolder().setFormat(PixelFormat.TRANSLUCENT);
}
// Setup the context factory for 1.x / 2.0 rendering
setEGLContextFactory(new ContextFactory());

// We need to choose an EGLConfig that matches the format of
// our surface exactly. This is going to be done in our
// custom config chooser. See ConfigChooser class definition
// below.
setEGLConfigChooser(translucent ? new ConfigChooser(8, 8, 8, 8, depth, stencil) : new ConfigChooser(5, 6, 5, 0, depth, stencil));

}

/** Creates OpenGL contexts. */
private static class ContextFactory implements GLSurfaceView.EGLContextFactory {
private static int EGL_CONTEXT_CLIENT_VERSION = 0x3098;

public EGLContext createContext(EGL10 egl, EGLDisplay display, EGLConfig eglConfig) {
EGLContext context;
if (mUseOpenGLES2) {
DebugLog.LOGI("Creating OpenGL ES 2.0 context");
checkEglError("Before eglCreateContext", egl);
int[] attrib_list_gl20 = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE };
context = egl.eglCreateContext(display, eglConfig, EGL10.EGL_NO_CONTEXT, attrib_list_gl20);
} else {
DebugLog.LOGI("Creating OpenGL ES 1.x context");
checkEglError("Before eglCreateContext", egl);
int[] attrib_list_gl1x = { EGL_CONTEXT_CLIENT_VERSION, 1, EGL10.EGL_NONE };
context = egl.eglCreateContext(display, eglConfig, EGL10.EGL_NO_CONTEXT, attrib_list_gl1x);
}

checkEglError("After eglCreateContext", egl);
return context;
}

public void destroyContext(EGL10 egl, EGLDisplay display, EGLContext context) {
egl.eglDestroyContext(display, context);
}
}

Then my app runs but I get "called unimplemented OpenGL ES API" errors constnatly.

Am I approaching anything badly here?
Title: Re: Vuforia + jPCT-AE
Post by: EgonOlsen on January 17, 2013, 05:25:00 pm
You can't expect gl to run if you don't initialize it. Might be that if you omit that initialization that the gl context is null, which makes jPCT-AE think that you want to use GLES 2.0, which will then fail because it hasn't been initialized. About the exception you are getting: It's always helpful to post the actual stack trace. Errors of that kind usually happen if another thread than the rendering thread fiddles around with the world in parallel to rendering, but i'm not sure where this should happen in this case.
Title: Re: Vuforia + jPCT-AE
Post by: kelmer on January 17, 2013, 05:43:00 pm
I do initialize it, like this:

Code: [Select]
mGlView = new QCARSampleGLView(this);

getWindow().setFormat(PixelFormat.TRANSLUCENT);

mRenderer = new ImageTargetsRenderer(this);
mGlView.setRenderer(mRenderer);

the init() method of QCARSampleGlView just sets the config.

Anyway here's the stacktrace:

Code: [Select]
01-17 17:40:02.776: I/jPCT-AE(24147): OpenGL version:    OpenGL ES 2.0 build 1.8@905891
01-17 17:40:02.776: I/jPCT-AE(24147): OpenGL renderer initialized (using 0 texture stages)
01-17 17:40:02.776: E/libEGL(24147): called unimplemented OpenGL ES API
01-17 17:40:02.776: E/libEGL(24147): called unimplemented OpenGL ES API
01-17 17:40:02.776: E/libEGL(24147): called unimplemented OpenGL ES API
01-17 17:40:02.776: I/jPCT-AE(24147): [ 1358440802791 ] - WARNING: State: 0/0/0/0/0/0/0
01-17 17:40:02.831: W/dalvikvm(24147): threadid=13: thread exiting with uncaught exception (group=0x40e57930)
01-17 17:40:02.839: E/AndroidRuntime(24147): FATAL EXCEPTION: GLThread 46650
01-17 17:40:02.839: E/AndroidRuntime(24147): java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
01-17 17:40:02.839: E/AndroidRuntime(24147): at com.threed.jpct.CompiledInstance._fill(CompiledInstance.java:1206)
01-17 17:40:02.839: E/AndroidRuntime(24147): at com.threed.jpct.CompiledInstance.fill(CompiledInstance.java:746)
01-17 17:40:02.839: E/AndroidRuntime(24147): at com.threed.jpct.Object3DCompiler.compile(Object3DCompiler.java:148)
01-17 17:40:02.839: E/AndroidRuntime(24147): at com.threed.jpct.World.compile(World.java:1951)
01-17 17:40:02.839: E/AndroidRuntime(24147): at com.threed.jpct.World.renderScene(World.java:1046)
01-17 17:40:02.839: E/AndroidRuntime(24147): at com.qualcomm.QCARSamples.ImageTargets.ImageTargetsRenderer.onDrawFrame(ImageTargetsRenderer.java:134)
01-17 17:40:02.839: E/AndroidRuntime(24147): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1516)
01-17 17:40:02.839: E/AndroidRuntime(24147): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
Title: Re: Vuforia + jPCT-AE
Post by: EgonOlsen on January 17, 2013, 05:47:08 pm
You are using GLES 2.0. You have to use the constructor in FrameBuffer for it (the one without the gl context). At least the code that you posted above uses the wrong one.
Title: Re: Vuforia + jPCT-AE
Post by: kelmer on January 17, 2013, 06:02:53 pm
Yes, that was it. Thanks!

Is it possible to make the background transparent? Using a RGBA color when calling clear() does not do it.
Title: Re: Vuforia + jPCT-AE
Post by: EgonOlsen on January 17, 2013, 06:46:31 pm
I think i dimly remember some transparent/opaque setting in either Activity or GLSurfaceView...
Title: Re: Vuforia + jPCT-AE
Post by: kelmer on January 17, 2013, 08:14:53 pm
Yes, that worked :)
Title: Re: Vuforia + jPCT-AE
Post by: kelmer on January 19, 2013, 01:46:06 pm
just one more question, I read here: http://www.jpct.net/forum2/index.php?topic=3005.0

that the modelview matrix needs some transformations before applying it in JPCT-AE.

I just took the matrix and directly applied it to the camera like this:

Code: [Select]
com.threed.jpct.Matrix _cameraMatrix = new com.threed.jpct.Matrix();;
SimpleVector _cameraPosition = new SimpleVector();

_camer4aPosition.set(modelViewMat[3],modelViewMat[7],modelViewMat[11]);
modelViewMat[3] = modelViewMat[7] = modelViewMat[11] = 0;

_cameraMatrix.setDump(modelViewMat);
//_cameraMatrix = _cameraMatrix.invert();

cam.setBack(_cameraMatrix);
cam.setPosition(_cameraPosition);

And seems to be working fine. Maybe I am missing something here? Do I actually need to transform the matrix in any way?
Title: Re: Vuforia + jPCT-AE
Post by: EgonOlsen on January 19, 2013, 02:49:25 pm
I would have expected that you have to do some conversions to transform between the different coordinate systems, but if it works this way, i see no need to bother with it unless it becomes a problem.
Title: Re: Vuforia + jPCT-AE
Post by: kelmer on January 24, 2013, 12:55:16 pm
I've got a little question. I perfected the integration with QCAR's engine, but I am faced with a doubt.

If I create a 30 units big cube and don't move it at all, it looks like the cube lies "beneath" the marker, so if I rotate the camera around it you can see it rotate as though it was inside the marker instead of above it. Obviously this is difficult to see (and probably to understand without seein it), but to clear things a bit it is as though the marker is positioned at (0, 0, 0) facing Z positive direction, and the cube lies at (0, 0, 0) but extends towards Z negative direction (or maybe it is the other way around).

For the cube I can just move it 30 points towards the positive z direction and I'm done, but with models I wouldn't know how much to move them so they lie perfectly above the marker. How can I solve this?

Does this all make sense to you? I'm not very clear as to how JPCT positions objects, or what is the difference between center, origin and transformed center...
Title: Re: Vuforia + jPCT-AE
Post by: EgonOlsen on January 24, 2013, 12:56:42 pm
Maybe you can post one or two screen shots? I've no idea what exactly you are talking about... ;)
Title: Re: Vuforia + jPCT-AE
Post by: kelmer on January 24, 2013, 01:12:42 pm
Okay, ionstead of a screenshot (which would make it even harder to understand) I am posting a scheme.

This is my scene from a sideview:



[attachment deleted by admin]
Title: Re: Vuforia + jPCT-AE
Post by: EgonOlsen on January 24, 2013, 01:23:15 pm
This depends on the coordinates of your model in object space. You can try to create a generic solution by looking at the bounding box (can be obtained from an Object3D's mesh after build() and translating your model accordingly.
Title: Re: Vuforia + jPCT-AE
Post by: kelmer on January 24, 2013, 01:26:21 pm
Okay, I checked with a plane and it's more like I'm showing in this attachment.

I understand your answer, but given that I'm using a cube (Primitives.getCube()), does this mean the cube is expanded from the origin in all directions? It's not created, like, towards positive x,y, and z axis?

[attachment deleted by admin]
Title: Re: Vuforia + jPCT-AE
Post by: kelmer on January 24, 2013, 01:32:54 pm
Here's another pic.

[attachment deleted by admin]
Title: Re: Vuforia + jPCT-AE
Post by: EgonOlsen on January 24, 2013, 02:23:55 pm
All objects from Primitives are lathe objects (except for the plane) which are created with the origin as center.
Title: Re: Vuforia + jPCT-AE
Post by: EgonOlsen on February 01, 2013, 03:06:38 pm
I've got a little question. I perfected the integration with QCAR's engine, but I am faced with a doubt.
BTW: If you have this working fine now, maybe you could add a page about it to the wiki? That would be great, because it seems to be a common problem and i personally can't cover it, because i've no experience with that particular topic. I can create a wiki account for you, if you want to contribute.
Title: Re: Vuforia + jPCT-AE
Post by: kelmer on February 03, 2013, 07:46:53 pm
Sorry for the delay, got stuck with other stuff.

I would gladly create that wiki page. PM with the details :)
Title: Re: Vuforia + jPCT-AE
Post by: EgonOlsen on February 03, 2013, 08:46:57 pm
You should have received an email from the wiki system with the account data.
Title: Re: Vuforia + jPCT-AE
Post by: kelmer on February 03, 2013, 08:55:28 pm
Got it, will get to it ASAP. I haven't got any experience editing wiki pages, though. Might need a little help.
Title: Re: Vuforia + jPCT-AE
Post by: EgonOlsen on February 03, 2013, 08:56:34 pm
It's pretty easy. Just open some of the other pages in edit-mode and you'll see.
Title: Re: Vuforia + jPCT-AE
Post by: kelmer on February 04, 2013, 12:35:16 am
It's done then:

http://www.jpct.net/wiki/index.php/Integrating_JPCT-AE_with_Vuforia#Setting_up_the_FOV

Please apply the pertinent corrections if I made any mistakes describing the process, I'm no expert myself!
Title: Re: Vuforia + jPCT-AE
Post by: EgonOlsen on February 04, 2013, 07:24:16 am
Looks great, many thanks for that. I'm sure that it will be helpful to others.

Edit: I've added a link it to the wiki's main page.
Title: Re: Vuforia + jPCT-AE
Post by: masonhsiung on February 06, 2013, 11:17:54 am
Hi,
I've followed this wiki instruction.
3D object can be rendered successfully, but it seems that the object is kind of floating/rotating while moving the target, is not perfectly sticked on the target(not like the teapot sample).

I also followed the last part, to translate the object 30 units(I'm using HelloWorld sample, which is using a Cube.) along with the Z-axis, but it didn't help.

Image targets sample screenshot is attached. ( as you can see, the 3D object is not sticked on the center of target)

Any idea?

Thanks!

regards,
Mason Hsiung

[attachment deleted by admin]
Title: Re: Vuforia + jPCT-AE
Post by: kelmer on February 06, 2013, 07:48:19 pm
Try with a plane, with no translation (you might need to rotate it 90deg), and let us know the results.
Title: Re: Vuforia + jPCT-AE
Post by: masonhsiung on February 07, 2013, 05:04:53 am
Hi, here are the plane screenshots(one is from front, another is from right, and it seems that I don't need to rotate 90 deg.).
I think the result is the same with the cube...

Any idea?

Thanks!

regards,
Mason Hsiung

[attachment deleted by admin]
Title: Re: Vuforia + jPCT-AE
Post by: kelmer on February 07, 2013, 09:25:21 am
It does seem like the objects are laying "behind" the marker.

Remove these lines from initialization, if you have them:

   cam.moveCamera(Camera.CAMERA_MOVEOUT, 50);
   cam.lookAt(cube.getTransformedCenter());

EDIT: Fixed the wiki to reflect this error
Title: Re: Vuforia + jPCT-AE
Post by: masonhsiung on February 07, 2013, 11:25:12 am
Hi, thanks! After removing these two lines, it looks quite ok now.
Thanks!

BTW, since we do still need to translate by Z axis, according to the 3D object size.
If we load some existing models(eg. md2, obj.. etc), how do we get the size of the 3D object? so that we can place the 3D object exactly on the target.

Thanks again!

regards,
Mason Hsiung
Title: Re: Vuforia + jPCT-AE
Post by: kelmer on February 07, 2013, 11:53:19 am
EgonOlsen already answered that question earlier on this very thread.
Title: Re: Vuforia + jPCT-AE
Post by: masonhsiung on February 07, 2013, 12:00:44 pm
Got it! thanks! I didn't notice that part :)
Thanks!