Log in or register to post comments

OpenGL difficulties

December 31, 2010 - 7:33am #1

Hi everybody!

Here's what I'd like to do: Using the Dominoes sample I want to place the green glow texture -that is normally used for marking the starting Domino- in the center of the trackable the moment the trackable is recognized. And increase the diameter of the green texture.

Easy, right? Not if you're new to OpenGL like I am.

I looked at the OpenGL code of the ImageTargets app and the Dominoes app as well and experimented a bit. As a result I managed to place a tiny cube with the non-transparent green glow texture as its six sides in the center of the trackable.

That's almost what I wanted: I don't want a cube, I want a big but flat, slightly transparent texture where the tiny cube is now.

It would be great if someone could give me a hint how to do that. Beneath is my patchwork code that draws the unloved cube. The code is placed in the Java_com_qualcomm_QCARSamples_Dominoes_DominoesRenderer_renderFrame(JNIEnv* env , jobject obj) method in Dominoes.cpp.

 // Constants:
        const Texture* const greenGlowTexture = textures[1];
        static const float kObjectScale	= 3.f;

         
         QCAR::Matrix44F transform = SampleMath::Matrix44FIdentity();
        float* transformPtr = &transform.data[0];

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

		glVertexAttribPointer(vertexHandle, 3, GL_FLOAT, GL_FALSE, 0,
                              (const GLvoid*) &cubeVertices[0]);
        glVertexAttribPointer(normalHandle, 3, GL_FLOAT, GL_FALSE, 0,
                              (const GLvoid*) &cubeNormals[0]);
        glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0,
                              (const GLvoid*) &cubeTexCoords[0]);
        
        glEnableVertexAttribArray(vertexHandle);
        glEnableVertexAttribArray(normalHandle);
        glEnableVertexAttribArray(textureCoordHandle);

        glUseProgram(shaderProgramID);    
        
        
        glBindTexture(GL_TEXTURE_2D, greenGlowTexture->mTextureID);
        glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE,
                           (GLfloat*)&modelViewProjection.data[0] );
        glDrawElements(GL_TRIANGLES, NUM_CUBE_INDEX, GL_UNSIGNED_SHORT, (const GLvoid*) &cubeIndices[0]);

        SampleUtils::checkGlError("Dominoes renderFrame");

Re: OpenGL difficulties

June 2, 2011 - 11:08am #9

You're never rendering any geometry with the green glow texture. In the Dominoes sample we render a flattened cube, you could also just render a plane object (sample code at http://ar.qualcomm.at/node/2000357). You'll need another call to glDrawElements or glDrawArrays after setting up the geometry using glVertexAttribPointer.

- Kim

Re: OpenGL difficulties

June 1, 2011 - 11:20pm #8

I want greenglow appear on the bootom of my object in imagtargets.cpp

i modified the code in imagetargets.cpp :

glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);

    glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
.
.
.
.
const Texture* const greenGlowTexture = textures[1];
if (strcmp(trackable->getName(), "shika1") == 0)
        {

        	        	int textureIndex = 0;
        	 const Texture* const thisTexture = textures[textureIndex];
.
.
.
.
.
QCAR::Matrix44F modelViewProjection;

                       
                       SampleUtils::translatePoseMatrix(0.0f, 0.0f, kObjectScale,
                                                        &modelViewMatrix.data[0]);
                       //SampleUtils::rotatePoseMatrix(90.0f, 1.0f, 0, 0,
                                                            //&modelViewMatrix.data[0]);
                       SampleUtils::scalePoseMatrix(25.f, 25.f, 25.f,
                                                    &modelViewMatrix.data[0]);
                       SampleUtils::multiplyMatrix(&projectionMatrix.data[0],
                                                   &modelViewMatrix.data[0] ,
                                                   &modelViewProjection.data[0]);

                       glUseProgram(shaderProgramID);
                       glBindTexture(GL_TEXTURE_2D, greenGlowTexture->mTextureID);

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

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

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

                               glBindTexture(GL_TEXTURE_2D, thisTexture->mTextureID);
                               glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE,
                                                  (GLfloat*)&modelViewProjection.data[0] );
                               glDrawArrays(GL_TRIANGLES, 0, logooNumVerts);
.
.
.
. 
glDisable(GL_BLEND);
    glDisable(GL_DEPTH_TEST);

In imagetargets.java :

mTextures.add(Texture.loadTextureFromApk("tes.png",
                getAssets()));
        mTextures.add(Texture.loadTextureFromApk("green_glow.png", getAssets()));

But after i start the app, my object is appear, not with the greenglow..am i do simething wrong??

Re: OpenGL difficulties

January 4, 2011 - 7:01am #7

Alright, it works. Here's what I've done: As you suggested I set the OpenGL1.1 flag in the .mk file to true.
After ndk-building and recompiling it, the unloved cube returned. But this time I knew how to squash it: glScalef(kObjectScale, kObjectScale, 0);

To make it blend in nicely I used the old

glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
...
glDisable(GL_BLEND);

Now it's all good.

Cheers :)

Re: OpenGL difficulties

January 3, 2011 - 1:32pm #6

Did you change the Android.mk file to compile against OpenGL ES 1.1? Open the Android.mk file in the project's jni folder, and set the USE_OPENGL_ES_1_1 flag to true. Then rebuild the native code using ndk-build.

- Kim

Re: OpenGL difficulties

January 3, 2011 - 12:59pm #5

One more thing: Out of curiosity I wanted to render the green glow using only openGL 1.1 means.

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


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

        #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);
 
 #endif

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

The code compiles but when an object is recognized nothing is displayed on the trackable.
What's the trick? :)

Re: OpenGL difficulties

January 1, 2011 - 1:37pm #4

Thanks Kim, your code worked great (after removing the semicolon that is always placed before an ampersand by the board). The green glow looks good and I'm perfectly happy with it.

Re: OpenGL difficulties

December 31, 2010 - 8:53am #3

Oh, and increasing the diameter is especially easy. Simply change the kObjectScale to something bigger!

- Kim

Re: OpenGL difficulties

December 31, 2010 - 8:46am #2

I think we can help you with this :)

The first thing I notice when testing your code is that the cube looks a little off. It isn't very "solid," when looking at the top you can see through to the bottom. That's because you don't have depth testing turned on:

glEnable(GL_DEPTH_TEST);

< your rendering code goes here >

glDisable(GL_DEPTH_TEST);

Try that out just to see what I mean. It's not important for this problem, but in general you will see a lot of strangeness with 3D objects if depth testing isn't enabled.

Now let's turn your cube into a square. You could use a separate model consisting of just two triangles, but I cheated a bit in the Dominoes application and just squashed the cube (he was unloved anyways). This is slightly less efficient, but if you're only drawing a few it hardly matters. Change your scale line to this:

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

That will flatten the cube along the Z axis (which is perpendicular to the target).

Now you should have a flat square, but it still isn't blending correctly. For that, you need to enable blending and set a blending function:

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

< your rendering code goes here >

glDisable(GL_BLEND);

That particular glBlendFunc is the most common. As with the depth test, be sure to undo your OpenGL state change at the end of your render method.

Try all this out, and let me know if you get stuck.

- Kim

Log in or register to post comments