Exemplo n.º 1
0
JNIEXPORT jobject JNICALL
Java_org_ronhuang_vistroller_Vistroller_getMarker(JNIEnv *env, jobject)
{
    //LOG("Java_org_ronhuang_vistroller_Vistroller_getMarker");

    jobject result = NULL;

    QCAR::State state = QCAR::Renderer::getInstance().begin();

    for (int i = 0; i < state.getNumActiveTrackables(); i++) {
        const QCAR::Trackable *trackable = state.getActiveTrackable(i);
        assert(trackable->getType() == QCAR::Trackable::MARKER);
        const QCAR::Marker *marker = static_cast<const QCAR::Marker*>(trackable);

        result = newMarker(env, marker->getMarkerId(), marker->getPose().data, marker->getSize().data);
        break; // FIXME: return only the first one for now.
    }

    if (NULL == result) {
        // Make sure always return an instance of Trackable.
        // Return invalid one.
        float pose[3 * 4] = {
            0, 0, 0, 0,
            0, 0, 0, 0,
            0, 0, 0, 0
        };
        float size[2] = {0, 0};
        result = newMarker(env, -1, pose, size);
    }

    QCAR::Renderer::getInstance().end();

    return result;
}
Exemplo n.º 2
0
// Just returns 1 for OpenGLEs 1.1 and 2 for other
JNIEXPORT bool JNICALL
Java_edu_ethz_s3d_S3D_hasTarget(JNIEnv *, jobject)
{
	// Get the state from QCAR and mark the beginning of a rendering section
    QCAR::State state = QCAR::Renderer::getInstance().begin();

    return (state.getNumActiveTrackables() > 0);
}
Exemplo n.º 3
0
JNIEXPORT int JNICALL
Java_com_robopeak_EasyQCAR_EasyQCAR_getNumActiveTrackables(JNIEnv *, jobject)
{
    if (qcar_begun)
        return g_state.getNumActiveTrackables();
    else
        return 0;
}
Exemplo n.º 4
0
JNIEXPORT void JNICALL
Java_edu_pugetsound_vichar_ar_ARGameRenderer_renderFrame(JNIEnv * env, jobject obj, jboolean updated, jfloatArray test, jint objSize)
{
	bool update;
	update = (bool) updated; //so we know whether or not to update the drawlist.
	float testScale = 0.3f;

	// here is an example of how to pull the elements out of the jfloatArray. I think c++ will implicitly handle the type casting of jfloats as floats,
	// but if you are getting errors, you can always explicitly type cast them like so (assuming you have jfloats in the array):
	// float x;
	// x = (float) posData[i];
	
	if(update){
		int i = 0;
		int j = 0;
		jsize len = env->GetArrayLength(test);
		jfloat* posData = env->GetFloatArrayElements(test, 0);
		while(i<len && posData[(i/objSize)*objSize] != 0){
			LOG("JSON to JNI test. Pos. %d : %f", i, posData[i]); //print the elements of the array.
			interpList[i/objSize][i%objSize]= (float) posData[i] * testScale;
			i++;
		}
		interpLength=(i)/objSize;
		LOG("%i", interpLength);
		env->ReleaseFloatArrayElements(test, posData, 0); //release memory
	}

    //LOG("Java_edu_pugetsound_vichar_ar_GLRenderer_renderFrame");
    // 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();

#ifdef USE_OPENGL_ES_1_1
    // Set GL11 flags:
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    glEnable(GL_TEXTURE_2D);
    glDisable(GL_LIGHTING);

#endif

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);

    // Did we find any trackables this frame?
    for(int tIdx = 0; tIdx < state.getNumActiveTrackables(); tIdx++)
    {
        // Get the trackable:
        const QCAR::Trackable* trackable = state.getActiveTrackable(tIdx);
        QCAR::Matrix44F modelViewMatrix =
            QCAR::Tool::convertPose2GLMatrix(trackable->getPose());

//Begin additions by Erin================================================================================
        QCAR::Matrix34F test;   //gets inverse pos matrix
        QCAR::Matrix34F pos;   //Gets positional data
        pos = trackable->getPose();

        //Get inverse
        test = SampleMath::phoneCoorMatrix(trackable->getPose());

        //Print results
//        LOG("Poisiton:");
//        LOG("%f %f %f %f",pos.data[0], pos.data[1], pos.data[2], pos.data[3]);
//        LOG("%f %f %f %f",pos.data[4], pos.data[5], pos.data[6], pos.data[7]);
//        LOG("%f %f %f %f",pos.data[8], pos.data[9], pos.data[10],pos.data[11]);
//        LOG("Inverse:");
//        LOG("%f %f %f %f",test.data[0], test.data[1], test.data[2], test.data[3]);
//        LOG("%f %f %f %f",test.data[4], test.data[5], test.data[6], test.data[7]);
//        LOG("%f %f %f %f",test.data[8], test.data[9], test.data[10], test.data[11]);
//        LOG("=========================");
        phoneLoc[0] = 1.0f;
        phoneLoc[1] = test.data[3];
        phoneLoc[2] = test.data[7];
        phoneLoc[3] = test.data[11];
//End============================================================================================

        // Assign Textures according in the texture indices defined at the beginning of the file, and based
        // on the loadTextures() method in ARGameActivity.java.


        const Texture* const tower_shellTexture = textures[tower_shellIndex];
        const Texture* const tower_topTexture = textures[tower_topIndex];
        const Texture* const bananaTexture = textures[banana180Index];

#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);
        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]);
