Log in or register to post comments

translation, scaling, rotation

August 18, 2012 - 9:05am #1

I'm displaying up to four 3d objects with the same marker at the same time.

I'm experiencing a couple of things that seem to be wrong, it's probably something I'm not doing correctly.

I need these objects to match in scale since they combine into one single object, however if I apply the same scale value to all of them they seem to have different ratio sizes. That's one of the issues.

 

On the other hand, since they combine and form a single object, I need them to rotate upon swipe maintaining the structure (they need to be stacked).

I've tried a lot of combinations without luck. First, every object I add seems to be centered in the target, even when my models match the position they need to be in in my 3dmax file.

Second, since this is happening, I tried translating them to the needed position, but when after doing so I rotate them, they all rotate in their center instead of doing so in the center of the projection. Not sure I'm being clear, let me know if I'm not!

 

I can post images or the part of the code where the objects are being transformed if needed.

 

Best

Juan

translation, scaling, rotation

July 1, 2013 - 1:43am #10

Hi, take into account the order of your transformations;

for example, I see in your code that you first apply some "animateMyModel" function to your modelview matrix, and then you apply translation after that;

keep i  mind that any transformation you do in the "animateMyModel" function at the beginning will affect the subsequent transformations.

Usually the best way to "debug" transformations is to enable them one at a time, and test the behaviour, then add one more transformation and test again, nd so on;

this usually helps understanding what goes wrong in the transformation chain.

 

 

translation, scaling, rotation

July 1, 2013 - 1:26am #9

Hi  AlessandroB

Thank you for the answer.

I have already do the same recommandation on this example.

I will show you a part of my code:

 

   //   animate my model is juste a rotate on the oz asis.
           animateMyModel(modelViewMatrix);
      
        SampleUtils::translatePoseMatrix(-kCadranScale/3 + kMyModelScale, kCadranScale/3- kMyModelScale, kCadranScale +     kMyModelScale,
                                         &modelViewMatrix.data[0]);

        SampleUtils::scalePoseMatrix(kMyModelScale, kMyModelScale, kMyModelScale,
                                     &modelViewMatrix.data[0]);
        SampleUtils::rotatePoseMatrix(-54.0f, 0.0f, 0.0f, 1.0f, &modelViewMatrix.data[0]);    
        SampleUtils::rotatePoseMatrix(90.0f, 1.0f, 0.0f, 0.0f, &modelViewMatrix.data[0]);

    
        SampleUtils::multiplyMatrix(&projectionMatrix.data[0],
                                    &modelViewMatrix.data[0] ,
                                    &modelViewProjection.data[0]);
     
           //  drawing MyModel
        glVertexAttribPointer(vertexHandle, 3, GL_FLOAT, GL_FALSE, 0,
                              (const GLvoid*) &MyModeVerts[0]);
       // glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0,(const GLvoid*) &MyModelTexCoords[0]);
      //  glVertexAttribPointer(colorHandle, 4, GL_FLOAT, GL_FALSE, 0,(const GLvoid*) &MyModelColor);
        glEnableVertexAttribArray(vertexHandle);
       // glEnableVertexAttribArray(textureCoordHandle);
      //  glEnableVertexAttribArray(colorHandle);
            
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, textureMyModel->mTextureID);
        glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE,
                           (GLfloat*)&modelViewProjection.data[0] );
        glUniform1i(texSampler2DHandle, 0 );
        
        // draw data
        glDrawArrays(GL_TRIANGLES, 0, MyModelNumVerts);

 

I note that  when the translation line is like this:

       SampleUtils::translatePoseMatrix(0.0, 0.0, kCadranScale +     kMyModelScale,
                                         &modelViewMatrix.data[0]);

 the translation vector is (0.0,0.0, k) my rotation seems working but if the translation vector is (k1,k2,k3) like in the following line:

SampleUtils::translatePoseMatrix(-kCadranScale/3 + kMyModelScale, kCadranScale/3- kMyModelScale, kCadranScale +     kMyModelScale,
                                         &modelViewMatrix.data[0]);

