gpt4 book ai didi

c++ - 从 C 中正确使用 C++ 函数

转载 作者:太空狗 更新时间:2023-10-29 20:39:37 40 4
gpt4 key购买 nike

今天早上我一直在尝试为 OpenGL 设置一个对象加载器并取得了一些进展,但现在我被困在似乎是让我的设置正常工作的最后一步。我的主要 OpenGL 程序是用 c (shift.c) 编写的,我的对象加载器是用 C++ (loader.cpp) 编写的。我还根据 this SO thread 为两个 loader.h 编写了链接器。 .

我认为我在翻译中做错了什么,因为到目前为止我所做的一切都没有正常工作。我的文件如下。

加载器.h

#ifndef LOADER_H
#define LOADER_H

#ifdef _cplusplus
class Model_OBJ
{
public:
Model_OBJ();
float* Model_OBJ::calculateNormal(float* coord1,float* coord2,float* coord3 );
int Model_OBJ::Load(char *filename); // Loads the model
void Model_OBJ::Draw(); // Draws the model on the screen
void Model_OBJ::Release(); // Release the model

float* normals; // Stores the normals
float* Faces_Triangles; // Stores the triangles
float* vertexBuffer; // Stores the points which make the object
long TotalConnectedPoints; // Stores the total number of connected verteces
long TotalConnectedTriangles; // Stores the total number of connected triangles

};
#endif /* __cplusplus */

#ifdef _cplusplus
extern "C" {
#endif
struct model_obj;

float* calculateNormal( float *coord1, float *coord2, float *coord3 );
int Load(char* filename);
void Release();
void Draw();

float* normals; // Stores the normals
float* Faces_Triangles; // Stores the triangles
float* vertexBuffer; // Stores the points which make the object
long TotalConnectedPoints; // Stores the total number of connected verteces
long TotalConnectedTriangles; // Stores the total number of connected triangles

#ifdef _cplusplus
}
#endif

#endif /* LOADER_H */

加载程序.cpp

/*
*
* Demonstrates how to load and display an Wavefront OBJ file.
* Using triangles and normals as static object. No texture mapping.
* http://talkera.org/opengl/
*
* OBJ files must be triangulated!!!
* Non triangulated objects wont work!
* You can use Blender to triangulate
*
*/

#include "loader.h"
// Rest of my includes

class Model_OBJ
{
public:
Model_OBJ();
float* calculateNormal(float* coord1,float* coord2,float* coord3 );
int Load(char *filename); // Loads the model
void Draw(); // Draws the model on the screen
void Release(); // Release the model

float* normals; // Stores the normals
float* Faces_Triangles; // Stores the triangles
float* vertexBuffer; // Stores the points which make the object
long TotalConnectedPoints; // Stores the total number of connected verteces
long TotalConnectedTriangles; // Stores the total number of connected triangles

};

#define POINTS_PER_VERTEX 3
#define TOTAL_FLOATS_IN_TRIANGLE 9
using namespace std;

Model_OBJ::Model_OBJ(){...}

float* Model_OBJ::calculateNormal( float *coord1, float *coord2, float *coord3 ){...}
int Model_OBJ::Load(char* filename){...}
void Model_OBJ::Release(){...}
void Model_OBJ::Draw(){...}

在我的 shift.c 文件中。我一直在尝试做如下的事情。但尚未成功。

#include "loader.h"
// Up in variable declarions
struct model_obj *obj1;

int main()
{
obj1.Load("file.obj");
}

最佳答案

对于像您这样的 C++ 结构,该 header 的 C 部分需要进行重大更改。通常,围绕 C++ 类的 C 包装器看起来更像这样。 (此外,您还必须将 class 更改为 struct。)

#ifdef _cplusplus
extern "C" {
#endif
struct Model_OBJ;

Model_OBJ* Model_OBJ_new(void);
void Model_OBJ_delete(Model_OBJ*self);

float* Model_OBJ_calculateNormal(Model_OBJ*self,float*coord1,float*coord2,float*coord3);
int Model_OBJ_Load(Model_OBJ*self,char*filename); // Loads the model
void Model_OBJ_Draw(Model_OBJ*self); // Draws the model on the screen

#ifdef _cplusplus
}
#endif

然后在 C++ 文件中,您还必须实现这些功能。

Model_OBJ* Model_OBJ_new(void) {return new Model_OBJ();}
void Model_OBJ_delete(Model_OBJ*self) {delete self}


float* Model_OBJ_calculateNormal(Model_OBJ*self,float*coord1,float*coord2,float*coord3)
{return self->calculateNormal(coord1,coord2,coord3);}
int Model_OBJ_Load(Model_OBJ*self,char*filename)
{return self->Load(filename);}
void Model_OBJ_Draw(Model_OBJ*self)
{return self->Draw();}

然后C代码就可以使用它了:

int main() {
Model_OBJ* obj1 = Model_OBJ_new();
Model_OBJ_Load(obj1, "file.obj");
Model_OBJ_delete(obj1);
}

关于c++ - 从 C 中正确使用 C++ 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27301070/

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