Log in or register to post comments

How to do this ?? (Virtualbuttons)

January 24, 2012 - 1:17am #1

I want to use buttons to change object.
when i press the Red button i want to show banana on market, blue button to show spider, yellow button to show robot and green buttons to show vase.

-so sorry about my bad English-

Re: How to do this ?? (Virtualbuttons)

March 5, 2012 - 10:46pm #16

Thank you Kim
I set statebutton = 0 at these point.

// We only render if there is something on the array
        if (vbCounter>0)
        {
			 statebutton=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);
        }

        if(statebutton ==0){

							const Texture* const thisTexture = textures[textureIndex];

									// Scale 3D model
							QCAR::Matrix44F modelViewScaled = modelViewMatrix;
							SampleUtils::scalePoseMatrix(kbananaScale, kbananaScale, kbananaScale,
														 &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*) &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]);

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

							glActiveTexture(GL_TEXTURE0);
							glBindTexture(GL_TEXTURE_2D, thisTexture->mTextureID);
							glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE,
											   (GLfloat*)&modelViewProjectionScaled.data[0] );
							glDrawArrays(GL_TRIANGLES, 0, bananaNumVerts);
							}

        SampleUtils::checkGlError("VirtualButtons renderFrame");

    }
    
    glDisable(GL_DEPTH_TEST);

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

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

Result is banana always show.
It overlap with new object when I press the button.
I Think I set it wrong place.
I don't know where to set it.

Re: How to do this ?? (Virtualbuttons)

March 5, 2012 - 8:20am #15

You need to set statebutton = 0 at some point. Try doing this at the beginning of the renderFrame function.

- Kim

Re: How to do this ?? (Virtualbuttons)

March 4, 2012 - 11:11am #14

this is code

#ifdef __cplusplus
extern "C"
{
#endif
int statebutton=0;
// Textures:
int textureCount                = 0;
Texture** textures              = 0;
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;
						statebutton = 1;
						 if (j==0)
							{
							const Texture* const thisTexture = textures[textureIndex];


							 // Scale 3D model
							QCAR::Matrix44F modelViewScaled = modelViewMatrix;
							SampleUtils::scalePoseMatrix(kSpiderScale, kSpiderScale, kSpiderScale,
														 &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*) &spiderVerts[0]);
							glVertexAttribPointer(normalHandle, 3, GL_FLOAT, GL_FALSE, 0,
												  (const GLvoid*) &spiderNormals[0]);
							glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0,
												  (const GLvoid*) &spiderTexCoords[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] );
							glDrawArrays(GL_TRIANGLES, 0, spiderNumVerts);
                                    
							}

						 if (j==1)
							{
				
							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]);
							}

						 if (j==2)
							{
	
							const Texture* const thisTexture = textures[textureIndex];

							// Scale 3D model
							QCAR::Matrix44F modelViewScaled = modelViewMatrix;
							SampleUtils::scalePoseMatrix(kSpiderexScale, kSpiderexScale, kSpiderexScale,
														 &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*) &spiderexVerts[0]);
							glVertexAttribPointer(normalHandle, 3, GL_FLOAT, GL_FALSE, 0,
												  (const GLvoid*) &spiderexNormals[0]);
							glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0,
												  (const GLvoid*) &spiderexTexCoords[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] );
							glDrawArrays(GL_TRIANGLES, 0, spiderexNumVerts);
							}

						 if (j==3)
							{
				
							const Texture* const thisTexture = textures[textureIndex];

							// Scale 3D model
							QCAR::Matrix44F modelViewScaled = modelViewMatrix;
							SampleUtils::scalePoseMatrix(kVaseScale, kVaseScale, kVaseScale,
														 &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*) &vaseVerts[0]);
							glVertexAttribPointer(normalHandle, 3, GL_FLOAT, GL_FALSE, 0,
												  (const GLvoid*) &vaseNormals[0]);
							glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0,
												  (const GLvoid*) &vaseTexCoords[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] );
							glDrawArrays(GL_TRIANGLES, 0, vaseNumVerts);
							}

                        break;
                    }
                }
            }            
            
 
// 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);
        }

        if(statebutton ==0){

							const Texture* const thisTexture = textures[textureIndex];

									// Scale 3D model
							QCAR::Matrix44F modelViewScaled = modelViewMatrix;
							SampleUtils::scalePoseMatrix(kbananaScale, kbananaScale, kbananaScale,
														 &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*) &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]);

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

							glActiveTexture(GL_TEXTURE0);
							glBindTexture(GL_TEXTURE_2D, thisTexture->mTextureID);
							glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE,
											   (GLfloat*)&modelViewProjectionScaled.data[0] );
							glDrawArrays(GL_TRIANGLES, 0, bananaNumVerts);
							}

what can I do?

Re: How to do this ?? (Virtualbuttons)

March 4, 2012 - 11:09am #13

Now, I can use the buttons to change object but I have some problem.

The first object that show on maker when I don't press the button is banana.
I want it to disapear when I press the button to change object.
the new object will show instead of banana.>>>this part I can do.

I want when I don't press the button, banana will show again on marker. >>> this part i can't do . Banana don't show again when I press button (first time) then don't press . Banana will show on marker before I don't press the button(first time) only.

Re: How to do this ?? (Virtualbuttons)