the object rotate not on himself but in unknowing direction.

Have you an idee?

Thanks.

 

translation, scaling, rotation

July 1, 2013 - 1:02am #8

Hi Aimingo,

I wrote some time ago a couple of articles in our FAQ section, which explains how to handle transformations (rotation, scaling, translation) of a 3D object with our samples;

I suggest to have a look:

https://developer.vuforia.com/forum/faq/android-how-can-i-rotate-teapot

https://developer.vuforia.com/forum/faq/opengl-how-do-i-scale-andor-position-my-models

HTH.

 

translation, scaling, rotation

July 1, 2013 - 12:34am #7

I have the same problem. Have anybody the solution?

After applaying transformation(translation,scaling and rotation)

I want to animate all my elements rotate on theme selfs.

It not works all the elements move in not define direction.

Sorry for my english that is so poor.

translation, scaling, rotation

September 19, 2012 - 9:11pm #6

Sure....and thanks for the help!

inside namespace

    const char* textureFilenames[] = {
        "interior.png",
        "exterior.png",
        "ruedas.png",
        "motorTrenDel.png"    };

    // Model scale factor
  
    float kChasisScale          = 100.0f;
    float kInteriorScale        = 100.0f;
    float kWheelScale           = 100.0f;
   
    float kLastChasisScale            = kChasisScale;
    float kLastInteriorScale          = kInteriorScale;
    float kLastWheelScale             = kWheelScale;
   
    float floatAngleX           = 0.0f;
    float floatAngleY           = 0.0f;
    float floatAngleZ           = 0.0f;
   
   
    int interiorTexture         = 2;
    int chasisTexture         = 1;
   
    int wheelTexture              = 0;
   
    QCAR::Matrix44F mCurrentRotation        = SampleMath::Matrix44FIdentity();
    QCAR::Matrix44F mAccumulatedRotation    = SampleMath::Matrix44FIdentity();
   

