Log in or register to post comments

create texturer dynamically

February 5, 2013 - 5:42am #1

HI dude

     I have to make  the texture  dynamically .

    i tried to do this with the help of this link   : https://developer.vuforia.com/forum/faq/android-how-do-i-add-textures-my-model

    but  i am getting black texture .

 

 

 

 
JNIEXPORT void JNICALL
Java_com_adstuck_times_iar_VideoPlayback_createGLTextureNative(
                                                    JNIEnv* env, jobject obj, jobject textureObjectNew)
{
    if (textureObjectNew != NULL)
    {
 
        myTexture = Texture::create(env, textureObjectNew);
        glGenTextures(1, &(myTexture->mTextureID));
        glBindTexture(GL_TEXTURE_2D, myTexture->mTextureID);
 
       // if ( myTexture->mTextureID != NULL)
        {
        LOG("texture is not nulllllllll   =======================>");    //  here i got the print statement.
        }
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
 
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, myTexture->mWidth,
                     myTexture->mHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE,(GLvoid*) myTexture->mData);
        LOG("dynamic texture created   ############################");  // here i got the print statement
 
 
 
    }
}
 
but when i use it ...............
 
void renderTransitionTo2D()
{
 
checkRender=false;
 
if (startTransition3Dto2D)
{
 
// Starts the Transition
transition3Dto2D->startTransition(transitionDuration, false, true);
LOG("if startTransition3Dto2D is true-------------------------------->");
// Initialize control state variables
startTransition3Dto2D = false;
 
}
else
{
 
//if (textures[0]->mTextureID != 0 )
//if ( myTexture->mTextureID != NULL)
{
 
transition3Dto2D->render(projectionMatrix, pose, myTexture->mTextureID);     // hete i used it as a texture 
LOG("if startTransition3Dto2D is false-------------------------------->");
// check if transition is finished
if (transition3Dto2D->transitionFinished() )
{
isShowing2DOverlay = true;
 
}
}
}
 
 
finally i got black texture..
 
pls help......

create texturer dynamically

August 13, 2016 - 3:10am #11

Hi! I just solved this problem and I'd like to share my methods with you. The key problem is not about the texture size( it doesn't need to be size of power of 2).

I worked on the ImageTargets sample, and I needed to get dynamic textures from a server through TCP link. After I dynamically load the texture into mTexture in ImageTarget.java like this( I referred to the loadTextureFromApk() function and wrote the loadTextureFromByteArray() function ):

            mTextures.set(0,Texture.loadTextureFromByteArray(buffer,offset,texLen));

I added the following codes into the begining of renderFrame() function in ImageTargetRenderer.java, to generate a new texture and bind it to GLES20.GL_TEXTURE_2D, and it worked! 

            GLES20.glGenTextures(1, mTextures.get(0).mTextureID, 0);

            GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextures.get(0).mTextureID[0]);

            GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,

                    GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);

            GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,

                    GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);

            GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA,

                    mTextures.get(0).mWidth, mTextures.get(0).mHeight, 0, GLES20.GL_RGBA,

                    GLES20.GL_UNSIGNED_BYTE, mTextures.get(0).mData);

Before that, I tried to call initRendering() after the texture is loaded, but it didn't work. I also tried to change my texture into 512*512 size, which didn't work either.

I've been searching for the solution for almost two days and finally I fixed it with myself! Hope this can help you. 

This solution may cost lots of CPU time and I'm looking for a faster solution.

create texturer dynamically

October 31, 2013 - 1:55am #10

And the moment i posted question, i found the answer :)

Just changing this line was enough to make it all work.

Thanks a lot :)
 

          currentTextureObject = env->NewGlobalRef(textureObj);

create texturer dynamically

October 31, 2013 - 1:48am #9

Hi,

 

I am trying to achieve quite same thing. I've already done java part and i get texture from bitmap, but when it comes to display it on target it gives me black screen as well, so i checked myTexture->textureId and it's showed up that it returns always 0. I think problem was the same because i was also sending the texture object from java code and trying to create and bind it out of renderFrame or initRendering methods. Then i found this thread, and try to move creation event in renderFrame, but i am somehow unable to do that. Can you help me to find where do i go wrong? Because i am getting this error now: 

                       JNI ERROR (app bug): accessed stale local reference 0xed70001d (index 7 in a table of size 1)
 
Here is what i do:
 
When i'm done with downloading image as bitmap, i create Texture (by exact same method) and send it to native method:
 
