3

I have been trying to setup an object loader for OpenGL this morning and have been making some progress, but now I am stuck on what seems to be the final step in getting my setup to work. My main OpenGL program is written in c (shift.c) and my object loader is written in C++ (loader.cpp). I have also written the linker for the two loader.h based on this SO thread.

I think that I have done something wrong in the translation, because everything I have done so far hasn't worked properly. My files are as followed.

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(){...}

In my shift.c file. I have been trying to do something like the following. But have yet to be successful.

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

int main()
{
  obj1.Load("file.obj");
}
Community
  • 1
  • 1
Scalahansolo
  • 2,615
  • 6
  • 26
  • 43
  • Any reason why you didn't go for C++ all the way ? You could probably even keep your existing C code and rename the file to `shift.cpp` that would likely make it work. C is not an exact subset of C++ but in this case it probably would act as-if it were. – ereOn Dec 04 '14 at 18:22
  • I also note you need a move constructor, move assignment, and destructor. And maybe copy constructor, copy assignment. – Mooing Duck Dec 04 '14 at 18:23
  • Any reason not to follow the example of the accepted answer in that post? –  Dec 04 '14 at 18:42
  • I did follow that answer in that post but ran into problems. That is why I posted another. – Scalahansolo Dec 04 '14 at 18:45
  • Please investigate the proper use of the opaque type `struct model_obj;` (which is not an Model_OBJ). See `struct animal; // a nice opaque type` in that post. –  Dec 04 '14 at 18:52

1 Answers1

5

With a C++ struct like yours, the C part of that header would require serious changes. Usually a C wrapper around a C++ class looks more like this. (Also you have to change class to 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

And then in the C++ file, you'd have to also have these functions implemented.

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();}

And then the C code can use it:

int main() {
    Model_OBJ* obj1 = Model_OBJ_new();
    Model_OBJ_Load(obj1, "file.obj");
    Model_OBJ_delete(obj1);
}
Mooing Duck
  • 64,318
  • 19
  • 100
  • 158