Log in or register to post comments

Loading another 3D model on touch of screen

August 17, 2012 - 11:14am #1

Hey guys

In ImageTargets application i want to initially show a 3D model and then when user touches the screen another 3D model should come on the same tracker image. 

In the ImageTargets.java file i have added this piece of code which intimates the cpp file when the user touches the screen 

I have modified the ImageTargets.cpp as follows :-

 public boolean onTouchEvent(MotionEvent event) {

    // TODO Auto-generated method stub

    int eventId = event.getAction();

     switch (eventId){

    case MotionEvent.ACTION_DOWN:

   callFromJava();  

break;

    }

    return super.onTouchEvent(event);

    }

 

After this i have done the changes in the ImageTargets.cpp file. I have added the callFromJava() function in the file as follows :-

Java_com_qualcomm_QCARSamples_ImageTargets_ImageTargets_callFromJava(JNIEnv *, jobject)

{

modelId = 1 ;

//return 1;

}


where modelId is the int variable in the file. After this i have done the following code in the renderFrame() method to add logic i.e. when application starts the modelId variable is 0 and when user touches the screen modelId = 1 and according to this the other 3D model should load . I have created another 3D model .h file and it works well separately so there should no be any issue in it. 

#ifdef USE_OPENGL_ES_1_1

        // Load projection matrix:

        glMatrixMode(GL_PROJECTION);

        glLoadMatrixf(projectionMatrix.data);

 

        // Load model view matrix:

        glMatrixMode(GL_MODELVIEW);

        glLoadMatrixf(modelViewMatrix.data);

        glTranslatef(0.f, 0.f, kObjectScale);

        glScalef(kObjectScale, kObjectScale, kObjectScale);

 

        // Draw object:

        glBindTexture(GL_TEXTURE_2D, thisTexture->mTextureID);

 

        if(modelId == 1){

 

         glTexCoordPointer(2, GL_FLOAT, 0, (const GLvoid*) &teapotTexCoords[0]);

        glVertexPointer(3, GL_FLOAT, 0, (const GLvoid*) &teapotVertices[0]);

        glNormalPointer(GL_FLOAT, 0,  (const GLvoid*) &teapotNormals[0]);

        glDrawElements(GL_TRIANGLES, NUM_TEAPOT_OBJECT_INDEX, GL_UNSIGNED_SHORT,

                       (const GLvoid*) &teapotIndices[0]);

        LOG("modelID %d", modelId);

        }else{

        glTexCoordPointer(2, GL_FLOAT, 0, (const GLvoid*) &bananaTexCoords[0]);

        glVertexPointer(3, GL_FLOAT, 0, (const GLvoid*) &bananaVerts[0]);

                glNormalPointer(GL_FLOAT, 0,  (const GLvoid*) &bananaNormals[0]);

 

                glDrawArrays(GL_TRIANGLES, 0, bananaNumVerts);

                LOG("modelID %d", modelId);

        }

#else

 

        QCAR::Matrix44F modelViewProjection;

 

        SampleUtils::translatePoseMatrix(0.0f, 0.0f, kObjectScale,

                                         &modelViewMatrix.data[0]);

        SampleUtils::scalePoseMatrix(kObjectScale, kObjectScale, kObjectScale,

                                     &modelViewMatrix.data[0]);

        SampleUtils::multiplyMatrix(&projectionMatrix.data[0],

                                    &modelViewMatrix.data[0] ,

                                    &modelViewProjection.data[0]);

 

        glUseProgram(shaderProgramID);

         

        if(modelId == 0){

glVertexAttribPointer(vertexHandle, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) &bananaVerts[0]);

glVertexAttribPointer(normalHandle, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) &bananaNormals[0]);

glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) &bananaTexCoords[0]);

LOG("modelID %d in else", modelId);

        }else{

 

        glVertexAttribPointer(vertexHandle, 3, GL_FLOAT, GL_FALSE, 0,(const GLvoid*) &teapotVertices[0]);

        glVertexAttribPointer(normalHandle, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) &teapotNormals[0]);

        glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0,(const GLvoid*) &teapotTexCoords[0]);

        LOG("modelID %d in else", modelId);

}

 

        glEnableVertexAttribArray(vertexHandle);

        glEnableVertexAttribArray(normalHandle);

        glEnableVertexAttribArray(textureCoordHandle);

        

        glActiveTexture(GL_TEXTURE0);

        glBindTexture(GL_TEXTURE_2D, thisTexture->mTextureID);

        glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE,

                           (GLfloat*)&modelViewProjection.data[0] );

        if(modelId == 0){

glDrawArrays(GL_TRIANGLES, 0, bananaNumVerts);

LOG("modelID %d last", modelId);

        }else{

        glDrawElements(GL_TRIANGLES, NUM_TEAPOT_OBJECT_INDEX, GL_UNSIGNED_SHORT,

                               (const GLvoid*) &teapotIndices[0]);

        LOG("modelID %d last", modelId);

        }

        SampleUtils::checkGlError("ImageTargets renderFrame");

#endif

    }

    glDisable(GL_DEPTH_TEST);


But still i am not able to see the teapot image . I can see the banana 3D model when i touch the screen. 

I have included both Teapot.h and banan.h in cpp 


Please guide me if this is not the way to achieve this goal.

Thanks in advance 

Loading another 3D model on touch of screen

August 22, 2012 - 3:22am #3

wow that was really helpful ^____^
thanks
also works for me :3

Loading another 3D model on touch of screen

August 17, 2012 - 1:58pm #2

I got it 

 

When i use glDrawElements and glDrawArrays then only glDrawArrays work. So in both the cases i loaded my own .h files and got the working using 

glDrawArrays() Cool hopefully will get my first android app live 

Log in or register to post comments