OpenGL纹理无法正确加载

时间:2013-07-18 10:33:12

标签: c++ opengl glut

我正在关注一系列教程,并且我已经达到了我已经复制的行代码停止工作的程度。我正在尝试渲染我加载的bmp图像,但在显示图像时,只显示多色线条,而不是加载的纹理。

显示时显示'纹理':

Messed up texture http://i40.tinypic.com/10hlg1v.jpg 以下是它应该是的:

Correct Texture http://i40.tinypic.com/16hp0tz.jpg

这是我加载位图的代码:

FILE* fp;
fp = fopen(filename, "r");

if (!fp) {
    perror("File could not be read");
    fclose(fp);
    return NULL;
}
char* headerField = new char[2];
fread(headerField,2,sizeof(char),fp);
if (strcmp(headerField, "BM"))
{
    delete[] headerField;
    perror("File is not a bitmap");
    fclose(fp);
    return NULL;

}

delete[] headerField;

unsigned int bmpDataLocation;

unsigned int bmpWidth;
unsigned int bmpHeight;
unsigned short numColorPlanes;
unsigned short bitsPerPixel;
unsigned int compressionMethod;
unsigned int bmpDataSize;

fseek(fp, 0x000a, SEEK_SET);
fread(&bmpDataLocation, 1, sizeof(unsigned int), fp);

fseek(fp, 0x0012, SEEK_SET);
fread(&bmpWidth, 1, sizeof(unsigned int), fp);
fread(&bmpHeight, 1, sizeof(unsigned int), fp);
fread(&numColorPlanes, 1, sizeof(unsigned short), fp);
fread(&bitsPerPixel, 1, sizeof(unsigned short), fp);
fread(&compressionMethod, 1, sizeof(unsigned int), fp);
fread(&bmpDataSize, 1, sizeof(unsigned int), fp);

if (numColorPlanes != 1 || bitsPerPixel != 24 || compressionMethod != 0)
{
    cout << "File is not raw BMP24" << endl;
    fclose(fp);
    return NULL;
}


unsigned char* bmpData = new unsigned char[bmpDataSize];

fseek(fp, bmpDataLocation, SEEK_SET);
fread(&bmpData, bmpDataSize, sizeof(unsigned char), fp);

fclose(fp);

这是我正在使用的纹理参数:

glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, format, GL_UNSIGNED_BYTE, data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);

这是我的渲染和显示代码:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

//Camara Transformations

glRotatef(Camara::rotation.x, 1, 0, 0);
glRotatef(Camara::rotation.y, 0, 1, 0);
glRotatef(Camara::rotation.z, 0, 0, 1);
glTranslatef(-Camara::position.x, -Camara::position.y, -Camara::position.z);

glBegin(GL_TRIANGLES);
    glColor3f(1.0f, 0.0f, 0.0f);
        glVertex3f(-1, 0, -3);

    glColor3f(0.0f, 1.0f, 0.0f);
        glVertex3f(0, 2, -3);

    glColor3f(0.0f, 0.0f, 1.0f);
        glVertex3f(1, 0, -3);
glEnd();


glBindTexture(GL_TEXTURE_2D, tex->getTextureID());

glBegin(GL_QUADS);

glColor3f(1, 1, 1);

glTexCoord2f(100, 100);
glVertex3f(100, 0, 100);

glTexCoord2f(-100, 100);
glVertex3f(-100, 0, 100);

glTexCoord2f(-100, -100);
glVertex3f(-100, 0, -100);

glTexCoord2f(100, -100);
glVertex3f(100, 0, -100);

glEnd();

glBegin(GL_QUADS);

glColor3f(1, 1, 1);

glTexCoord2f(100, 100);
glVertex3f(100, 10, 100);

glTexCoord2f(-100, 100);
glVertex3f(-100, 10, 100);

glTexCoord2f(-100, -100);
glVertex3f(-100, 10, -100);

glTexCoord2f(100, -100);
glVertex3f(100, 10, -100);

glEnd();


glBindTexture(GL_TEXTURE_2D, 0);

glutSwapBuffers();

所有正常的gl设置都已完成,因为在乱搞纹理的左边只是一个三角形。

编辑:显然混乱所谓的纹理是随机的,现在看起来像这样: Messed up Texture 2 http://i40.tinypic.com/w1d00z.jpg

如果我希望我的天空和地板看起来像电脑内部,这一切都非常好,但我真的不希望这样......

编辑:主要功能

中的代码
    //Init GLUT and OpenGL/AL
glutInit(&argc, argv);
glutInitWindowPosition(0, 0);
glutInitWindowSize(WINDOW_WIDTH,WINDOW_HEIGHT);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);

//Create Window Stuffs
glutCreateWindow(WINDOW_TITLE);

//Setup GLUT Callback Functions

glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(display);

glutKeyboardFunc(Keyboard::keyDown);
glutKeyboardUpFunc(Keyboard::keyUp);

glutMotionFunc(Mouse::move);
glutPassiveMotionFunc(Mouse::move);

//Enable features

glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);

glGetError();

//Setup Subcontent

tex = Texture::loadBitmap("Texture.bmp");
if (!tex)
{
    return 1;
}

Camara::position.y = 1.6f;


//Enter main loop
glutMainLoop();


return 0;

1 个答案:

答案 0 :(得分:0)

你的问题应该是你如何阅读数据,OpenGL代码和纹理绑定似乎对我来说是正确的。 另外要指出的是,对于我认为你有问题的部分BMP加载,这是一个C而不是C ++。

无论如何我怀疑读取标题,例如对于NumberOfPlanes,BitsPerPixel,TypeCompression我已经为每个读取了2个字节,我只看到1个在你身边。 Wiki包含此类细节。

一旦完成它,我会建议你,数据的字节顺序将以BGR格式翻转,你需要处理它(在纹理绑定或加载时)图像)。

正如评论中提到的那样,如果您使用第三方库(DeVil,FreeImage,无论如何),为每种格式创建自己的加载器都会让您戒掉美好时光,这真的是最好的,我认为没有人我想特别坚持使用其中一个非常相关的OpenGL功能,纹理压缩。