Start a new topic

Can we get the screen coordinates from target model and projection matrices ?

How get the screen cordinates (X,Y points) from targets modelview and projection matrix?


I tried following code but the values were very high


 GLKMatrix4  modelView;

 GLKMatrix4  projection;

 memcpy(modelView.m, target.modelView, 16*sizeof(float));

 memcpy(projection.m, target.projection, 16*sizeof(float));

 GLKMatrix4 modelviewprojectionMatrix = GLKMatrix4Multiply(projection, modelView);

 static GLfloat rectVerts[] = {-0.1f, -0.1f, 0.0f,-0.1f,

0.1f, 0.0f,0.1f, 0.1f,

0.0f,0.1f, -0.1f, 0.0f

,0.0f,0.0f,0.0f,1.0f};

 GLKVector4 p_clip = GLKMatrix4MultiplyVector4(modelviewprojectionMatrix, GLKVector4MakeWithArray(rectVerts));

 GLKVector4 p_ndc = GLKVector4DivideScalar(p_clip, p_clip.w);

 GLint view[4]={0};

 glGetIntegerv(GL_VIEWPORT,view);

 CGRect rect = CGRectMake(view[0], view[1], view[2], view[3]);

 rect = [self convertCGRect:rect];

 float x = (rect.origin.x + rect.size.width) * (p_ndc.x + 1) /2;

 NSLog(@"Test x = %f",x);


Hello!


If the goal is to get the coordinates of the target in screen coordinates, the easiest way is to just use the wikitude plugins API and retreive the values from the RecognizedTarget in the update(const std::list<wikitude::sdk::RecognizedTarget>& recognizedTargets_) - method of the plugin class. More detailed documentation can be found at: 


https://www.wikitude.com/external/doc/documentation/latest/iosnative/pluginsapi.html#plugins-api  

Hi Thanks for your reply.....It is not working with instant tracker but works with other tracking

Ok the whole idea is the same for 2d tracking as well as instant tracking.

What could be an issue is the actual object coordinates you want to transform.

You can refer to our native samples application to see the object coordinates of the drawn rectangle in the 2D tracking example and the Instant tracking example. 

Specifically the scale-property of the drawn rectangle is different in this case..


Hope that helps,

Markus

HI...I am still having this issue...I have managed to calculate screen coordinates but now I want to calculate the distance 


I am geeting callback on update method on each frame but the size of _currentlyRecognizedTargets always zero for instant tracking and for other tracking I am getting non zero count

 

void SimpleYUVInputPlugin::update(const std::list<wikitude::sdk::RecognizedTarget>& recognizedTargets_) {

 std::lock_guard<std::mutex> lock(_currentlyRecognizedTargetsMutex);

_currentlyRecognizedTargets = std::list<wikitude::sdk::RecognizedTarget>(recognizedTargets_);

NSLog(@"Calculate Size %lu",_currentlyRecognizedTargets.size());

}

 

// Code for instant tracking

self.instantTracker = [self.wikitudeSDK.trackerManager createInstantTracker:self configuration:nil];

self.trackingState = WTInstantTrackerInitializing;

self.renderableRectangle = [[StrokedRectangle alloc] init];

self.renderableRectangle.scale = 0.45;

 

 

 

 

/* 

      //Code for image tracking from resources

 

    __weak typeof(self) weakSelf = self;

NSURL *imageTrackerResourceURL = [[NSBundle mainBundle] URLForResource:@"magazine" withExtension:@"wtc" subdirectory:@"Assets"];

self.targetCollectionResource = [self.wikitudeSDK.trackerManager createTargetCollectionResourceFromURL:imageTrackerResourceURL completion:^(BOOL success, NSError * _Nullable error) {

if ( !success )

{

NSLog(@"Failed to load URL resource. Reason: %@", [error localizedDescription]);

}

else

{

weakSelf.imageTracker = [weakSelf.wikitudeSDK.trackerManager createImageTrackerFromTargetCollectionResource:weakSelf.targetCollectionResource delegate:weakSelf configuration:nil];

}

}]; */


in the above code if I uncomment  //Code for image tracking from resources   I get the NSLog(@"Calculate Size %lu",_currentlyRecognizedTargets.size());

  as one during the tracking and for  // Code for instant tracking  it is always zero even if it is in tracking state

As Instant Tracking tracks the whole scene you will not get any updates in the recognized targets as this is intended for 2D image targets.


To get the distance from the origin of the scene to the camera, you need to extract the translation part from the given modelview matrix, which you can get from the last column of that matrix. This gives you a Vector with the x,y,z translation. You then just need to calculate the length of that vector to get the distance from the origin of the scene to the camera.


Hope that helps,

Markus

Login or Signup to post a comment