【发布时间】:2014-12-04 18:13:50
【问题描述】:
我今天早上一直在尝试为 OpenGL 设置一个对象加载器,并且已经取得了一些进展,但现在我陷入了让我的设置正常工作的最后一步。我的主要 OpenGL 程序是用c (shift.c) 编写的,我的对象加载器是用C++ (loader.cpp) 编写的。我还根据this SO thread 编写了两个loader.h 的链接器。
我认为我在翻译中做错了,因为到目前为止我所做的一切都没有正常工作。我的文件如下。
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 */
loader.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++ 吗?您甚至可以保留现有的 C 代码并将文件重命名为
shift.cpp,这可能会使其正常工作。 C 不是 C++ 的确切子集,但在这种情况下,它可能会表现得好像是一样。 -
我还注意到您需要一个移动构造函数、移动赋值和析构函数。也许还有复制构造函数,复制赋值。
-
有什么理由不遵循该帖子中已接受答案的示例?
-
我确实按照那个帖子中的答案,但遇到了问题。这就是我发布另一个的原因。
-
请调查不透明类型
struct model_obj;(不是Model_OBJ)的正确使用。请参阅该帖子中的struct animal; // a nice opaque type。