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. Graphics
  4. My texture just didnot work! Anyone give a hand?

My texture just didnot work! Anyone give a hand?

Scheduled Pinned Locked Moved Graphics
databasegraphicsquestion
5 Posts 3 Posters 2 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

    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.
    
    I T 2 Replies 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.
      
      I Offline
      I Offline
      IdUnknown
      wrote on last edited by
      #2

      For the PIXELFORMATDESCRIPTOR you set the pixel type to PFD_TYPE_RGBA so i think you need to set the colorbits to 32 instead of 24. And for the mipmaps gluBuild2DMipmaps(GL_TEXTURE_2D, 3, imageWidth, imageHeight, GL_RGB, GL_UNSIGNED_BYTE, image); try this instead gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, imageWidth, imageHeight, GL_RGBA, GL_UNSIGNED_BYTE, image); or gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, imageWidth, imageHeight, GL_BGRA, GL_UNSIGNED_BYTE, image); notice the fifth parameter. This is what I use for loading Windows bitmap into texture.

      A 1 Reply Last reply
      0
      • I IdUnknown

        For the PIXELFORMATDESCRIPTOR you set the pixel type to PFD_TYPE_RGBA so i think you need to set the colorbits to 32 instead of 24. And for the mipmaps gluBuild2DMipmaps(GL_TEXTURE_2D, 3, imageWidth, imageHeight, GL_RGB, GL_UNSIGNED_BYTE, image); try this instead gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, imageWidth, imageHeight, GL_RGBA, GL_UNSIGNED_BYTE, image); or gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, imageWidth, imageHeight, GL_BGRA, GL_UNSIGNED_BYTE, image); notice the fifth parameter. This is what I use for loading Windows bitmap into texture.

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

        not work,The program work well in others' computer,But failed again in my computer. My gpu is nViDIA, geforce G210M. I think it something with my gpu. But I knouw nothing about it Thanks all the same

        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.
          
          T Offline
          T Offline
          Tim Craig
          wrote on last edited by
          #4

          What size is the image you're trying to use as a texture? I haven't played with OpenGL for a bit but I seem to remember the image has to be sized to even powers of 2. For example, 2, 4, 8, 16, 32....

          You measure democracy by the freedom it gives its dissidents, not the freedom it gives its assimilated conformists.

          A 1 Reply Last reply
          0
          • T Tim Craig

            What size is the image you're trying to use as a texture? I haven't played with OpenGL for a bit but I seem to remember the image has to be sized to even powers of 2. For example, 2, 4, 8, 16, 32....

            You measure democracy by the freedom it gives its dissidents, not the freedom it gives its assimilated conformists.

            A Offline
            A Offline
            Archy_Yu
            wrote on last edited by
            #5
            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