OpenGL图像不映射到坐标

时间:2018-04-08 12:12:05

标签: opengl bitmap glut freeglut

当我尝试在正方形上显示图像时,它们无法正确映射到角落。 这些是原始图像和输出 -

Original Image

enter image description here

enter image description here

enter image description here

我从网上下载了原始图片,然后使用在线转换器将它们转换为bmp,以便在代码中使用它们。

这是代码 -

#include <stdio.h> 
#include <GL/glut.h>
GLuint LoadBMP(const char *fileName)
{
    FILE *file;
    unsigned char header[54],*data;
    unsigned int dataPos,size,width, height;
    file = fopen(fileName, "rb");
    fread(header, 1, 54, file);
    dataPos = *(int*)&(header[0x0A]);
    size = *(int*)&(header[0x22]);
    width = *(int*)&(header[0x12]);
    height = *(int*)&(header[0x16]);
    if (size == 0) size = width * height * 3;
    if (dataPos == 0) dataPos = 54;
    data = new unsigned char[size];
    fread(data, 1, size, file);
    fclose(file);
    GLuint texture;
    glGenTextures(1, &texture); 
    glBindTexture(GL_TEXTURE_2D, texture);  
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);  
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, data); 
    return texture;
}
GLint texture;

void display(void) 
{ 
 glClear( GL_COLOR_BUFFER_BIT); 
 // glColor3f(0.0, 1.0, 0.0); 
 glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, texture);
 glBegin(GL_POLYGON); 
  glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.5, -0.5, 0.0); 
  glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.5, 0.5, 0.0); 
  glTexCoord2f(1.0f, 1.0f); glVertex3f(0.5, 0.5, 0.0); 
  glTexCoord2f(1.0f, 0.0f); glVertex3f(0.5, -0.5, 0.0); 
 glEnd(); 
 glDisable(GL_TEXTURE_2D);

 glFlush(); 
}

int main(int argc, char **argv) 
{ 
 printf("hello world\n"); 
 glutInit(&argc, argv); 
 glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);

 glutInitWindowPosition(100,100); 
 glutInitWindowSize(500,500); 
 glViewport(0, 0, 500, 500);
 glutCreateWindow ("square");

 glClearColor(0.0, 0.0, 0.0, 0.0);         // black background 
 glMatrixMode(GL_PROJECTION);              // setup viewing projection 
 glLoadIdentity();                           // start with identity matrix 

 glutDisplayFunc(display); 
 texture = LoadBMP("wall.bmp");
 glutMainLoop();

 return 0; 
}

1 个答案:

答案 0 :(得分:1)

您的代码使用静态标头大小,因此由于Microsoft更改操作系统导致BMP标头更改不同,因此看起来可能会出现问题。

如果您使用的是Linux,请尝试使用file命令,找出您正在使用的BMP类型:

BMP Headers

您的代码看起来适用于Window 3.x格式,您应该尝试转换为该格式并再次检查。您可以将ffmpeg用于此目的:

ffmpeg