#else
		/* MATRIX GUIDE
		ModelViewMatrix = starting point matrix, with no mods is the center of the image target.
		Is only modified when not using Prefab transforms

		ObjectMatrix = ModelViewMatrix * transform matrix(in the case of prefab transforms)
		OR
		ObjectMatrix = ModelViewMatrix with all transforms performed.

		ProjectionMatrix = Is set utilizing a method in this class, when called by the Java. Is not modified by
		drawing process.

		ModelViewProjection = (w/ prefab transforms) ObjectMatrix * Projection
		ModelViewProjection = (w/o prefab transforms) Modified ModelViewMatrix * Projection
		*/


		drawCount=0;
		//Get List of Objects to Draw
		//obtain list of objects to draw -- fills drawList.
		updateDrawList();

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

		//Initialize intermediate and final Matricies
		//Intermediate matrix used only in multiplying using the transform matrix
		QCAR::Matrix44F objectMatrix;

		//The final matrix that is used to draw
		QCAR::Matrix44F modelViewProjection;

        glUseProgram(shaderProgramID);

		
		// Render the models
		for (int i = 0; i < drawCount; i++) {

			//Need to figure out which textures/verticies/etc to use here, currently hardcoded to turrets
			
			//Reinitalize ModelViewMatrix to its initialstate, as at this point it gets modified.
			modelViewMatrix = QCAR::Tool::convertPose2GLMatrix(trackable->getPose());

			//Loads the current model from the drawList
			Model* model = &drawList[i];

			//LOG("%i%i",i,drawCount);


			//These Lines used only for prefab transforms
			//QCAR::Matrix44F transform;
			//float* transform=&model->transform.data[0];
			
			//Test Prints to ensure data was being stored correctly
			//LOG("OBJECT ID:");
			//LOG("%i",model->id);
			

			//Verts,norms,texcords assigned here -- Is currently hardcoded to turret coords
			glVertexAttribPointer(vertexHandle, 3, GL_FLOAT, GL_FALSE, 0,
								 (const GLvoid*) &tower_topVerts[0]);
			glVertexAttribPointer(normalHandle, 3, GL_FLOAT, GL_FALSE, 0,
								  (const GLvoid*) &tower_topNormals[0]);
			glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0,
								  (const GLvoid*) &tower_topTexCoords[0]);


			//NON-HARDCODED VERSION
			/*
			glVertexAttribPointer(vertexHandle, 3, GL_FLOAT, GL_FALSE, 0,
								 (const GLvoid*) &model->&vertPointer);
			glVertexAttribPointer(normalHandle, 3, GL_FLOAT, GL_FALSE, 0,
								  (const GLvoid*) &model->&normalPointer);
			glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0,
								  (const GLvoid*) &model->&texPointer);
			*/
		
			//Open GL initialization
			glEnableVertexAttribArray(vertexHandle);
			glEnableVertexAttribArray(normalHandle);
			glEnableVertexAttribArray(textureCoordHandle);


			//Prep Transforms
			float* position=&model->pos[0];
			float* angle=&model->ang[0];

			//LOG("%f%f%f",position[0],position[1],position[2]);

			//Begin Transforms
			//BE WARY OF GETTING RID OR ADDING KObject SCALE
			SampleUtils::translatePoseMatrix(position[0], position[1], kObjectScale + position[2],
										&modelViewMatrix.data[0]);
			// So the tower_top appears upright
			SampleUtils::rotatePoseMatrix(90.0f + angle[0], 1.0f, 0.0f, 0.0f,
                        				&modelViewMatrix.data[0]);
			SampleUtils::rotatePoseMatrix(angle[1], 0.0f, 1.0f, 0.0f,
                        				&modelViewMatrix.data[0]);
			SampleUtils::rotatePoseMatrix(angle[2], 0.0f, 0.0f, 1.0f,
										&modelViewMatrix.data[0]);
			SampleUtils::scalePoseMatrix(kObjectScale, kObjectScale, kObjectScale,
										&modelViewMatrix.data[0]);

			//Combine projectionMatrix and modelViewMatrix to create final modelViewProejctionMatrix
			SampleUtils::multiplyMatrix(&projectionMatrix.data[0],
										&modelViewMatrix.data[0] ,
										&modelViewProjection.data[0]);

			
			//attempt to use stored transform matrix
			//SampleUtils::multiplyMatrix(&modelViewMatrix.data[0], transform, &objectMatrix.data[0]);
			//SampleUtils::multiplyMatrix(&projectionMatrix.data[0], &objectMatrix.data[0], &modelViewProjection.data[0]);
			//glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE, (GLfloat*)&modelViewProjection.data[0]);

			//Assign and bind texture -- once again this is hard coded to turrets
			glActiveTexture(GL_TEXTURE0);
			glBindTexture(GL_TEXTURE_2D, tower_topTexture->mTextureID);


			//un-hardcoding
			//glBindTexture(GL_TEXTURE_2D, model->&texturePointer->mTextureID);

			//apply modelViewProjectionMatrix
			glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE,
							   (GLfloat*)&modelViewProjection.data[0] );
			//Draw -- hardcoded to turret
			glDrawArrays(GL_TRIANGLES, 0, tower_topNumVerts);

			//Un-hardcoding
			//glDrawArrays(GL_TRIANGLES, 0, model->&NumVerts);
			
			//Unused method that previous attempted to draw.
			//renderModel(&model->transform.data[0]);
			}

		//Output given after every frame is fully rendered
