《OGRE 3D 渲染引擎原理与架构》2.2 视口

视口是相机与渲染目标的交汇点:相对矩形(0~1)× 目标像素尺寸 = 实际像素矩形,窗口 resize 时自动重算;低 ZOrder 叠放靠前;背景色在渲染前填充。lab 复现相对/实际尺寸换算、resize 重算与相机宽高比自动同步。

2.2 视口

相机决定「从哪看」,但画面落在目标的哪块区域,由 Viewport 说了算。OGRE 里视口是一个极轻的对象:一头连着相机(Camera*),一头连着渲染目标(RenderTarget*),中间是一个相对尺寸矩形。这一节看它怎么把相对尺寸变成像素、怎么处理目标 resize。

本章与站内既有内容的分工

卷二 11.x 讲交换链与渲染目标本身(RenderTarget 的像素表面)。这一节是目标上的一块「窗口」:一个目标可以挂多个视口(分屏、小地图、画中画),视口只负责在目标表面划出区域并绑定相机。

视口:相机与目标的交汇

OGRE 头文件注释把视口的角色一句话讲透:

// OgreMain/include/OgreViewport.h:Viewport 类注释(v14.6.3 源码摘录)
        A viewport is the meeting of a camera and a rendering surface -
        the camera renders the scene from a viewpoint, and places its
        results into some subset of a rendering target, which may be the
        whole surface or just a part of the surface. Each viewport has a
        single camera as source and a single target as destination. A
        camera only has 1 viewport, but a render target may have several.
        A viewport also has a Z-order, i.e. if there is more than one
        viewport on a single render target and they overlap, one must
        obscure the other in some predetermined way.

注释语义——视口是相机与渲染表面的「会面处」:相机从某个视点渲染场景,把结果放进渲染目标的子集(整块或局部)。一个相机只有一个视口,但一个渲染目标可以有好几个;多个视口重叠时靠 Z-order 决定谁压谁。

「一个目标多个视口」正是分屏与小地图的地基:每个视口各绑一个相机、各占一块区域。

相对尺寸:0 到 1 的坐标

视口的构造函数不接收像素,而是 0~1 的相对坐标——这样目标怎么 resize,视口比例都不变:

// OgreMain/include/OgreViewport.h:Viewport 构造函数(v14.6.3 源码摘录,已删减)
        /** The usual constructor.
            @param camera
                Pointer to a camera to be the source for the image.
            @param target
                Pointer to the render target to be the destination
                for the rendering.
            @param left, top, width, height
                Dimensions of the viewport, expressed as a value between
                0 and 1. This allows the dimensions to apply irrespective of
                changes in the target's size: e.g. to fill the whole area,
                values of 0,0,1,1 are appropriate.
            @param ZOrder
                Relative Z-order on the target. Lower = further to
                the front.
        */
        Viewport(Camera* camera, RenderTarget* target, float left, float top, float width, float height, int ZOrder)
            : Viewport(camera, target, {left, top, left + width, top + height}, ZOrder)
        {
        }

构造时把 (left, top, width, height) 转成 {left, top, left+width, top+height} 的 FloatRect(四值矩形),再交给真正的构造器。ZOrder 注释值得记牢:越低越靠前(绘制次序)。

相对 → 实际像素:_updateDimensions

每次目标尺寸变化,视口重算自己的像素矩形。核心一行:

// OgreMain/src/OgreViewport.cpp:Viewport::_updateDimensions(v14.6.3 源码摘录,已删减)
        mActRect = Rect(mRelRect.left * width, mRelRect.top * height, mRelRect.right * width, mRelRect.bottom * height);

        // This will check if the cameras getAutoAspectRatio() property is set.
        // If it's true its aspect ratio is fit to the current viewport
        // If it's false the camera remains unchanged.
        // This allows cameras to be used to render to many viewports,
        // which can have their own dimensions and aspect ratios.

        if (mCamera) 
        {
            if (mCamera->getAutoAspectRatio())
                mCamera->setAspectRatio((float)mActRect.width() / (float)mActRect.height());
        }

换算公式:实际像素 = 相对值 × 目标像素。mRelRect 的 left/top/right/bottom 各自乘目标宽/高,得到 mActRect 的像素矩形。随后有一个精巧联动:若相机设了 autoAspectRatio,相机宽高比会被同步成视口的像素宽高比——这是「相机跟随窗口形状」的实现点。分屏里两个视口形状不同,就各自覆盖相机的宽高比。

相对尺寸与像素尺寸的读取

视口对外暴露两组读取器,一组相对、一组像素:

// OgreMain/include/OgreViewport.h:相对与实际尺寸(v14.6.3 源码摘录,已删减)
        int getActualLeft(void) const { return mActRect.left; }
        int getActualTop(void) const { return mActRect.top; }
        int getActualWidth(void) const { return mActRect.width(); }
        int getActualHeight(void) const { return mActRect.height(); }
        Rect getActualDimensions() const { return mActRect; }

