gpt4 book ai didi

c# - 从 C# 中使用 C++ 创建的函数的最佳使用

转载 作者:行者123 更新时间:2023-11-28 06:48:16 24 4
gpt4 key购买 nike

<分区>

在.h文件中

void EXPORT_API GetModelReferences(int &nVertices, void * i_vertices, int &nTriangles, void* i_triangles, void * i_normals, void * i_uvCoordinates);

其中 EXPORT_API#define EXPORT_API __declspec(dllexport) 的定义

我创建了一个具有一个功能的 dll:

GetModelReferences(int &nVertices, void * i_vertices, int &nTriangles, void* i_triangles, void * i_normals, void * i_uvCoordinates);

现在我想在 C# 代码中使用它,所以这就是我的 C# 的样子:

    [DllImport("LoadModelDll")]

//unsafe
public unsafe static extern void GetModelReferences(ref int nVertices, ref float[] vertices,ref int nTriangles,ref float[] triangles,ref float[] normals,ref float[] uvCoordinates);

//static extern void GetModelReferences(ref int nVertices, float* vertices, ref int nTriangles, IntPtr triangles, float*[] normals, float*[] uvCoordinates);



// Use this for initialization
void Start () {
MeshFilter mf = GetComponent<MeshFilter> ();
Mesh mesh = new Mesh ();
mf.mesh = mesh;

unsafe {
int lnVertices = 0;
int lnTriangls = 0;


float[] lVertices = new float[12];
float[] lTriangles = new float[6];
float[] lNormals = new float[12];
float[] lUVcoordinates = new float[8];

GetModelReferences(ref lnVertices, ref lVertices ,ref lnTriangls,ref lTriangles,ref lNormals,ref lUVcoordinates);

}
}

程序在点击调用函数的行时崩溃。我是 C# 编程的新手,不确定如何管理它。

CODE OF C++:


void GetModelReferences(int &nVertices, void * i_vertices, int &nTriangles, void* i_triangles, void * i_normals, void * i_uvCoordinates){

float * vertices = (float*)i_vertices;
int * triangles = (int*)i_triangles;
float * normals = (float*)i_normals;
float * uvCoordinates = (float*)i_uvCoordinates;

//set uv coordinates
int uvCoordinatesArray[8];

uvCoordinatesArray[0] = 0;
uvCoordinatesArray[1] = 0;

uvCoordinatesArray[2] = 1;
uvCoordinatesArray[3] = 0;

uvCoordinatesArray[4] = 0;
uvCoordinatesArray[5] = 1;

uvCoordinatesArray[6] = 1;
uvCoordinatesArray[7] = 1;

//set normals
int normalsArray[12];
normalsArray[0] = 0;
normalsArray[1] = 0;
normalsArray[2] = 1;

normalsArray[3] = 0;
normalsArray[4] = 0;
normalsArray[5] = 1;

normalsArray[6] = 0;
normalsArray[7] = 0;
normalsArray[8] = 1;

normalsArray[9] = 0;
normalsArray[10] = 0;
normalsArray[11] = 1;

memcpy(normals, &normalsArray, sizeof(int) * 12);

//set nTriangles
int numberOfTringles = 6;
nTriangles = numberOfTringles;


//set nVertices parameter
int numberOfVertices = 12;
nVertices = numberOfVertices;



//set vertices parameter
float width = 350;
float height = 50;
float verticesArray[12];

verticesArray[0] = 0;
verticesArray[1] = 0;
verticesArray[2] = 0;

verticesArray[3] = width;
verticesArray[4] = 0;
verticesArray[5] = 0;

verticesArray[6] = 0;
verticesArray[7] = height;
verticesArray[8] = 0;

verticesArray[9] = width;
verticesArray[10] = height;
verticesArray[11] = 0;

memcpy(vertices, &verticesArray, sizeof(float)*4*3);


// setting triangle values.
int tri[6];

tri[0] = 0;
tri[1] = 2;
tri[2] = 1;
tri[3] = 2;
tri[4] = 3;
tri[5] = 1;

memcpy(triangles, &tri, sizeof(float)* 6);
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com