Search code examples
c++opengltexture-mappingtexture2d

OpenGL texture not working


I'm trying to learn how to use textures in OpenGL. I started reading the chapter on texture mapping in OpenGL Redbook. I didn't understand it so I googled and found this tutorial. I followed instructions and I still am not able to get it to work. This is the code.

This is my texture image . I used Photoshop to convert it in to this. The size of the file is 175KB which is about the right size (200x300x3=180000).

I tried to read the file in another program and see if the contents are right. It seems I can only read about 221 pixels (it's approx. the first line of image) and it reaches eof(). In my program I read as much as I expect in there to be, but when I debugged I noticed after some points bytes are zeroed.

Now I'm confused. Is there a problem with my program? Is there a problem with texture? Is there a problem with me? What am I doing wrong? How do I fix it?


Solution

  • The problem is your code. You attempt to read a binary file with text mode. Instead use the following code:

    ifstream tex("ace.raw", ios::in | ios::binary);
    if(tex.is_open())
      for(int j=0; j<imH; ++j)
        for(int i=0; i<imW; ++i)
          for(int k=0; k<3; ++k)
          {
            face[j][i][k] = tex.get();
          }
    
    tex.close();
    

    Or much shorter and equivalent code:

    ifstream tex("ace.raw", ios::in | ios::binary);
    if (tex.is_open())
      tex.read((char*)face, sizeof(face));
    
    tex.close();
    

    Both codes are tested and "face" variable consists exactly same contents as "ace.raw".