Calculation of procedural mesh indices (C++)

Take a look at the figure below:

54780-meshindicesproblem.png

In this example, the plane mesh displays a total of 9 vertices and 8 triangles, composing a total of 4 quads.
So my question here is simply this:
How would you calculate the most optimal index buffer for this?
Keep in mind that the actual number of X and Y vertices may vary, but the overall mesh will still be a series of quads.

Bonus Question:

What is the default flag (triangle fan, triangle strip, etc) that UE4 uses while rendering, and what is the default vertex winding order?

This is a bit old, but since nobody has answered this yet, I’ll just go ahead and add this, since I was trying to figure this out myself and others might benefit from it. :slight_smile:

You would probably be interested in the following function which should do what you ask to:
From KismetProceduralMeshLibrary.h (part of the ProceduralMeshComponent plugin)

/** 
	 *	Generate an index buffer for a grid of quads. 
	 *	@param	NumX			Number of vertices in X direction (must be >= 2)
	 *	@param	NumY			Number of vertices in y direction (must be >= 2)
	 *	@param	bWinding		Reverses winding of indices generated for each quad
	 *	@out	Triangles		Output index buffer
	 */
	UFUNCTION(BlueprintCallable, Category = "Components|ProceduralMesh")
	static void CreateGridMeshTriangles(int32 NumX, int32 NumY, bool bWinding, TArray<int32>& Triangles);

So all you need is to know how many vertices you have in x and y direction on your grid.

You can probably also look at the source code of the function if you want to know how it’s done exactly.
Also, even though this says BluePrintCallable, you can easily access these functions in C++:

First you’ll have to add the ProceduralMeshComponent Module to your Build.cs file.
For Example:

PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "ProceduralMeshComponent" });

Then you can just include the header file as you would normally:

#include "ProceduralMeshComponent.h"
#include "KismetProceduralMeshLibrary.h"

Just beware that this functionality is still marked as experimental.

I am using Procedural Mesh Component in a C++ class and I wonder how to use this CreateGridMeshTriangles method. I’ve added #include "KismetProceduralMeshLibrary.h" to my cpp file, and have added

TArray<int32> Triangles;
UKismetProceduralMeshLibrary::CreateGridMeshTriangles(10, 10, true, Triangles);

to a method, but I am not sure how this is going to produce a mesh… any tips or examples of actual usage would be greatly appreciate, I’m also new to C++, although familiar with other programming languages.