Skip to content

图像管理系统(NGraphics)

图像管理系统允许您通过NGraphics接口类调用多种绘制指令,以此来渲染真实拟真的画面。

  • 静态函数:
接口描述说明
DrawLine绘制线
DrawBox绘制盒子
ActiveCurrentGLContext激活当前的GL上下文
InvalidateRenderState重置所有渲染状态
BackGraphicState备份当前渲染状态
RestoreGraphicState还原备份的渲染状态
Blit将纹理中的像素渲染到渲染纹理
GetSceneRenderTexture获取场景渲染纹理
CreateThumbSaveToFile创建Actor的缩略图并保存图片
GetGraphicAPIVersion获取当前的图形API版本

图形绘制接口

DrawLine

  • 接口
cpp
static void DrawLine(const Vector3& begin, const Vector3& end, const Color& color = Color::White)
  • 参数
参数说明
begin线的起点
end线的终点
color线的颜色
  • 描述

绘制一条线。该接口将使用内置的着色器绘制一条自定义起点、终点和颜色的线。

当前帧绘制结束后,线的网格数据会被清除,如果需要多帧显示线,需要一直调用该函数。

cpp
void ExampleClass::Update()
{
	NGraphics::DrawLine(Vector3f(0, 0, 0), Vector3f(0, 0, 1), Color::White);
}

DrawBox

  • 接口
cpp
static void DrawBox(const AABB& box, const Color& color = Color::White);
static void DrawBox(const OBB& box, const Color& color = Color::White);
  • 参数
参数说明
box盒子的AABB或者OBB
color盒子边框线的颜色
  • 描述

绘制一条包围盒(边框线)。该接口将使用内置的着色器绘制一个自定义包围盒、颜色的盒子。

当前帧绘制结束后,盒子的网格数据会被清除,如果需要多帧显示盒子,需要一直调用该函数。

cpp
void ExampleClass::Update()
{
	AABB box(Vector3f(0, 0, 0), Vector3f(1, 1, 1));
	NGraphics::DrawLine(box, Color::White);
}

渲染状态管理接口

ActiveCurrentGLContext

  • 接口
cpp
static void ActiveCurrentGLContext();
  • 描述

激活当前的渲染上下文。

cpp
void ExampleClass::Update()
{
	NGraphics:: ActiveCurrentGLContext();
}

InvalidateRenderState

  • 接口
cpp
static void InvalidateRenderState();
  • 描述

重置当前的渲染状态并和GPU同步状态机。

cpp
void ExampleClass::Update()
{
	NGraphics:: InvalidateRenderState();
}

BackGraphicState

  • 接口
cpp
static void BackGraphicState();
  • 描述

备份当前的渲染状态。

通常嵌入GL代码时使用,以免打破状态机。

cpp
void ExampleClass::Update()
{
	NGraphics:: BackGraphicState();
}

RestoreGraphicState

  • 接口
cpp
static void RestoreGraphicState();
  • 描述

还原当前的渲染状态。

通常嵌入GL代码时使用,以免打破状态机。

cpp
void ExampleClass::Update()
{
	NGraphics:: RestoreGraphicState();
}

Blit接口

  • 接口
cpp
static void Blit(NRenderTexturePtr source, NRenderTexturePtr target);
static void Blit(NRenderTexturePtr source, NRenderTexturePtr target, float x, float y, float w, float h);
static void Blit(NRenderTexturePtr source, NRenderTexturePtr target, float x, float y, float w, float h, Vector2 texTiling, Vector2 offset);
static void Blit(NRenderTexturePtr source, NRenderTexturePtr target, NMaterialPtr render);
static void Blit(NRenderTexturePtr source, NRenderTexturePtr target, NMaterialPtr render, float x, float y, float w, float h);
static void Blit(NRenderTexturePtr source, NRenderTexturePtr target, NMaterialPtr render, float x, float y, float w, float h, Vector2 texTiling, Vector2 offset);
  • 参数
参数说明
source源纹理
target目标渲染纹理
x、y、w、h目标渲染纹理的(x, y)到(x + width, y + height)的区域
offset源纹理的偏移
texTiling源纹理的缩放
render渲染材质
  • 描述

使用材质(可选)将纹理中的像素渲染到渲染纹理。 此方法属于在GPU上实现纹理像素的拷贝。

当使用NGraphics::Blit函数时,引擎将target设置为当前激活的渲染目标,根据x、y、w、h设置当前的视口大小,将源纹理作为mainTexture传递给材质render,将offset和texTiling传递给着色器以变化纹理采样坐标。

您可以在后处理中,通过NGraphics::Blit函数实现自定义的后处理效果。 需要避免将source和target设置成相同的渲染纹理,以免出现未定义的渲染结果。

cpp
NRenderTexturePtr src;
NRenderTexturePtr dest;
void ExampleClass::Update()
{
	NGraphics::Blit(src, dest);
}

场景画面接口

  • 接口
cpp
static NRenderTexturePtr GetSceneRenderTexture();
  • 描述

获取当前的场景画面。

目前仅支持在后处理阶段获取场景画面,其他生命周期内将得到未知结果。

cpp
void ExamplePostFxPass::Execute(NRenderTexturePtr src, NRenderTexturePtr dest)
{
	NRenderTexturePtr sceneRT = NGraphics::GetSceneRenderTexture();
	NGraphics::Blit(sceneRT, dest);
}

创建预览缩略图接口

  • 接口
cpp
static void CreateThumbSaveToFile(const NActorPtr& actor, const std::string absFilePath, std::function<void(bool)> callback = nullptr, ExportImageFormat format = ExportImageFormat::Image_PNG, bool flip = false);
  • 参数
参数说明
actor需要拍摄缩略图的对象
absFilePath图像保存路径
callback将渲染结果作为参数执行回调
format保存的图像文件类型
flip是否翻转图像
  • 描述

拍摄指定对象的缩略图并保存。

该接口将拍摄物体的图像渲染到缩略图上,并在渲染结束后,根据回调和是否翻转处理图像,最后根据存储图像类型保存图片。

cpp
void ExampleClass::Update()
{
	NActorPtr actor = GetNActor();
	NGraphics::CreateThumbSaveToFile(actor, “D:/Test/Test.png”, nullptr, ExportImageFormat::Image_PNG, false);
}

获取图形API版本接口

  • 接口
cpp
static std::string GetGraphicAPIVersion();
  • 描述

获取当前的图形API版本。

cpp
void ExampleClass::Update()
{
	LogEngine(“%s”, NGraphics::GetGraphicAPIVersion().c_str());
}