//		LOG("Render Frame Complete");
		 
        SampleUtils::checkGlError("ImageTargets renderFrame");

#endif

    }

    glDisable(GL_DEPTH_TEST);

#ifdef USE_OPENGL_ES_1_1
    glDisable(GL_TEXTURE_2D);
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_NORMAL_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
#else
	//Deinit Open GL
    glDisableVertexAttribArray(vertexHandle);
    glDisableVertexAttribArray(normalHandle);
    glDisableVertexAttribArray(textureCoordHandle);
#endif
	//END
    QCAR::Renderer::getInstance().end();
}
Exemplo n.º 5
0
JNIEXPORT jboolean JNICALL
Java_com_snda_sdar_ImageTargetsRenderer_renderFrame(JNIEnv *env, jobject obj)
{
	//LOG("Java_com_snda_sdar_ImageTargets_GLRenderer_renderFrame");

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

	jboolean isDetected = false;

	// Render video background:
	QCAR::State state = QCAR::Renderer::getInstance().begin();

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

#ifdef USE_OPENGL_ES_1_1
	// Set GL11 flags:
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_NORMAL_ARRAY);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);

	glEnable(GL_TEXTURE_2D);
	glDisable(GL_LIGHTING);

#endif

	glEnable(GL_DEPTH_TEST);
	glEnable(GL_CULL_FACE);

	// Did we find any trackables this frame?
	for(int tIdx = 0; tIdx < state.getNumActiveTrackables(); tIdx++)
	{
		isDetected = true;

		// Get the trackable:
		const QCAR::Trackable* trackable = state.getActiveTrackable(tIdx);
		QCAR::Matrix44F modelViewMatrix =
		QCAR::Tool::convertPose2GLMatrix(trackable->getPose());

		// Choose the texture based on the target name:
		//int textureIndex = (!strcmp(trackable->getName(), "stones")) ? 0 : 1;

		const Texture* const thisTexture = textures[textureIndex];
		const Texture* const tagTexture = textures[textureCount - 1];

#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);
		glTexCoordPointer(2, GL_FLOAT, 0, (const GLvoid*) &planeTexCoords[0]);
		glVertexPointer(3, GL_FLOAT, 0, (const GLvoid*) &planeVertices[0]);
		glNormalPointer(GL_FLOAT, 0, (const GLvoid*) &planeNormals[0]);
		glDrawElements(GL_TRIANGLES, NUM_PLANE_OBJECT_INDEX, GL_UNSIGNED_SHORT,
				(const GLvoid*) &planeIndices[0]);

		//		// Load model view matrix:
		//		glMatrixMode(GL_MODELVIEW);
		//		glLoadMatrixf(modelViewMatrix.data);
		//		glTranslatef(50.f, 50.f, kObjectScale);
		//		glScalef(1, 1, 1);
		//
		//		// Draw object:
		//		glBindTexture(GL_TEXTURE_2D, tagTexture->mTextureID);
		//		glTexCoordPointer(2, GL_FLOAT, 0, (const GLvoid*) &planeTexCoords[0]);
		//		glVertexPointer(3, GL_FLOAT, 0, (const GLvoid*) &planeVertices[0]);
		//		glNormalPointer(GL_FLOAT, 0, (const GLvoid*) &planeNormals[0]);
		//		glDrawElements(GL_TRIANGLES, NUM_PLANE_OBJECT_INDEX, GL_UNSIGNED_SHORT,
		//				(const GLvoid*) &planeIndices[0]);
#else

		QCAR::Matrix44F modelViewMatrix2;

		for (int i = 0; i < 16; i++)
		{
			modelViewMatrix2.data[i] = modelViewMatrix.data[i];
		}

		QCAR::Matrix44F modelViewProjection;
		QCAR::Matrix44F modelViewProjection2;

		SampleUtils::translatePoseMatrix(translateX, translateY, kObjectScale,
				&modelViewMatrix.data[0]);
		SampleUtils::scalePoseMatrix(kObjectScale, kObjectScale, kObjectScale,
				&modelViewMatrix.data[0]);

		SampleUtils::rotatePoseMatrix(angleX, 0, 1, 0,
				&modelViewMatrix.data[0]);
		SampleUtils::rotatePoseMatrix(angleY, 1, 0, 0,
				&modelViewMatrix.data[0]);

		SampleUtils::multiplyMatrix(&projectionMatrix.data[0],
				&modelViewMatrix.data[0] ,
				&modelViewProjection.data[0]);

		glUseProgram(shaderProgramID);

		glVertexAttribPointer(vertexHandle, 3, GL_FLOAT, GL_FALSE, 0,
				(const GLvoid*) &planeVertices[0]);
		glVertexAttribPointer(normalHandle, 3, GL_FLOAT, GL_FALSE, 0,
				(const GLvoid*) &planeNormals[0]);
		glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0,
				(const GLvoid*) &planeTexCoords[0]);

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

		glActiveTexture(GL_TEXTURE0);
		glBindTexture(GL_TEXTURE_2D, thisTexture->mTextureID);
		glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE,
				(GLfloat*)&modelViewProjection.data[0] );
		glDrawElements(GL_TRIANGLES, NUM_PLANE_OBJECT_INDEX, GL_UNSIGNED_SHORT,
				(const GLvoid*) &planeIndices[0]);

		/////////////////////////////////////////////////////////////////////////////

		SampleUtils::translatePoseMatrix(50, 50, kObjectScale,
				&modelViewMatrix2.data[0]);
		SampleUtils::scalePoseMatrix(1, 1, 1,
				&modelViewMatrix2.data[0]);

		SampleUtils::multiplyMatrix(&projectionMatrix.data[0],
				&modelViewMatrix2.data[0] ,
				&modelViewProjection2.data[0]);

		glActiveTexture(GL_TEXTURE0);
		glBindTexture(GL_TEXTURE_2D, tagTexture->mTextureID);
		glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE,
				(GLfloat*)&modelViewProjection2.data[0] );
		glDrawElements(GL_TRIANGLES, NUM_PLANE_OBJECT_INDEX, GL_UNSIGNED_SHORT,
				(const GLvoid*) &planeIndices[0]);

		jclass renderClass = env->GetObjectClass(obj);

		jmethodID screenPointMethodId = env->GetMethodID(renderClass , "screenPoint", "(FF)V");
		if (!screenPointMethodId)
		{
			LOG("Function screenPoint(float, float) not found.");
			return 0;
		}

		const QCAR::CameraCalibration& cameraCalibration =
		QCAR::CameraDevice::getInstance().getCameraCalibration();
		QCAR::Vec2F cameraPoint = QCAR::Tool::projectPoint(cameraCalibration, trackable->getPose(), QCAR::Vec3F(0, 0, 0));

		QCAR::Vec2F screenPoint = cameraPointToScreenPoint(cameraPoint, true);

		env->CallObjectMethod(obj, screenPointMethodId, screenPoint.data[0], screenPoint.data[1]);

		SampleUtils::checkGlError("ImageTargets renderFrame");
