Results 1 to 6 of 6

Thread: Texture not working properly

  1. #1
    Member Contributor
    Join Date
    May 2012
    Posts
    51

    Texture not working properly

    I've managed to load my bitmap image successfully but it won't cannot be displayed correctly on the texture.

    the link below is an example of my error
    http://image83.webshots.com/183/2/67...2ZIybHb_fs.jpg

    whereas this is the image, I could load up into open gl

    http://image87.webshots.com/87/5/3/7...2LlWmci_ph.jpg

    mycode is below.

    Particularly , if you could look at "myinit" and "display" function, thats where the source of my error could be.

    Dont worry about the long function at the bottom, its for reading bitmap images

    Code :
    #include <windows.h>
    #include <math.h> 
    #include<glut.h>
    #include<iostream>
    using namespace std;
     
     
    GLubyte *                         
    ReadBitmap(const char *filename, 
                 BITMAPINFO **info);    
     
     
     
    //Initialising for image load 
    GLubyte    *pixels; // "Image tile" pixel data
    BITMAPINFO *info; // Spaceship Bitmap information 
     
     
     
    void myinit(void)
    {
        glClearColor (0.0, 0.0, 0.0, 0.0);
        glEnable(GL_DEPTH_TEST);
        glDepthFunc(GL_LEQUAL);
     
    	pixels = ReadBitmap("tile1.bmp", &amp;info);
    	  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    	  glTexImage2D(GL_TEXTURE_2D, 0, 3,info->bmiHeader.biWidth /*checkImageWidth*/, 
    		  info->bmiHeader.biHeight /*checkImageHeight*/, 0, GL_RGB, GL_UNSIGNED_BYTE, 
            pixels/*&amp;checkImage[0][0][0]*/);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
            GL_NEAREST);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, 
            GL_NEAREST);
        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
        glEnable(GL_TEXTURE_2D);
        glShadeModel(GL_FLAT);
    }
     
     
    void display(void)
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glBegin(GL_QUADS);
        glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0);
        glTexCoord2f(0.0, 0.5); glVertex3f(-2.0, 1.0, 0.0);
        glTexCoord2f(0.5, 0.5); glVertex3f(0.0, 1.0, 0.0);
        glTexCoord2f(0.5, 0.0); glVertex3f(0.0, -1.0, 0.0);
     
        glTexCoord2f(0.0, 0.0); glVertex3f(1.0, -1.0, 0.0);
        glTexCoord2f(0.0, 1.0); glVertex3f(1.0, 1.0, 0.0);
        glTexCoord2f(1.0, 1.0); glVertex3f(2.41421, 1.0, -1.41421);
        glTexCoord2f(1.0, 0.0); glVertex3f(2.41421, -1.0,-1.41421);
        glEnd();
        glFlush();
    	glutSwapBuffers();
    }
     
     
     
     
     
     
     
    //void myReshape(int w, int h)
    //{
    //	// on reshape and on startup, keep the viewport to be the entire size of the window
    //	glViewport(0,0,(GLsizei)w, (GLsizei) h);
    //	glMatrixMode(GL_PROJECTION);//centre of screen = origin 
    //	glLoadIdentity();
    //
    //	//keep our logical coordinate system constant
    //	gluOrtho2D(0.0,600.0,0.0,337.0);
    //	glMatrixMode(GL_PROJECTION);  /*glMatrixMode(GL_MODELVIEW);*/ //conner of screen = origin 
    //	glLoadIdentity();
    //}
     
    void myReshape(GLsizei w, GLsizei h)
    {
        glViewport(0, 0, w, h);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(60.0, 1.0*(GLfloat)w/(GLfloat)h, 1.0, 30.0);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glTranslatef(0.0, 0.0, -3.6);
    }
     
     
     
    int main()
    {
        glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
        glutInitWindowPosition (500, 500);
        glutCreateWindow("texturing");
        myinit();
        glutReshapeFunc(myReshape);
    glutDisplayFunc(display);
        glutMainLoop();
    }
     
     
     
     
     
     
     
     
     
     
    /* read bitmap information. Returns the final bitmap pixel values */
    GLubyte *                         
    ReadBitmap(const char *filename, 
                 BITMAPINFO **info)    
    {
      FILE             *fp;          /* Open file pointer */
      GLubyte          *pixels;        /* Bitmap pixel bits */
      int              imgsize;      /* Size of bitmap image*/
      int              infosize;     /* Size of header information */
      BITMAPFILEHEADER header;       /* File header */
     
     
      // Try opening the file; use "rb" mode to read a binary file. 
      if ((fp = fopen(filename, "rb")) == NULL)
      {cout<<"Rb mode not working"<<endl; return (NULL);}
     
      // Read the file header
      if (fread(&amp;header, sizeof(BITMAPFILEHEADER), 1, fp) < 1)
      {
      // Couldn't read the file header 
    	  fclose(fp);
        cout<<"couldn't read the file header"<<endl; return (NULL);
      }
     
      if (header.bfType != 'MB')	/* Check for BM reversed... */
      {
      // Not a bitmap file
        fclose(fp);
        cout<<"not a bitmap file"<<endl;return (NULL);
      }
     
      infosize = header.bfOffBits - sizeof(BITMAPFILEHEADER);
      if ((*info = (BITMAPINFO *)malloc(infosize)) == NULL)
      {
        fclose(fp);
        return (NULL);
      }
     
      if (fread(*info, 1, infosize, fp) < infosize)
      {
        free(*info);
        fclose(fp);
        return (NULL);
      }
     
      /* Now that we have all the header info read in, allocate memory for *
       * the bitmap and read it in...  
       */
      imgsize = (*info)->bmiHeader.biSizeImage;
      // sometimes imagesize is not set in files
      if (imgsize == 0)
         imgsize = ((*info)->bmiHeader.biWidth *
                    (*info)->bmiHeader.biBitCount + 7) / 8 *
      	             abs((*info)->bmiHeader.biHeight);
     
      if ((pixels = (unsigned char *)malloc(imgsize)) == NULL)
      {
        free(*info);
        fclose(fp);
        return (NULL);
      }
     
      if (fread(pixels, 1, imgsize, fp) < imgsize)
      {
        free(*info);
        free(pixels);
        fclose(fp);
        return (NULL);
       }
     
       fclose(fp);
       return (pixels);
    }


  2. #2
    Senior Member Regular Contributor
    Join Date
    Nov 2010
    Location
    Brazil, Rio de Janeiro
    Posts
    147

    Re: Texture not working properly

    I'm not really sure about it, but I think you gonna need set the texture parameters first and than load the texture. At least, all the examples I saw do so.

    Code :
    glClearColor (0.0, 0.0, 0.0, 0.0);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
     
    pixels = ReadBitmap("tile1.bmp", &amp;info);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
     
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,  GL_NEAREST);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
     
    glTexImage2D(GL_TEXTURE_2D, 0, 3,info->bmiHeader.biWidth /*checkImageWidth*/, info->bmiHeader.biHeight /*checkImageHeight*/, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels/*&amp;checkImage[0][0][0]*/);
     
    glEnable(GL_TEXTURE_2D);
    glShadeModel(GL_FLAT);

    Another thing, where is you call to glGenTexture() and glBindTexture() ?

  3. #3
    Member Contributor
    Join Date
    May 2012
    Posts
    51

    Re: Texture not working properly

    Oh, sorry there, I didn't notice how they images were not visible. Anyway top add further description, my texture looks pixelated and the colours are wrong

  4. #4
    Senior Member Regular Contributor remdul's Avatar
    Join Date
    Mar 2004
    Location
    The Netherlands
    Posts
    346

    Re: Texture not working properly

    To fix the 'pixelated' appearance, enable linear texture filtering. Instead of GL_NEAREST, use GL_LINEAR.

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    If you will be rendering 3d models with these textures, also consider mipmapping. See OpenGL Wiki: http://www.opengl.org/wiki/Texture .

    As for the other prolbem, it seems you are loading textures from BMP images. IIRC, BMPs have the pixel colors stored as BGR, rather than RGB. So, in the call to glTexImage2D, replace GL_RGB with GL_BGR.

    I should also note that BMP images have padded pixels for each row if the image width is not dividable by 4, which can be source problems. Also keep in mind that BMP image data is stored 'upside down', however, OpenGL does the same internally so generally this should not be a problem.

    For beginners I would recommend the TGA image format over BMP as it has a much simpler structure and is easier to read and write. FYI: http://gpwiki.org/index.php/TGA .

  5. #5
    Member Contributor
    Join Date
    May 2012
    Posts
    51

    Re: Texture not working properly

    does TGA support transparency

  6. #6
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,574

    Re: Texture not working properly

    Yes, TGA have support for RGBA.

Similar Threads

  1. Picking is not working properly!
    By mehmedean in forum OpenGL: Advanced Coding
    Replies: 3
    Last Post: 01-11-2010, 04:24 AM
  2. Picking is not working properly!
    By mehmedean in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 01-11-2010, 04:22 AM
  3. Blend not working properly
    By in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 11-20-2005, 05:17 PM
  4. Gl not working properly?
    By Feesh in forum OpenGL: User Software
    Replies: 3
    Last Post: 01-05-2005, 04:54 PM
  5. gluProject not working properly
    By Riffminster in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 03-14-2002, 06:34 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