我似乎无法加载位图并显示它:S ...有谁知道这里有什么问题?我的bmp是256 x 265的大小。我目前只能看到黑屏(没有显示任何内容)。任何帮助将不胜感激!
int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing
{
GLuint texture = LoadBMP("C:/Untitled.bmp");
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
glBegin(GL_QUADS);
glTexCoord2d(2.8f, 2.8f); glVertex2f(2.8f, 2.8f);
glTexCoord2d(2.8f, 2.8f); glVertex2f(2.8f, 2.8f);
glTexCoord2d(2.8f, 2.8f); glVertex2f(2.8f, 2.8f);
glTexCoord2d(2.8f, 2.8f); glVertex2f(2.8f, 2.8f);
glEnd();
glDisable(GL_TEXTURE_2D);
return TRUE; // Keep Going
}
GLuint LoadBMP(const char *fileName)
{
FILE *file;
unsigned char header[54];
unsigned int dataPos;
unsigned int size;
unsigned int width, height;
unsigned char *data;
file = fopen(fileName, "rb");
if (file == NULL)
{
MessageBox(NULL, L"Error: Invaild file path!", L"Error", MB_OK);
return false;
}
if (fread(header, 1, 54, file) != 54)
{
MessageBox(NULL, L"Error: Invaild file!", L"Error", MB_OK);
return false;
}
if (header[0] != 'B' || header[1] != 'M')
{
MessageBox(NULL, L"Error: Invaild file!", L"Error", MB_OK);
return false;
}
dataPos = (int)&(header[0x0A]);
size = (int)&(header[0x22]);
width = (int)&(header[0x12]);
height = (int)&(header[0x16]);
if (size == NULL)
size = width * height * 3;
if (dataPos == NULL)
dataPos = 54;
data = new unsigned char[size];
fread(data, 1, size, file);
fclose(file);
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
return texture;
}
答案 0 :(得分:1)
当然你没有看到任何东西,你为每个顶点使用相同的纹理坐标:)
OpenGL中的纹理坐标(除非您使用的是特殊的 矩形纹理 )在 0.0 - 范围内标准化1.0 即可。其中( 0.0 , 0.0 )表示图像的左下角,( 1.0 , 1.0 )表示右上角。您应该考虑在四边形上拉伸图像一次,因此请使用坐标的所有四个角的组合。
此外,使用glTexCoord2f (...)
,因为您显然使用单精度浮点坐标而不是双精度。
glBegin( GL_QUADS);
glTexCoord2f (0.0f, 0.0f); glVertex2f (-1.0f, -1.0f);
glTexCoord2f (0.0f, 1.0f); glVertex2f (-1.0f, 1.0f);
glTexCoord2f (1.0f, 1.0f); glVertex2f ( 1.0f, 1.0f);
glTexCoord2f (1.0f, 0.0f); glVertex2f ( 1.0f, -1.0f);
glEnd ();