#endif

	}

	glDisable(GL_DEPTH_TEST);

#ifdef USE_OPENGL_ES_1_1        
	glDisable(GL_TEXTURE_2D);
	glDisableClientState(GL_VERTEX_ARRAY);
	glDisableClientState(GL_NORMAL_ARRAY);
	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
#else
	glDisableVertexAttribArray(vertexHandle);
	glDisableVertexAttribArray(normalHandle);
	glDisableVertexAttribArray(textureCoordHandle);
#endif

	QCAR::Renderer::getInstance().end();

	return isDetected;
}
Exemplo n.º 6
0
// Does all the rendering stuff
JNIEXPORT void JNICALL
Java_edu_ethz_s3d_S3DRenderer_renderFrame(JNIEnv *, jobject)
{
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glEnable(GL_BLEND);

    // 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();
       
#ifdef USE_OPENGL_ES_1_1
    // Set GL11 flags:
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    glEnable(GL_TEXTURE_2D);
    glDisable(GL_LIGHTING);
        
#endif

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);

    // TODO: Only work, when there is exactly one trackable
    // Did we find any trackables this frame?
    for(int tIdx = 0; tIdx < state.getNumActiveTrackables(); tIdx++)
    {
        // Get the trackable:
        const QCAR::Trackable* trackable = state.getActiveTrackable(tIdx);
        QCAR::Matrix44F modelViewMatrix = QCAR::Tool::convertPose2GLMatrix(trackable->getPose());

        // Choose the texture based on the target name:
        int textureIndex;
        if (strcmp(trackable->getName(), "chips") == 0)
        {
            textureIndex = 0;
        }
        else if (strcmp(trackable->getName(), "stones") == 0)
        {
            textureIndex = 1;
        }
        else
        {
            textureIndex = 2;
        }

        const Texture* const thisTexture = textures[textureIndex];

#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);
        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]);