「相对」版本(getLeft/getTop/getWidth/getHeight)返回 mRelRect 的 0~1 值;「实际」版本(getActualLeft 等)返回像素。创建时相对尺寸已换算好实际尺寸,resize 时经 _updateDimensions 刷新。

背景色与元素

视口还管渲染前的底色:

// OgreMain/include/OgreViewport.h:setBackgroundColour(v14.6.3 源码摘录,已删减)
        void setBackgroundColour(const ColourValue& colour) { mBackColour = colour; }

        /** Gets the background colour.
        */
        const ColourValue& getBackgroundColour(void) const { return mBackColour; }

背景色是渲染器每帧在画场景前先填充的「画布底色」——天空盒/天空平面(第 11 章)就是在此基础上叠的。相机经 getCamera() 取回、目标经 getTarget() 取回、叠放次序经 getZOrder() 取回。

源码定位

  • OgreMain/include/OgreViewport.h :Viewport 类(56-)、构造与 ZOrder(90-96)、getTarget/getCamera/getZOrder(121-131)、相对尺寸(137-140)、setDimensions(157-)、实际像素(166-170)、setBackgroundColour/getBackgroundColour(172-176)
  • OgreMain/src/OgreViewport.cpp :Viewport::_updateDimensions(97-135)

上游引用固定到 OGRE v14.6.3 源码 。

可运行产物:C++ 最小验证

下面复现视口的相对/实际尺寸换算。它与 src/02-2-viewport.cpp 逐字节一致:

// file: src/02-2-viewport.cpp
// tier: L1
// verify: clang++ -std=c++20 -I. src/02-2-viewport.cpp -o bin/02-2-viewport && ./bin/02-2-viewport
//
// 复现 Viewport 视口(对应 OgreViewport.h 46-、OgreViewport.cpp:Viewport::_updateDimensions 97-):
//   Viewport 是相机与渲染目标的交汇:相对矩形(0..1) × 目标像素 = 实际像素矩形;
//   ZOrder 决定视口在目标上的叠放次序(低 = 更靠前);
//   背景色在渲染前填充;setDimensions 改相对尺寸并重算实际尺寸。
// 独立最小验证,非 OGRE 源码。
#include "verify.h"

#include <string>

namespace ogre_repro {

// 对应 RenderTarget:像素宽高
struct RenderTarget {
  int width = 0;
  int height = 0;
};

// 极简相机:名字 + 是否自动适配宽高比
class Camera {
 public:
  explicit Camera(const std::string& name) : mName(name) {}
  const std::string& getName() const { return mName; }
  void setAutoAspectRatio(bool b) { mAutoAspectRatio = b; }
  bool getAutoAspectRatio() const { return mAutoAspectRatio; }
  void setAspectRatio(float r) { mAspectRatio = r; }

 private:
  std::string mName;
  bool mAutoAspectRatio = false;
  float mAspectRatio = 1.3333f;
};

// 对应 FloatRect:left/top/right/bottom
struct FloatRect {
  float left = 0, top = 0, right = 0, bottom = 0;
  float width() const { return right - left; }
  float height() const { return bottom - top; }
};

// 对应 Rect(像素):left/top/width/height
struct Rect {
  float left = 0, top = 0, right = 0, bottom = 0;
  float width() const { return right - left; }
  float height() const { return bottom - top; }
};

// 对应 ColourValue:RGBA
struct ColourValue {
  float r = 0, g = 0, b = 0, a = 1;
};

// 对应 Viewport(OgreViewport.h 56-,extends ViewportAlloc)
class Viewport {
 public:
  Viewport(Camera* camera, RenderTarget* target, float left, float top, float width, float height, int zOrder)
      : mCamera(camera), mTarget(target), mZOrder(zOrder) {
    mRelRect = FloatRect{left, top, left + width, top + height};
    updateDimensions();
  }
  // 对应 _updateDimensions(OgreViewport.cpp 97-):
  //   相对(0..1) × 目标像素 = 实际像素;若相机 autoAspectRatio 则同步相机宽高比
  void updateDimensions() {
    float tw = static_cast<float>(mTarget->width);
    float th = static_cast<float>(mTarget->height);
    mActRect = Rect{mRelRect.left * tw, mRelRect.top * th,
                    mRelRect.right * tw, mRelRect.bottom * th};
    if (mCamera && mCamera->getAutoAspectRatio())
      mCamera->setAspectRatio(mActRect.width() / mActRect.height());
  }
  // 对应 setDimensions(OgreViewport.cpp 122-)
  void setDimensions(float left, float top, float width, float height) {
    mRelRect = FloatRect{left, top, left + width, top + height};
    updateDimensions();
  }

