我可以将矩形表示为x y w h rgb吗?

问题描述 投票:0回答:2

我正在学习OpenGL,并且一直在尝试为GUI创建小部件。将单色框渲染为(pos, color)[4]对我来说有点浪费。有没有办法将其压缩为(x, y, w, h, color)

我不会画足够多的矩形,这无关紧要,但我很好奇。

注意:为方便起见,我用元组表示每个顶点/对象。

user-interface opengl graphics glsl
2个回答
0
投票

嗯,您可以以某种方式将代码抽象到使用这些参数的某个类中,就像SDL2一样。没有OpenGL类可以帮助您解决这些问题。类似于:

struct Vertex {
    glm::vec3 position;
    glm::vec3 color;
};
class Model {
private:
    std::vector<Vertex> mVertices;
public:
    Model(/* Pass Vertices or Load Model through OBJ */);
};

这是我使用的,很抱歉,没有传递顶点,而是传递unsigned int宽度,unsigned int高度,unsigned int xpos,unsigned int ypos并将它们传递给转换矩阵。希望能有所帮助!


0
投票

好,所以在尝试了Spektre给出的提示之后,我想我有一个解决方案。

我已将形状表示为:

struct Shape {
  enum Kind : byte {
    LineRect,
    FillRect,
    ...
  }

  float2 position, extent;

  uint color;

  Kind kind;
}

顶点着色器采用该结构,并将整个对象与投影矩阵一起传递到几何着色器以生成顶点。几何着色器打开kind并发出形状的顶点,将每个顶点乘以投影矩阵。

此格式需要21个字节(float +float+float+float+uint+ byte)来表示矩形,圆形,等腰三角形以及可能的椭圆形。我有理由相信纹理化的矩形也是可行的,其中color分为uv坐标的ushort[2]

© www.soinside.com 2019 - 2024. All rights reserved.