#else

        // Calculate the projection matrix
        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]);

        // Calculate the camera position
        QCAR::Matrix44F inverseModelView = SampleMath::Matrix44FTranspose(SampleMath::Matrix44FInverse(modelViewMatrix));
        QCAR::Vec3F cameraPosition(inverseModelView.data[12], inverseModelView.data[13], inverseModelView.data[14]);

        // Select the shader program
        glUseProgram(shaderProgramID);

        // Load the vertex attributes
        glVertexAttribPointer(vertexHandle, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) &teapotVertices[0]);
        glVertexAttribPointer(vertexColorHandle,3,GL_FLOAT, GL_FALSE, 0, (const GLvoid*) &color[0]);
        glVertexAttribPointer(normalHandle, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) &teapotNormals[0]);

        // Enable the vertex attributes
        glEnableVertexAttribArray(vertexHandle);
        glEnableVertexAttribArray(normalHandle);
        glEnableVertexAttribArray(vertexColorHandle);
        
        // Load the reconstruction data
        if (reconstructionHandler != NULL) {
        	// First delete the old texture
        	glDeleteTextures(1, &texId);
        	// Load the reconstruction texture and its corresponding data
        	texId = reconstructionHandler->getTexture();
            float slices[2] = { reconstructionHandler->getOverX(), reconstructionHandler->getOVerY() };
            glUniform2fv(glGetUniformLocation(shaderProgramID, "slicesOver"), 1, (const GLfloat*) slices);
            glUniform1f(glGetUniformLocation(shaderProgramID, "numberOfSlices"), reconstructionHandler->getNSlices());
        }
        else {
        	// Load the texture specifications for the white texture
            float slices[2] = { 2.f, 1.f };
            glUniform2fv(glGetUniformLocation(shaderProgramID, "slicesOver"), 1, (const GLfloat*) slices);
            glUniform1f(glGetUniformLocation(shaderProgramID, "numberOfSlices"), 2);
        }

        // Load the eye position into the shader program
        glUniform3fv(glGetUniformLocation(shaderProgramID, "eyePos"), 1, (const GLfloat*) &cameraPosition.data[0]);

        // Load the texture into texture0 and set uVolData to 0 to load it
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, texId);
        glUniform1i(glGetUniformLocation(shaderProgramID, "uVolData"), 0);

        // Load the projection matrix into its uniform
        glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE,
                           (GLfloat*)&modelViewProjection.data[0] );

        // Draw the cube
        glDrawElements(GL_TRIANGLES, NUM_TEAPOT_OBJECT_INDEX, GL_UNSIGNED_SHORT,
                       (const GLvoid*) &teapotIndices[0]);

        SampleUtils::checkGlError("ImageTargets renderFrame");
