Author Topic: Drawing 3D models using 2 libraries(JPCT + QCAR) results in depthTest confusion  (Read 905 times)

Offline Anuxander

  • byte
  • *
  • Posts: 6
    • View Profile
Hi guys, thanks in advance for reading my topic. This is quite a complicated question I guess.

It sounds weird to draw things from 2 places, why wont I draw all models with JPCT? The reason is that QCAR has provided useful codes to draw a "transparent" cylinder/squareBox to act as the TARGET itself. Its transparent texture can show the camera preview screen while blocking any drawn models which lie behind it. I tried to draw the cylinder in JPCT also but I couldn't make such a texture. Even after blending I got it transparent but it fails to block any 3D models lying behind it. So I decided to leave the cylinder drawing to QCAR.

Here is my code in QCAR:
Code: [Select]
                glEnable (GL_DEPTH_TEST);
glEnable (GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// prepare the cylinder

SampleUtils::scalePoseMatrix(kCylinderScaleX, kCylinderScaleY,
kCylinderScaleZ, &modelViewMatrix.data[0]);
SampleUtils::multiplyMatrix(&projectionMatrix.data[0],
&modelViewMatrix.data[0], &modelViewProjection.data[0]);
SampleUtils::checkGlError("CylinderTargets prepareCylinder");

glUseProgram(shaderProgramID);
glVertexAttribPointer(vertexHandle, 3, GL_FLOAT, GL_FALSE, 0,
(const GLvoid*) mCylinderModel.ptrVertices());
glVertexAttribPointer(normalHandle, 3, GL_FLOAT, GL_FALSE, 0,
(const GLvoid*) mCylinderModel.ptrNormals());
glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0,
(const GLvoid*) mCylinderModel.ptrTexCoords());

glEnableVertexAttribArray(vertexHandle);
glEnableVertexAttribArray(normalHandle);
glEnableVertexAttribArray(textureCoordHandle);

glActiveTexture (GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textures[0]->mTextureID);
glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE,
(GLfloat*) &modelViewProjection.data[0]);
glUniform1i(texSampler2DHandle, 0 /*GL_TEXTURE0*/);
glDrawElements(GL_TRIANGLES, mCylinderModel.nbIndices(),
GL_UNSIGNED_SHORT, (const GLvoid*) mCylinderModel.ptrIndices());
So my cylinder is drawn now.

The following passes the matrix of the TARGET to JPCT:
Quote
      modelViewMatrix = QCAR::Tool::convertPose2GLMatrix(result->getPose());
      SampleUtils::multiplyMatrix(&projectionMatrix.data[0],
            &modelViewMatrix.data[0], &modelViewProjection.data[0]);
      jfloatArray modelviewArray = env->NewFloatArray(16);
      env->SetFloatArrayRegion(modelviewArray, 0, 16, modelViewMatrix.data);
      env->CallVoidMethod(obj, updateMatrixMethod, modelviewArray);
      env->DeleteLocalRef(modelviewArray);
      env->CallVoidMethod(obj, fovMethod, fovRadians);
      env->CallVoidMethod(obj, fovyMethod, fovyRadians);

This is where to receive the matrix in JPCT:
Code: [Select]
public void updateModelviewMatrix(float mat[]) {

modelViewMat = mat;
}

and onDraw:
Code: [Select]
public void onDrawFrame(GL10 gl) {
if (!mIsActive)
return;

// Update render view (projection matrix and viewport) if needed:
mActivity.updateRenderView();
// Call our native function to render content
modelViewMat = null;
cylinderModelViewMat = null;
renderFrame();    //here call the QCAR native code so that JPCT receives the new matrix
updateCamera();
world.renderScene(fb);
world.draw(fb);
fb.display();


}

The updateCamera method:
Code: [Select]
public void updateCamera() {
Matrix m = new Matrix();
if (modelViewMat != null) {
firstObj.setVisibility(true);
m.setDump(modelViewMat);
cam.setBack(m);
return;
}
firstObj.setVisibility(false);
cam.setBack(originalMatrix);

}

While tracking 3D TARGETS, depth test is very important as animated models may go behind or in front of the targets. So they should be hindered or shown in case. In the above example, there is simply a cylinder drawn in QCAR and a firstObj(a Object3D type md2) drawn in JPCT.

Now the problem Im facing is that when I put my camera close to the target, things go perfect - depth test gives a correct order - e.g firstObj on top of the cylinder, in case that it is closer to the camera. But when I slowly move my camera further from the TARGET, the firstObj "dives" into the cylinder slowly as well ( cylinder gradually rises to top and firstObj gets covered by it), which is not correct accroding to the translation I set.

The firstObj is not supposed to be covered by the cylinder. This just happens when 2 of them overlaps while the camera is pulled away for a certain extent. Other testing like translating and rotating gives no error.

After reading plenty of posts in forums, I suspect this is caused by different coordinates systems from QCAR & JPCT, but I have tried various suggested solutions which do not even output a drawing. Some say that QCAR matrix is column major and JPCT is row major. But why do all other parts of it work perfectly well if the matrix major does matter? I didnt even invert it.

**Note that if I draw the cylinder also in JPCT : Primitives.getCylinder........, then translate it to stick to the virtual target from QCAR, error doesnt happen! No matter how I pull and move my camera, the firstObj and cylinder keep in correct drawing order. The only shame is that I could not give the cylinder a correct "transparent" texture by this. But this case, has proven that the way passing the matrix from QCAR to JPCT is exactly right.

It has been very confusing and I have been struggling to this for several days. If you have any questions or suggestions, please give me a reply. Thank you for reading such a long question!!

« Last Edit: August 22, 2013, 12:33:51 pm by Anuxander »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11745
    • View Profile
    • http://www.jpct.net
The different coordinate system isn't the reason, because down on the hardware level, it's all the same anyway. The reason might be different settings for near and far clipping planes. Try to set the values in one to the values of the others and see if that helps (keep in mind that jPCT ignores the near plane setting until you adjust the Config setting).

Offline Anuxander

  • byte
  • *
  • Posts: 6
    • View Profile
Thanks EgonOlsen! You are sooooo right!

While change jPCT's far/near to QCAR's had no effect (increasing both plane values), I changed the configuration of QCAR's to jPCT's. Things are all correct now.

You helped me a lot. Thanks again ;D ;D

Offline Anuxander

  • byte
  • *
  • Posts: 6
    • View Profile
In case someone would like to know, QCAR's initial far plane is set to 2500, near plane to 2, which are larger than the default values of jPCT (1 & 1000). They have to be unified to solve the depth problem as I mentioned along Z direction.