A Mesh Generator

Hello Guys, i have posted in this forum about CustomGeometry component, i have some headcache to export Mesh generated,
So i think it best Create my own mesh Generator, i will Share with community.
the i did not put texcoord parameters, if you place I’ll thank


#pragma once
#include <Urho3D\Urho3DAll.h>
/// Created by Ruben Pontes
/// If you like this code, please place my credits
/// i'm stay happy if you share my work
namespace REngine {
	namespace Tools {
		struct MeshFace {
			Vector3 v0;
			Vector3 v1;
			Vector3 v2;
			Vector3 normal;
			Vector2 texcoord;
		typedef unsigned short ushort;
		typedef unsigned int uint;
		class Mesh : public Object {
			URHO3D_OBJECT(Mesh, Object);
			Mesh(Context* context);
			void AddFace(Vector3 v0, Vector3 v1, Vector3 v2);
			void AddFace(Vector3 v0, Vector3 v1, Vector3 v2,Vector3 normal);
			void AddFace(MeshFace face);
			void SetFace(uint i, MeshFace face);
			MeshFace GetFace(uint i);
			void Commit();
			void CalculateNormals(bool check);
			void Clear();
			void Save(String path);
			Geometry* GetGeometry();
			Model* GetModel();
			VertexBuffer* GetVertexBuffer();
			IndexBuffer* GetIndexBuffer();
			bool _calcNormals;
			Vector<MeshFace> _faces;
			SharedPtr<Geometry> _geometry;
			SharedPtr<Model> _model;
			SharedPtr<VertexBuffer> _vertexBuffer;
			SharedPtr<IndexBuffer> _indexBuffer;
			Vector<float> _vertexData;
			Vector<ushort> _indexData;


#include "Mesh.h"
namespace REngine {
	namespace Tools {
		Mesh::Mesh(Context * context) : Object(context)

		void Mesh::AddFace(Vector3 v0, Vector3 v1, Vector3 v2)
			MeshFace face;
			face.v0 = v0;
			face.v1 = v1;
			face.v2 = v2;
		void Mesh::AddFace(Vector3 v0, Vector3 v1, Vector3 v2, Vector3 normal)
			MeshFace face;
			face.v0 = v0;
			face.v1 = v1;
			face.v2 = v2;
			face.normal = normal;
		void Mesh::AddFace(MeshFace face)
		void Mesh::SetFace(uint i, MeshFace face)
			assert(i >= _faces.Size());
			_faces[i] = face;
		MeshFace Mesh::GetFace(uint i)
			return _faces[i];
		void Mesh::Commit()
			BoundingBox box;
			uint lastFace = 0;
			for (uint i = 0; i < _faces.Size(); i++) {
				MeshFace face = _faces[i];
				//Calculate BoundingBox
				//Assign Vertices





				_indexData.Push(lastFace + 1);
				_indexData.Push(lastFace + 2);
				lastFace += 3;
			// Calculate face normals now
			for (unsigned i = 0; i < _faces.Size() * 2; i += 3)
				Vector3& v1 = *(reinterpret_cast<Vector3*>(&_vertexData[6 * i]));
				Vector3& v2 = *(reinterpret_cast<Vector3*>(&_vertexData[6 * (i + 1)]));
				Vector3& v3 = *(reinterpret_cast<Vector3*>(&_vertexData[6 * (i + 2)]));
				Vector3& n1 = *(reinterpret_cast<Vector3*>(&_vertexData[6 * i + 3]));
				Vector3& n2 = *(reinterpret_cast<Vector3*>(&_vertexData[6 * (i + 1) + 3]));
				Vector3& n3 = *(reinterpret_cast<Vector3*>(&_vertexData[6 * (i + 2) + 3]));

				Vector3 edge1 = v1 - v2;
				Vector3 edge2 = v1 - v3;
				n1 = n2 = n3 = edge1.CrossProduct(edge2).Normalized();
			if(_model == NULL)
			_model = new Model(context_);
			if (_vertexBuffer == NULL)
			_vertexBuffer = new VertexBuffer(context_);
			if (_indexBuffer == NULL)
			_indexBuffer = new IndexBuffer(context_);
			if (_geometry == NULL)
			_geometry = new Geometry(context_);

			PODVector<VertexElement> elements;
			elements.Push(VertexElement(TYPE_VECTOR3, SEM_POSITION));
			elements.Push(VertexElement(TYPE_VECTOR3, SEM_NORMAL));

			_vertexBuffer->SetSize(_faces.Size() * 3, elements);

			_indexBuffer->SetSize(_faces.Size() * 3, false);

			_geometry->SetVertexBuffer(0, _vertexBuffer);
			_geometry->SetDrawRange(TRIANGLE_LIST, 0, _faces.Size()*3);

			_model->SetGeometry(0, 0, _geometry);
			Vector<SharedPtr<VertexBuffer> > vertexBuffers;
			Vector<SharedPtr<IndexBuffer> > indexBuffers;
			PODVector<unsigned> morphRangeStarts;
			PODVector<unsigned> morphRangeCounts;
			_model->SetVertexBuffers(vertexBuffers, morphRangeStarts, morphRangeCounts);
		void Mesh::CalculateNormals(bool check)
			_calcNormals = check;
		void Mesh::Clear()
		void Mesh::Save(String path)
			if (_model == NULL)
				URHO3D_LOGERROR("REngine::Mesh Mesh is not Build");
			File file(context_, path, FILE_WRITE);
			if (file.IsOpen()) {
		Geometry * Mesh::GetGeometry()
			return _geometry;
		Model * Mesh::GetModel()
			return _model;
		VertexBuffer * Mesh::GetVertexBuffer()
			return _vertexBuffer;
		IndexBuffer * Mesh::GetIndexBuffer()
			return _indexBuffer;
[b]How to use[/b]
Mesh mesh = new Mesh(context_);
mesh->Commit() // Use this for build geometry
mesh->Save("c://MeshName") // If you save mesh
StaticMesh* staticMesh = new StaticMesh(context_);
staticMesh->SetModel(mesh->GetModel()); // For Display

Some Screenshots using the Code
This is my tool for generate Roads



cool !

How’s the performance of it? Thanks for the contribution. This should be available in the engine itself.

I coudn’t test the performance, but i think is nearly value of CustomGeometry because i used CustomGeometry with reference

I stumbled upon this thread not to long ago and decided to update the code so I could utilize it in my project. This update includes calculating normals, tangents, and uvs (not perfect). Hopefully someone else can continue to improve on the code. Thanks for your contribution @rbnpontes!





Just a note, I did this update really quickly… so there are definitely some inefficiencies in it which should be resolved. I’m sure some of those are easy to spot as you traverse through the code. Although, for the most part it does work pretty well.

1 Like