#endif

    }

    glDisable(GL_DEPTH_TEST);

#ifdef USE_OPENGL_ES_1_1        
    glDisable(GL_TEXTURE_2D);
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_NORMAL_ARRAY);
    glDisableClientState(GL_COLOR_ARRAY);
#else
    glDisableVertexAttribArray(vertexHandle);
    glDisableVertexAttribArray(normalHandle);
    glDisableVertexAttribArray(vertexColorHandle);
#endif

    QCAR::Renderer::getInstance().end();

    glDisable(GL_BLEND);
}
Exemplo n.º 7
0
JNIEXPORT void JNICALL
Java_com_qualcomm_QCARSamples_ImageTargets_ImageTargetsRenderer_renderFrame(JNIEnv *, jobject)
{
    //LOG("Java_com_qualcomm_QCARSamples_ImageTargets_GLRenderer_renderFrame");

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

    // Render video background:
    QCAR::State state = QCAR::Renderer::getInstance().begin();
        
#ifdef USE_OPENGL_ES_1_1
    // Set GL11 flags:
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    glEnable(GL_TEXTURE_2D);
    glDisable(GL_LIGHTING);
        
#endif

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);

    // Did we find any trackables this frame?
    for(int tIdx = 0; tIdx < state.getNumActiveTrackables(); tIdx++)
    {
        // Get the trackable:
        const QCAR::Trackable* trackable = state.getActiveTrackable(tIdx);
        QCAR::Matrix44F modelViewMatrix =
            QCAR::Tool::convertPose2GLMatrix(trackable->getPose());        

        // Choose the texture based on the target name:
        int textureIndex = (!strcmp(trackable->getName(), "stones")) ? 0 : 1;
        const Texture* const thisTexture = textures[textureIndex];

#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);
        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]);
