注意bmp图片的格式问题,32位ARGB  或者24位RGB。你所采用的素材一定要注意是多少位的就用多少位的。否则会显示错误的图片或者其他什么的错误。

代码如下

32位版本

#include <GL/glut.h>

static GLint     ImageWidth;
static GLint ImageHeight;
static GLint PixelLength;
static GLubyte* PixelData; #include <stdio.h>
#include <stdlib.h> void display(void)
{
// 清除屏幕并不必要
// 每次绘制时,画面都覆盖整个屏幕
// 因此无论是否清除屏幕,结果都一样
// glClear(GL_COLOR_BUFFER_BIT); // 绘制像素
// glBitmap(ImageWidth,ImageHeight,0,0,0.0,0.0,PixelData);
glDrawPixels(ImageWidth, ImageHeight,
GL_ABGR_EXT, GL_UNSIGNED_BYTE, PixelData); // 完成绘制
glutSwapBuffers();
} int main(int argc, char* argv[])
{
// 打开文件
FILE* pFile = fopen("/home/aerk/bbb.bmp", "rb");
if( pFile == )
exit(); // 读取图象的大小信息
fseek(pFile, 0x0012, SEEK_SET);
fread(&ImageWidth, sizeof(ImageWidth), , pFile);
fread(&ImageHeight, sizeof(ImageHeight), , pFile); // 计算像素数据长度
PixelLength = ImageWidth * ;
while( PixelLength % != )
++PixelLength;
PixelLength *= ImageHeight; // 读取像素数据
PixelData = (GLubyte*)malloc(PixelLength);
if( PixelData == )
exit(); fseek(pFile, , SEEK_SET);
fread(PixelData, PixelLength, , pFile); // 关闭文件
fclose(pFile); // 初始化GLUT并运行
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(, );
glutInitWindowSize(ImageWidth, ImageHeight);
glutCreateWindow("FileName");
glutDisplayFunc(&display);
glutMainLoop(); // 释放内存
// 实际上,glutMainLoop函数永远不会返回,这里也永远不会到达
// 这里写释放内存只是出于一种个人习惯
// 不用担心内存无法释放。在程序结束时操作系统会自动回收所有内存
free(PixelData); return ;
}

24位版本

#include <GL/glut.h>

static GLint     ImageWidth;
static GLint ImageHeight;
static GLint PixelLength;
static GLubyte* PixelData; #include <stdio.h>
#include <stdlib.h> void display(void)
{
// 清除屏幕并不必要
// 每次绘制时,画面都覆盖整个屏幕
// 因此无论是否清除屏幕,结果都一样
// glClear(GL_COLOR_BUFFER_BIT); // 绘制像素
// glBitmap(ImageWidth,ImageHeight,0,0,0.0,0.0,PixelData);
glDrawPixels(ImageWidth, ImageHeight,
GL_BGR_EXT, GL_UNSIGNED_BYTE, PixelData); // 完成绘制
glutSwapBuffers();
} int main(int argc, char* argv[])
{
// 打开文件
FILE* pFile = fopen("/home/aerk/bbb.bmp", "rb");
if( pFile == )
exit(); // 读取图象的大小信息
fseek(pFile, 0x0012, SEEK_SET);
fread(&ImageWidth, sizeof(ImageWidth), , pFile);
fread(&ImageHeight, sizeof(ImageHeight), , pFile); // 计算像素数据长度
PixelLength = ImageWidth * ;
while( PixelLength % != )
++PixelLength;
PixelLength *= ImageHeight; // 读取像素数据
PixelData = (GLubyte*)malloc(PixelLength);
if( PixelData == )
exit(); fseek(pFile, , SEEK_SET);
fread(PixelData, PixelLength, , pFile); // 关闭文件
fclose(pFile); // 初始化GLUT并运行
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(, );
glutInitWindowSize(ImageWidth, ImageHeight);
glutCreateWindow("FileName");
glutDisplayFunc(&display);
glutMainLoop(); // 释放内存
// 实际上,glutMainLoop函数永远不会返回,这里也永远不会到达
// 这里写释放内存只是出于一种个人习惯
// 不用担心内存无法释放。在程序结束时操作系统会自动回收所有内存
free(PixelData); return ;
}

