Question

I have searched for this but unfortunately the only answers I find are those that deal with the issue of textures not being mapped, in this case the texture is mapped but does not look like the loaded surface.

Anyway, ignoring the fact that power of 2 square images aren't required (which I've yet to properly look into yet) I've made a functional script for expanding the surface to power of 2 dimensions and a structure for storing the resized surface, the old dimensions of the surface and the ID for the texture.

My surface to texture code is (arguments are SDL_Surface* surf, antTex* tex):

tex->w=surf->w;
tex->h=surf->h;
tex->surf=resizeToNearestPow2(surf);
glGenTextures(1, &tex->id);
glBindTexture(GL_TEXTURE_2D, tex->id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex->surf->w, tex->surf->h, 0, GL_BGRA, GL_UNSIGNED_BYTE, tex->surf->pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, NULL);

The antTex struct is:

GLuint id;          //Texture ID
int w, h;           //Original Image Size
int rw, rh;         //Resized Canvas Size
SDL_Surface* surf;  //Resized Surface

Everything about resizing the image appears to be working fine (having used SDL's SDL_SaveBMP function to check the resulting image), I used SDL2_image.h's IMG_Load for loading the original image.

When I draw a plane with the texture mapped to it while GL_BLEND is enabled nothing shows up at all, when I disable it I get this (the red is the window's background colour):

http://i1200.photobucket.com/albums/bb336/DarknessXeagle/error_zps03e1e5a1.png

The original image was this:

http://i1200.photobucket.com/albums/bb336/DarknessXeagle/d_zpsc8b9af6f.png

This is my function for drawing plane with the texture mapped to it (arguments are antTex* tex, int x, int y):

glBindTexture(GL_TEXTURE_2D, tex->id);
int w, h;
w=tex->w;
h=tex->h;
GLfloat tw, th;
th=(GLfloat)tex->h/(GLfloat)tex->rh;
tw=(GLfloat)tex->w/(GLfloat)tex->rw;
glPushMatrix();
    glTranslatef(x, y, 0);
    glBegin(GL_QUADS);
        glNormal3f(0, 0, 1);
        glTexCoord2f(0, th);    glVertex2f(0, h);
        glTexCoord2f(tw, th);   glVertex2f(w, h);
        glTexCoord2f(tw, 0);    glVertex2f(w, 0);
        glTexCoord2f(0, 0);     glVertex2f(0, 0);
    glEnd();
glPopMatrix();
glBindTexture(GL_TEXTURE_2D, NULL);
Was it helpful?

Solution

You have a problem with u and v coordinates passed to render the texture into the quad using glTexCoord2f.

You need to change:

glTexCoord2f(0, th);    glVertex2f(0, h);
glTexCoord2f(tw, th);   glVertex2f(w, h);
glTexCoord2f(tw, 0);    glVertex2f(w, 0);
glTexCoord2f(0, 0);     glVertex2f(0, 0);

into:

glTexCoord2f(0, 1);    glVertex2f(0, h);
glTexCoord2f(1, 1);   glVertex2f(w, h);
glTexCoord2f(1, 0);    glVertex2f(w, 0);
glTexCoord2f(0, 0);     glVertex2f(0, 0);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top