Skip to content
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

Added a custom mesh to the example models/mesh_generation #462

Merged
merged 4 commits into from
Nov 14, 2024

Conversation

Hultan
Copy link
Contributor

@Hultan Hultan commented Nov 11, 2024

This example at raylib.com has a custom mesh, which was missing in the golang example. So I added it...

However, the program panics when unloading the custom model. So I had to create the following function to clear the mesh before calling unload, and now it does not panic.

I don't know if this is something that I don't understand, or if it is because of some underlying problem in the Mesh type, or UnloadModel function? Someone who knows more about the C code than me, might want to take a look at that.

func clearCustomMesh(models []rl.Model) {
	// For some reason the custom model
	// (id = 8) panics when unloading it.
	// So we clear the mesh for it manually
	// here.
	models[8].Meshes.Vertices = nil
	models[8].Meshes.Normals = nil
	models[8].Meshes.Texcoords = nil
}

@gen2brain
Copy link
Owner

This is probably related to #303, what you are clearing there doesn't look like is doing anything at all.

@Hultan
Copy link
Contributor Author

Hultan commented Nov 11, 2024

Well, try it out yourself. Remove the call to the clearCustomMesh function, and the program will panic in rl.UnloadModel(models[i]).

I don't understand why it works to set these values to nil, but it did on my machine...

@gen2brain
Copy link
Owner

Did you check the issue?

@Hultan
Copy link
Contributor Author

Hultan commented Nov 11, 2024

I have now skimmed through it, but I still not sure what I am supposed to do differently?
Should I do something like this? This code still crashes:

	rl.UnloadTexture(texture)
	for i := 0; i < numModels; i++ {
		meshes := unsafe.Slice(models[i].Meshes, models[i].MeshCount)
		for m := range meshes {
			rl.UnloadMesh(&meshes[m])
		}
		rl.UnloadModel(models[i])
	}

@gen2brain
Copy link
Owner

No idea, I was just pointing out that it is probably related as you are using the custom mesh. There is an explanation there and a workaround implemented. Sorry, I don't have time to go through all that now.

@Hultan
Copy link
Contributor Author

Hultan commented Nov 11, 2024

No problem, I will look into it a little bit more, but that might take a couple of days...

@JupiterRider
Copy link
Contributor

Vertices, Normals and Texcoords of your custom mesh are Go slices.
UnloadModel calls UnloadMesh for every mesh and UnloadMesh tries to free your Go slices. The panic happens, because it cannot free Go slices.

free is a C function and it expects to free C memory and not a Go slice.

@Hultan
Copy link
Contributor Author

Hultan commented Nov 11, 2024

Ok, now I understand the problem, but what is the solution then? To just do what I did, and nilthem?

Or to not call UnloadModel/UnloadMesh at all for Custom Models?

@JupiterRider
Copy link
Contributor

Ok, now I understand the problem, but what is the solution then? To just do what I did, and nilthem?

Or to not call UnloadModel/UnloadMesh at all for Custom Models?

Yes. Setting the Go slices to Nil (like you already did) is fine.

@Hultan
Copy link
Contributor Author

Hultan commented Nov 11, 2024

Then I guess this can be merged now, I added your explanation to my clearCustomMesh function as a comment.

@gen2brain gen2brain merged commit f465c30 into gen2brain:master Nov 14, 2024
10 checks passed
@Hultan Hultan deleted the mesh_generation branch November 15, 2024 05:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants