0

My question is probably pretty simple to answer

Im importing a 128x128 texture atlas RAW file. I want to make one of the faces of my cube an image stored in that texture atlas. I drew the image with the dimensions 10x5 starting at pixel ( 10 , 5 )

So my image is represented at Top Left Corner ( 10 , 5) and bottom right corner (20, 10)

Image Corners Look Like this

(10 , 5) ----- (20 , 5)

(20 , 5) ----- (20 , 10)

If i read correctly

glTexCoord2f() scales the whole image from 0 - 1 when implemented. If i want to get a sub image in that texture atlas, would I just choose the pixels I want to draw and divide it by 128?

GLuint textAtlas = LoadTextureRAW("TextAtlas128", 1, 128,128 );

glPushAttrib(GL_ENABLE_BIT);
    glEnable( GL_TEXTURE_2D );
    glBindTexture( GL_TEXTURE_2D, textAtlas );
    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
    glPushMatrix();
        glBegin(GL_QUADS);
.
.
.

glTexCoord2f(10.0f/128, 05.0f/128); glVertex3d(...

glTexCoord2f(10.0f/128, 10.0f/128); glVertex3d(...

glTexCoord2f(20.0f/128, 10.0f/128); glVertex3d(...

glTexCoord2f(20.0f/128, 05.0f/128); glVertex3d(...

Any help would be amazing.

void drawBlocks(){
    float percentage = .3;
    int i , j ,k;
    int sX = -80; int sY = 90; int sZ = -620;
    GLuint textAtlas = LoadTextureRAW("TextAtlas128", 1, 128,128 );

    glPushAttrib(GL_ENABLE_BIT);
        glEnable( GL_TEXTURE_2D );
        glBindTexture( GL_TEXTURE_2D, textAtlas );
        glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
        glPushMatrix();
            glBegin(GL_QUADS);
                for(i = 0; i < 4; i++){
                    for(j = 0; j < 8; j++){
                        for(k = 0; k < 9; k++){
                            if(block[i][j][k].exists == 1){
                                //Front Face
                                glNormal3f(0,0,1);
                                glTexCoord2f(10.0/128, 05.0/128); glVertex3d( sX + (i * 40)      , sY - (j * 20)     , sZ - (k * 20));
                                glTexCoord2f(10.0/128, 10.0/128); glVertex3d( sX + (i * 40)      , sY - (j * 20) -20 , sZ - (k * 20));
                                glTexCoord2f(20.0/128, 10.0/128); glVertex3d( sX + (i * 40) + 40 , sY - (j * 20) -20 , sZ - (k * 20));
                                glTexCoord2f(20.0/128, 05.0/128); glVertex3d( sX + (i * 40) + 40 , sY - (j * 20)     , sZ - (k * 20));


                                //Back Face
                                glVertex3d( sX + (i * 40)      , sY - (j * 20)     , sZ - (k * 20) - 20);
                                glVertex3d( sX + (i * 40)      , sY - (j * 20) -20 , sZ - (k * 20) - 20);
                                glVertex3d( sX + (i * 40) + 40 , sY - (j * 20) -20 , sZ - (k * 20) - 20);
                                glVertex3d( sX + (i * 40) + 40 , sY - (j * 20)     , sZ - (k * 20) - 20);

                                //Top Face
                                glVertex3d( sX + (i * 40)      , sY - (j * 20)  , sZ - (k * 20) - 20);
                                glVertex3d( sX + (i * 40)      , sY - (j * 20)  , sZ - (k * 20)     );
                                glVertex3d( sX + (i * 40) + 40 , sY - (j * 20)  , sZ - (k * 20)     );
                                glVertex3d( sX + (i * 40) + 40 , sY - (j * 20)  , sZ - (k * 20) - 20);

                                //Bottom Face
                                glVertex3d( sX + (i * 40)      , sY - (j * 20) - 20 , sZ - (k * 20) - 20);
                                glVertex3d( sX + (i * 40)      , sY - (j * 20) - 20 , sZ - (k * 20)     );
                                glVertex3d( sX + (i * 40) + 40 , sY - (j * 20) - 20 , sZ - (k * 20)     );
                                glVertex3d( sX + (i * 40) + 40 , sY - (j * 20) - 20 , sZ - (k * 20) - 20);

                                //Right Face
                                glVertex3d( sX + (i * 40) + 40 , sY - (j * 20)      , sZ - (k * 20)     );
                                glVertex3d( sX + (i * 40) + 40 , sY - (j * 20) - 20 , sZ - (k * 20)     );
                                glVertex3d( sX + (i * 40) + 40 , sY - (j * 20) - 20 , sZ - (k * 20) - 20);
                                glVertex3d( sX + (i * 40) + 40 , sY - (j * 20)      , sZ - (k * 20) - 20);

                                //Left Face
                                glVertex3d( sX + (i * 40) , sY - (j * 20)      , sZ - (k * 20)     );
                                glVertex3d( sX + (i * 40) , sY - (j * 20) - 20 , sZ - (k * 20)     );
                                glVertex3d( sX + (i * 40) , sY - (j * 20) - 20 , sZ - (k * 20) - 20);
                                glVertex3d( sX + (i * 40) , sY - (j * 20)      , sZ - (k * 20) - 20);
                            }
                        }
                    }
                }
            glEnd();
        glPopMatrix();
    glPopAttrib();
}

void drawPlanes(){
    /*GLuint texture  =  LoadTextureRAW( "Wall Texture", 1 );*/
    GLuint texture  =  LoadTextureRAW( "walls", 1, 32,32 );

    glPushAttrib(GL_ENABLE_BIT);
        glEnable( GL_TEXTURE_2D );
        glBindTexture( GL_TEXTURE_2D, texture );
        glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);


        glBegin(GL_QUADS);
            //Floor
            glNormal3f( 0.0f , 1.0f, 0.0f);

            glTexCoord2f(0.0f, 0.0f);   glVertex3f( -100.0f , -100.0f , -725.0f);
            glTexCoord2f(0.0f, 10.0f);  glVertex3f(100.0f , -100.0f , -725.0f);
            glTexCoord2f(10.0f, 10.0f); glVertex3f(100.0f , -100.0f ,  000.0f);
            glTexCoord2f(10.0f, 0.0f);  glVertex3f( -100.0f , -100.0f ,  000.0f);

            //Ceiling
            glNormal3f(0.0f,-1.0f,0.0f);

            glTexCoord2f(0.0f, 0.0f);   glVertex3f( 100.0f, 100.0f, -725.0f);
            glTexCoord2f(0.0f, 10.0f);  glVertex3f(-100.0f, 100.0f, -725.0f);
            glTexCoord2f(10.0f, 10.0f); glVertex3f(-100.0f, 100.0f,  000.0f);
            glTexCoord2f(10.0f, 0.0f);  glVertex3f( 100.0f, 100.0f,  000.0f);

            //Right Wall
            glNormal3f( -1.0f , 0.0f, 0.0f);

            glTexCoord2f(10.0f, 10.0f); glVertex3f(100.0f , 100.0f ,  000.0f);
            glTexCoord2f(0.0f, 10.0f);  glVertex3f(100.0f , 100.0f , -725.0f);
            glTexCoord2f(0.0f, 00.0f);  glVertex3f(100.0f ,-100.0f , -725.0f);
            glTexCoord2f(10.0f, 0.0f);  glVertex3f(100.0f ,-100.0f,   000.0f);

            //LeftWall
            glNormal3f( 1.0f , 0.0f, 0.0f);

            glTexCoord2f(10.0f, 10.0f); glVertex3f(-100.0f ,  100.0f , -725.0f);
            glTexCoord2f(0.0f, 10.0f);  glVertex3f(-100.0f ,  100.0f ,  000.0f);
            glTexCoord2f(0.0f, 00.0f);  glVertex3f(-100.0f , -100.0f ,  000.0f);
            glTexCoord2f(10.0f, 0.0f);  glVertex3f(-100.0f , -100.0f , -725.0f);

            //Back Wall
            glNormal3f( 0.0f , 0.0f, 1.0f);

            glTexCoord2f(10.0f, 10.0f); glVertex3f( 100.0f ,  100.0f , -725.0f);
            glTexCoord2f(0.0f, 10.0f);  glVertex3f(-100.0f ,  100.0f , -725.0f);
            glTexCoord2f(0.0f, 00.0f);  glVertex3f(-100.0f , -100.0f , -725.0f);
            glTexCoord2f(10.0f, 0.0f);  glVertex3f( 100.0f , -100.0f , -725.0f);


        glEnd();
    glPopAttrib();

}
/**
    Done Drawing Stuff
*/

/**
    Texture Stuff
*/

// load a 256x256 RGB .RAW file as a texture
GLuint LoadTextureRAW( const char * filename, int wrap , int width, int height )
{
    GLuint texture;
    GLbyte *data;
    FILE * file;

    // open texture data
    file = fopen( filename, "rb" );
    if ( file == NULL ) {
        printf("Not found");
        return 0;
    }

    data = (GLbyte*)malloc(width * height * 3);

    // read texture data
    fread( data, width * height * 3, 1, file );
    fclose( file );

    // allocate a texture name
    glGenTextures( 1, &texture );

    // select our current texture
    glBindTexture( GL_TEXTURE_2D, texture );

    // select modulate to mix texture with color for shading
    glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );

    // when texture area is small, bilinear filter the closest mipmap
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
                     GL_LINEAR_MIPMAP_NEAREST );
    // when texture area is large, bilinear filter the first mipmap
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

    // if wrap is true, the texture wraps over at the edges (repeat)
    //       ... false, the texture ends at the edges (clamp)
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
                     wrap ? GL_REPEAT : GL_CLAMP );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
                     wrap ? GL_REPEAT : GL_CLAMP );

    // build our texture mipmaps
    gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,
                       GL_RGB, GL_UNSIGNED_BYTE, data );

    // free buffer
    free( data );

    return texture;
}

EDIT: Screen Shots

http://dl.dropbox.com/u/12914798/Texture%20Atlas.png

http://dl.dropbox.com/u/12914798/ingame.png

http://dl.dropbox.com/u/12914798/Code%20Example.png

meriley
  • 1,831
  • 3
  • 20
  • 33

1 Answers1

1

would I just choose the pixels I want to draw and divide it by 128?

Yep. Is that not working for you? Seems like you know what to do, not sure why you didn't just try it :)

Tim
  • 35,413
  • 11
  • 95
  • 121
  • Hmm, not quite sure. I'm trying to guess what's being displayed on those blocks but I can't really tell, does that look like anything to you? Maybe something gone wrong in your texture loading function. Care to put up the whole drawing code in your post? (just put the code in a code block is preferable to attaching screenshot of IDE) – Tim Apr 14 '12 at 02:27
  • Sure thing. The texture loading is the same as the way i loaded the textures for the walls. But ill post the code. – meriley Apr 14 '12 at 03:21
  • Looks like a corrupted texture atlas caused the issue. Thanks for help! – meriley Apr 14 '12 at 04:48