OpenGL Freeglute,未对齐的纹理,BMP格式

时间:2019-05-26 18:50:58

标签: c opengl textures render

我无法解决的小问题,但是我尝试结合...

我尝试显示的图片与原始图片不匹配:O The source bmp image 我的程序显示了什么: the misaligned picture

为什么它从结尾开始一点点,而不是原来的? 这个问题似乎与我猜不到的任何图像参数或该部分中的任何内容无关,但是也许我推测它必须与widht或height变量有关,或者我可能无法以正确的方式扫描图像?真的是我在这里迷路了,尝试了几乎所有的东西似乎都是一个小错误:(

GLuint LoadTexture(const char* filename)
{

GLuint texture;

int width, height;

unsigned char* data;

FILE* file;

fopen_s(&file, filename, "rb");

if (file == NULL) return 0;
width = 72;
height = 48;
data = (unsigned char*)malloc(width * height * 3);
//int size = fseek(file,);
fread(data, width * height * 3, 1, file);
fclose(file);

for (int i = 0; i < width * height; ++i)
{
    int index = i * 3;
    unsigned char B, R;
    B = data[index];
    R = data[index + 2];

    data[index] = R;
    data[index + 2] = B;

}

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);


glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height, GL_RGB, GL_UNSIGNED_BYTE, data);

free(data);

return texture;
}


void drawCube(float x, float y, int fill) {
glBindTexture(GL_TEXTURE_2D, texture);
glEnable(GL_TEXTURE_2D);

if (fill) {
    glBegin(GL_QUADS);
}
else {
    glBegin(GL_LINE_LOOP);

}
glTexCoord2f(0, 0); glVertex2f(x, y);
glTexCoord2f(1, 0); glVertex2f(x + 1, y);
glTexCoord2f(1, 1); glVertex2f(x + 1, y + 1);
glTexCoord2f(0, 1); glVertex2f(x, y + 1);
glEnd();
}

0 个答案:

没有答案