1

I've created a cube and then changed its coordinates to make it a beam like this (click me).

I am sure that the texture is being loaded fine as I have successfully loaded other textures in the project (using SOIL).

The code of the beam is this:

glBindTexture(GL_TEXTURE_2D, obstacle_texture);
glBegin(GL_QUAD_STRIP);
    // front
    glTexCoord2f(0.0f, 0.0f);   glVertex3f(0.0f, 0.0f, 0.0f);
    glTexCoord2f(1.0f, 0.0f);   glVertex3f(side, 0.0f, 0.0f);
    glTexCoord2f(1.0f, 1.0f);   glVertex3f(side, height, 0.0f);
    glTexCoord2f(0.0f, 1.0f );  glVertex3f(0.0f, height, 0.0f);
    // back
    glTexCoord2f(0.0f, 0.0f);   glVertex3f(0.0f, 0.0f, -side);
    glTexCoord2f(0.0f, 1.0f);   glVertex3f(side, 0.0f, -side);
    glTexCoord2f(1.0f, 0.0f);   glVertex3f(side, height, -side);
    glTexCoord2f(1.0f, 1.0f);   glVertex3f(0.0f, height, -side);
    // right
    glTexCoord2f(0.0f, 0.0f);   glVertex3f(side, 0.0f, 0.0f);
    glTexCoord2f(0.0f, 1.0f);   glVertex3f(side, 0.0f, -side);
    glTexCoord2f(1.0f, 0.0f);   glVertex3f(side, height, -side);
    glTexCoord2f(1.0f, 1.0f);   glVertex3f(side, height, 0.0f);
    // left
    glTexCoord2f(0.0f, 0.0f);   glVertex3f(0.0f, 0.0f, 0.0f);
    glTexCoord2f(0.0f, 1.0f);   glVertex3f(0.0f, 0.0f, -side);
    glTexCoord2f(1.0f, 0.0f);   glVertex3f(0.0f, height, -side);
    glTexCoord2f(1.0f, 1.0f);   glVertex3f(0.0f, height, 0.0f);
    // top
    glTexCoord2f(0.0f, 0.0f);   glVertex3f(0.0f, height, 0.0f);
    glTexCoord2f(1.0f, 0.0f);   glVertex3f(side, height, 0.0f);
    glTexCoord2f(1.0f, 1.0f);   glVertex3f(side, height, -side);
    glTexCoord2f(0.0f, 1.0f);   glVertex3f(0.0f, height, -side);
    // bottom
    glTexCoord2f(0.0f, 0.0f);   glVertex3f(0.0f, 0.0f, 0.0f);
    glTexCoord2f(0.0f, 1.0f);   glVertex3f(side, 0.0f, 0.0f);
    glTexCoord2f(1.0f, 1.0f);   glVertex3f(side, 0.0f, -side);
    glTexCoord2f(0.0f, 1.0f);   glVertex3f(0.0f, 0.0f, -side);
glEnd(); 

The result is this:

beam_wrongTextureMapping

There should be something wrong with texture mapping OR
I should have left the cube as it is and applied the texture to it, and then scale it (but I think this would make the texture look distorted).

What should I do?

Update #1: Before SOIL I had been using a loader for BMP files I've found at a site. I've used it now, and it looks like this:

enter image description here

Notice that it seems as if the cube has some triangular wholes where I've pointed it out. I am using SOIL like this:

GLuint tex_2d = SOIL_load_OGL_texture
        (
        imagepath,
        SOIL_LOAD_AUTO,
        SOIL_CREATE_NEW_ID,
        SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
        );
    glBindTexture(GL_TEXTURE_2D, tex_2d);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
Chris
  • 3,619
  • 8
  • 44
  • 64

1 Answers1

4

So, at least three problems here.

You're rendering a quad-strip, but giving coordinates as if you are rendering separate quads. So you'll get extra primitives being rendered. This is not the source of your problems, but you are spitting out bad geometry in there somewhere.

Some of the UV coordinates seem off - on some faces the coords proceed around the face, whereas in others the coords zig-zag.

Here are your first two faces:

glTexCoord2f(0.0f, 0.0f);   glVertex3f(0.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f);   glVertex3f(side, 0.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);   glVertex3f(side, height, 0.0f);
glTexCoord2f(0.0f, 1.0f );  glVertex3f(0.0f, height, 0.0f);

glTexCoord2f(0.0f, 0.0f);   glVertex3f(0.0f, 0.0f, -side);
glTexCoord2f(0.0f, 1.0f);   glVertex3f(side, 0.0f, -side);
glTexCoord2f(1.0f, 0.0f);   glVertex3f(side, height, -side);
glTexCoord2f(1.0f, 1.0f);   glVertex3f(0.0f, height, -side);

In the second face, the middle two UV coordinates are diagonally opposed, but not so on the first. Only one of these can be correct, so on the face which is not correct, you will get bad texturing.

Your texture loading issue is likely to be a padding problem, with the texture being loaded having a different padding requirement to what GL is expecting (GL, by default, expects lines of texture data to be padded to 4 bytes). Either tell GL how your texture is padded, or change the texture such that it matches (this is why your BMP texture works better - the padding requirements for BMP coincidentally work with GL).

Possible fixes for this are:

  • Tell GL how your texture is packed, using (for example) glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  • Change the dimensions of your texture, such that the padding matches without changing anything
  • Change the loading of your texture, such that the padding is consistent with what GL expects

I suspect there might be something further wrong here, as you have a strange yellow stripe appearing on both the BMP and PNG textures, but I'd fix the obvious problems first.

Finally, standard advice - avoid the old style fixed-function pipeline with individual vertex submission, and use VBOs and draw calls.

JasonD
  • 16,464
  • 2
  • 29
  • 44
  • About the VBOs: This is for an assignment and we have been taught the glVertex method to draw. Since, this is newer and provides better performance I am going to use it next time I write something in OpenGL. 1. Changed the quad_strip to quad and it looks without those wholes (I had changed it to strip in order to see if the cube is drawn well and then I forgot to restore it) 2. I've fixed the coordinates, and the 1st one you've posted is correct the correct one. Continued.. – Chris Feb 19 '13 at 11:40
  • 3. I've also changed the dimensions of the texture to be square since I've noticed that my other textures were of square dimensions as well. Thank you for your reply. – Chris Feb 19 '13 at 11:40