简体   繁体   中英

OpenGL Vertices and indices to wavefront obj

I have created my own vertices, colors, and indices which will be rendered with openGL. Here are source codes to render vertices, colors and indices.

//CODES TO SET VBO....
void displayCallback() {    
    glVertexPointer(...);
    glColorPointer(...);
    glDrawElemnts(GL_TRIANGLE_STRIP,...);
}

As you can see, indices will be build as GL_TRIANGLE_STRIP and will be rendered. After rendering it, I want to save wavefront object file with vertex, index, and color data.

I've tried to make .obj file lie this.

for(int i = 0; i < vertexSize; ++i) {
    fprintf(fp, "v %f %f %f\n", vertices[i*3], vertices[i*3+1], vertices[i*3+2]);
}

fprintf("fp, "s 1\n");

for(int i = 0; i < indexSize; ++i) {
    fprintf(fp, "f %d %d %d\n", indices[i*3], indices[i*3+1], indices[i*3+2]);
}

I've tried to re-open it using assimp viewer, ( http://www.assimp.org/ ) it failed to load. Am I missing something?

Thanks.

Please add some simple sample obj file (as code like cube) so we can see what the file has in it. To see what I mean by that take a look at:

My bet is that you got wrong Indices.

  1. GL_TRIANGLE_STRIP has 1 vertex per triangle not 3

    According to OpenGL doc A vertex stream of n length will generate n-2 triangles with GL_TRIANGLE_STRIP .

  2. Wavefront obj file indexes are starting from 1

    So you most likely indexing from 0 so check for that... and use +1 or not accordingly.

I do not know your data structure architecture/topology but in my opinion your faces should be saved like this (not tested as I do not have your arrays/tables and model) putted #1,#2 together:

fprintf(fp, "f %d %d %d\n", indices[0]+1, indices[1]+1, indices[2]+1);
for(int i = 3; i < indexSize; i++) 
 fprintf(fp, "f %d %d %d\n", indices[i-2]+1, indices[i-1]+1, indices[i]+1);

Color data

For this wavefront obj is using material extensions in separate files mtl,stl (I am not familiar with those sorry ...) but nowadays 3D scanners are using undocumented color encoding directly inside obj files (supported by some viewers) it is done like this:

v -5.231932 438.659877 -432.038039 0.000000 1.000000 0.000000

So if vertex has 6 coordinates the first 3 are x,y,z and last 3 are r,g,b so cube from linked answer colored in red will be

v -1.0 -1.0 -1.0 1 0 0
v +1.0 -1.0 -1.0 1 0 0
v +1.0 +1.0 -1.0 1 0 0
v -1.0 +1.0 -1.0 1 0 0
v -1.0 -1.0 +1.0 1 0 0
v +1.0 -1.0 +1.0 1 0 0
v +1.0 +1.0 +1.0 1 0 0
v -1.0 +1.0 +1.0 1 0 0

f 1 2 3 4 
f 5 6 7 8 
f 1 2 6 5 
f 2 3 7 6 
f 3 4 8 7 
f 4 1 5 8 

You can try it in your viewer to check if it is supporting such encoding... I saw on some low cost 3D scanner apps that they where able to save such files but after reloading it not be able to render the colors ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM