Log in or register to post comments

Adjust image which added as texture on virtual button sample cod

September 27, 2012 - 4:59am #1

I  am making an application in which i used opengl code to add image as texture onto virtual button sample code , and i got help from internet and add image as texture but now problem is i want to show this image of specific place but i couldn't understand how to handle this, because i am new bee in opengl. here is code

static const float planeVertices[] = { -0.5, -0.5, 0.0, 0.5, -0.5, 0.0, 0.5, 0.5, 0.0, -0.5, 0.5, 0.0, };

                  static const float planeTexcoords[] = { 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0 };

                  static const float planeNormals[] = { 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0 };

                  static const unsigned short planeIndices[] = { 0, 1, 2, 0, 2, 3 };

                  QCAR::Vec2F targetSize = ((QCAR::ImageTarget *) trackable)->getSize();

                  QCAR::Matrix44F modelViewProjection;

                  ShaderUtils::translatePoseMatrix(0.0f, 0.0f, kObjectScale, &modelViewMatrix.data[0]);

                  ShaderUtils::scalePoseMatrix(targetSize.data[0]/8, targetSize.data[1]/8, 1.0f, &modelViewMatrix.data[0]);
                  ShaderUtils::multiplyMatrix(&qUtils.projectionMatrix.data[0], &modelViewMatrix.data[0] , &modelViewProjection.data[0]);
                 // glTranslatef(100.0f,0.5f, -kObjectScale);
                  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 textureID]);
                  glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE, (GLfloat*)&modelViewProjection.data[0] );
                  glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (const GLvoid*) &planeIndices[0]);
Thanks in Advance. Have a great day.

Adjust image which added as texture on virtual button sample cod

September 28, 2012 - 2:55am #4

Hi Aleem

I want to change image position on bottom right

I am not quite sure I understand what you mean, though I am guessing that you want to move (translate) the plane somehow.

I think your best bet is to learn some OpenGL ES and there are plenty of Tutorials on the web - unfortunately there is no shortcut, which is why we generally recommend Unity as it's far easier to learn.

If you simply want to translate, below is some sample code showing how to do this that will draw a 2nd teapot in the image targets sample.

HTH

N

 

 

#ifndef USE_OPENGL1

        else {

            // OpenGL 2

            QCAR::Matrix44F modelViewProjection;

            

            ShaderUtils::translatePoseMatrix(0.0f, 0.0f, kObjectScale, &modelViewMatrix.data[0]);

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

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

            

            glUseProgram(shaderProgramID);

            

            glVertexAttribPointer(vertexHandle, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)obj3D.vertices);

            glVertexAttribPointer(normalHandle, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)obj3D.normals);

            glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)obj3D.texCoords);

            

            glEnableVertexAttribArray(vertexHandle);

            glEnableVertexAttribArray(normalHandle);

            glEnableVertexAttribArray(textureCoordHandle);

            

            glActiveTexture(GL_TEXTURE0);

            glBindTexture(GL_TEXTURE_2D, [obj3D.texture textureID]);

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

            glDrawElements(GL_TRIANGLES, obj3D.numIndices, GL_UNSIGNED_SHORT, (const GLvoid*)obj3D.indices);

 

            

            // second teapot

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

            

            ShaderUtils::translatePoseMatrix(10.0f, 0.0f, kObjectScale, &modelViewMatrix.data[0]);

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

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

            

            glUseProgram(shaderProgramID);

            

            glVertexAttribPointer(vertexHandle, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)obj3D.vertices);

            glVertexAttribPointer(normalHandle, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)obj3D.normals);

            glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)obj3D.texCoords);

            

            glEnableVertexAttribArray(vertexHandle);

            glEnableVertexAttribArray(normalHandle);

            glEnableVertexAttribArray(textureCoordHandle);

            

            glActiveTexture(GL_TEXTURE0);

            glBindTexture(GL_TEXTURE_2D, [obj3D.texture textureID]);

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

            glDrawElements(GL_TRIANGLES, obj3D.numIndices, GL_UNSIGNED_SHORT, (const GLvoid*)obj3D.indices);

            

 

            

            ShaderUtils::checkGlError("EAGLView renderFrameQCAR");

        }

#endif

 

Adjust image which added as texture on virtual button sample cod

September 27, 2012 - 10:47pm #3

I almost did same thing of your answer(following showed) which you shared link with me but i want to adjust image position (x,y). I want to change image position on bottom right but i couldn't understand how to do this. Please help me to this.

 

Take the image targets app and in EAGLView.mm using the following code at the top to define the plane:

static const float planeVertices[] =

{

    -0.5, -0.5, 0.0, 0.5, -0.5, 0.0, 0.5, 0.5, 0.0, -0.5, 0.5, 0.0,

};

 

static const float planeTexcoords[] =

{

    0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0

};

 

static const float planeNormals[] =

{

    0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0

};

 

static const unsigned short planeIndices[] =

{

    0, 1, 2, 0, 2, 3

};

 

 

Then in the rendering section under the OpenGL 2 part, comment the teapot rendering code out and replace it with the following:

 

            QCAR::Vec2F targetSize = ((QCAR::ImageTarget *) trackable)->getSize();

            

            QCAR::Matrix44F modelViewProjection;

            

            ShaderUtils::translatePoseMatrix(0.0f, 0.0f, kObjectScale,

                                             &modelViewMatrix.data[0]);

            ShaderUtils::scalePoseMatrix(targetSize.data[0], targetSize.data[1], 1.0f,

                                         &modelViewMatrix.data[0]);

            ShaderUtils::multiplyMatrix(&qUtils.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, [obj3D.texture textureID]);

            glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE,

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

            glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT,

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

Did same thing but want to change x and y coordinate. Thanks

Adjust image which added as texture on virtual button sample cod

September 27, 2012 - 6:48am #2

Hi Aleem

Not quite sure what you are trying to do here, but you might find this thread useful:

https://ar.qualcomm.at/content/drawing-rectangle-besides-marker-and-getting-its-coordinates-fill-image

...as I posted some code that would overlay a plane on top of an image target.

If you are new to OpenGL and you want to check your draw code then it would be an idea to have a test harness that runs in the simulator which can let you quickly test that an object draws correctly, and then you can import into your app that runs on the device.

HTH

N

Log in or register to post comments