February 20, 2012 - 6:52pm #12

Thank you so much .:)

Re: How to do this ?? (Virtualbuttons)

February 9, 2012 - 3:43pm #11

Basic coding skills are a requirement for working with the SDK. Pick apart that renderFrame method until you understand it, then you should be able to work with it to conditionally render the model you want.

You may also want to look at the FrameMarkers sample. It renders a different model on each marker, that code might be a useful reference.

- Kim

Re: How to do this ?? (Virtualbuttons)

February 8, 2012 - 11:22pm #10

I already define kSpiderScale.
It not error right now but noting appear on my trackables.

I know about c++ a little bit .
Thus
can you tell me how to write conditionally render the teapot when buttons aren't pressed or guide me a little bit.

thank a lot:o

Re: How to do this ?? (Virtualbuttons)

February 7, 2012 - 9:22pm #9

Did you define kSpiderScale? Make sure you do it before it is used in the file.

We're getting into basic coding problems now. You'll need to conditionally render the teapot when buttons aren't pressed.

- Kim

Re: How to do this ?? (Virtualbuttons)

February 7, 2012 - 12:32am #8

Thank you so much Kim:)
I remove it already but teapot is disappear. I want teapot appear on target when I don't press the buttons.

And when I build VirtualButtons shared library It error about kSpiderScale

C:/development/android/qcar-android-1-0-6/samples/virtualbuttons/jni/VirtualButt ons.cpp: In function 'void Java_com_qualcomm_QCARSamples_VirtualButtons_VirtualB uttonsRenderer_renderFrame(JNIEnv*, _jobject*)':
C:/development/android/qcar-android-1-0-6/samples/virtualbuttons/jni/VirtualButt ons.cpp:284: error: 'kSpiderScale' was not declared in this scope
/cygdrive/c/Development/Android/android-ndk-r6/build/core/build-binary.mk:220: r ecipe for target `/cygdrive/c/development/android/qcar-android-1-0-6/samples/vir tualbuttons/obj/local/armeabi/objs-debug/VirtualButtons/VirtualButtons.o' failed
make: *** [/cygdrive/c/development/android/qcar-android-1-0-6/samples/virtualbut tons/obj/local/armeabi/objs-debug/VirtualButtons/VirtualButtons.o] Error 1

This is code

// 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;
		// Assumptions:
        assert(textureIndex < textureCount);
        const Texture* const thisTexture = textures[textureIndex];

		// Scale 3D model
        QCAR::Matrix44F modelViewScaled = modelViewMatrix;
        SampleUtils::scalePoseMatrix(kSpiderScale, kSpiderScale, kSpiderScale,
                                     &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*) &spiderVerts[0]);
        glVertexAttribPointer(normalHandle, 3, GL_FLOAT, GL_FALSE, 0,
                              (const GLvoid*) &spiderNormals[0]);
        glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0,
                              (const GLvoid*) &spiderTexCoords[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] );
        glDrawArrays(GL_TRIANGLES, 0, spiderNumVerts);

                        break;
                    }
                }
            } 

Re: How to do this ?? (Virtualbuttons)

February 6, 2012 - 7:24pm #7

It's very similar to the code you posted here. Comment out this section (towards the end of the function):

        // 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]);

- Kim

Re: How to do this ?? (Virtualbuttons)

February 5, 2012 - 7:49pm #6

thank you Kim.
what the teapot rendering code that i must remove?:p

Re: How to do this ?? (Virtualbuttons)

February 3, 2012 - 2:48pm #5

Did you remove the teapot rendering code at the end of that function? If not, you'll end up rendering the teapot over your model, most likely hiding it.

As always, make sure you're running ndk-build and refreshing (or cleaning) the Eclipse project after making changes to the native code.

- Kim

Re: How to do this ?? (Virtualbuttons)

February 2, 2012 - 9:13pm #4

Kim
I try it already. (my object name is spider)

// 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)
                    {
						 // Assumptions:
        assert(textureIndex < textureCount);
        const Texture* const thisTexture = textures[textureIndex];

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

        GLfloat vbVertices[96];
        unsigned char vbCounter=0;

		 // 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*) &spiderVerts[0]);
        glVertexAttribPointer(normalHandle, 3, GL_FLOAT, GL_FALSE, 0,
                              (const GLvoid*) &spiderNormals[0]);
        glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0,
                              (const GLvoid*) &spiderTexCoords[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] );
       glDrawArrays(GL_TRIANGLES, 0, spiderNumVerts);
                        break;
                    }
                }
            }             

instead this script

// 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;
        }
    }
}

i think,i do it wrong.
because when i use it, the object not change and the texure not change too. just show teapot only.

sorry for my foolish :o

Re: How to do this ?? (Virtualbuttons)

February 2, 2012 - 7:12pm #3

Thank you so much Kim. I will try to do.

Re: How to do this ?? (Virtualbuttons)

January 24, 2012 - 8:04pm #2

Start with the VirtualButtons sample. Take a look at the renderFrame method in VirtualButtons.cpp. This part is where the button press is handled:

// 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;
        }
    }
}

This code picks the texture index based on the button that is currently pressed. You can update it to pick a particular model to render as well. Do this by swapping the vertices, normals, texcoords, and indices used later in the same function.

- Kim

Log in or register to post comments