Блог
GameDev.ru / Страницы / Блог [Форум / Инфо]

Блог

Сегодня закончил с рендером текста и консоли, начал гуи.
BldCon | Шрифты, консоль, гуи, материалы, шейдеры в Blooody Game Engine
демка: BloodyTest02
common материалы:
default 
{
  color      parm0 parm1 parm2 parm3
  shader     shaders/ubergeneric
  {
  }
}

defaultDeferred 
{
  color      parm0 parm1 parm2 parm3
  sort       deferred
  shader     shaders/ubergeneric
  {
  }
}

defaultFont 
{
  color      parm0 parm1 parm2 parm3
  diffuseMap textures/fonts/chars.tga linear_clamp
  shader     shaders/ubernomip

  {
    blendFunc  blend
  }
}

defaultConsole
{
  color      parm0 parm1 parm2 parm3
  diffuseMap textures/bloody.tga linear_repeat
  shader     shaders/ubernomip


  {
    blendFunc  none
    translate  0.00005 * time, 0.0
  }
  {
    blendFunc  filter
    translate  0.0, 0.00005 * time
  }
  {
    blendFunc  add
    translate  0.00005 * time, 0.00005 * time
  }
  {
    blendFunc  filter
    translate  -0.00005 * time, 0.0
  }
  {
    blendFunc  add
    translate  0.0, -0.00005 * time
  }
  {
    blendFunc  filter
    translate  -0.00005 * time, -0.00005 * time
  }
  {
    blendFunc  add
  }
}

colored2D
{
  color      parm0 parm1 parm2 parm3
  {
    shader     shaders/colored2D
  }
}

colored3D
{
  color      parm0 parm1 parm2 parm3
  {
    shader     shaders/colored3D
  }
}

white2D
{
  {
    shader     shaders/white2D
  }
}

white3D
{
  {
    shader     shaders/white3D
  }
}
Материалы в движке, Quale 3 Style, но без FFP, может быть 3 типа материала:
1) Generic, для отрисовки простой 3D геометрии.
2) Deferred, для отрисовки геометрии в gbuffer который используется для Deferred Shading.
3) NoMip, для отрисовки 2D геометрии, gui, картинок, текста, пост эффектов, etc.
В шейдерах можно использовать таблицы с значениями
table sinTable {{ 0.000000, 0.024541, 0.049068, 0.073565, 0.098017, 0.122411, 0.146730, 0.170962, 0.195090, 0.219101, 0.242980, 0.266713, 0.290285, 0.313682, 0.336890, 0.359895, 0.382683, 0.405241, 0.427555, 0.449611, 0.471397, 0.492898, 0.514103, 0.534998, 0.555570, 0.575808, 0.595699, 0.615232, 0.634393, 0.653173, 0.671559, 0.689541, 0.707107, 0.724247, 0.740951, 0.757209, 0.773010, 0.788346, 0.803208, 0.817585, 0.831470, 0.844854, 0.857729, 0.870087, 0.881921, 0.893224, 0.903989, 0.914210, 0.923880, 0.932993, 0.941544, 0.949528, 0.956940, 0.963776, 0.970031, 0.975702, 0.980785, 0.985278, 0.989177, 0.992480, 0.995185, 0.997290, 0.998795, 0.999699, 1.000000, 0.999699, 0.998795, 0.997290, 0.995185, 0.992480, 0.989177, 0.985278, 0.980785, 0.975702, 0.970031, 0.963776, 0.956940, 0.949528, 0.941544, 0.932993, 0.923880, 0.914210, 0.903989, 0.893224, 0.881921, 0.870087, 0.857729, 0.844854, 0.831470, 0.817585, 0.803208, 0.788346, 0.773010, 0.757209, 0.740951, 0.724247, 0.707107, 0.689541, 0.671559, 0.653173, 0.634393, 0.615232, 0.595699, 0.575808, 0.555570, 0.534998, 0.514103, 0.492898, 0.471397, 0.449611, 0.427555, 0.405241, 0.382683, 0.359895, 0.336890, 0.313682, 0.290285, 0.266713, 0.242980, 0.219101, 0.195090, 0.170962, 0.146730, 0.122411, 0.098017, 0.073565, 0.049068, 0.024541, 0.000000, -0.024541, -0.049068, -0.073565, -0.098017, -0.122411, -0.146730, -0.170962, -0.195090, -0.219101, -0.242980, -0.266713, -0.290285, -0.313682, -0.336890, -0.359895, -0.382683, -0.405241, -0.427555, -0.449611, -0.471397, -0.492898, -0.514103, -0.534998, -0.555570, -0.575808, -0.595699, -0.615232, -0.634393, -0.653173, -0.671559, -0.689541, -0.707107, -0.724247, -0.740951, -0.757209, -0.773010, -0.788346, -0.803208, -0.817585, -0.831470, -0.844854, -0.857729, -0.870087, -0.881921, -0.893224, -0.903989, -0.914210, -0.923880, -0.932993, -0.941544, -0.949528, -0.956940, -0.963776, -0.970031, -0.975702, -0.980785, -0.985278, -0.989177, -0.992480, -0.995185, -0.997290, -0.998795, -0.999699, -1.000000, -0.999699, -0.998795, -0.997290, -0.995185, -0.992480, -0.989177, -0.985278, -0.980785, -0.975702, -0.970031, -0.963776, -0.956940, -0.949528, -0.941544, -0.932993, -0.923880, -0.914210, -0.903989, -0.893224, -0.881921, -0.870087, -0.857729, -0.844854, -0.831470, -0.817585, -0.803208, -0.788346, -0.773010, -0.757209, -0.740951, -0.724247, -0.707107, -0.689541, -0.671559, -0.653173, -0.634393, -0.615232, -0.595699, -0.575808, -0.555570, -0.534998, -0.514103, -0.492898, -0.471397, -0.449611, -0.427555, -0.405241, -0.382683, -0.359895, -0.336890, -0.313682, -0.290285, -0.266713, -0.242980, -0.219101, -0.195090, -0.170962, -0.146730, -0.122411, -0.098017, -0.073565, -0.049068, -0.024541 }}

