Skip to content

Commit

Permalink
Gloom: Vertex flags control texture coordinate generation
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Sep 1, 2019
1 parent 3f26cff commit 764ad78
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 20 deletions.
18 changes: 13 additions & 5 deletions doomsday/tests/test_gloom/gloom/render/mapbuild.cpp
Expand Up @@ -24,14 +24,15 @@ using namespace de;

namespace gloom {

internal::AttribSpec const MapVertex::_spec[4] =
internal::AttribSpec const MapVertex::_spec[5] =
{
{ internal::AttribSpec::Position, 3, GL_FLOAT, false, sizeof(MapVertex), 0 },
{ internal::AttribSpec::Normal, 3, GL_FLOAT, false, sizeof(MapVertex), 3 * 4 },
{ internal::AttribSpec::TexCoord0, 2, GL_FLOAT, false, sizeof(MapVertex), 6 * 4 },
{ internal::AttribSpec::Texture, 1, GL_UNSIGNED_INT, false, sizeof(MapVertex), 8 * 4 }
{ internal::AttribSpec::Texture, 1, GL_UNSIGNED_INT, false, sizeof(MapVertex), 8 * 4 },
{ internal::AttribSpec::Flags, 1, GL_FLOAT, false, sizeof(MapVertex), 9 * 4 },
};
LIBGUI_VERTEX_FORMAT_SPEC(MapVertex, 9 * 4)
LIBGUI_VERTEX_FORMAT_SPEC(MapVertex, 10 * 4)

DENG2_PIMPL_NOREF(MapBuild)
{
Expand Down Expand Up @@ -120,10 +121,12 @@ DENG2_PIMPL_NOREF(MapBuild)
QHash<ID, Buffer::Index> pointIndices;

f.texture = textures["world.grass"];
f.normal = Vector3f(0, 1, 0);
f.normal = Vector3f(0, 1, 0);
f.flags = MapVertex::WorldSpaceXZToTexCoords;

c.texture = textures["world.stone"];
c.normal = Vector3f(0, -1, 0);
c.normal = Vector3f(0, -1, 0);
c.flags = MapVertex::WorldSpaceXZToTexCoords;

for (const ID pointID : planeVerts[0].keys())
{
Expand Down Expand Up @@ -182,6 +185,7 @@ DENG2_PIMPL_NOREF(MapBuild)

v.texture = textures["world.dirt"];
v.normal = normal;
v.flags = 0;

v.pos = planeVerts[0][start];
v.texCoord = Vector2f(0, 0);
Expand All @@ -205,6 +209,10 @@ DENG2_PIMPL_NOREF(MapBuild)
buf->setVertices(verts, gl::Static);
buf->setIndices(gl::Triangles, indices, gl::Static);

DENG2_ASSERT(indices.size() % 3 == 0);

qDebug() << "Built" << verts.size() << "vertices and" << indices.size() << "indices";

return buf;
}
};
Expand Down
7 changes: 6 additions & 1 deletion doomsday/tests/test_gloom/gloom/render/mapbuild.h
Expand Up @@ -36,8 +36,13 @@ struct MapVertex
de::Vector3f normal;
de::Vector2f texCoord;
uint32_t texture;
uint32_t flags;

LIBGUI_DECLARE_VERTEX_FORMAT(4)
LIBGUI_DECLARE_VERTEX_FORMAT(5)

enum Flag {
WorldSpaceXZToTexCoords = 0x1,
};
};

class MapBuild
Expand Down
@@ -0,0 +1,8 @@
#ifndef GLOOM_FLAGS_H
#define GLOOM_FLAGS_H

const uint Surface_WorldSpaceXZToTexCoords = 1u;

#define testFlag(flags, f) (((flags) & (f)) != 0u)

#endif // GLOOM_FLAGS_H
Expand Up @@ -6,20 +6,29 @@ uniform sampler2D uTex;
uniform sampler2D uTextureMetrics;
uniform float uTexelsPerMeter;

DENG_VAR vec2 vUV;
DENG_VAR vec3 vNormal;
flat DENG_VAR uint vTexture;
DENG_VAR vec2 vUV;
DENG_VAR vec3 vNormal;
flat DENG_VAR float vTexture;
flat DENG_VAR uint vFlags;

void main(void) {
vec4 uvRect = texelFetch(uTextureMetrics, ivec2(0, vTexture), 0);
vec4 texelSize = texelFetch(uTextureMetrics, ivec2(1, vTexture), 0);
//out_FragColor = vec4((vNormal + vec3(1.0)/2.0), 1.0); return;

uint texIndex = uint(vTexture + 0.5);

vec4 uvRect = texelFetch(uTextureMetrics, ivec2(0, texIndex), 0);
vec4 texelSize = texelFetch(uTextureMetrics, ivec2(1, texIndex), 0);
float texScale = uTexelsPerMeter / texelSize.x;

vec2 normUV = vUV * texScale;
vec2 uv = uvRect.xy + fract(normUV) * uvRect.zw;

vec4 color = textureLod(uTex, uv, mipLevel(normUV, texelSize.xy) - 0.5);
if (color.a < 0.005) discard;

if (color.a < 0.005)
{
out_FragColor = vec4(1.0, 0.0, 1.0, 1.0);
return;
}

out_FragColor = color;
}
@@ -1,17 +1,26 @@
#include 'flags.glsl'

uniform mat4 uMvpMatrix;

DENG_ATTRIB vec4 aVertex;
DENG_ATTRIB vec2 aUV;
DENG_ATTRIB vec3 aNormal;
DENG_ATTRIB uint aTexture;
DENG_ATTRIB vec4 aVertex;
DENG_ATTRIB vec2 aUV;
DENG_ATTRIB vec3 aNormal;
DENG_ATTRIB float aTexture;
DENG_ATTRIB float aFlags;

DENG_VAR vec2 vUV;
DENG_VAR vec3 vNormal;
flat DENG_VAR uint vTexture;
DENG_VAR vec2 vUV;
DENG_VAR vec3 vNormal;
flat DENG_VAR float vTexture;
flat DENG_VAR uint vFlags;

void main(void) {
gl_Position = uMvpMatrix * aVertex;
vUV = aUV;
vNormal = aNormal;
vTexture = aTexture;
vFlags = floatBitsToUint(aFlags);

if (testFlag(vFlags, Surface_WorldSpaceXZToTexCoords)) {
vUV += aVertex.xz;
}
}

0 comments on commit 764ad78

Please sign in to comment.