opengl打开本地bmp图片绘制的更多相关文章

  1. perl打开本地/服务器图片

    index.html <html> <body> <h2> perl read img </h2> <img src = "displa ...

  2. MFC 对话框Picture Control(图片控件)中静态和动态显示Bmp图片

    版权声明:本文为博主原创文章,转载请注明CSDN博客源地址! 共同学习,一起进步~ https://blog.csdn.net/Eastmount/article/details/26404733   ...

  3. [转]opengl入门例题(读取bmp图片,并显示)

    #include<gl/glut.h> #define FileName "bliss.bmp" static GLint imagewidth; static GLi ...

  4. OPENGL 显示BMP图片+旋转

    VS2010/Windows 7/ 1. 需包含头文件 stdio.h, glaux.h, glut.h.需要对应的lib,并添加包含路径 2. 窗口显示用glut库的函数 3. bmp图片从本地读取 ...

  5. MFC对话框显示BMP图片

    1.MFC对话框显示BMP图片我们先从简单的开始吧.先分一个类: (一) 非动态显示图片(即图片先通过资源管理器载入,有一个固定ID) (二) 动态载入图片(即只需要在程序中指定图片的路径即可载入) ...

  6. BMP图片格式模型(2)

    因为公司的主要业务是图像识别相关的,因此对图像处理.识别是我学习的重点.虽然写程序也不少年了,但是对于图像处理领域,我还是一个新兵.对很多基础的概念也还是存在盲区,所以想在边学边做的过程中,对一些概念 ...

  7. OpenGL入门学习 课程 (三) 绘制几何图形的一些细节问题

    http://oulehui.blog.163.com/blog/static/79614698201191832753312/ 先回顾一下我们都学习了些什么: 第一课,编写第一个OpenGL程序第二 ...

  8. bmp图片的有关操作

    读取bmp图片 并生成新的bmp图片 #include "stdafx.h"#include <windows.h>#include <cmath>#inc ...

  9. Windows 保存BMP图片

    在Windows下保存BMP图片还是挺方便的,直接上代码,拷贝就能用 void savebmp(uchar * pdata, char * bmp_file, int width, int heigh ...

随机推荐

  1. 文件I/O(不带缓冲)之原子操作

    一.添写至一个文件 考虑一个进程,它要将数据添加到一个文件尾端.早期的UNIX系统并不支持open的O_APPEND选项,所以程序被编写成下列形式: ) < ) /* position to E ...

  2. (原)linux 编译 lwqq

    1.安装工具 apt-get install automake apt-get install autoconf apt-get install libtool apt-get install lib ...

  3. Pitfalls of the Hibernate Second-Level / Query Caches--reference

    This post will go through how to setup the Hibernate Second-Level and Query caches, how they work an ...

  4. Memcached source code analysis -- Analysis of change of state--reference

    This article mainly introduces the process of Memcached, libevent structure of the main thread and w ...

  5. Asp.Net 之 下载文件的常用方式

    1.直接使用Response.TransmitFile(filename)方法 protected void Button_Click(object sender, EventArgs e) { /* ...

  6. Android开发环境搭建完全图解(转)

    本文介绍从0开始,在Linux系统下,搭建一个Android开发环境的方法. 如果你是<Learning Android>这本书的读者,你也可以参考这篇文章,因为这篇文章是以书中的安装方法 ...

  7. android客户端从服务器端获取json数据并解析的实现代码

    今天总结一下android客户端从服务器端获取json数据的实现代码,需要的朋友可以参考下   首先客户端从服务器端获取json数据 1.利用HttpUrlConnection /** * 从指定的U ...

  8. HTML5表单内元素的required属性

    当表单内元素(如input.textarea)中有required属性并且值不为false的时候,则该字段不能为空,否则提交失败. <input type="text" au ...

  9. Matlab使用难点记忆

    MATLAB的数据显示格式 虽然在MATLAB系统中数据的存储和计算都是双精度进行的,但MATLAB可以利用菜单或format命令来调整数据的显示格式.Format命令的格式和作用如下: l  for ...

  10. 解决闪光灯代码在Nexus5上面打不开的问题

    参考:http://blog.csdn.net/cy524563/article/details/41545387 关键在于: int textureId = 0;whyCamera.setPrevi ...