Results 1 to 4 of 4

Thread: texture problem?

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2009
    Posts
    2

    texture problem?

    I am a freshman to opengl. I want to display a bmp image file in a window as a texture. I used glut library. but my code cannot work well as I expected. It display a white quads which should be the bmp image. Who can point out my failure, thanks!
    here is code:

    GLuint texture;

    AUX_RGBImageRec *LoadBMP(char *filename)
    {
    FILE *f;
    if(filename==NULL)
    return NULL;
    f=fopen(filename,"r");
    if(!f)
    return NULL;

    fclose(f);
    return auxDIBImageLoadA(filename);
    }


    int LoadGLTextures()
    {
    int Status=FALSE;
    AUX_RGBImageRec *textureImage[1];
    memset(textureImage,0,sizeof(void *)*1);
    if (textureImage[0]=LoadBMP("image130.bmp"))
    {
    Status=TRUE;
    glGenTextures(1,&texture);
    glBindTexture(GL_TEXTURE_2D,texture);
    glTexImage2D(GL_TEXTURE_2D,0,3,textureImage[0]->sizeX,textureImage[0]->sizeY, 0,GL_RGB,GL_UNSIGNED_BYTE,textureImage[0]->data);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
    }
    if (textureImage[0])
    {
    if (textureImage[0]->data)
    {
    free(textureImage[0]->data);
    }
    free(textureImage[0]);
    }
    return Status;
    }


    //int LoadGLTextures()
    //{
    //
    //
    //
    //
    // int status=FALSE;
    // AUX_RGBImageRec *TextureImage=NULL;
    // if(TextureImage=LoadBMP("image130.bmp"))
    // {
    // status=TRUE;
    // glGenTextures(1,&texture);
    // glBindTexture(GL_TEXTURE_2D,texture);
    // glTexImage2D(GL_TEXTURE_2D,0,3,TextureImage->sizeX,TextureImage->sizeY,0,GL_RGB,GL_UNSIGNED_BYTE,TextureImage->data);
    // glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
    // glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
    //
    // if(TextureImage)
    // {
    // if(TextureImage->data)
    // free(TextureImage->data);
    // free(TextureImage);
    // }
    // }
    // return status;
    //}
    //
    //
    void reshape(int w,int h)
    {
    glViewport(0,0,(GLsizei)w,(GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60.0,(GLfloat)w/(GLfloat)h,1.0,100.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    }



    void display(void )
    {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glLoadIdentity();
    glTranslatef(0.0,0.0,-2.0);

    //glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D,texture);
    glBegin(GL_QUADS);
    glTexCoord2d(0,0);glVertex3f(-1,-1,0.0);
    glTexCoord2d(1,0);glVertex3f(-1,1,0.0);
    glTexCoord2d(1,1);glVertex3f(1,1,0.0);
    glTexCoord2d(0,1);glVertex3f(1,-1,0.0);

    glEnd();

    //glDisable(GL_TEXTURE_2D);

    glBegin(GL_LINES);
    glVertex2d(0.0,0);
    glVertex2d(2,2);
    glEnd();


    glFlush();
    glutSwapBuffers();
    }

    void init()
    {
    glEnable(GL_TEXTURE_2D);
    glClearColor(0.0f,0.0f,0.0f,0.0f);
    glClearDepth(1.0f);
    glShadeModel(GL_SMOOTH);
    LoadGLTextures();
    glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
    }


    int main(int argc,char ** argv)
    {
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

    glutInitWindowPosition(100,100);
    glutCreateWindow(argv[0]);

    init();

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);

    glutMainLoop();

    return 0;
    }

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Oct 2004
    Posts
    4,658

    Re: texture problem?

    Quote Originally Posted by txwd0033
    I am a freshman to opengl. I want to display a bmp image file in a window as a texture. I used glut library. but my code cannot work well as I expected. It display a white quads which should be the bmp image. Who can point out my failure, thanks!
    Divide and conquer. If you're having trouble with something, rip it out and plug in something much simpler. See if the problem goes away. Rinse, repeat.

    For instance, first rip out that BMP loader gizmo, and start with this known-working LoadGLTexture function in your code that uses a hard-coded texture:

    Code :
    void LoadGLTextures()
    {
      unsigned char image[4][4] = { { 0  ,0  ,255,255 },
                                    { 255,0  ,0  ,255 },
                                    { 255,0  ,0  ,255 },
                                    { 255,255,255,255 } };
     
      glGenTextures( 1,&texture );
      glBindTexture( GL_TEXTURE_2D, texture );
      glTexImage2D ( GL_TEXTURE_2D,0, GL_RGBA8,
                     2, 2,
                     0,
                     GL_RGBA, GL_UNSIGNED_BYTE,
                     image );
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
    }

    The texture is a 2x2 texture with nearest filtering with colors (left-to-right, bottom-to-top) of: blue, red, red, white.

    Now play around with formats and content of that hard-coded array.

    Then plug in your BMP loader. When it breaks, you'll know you just added the problem.

  3. #3
    Junior Member Newbie
    Join Date
    Mar 2009
    Posts
    2

    Re: texture problem?

    thank your response!
    I find the mistake was caused by my image's width. In Opengl, texture image's width must be power of 2.

    thank you again!

  4. #4
    Senior Member OpenGL Guru
    Join Date
    Oct 2004
    Posts
    4,658

    Re: texture problem?

    Quote Originally Posted by txwd0033
    ...image's width. In Opengl, texture image's width must be power of 2.
    No, not correct. OpenGL drivers can support this, in at least two ways.

    If your OpenGL drivers support the ARB_texture_non_power_of_two extension, you can have ordinary textures be non-power-of-two resolution.

    Alternatively, if your OpenGL drivers support rectangle textures (ARB_texture_rectangle or NV_texture_rectangle extensions), then you can get non-power-of-two resolution that way.

    If neither of these is supported, then your OpenGL drivers don't support non-power-of-two resolutions. But that's a limitation of your OpenGL driver or GPU, not of OpenGL in general.

    Note that OpenGL 2.0+ requires ARB_texture_non_power_of_two support.

Similar Threads

  1. Replies: 1
    Last Post: 09-27-2014, 09:41 PM
  2. Replies: 0
    Last Post: 04-24-2014, 10:56 PM
  3. how to get the horizontal plane extent ?
    By yanasdf789 in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 04-08-2014, 09:14 PM
  4. how to get the horizontal plane extent ?
    By yanasdf789 in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 04-08-2014, 09:13 PM
  5. Replies: 0
    Last Post: 02-23-2014, 07:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean