我今天早上一直在尝试为OpenGL设置一个对象加载器,并且已经取得了一些进展,但是现在我被困在似乎是让我的设置工作的最后一步。我的主要OpenGL程序是用c (shift.c)
编写的,我的对象加载器是用C++ (loader.cpp)
编写的。我还根据this SO thread编写了两个loader.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 */
/*
*
* 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");
}
答案 0 :(得分:5)
使用像你这样的C ++结构,该标题的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);
}