Skip to main content
gamedev.net
🔒 Locked

Texturing problem...

Started by newera Jul 10, 2006 at 7:29 PM 8 replies 950+ views
Original Post
newera
newera
Hello all, I looked every where(in my code), and I am going crazy over this problem. I read the OpenGL game programming book. With that said, lets get to my dilemma. I have drawing rectangles and lines working. But when I try to load a texture it just appears as an ugly brown colored square of where the texture should be. I am pulling my hair out, because I have references to other code just about identical to mine. Can anyone tell whats going on? In my renderer:

void GLRender::DrawImage( CPointer<Image> ℑ, int x, int y, int width, int height)
{
    /**
     * Get the texture num 
     */
    glBindTexture(GL_TEXTURE_2D, image->textNum );

    /**
     * draw the image out
     */
    glBegin(GL_QUADS);
            glTexCoord2f(0, 0);
            glVertex2f(x, y);

            glTexCoord2f(0, height );
            glVertex2f(x, y+height);

            glTexCoord2f(width, height);
            glVertex2f(x+width, y+height);

            glTexCoord2f(width, 0);
            glVertex2f(x+width, y);
    glEnd();
}
And here is the loader code:

/**
=========================
Load a targa file, and load
it in ogl
=========================
*/
bool TextureLoader::LoadTGA( const std::string &s )
{
    TGAHEADER tga;
    CPointer<Image> image = new Image();
    image->data = 0;

    image->data = LoadTGAFile(s, &tga);
    // Didn't load the file
    if ( !image->data ) {
        std::cout << "\nNo data\n" << std::endl;     
        return false;
    }

    image->width    = tga.imageWidth;
    image->height   = tga.imageHeight;
    image->bitDepth = tga.imageWidth;
    image->id       = ++m_idcounter;
    image->imageType = TGA;
    image->nBPP     = tga.nBPP; 

    int newwidth = NextPowerOfTwo( image->width );
    int newheight = NextPowerOfTwo( image->height );

    // Get the maximal texture size supported, and change the size of thetexture if it is to big
	int maxTextureSize;
	glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
	if(newwidth  > maxTextureSize) newwidth  = maxTextureSize;
	if(newheight > maxTextureSize) newheight = maxTextureSize;
    // TODO - scale image to proper format
    // I am using images that are powers of 2

    

    glGenTextures(1, &image->textNum);		// Create The Texture

    // bind the texture
    glBindTexture(GL_TEXTURE_2D, image->textNum);
    
    // Disable Antialiazing for magnification/minification
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    gluBuild2DMipmaps(  GL_TEXTURE_2D, image->nBPP, 
                        image->width, image->height, 
                        (image->nBPP == 3 ? GL_RGB : GL_RGBA),
                        GL_UNSIGNED_BYTE, image->data);


    // add to the list
    imageList[ image->id ] = image;
    
    if ( image->data )
        delete []image->data;

    return true;
}
Any help would be greatly appreciated, thanks, -Tony
dave
dave
I'm no expert in OpenGL but i can throw this into the works.

Where you generate the vertices, if the height and width cause wrapping, and the height and width are large values, then a largely brown texture tiled could appear to be completely brown.

Hope that helps,

Dave
newera
newera
I don't think that is it, because I get the image width & height and set them respectively. Hmm, this thing has me stumped.

Thanks though
jarro_2783
jarro_2783
I think what the first guy said was right.

glTexCoord2f(0, 0);glVertex2f(x, y);glTexCoord2f(0, height );glVertex2f(x, y+height);glTexCoord2f(width, height);glVertex2f(x+width, y+height);glTexCoord2f(width, 0);glVertex2f(x+width, y);


glTexCoord2f(width, height);
This is setting the uv of that vertice as u = width, v = height.

Let's suppose width = 200, height = 200, x = 0, y = 0.

since uv values are between 0 and 1 and you have repeating on, the texture is repeated 200 times between 0,0 and 0,200. So depending on the level of zoom, you may be seeing the texture as one pixel for the whole texture repeated 200 times. If you got closer to the square you may find that you see the texture properly.

Try setting the uv between 0 and 1 and see what happens.
newera
newera
I'm not quite sure what you mean by uv, I thought glTexCoord2f was the Texture Coordinate?

And yes, I do have textures enabled.

Edit: The weird thing is, I have the same drawing routine working correctly in Java. So I'm guessing it has something to do with the loader?
jarro_2783
jarro_2783
It is the texture coordinate, but we call it uv. u is across, v is up.
It specifies where on the texture that vertex lies in the range 0 to 1. So if you imagine the bottom left of the texture is 0,0 and the bottom right is 1,0, then halfway across the texture is 0.5,0 and halfway up and across is 0.5,0.5.
So if you want a texture to appear once on a quad (to fill the quad), you specify 0,0 for the bottom left corner and 1,1 for the top right corner and 1,0 and 0,1 for the other two corners.
If you want it to appear four times, you would use 0,0 for the bottom left and 2,2 for the top right.
kayX
kayX
Try changing to

glTexCoord2f(0, 0);
glVertex2f(0, 0);

glTexCoord2f(0, height );
glVertex2f(0, 1);

glTexCoord2f(width, height);
glVertex2f(1, 1);

glTexCoord2f(width, 0);
glVertex2f(1, 0);
jarro_2783
jarro_2783
no that will be worse.

make it:


glTexCoord2f(0, 0);
glVertex2f(0, 0);

glTexCoord2f(1,0 );
glVertex2f(width, 0);

glTexCoord2f(1, 1);
glVertex2f(width, height);

glTexCoord2f(0,1);
glVertex2f(0, height);

remember to draw quads anti clockwise because that is the convention for the front face for when you cull the back faces.
newera
newera
Thanks guys, I'll try it when I get home from work, ratings++ for all :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.