and inside renderFrameQCAR

 

            // OpenGL 2
           
           
            QCAR::Matrix44F modelViewProjection;
            
            glUseProgram(shaderProgramID);
            
            
            
            // MOTOR
            modelViewMatrix = QCAR::Tool::convertPose2GLMatrix(trackable->getPose());
            
            mCurrentRotation        = SampleMath::Matrix44FIdentity();
            
            ShaderUtils::scalePoseMatrix(kWheelScale, kWheelScale, kWheelScale, &modelViewMatrix.data[0]);
            ShaderUtils::rotatePoseMatrix(floatAngleZ, 0.0f, 0.0f, 1.0f, &mCurrentRotation.data[0]);
            ShaderUtils::rotatePoseMatrix(floatAngleY, 0.0f, 1.0f, 0.0f, &mCurrentRotation.data[0]);
            ShaderUtils::rotatePoseMatrix(floatAngleX, 1.0f, 0.0f, 0.0f, &mCurrentRotation.data[0]);
            
            ShaderUtils::multiplyMatrix(&mCurrentRotation.data[0], &mAccumulatedRotation.data[0], &mAccumulatedRotation.data[0]);
            
            ShaderUtils::multiplyMatrix(&modelViewMatrix.data[0], &mAccumulatedRotation.data[0], &modelViewMatrix.data[0]);
            
            ShaderUtils::multiplyMatrix(&qUtils.projectionMatrix.data[0], &modelViewMatrix.data[0], &modelViewProjection.data[0]);
            
            floatAngleX = 0.0f;
            floatAngleY = 0.0f;
            floatAngleZ = 0.0f;
            
            glVertexAttribPointer(vertexHandle, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) &motorTrenDelVerts[0]);
            glVertexAttribPointer(normalHandle, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) &motorTrenDelNormals[0]);
            glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) &motorTrenDelTexCoords[0]);
            
            glEnableVertexAttribArray(vertexHandle);
            glEnableVertexAttribArray(normalHandle);
            glEnableVertexAttribArray(textureCoordHandle);
            
            glActiveTexture(GL_TEXTURE0);
            glBindTexture(GL_TEXTURE_2D, [[textures objectAtIndex:3] textureID]);
            glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE, (const GLfloat*)&modelViewProjection.data[0]);
            
            glDrawArrays(GL_TRIANGLES, 0, motorTrenDelNumVerts);
            
            // INTERIOR
            modelViewMatrix = QCAR::Tool::convertPose2GLMatrix(trackable->getPose());
            
            
            ShaderUtils::scalePoseMatrix(kWheelScale, kWheelScale, kWheelScale, &modelViewMatrix.data[0]);
            ShaderUtils::rotatePoseMatrix(floatAngleZ, 0.0f, 0.0f, 1.0f, &mCurrentRotation.data[0]);
            ShaderUtils::rotatePoseMatrix(floatAngleY, 0.0f, 1.0f, 0.0f, &mCurrentRotation.data[0]);
            ShaderUtils::rotatePoseMatrix(floatAngleX, 1.0f, 0.0f, 0.0f, &mCurrentRotation.data[0]);
            
            ShaderUtils::multiplyMatrix(&mCurrentRotation.data[0], &mAccumulatedRotation.data[0], &mAccumulatedRotation.data[0]);
            
            ShaderUtils::multiplyMatrix(&modelViewMatrix.data[0], &mAccumulatedRotation.data[0], &modelViewMatrix.data[0]);
            
            ShaderUtils::multiplyMatrix(&qUtils.projectionMatrix.data[0], &modelViewMatrix.data[0], &modelViewProjection.data[0]);
            
            floatAngleX = 0.0f;
            floatAngleY = 0.0f;
            floatAngleZ = 0.0f;
            
            glVertexAttribPointer(vertexHandle, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) &interiorVerts[0]);
            glVertexAttribPointer(normalHandle, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) &interiorNormals[0]);
            glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) &interiorTexCoords[0]);
            
            glEnableVertexAttribArray(vertexHandle);
            glEnableVertexAttribArray(normalHandle);
            glEnableVertexAttribArray(textureCoordHandle);
            
            glActiveTexture(GL_TEXTURE0);
            glBindTexture(GL_TEXTURE_2D, [[textures objectAtIndex:0] textureID]);
            glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE, (const GLfloat*)&modelViewProjection.data[0]);
            
            glDrawArrays(GL_TRIANGLES, 0, interiorNumVerts);
            
            
            // EXTERIOR
            modelViewMatrix = QCAR::Tool::convertPose2GLMatrix(trackable->getPose());
            
            
            ShaderUtils::scalePoseMatrix(kWheelScale, kWheelScale, kWheelScale, &modelViewMatrix.data[0]);
            ShaderUtils::rotatePoseMatrix(floatAngleZ, 0.0f, 0.0f, 1.0f, &mCurrentRotation.data[0]);
            ShaderUtils::rotatePoseMatrix(floatAngleY, 0.0f, 1.0f, 0.0f, &mCurrentRotation.data[0]);
            ShaderUtils::rotatePoseMatrix(floatAngleX, 1.0f, 0.0f, 0.0f, &mCurrentRotation.data[0]);
            
            ShaderUtils::multiplyMatrix(&mCurrentRotation.data[0], &mAccumulatedRotation.data[0], &mAccumulatedRotation.data[0]);
            
            ShaderUtils::multiplyMatrix(&modelViewMatrix.data[0], &mAccumulatedRotation.data[0], &modelViewMatrix.data[0]);
            
            ShaderUtils::multiplyMatrix(&qUtils.projectionMatrix.data[0], &modelViewMatrix.data[0], &modelViewProjection.data[0]);
            
            floatAngleX = 0.0f;
            floatAngleY = 0.0f;
            floatAngleZ = 0.0f;
            
            glVertexAttribPointer(vertexHandle, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) &exteriorVerts[0]);
            glVertexAttribPointer(normalHandle, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) &exteriorNormals[0]);
            glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) &exteriorTexCoords[0]);
            
            glEnableVertexAttribArray(vertexHandle);
            glEnableVertexAttribArray(normalHandle);
            glEnableVertexAttribArray(textureCoordHandle);
            
            glActiveTexture(GL_TEXTURE0);
            glBindTexture(GL_TEXTURE_2D, [[textures objectAtIndex:1] textureID]);
            glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE, (const GLfloat*)&modelViewProjection.data[0]);
            
            glDrawArrays(GL_TRIANGLES, 0, exteriorNumVerts);
            
            
            // RUEDAS
            modelViewMatrix = QCAR::Tool::convertPose2GLMatrix(trackable->getPose());
            
            
            ShaderUtils::scalePoseMatrix(kWheelScale, kWheelScale, kWheelScale, &modelViewMatrix.data[0]);
            ShaderUtils::rotatePoseMatrix(floatAngleZ, 0.0f, 0.0f, 1.0f, &mCurrentRotation.data[0]);
            ShaderUtils::rotatePoseMatrix(floatAngleY, 0.0f, 1.0f, 0.0f, &mCurrentRotation.data[0]);
            ShaderUtils::rotatePoseMatrix(floatAngleX, 1.0f, 0.0f, 0.0f, &mCurrentRotation.data[0]);
            
            ShaderUtils::multiplyMatrix(&mCurrentRotation.data[0], &mAccumulatedRotation.data[0], &mAccumulatedRotation.data[0]);
            
            ShaderUtils::multiplyMatrix(&modelViewMatrix.data[0], &mAccumulatedRotation.data[0], &modelViewMatrix.data[0]);
            
            ShaderUtils::multiplyMatrix(&qUtils.projectionMatrix.data[0], &modelViewMatrix.data[0], &modelViewProjection.data[0]);
            
            floatAngleX = 0.0f;
            floatAngleY = 0.0f;
            floatAngleZ = 0.0f;
            
            glVertexAttribPointer(vertexHandle, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) &ruedasVerts[0]);
            glVertexAttribPointer(normalHandle, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) &ruedasNormals[0]);
            glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) &ruedasTexCoords[0]);
            
            glEnableVertexAttribArray(vertexHandle);
            glEnableVertexAttribArray(normalHandle);
            glEnableVertexAttribArray(textureCoordHandle);
            
            glActiveTexture(GL_TEXTURE0);
            glBindTexture(GL_TEXTURE_2D, [[textures objectAtIndex:2] textureID]);
            glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE, (const GLfloat*)&modelViewProjection.data[0]);
            
            glDrawArrays(GL_TRIANGLES, 0, ruedasNumVerts);
            
            ShaderUtils::checkGlError("EAGLView renderFrameQCAR");
           

 