table cosTable {{ 1.000000, 0.999699, 0.998795, 0.997290, 0.995185, 0.992480, 0.989177, 0.985278, 0.980785, 0.975702, 0.970031, 0.963776, 0.956940, 0.949528, 0.941544, 0.932993, 0.923880, 0.914210, 0.903989, 0.893224, 0.881921, 0.870087, 0.857729, 0.844854, 0.831470, 0.817585, 0.803208, 0.788346, 0.773010, 0.757209, 0.740951, 0.724247, 0.707107, 0.689541, 0.671559, 0.653173, 0.634393, 0.615232, 0.595699, 0.575808, 0.555570, 0.534998, 0.514103, 0.492898, 0.471397, 0.449611, 0.427555, 0.405241, 0.382683, 0.359895, 0.336890, 0.313682, 0.290285, 0.266713, 0.242980, 0.219101, 0.195090, 0.170962, 0.146730, 0.122411, 0.098017, 0.073565, 0.049068, 0.024541, 0.000000, -0.024541, -0.049068, -0.073565, -0.098017, -0.122411, -0.146730, -0.170962, -0.195090, -0.219101, -0.242980, -0.266713, -0.290285, -0.313682, -0.336890, -0.359895, -0.382683, -0.405241, -0.427555, -0.449611, -0.471397, -0.492898, -0.514103, -0.534998, -0.555570, -0.575808, -0.595699, -0.615232, -0.634393, -0.653173, -0.671559, -0.689541, -0.707107, -0.724247, -0.740951, -0.757209, -0.773010, -0.788346, -0.803208, -0.817585, -0.831470, -0.844854, -0.857729, -0.870087, -0.881921, -0.893224, -0.903989, -0.914210, -0.923880, -0.932993, -0.941544, -0.949528, -0.956940, -0.963776, -0.970031, -0.975702, -0.980785, -0.985278, -0.989177, -0.992480, -0.995185, -0.997290, -0.998795, -0.999699, -1.000000, -0.999699, -0.998795, -0.997290, -0.995185, -0.992480, -0.989177, -0.985278, -0.980785, -0.975702, -0.970031, -0.963776, -0.956940, -0.949528, -0.941544, -0.932993, -0.923880, -0.914210, -0.903989, -0.893224, -0.881921, -0.870087, -0.857729, -0.844854, -0.831470, -0.817585, -0.803208, -0.788346, -0.773010, -0.757209, -0.740951, -0.724247, -0.707107, -0.689541, -0.671559, -0.653173, -0.634393, -0.615232, -0.595699, -0.575808, -0.555570, -0.534998, -0.514103, -0.492898, -0.471397, -0.449611, -0.427555, -0.405241, -0.382683, -0.359895, -0.336890, -0.313682, -0.290285, -0.266713, -0.242980, -0.219101, -0.195090, -0.170962, -0.146730, -0.122411, -0.098017, -0.073565, -0.049068, -0.024541, 0.000000, 0.024541, 0.049068, 0.073565, 0.098017, 0.122411, 0.146730, 0.170962, 0.195090, 0.219101, 0.242980, 0.266713, 0.290285, 0.313682, 0.336890, 0.359895, 0.382683, 0.405241, 0.427555, 0.449611, 0.471397, 0.492898, 0.514103, 0.534998, 0.555570, 0.575808, 0.595699, 0.615232, 0.634393, 0.653173, 0.671559, 0.689541, 0.707107, 0.724247, 0.740951, 0.757209, 0.773010, 0.788346, 0.803208, 0.817585, 0.831470, 0.844854, 0.857729, 0.870087, 0.881921, 0.893224, 0.903989, 0.914210, 0.923880, 0.932993, 0.941544, 0.949528, 0.956940, 0.963776, 0.970031, 0.975702, 0.980785, 0.985278, 0.989177, 0.992480, 0.995185, 0.997290, 0.998795, 0.999699 }}

