3
\$\begingroup\$

TL;DR I need to switch from a vertex array passed to the pixel shader with a real vertex shader approach.


In my prototype, the terrain is procedurally generated and I have the following result:

Early prototype

The terrain vertices are passed directly to the pixel shader through an array:

//Number of vertices in terrain array
static const int verticesCount = 20;

//The vertices array, in pixel coordinates
float2 terrain[verticesCount];

//A fixed distance between neighbor vertices
float fixedDistance;

float4 PixelShaderFunction(float2 pixelCoords : VPOS,
                           float2 textureCoords :TEXCOORD0) : COLOR0
{
    float4 color = tex2D(s0, textureCoords);
    int i = pixelCoords.x / fixedDistance;
    float delta = (pixelCoords.x - terrain[i].x)/(terrain[i+1].x - terrain[i].x);
    if(smoothInterpolate(terrain[i].y, terrain[i+1].y, delta) >= pixelCoords.y)
    {
        color.rgba = 0;
    }
    return color;
}

As you can see I just interpolate between the vertices and mask the texture to the terrain.

Now I need a different approach because I'm using a view matrix for the camera and it does not affect these vertices that do not go through the vertex shader, how can I replace the terrain array with a vertex shader to get a similar result?

All suggestions are welcome!

\$\endgroup\$
2
  • \$\begingroup\$ Have you considered using the vertices to render geometry? \$\endgroup\$ Mar 10, 2013 at 10:09
  • \$\begingroup\$ That's exactly what I'm considering in order to ask this question, I just don't know how yet. \$\endgroup\$
    – V.M.
    Mar 10, 2013 at 23:02

1 Answer 1

1
\$\begingroup\$

Rendering it as a quad (two triangles) and passing the heights as an 1D texture, like a heithmap texture, can do the trick.

\$\endgroup\$
3
  • \$\begingroup\$ Sorry for the late response but I'm using XNA's Spritebatch to draw the terrain and it already uses a quad, I liked the idea of passing the vertices through a real texture and I'll probably use it. But the question is really about what to do with these values, right now I'm trying to fix it translating the vertices to UV addresses, do you have any other suggestion? \$\endgroup\$
    – V.M.
    Mar 10, 2013 at 23:26
  • \$\begingroup\$ if your level width is 1000 you only have to divide vertex coordinate x by 1000, and you will get values in [0..1] range, like you need to sample the 1D texture... ;) \$\endgroup\$
    – Blau
    Mar 10, 2013 at 23:44
  • \$\begingroup\$ That's the exact approach I'm implementing, thank you! :D \$\endgroup\$
    – V.M.
    Mar 10, 2013 at 23:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .