可以加载单个纹理但是在加载到数组时遇到问题

时间:2013-04-14 02:50:59

标签: c++ opengl

我无法将纹理加载到GLuints类型的数组上,但加载单个纹理时不会出现问题。当我调试它时,我看到textureIDs未分配任何值,即使在调用setTexture(...)后也只有0 但问题不会发生在加载到textureID的单个纹理上。要么我缺少一些如此明显的东西,要么缺乏对Opengl或C ++的理解。

此上下文中的相关功能

GLuint textureIDs[3];
GLuint textureID;

Ground Constructor

Ground::Ground(void) : 
textureId(0),groundTextures()
{
    setAllTexture();                        
}

绘制:绘制地面功能的地面功能

void Ground::draw()
{
    glPushMatrix();
    //Ground
    glPushMatrix(); 

    glTranslatef(0,-1,0); //all buildings and ground

    //ground
    glPushMatrix();
    glTranslatef(-5,0,-20);
    glScalef(40,0.2,40);
    setTexture("Textures/rue2.bmp", textureId, true, true);
    drawRectangle(1.0f);
    glPopMatrix();
    glPopMatrix();

}

settexture()设置纹理

void Ground::setTexture(const char* textureName, GLuint& textId, bool stretchX, bool     stretchZ)
{
    if (textId == 0)
    {
        textId =  (GLuint)createTexture(textureName);   
    }

    if (textId != 0)
    {
        //enable texture coordinate generation

        glEnable(GL_NORMALIZE);
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_COLOR_MATERIAL);
        glEnable(GL_TEXTURE_2D);
        glEnable(GL_BLEND); //Enable alpha blending

        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //Set the blend function

        glBindTexture(GL_TEXTURE_2D, textId);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, stretchX ? GL_REPEAT : GL_CLAMP);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, stretchZ ? GL_REPEAT : GL_CLAMP);
        glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    }

}

setAllTexture:将纹理加载到GLuints数组

int Ground::setAllTexture()
{
    setTexture("Textures/asphalt.bmp",groundTextures[0],false, false );
    //set up textures    for all of them and bind them to ground textures.
    glBindTexture(GL_TEXTURE_2D, groundTextures[0]);

    setTexture("Textures/concreteFloor.bmp",groundTextures[1],false, false);
    //set up textures for all of them and bind them to ground textures.
    glBindTexture(GL_TEXTURE_2D, groundTextures[1]);


    setTexture("Textures/dirtyGrass.bmp",groundTextures[2],false, false);
    //set up textures for all of them and bind them to ground texture
    glBindTexture(GL_TEXTURE_2D, groundTextures[2]);

    for ( unsigned int i =0 ; i < 3 ; i ++ )        
    //check to see if all textures loaded properly
    {
        if ( groundTextures[i] == 0 )
            return false;
        }
        return true;
        //if you're here, every texture was loaded correctly.
    }

ImageLoader:加载24位RGB位图

Image* loadBMP(const char* filename) {
    ifstream input;
    input.open(filename, ifstream::binary);
    assert(!input.fail() || !"Could not find file");
    char buffer[2];
    input.read(buffer, 2);
    assert(buffer[0] == 'B' && buffer[1] == 'M' || !"Not a bitmap file");
    input.ignore(8);
    int dataOffset = readInt(input);

    //Read the header
    int headerSize = readInt(input);
    int width;
    int height;
    switch(headerSize) {
        case 40:
            //V3
            width = readInt(input);
            height = readInt(input);
            input.ignore(2);
            assert(readShort(input) == 24 || !"Image is not 24 bits per pixel");
            assert(readShort(input) == 0 || !"Image is compressed");
            break;
        case 12:
            //OS/2 V1
            width = readShort(input);
            height = readShort(input);
            input.ignore(2);
            assert(readShort(input) == 24 || !"Image is not 24 bits per pixel");
            break;
        case 64:
            //OS/2 V2
            assert(!"Can't load OS/2 V2 bitmaps");
            break;
        case 108:
            //Windows V4
            assert(!"Can't load Windows V4 bitmaps");
            break;
        case 124:
            //Windows V5
            assert(!"Can't load Windows V5 bitmaps");
            break;
        default:
            assert(!"Unknown bitmap format");
    }

    //Read the data
    int bytesPerRow = ((width * 3 + 3) / 4) * 4 - (width * 3 % 4);
    int size = bytesPerRow * height;
    auto_array<char> pixels(new char[size]);
    input.seekg(dataOffset, ios_base::beg);
    input.read(pixels.get(), size);

    //Get the data into the right format
    auto_array<char> pixels2(new char[width * height * 3]);
    for(int y = 0; y < height; y++) {
        for(int x = 0; x < width; x++) {
            for(int c = 0; c < 3; c++) {
                pixels2[3 * (width * y + x) + c] =
                pixels[bytesPerRow * y + 3 * x + (2 - c)];
            }
        }
    }

    input.close();
    return new Image(pixels2.release(), width, height);
}

createTexture:创建纹理并返回GLuint

unsigned int createTexture( const char* imageName )
{
    Image* image = loadBMP(imageName);
    GLuint textureId = 0;
    glGenTextures(1, &textureId);
    glBindTexture(GL_TEXTURE_2D, textureId);
    glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,image->width, image-   >height,0,GL_RGB,GL_UNSIGNED_BYTE,image->pixels);

    delete image;

    return (unsigned int) textureId;
}

0 个答案:

没有答案