OpenGL为什么有的图像可以读取有的读取不了
以下为加载图片的函数:
#define BMP_Header_Length 54
GLuint MyGLWidget::load_texture(const char* file_name)
{
GLint width, height, total_bytes;
GLubyte* pixels = 0;
GLuint texture_ID = 0;
FILE* pFile = fopen(file_name, "rb");
if( pFile == 0 )
return 0;
fseek(pFile, 0x0012, SEEK_SET);
fread(&width, sizeof(width), 1, pFile);
fread(&height, sizeof(height), 1, pFile);
fseek(pFile, BMP_Header_Length, SEEK_SET);
{
GLint line_bytes = width * 3;
while( line_bytes % 4 != 0 )
++line_bytes;
total_bytes = line_bytes * height;
}
pixels = NULL;
pixels=(GLubyte*)malloc(total_bytes);
if( pixels == 0 )
{
fclose(pFile);
return 0;
}
GLint max;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max);
if( !power_of_two(width)
|| !power_of_two(height)
|| width > max
|| height > max )
{
const GLint new_width = 256;
const GLint new_height = 256;
GLint new_line_bytes, new_total_bytes;
GLubyte* new_pixels = 0;
new_line_bytes = new_width * 3;
while( new_line_bytes % 4 != 0 )
++new_line_bytes;
new_total_bytes = new_line_bytes * new_height;
new_pixels = (GLubyte*)malloc(new_total_bytes);
if( new_pixels == 0 )
{
free(pixels);
fclose(pFile);
return 0;
}
gluScaleImage(GL_RGB,width, height, GL_UNSIGNED_BYTE, pixels,new_width, new_height, GL_UNSIGNED_BYTE, new_pixels);
free(pixels);
pixels = new_pixels;
width = new_width;
height = new_height;
}
if( fread(pixels, total_bytes, 1, pFile) <= 0 )
{
free(pixels);
fclose(pFile);
return 0;
}
glGenTextures(1, &texture_ID);
if( texture_ID == 0 )
{
free(pixels);
fclose(pFile);
return 0;
}
glBindTexture(GL_TEXTURE_2D, texture_ID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, pixels);
free(pixels);
fclose(pFile);
return texture_ID;}相关推荐
opengl读取stl文件
STL是三维模型常用的文件格式。对STL文件进行读取和显示,是对模型进行后续操作的前提。在对 STL 文件格式进行详细分析的基础上,以 VC++作为开发平台;利用 C++标准 IO 库的 ifstream 类 型定义对象并绑定STL文件;再用标准库string类型中的getline逐行读取绑定的STL文件;最后, 应用 OpenGL 中绘制三角面片编程技术实现对 STL 文件格式的直观显示。通过试验验证了读取和显 示效果。
Android OpenGL 渲染图像读取哪家强
该原创文章首发于微信公众号:字节流动 glReadPixels glReadPixels 是 OpenGL ES 的 API ,OpenGL ES 2.0 和 3.0 均支持。 使用非常方便,下面一行代码即可搞定,但是效率也是最低的。 glReadPixels(0, 0, outImage.width, outImage.height, GL_RGBA, GL_UNSIGNED_BYTE, buffer); 当调用 glReadPixels 时,首先会影响 CPU 时钟周期,同时 GPU 会等待当前帧.
位图、图像(openGL)
0.概述 相对于几何图元而言的;opengl除了可以处理几何图元外,还可以处理位图和图像。位图和图像数据都是采用矩阵的像素数组的格式,它们之间的唯一区别在于位图的每一个像素是由一个位的信息组成的,而图像的每个像素一般包含了多个位。 另外opengl并不支持从文件中读取像素和图像以及把像素和图像保存到文件中。 1.位图 a.位图绘制 位图位置(光
opengl读取并在屏幕上显示24位bmp图像.doc
opengl读取并在屏幕上显示24位bmp图像.doc
OpenGL读取、显示bmp图片
测试环境:xcode13.3 MacPro13-inch2020 M1 OpenGL版本:OpenGL 4.5 语言:c++17 测试图片格式:bmp 建议: 1.先完成xcode配置OpenGL工程环境,网上有教程。 2.本程序支持读取任意分辨率的bmp图片,并显示。


工具平台和程序库
