Jump to content
  • Advertisement
Sign in to follow this  
KevArnold

OpenGL How many ways to load/dispaly a texture? (EGL 1.1)

This topic is 3385 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am creating an opengl es 1.1 application that has issues with textures becoming distorted randomly. The code below is a simplified version of the larger application that has the issue. The issue does not exist under an emulator so I believe the code is correct but was wondering if there were other ways to load and display textures other than using glTex(Sub)Image and then glDrawArrays/glDrawElements? I am not sure the chip provider is going to provide a solution so I am trying to find alternatives. The actual problem occurs because of a call to glDrawArrays, the first time the texture is displayed it will look fine and then the second time it looks like it has random data on it. The platform I am using does not have support for glDrawTexsOES.
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 texture\n", txtr);
    }
    textpos++;   

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

    return true;
}

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!