  RenderTarget* getTarget() const { return mTarget; }   // 对应 getTarget(121)
  Camera* getCamera() const { return mCamera; }         // 对应 getCamera(125)
  int getZOrder() const { return mZOrder; }              // 对应 getZOrder(131)
  float getLeft() const { return mRelRect.left; }        // 相对尺寸(137-140)
  float getTop() const { return mRelRect.top; }
  float getWidth() const { return mRelRect.width(); }
  float getHeight() const { return mRelRect.height(); }
  float getActualLeft() const { return mActRect.left; }  // 实际像素(166-170)
  float getActualTop() const { return mActRect.top; }
  float getActualWidth() const { return mActRect.width(); }
  float getActualHeight() const { return mActRect.height(); }

  void setBackgroundColour(const ColourValue& c) { mBackColour = c; }  // 172
  const ColourValue& getBackgroundColour() const { return mBackColour; }

 private:
  Camera* mCamera;
  RenderTarget* mTarget;
  int mZOrder;
  FloatRect mRelRect;
  Rect mActRect;
  ColourValue mBackColour;
};

}  // namespace ogre_repro

int main() {
  using namespace ogre_repro;

  RenderTarget rt;
  rt.width = 1920;
  rt.height = 1080;
  Camera cam("MainCamera");

  // 全屏视口:相对(0,0,1,1) → 像素(0,0,1920,1080)
  Viewport full(&cam, &rt, 0, 0, 1, 1, 0);
  CHECK(full.getTarget() == &rt);
  CHECK(full.getCamera() == &cam);
  CHECK(full.getZOrder() == 0);
  CHECK(full.getActualLeft() == 0 && full.getActualTop() == 0);
  CHECK(full.getActualWidth() == 1920 && full.getActualHeight() == 1080);

  // 半屏左上角:相对(0,0,0.5,0.5) → 像素(0,0,960,540)
  Viewport half(&cam, &rt, 0, 0, 0.5f, 0.5f, 10);
  CHECK(half.getWidth() == 0.5f && half.getHeight() == 0.5f);
  CHECK(half.getActualWidth() == 960 && half.getActualHeight() == 540);
  CHECK(half.getZOrder() == 10);  // 值更大,叠放更靠后

  // setDimensions 改相对尺寸后重算实际像素
  half.setDimensions(0.5f, 0.5f, 0.5f, 0.5f);
  CHECK(half.getLeft() == 0.5f && half.getTop() == 0.5f);
  CHECK(half.getActualLeft() == 960 && half.getActualTop() == 540);
  CHECK(half.getActualWidth() == 960 && half.getActualHeight() == 540);

  // 背景色
  Viewport vp2(&cam, &rt, 0, 0, 1, 1, 0);
  ColourValue sky{0.1f, 0.2f, 0.3f, 1.0f};
  vp2.setBackgroundColour(sky);
  const ColourValue& back = vp2.getBackgroundColour();
  CHECK(back.r == 0.1f && back.g == 0.2f && back.b == 0.3f);

  // autoAspectRatio:视口尺寸变化时相机宽高比自动同步
  cam.setAutoAspectRatio(true);
  Viewport ar(&cam, &rt, 0, 0, 0.5f, 1.0f, 0);  // 相对宽 0.5 → 像素 960/1080
  // 更新目标尺寸(比如窗口 resize)后重算
  rt.width = 1280;
  rt.height = 720;
  ar.updateDimensions();
  CHECK(ar.getActualWidth() == 640 && ar.getActualHeight() == 720);

  return lab::report("02-2 视口");
}

本机实测(Apple clang 16,clang++ -std=c++20):编译通过(-Wall 零警告),断言 13/13 通过。

小结

  • Viewport 是相机与渲染目标的交汇:一个目标可挂多个视口(分屏/小地图)。
  • 视口用 0~1 相对矩形定位,实际像素 = 相对值 × 目标像素,resize 时 _updateDimensions 重算。
  • ZOrder 越低叠放越靠前。
  • autoAspectRatio 让相机宽高比跟随视口像素比例,分屏时各视口可各自适配。
  • 背景色是渲染前填充的画布底色。

阅读导航:上一节:2.1 相机与视锥 · 下一节:2.3 投影与裁剪 。

继续阅读

探索更多技术文章

浏览归档,发现更多关于系统设计、工具链和工程实践的内容。

全部文章 返回首页

「books」更多文章

  1. 《大规模分布式计算深入解析》15.3 落地:从 Neo4j 到自研图引擎
  2. 《大规模分布式计算深入解析·图计算与图数据库内核》 · 目录与学习路径(45 篇)
  3. 《大规模分布式计算深入解析》15.2 十一卷主线:从切分到图