在写光栅渲染器时,需要加载图片获得像素以便进行纹理插值,试了几种方法发现下面这种比价简单,效率也可以接受

  Texture2D是我自己定义的类,其中m_pixelBuffer是一个动态二维数组,每个元素为ZCFLOAT3(自定义类型用来保存颜色rgb值)。

 #include "LoadBitmap.h"
#include <windows.h>
#include <gdiplus.h> #include <iostream>
#include <fstream>
#include <sstream> #pragma comment(lib, "gdiplus.lib")
using namespace std;
using namespace Gdiplus; Texture2D MathUtil::LoadBitmapToColorArray(wstring filePath)
{
GdiplusStartupInput gdiplusstartupinput;
ULONG_PTR gdiplustoken;
GdiplusStartup(&gdiplustoken, &gdiplusstartupinput, nullptr); Bitmap* bmp = new Bitmap(filePath.c_str());
if (!bmp)
{
MessageBox(nullptr, "error", "picture path is null!", MB_OK);
delete bmp;
GdiplusShutdown(gdiplustoken);
return Texture2D(,);
}
else
{
UINT height = bmp->GetHeight();
UINT width = bmp->GetWidth();
//Texture2D
Texture2D texture(width, height); Color color; for (int y = ; y < height; y++)
for (int x = ; x < width; x++)
{
bmp->GetPixel(x, y, &color); texture.m_pixelBuffer[x][y] = ZCFLOAT3(
color.GetRed() / .f,
color.GetGreen() / .f,
color.GetBlue() / .f
);
} delete bmp;
GdiplusShutdown(gdiplustoken);
return texture;
} }

最新文章

  1. HTML &lt;a&gt; 标签的 target 属性
  2. Google Chrome 应用商店上传扩展程序
  3. C# Socket 入门4 UPD 发送结构体(转)
  4. HDU 3555 Bomb (数位DP-记忆化搜索模板)
  5. WCF 绑定(Binding)
  6. QuickSort 递归 分治
  7. Android自己定义控件之应用程序首页轮播图
  8. C#进程间通讯技术-整理。
  9. 转:C++:从子类访问父类的私有函数
  10. WebStorm 快捷键一览
  11. argparse库 学习记录
  12. MATLAB符号对象与符号运算
  13. 自制模态窗体闪烁效果: MessageBeep &amp; FlashWindowEx
  14. (转)小谈keepalived vip漂移原理与VRRP协议
  15. ubuntu安装mysql,redis,python-mysqldb
  16. 廖雪峰Java2面向对象编程-6Java核心类-1字符串和编码
  17. js 表单序列化为json对象
  18. windows下dump文件调试
  19. Mac 平台安装 Android Studio 集成 Android SDK
  20. Iterable转List

热门文章

  1. That&#39;s life,多一些韧性,才有更多的任性(转)
  2. Java学习笔记——File类之文件管理和读写操作、下载图片
  3. IOS开发之----四舍五入问题
  4. a++为啥不能用作左值
  5. Ctrl-A全选
  6. ServicePrvider实现揭秘
  7. Linux查看用户数、登录用户
  8. apache +php +php curl 模块设置
  9. 怎样从host之外连接到docker container
  10. zend studio 10 实现代码自动换行