gpt4 book ai didi

C++ OpenGL 纹理未加载

转载 作者:搜寻专家 更新时间:2023-10-31 01:37:50 25 4
gpt4 key购买 nike

void OGLRectangle::LoadTexture(const char* filename)
{
unsigned int texture;
int width, height;
BYTE * data;
FILE * file;

file = fopen(filename, "rb");

width = 1920;
height = 1080;
data = new BYTE[height * width * 3];

fread(data, width * height * 3, 1, file);
fclose(file);

glGenTextures(1.0, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
tex = 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);

glTexImage2D(GL_TEXTURE_2D, 0, 2, width, height,0, GL_RGB, GL_UNSIGNED_BYTE, data);

delete [] data;

我有这段代码要在图像中渲染,调用该方法的方法是:LoadTexture("C:\\Users\Rhys\Documents\Hills.bmp");该文件存在。

然后我尝试使用以下方法将它渲染到 openGL 窗口;

    glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tex);
glBegin(GL_QUADS);
glTexCoord2d(0.0, 0.0); glVertex2d(0.0, 0.0);
glTexCoord2d(1.0, 0.0); glVertex2d(100.0, 0.0);
glTexCoord2d(1.0, 1.0); glVertex2d(100.0, 100.0);
glTexCoord2d(0.0, 1.0); glVertex2d(0.0, 100.0);
glEnd();
glDisable(GL_TEXTURE_2D);

但是,我在屏幕上看到的只是一个深蓝色框,其中没有渲染纹理。我已经搜索了有关如何执行此操作的教程,甚至问过我的讲师,但我似乎仍然无法找出它不起作用的原因。任何帮助将不胜感激。

最佳答案

加载的.bmp 文件一定有一点不同

此代码只是将 bmp 文件加载到内存 m_pcbData,不支持压缩和索引颜色。

bool CBMPImage::LoadFromFile(const CString& FileName)
{
BITMAPINFOHEADER BitmapInfo;
ZeroMemory(&BitmapInfo, sizeof(BITMAPINFOHEADER));

BITMAPFILEHEADER BitmapFile;
ZeroMemory(&BitmapFile, sizeof(BITMAPFILEHEADER));

std::ifstream FileStream(FileName, std::ios::binary | std::ios::in);

if (!FileStream.good())
return false;

// Read bitmap file info
FileStream.read(reinterpret_cast<char*>(&BitmapFile), sizeof(BITMAPFILEHEADER));
// Read bitmap info
FileStream.read(reinterpret_cast<char*>(&BitmapInfo), sizeof(BITMAPINFOHEADER));

// Proper bitmap file supports only 1 plane
if (BitmapInfo.biPlanes != 1)
return false;

m_cbAlphaBits = 0;
m_cbRedBits = 0;
m_cbGreenBits = 0;
m_cbBlueBits = 0;

// Retrives bits per pixel info
m_cbBitsPerPel = (BMPbyte)BitmapInfo.biBitCount;

// Width and height of image
m_nWidth = BitmapInfo.biWidth;
m_nHeight = BitmapInfo.biHeight;

// Compute bitmap file size
m_nSize = 4 * ((m_nWidth * m_cbBitsPerPel + 31) / 32) * m_nHeight;

// Less important info
m_nPixelWidthPerMeter = BitmapInfo.biXPelsPerMeter;
m_nPixelHeightPerMeter = BitmapInfo.biYPelsPerMeter;

// Indexes info not important in our case
m_nClrCount = BitmapInfo.biClrUsed;
m_nClrImportant = BitmapInfo.biClrImportant;

// COMPRESSION MUST BE BI_RGB
m_Compression = (BMPCompression)BitmapInfo.biCompression;

delete [] m_pcbData;
m_pcbData = NULL;

// Allocate proper data size
m_pcbData = new BMPbyte[m_nSize];

// Read actual image data, considering offset of file header
FileStream.seekg(BitmapFile.bfOffBits);
FileStream.read(reinterpret_cast<char*>(m_pcbData), m_nSize);

FileStream.close();

return true;
}

比将 bmp 纹理数据加载到 OpenGL

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, Image.GetWidth(), Image.GetHeight(), 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, (GLvoid*)Image.GetImageData());

GL_BGR_EXT 很重要,因为 bmp 以反向字节顺序存储图像数据。

其次,由于使用了纹理环境 GL_TEXTURE_ENV_MODE, GL_MODULATE

,您必须将 Material 颜色指定为白色

如@Reto Koradi 所述,您必须指定在使用这些函数调用之一加载纹理图像之前生成 mipmap。

glGenerateMipmap(GL_TEXTURE_2D);

glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);

加上你使用的不是两个纹理的幂 (width = 1920;
height = 1080;)
它可能不起作用。

关于C++ OpenGL 纹理未加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33653024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com