简体   繁体   English

如何获取OpenGL 3D模型截面图?

[英]how to get opengl 3d model sectional drawing?

I have load an obj file to render my opengl model using pyopengl and pygame. 我已经加载了一个obj文件以使用pyopengl和pygame渲染我的opengl模型。 The 3D model show successfully. 3D模型显示成功。

Below is the 3D model i render with obj file, Now i cut my model into ten pieces through y axis , my question is how to get the sectional drawing in each piece? 以下是我使用obj文件渲染的3D模型,现在我通过y轴将模型切割成十个零件,我的问题是如何获取每个零件的剖面图?

I'm really very new to openGL, Is there any way can do that? 我对openGL真的很陌生,有什么办法可以做到吗?

我的3D模型

There are two ways to do this and both use clipping to "slice" the object. 有两种方法可以执行此操作,并且都可以使用裁剪来“切片”对象。

  1. In older versions of OpenGL you can use user clip planes to "isolate" the slices you desire. 在旧版本的OpenGL中,您可以使用用户剪切平面来“隔离”所需的切片。 You probably want to rotate the object before you clip it, but it's unclear from your question. 您可能想在裁剪对象之前先旋转它,但是您的问题尚不清楚。 You will need to call glClipPlane() and you will need to enable it using glEnable with the argument GL_CLIP_PLANE0 , GL_CLIP_PLANE1 , ... 您将需要调用glClipPlane()并需要使用glEnable并将其与参数GL_CLIP_PLANE0GL_CLIP_PLANE1 ,...一起启用。

If you don't understand what a plane equation is you will have to read up on that. 如果您不了解什么是平面方程,则必须继续阅读。

In theory you should check to see how many user clip planes exist on your GPU by calling glGetIntegerv with argument GL_MAX_CLIP_PLANES but all GPUs support at least 6. 理论上,您应该通过使用参数GL_MAX_CLIP_PLANES调用glGetIntegerv来检查GPU上有多少用户裁剪平面,但是所有GPU至少支持6个。

  1. Since user clip planes are deprecated in modern Core OpenGL you will need to use a shader to get the same effect. 由于在现代Core OpenGL中不建议使用用户剪切平面,因此您将需要使用着色器来获得相同的效果。 See gl_ClipDistance[] 参见gl_ClipDistance[]

Searching around on Google should get you plenty of examples for either of these. 在Google上四处搜索应该会为您提供大量的示例。

Sorry not to provide source code but I don't like to post code unless I am 100% sure it works and I don't have the time right now to check it. 抱歉,没有提供源代码,但我不喜欢发布代码,除非我100%确信它可以工作并且我现在没有时间检查它。 However I am 100% sure you can easily find some great examples on the internet. 不过,我100%确信您可以在互联网上轻松找到一些出色的例子。

Finally, if you can't make it work with clip planes and some hacks to make the cross sections visible then this may indeed be complicated because creating closed cross sections from an existing model is a hard problem. 最后,如果您不能使其与剪切平面配合使用,并且有些技巧无法使横截面可见,那么这可能确实很复杂,因为从现有模型创建闭合横截面是一个难题。

You would need to split the object, and then rotate the pieces so that they are seen from the side. 您将需要分割对象,然后旋转零件,以便从侧面看到它们。 (Or move the camera. The two ideas are equivalent. But if you're coding this from scratch, you don't really have the abstraction of a 'camera'.) At that point, you can just render all the slices. (或者移动相机。这两个想法是等效的。但是,如果您从头开始编写代码,则实际上并没有“相机”的抽象概念。)此时,您可以渲染所有切片。

This is complicated to do in raw OpenGL and python, essentially because objects in OpenGL are not solid. 在原始OpenGL和python中这样做很复杂,主要是因为OpenGL中的对象不是实体。 I would highly recommend that you slice the object into pieces ahead of time in a modeling program. 我强烈建议您在建模程序中提前将对象切成碎片。 If you need to drive those operations with scripting, perhaps look into Blender's python scripting system. 如果您需要使用脚本来驱动这些操作,则可以考虑使用Blender的python脚本系统。

Now, to explain why: 现在,解释一下原因:

When you slice a real-life orange, you expect to get cross sections. 切成现实生活中的橙子时,您希望得到横截面。 You expect to be able to see the flesh of the fruit inside, with all those triangular pieces. 您期望能够看到带有所有这些三角形块的水果的果肉。

There is nothing inside a standard polygonal 3D model. 标准多边形3D模型内部没有任何内容。

Additionally, as the rind of a real orange has thickness, it is possible to view the rind from the side. 另外,由于真实橙皮的果皮具有厚度,因此可以从侧面观察果皮。 In contrast, one face of a 3D model is infinitely thin, so when you view it from the side, you will see nothing at all. 相反,3D模型的一个面是无限薄的,因此,从侧面看时,您什么也看不到。 So if you were to render the slices of this simple model, from the side, each render would be completely blank. 因此,如果要从侧面渲染此简单模型的切片,则每个渲染都将完全空白。

(Well, the bits at the end will have 'caps', like the ends of a loaf a bread, but the middle sections will be totally invisible.) (好吧,末端的面包将像面包的末端一样带有“盖子”,但是中间的部分将是完全看不见的。)

Without a programming library that has a conception of what a cut is, this will get very complicated, very fast. 如果没有一个关于剪切的概念的编程库,这将变得非常复杂,非常快。 Simply making the cuts is not enough. 仅进行削减是不够的。 You must seal up the holes created by slicing into the original shape, if you want to see the cross-sections. 如果要查看横截面,必须将通过切成原始形状而创建的孔密封起来。 However, filling up the cross sections has to be done intelligently, otherwise you'll wind up with all sorts of weird shading artifacts (fyi: this is caused by n-gons, if you want to go discover more about those issues). 但是,必须聪明地完成横截面的填充,否则您将结出各种奇怪的阴影瑕疵(fyi:这是由n边形引起的,如果您想进一步了解这些问题)。

To return to the original statement: 返回原始语句:

Modeling programs are designed to address problems such as these, so I would suggest you leverage their power if possible. 建模程序旨在解决此类问题,因此,我建议您尽可能利用它们的功能。 Or at least, you can examine how Blender implements this functionality, as it is open source. 或者至少,您可以检查Blender是开放源代码如何实现此功能。

In Blender, you could make these cuts with the knife tool*, and then fill up the holes with the 'make face' command (just hit F). 在Blender中,您可以使用刀具*进行这些切割,然后使用“ make face”命令(仅按F键)填充孔。 Very simple, even for those who are not great at art. 非常简单,即使对于那些不擅长艺术的人也是如此。 I encourage you to learn a little bit about 3D modeling before doing too much 3D programming. 我鼓励您在进行过多3D编程之前先学习一些有关3D建模的知识。 It personally helped me a lot. 它对我个人有很大帮助。

*(The loop cut tool may do the job as well, but it's hard to tell without understanding the topology of your model. You probably don't want to get into understanding topology right now, so just use the knife) *(循环剪切工具也可以完成这项工作,但是如果不了解模型的拓扑结构就很难分辨出来。您可能不想现在就开始了解拓扑结构,所以只需要用刀即可)

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

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