简体   繁体   English

C ++ OpenGL纹理未加载

[英]C++ OpenGL Texture not loading

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;

} }

I have this code to render in an image, the method is called with: LoadTexture("C:\\\\Users\\Rhys\\Documents\\Hills.bmp"); 我有这段代码可以在图像中呈现,该方法的调用方式为:LoadTexture(“ C:\\\\ Users \\ Rhys \\ Documents \\ Hills.bmp”); The file exists. 该文件存在。

Then I'm trying to render it to the openGL window using; 然后,我尝试使用渲染到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);

However, all I'm getting on screen is a darkish blue box, with no texture rendered in it. 但是,我在屏幕上看到的只是一个深蓝色的框,其中没有渲染纹理。 I have searched for tutorials on how to do this, even asked my lecturer and I still cannot seem to find out why its not working. 我已经搜索了有关如何执行此操作的教程,甚至问了我的讲师,我仍然似乎无法找出为什么它不起作用。 Any help will be greatly appreciated. 任何帮助将不胜感激。

The .bmp files loading must be little different .bmp文件加载必须稍有不同

This code simply loads bmp file to memory m_pcbData without compression and indexed color support. 此代码仅将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;
}

than load bmp texture data to OpenGL 比将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 is important because bmp stores image data in reverse byte order. GL_BGR_EXT很重要,因为bmp以相反的字节顺序存储图像数据。

Secondly you must specify your material color as white because of usage that texture environment GL_TEXTURE_ENV_MODE, GL_MODULATE 其次,由于纹理环境GL_TEXTURE_ENV_MODE, GL_MODULATE的使用,您必须将材料颜色指定为白色

And as mentioned @Reto Koradi, you must specify to generate mipmaps before texture image loading using one of these function calls. 就像@Reto Koradi提到的那样,您必须指定在使用这些函数调用之一加载纹理图像之前生成mipmap。

glGenerateMipmap(GL_TEXTURE_2D);

or 要么

glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);

Plus as you used not power of two textures (width = 1920; height = 1080;) it may not work. 另外,由于您没有使用两个纹理的幂(width = 1920; height = 1080;)因此它可能无法工作。

You're setting the attribute to sample with mipmaps: 您正在设置属性以使用mipmaps进行采样:

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);

You should only set that if your textures actually has mipmaps. 仅当纹理实际具有mipmap时才应进行设置。 To generate mipmaps, you can call: 要生成Mipmap,可以调用:

glGenerateMipmap(GL_TEXTURE_2D);

after the glTexImage2D() call. glTexImage2D()调用之后。 Or you can simply set the sampler attribute to not use mipmaps: 或者,您可以简单地将sampler属性设置为不使用mipmap:

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

As has already been pointed out: If your image file is indeed a BMP, and not just a raw image file, your image loading code will also need work. 正如已经指出的那样:如果您的图像文件确实是BMP,而不仅仅是原始图像文件,则图像加载代码也将需要工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM