Intersect a cube with a cylinder

Hi guys .

I want to produce something like this in open GL.


Basicly doing the intersection of a cylinder and a cube/paralelipip.

I cant find any info how to do this online with open GL , what is the command to do it?

It sounds like you want to implement CSG operations.

OpenGL® on it’s own doesn’t have any explicit support for that kind of thing. OpenGL® works with triangle meshes, not volumes.
(in other words: there is no command for this, this is way too high level)

You can of course use your favourite CSG model editing program, export to some triangle mesh format, load it in your GL program and render it.

AFAIK there’s also a way of implementing CSG operations using the stencil buffer (well, union simply means rendering both, it gets more interesting
for intersections or subtraction).

There is a C++ library for doing that and if you type OpenGL® and CSG into Google, you will a bunch of stackoverflow
posts, as well as posts on this forum and some explanations on how the stencil technique I mentioned works.
And some papers:

there’s also a way of implementing CSG operations using the stencil buffer …

I’ve played with this stencil buffer stuff. Downloaded some code, put it on my windows desktop, and it worked! The code isn’t that complicated but I don’t really know why it works. I played with it quite a bit - commenting out things, changing order of commands. I even successfully integrated into a baseball simulation I wrote. Still don’t know how it works.

The OP might want to know that the stencil buffer approach is a visual effect only. It does not actually do a boolean operation which results in a new mesh.