texture colours opengl

Posted by user1324894 on Stack Overflow See other posts from Stack Overflow or by user1324894
Published on 2012-04-11T17:25:17Z Indexed on 2012/04/11 17:29 UTC
Read the original article Hit count: 197

Filed under:
|
|
|
|

Hi I am making a simple 2D game in c++ and for the map I am doing texture mapping by using tiles and assigning textures to those tiles. However, when I run the programme the textures become black and white when I want them to be the colour they are in the .png image. This is my code:

int worldMap[10][10] = {
    {0,0,0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0,0,0},
};

void background()
{
    glClearColor(0.0,0.0,0.0,0.0);

    /**********************************************************************************************/

    // Texture loading object
    nv::Image img;

    // Return true on success
    if(img.loadImageFromFile("Image_Loading/field.png"))
    {
        glGenTextures(1, &myTexture);
        glBindTexture(GL_TEXTURE_2D, myTexture);
        glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
        glTexImage2D(GL_TEXTURE_2D, 0, img.getInternalFormat(), img.getWidth(), img.getHeight(), 0, img.getFormat(), img.getType(), img.getLevel(0));
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
        glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 16.0f);       
    }

    else
        MessageBox(NULL, "Failed to load texture", "End of the world", MB_OK | MB_ICONINFORMATION);

    /**********************************************************************************************/
}

void drawTiles (void) { //our function to draw the tiles

    for (int i = 0; i < 10; i++) //loop through the height of the map
    {
        for (int j = 0; j < 10; j++) //loop through the width of the map
        {
            if (worldMap[i][j] == 0) //if the map at this position contains a 0
            {
                glBindTexture( GL_TEXTURE_2D, myTexture ); //bind our grass texture to our shape
            }

            glPushMatrix(); //push the matrix so that our translations only affect this tile
                glTranslatef(j, -i, 0); //translate the tile to where it should belong

                glBegin (GL_QUADS); //begin drawing our quads
                    glTexCoord2d(10, 0);
                    glVertex2f((-10 + mapX),(-10 + mapY)); //with our vertices we have to assign a texcoord

                    glTexCoord2d(10, 0);
                    glVertex2f((10 + mapX),(-10 + mapY)); //so that our texture has some points to draw to

                    glTexCoord2d(10, 10);
                    glVertex2f((10 + mapX),(10 + mapY));

                    glTexCoord2d(0, 10);
                    glVertex2f((-10 + mapX),(10 + mapY));
                glEnd();
            glPopMatrix(); //pop the matrix
        } //end first loop
    } //end second loop
}

void display()
{
    glClearColor (0.0,0.0,0.0,1.0);
    glClear(GL_COLOR_BUFFER_BIT);

    /**********************************************************************************************/

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    gluOrtho2D( -5, 5, -5, 5);

    glMatrixMode( GL_MODELVIEW );

    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, myTexture);

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    drawTiles();

    glDisable(GL_TEXTURE_2D);

    glDisable(GL_BLEND);
    /**********************************************************************************************/
}


void character ()
{

    glBegin(GL_POLYGON);
        glVertex2f((-0.5+characterX),(-0.5 +characterY));
        glVertex2f((-0.5+characterX),(0.5+characterY));
        glVertex2f((0.5+characterX),(0.5+characterY));
        glVertex2f((0.5+characterX),(-0.5+characterY));

        glTranslatef(characterX, characterY, 0.0f);

    glEnd();


}

Can anybody help please?

© Stack Overflow or respective owner

Related posts about c++

Related posts about opengl