纹理不会出现FreeGLUT

问题描述 投票:0回答:2

我有一个简单的平原(正方形),上面有照明和材料,我想在上面放一个地球纹理。但什么也没出现。我已经检查过,并认为它可以正确加载RGB数据,因此我的猜测是此行存在问题(数据为* char,图像为BMP24)

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, outWidth, outHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, (GLvoid*)data);

这里是.bmp加载函数:

GLuint LoadTexture(const char* imagepath) {
    printf("Reading image %s\n", imagepath);
    unsigned int outWidth = -1;
    unsigned int outHeight = -1;
    unsigned char header[54];
    unsigned int dataPos;
    unsigned int imageSize;

    FILE* file;
    if (errno_t err = fopen_s(&file, imagepath, "rb") != 0) {
        perror("Can't Open file");
        return 0;
    }

    // If less than 54 byes are read, problem
    if (fread(header, 1, 54, file) != 54) {
        printf("Not a correct BMP file\n");
        return NULL;
    }
    // A BMP files always begins with "BM"
    if (header[0] != 'B' || header[1] != 'M') {
        printf("Not a correct BMP file\n");
        return NULL;
    }
    // Make sure this is a 24bpp file
    if (*(int*)&(header[0x1E]) != 0) { printf("Not a correct BMP file\n");    return NULL; }
    if (*(int*)&(header[0x1C]) != 24) { printf("Not a correct BMP file\n");    return NULL; }

    // Read the information about the image
    dataPos = *(int*)&(header[0x0A]);
    outWidth = *(int*)&(header[0x12]);
    outHeight = *(int*)&(header[0x16]);
    imageSize = *(int*)&(header[0x22]);

    // Some BMP files are misformatted, guess missing information
    if (imageSize == 0)    imageSize = outWidth * outHeight * 3; // 3 : one byte for each Red, Green and Blue component
    if (dataPos == 0)      dataPos = 54; // The BMP header is done that way

    // Read the actual data from the file into the buffer
    unsigned char* data = new unsigned char[imageSize];
    fread(data, 3, imageSize, file);


    // Everything is in memory now, the file wan be closed
    fclose(file);

    GLuint tex;
    glGenTextures(1, &tex);
    glBindTexture(GL_TEXTURE_2D, tex);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, outWidth, outHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, (GLvoid*)data);

    return tex;
}

这是主要和显示功能:

void render(void) {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    float ratio = 0.75;
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, earthTextureData);

    glBegin(GL_QUADS);
    glVertex3f(-0.5, -0.5, 0);
    glVertex3f(-0.5, 0.5, 0);
    glVertex3f(0.5, 0.5, 0);
    glVertex3f(0.5, -0.5, 0);
    glEnd();

    glDisable(GL_TEXTURE_2D);

    glutSwapBuffers();
}


int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(1200, 800);
    glutCreateWindow("app");

    init(); // custome initilizing
    initLighting();

    earthTextureData = LoadTexture("Texture/globe.bmp");

    glutDisplayFunc(render);
    glutReshapeFunc(reshape);
    glutMotionFunc(mouseMove);
    glutKeyboardFunc(keyboard);
    glutIdleFunc(idle);

    glutMainLoop();
}

我玩过代码,有时(当我使用int代替char *时,我得到了覆盖所有内容的红色材料,但不知道是什么原因造成的]]

我有一个简单的平原(正方形),上面有照明和材料,我想在上面放一个地球纹理。但什么也没出现。我已经检查过,并认为它可以正确加载RGB数据,所以我的猜测是...

c++ opengl glut texture-mapping freeglut
2个回答
0
投票

您必须告诉OpenGL有关在何处以及如何放置该纹理的UV坐标。这可以在您的渲染函数中完成。


0
投票

您错过了通过glTexParameter设置“设置纹理参数”。

© www.soinside.com 2019 - 2024. All rights reserved.