Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. My texture Problem

My texture Problem

Scheduled Pinned Locked Moved C / C++ / MFC
help
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Archy_Yu
    wrote on last edited by
    #1

    I read data from a bmp file,and use it as a texture,but it just failed,which work well on others' computer,My code is no problem.Any help is appreciated .Thanks!

    C 1 Reply Last reply
    0
    • A Archy_Yu

      I read data from a bmp file,and use it as a texture,but it just failed,which work well on others' computer,My code is no problem.Any help is appreciated .Thanks!

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      Could you please give more explanation ? We don't even know how you are loading your texture, how you display it, ... Please post some (relevant) code too (and use the code block tag).

      Cédric Moonen Software developer
      Charting control [v2.0] OpenGL game tutorial in C++

      A 1 Reply Last reply
      0
      • C Cedric Moonen

        Could you please give more explanation ? We don't even know how you are loading your texture, how you display it, ... Please post some (relevant) code too (and use the code block tag).

        Cédric Moonen Software developer
        Charting control [v2.0] OpenGL game tutorial in C++

        A Offline
        A Offline
        Archy_Yu
        wrote on last edited by
        #3

        unsigned int ID; /** < 生成纹理的ID号 */
        int imageWidth; /** < 图像宽度 */
        int imageHeight; /** < 图像高度 */
        unsigned char *image; /** < 指向图像数据的指针 */
        FILE *pFile = 0;

        BITMAPINFOHEADER bitmapInfoHeader;
        BITMAPFILEHEADER header;

        unsigned char textureColors = 0;

        pFile = fopen("桌面.bmp", "rb");
        if(pFile == 0) return false;

        fread(&header, sizeof(BITMAPFILEHEADER), 1, pFile);

        if(header.bfType != BITMAP_ID)
        {
        fclose(pFile);
        return false;
        }

        fread(&bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, pFile);

        imageWidth = bitmapInfoHeader.biWidth;
        imageHeight = bitmapInfoHeader.biHeight;

        if(bitmapInfoHeader.biSizeImage == 0)
        bitmapInfoHeader.biSizeImage = bitmapInfoHeader.biWidth *
        bitmapInfoHeader.biHeight * 3;

        fseek(pFile, header.bfOffBits, SEEK_SET);

        image = new unsigned char[bitmapInfoHeader.biSizeImage];

        fread(image, 1, bitmapInfoHeader.biSizeImage, pFile);

        for(int index = 0; index < (int)bitmapInfoHeader.biSizeImage; index+=3)
        {
        textureColors = image[index];
        image[index] = image[index + 2];
        image[index + 2] = textureColors;
        }

        fclose(pFile);
        glPixelStorei(GL_UNPACK_ALIGNMENT,1);

        glGenTextures(1, &ID);

        glBindTexture(GL\_TEXTURE\_2D, ID); 
        

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);

        gluBuild2DMipmaps(GL\_TEXTURE\_2D, 3, imageWidth, 
                      imageHeight, GL\_RGB, GL\_UNSIGNED\_BYTE, 
                      image); 
        

        glEnable(GL_TEXTURE_2D);
        glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);

        glPushMatrix();
        glScaled(45.0,30.0,0);
        glBindTexture(GL_TEXTURE_2D, ID);
        glBegin(GL_QUADS);
        glTexCoord2f(0.0f, 0.0f); glVertex3f(-2.0f, -1.0f, 0.0f);
        glTexCoord2f(1.0f, 0.0f); glVertex3f(-2.0f, 1.0f, 0.0f);
        glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.0f, 1.0f, 0.0f);
        glTexCoord2f(0.0f, 1.0f); glVertex3f( 0.0f, -1.0f, 0.0f);

        glEnd();
        glPopMatrix();

        GLvoid ReShapeGLScene( GLsizei width,GLsizei height )
        {
        glViewport(0,0,(GLsizei)width,(GLsizei)height);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho((GLdouble)-450.0,(GLdouble)450.0,(GLdouble)-300.0,(GLdouble)300.0,50.0,-10.0);
        glMatrixMode(G

        C 1 Reply Last reply
        0
        • A Archy_Yu

          unsigned int ID; /** < 生成纹理的ID号 */
          int imageWidth; /** < 图像宽度 */
          int imageHeight; /** < 图像高度 */
          unsigned char *image; /** < 指向图像数据的指针 */
          FILE *pFile = 0;

          BITMAPINFOHEADER bitmapInfoHeader;
          BITMAPFILEHEADER header;

          unsigned char textureColors = 0;

          pFile = fopen("桌面.bmp", "rb");
          if(pFile == 0) return false;

          fread(&header, sizeof(BITMAPFILEHEADER), 1, pFile);

          if(header.bfType != BITMAP_ID)
          {
          fclose(pFile);
          return false;
          }

          fread(&bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, pFile);

          imageWidth = bitmapInfoHeader.biWidth;
          imageHeight = bitmapInfoHeader.biHeight;

          if(bitmapInfoHeader.biSizeImage == 0)
          bitmapInfoHeader.biSizeImage = bitmapInfoHeader.biWidth *
          bitmapInfoHeader.biHeight * 3;

          fseek(pFile, header.bfOffBits, SEEK_SET);

          image = new unsigned char[bitmapInfoHeader.biSizeImage];

          fread(image, 1, bitmapInfoHeader.biSizeImage, pFile);

          for(int index = 0; index < (int)bitmapInfoHeader.biSizeImage; index+=3)
          {
          textureColors = image[index];
          image[index] = image[index + 2];
          image[index + 2] = textureColors;
          }

          fclose(pFile);
          glPixelStorei(GL_UNPACK_ALIGNMENT,1);

          glGenTextures(1, &ID);

          glBindTexture(GL\_TEXTURE\_2D, ID); 
          

          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
          glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
          glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);

          gluBuild2DMipmaps(GL\_TEXTURE\_2D, 3, imageWidth, 
                        imageHeight, GL\_RGB, GL\_UNSIGNED\_BYTE, 
                        image); 
          

          glEnable(GL_TEXTURE_2D);
          glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);

          glPushMatrix();
          glScaled(45.0,30.0,0);
          glBindTexture(GL_TEXTURE_2D, ID);
          glBegin(GL_QUADS);
          glTexCoord2f(0.0f, 0.0f); glVertex3f(-2.0f, -1.0f, 0.0f);
          glTexCoord2f(1.0f, 0.0f); glVertex3f(-2.0f, 1.0f, 0.0f);
          glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.0f, 1.0f, 0.0f);
          glTexCoord2f(0.0f, 1.0f); glVertex3f( 0.0f, -1.0f, 0.0f);

          glEnd();
          glPopMatrix();

          GLvoid ReShapeGLScene( GLsizei width,GLsizei height )
          {
          glViewport(0,0,(GLsizei)width,(GLsizei)height);
          glMatrixMode(GL_PROJECTION);
          glLoadIdentity();
          glOrtho((GLdouble)-450.0,(GLdouble)450.0,(GLdouble)-300.0,(GLdouble)300.0,50.0,-10.0);
          glMatrixMode(G

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          What is your problem in fact ? Because you just said that it failed, that rather vague... Anyway I see you are using OpenGL, make sure that the width and height of your images are a power of 2.

          Cédric Moonen Software developer
          Charting control [v2.0] OpenGL game tutorial in C++

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups