资源简介
本程序为OpenGL纹理贴图小程序,图片格式为jpg。代码中如有疑问请指出,会为你一一解答。

代码片段和文件信息
#include “jpegtools.h“
void DecodeJPG(jpeg_decompress_struct* cinfo JPGtexture *pImageData)
{
GLint rowSpan;
//读取JPEG文件头
jpeg_read_header(cinfo TRUE);
// 使用压缩信息开始解压缩
jpeg_start_decompress(cinfo);
// 读取图像大小、像素数据
rowSpan = cinfo->image_width * cinfo->num_components;
pImageData->width = cinfo->image_width;
pImageData->height = cinfo->image_height;
//为pImageData->data分配内存
pImageData->texels = new GLubyte[rowSpan * pImageData->height];
switch (cinfo->num_components)
{
case 1:
{
pImageData->internalFormat = 1;
pImageData->format = GL_LUMINANCE;
break;
}
case 2:
{
pImageData->internalFormat = 2;
pImageData->format = GL_LUMINANCE_ALPHA;
break;
}
case 3:
{
pImageData->internalFormat = 3;
pImageData->format = GL_RGB;
break;
}
case 4:
{
pImageData->internalFormat = 4;
pImageData->format = GL_RGBA;
break;
}
default:
break;
}
//创建每一行数据的指针
GLubyte ** rowPtr = new GLubyte *[pImageData->height];
for (int i = 0; i < pImageData->height; i++)
rowPtr[i] = &(pImageData->texels[(pImageData->height-1-i)*rowSpan]);
//读取像素数据
int rowsRead = 0;
while (cinfo->output_scanline < cinfo->output_height)
{
rowsRead+=jpeg_read_scanlines(cinfo&rowPtr[rowsRead]
cinfo->output_height-rowsRead);
}
// 释放临时使用的指针
delete [] rowPtr;
// 解压缩结束
jpeg_finish_decompress(cinfo);
}
JPGtexture *ReadJPEGFromFile(const char *filename)
{
struct jpeg_decompress_struct cinfo;
JPGtexture *pImageData = NULL; //存放JPEG数据
FILE *pFile;
//打开文件
if((pFile = fopen(filename “rb“)) == NULL)
{
fprintf (stderr “error: couldn‘t open \“%s\“!\n“ filename);
return NULL;
}
// 定义一个错误句柄
jpeg_error_mgr jerr;
//解压缩信息对象指向错误句柄
cinfo.err = jpeg_std_error(&jerr);
// 初始化解压缩对象
jpeg_create_decompress(&cinfo);
//指定数据源
jpeg_stdio_src(&cinfo pFile);
//分配内存,用于存放数据
pImageData = (JPGtexture*)malloc(sizeof(JPGtexture));
// 进行解压缩
DecodeJPG(&cinfo pImageData);
// 释放内存
jpeg_destroy_decompress(&cinfo);
fclose(pFile);
// 返回已经解压缩后的数据
return pImageData;
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-04-14 13:16 JPEG_texture\
文件 427514 2016-10-11 13:18 JPEG_texture\car.jpg
文件 287 2016-10-10 13:58 JPEG_texture\Identity.fp
文件 327 2016-10-10 13:57 JPEG_texture\Identity.vp
文件 2756 2016-10-11 14:26 JPEG_texture\jpegtools.cpp
文件 444 2016-10-11 14:24 JPEG_texture\jpegtools.h
文件 5386 2016-10-11 14:20 JPEG_texture\main.cpp
文件 307 2016-10-11 13:25 JPEG_texture\Rectangle.pro
- 上一篇:Qt编写的串口收发数据小程序源码
- 下一篇:OpenGL透视投影小程序源码
相关资源
- OpenGL参考手册
- Qt Creator opengl实现四元数鼠标控制轨迹
- OpenGL文档,api大全,可直接查询函数
- opengl轮廓字体源代码
- MFC读三维模型obj文件
- 利用OpenGL写毛笔字算法
- MFC中OpenGL面和体的绘制以及动画效果
- 基于OPENGL的光线跟踪源代码368758
- VC 实现三维旋转(源码)
- 如何实现bmp位图透明贴图
- 基于Gabor滤波器的图像纹理特征提取
- 自编用openGL实现3D分形树,分形山
- OpenGL球形贴图自旋程序
- OpenGL导入贴图的Texture类
- 计算机图形学(openGL)代码
- 用OpenGL开发的机械臂运动仿真程序(
- OpenGL-3D坦克模拟
- 基于纹理的图像检索源代码
- OPENGL实现世界上最小的3D游戏
- VS2012OpenGL配置所需要的全部libdllh文件
- 线性和反向跷跷板中的最大零纹理
- 反向跷跷板中具有破碎的循环对称性
- 基于OpenGL的仿蝗虫机器人三维动态仿
- 图形学 - OpenGL实现3种三维茶壶显示源
- opengl程序-会跳舞的骷髅
- opengl实现三维网格光顺Laplacian算法
- opengl——爆炸
- OpenGL三维地形建模
- opengl游戏编程徐明亮版(含源码)
- 用OPENGL画的一个简单的直升飞机
评论
共有 条评论