OO体系结构,用于在基于着色器的游戏中渲染 [英] OO architecture for rendering in shader based games

查看:114
本文介绍了OO体系结构,用于在基于着色器的游戏中渲染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 接口实体{$ b}在构建游戏引擎时,我一直在碰到这个问题。 $ b draw(); 


class World {
draw(){
for(e in entities)
e.draw();




$ b

这只是伪代码来粗略显示图形发生。每个实体子类都实现其自己的图形。这个世界循环遍历所有的实体,没有特定的顺序,并告诉它们一个接一个地绘制自己。



但是,使用基于shader的图形,这往往是非常低效的,甚至不可行。每个实体类型可能都会有自己的着色器程序。为了最大限度地减少程序变更,每个特定类型的所有实体都需要一起绘制。简单类型的实体(如粒子)也可能希望以其他方式聚合其绘图,例如共享一个大顶点数组。而且它会因混合而变得很毛茸茸,并且这种情况下,某些实体类型需要在某些时候相对于其他实体进行渲染,或者甚至需要多次进行不同的传递。



通常最终是每个实体类的某种渲染器单例,它保存所有实例的列表并一次绘制它们。这并不坏,因为它将绘图与游戏逻辑分开。但渲染器需要找出要绘制的实体的哪个子集,并且它需要访问图形管道的多个不同部分。这是我的对象模型容易混乱的地方,有很多重复代码,紧密耦合和其他不好的事情。

所以我的问题是:什么是好的架构对于这种高效,多功能和模块化的游戏绘图?解析方案

使用两阶段方法:首先循环遍历所有实体,而不是绘图,让他们将自己的引用插入到(图)批处理列表中。然后通过OpenGL状态和着色器使用对列表进行排序;在每次状态转换之后对插入状态转换器对象进行排序。



最后遍历执行列表中引用的每个对象的绘图例程的列表。


I keep hitting this problem when building game engines where my classes want to look like this:

interface Entity {
  draw();
}

class World {
  draw() {
    for (e in entities)
      e.draw();
  }
}

That's just pseudo-code to show roughly how the drawing happens. Each entity subclass implements its own drawing. The world loops through all the entities in no particular order and tells them to draw themselves one by one.

But with shader based graphics, this tends to be horribly inefficient or even infeasible. Each entity type is probably going to have its own shader program. To minimize program changes, all entities of each particular type need to be drawn together. Simple types of entities, like particles, may also want to aggregate their drawing in other ways, like sharing one big vertex array. And it gets really hairy with blending and such where some entity types need to be rendered at certain times relative to others, or even at multiple times for different passes.

What I normally end up with is some sort of renderer singleton for each entity class that keeps a list of all instances and draws them all at once. That's not so bad since it separates the drawing from the game logic. But the renderer needs to figure out which subset of entities to draw and it needs access to multiple different parts of the graphics pipeline. This is where my object model tends to get messy, with lots of duplicate code, tight coupling, and other bad things.

So my question is: what is a good architecture for this kind of game drawing that is efficient, versatile, and modular?

解决方案

Use a two stages approach: First loop through all entities, but instead of drawing let them insert references to themself into a (the) drawing batch list. Then sort the list by OpenGL state and shader use; after sorting insert state changer objects at every state transistion.

Finally iterate through the list executing the drawing routine of each object referenced in the list.

这篇关于OO体系结构,用于在基于着色器的游戏中渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