Project 3: Grass Sim

Overview

One of the main problems with rendering lots of primitives, is each one requires its own draw call/position definition.
A solution to this is instanced rendering, where you give opengl a set of vertices and tell it to render it X amount of times. Since it computes everything internally from then on the cost of it is near 0.

Instanced rendering does this (pseudo code):
for 0, X do
Set Instance ID
DrawArrays/DrawElements.etc

In the form of this:
GL.DrawArraysInstanced(PrimitiveType.Triangles, 0, 3, X);

So now the fun part involves the shaders because each instance cannot be given a unique uniform value (which is what I do normally).
Each instance has an ID from 0->X, utilising this in interesting ways allows you to produce cool effects for practically free.

Here's what I came up with after playing around with it:


Video after adding a time uniform:

video

 

Vertex Shader Details

To distribute the triangles uniformly given a number from 0 -> 10,000.
int x = int( mod( gl_InstanceID, 100 ) );
int z = int( gl_InstanceID / 100 );

To offset the period of each triangle's sin value (not worked out how to make fake-random numbers on shaders yet). Give uT as a value from 0, 2PI. Remember uT is defined once for all the "grass".
float sVal = sin( gl_InstanceID + uT );
oDisplace = vec3( sVal, 0, 0 );

Conclusion

My "grass" is nothing like grass yet, I'll be posting more in the next few days with any progress. I think colouring them is my next objective and then figuring out that tricky random displacement problem.

Here's a download link if you want to give it a spin: LINK