Save openGL objects to file

Started by
22 comments, last by swiftcoder 11 years, 6 months ago
Hi guys,
I am making an L-sytem tree application. I require a way to save the generated models into a text file(.obj seems the best as it saves the vertices). Check this site for the L-system framework
I know how read and write to a text with plain C++ but the withOpenGL it is not straight forward.
Can someone please provide me with any help on this matter.?
Preferablly an example like saving the openGL cube or teapot veryices into a text file. Thanks
Advertisement
There is no such concept as an OpenGL cube or teapot, you supply the geometry to the API and it is your responsibility to serialize/deserialize geometry and other assets yourself.

What specifically are you trying to do? Save a mesh you've generated yourself to disk? If so, you will need to write the geometry in the file format of your choice or use a library like the dubiously titled AssImp.
Thanks GeneralQuery yes saving the mesh is what i am trying to do.
I will check out AssImp but if you know a way of writing the geometry
in .OBJ format i will really appreciate it.
AssImp, AFAIK, can export meshes to a number of formats, including OBJ. You could implement your own OBJ exporter as it's simple enough but IMO you may as well use an existing solution unless you wish to do this as an academic exercise. It's a fairly boring process to DIY and you'll get far more flexibility and less headaches from using something like AssImp. You can reverse engineer the OBJ format easily enough or check out one of the many tutorials on the topic, or even lift the code from AssImp if you so desire.
Why do you need to save the generated vertices?

I mean, it's a well defined, recursion driven system - You can simply save the proper function, and then run the generation algorithm whenever the object is actually needed.

+---------------------------------------------------------------------+

| Game Dev video tutorials -> http://www.youtube.com/goranmilovano | +---------------------------------------------------------------------+
[color=#ff0000]lol i didnt read about writing it as text file well it is more complicated than this (loading)

well if you want to save vertices only just do sth like this

struct THeader{
int vertlen; //amount of vertices you want to save
};

struct t3dpoint {
float x;
float y;
float z;
};
FILE* f = fopen(filename,"wb+");

fwrite(&header,sizeof(THeader),1,f);

now you can save them
//through all vertices in array
for (j=0; j < header.vertlen ; j++) fwrite(&verts[j],sizeof(verts[j]),1,f); //or sizeof t3dpoint (whatever)
fclose(f);




and you want to load them





int length;
int j;
FILE* f = fopen(filename,"rb");

memset(&header,0,sizeof(THeader)); //do this if header consist more than one var if not ther is no need even to make a struct from header
fread(&header,sizeof(THeader),1,f);

int j;

set the length of verts - array in excample it can be simple pointer (t3dpoint * verts;)
verts = new t3dpoint[header.vertlen];
for (j=0; j < header.vertlen ; j++) {
t3dpoint imo;
fread(&imo,sizeof(imo),1,f);
verts[j] = imo; //something like this i use static arrays so i just assign it like thus if this does not work you will need to write (*verts) = imo; but have no idea if it will work anyway :P
}

fclose(f);
}



Congrats! you have your own file type
Thanks for the replies people.
This question is addressed to ____ ,
I have tried using your sample code to save the vertices of an opengl cube to file but
i keep getting errors can you please provide a way foward. Thanks
pls tell me what sort of errors, where compiler sees the error? or the progarm breaks when loading saving, pls also give code what you use, cheers
and almost forgot add #include "iostream.h"
Hi _____,
Iam using the first part of your code the error is on this line
fwrite(&header,sizeof(THeader),1,f);


My full code is below, iam just trying to save the cube vertices into memory
please give me any ideas you might have. Thanks
[source lang="cpp"]

#include <GL/freeglut.h>
#include <iostream>
#include <stdio.h>
bool fullscreen = false;
bool mouseDown = false;

float xrot = 0.0f;
float yrot = 0.0f;

float xdiff = 0.0f;
float ydiff = 0.0f;

void drawBox()
{
glColor3f(0.0f, 0.0f, 1.0f);
glutSolidCube(2);
}

bool init()
{
glClearColor(0.93f, 0.93f, 0.93f, 0.0f);

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glClearDepth(1.0f);

return true;
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

gluLookAt(
0.0f, 0.0f, 3.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f);

glRotatef(xrot, 1.0f, 0.0f, 0.0f);
glRotatef(yrot, 0.0f, 1.0f, 0.0f);


drawBox();

glFlush();
glutSwapBuffers();
}

void resize(int w, int h)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glViewport(0, 0, w, h);

gluPerspective(45.0f, 1.0f * w / h, 1.0f, 100.0f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void idle()
{
if (!mouseDown)
{
xrot += 0.3f;
yrot += 0.4f;
}

glutPostRedisplay();
}

void keyboard(unsigned char key, int x, int y)
{
switch(key)
{
case 27 :
exit(1); break;
}
}




//*
///save vertices
struct THeader{
int vertlen; //amount of vertices you want to save
THeader();
};

struct t3dpoint {
float x;
float y;
float z;
};
FILE* f = fopen("vertices.txt","wb+");

fwrite(&header,sizeof(THeader),1,f);

//now you can save them
//through all vertices in array
for (j=0; j < header.vertlen ; j++) fwrite(&verts[j],sizeof(verts[j]),1,f); //or sizeof t3dpoint (whatever)
fclose(f);
///

/*///Load
int length;
int j;
FILE* f = fopen(filename,"rb");

memset(&header,0,sizeof(THeader)); //do this if header consist more than one var if not ther is no need even to make a struct from header
fread(&header,sizeof(THeader),1,f);

int j;

//set the length of verts - array in excample it can be simple pointer (t3dpoint * verts;)
verts = new t3dpoint[header.vertlen];
for (j=0; j < header.vertlen ; j++) {
t3dpoint imo;
fread(&imo,sizeof(imo),1,f);
verts[j] = imo; //something like this i use static arrays so i just assign it like thus if this does not work you will need to write (*verts) = imo; but have no idea if it will work anyway tongue.png
}

fclose(f);
}
//*/
//*/
int main(int argc, char *argv[])
{
glutInit(&argc, argv);

glutInitWindowPosition(50, 50);
glutInitWindowSize(500, 500);

glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);

glutCreateWindow("13 - Solid Shapes");

glutDisplayFunc(display);
glutKeyboardFunc(keyboard);

glutReshapeFunc(resize);
glutIdleFunc(idle);

if (!init())
return 1;

glutMainLoop();

return 0;
}

[/source]
Why do you persistently refuse to tell the actual error reported? Don't omit important information.

This topic is closed to new replies.

Advertisement