#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);
         
        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]);
        
        glEnableVertexAttribArray(vertexHandle);
        glEnableVertexAttribArray(normalHandle);
        glEnableVertexAttribArray(textureCoordHandle);
        
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, thisTexture->mTextureID);
        glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE,
                           (GLfloat*)&modelViewProjection.data[0] );
        glDrawElements(GL_TRIANGLES, NUM_TEAPOT_OBJECT_INDEX, GL_UNSIGNED_SHORT,
                       (const GLvoid*) &teapotIndices[0]);

        SampleUtils::checkGlError("ImageTargets renderFrame");
#endif

    }

    glDisable(GL_DEPTH_TEST);

#ifdef USE_OPENGL_ES_1_1        
    glDisable(GL_TEXTURE_2D);
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_NORMAL_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
#else
    glDisableVertexAttribArray(vertexHandle);
    glDisableVertexAttribArray(normalHandle);
    glDisableVertexAttribArray(textureCoordHandle);
#endif

    QCAR::Renderer::getInstance().end();
}
Exemplo n.º 8
0
JNIEXPORT void JNICALL
Java_com_qualcomm_QCARSamples_VirtualButtons_VirtualButtonsRenderer_renderFrame(JNIEnv *, jobject)
{
    //LOG("Java_com_qualcomm_QCARSamples_VirtualButtons_GLRenderer_renderFrame");
 
    // Clear color and depth buffer 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Render video background:
    QCAR::State state = QCAR::Renderer::getInstance().begin();    

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);

    // Did we find any trackables this frame?
    if (state.getNumActiveTrackables())
    {
        // Get the trackable:
        const QCAR::Trackable* trackable = state.getActiveTrackable(0);
        QCAR::Matrix44F modelViewMatrix =
            QCAR::Tool::convertPose2GLMatrix(trackable->getPose()); 

        // The image target:
        assert(trackable->getType() == QCAR::Trackable::IMAGE_TARGET);
        const QCAR::ImageTarget* target =
            static_cast<const QCAR::ImageTarget*>(trackable);

        // 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[96];
        unsigned char vbCounter=0;

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

            // If the button is pressed, than use this texture:
            if (button->isPressed())
            {
                // Run through button name array to find texture index
                for (int j = 0; j < NUM_BUTTONS; ++j)
                {
                    if (strcmp(button->getName(), virtualButtonColors[j]) == 0)
                    {
                        textureIndex = j+1;
                        break;
                    }
                }
            }            
            
            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, target->getNumVirtualButtons()*8); 

            SampleUtils::checkGlError("VirtualButtons drawButton");

            glDisableVertexAttribArray(vbVertexHandle);
        }

        // Assumptions:
        assert(textureIndex < textureCount);
        const Texture* const thisTexture = textures[textureIndex];                            

        
        // Scale 3D model
        QCAR::Matrix44F modelViewScaled = modelViewMatrix;
        SampleUtils::scalePoseMatrix(kTeapotScale, kTeapotScale, kTeapotScale,
                                     &modelViewScaled.data[0]);

        QCAR::Matrix44F modelViewProjectionScaled;
        SampleUtils::multiplyMatrix(&projectionMatrix.data[0],
                                    &modelViewScaled.data[0],
                                    &modelViewProjectionScaled.data[0]);
                                    
        // Render 3D model
        glUseProgram(shaderProgramID);
 
        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]);

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

        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, thisTexture->mTextureID);
        glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE,
                           (GLfloat*)&modelViewProjectionScaled.data[0] );
        glDrawElements(GL_TRIANGLES, NUM_TEAPOT_OBJECT_INDEX, GL_UNSIGNED_SHORT,
                       (const GLvoid*) &teapotIndices[0]);

        SampleUtils::checkGlError("VirtualButtons renderFrame");

    }
    
    glDisable(GL_DEPTH_TEST);

    glDisableVertexAttribArray(vertexHandle);
    glDisableVertexAttribArray(normalHandle);
    glDisableVertexAttribArray(textureCoordHandle);

    QCAR::Renderer::getInstance().end();
}
JNIEXPORT void JNICALL
Java_name_nailgun_irrlichtvuforia_Renderer_renderFrame(JNIEnv *, jobject)
{
    QCAR::State state = QCAR::Renderer::getInstance().begin();
    QCAR::Renderer::getInstance().drawVideoBackground();
       
#ifndef DONT_SAVE_STATE
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    glEnable(GL_TEXTURE_2D);
    glDisable(GL_LIGHTING);

    glEnable(GL_DEPTH_TEST);
    //glEnable(GL_CULL_FACE);
#endif

    // Did we find any trackables this frame?
    for(int tIdx = 0; tIdx < state.getNumActiveTrackables(); tIdx++)
    {
        // Get the trackable:
        const QCAR::Trackable* trackable = state.getActiveTrackable(tIdx);
        QCAR::Matrix44F modelViewMatrix =
            QCAR::Tool::convertPose2GLMatrix(trackable->getPose());        

        // Choose the texture based on the target name:
        int textureIndex;
        if (strcmp(trackable->getName(), "chips") == 0)
        {
            textureIndex = 0;
        }
        else if (strcmp(trackable->getName(), "stones") == 0)
        {
            textureIndex = 1;
        }
        else
        {
            textureIndex = 2;
        }

        if (!mDevice->run()) {
            // TODO: error
        }

        irr::core::matrix4& cameraMatrix = mTransformationNode->getRelativeTransformationMatrix();
        cameraMatrix.setM(modelViewMatrix.data);

        mDriver->beginScene(false, true);
        mSceneManager->drawAll();
        mDriver->endScene();
    }

#ifndef DONT_SAVE_STATE        
    glDisable(GL_DEPTH_TEST);

    glDisable(GL_TEXTURE_2D);
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_NORMAL_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
#endif

    QCAR::Renderer::getInstance().end();
}
Exemplo n.º 10
0
JNIEXPORT void JNICALL
Java_com_mx_ipn_escom_ars_recorrido_ArsRenderer_renderFrame2(JNIEnv *env, jobject obj)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    QCAR::State state = QCAR::Renderer::getInstance().begin();
    QCAR::Renderer::getInstance().drawVideoBackground();

