Imagination PowerVR SDK Blog

Simple program with texture distortion


#1

I built a very simple application that shows textures being distorted on an IMX board but works fine under the emulator. The application simply calls the begin/end functions listed below in response to a WM_LBUTTONDOWN message.

What the application does is loop through a list of textures showing the last 5 in the list. If its at the end of the list it creates a new texture. The reason for this is to display previously created and displayed textures that are now actually distorted for unknown reasons.

The code below is how I load images in my true application. Maybe there is a better way to create textures? I would gladly change it if it would stop the distortion from occuring. Or maybe I am doing something wrong?

-
typedef struct {
    unsigned char Red;
    unsigned char Green;
    unsigned char Blue;
    unsigned char Alpha;
}RGBAQuad, pRGBAQuad;

bool Begin()
{
    GLfloat width = nWidth;
    GLfloat height = nHeight;

    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    glOrthof (0, width, height, 0, 0, 1);
    glDisable(GL_DEPTH_TEST);
    glMatrixMode (GL_MODELVIEW);
    glLoadIdentity();

    glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

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

    //
    if (textures.size() == 0 || textpos == textures.end())
    {
        thisFrameTextureID = 0;
        glGenTextures(1, &thisFrameTextureID);
        glBindTexture(GL_TEXTURE_2D, thisFrameTextureID);   
       
        RGBAQuad
rgbadata = new RGBAQuad[TEST_TXTR_SIZE * TEST_TXTR_SIZE];
        unsigned char val = rand() % 255;

        unsigned long idx = 0;
        for (int w = 0; w < TEST_TXTR_SIZE; w++)
        {           
            for (int h = 0; h < TEST_TXTR_SIZE; h++)
            {
                idx = h + (w * TEST_TXTR_SIZE);

                rgbadata[idx].Red = val;
                rgbadata[idx].Green = val;
                rgbadata[idx].Blue = val;
                rgbadata[idx].Alpha = val;
            }
        }

        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TEST_TXTR_SIZE, TEST_TXTR_SIZE, 0, GL_RGBA, GL_UNSIGNED_BYTE, (unsigned char*)rgbadata);
       
        glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);   
        glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);        //was i

        glDisable(GL_TEXTURE_2D);

        textures.push_back(thisFrameTextureID);
        delete rgbadata;
       
        textpos = textures.begin();
        if (textures.size() > 5)
        {
            int moveback = 5;
            textpos = textures.end();
            while (textpos != textures.begin() && moveback )               
                textpos–, moveback–;
        }
    }

    return 1;
}

bool End()
{
    if (*textpos)
    {
        GLuint txtr = *textpos;

        GLfloat pfVertices[] = {(float)0, (float)0, //0.0f,
                                (float)0, (float)nHeight, //0.0f,
                                (float)nWidth, (float)0, //0.0f,
                                (float)nWidth, (float)nHeight//, //0.0f
        };

        GLfloat pfTexCoords[] = { 0.0f, 0.0f,
                                0.0f, 1.0f,
                                1.0f, 0.0f,
                                1.0f, 1.0f };

        //make sure Texture is enabled
        if(!(glIsEnabled(GL_TEXTURE_2D)))
            glEnable(GL_TEXTURE_2D);

        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);

        glBindTexture(GL_TEXTURE_2D, txtr);

        glVertexPointer(2, GL_FLOAT, 0, pfVertices);
        glTexCoordPointer(2, GL_FLOAT, 0, pfTexCoords);

        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

        glDisableClientState(GL_VERTEX_ARRAY);
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);   

        printf(“displayed %d texturen”, txtr);
    }
    textpos++;   

    glFlush();
    if (EGL_TRUE != eglSwapBuffers( eglDisplay, eglSurface ))
        return false;

    return true;
}
-