Log in or register to post comments

Call Java Method in Init_Rendering

October 19, 2013 - 11:24pm #1

hey , 

I'm working on application that run 3D Model And Video on diffrent targets , 

I'm having a problem while working on Virtual Button on the 3D model  , the Button Shows  , but i want to make the button on Pressed call a java method  

here's my Code : 

 

JNIEXPORT void JNICALL
Java_com_qualcomm_QCARSamples_VideoPlayback_VideoPlaybackRenderer_renderFrame(JNIEnv* env, jobject obj)
{

	// Clear color and depth buffer
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	// Get the state from QCAR and mark the beginning of a rendering section
	QCAR::State state = QCAR::Renderer::getInstance().begin();

	// Explicitly render the Video Background
	QCAR::Renderer::getInstance().drawVideoBackground();

	glEnable(GL_DEPTH_TEST);

	// We must detect if background reflection is active and adjust the culling direction.
	// If the reflection is active, this means the post matrix has been reflected as well,
	// therefore standard counter clockwise face culling will result in "inside out" models.
	glEnable(GL_CULL_FACE);
	glCullFace(GL_BACK);
	if(QCAR::Renderer::getInstance().getVideoBackgroundConfig().mReflection == QCAR::VIDEO_BACKGROUND_REFLECTION_ON)
	glFrontFace(GL_CW);//Front camera
	else
	glFrontFace(GL_CCW);//Back camera

	for (int i=0; i<NUM_TARGETS; i++)
	{
		isTracking[i] = false;
		targetPositiveDimensions[i].data[0] = 0.0;
		targetPositiveDimensions[i].data[1] = 0.0;
	}
		static float lastTime = 0.001f * getCurrentTimeMilliseconds();
 
    // if no targets => reset animation time and lastTrackableId
    if (state.getNumTrackableResults() == 0)
    {
     lastTime = 0.001f * getCurrentTimeMilliseconds();
     animationTime = 0;
     lastTrackableId = -1;
    }
	// Did we find any trackables this frame?
	


	for(int tIdx = 0; tIdx < state.getNumTrackableResults(); tIdx++)
	{
		// Get the trackable:
		const QCAR::TrackableResult* trackableResult = state.getTrackableResult(tIdx);

		const QCAR::ImageTarget& imageTarget = (const QCAR::ImageTarget&) trackableResult->getTrackable();

		const QCAR::ImageTargetResult* imageTargetResult = static_cast<const QCAR::ImageTargetResult*>(trackableResult);
		int currentTarget;


                if (strcmp(imageTarget.getName(), "pepsi") == 0){
					currentTarget = STONES;
			  const QCAR::TrackableResult* trackableResult = state.getTrackableResult(0);
					QCAR::Matrix44F modelViewMatrix =
				    QCAR::Tool::convertPose2GLMatrix(trackableResult->getPose());

        // The image target specific result:
        assert(trackableResult->getType() == QCAR::TrackableResult::IMAGE_TARGET_RESULT);
        const QCAR::ImageTargetResult* imageTargetResult =
            static_cast<const QCAR::ImageTargetResult*>(trackableResult);
        
        // Set transformations:
        QCAR::Matrix44F modelViewProjection;
        SampleUtils::multiplyMatrix(&projectionMatrix.data[0],
                                    &modelViewMatrix.data[0],
                                    &modelViewProjection.data[0]);


        // Set the texture used for the teapot model:
        int textureIndex = 0;

        GLfloat vbVertices[imageTargetResult->getNumVirtualButtons()*24];
        unsigned char vbCounter=0;

        // Iterate through this targets virtual buttons:
        for (int i = 0; i < imageTargetResult->getNumVirtualButtons(); ++i)
        {
            const QCAR::VirtualButtonResult* buttonResult = imageTargetResult->getVirtualButtonResult(i);
            const QCAR::VirtualButton& button = buttonResult->getVirtualButton();

            // If the button is pressed, than use this texture:
            if (buttonResult->isPressed())
            {
				LOG("Pressed");
								jclass javaClass = env->GetObjectClass(obj);
								LOG("javaClass");
								jmethodID method = env->GetMethodID(javaClass, "displayMessage", "(Ljava/lang/String;)V");
								LOG("method");
								env->CallVoidMethod(obj, method, "Virtual");
								LOG("CallVoidMethod");
		
            }

            const QCAR::Area* vbArea = &button.getArea();
            assert(vbArea->getType() == QCAR::Area::RECTANGLE);
            const QCAR::Rectangle* vbRectangle = static_cast<const QCAR::Rectangle*>(vbArea);


            // We add the vertices to a common array in order to have one single
            // draw call. This is more efficient than having multiple glDrawArray calls
            vbVertices[vbCounter   ]=vbRectangle->getLeftTopX();
            vbVertices[vbCounter+ 1]=vbRectangle->getLeftTopY();
            vbVertices[vbCounter+ 2]=0.0f;
            vbVertices[vbCounter+ 3]=vbRectangle->getRightBottomX();
            vbVertices[vbCounter+ 4]=vbRectangle->getLeftTopY();
            vbVertices[vbCounter+ 5]=0.0f;
            vbVertices[vbCounter+ 6]=vbRectangle->getRightBottomX();
            vbVertices[vbCounter+ 7]=vbRectangle->getLeftTopY();
            vbVertices[vbCounter+ 8]=0.0f;
            vbVertices[vbCounter+ 9]=vbRectangle->getRightBottomX();
            vbVertices[vbCounter+10]=vbRectangle->getRightBottomY();
            vbVertices[vbCounter+11]=0.0f;
            vbVertices[vbCounter+12]=vbRectangle->getRightBottomX();
            vbVertices[vbCounter+13]=vbRectangle->getRightBottomY();
            vbVertices[vbCounter+14]=0.0f;
            vbVertices[vbCounter+15]=vbRectangle->getLeftTopX();
            vbVertices[vbCounter+16]=vbRectangle->getRightBottomY();
            vbVertices[vbCounter+17]=0.0f;
            vbVertices[vbCounter+18]=vbRectangle->getLeftTopX();
            vbVertices[vbCounter+19]=vbRectangle->getRightBottomY();
            vbVertices[vbCounter+20]=0.0f;
            vbVertices[vbCounter+21]=vbRectangle->getLeftTopX();
            vbVertices[vbCounter+22]=vbRectangle->getLeftTopY();
            vbVertices[vbCounter+23]=0.0f;
            vbCounter+=24;

        }

        // We only render if there is something on the array
        if (vbCounter>0)
        {
            // Render frame around button
            glUseProgram(vbShaderProgramID);

            glVertexAttribPointer(vbVertexHandle, 3, GL_FLOAT, GL_FALSE, 0,
                (const GLvoid*) &vbVertices[0]);

            glEnableVertexAttribArray(vbVertexHandle);

            glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE,
                (GLfloat*)&modelViewProjection.data[0] );

            // We multiply by 8 because that's the number of vertices per button
            // The reason is that GL_LINES considers only pairs. So some vertices
            // must be repeated.
            glDrawArrays(GL_LINES, 0, imageTargetResult->getNumVirtualButtons()*8);

            SampleUtils::checkGlError("VirtualButtons drawButton");

            glDisableVertexAttribArray(vbVertexHandle);
        }

        // Assumptions:
        assert(textureIndex < textureCount);
        const Texture* const thisTexture = textures[textureIndex];
		    sodamodelViewMatrix[currentTarget] = QCAR::Tool::convertPose2GLMatrix(trackableResult->getPose());
                 
             

			   SampleUtils::translatePoseMatrix(0.0f, 0.0f, kObjectScale,
                                                 &sodamodelViewMatrix[currentTarget].data[0]);
												 
												 
				 ////// Rotation Code /////////////								 
				const QCAR::TrackableResult* result = state.getTrackableResult(tIdx);
				const QCAR::Trackable& trackable = result->getTrackable();
		
				if (trackable.getId() != lastTrackableId)
				{
					// reset animation time
					animationTime = 0;

					// Update lastTrackableId
					lastTrackableId = trackable.getId();
				}

				float currentTime = 0.001f * getCurrentTimeMilliseconds();

				float elapsedTime = currentTime - lastTime;

				animationTime += elapsedTime;

				lastTime = currentTime;

				// clamp to total duration
						if (animationTime > TOTAL_ANIMATION_DURATION)
				{
					animationTime = 0;
				}			 
				
				float totalRotationAngle = 90.0f;// use your custom value here
				float currentRotationAngle = 360.0f * (animationTime / TOTAL_ANIMATION_DURATION);
				SampleUtils::rotatePoseMatrix(currentRotationAngle, 0.f, 1.f, 0.f,&sodamodelViewMatrix[currentTarget].data[0]);
					//////////////// END Rotation Code /////////////////////
												 
                SampleUtils::scalePoseMatrix(kObjectScale, kObjectScale, kObjectScale,
                                             &sodamodelViewMatrix[currentTarget].data[0]);
                SampleUtils::multiplyMatrix(&projectionMatrix.data[0],
                                            &sodamodelViewMatrix[currentTarget].data[0] ,
                                            &modelViewProjection.data[0]);

                glUseProgram(shaderProgramID);

				

                glVertexAttribPointer(vertexHandle, 3, GL_FLOAT, GL_FALSE, 0,
                                      (const GLvoid*) &soda_canVerts[0]);
                glVertexAttribPointer(normalHandle, 3, GL_FLOAT, GL_FALSE, 0,
                                      (const GLvoid*) &soda_canNormals[0]);
                glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0,
                                      (const GLvoid*) &soda_canTexCoords[0]);

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

                glActiveTexture(GL_TEXTURE0);
                glBindTexture(GL_TEXTURE_2D, Itextures[currentTarget]->ITextureID);
                glUniform1i(texSampler2DHandle, 0 /*GL_TEXTURE0*/);
                glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE,
                                   (GLfloat*)&modelViewProjection.data[0] );
               //Ram glDrawElements(GL_TRIANGLES, NUM_TEAPOT_OBJECT_INDEX, GL_UNSIGNED_SHORT,
                //               (const GLvoid*) &teapotIndices[0]);
                glDrawArrays(GL_TRIANGLES, 0, soda_canNumVerts);
                SampleUtils::checkGlError("ImageTargets renderFrame");


				
                } }

 

Log in or register to post comments