0

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

在这种情况下的相关职能

GLuint textureIDs[3];
GLuint textureID;

地面建设者

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:加载 24bit 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;
}
4

0 回答 0