-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[models] Add GenMeshDefault to improve Mesh generation #1556
Comments
AllocateMeshData from https://github.com/JeffM2501/raylibExtras/blob/1c56e6e2fe617642f0529d3fc73d2a33373ec9e5/RLGeoTools_C/RLGeoTools.c#L40 effectively does this already, having it included in the base system would be nice. I would not call It GenMeshDefault because that makes it seem like the generated mesh is valid (like the default font is valid, and the default texture is valid). In this case the mesh is not valid, just allocated, so it may be better to simply name the function AllocateMesh, or just GenMesh, or GenMeshCustom or something like that. |
|
@JeffM2501 Makes sense. I agree that |
Here is my initial implementation for Unsure on the enum name. We could use it with // Generate a mesh using flags to specify attributes
Mesh GenMeshCustom(int vertexCount, int triangleCount, int flags)
{
Mesh mesh = { 0 };
mesh.vertexCount = vertexCount;
mesh.triangleCount = triangleCount;
TraceLog(LOG_INFO, "GenMeshCustom: vertexCount=%d triangleCount=%d", mesh.vertexCount, mesh.triangleCount);
if (flags & MESH_VERTEX)
{
mesh.vertices = (float *)RL_MALLOC(mesh.vertexCount*3*sizeof(float));
}
if (flags & MESH_TEXCOORD)
{
mesh.texcoords = (float *)RL_MALLOC(mesh.vertexCount*2*sizeof(float));
}
if (flags & MESH_TEXCOORD2)
{
mesh.texcoords2 = (float *)RL_MALLOC(mesh.vertexCount*2*sizeof(float));
}
if (flags & MESH_NORMAL)
{
mesh.normals = (float *)RL_MALLOC(mesh.vertexCount*3*sizeof(float));
}
if (flags & MESH_TANGENT)
{
mesh.tangents = (float *)RL_MALLOC(mesh.vertexCount*4*sizeof(float));
}
if (flags & MESH_COLOR)
{
mesh.colors = (unsigned char *)RL_MALLOC(mesh.vertexCount*4*sizeof(float));
}
if (flags & MESH_INDEX)
{
mesh.indices = (unsigned short *)RL_MALLOC(mesh.triangleCount*3*sizeof(float));
}
mesh.vboId = (unsigned int *)RL_CALLOC(DEFAULT_MESH_VERTEX_BUFFERS, sizeof(unsigned int));
return mesh;
} |
I don't think it's valid to generate a mesh without vertices, so having that as a flag is a bit redundant. The same may be true for texture coords as well. I see rlLoadMesh always assumes the vertecies and texcoords buffers exists. The others are checked against null. I'd put the word "GEN" in the enums to show they go with generation, and so they don't conflict with other future terms. |
I can remove checks for required data if that is intended. Though I would keep the flags for updating the mesh. The GEN prefix makes sense if the flags are only used for generation. Maybe |
I'd like to add that keeping the flags and checks would allow one to create a mesh consisting only of an IndexBuffer. |
@chrisdill @JeffM2501 I'm doing a huge redesign of I'm also considering the usefulness of Finally, about void rlUpdateVertexBuffer(int bufferId, void *data, int dataSize, int offset); I think most users requiring updating vertex data on GPU could directly deal with Well, just some thoughts, as commented, I'm doing a big internal redesign (that actually requires exposing a lot of OpenGL functionality in In case you wonder the reason for that big redesign, it would be useful for a future |
The goal of Creating a I think |
I think (off the top of my head) I might be relying on triCount in models.c to split OBJ's into material meshes, this needs careful review ! I still need to investigate further why its not currently working.... |
@chrisdill @chriscamacho Actually, just discovered that for some Meshes |
This is generally the case when using IndexedRendering. Its very idea is to reuse vertecies, thus (mesh.triangleCount*3 != mesh.vertexCount). |
@Crydsch so what kicks the vertex shader, if you have no vertex data... @raysan5 afaict by the time stuff comes out of tinyobj_loader its all indexed, but then I've just looked up the indexes and probably on some models duplicating some verts (deliberately) I can't see the "wasted" memory being significant and it's potentially less lookups for the GPU, but who knows what's faster on what hardware with which drivers - impossible to second guess... so long story short with OBJ loading the *3 rule should hold... |
@chriscamacho I am using the normal Also, "un-indexing" meshes sounds very wrong to me. Indexed meshes should generally be preferred. Source: https://www.khronos.org/opengl/wiki/Vertex_Shader#Invocation_frequency |
@Crydsch if unindexing is so wrong feel free to implement something better ! What you get in an OBJ is just a soup of vertexes, and a bunch of indexes, the easiest way to pick them apart is to "un-index" as you put it, in practice in almost all the models I tested there were very few duplicated vertices - that's just how artists roll... regarding your zero vertex technique its not zero vertices its a whole bunch of vertices you're just ignoring the data (unless I misunderstand your explanation?), I struggle to see the advantage of this technique, if there is data there you may as well use it - even if you are going to add deltas onto it later... |
@chriscamacho Don't get me wrong, if there are not many duplicate indexes in a mesh then indexed rendering may not be beneficial. Actually there is vertex data, just not in a buffer i need to load onto the gpu. I use the vertex shader to generate the vertex data. |
After lot of thinking about this functionality, I decided to add a simple Advance users requiring some specific vertex attributes can create the |
Issue description
Currently to generate a Mesh with your own data(vertices, texcoords etc), you need to allocate it manually and make sure the vertexCount is setup properly. This works although I think it can be made easier for most use cases.
My suggestion is to add a
GenMeshDefault
function to help with Mesh creation. You pass in the vertexCount, triangleCount and a enum to flag the arrays you want and it allocates them for you. You could also store the flags in the mesh for querying later.The naming can be changed as needed. This was the first thing that I thought of.
Any feedback or discussion about this issue is appreciated! :)
Code Example
The text was updated successfully, but these errors were encountered: