简体   繁体   English

OpenGL顶点和波前obj的索引

[英]OpenGL Vertices and indices to wavefront obj

I have created my own vertices, colors, and indices which will be rendered with openGL. 我创建了自己的顶点,颜色和索引,它们将通过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. 如您所见,索引将被构建为GL_TRIANGLE_STRIP并将被呈现。 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. 我试图使.obj文件说谎。

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. 我尝试使用assimp浏览器( http://www.assimp.org/ )重新打开它,但无法加载。 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. 请添加一些简单的示例obj文件(如多维数据集之类的代码),以便我们可以查看文件中包含的内容。 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 GL_TRIANGLE_STRIP每个三角形有1顶点,而不是3

    According to OpenGL doc A vertex stream of n length will generate n-2 triangles with GL_TRIANGLE_STRIP . 根据OpenGL文档 ,长度为n的顶点流将生成GL_TRIANGLE_STRIP n-2三角形。

  2. Wavefront obj file indexes are starting from 1 Wavefront obj文件索引从1开始

    So you most likely indexing from 0 so check for that... and use +1 or not accordingly. 因此,您最有可能从0编制索引,因此请检查该...并相应地使用+1或不使用+1

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: 我不知道您的数据结构的体系结构/拓扑,但是我认为您的脸应该这样保存(未经测试,因为我没有您的数组/表和模型)将#1,#2放在一起:

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: 对于此波前obj,它在单独的文件mtl,stl中使用了材料扩展名(我不熟悉那些抱歉的人...),但是如今3D扫描仪正直接在obj文件中使用未记录的颜色编码(某些观看者支持),这样做是这样的:

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 因此,如果顶点有6坐标,则前3x,y,z ,后3r,g,b因此链接答案中用红色显示的立方体将是

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 ... 您可以在查看器中尝试它,以检查它是否支持这种编码...我在一些低成本3D扫描仪应用程序上看到它们能够保存此类文件,但在重新加载后无法渲染颜色...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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