17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-10-18 21:59:18 +02:00
Files
maszyna/shaders/terrainmesh.geom
maj00r 9408216ae7 Give the terrain its own archive, its normals, and a level per tile
The tiles moved out of the scenery's pak into an archive of their own: a
sorted index of fixed-size entries found by where a tile sits in the
quadtree rather than by a name, each entry compressed on its own so a
tile can be read without touching the rest. A name apiece would have been
seventy megabytes of index to read before the first tile could be drawn.

An entry carries what its tile's ground asks for - how far it departs
from the finest mesh, and how long its triangles are - because which
level to draw has to be settled for tiles that are not on the card yet,
and the index is the only thing about them that is read up front. Until
now the figure came from the tile's level, which is the worst tile in it,
so one steep corner spoke for every tile that shared its level.

The mesh carries a normal per vertex, octahedron-encoded into two bytes,
in place of the face normal the fragment stage worked out from how the
view position changed across a triangle. A face normal makes the ground
read as facets, and a coarser level's facets face differently from the
ones they stand for, so the light over a hillside changed as the level
did. A geometry stage hands the fragment stage the three materials of the
triangle it is in and the barycentric weights, so ground changes over a
triangle rather than along its edge; 3.3 has no gl_BaryCoordEXT.

And the level a tile is drawn at now counts the camera's height. The
distance was measured on the ground plane alone, which puts the tile
underfoot nought metres away from a camera a kilometre above it - and
nought metres asks for the finest level there is, so a view from the air
wanted the whole ground at full detail, more tiles than the buffers hold,
and what would not fit came out as holes.

Archives written under the old rules are refused at the door and baked
again.
2026-09-20 21:22:03 +02:00

43 lines
1.4 KiB
GLSL

// Terrain mesh, geometry stage.
//
// It is here only to tell the fragment stage which three materials the triangle it is in has at its
// corners, and how near that fragment is to each of them. A vertex carries one material; a triangle
// crossing a boundary between two kinds of ground has different ones at its corners, and what makes
// that read as a transition rather than as a line along the triangle's edge is blending the three by
// their barycentric weight.
//
// Nothing else needs a whole stage: the weights cannot be worked out in the fragment stage on its own,
// since an indexed mesh shares a vertex between triangles and a vertex cannot say which corner of which
// triangle it is. Desktop GL 4.6 has gl_BaryCoordEXT for exactly this; 3.3 does not.
#include <common>
layout(triangles) in;
layout(triangle_strip, max_vertices = 3) out;
in vec4 v_pos[];
in vec3 v_normal[];
in vec2 v_ground[];
in uint v_material[];
out vec4 f_pos;
out vec3 f_normal;
out vec2 f_ground;
out vec3 f_weights;
flat out uvec3 f_materials;
void main()
{
uvec3 materials = uvec3( v_material[ 0 ], v_material[ 1 ], v_material[ 2 ] );
for( int corner = 0; corner < 3; ++corner ) {
f_pos = v_pos[ corner ];
f_normal = v_normal[ corner ];
f_ground = v_ground[ corner ];
f_weights = vec3( corner == 0 ? 1.0 : 0.0, corner == 1 ? 1.0 : 0.0, corner == 2 ? 1.0 : 0.0 );
f_materials = materials;
gl_Position = gl_in[ corner ].gl_Position;
EmitVertex();
}
EndPrimitive();
}