table triangleTable {{ 0, .1, .2, .3, .4, .5, .6, .7, .8, .9, 1, .9, .8, .7, .6, .5, .4, .3, .2, .1, 0, -.1, -.2, -.3, -.4, -.5, -.6, -.7, -.8, -.9, -1, -.9, -.8, -.7, -.6, -.5, -.4, -.3, -.2, -.1 }}

table squareTable { snap { 1, -1 }}

table colorTable {{ 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.0 }}

Пример применения:

    color colorTable[time * 0.0005] colorTable[time * 0.0005] colorTable[time * 0.0005] 1.0

Шейдеры в движке на языке GLSL, в одном шейдере может быть сразу vertex, fragment, geometry шейдеры:

#vertex_shader

layout(location = 0) in vec2 iPosition;

#if defined(DIFFUSE_MAP) || defined(DETAIL_MAP)
layout(location = 1) in vec2 iTexcoord;

out vec2 vTexcoord;

#if defined(TEXTURE_TRANSFORM)
uniform mat4 uTextureMatrix;
#endif
#endif

void main() {
  gl_Position = vec4(iPosition.xy, 0.0, 1.0);

#if defined(DIFFUSE_MAP) || defined(DETAIL_MAP)
#if defined(TEXTURE_TRANSFORM)
  vTexcoord.s = dot(vec4(iTexcoord, 0, 1), uTextureMatrix[0]);
  vTexcoord.t = dot(vec4(iTexcoord, 0, 1), uTextureMatrix[1]);
#else
  vTexcoord = iTexcoord;
#endif
#endif
}

#fragment_shader

layout(location = 0) out vec4 oFragmentColor0;

uniform vec4 uColor;

#if defined(DIFFUSE_MAP) || defined(DETAIL_MAP)
in vec2 vTexcoord;
#endif

#if defined(DIFFUSE_MAP)
uniform sampler2D   uDiffuseMap;
#endif

#if defined(DETAIL_MAP)
uniform sampler2D   uDetailMap;
uniform float       uDetailFactor;
#endif

#if defined(DIFFUSE_MAP) || defined(DETAIL_MAP)
vec4 GetMaterialColor() {
  vec4 result;
#if defined(DIFFUSE_MAP)
  result = texture2D(uDiffuseMap, vTexcoord) * uColor;
#else
  result = uColor;
#endif
#if defined(DETAIL_MAP)
  #if defined(DETAIL_MULTIPLY)
    result *= texture2D(uDetailMap, vTexcoord);
  #elif defined(DETAIL_ADDITIVE)
    result += texture2D(uDetailMap, vTexcoord) * uDetailFactor;
  #elif defined(DETAIL_LERP_SRC_COLOR)
    result = mix(result, texture2D(uDetailMap, vTexcoord), result);
  #elif defined(DETAIL_LERP_DST_COLOR)
    vec4 dstColor = texture2D(uDetailMap, vTexcoord);
    result = mix(result, dstColor, dstColor);
  #elif defined(DETAIL_LERP_SRC_ALPHA)
    result = mix(result, texture2D(uDetailMap, vTexcoord), result.a);
  #elif defined(DETAIL_LERP_DST_ALPHA)
    vec4 dstColor = texture2D(uDetailMap, vTexcoord);
    result = mix(result, dstColor, dstColor.a);
  #elif defined(DETAIL_LERP_FACTOR)
    result = mix(result, texture2D(uDetailMap, vTexcoord), uDetailFactor);
  #endif
#endif
  return result;
}
#endif

void main() {
#if defined(DIFFUSE_MAP) || defined(DETAIL_MAP)
  oFragmentColor0 = GetMaterialColor();
#else
  oFragmentColor0 = uColor;
#endif
}

В файле шейдера можно вставлять код других шейдеров с помощью встроенной команды #import, #import shaders\utils например.

Материалы поддерживаю несколько типов маппинга, на текущий момент это:
diffuseMap, specularMap, specularLevelMap, specularExponentMap, normalMap, detailMap, ambientMap, emissionMap, environmentMap.
Возможно ещё будет heightMap если будет parallax mapping.
Можно и свои текстуры использовать с помощью команды shaderMap, shaderMap myPuperTexture, textures/myPuperTexture.tga, linear_repeat

