# Converting IntPtr to objects (here Raylib Material[])

I'm trying to apply a texture to the material of a Model but I could not figure out how to do it:

so I made a direct translation of the c code taken from examples:

model.materials[0].maps[MAP_DIFFUSE].texture = texture;


(my c# translation)

m_LP_SpookyTree = LP_SpookyTree.GetModel;


According to the cs file on the repo, the "materials" definition is an Int pointer. The problem comes from the Intptr, how could I get the material array from it?

PS: I've created an issue on Raylib.cs GitHub repo so if I get an answer on one before the other, I'll update accordingly

EDIT 1:

I've tried to change every IntPtr to the object it should actually point to regarding the comments in the source file, compile the lib, and imported it. unfortunately, it broke. so I don't think it's possible.

EDIT 2:

https://stackoverflow.com/questions/728105/how-to-convert-an-intptr-back-into-an-object

I've implemented it like so:

public static object pointerToObject(IntPtr pMapping, Type type)
{
return Marshal.PtrToStructure(pMapping, type);
}


but now I get this error:

System.MissingMethodException : 'No parameterless constructor defined for type 'Raylib_cs.Material[]'.'


PS: The new code:

Material[] materials = (Material[])PointerManipulation.pointerToObject(m_LP_SpookyTree.materials,typeof(Material[]));
MaterialMap[] materialMaps = (MaterialMap[])PointerManipulation.pointerToObject(materials[0].maps, typeof(MaterialMap[]));



I've found a way to do it without strange coding.

I've found in this repo linked on the Raylib.cs readme.md file a translation of the c examples to c#.

Though, I had to do small modifications to the script.

Following are the original script and the modified version that worked for me:



// Set map diffuse texture
Material* materials = (Material*)model.materials.ToPointer();
MaterialMap* maps = (MaterialMap*)materials[0].maps.ToPointer();


mine:

            m_LP_SpookyTree = LP_SpookyTree.GetModel;
unsafe {
Material* materials = (Material*)m_LP_SpookyTree.materials.ToPointer();
MaterialMap* maps = (MaterialMap*)materials[0].maps.ToPointer();

maps[(int)MaterialMapType.MAP_ALBEDO].texture = d_LP_SpookyTree;

Raylib.GenTextureMipmaps(ref maps[(int)MaterialMapType.MAP_ALBEDO].texture);
}


I Had to add Raylib name before LoadTexture and GenTextureMipmaps because else it would not work since these are static methods from the RayLib cs binder.

So on a technical point, I think, from what I've seen in the code, that to translate an IntPtr we don't need to use strange methods that do not work. Instead, an explicit cast- as followed - suffice to make the pie. You might actually correct me since I'm not a really good programmer.

(Material*)m_LP_SpookyTree.materials.ToPointer();


In comparison here is the c code to get the material array:

m_LP_SpookyTree.materials;


In the cs binder, this returns an IntPtr which must be converted to its correct type.

also, I had to change MAP_ALBEDO to MaterialMapType.MAP_ALBEDO.

you can witness following is the working proof:

Hope it would help Cs users of Raylib