I have made another project modifying the ImageTargets sample to make sure I was doing things the right way.

What I have noticed is that although lineStart seems correct, the lineEnd point is skewed by a great amount to the left. Why would that be?

Could someone please help me out with this? I have been struggling with it for 2 weeks and I am beginning to pull my hair out.

You could replicate my steps by modifying the ImageTargets sample project.

I have added the following method to QCARUtils.mm :

bool touchPointIntersectsCameraObject(QCAR::Vec2F &point)
{
QCARutils *qUtils = [QCARutils getInstance];
// Window Coordinates to Normalized Device Coordinates
QCAR::VideoBackgroundConfig config = QCAR::Renderer::getInstance().getVideoBackgroundConfig();
float halfScreenWidth = qUtils.viewSize.height / 2.0f; // inversed - matches width of the target
float halfScreenHeight = qUtils.viewSize.width / 2.0f; // inversed - matches height of the target
float halfViewportWidth = config.mSize.data[0] / 2.0f;
float halfViewportHeight = config.mSize.data[1] / 2.0f;
float x = (qUtils.contentScalingFactor * point.data[0] - halfScreenWidth) / halfViewportWidth;
float y = (qUtils.contentScalingFactor * point.data[1] - halfScreenHeight) / halfViewportHeight * -1;
QCAR::Vec4F ndcNear(x, y, -1, 1);
QCAR::Vec4F ndcFar(x, y, 1, 1);
// Normalized Device Coordinates to Eye Coordinates
QCAR::Matrix44F projectionMatrix = [QCARutils getInstance].projectionMatrix;
QCAR::Matrix44F inverseProjMatrix = SampleMath::Matrix44FInverse(projectionMatrix);
QCAR::Vec4F pointOnNearPlane = SampleMath::Vec4FTransform(ndcNear, inverseProjMatrix);
QCAR::Vec4F pointOnFarPlane = SampleMath::Vec4FTransform(ndcFar, inverseProjMatrix);
pointOnNearPlane = SampleMath::Vec4FDiv(pointOnNearPlane, pointOnNearPlane.data[3]);
pointOnFarPlane = SampleMath::Vec4FDiv(pointOnFarPlane, pointOnFarPlane.data[3]);
// Eye Coordinates to Object Coordinates
QCAR::Matrix44F modelViewMatrix = [QCARutils getInstance].modelViewMatrix;
QCAR::Matrix44F inverseModelViewMatrix = SampleMath::Matrix44FInverse(modelViewMatrix);
QCAR::Vec4F nearWorld = SampleMath::Vec4FTransform(pointOnNearPlane, inverseModelViewMatrix);
QCAR::Vec4F farWorld = SampleMath::Vec4FTransform(pointOnFarPlane, inverseModelViewMatrix);
QCAR::Vec3F lineStart = QCAR::Vec3F(nearWorld.data[0], nearWorld.data[1], nearWorld.data[2]);
QCAR::Vec3F lineEnd = QCAR::Vec3F(farWorld.data[0], farWorld.data[1], farWorld.data[2]);
// NSLog(@"Line start: %.2f, %.2f, %.2f", lineStart.data[0], lineStart.data[1], lineStart.data[2]);
// NSLog(@"Line end: %.2f, %.2f, %.2f", lineEnd.data[0], lineEnd.data[1], lineEnd.data[2]);
if (linePlaneIntersection(lineStart, lineEnd, QCAR::Vec3F(0, 0, 0), QCAR::Vec3F(0, 0, 1), intersection)) {
NSLog(@"Intersection is: %.2f, %.2f, %.2f", intersection.data[0], intersection.data[1], intersection.data[2]);
[EAGLView addTouchPointX:intersection.data[0] Y:intersection.data[1] Z:intersection.data[2]];
}
return false;
}

which is almost the same as in the Dominoes sample. I obtain the modelview matrix using:

modelViewMatrix = QCAR::Tool::convertPose2GLMatrix(result->getPose());

within renderFrameQCAR, which I later on pass in to the touchPointIntersectsCameraObject method through a member var in QCARUtils. I make sure that the modelview is not modified along the way.

I have added a touchesBegan method within ImageTargetsOverlayViewController that goes like:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint pointOfTouch = [touch locationInView:self.view];
BOOL touchedVirtualObject = [[QCARutils getInstance] didTouchVirtualObjectAtPoint:pointOfTouch];
if (touchedVirtualObject) {
NSLog(@"You touched a virtual object!");
}
}

and set UserInteractionEnabled to YES for the ImageTargetsOverlayViewController to receive touches.

What else could I be missing?

Many thanks,

Mike

Your welcome!

Glad I could help.

N