All objects are drawn and maintain rotation perfectly. The only issue I'm having is, even when all objects are parts of a unique model (a car), each 'part' is displayed with a different scale and position to the one in the model they're taken from, instead of maintaining the correct proportions between them.
           

translation, scaling, rotation

September 19, 2012 - 6:40pm #5

Can you post the code that you're using to position and scale these models?

translation, scaling, rotation

August 24, 2012 - 3:29am #4

Hi juan

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

..is the correct way to reset the matrix.

I cannot think why the scale and translation might be messed up, but there has to be an explanation, and it may have something to do with the transformations happening one after the other.  You could also try just starting with one object and adding others one by one to see where it goes wrong.

HTH

N

 

translation, scaling, rotation

August 23, 2012 - 11:41am #3

Thanks for your replay NalinS.

What would be the right way to reset the modelview matrix?

Before the code for rendering (scaling, translating, rotation and drawing) each object I have:

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

so I assume it's being reset, I took this from the MultiTargets sample.

I'm not sure what could be wrong, but both scale and translation are messed up.

translation, scaling, rotation

August 20, 2012 - 4:19am #2

Hi juanjuan

If they have different sizes, it could be that the modelview matrix is not being reset initialised correctly for each object, in which case some of the transformations might be cumulative.

If you need them to form a single object, then effectively you may need a mini scene graph with a parent object and each of your four objects underneath as a child - which means that each child object needs its own matrix.  This is possible but will involve a little effort.

The other alternative might be to combine the objects into one, though this might be impractical for your needs.

HTH

Log in or register to post comments