Комментарии [8]
2 сен. 2011

Решил изучить геометрические шейдеры, ну и заодно добавить их в движок, решил сделать простенькую тесселляцию :)

Subdivision Level 2 - (6 вершин на входе, 2 треугольника на входе), (48 вершин на выходе, 16 треугольников на выходе)

Демка(Необходима поддержка OpenGL 3.3):
BloodyGS2

Картинка:
GSTESS2 | GLSL Geometry Shaders, Простенькая тесселляция :)

Шейдир:

#vertex_shader

layout(location = 0) in vec2 iPosition;
layout(location = 1) in vec2 iTexcoord;

out vec2 vTexcoord;

#if defined(TEXTURE_TRANSFORM)
uniform mat4 uTextureMatrix;
#endif

void main() {
  gl_Position = vec4(iPosition.xy, 0.0, 1.0);
#if defined(TEXTURE_TRANSFORM)
  vTexcoord.s = dot(iTexcoord, uTextureMatrix[0].st);
  vTexcoord.t = dot(iTexcoord, uTextureMatrix[1].st);
#else
  vTexcoord = iTexcoord;
#endif
}

#fragment_shader

layout(location = 0) out vec4 oFragmentColor0;

uniform vec4 uColor;

in vec2 vTexcoord;

void main() {
  oFragmentColor0 = uColor;
}

#geometry_shader


layout(triangles) in;
layout(triangle_strip, max_vertices = 48) out;
in  vec2 iTexcoord[3];
out vec2 vTexcoord;

struct Triangle {
  vec4 position;
  vec2 texcoord;
};

void EmitTriangle( in Triangle v0, in Triangle v1, in Triangle v2 ) {
  gl_Position = v0.position;
  vTexcoord = v0.texcoord;
  EmitVertex();

  gl_Position = v1.position;
  vTexcoord = v1.texcoord;
  EmitVertex();

  gl_Position = v2.position;
  vTexcoord = v2.texcoord;
  EmitVertex();

  EndPrimitive();
}

void SubdivideTriangle2( in Triangle v0, in Triangle v1, in Triangle v2, int level ) {
  Triangle v3;
  v3.position = mix(v0.position, v1.position, 0.5);
  v3.texcoord = mix(v0.texcoord, v1.texcoord, 0.5);

  Triangle v4;
  v4.position = mix(v1.position, v2.position, 0.5);
  v4.texcoord = mix(v1.texcoord, v2.texcoord, 0.5);

  Triangle v5;
  v5.position = mix(v2.position, v0.position, 0.5);
  v5.texcoord = mix(v2.texcoord, v0.texcoord, 0.5);

  EmitTriangle(v0, v3, v5);
  EmitTriangle(v3, v4, v5);
  EmitTriangle(v3, v1, v4);
  EmitTriangle(v5, v4, v2);
}

void SubdivideTriangle( in Triangle v0, in Triangle v1, in Triangle v2, int level ) {
  Triangle v3;
  v3.position = mix(v0.position, v1.position, 0.5);
  v3.texcoord = mix(v0.texcoord, v1.texcoord, 0.5);

  Triangle v4;
  v4.position = mix(v1.position, v2.position, 0.5);
  v4.texcoord = mix(v1.texcoord, v2.texcoord, 0.5);

  Triangle v5;
  v5.position = mix(v2.position, v0.position, 0.5);
  v5.texcoord = mix(v2.texcoord, v0.texcoord, 0.5);

  if( level > 1 ) {
    SubdivideTriangle2(v0, v3, v5, level);
    SubdivideTriangle2(v3, v4, v5, level);
    SubdivideTriangle2(v3, v1, v4, level);
    SubdivideTriangle2(v5, v4, v2, level);
  } else {
    EmitTriangle(v0, v3, v5);
    EmitTriangle(v3, v4, v5);
    EmitTriangle(v3, v1, v4);
    EmitTriangle(v5, v4, v2);
  }
}

void main() {

  Triangle v0;
  v0.position = gl_in[0].gl_Position;
  v0.texcoord = iTexcoord[0];

  Triangle v1;
  v1.position = gl_in[1].gl_Position;
  v1.texcoord = iTexcoord[1];

  Triangle v2;
  v2.position = gl_in[2].gl_Position;
  v2.texcoord = iTexcoord[2];

  int subdivisionLevel = 2;

  if( subdivisionLevel == 0 ) {
    EmitTriangle(v0, v1, v2);
  } else
    SubdivideTriangle(v0, v1, v2, subdivisionLevel);
}

Насколько оно медленно я не мерял, но то что медленно это точно ;)

Комментарии [11]
23 авг. 2011

2001—2017 © GameDev.ru — Разработка игр