Irrlicht 3D Engine 笔记系列 之 教程6- 2D Graphics
作者:i_dovelemon
日期:2015 / 7 / 1
来源: CSDN
主题:2D Graphics, Irrlicht
教程翻译
#include <irrlicht.h>
#include "driverChoice.h" using namespace irr; #ifdef _MSC_VER
#pragma comment(lib, "Irrlicht.lib")
#endif
int main()
{
// ask user for driver
video::E_DRIVER_TYPE driverType=driverChoiceConsole();
if (driverType==video::EDT_COUNT)
return 1; // create device IrrlichtDevice *device = createDevice(driverType,
core::dimension2d<u32>(512, 384)); if (device == 0)
return 1; // could not create selected driver. device->setWindowCaption(L"Irrlicht Engine - 2D Graphics Demo"); video::IVideoDriver* driver = device->getVideoDriver();
在本教程中全部须要使用到的2d图形。都保存在纹理文件2ddemo.png中(能够在引擎目录中找到)。
因为我们希望绘制带有colorkey的sprite,所以,我们须要载入这个纹理,然后通知引擎,怎样依据colorkey来使纹理的哪一个部分透明掉。
video::ITexture* images = driver->getTexture("../../media/2ddemo.png");
driver->makeColorKeyTexture(images, core::position2d<s32>(0,0));
为了可以绘制文本。我们须要先载入字体。首先。我们使用引擎内置的字体来绘制。然后载入另外一个外部的字体。
同一时候我们还须要指定在纹理的什么位置存在着我们想要绘制的图片。
gui::IGUIFont* font = device->getGUIEnvironment()->getBuiltInFont();
gui::IGUIFont* font2 =
device->getGUIEnvironment()->getFont("../../media/fonthaettenschweiler.bmp"); core::rect<s32> imp1(349,15,385,78);
core::rect<s32> imp2(387,15,423,78);
准备一个好的2D过滤器,用来对纹理尽心过滤採样等。
driver->getMaterial2D().TextureLayer[0].BilinearFilter=true;
driver->getMaterial2D().AntiAliasing=video::EAAM_FULL_BASIC;
好了,全部的工作,都准备完成了,如今我们在绘制循环中绘制全部的内容。在本教程中。我们只绘制2d图形,可是我们相同能够在beginscene和endscene之间加入其它绘制3D图形的函数调用。
while(device->run() && driver)
{
if (device->isWindowActive())
{
u32 time = device->getTimer()->getTime(); driver->beginScene(true, true, video::SColor(255,120,102,136));
首先,我们绘制3个sprite。函数的最后一个參数,表示我们是否使用纹理像素中的alpha通道值。倒数第二个參数用于给定一个颜色值,通过这个值我们可以对图形2d图形进行二次着色。而且改变纹理总体的透明度。假设值为(255,255,255,255)那么纹理将保持不变。最后一个sprite使用基于时间来改变的r通道进行绘制。
// draw fire & dragons background world
driver->draw2DImage(images, core::position2d<s32>(50,50),
core::rect<s32>(0,0,342,224), 0,
video::SColor(255,255,255,255), true); // draw flying imp
driver->draw2DImage(images, core::position2d<s32>(164,125),
(time/500 % 2) ? imp1 : imp2, 0,
video::SColor(255,255,255,255), true); // draw second flying imp with colorcylce
driver->draw2DImage(images, core::position2d<s32>(270,105),
(time/500 % 2) ? imp1 : imp2, 0,
video::SColor(255,(time) % 255,255,255), true);
绘制文本很的简单。以下的代码已经可以自我解释了。
// draw some text
if (font)
font->draw(L"This demo shows that Irrlicht is also capable of drawing 2D graphics.",
core::rect<s32>(130,10,300,50),
video::SColor(255,255,255,255)); // draw some other text
if (font2)
font2->draw(L"Also mixing with 3d graphics is possible.",
core::rect<s32>(130,20,300,60),
video::SColor(255,time % 255,time % 255,255));
接下来。我们绘制一个Irrlicht引擎的Logo。因为我们使用了过滤器,所以略微的对纹理进行缩放操作。
driver->enableMaterial2D();
driver->draw2DImage(images, core::rect<s32>(10,10,108,48),
core::rect<s32>(354,87,442,118));
driver->enableMaterial2D(false);
最后,在鼠标的位置绘制一个半透明的矩形出来。
core::position2d<s32> m = device->getCursorControl()->getPosition();
driver->draw2DRectangle(video::SColor(100,255,255,255),
core::rect<s32>(m.X-20, m.Y-20, m.X+20, m.Y+20)); driver->endScene();
}
} device->drop(); return 0;
}
好了,这就是本教程的所有了。
重点内容解析
IVideoDriver继承树
- CBurningVideoDriver
- CD3D8Driver
- CD3D9Driver
- COpenGLDriver
- CSoftwareDrive
对于一个CBurningVideoDriver,临时不知道基于什么设备驱动实现,貌似是优化的软件渲染设备。
至于后面的CD3D8Driver就是基于DirectX8的渲染设备,CD3D9Driver就是基于DirectX9的渲染设备;COpenGLDriver就是基于OpenGL的渲染设备了。CSoftwareDriver就是基于软件实现的渲染设备。
接下来,我们具体的看看IVideoDriver的作用。
IVideoDriver的用途
/** This interface is one of the most important interfaces of
the Irrlicht Engine: All rendering and texture manipulation is done with
this interface. You are able to use the Irrlicht Engine by only
invoking methods of this interface if you like to, although the
irr::scene::ISceneManager interface provides a lot of powerful classes
and methods to make the programmer's life easier.
*/
引擎怎样选择创建哪种VideoDriver?
<span style="font-family:Microsoft YaHei;font-size:14px;">//! create the driver
void CIrrDeviceWin32::createDriver()
{
switch(CreationParams.DriverType)
{
case video::EDT_DIRECT3D8:
#ifdef _IRR_COMPILE_WITH_DIRECT3D_8_ VideoDriver = video::createDirectX8Driver(CreationParams, FileSystem, HWnd); if (!VideoDriver)
{
os::Printer::log("Could not create DIRECT3D8 Driver.", ELL_ERROR);
}
#else
os::Printer::log("DIRECT3D8 Driver was not compiled into this dll. Try another one.", ELL_ERROR);
#endif // _IRR_COMPILE_WITH_DIRECT3D_8_ break; case video::EDT_DIRECT3D9:
#ifdef _IRR_COMPILE_WITH_DIRECT3D_9_ VideoDriver = video::createDirectX9Driver(CreationParams, FileSystem, HWnd); if (!VideoDriver)
{
os::Printer::log("Could not create DIRECT3D9 Driver.", ELL_ERROR);
}
#else
os::Printer::log("DIRECT3D9 Driver was not compiled into this dll. Try another one.", ELL_ERROR);
#endif // _IRR_COMPILE_WITH_DIRECT3D_9_ break; case video::EDT_OPENGL: #ifdef _IRR_COMPILE_WITH_OPENGL_
switchToFullScreen(); VideoDriver = video::createOpenGLDriver(CreationParams, FileSystem, this);
if (!VideoDriver)
{
os::Printer::log("Could not create OpenGL driver.", ELL_ERROR);
}
#else
os::Printer::log("OpenGL driver was not compiled in.", ELL_ERROR);
#endif
break; case video::EDT_SOFTWARE: #ifdef _IRR_COMPILE_WITH_SOFTWARE_
switchToFullScreen(); VideoDriver = video::createSoftwareDriver(CreationParams.WindowSize, CreationParams.Fullscreen, FileSystem, this);
#else
os::Printer::log("Software driver was not compiled in.", ELL_ERROR);
#endif break; case video::EDT_BURNINGSVIDEO:
#ifdef _IRR_COMPILE_WITH_BURNINGSVIDEO_
switchToFullScreen(); VideoDriver = video::createBurningVideoDriver(CreationParams, FileSystem, this);
#else
os::Printer::log("Burning's Video driver was not compiled in.", ELL_ERROR);
#endif
break; case video::EDT_NULL:
// create null driver
VideoDriver = video::createNullDriver(FileSystem, CreationParams.WindowSize);
break; default:
os::Printer::log("Unable to create video driver of unknown type.", ELL_ERROR);
break;
}
}</span>
从这个函数就能够看出。创建VideoDriver的决定是依据CreationParams.DriverType的值来决定的,而这个值是由用户在创建Device的时候指定的,对于本教程来说该值为:EDT_DIRECT3D9。
ITexture继承树
我们相同的对该接口以及接口的继承树了解下。以下是它的继承树结构图:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaV9kb3ZlbGVtb24=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
- CD3D8Texture
- CD3D9Texture
- COpenGLTexture
- CSoftwareTexture
- CSoftwareTexture2
- SDummyTexture
ITexture的用途
/** An ITexture is created by an IVideoDriver by using IVideoDriver::addTexture
or IVideoDriver::getTexture. After that, the texture may only be used by this
VideoDriver. As you can imagine, textures of the DirectX and the OpenGL device
will, e.g., not be compatible. An exception is the Software device and the
NULL device, their textures are compatible. If you try to use a texture
created by one device with an other device, the device will refuse to do that
and write a warning or an error message to the output buffer.
*/
而一个ITexture基本的就是对详细VideoDriver的Texture格式进行了封装操作而已。
结束语
做技术的人,要耐得住寂寞。深入到技术源头中去学习先人们伟大的智慧结晶!!
!
Irrlicht 3D Engine 笔记系列 之 教程6- 2D Graphics的更多相关文章
- Irrlicht 3D Engine 笔记系列之 教程4 - Movement
作者: i_dovelemon 日期: 2014 / 12 / 16 来源: CSDN 主题: Event Receiver, Animator, Framerate independent move ...
- Irrlicht 3D Engine 笔记系列 之 教程5- User Interface
作者:i_dovelemon 日期:2014 / 12 / 18 来源:CSDN 主题:GUI 引言 今天.博主学习了第五个教程. 这个教程解说了怎样使用Irrlicht内置的一个基础模块.GUI模块 ...
- Irrlicht 3D Engine 笔记系列 之 自己定义Animator
作者: i_dovelemon 日期: 2014 / 12 / 17 来源: CSDN 主题: Custom Animator, Referenced count 引言 在昨天的文章<Irrli ...
- 《zw版·Halcon-delphi系列原创教程》 2d照片-3d逆向建模脚本
<zw版·Halcon-delphi系列原创教程> 2d照片-3d逆向建模脚本 3D逆向建模,是逆向工程的核心要素. 3D逆向建模,除了目前通用的3D点云模式,通过2D图像实现 ...
- Unreal Engine 4 系列教程 Part 1:入门
原文:Unreal Engine 4 Tutorial for Beginners: Getting Started 作者:Tommy Tran 译者:Shuchang Liu 本篇教程将引导你安装U ...
- Unreal Engine 4 系列教程 Part 2:蓝图教程
.katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...
- Unreal Engine 4 系列教程 Part 9:AI教程
.katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...
- Unreal Engine 4 系列教程 Part 7:音频教程
.katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...
- Unreal Engine 4 系列教程 Part 6:动画教程
.katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...
随机推荐
- Python 绘图与可视化 matplotlib(下)
详细的参考链接:更详细的:https://www.cnblogs.com/zhizhan/p/5615947.html 图像.子图.坐标轴以及记号 Matplotlib中图像的意思是打开的整个画图窗口 ...
- 排序代码(python,c++) 及 基本算法复杂度
0.导语 本节为手撕代码系列之第一弹,主要来手撕排序算法,主要包括以下几大排序算法: 直接插入排序 冒泡排序 选择排序 快速排序 希尔排序 堆排序 归并排序 1.直接插入排序 [算法思想] 每一步将一 ...
- [luogu] P3202 [HNOI2009]通往城堡之路(贪心)
P3202 [HNOI2009]通往城堡之路 题目描述 听说公主被关押在城堡里,彭大侠下定决心:不管一路上有多少坎坷,不管城堡中的看守有多少厉害,不管救了公主之后公主会不会再被抓走,不管公主是否漂亮. ...
- POJ 2154
这题的时间卡的.... 必须用欧拉来优化,而且要加素数表.最重要是,因为最后结果要/n,而数据很大,所以,必须在之前就先/n了,否则会爆数据. #include <iostream> #i ...
- [ES2018] Two ways to write for-await-of
// Asynchronous iteration --> Symbol.asyncIterator async function main() { const syncIterable = [ ...
- 初识Dubbo 系列之5-Dubbo 成熟度
成熟度 功能成熟度 Feature特征 Maturity成熟度 Strength强度 Problem问题 Advise建议 User用户 并发控制 Tested 并发控制 试用 连接控制 Te ...
- ADO.NET (二)—— ADO和ADO .NET对照
ADO.NET (二)-- ADO和ADO .NET对照 我们知道ADO.NET的两大核心组件各自是Data Provider和DataSet.假设说 DataSet是ADO.NET的心 ...
- mysql的查询练习1
1.多表查询
- Baby_Step,Gaint_Step(分析具体解释+模板)
下面是总结自他人博客资料.以及本人自己的学习经验. [Baby_Step,Gaint_Step定义] 高次同余方程. BL == N (mod P) 求解最小的L.因为数据范围非常大,暴力不行 这里用 ...
- sql server2008对字符串日期字段分区
近期对公司产品的日志数据库做了一个数据分区,数据库使用的是sql server 2008,这里给大家提供一个參考. 须要特别说明的是,非常多网上的样例分区字段都使用的是时间类型的.而这里因为时间字段原 ...