#ifdef USE_OPENGL_ES_1_1
    // Set GL11 flags:
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);
//    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    glEnable(GL_TEXTURE_2D);
    glEnable(GL_LIGHTING);
//    glDisable(GL_LIGHTING);

#endif

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);

    // Did we find any trackables this frame?
    for(int tIdx = 0; tIdx < state.getNumActiveTrackables(); tIdx++)
    {
        // Get the trackable:
        const QCAR::Trackable* trackable = state.getActiveTrackable(tIdx);
        QCAR::Matrix44F modelViewMatrix =
            QCAR::Tool::convertPose2GLMatrix(trackable->getPose());

#ifdef USE_OPENGL_ES_1_1
        // Load projection matrix:
        glMatrixMode(GL_PROJECTION);
        glLoadMatrixf(projectionMatrix2.data);

        // Load model view matrix:
        glMatrixMode(GL_MODELVIEW);
        glLoadMatrixf(modelViewMatrix.data);
        glTranslatef(0.f, 0.f, kObjectScale);
        glScalef(kObjectScale, kObjectScale, kObjectScale);

        jclass javaClass = env->GetObjectClass(obj);

        char *buf = (char*)malloc(10);
        strcpy(buf, trackable->getName());
   	    jstring jstrBuf = env->NewStringUTF(buf);

	    jmethodID method1 = env->GetMethodID(javaClass, "prueba2", "(Ljava/lang/String;)V");

	    env->CallVoidMethod(obj, method1,jstrBuf);
	    env->DeleteLocalRef(jstrBuf);

#endif

    }

    glDisable(GL_DEPTH_TEST);

#ifdef USE_OPENGL_ES_1_1
//    glDisable(GL_TEXTURE_2D);
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_NORMAL_ARRAY);
#endif

    QCAR::Renderer::getInstance().end();
}