Texture* currentTexture;
jobject currentTextureObject;
bool textureHasChanged = false;
 
JNIEXPORT void JNICALL
Java_com_qualcomm_QCARSamples_ImageTargets_ImageTargets_setTexture(JNIEnv *env, jobject obj, jobject textureObj)
currentTextureObject = textureObj;
textureHasChanged = true; 
}
 
JNIEXPORT void JNICALL
Java_com_qualcomm_QCARSamples_ImageTargets_ImageTargetsRenderer_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();
 
    if(currentTexture == NULL || textureHasChanged) {
           LOG("TextureIssue:::textureCreation");
           currentTexture = Texture::create(env, currentTextureObject);
           LOG("TextureIssue:::textureCreated");
           handleTexture();
           textureHasChanged = false;
           LOG("TextureIssue:::textureHandled");
}
 
....
}
 
void handleTexture()
{
    LOG("TextureIssue:::handleTexture");
    // Now generate the OpenGL texture objects and add settings
    if (currentTexture != NULL)
    {
        LOG("TextureIssue:::textureId: %d", (currentTexture->mTextureID));
        glGenTextures(1, &(currentTexture->mTextureID));
        glBindTexture(GL_TEXTURE_2D, currentTexture->mTextureID);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, currentTexture->mWidth, currentTexture->mHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*) currentTexture->mData);
    } else {
        LOG("TextureIssue:::currentTexture is null.");
    }
}
 

And on logs, i see "TextureIssue:::textureCreation" message but i never get "TextureIssue:::textureCreated" message.

create texturer dynamically

February 6, 2013 - 4:34am #8

Great!

create texturer dynamically

February 6, 2013 - 4:30am #7

thanks dude...

i did all GL works in renderFrame .Now i  m able to create texture dynamically ...

create texturer dynamically

February 6, 2013 - 2:44am #6

OK, this error message is clarifying the issue quite a bit: 

you need to call your texture creation from within the openGL rendering thread; if you execute the glGenTextures (and any other OpenGL code) out of the OpenGL rendering thread, you will not have a valid OpenGL context and as a consequence OpenGL will not work (as the message says, "no current context").

This is a general rule for OpenGL programming.

In practice, for you this means that you need to create your texture either in your initRendering function, or in the renderFrame function (check the original sample code), as these functions are both guaranteed to be called in the OpenGL rendering thread (because they are called from the Java renderer).

So, you will need to adjust your code so to make that happen.

 

create texturer dynamically

February 6, 2013 - 2:10am #5

Hi aless

    i am getting the error :

            : E/libEGL(32702): call to OpenGL ES API with no current context (logged once per thread)

 

 between

 

                LOG("start.................creating texture--");
 
                   myTexture = Texture::create(env, textureObjectNew);
 
                    glGenTextures(1, &(myTexture->mTextureID));
 
               LOG("--------------------------------------------");
 
 
i thinK   myTexture = Texture::create(env, textureObjectNew); is not  creating 

create texturer dynamically

February 6, 2013 - 12:27am #4

Ok, good, so we now know that the texture has not been created properly;

if you look at your code:

if (textureObjectNew != NULL)
    {
 
        myTexture = Texture::create(env, textureObjectNew);

this is where the problem is (probably) occurring; you should print out some LOG from inside the Texture::create() function and see what goes wrong in that function.

If everything goes well inside that function, then it means that the something goes wrong in glGenTextures(1, &(myTexture->mTextureID));

If that's the case, this means that your image has some problems with being transformed into an OpenGL texture (let me know if that's the case..)

create texturer dynamically

February 5, 2013 - 11:27pm #3

Hi  Aless..

          I  checked  all  three cases ..

          the last two cases are fine. but  always i am getting   ..  myTexture->mTextureID==0 (not greater then 0) . 

       As you told (if it is == 0, the texture is not valid, so it has not been created for some reasons) . Now i want to know the reason.

      or any property of texture thats i miss.pls.

 

       

   

create texturer dynamically

February 5, 2013 - 5:56am #2

hi, you should check a few things:

  1. myTexture->mTextureID is greater than zero (if it is == 0, the texture is not valid, so it has not been created for some reasons)
  2. myTexture->mData is not NULL (check this in your createGLTextureNative() function)
  3. myTexture->mWidth and myTexture->mHeight have good values (i.e. they match the size of your input PNG or JPG image, for instance 512 x 256, or whatever it is...)

If you notice anything wrong with the above, then it means something is wrong with your input images or texture creation.

Log in or register to post comments