bigman's picture

Render CSG with custom objects

Hi!

I need to implement a boolean operation on geometry objects. It will be a part of my 3D apliaction. I am using OpenTK. I have found example in OpenTK examples describing render of csg (StencilCSG). I have included that example into my graphics engine and it works. Unfortunately it works only for geometry objects form that example (ChamferCube and SlicedSphere). When I use my custom objects (e.g. my version of sphere - I changed DrawOperandB() to draw my sphere) it will not work. I do not know what in object's definition need to be changed in order to obtain CSG functionality?

Example of sphere:

const double HalfPI = Math.PI * 0.5;
            double OneThroughPrecision = 1.0 / Precision;
            double TwoPIThroughPrecision = (Math.PI * 2.0 * OneThroughPrecision);
 
            double theta1, theta2, theta3, X, Y, Z;
            Vector3d Position;
            Vector3d Normal;
            GL.Begin(BeginMode.TriangleStrip);
            for (uint j = 0; j < Precision / 2; j++)
            {
                theta1 = (j * TwoPIThroughPrecision) - HalfPI;
                theta2 = ((j + 1) * TwoPIThroughPrecision) - HalfPI;
 
 
                for (uint i = 0; i <= Precision; i++)
                {
                    theta3 = i * TwoPIThroughPrecision;
 
                    X = Math.Cos(theta2) * Math.Cos(theta3);
                    Y = Math.Sin(theta2);
                    Z = Math.Cos(theta2) * Math.Sin(theta3);
                    Position.X = Center.X + Radius * X;
                    Position.Y = Center.Y + Radius * Y;
                    Position.Z = Center.Z + Radius * Z;
                    Normal = Vector3d.Normalize(Position);
 
                    GL.TexCoord2(i * OneThroughPrecision, 2.0f * (j + 1) * OneThroughPrecision);
                    GL.Normal3(Normal);
                    GL.Vertex3(Position);
 
                    X = Math.Cos(theta1) * Math.Cos(theta3);
                    Y = Math.Sin(theta1);
                    Z = Math.Cos(theta1) * Math.Sin(theta3);
                    Position.X = Center.X + Radius * X;
                    Position.Y = Center.Y + Radius * Y;
                    Position.Z = Center.Z + Radius * Z;
                    Normal = Vector3d.Normalize(Position);
 
                    GL.TexCoord2(i * OneThroughPrecision, 2.0f * j * OneThroughPrecision);
                    GL.Normal3(Normal);
                    GL.Vertex3(Position);
                }
 
            }
            GL.End();

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

Please define what exactly does not work. What have you tried so far?

As a sanity check, render the sphere normally and make sure that (a) it renders correctly and (b) it actually intersects with OperandA.

bigman's picture

Thank you for the answer. I greatly appreciate your help because I am stuck on this.

I used your code from example in my application and if I use your definition of geometry objects everything works well. If i use my sphere (the code that I put in my previous post) instead of sphere from your example as OperandB and I use the same OperandA (intersected object) I will obtain on scen only full object from OperandA but without intersection performed by OperandB. I put my sphere exactly in the same space position and with the same radius. If i render my sphere normally as object (not a part of csg procedure) it looks fine for me. I think the problem is in my definitions of geometry objects (e.g. sphere) because if I use objects form your example everything works but I do not know what is wrong with that definition. Do I need to re-write my definitions of geometry objects and inherit from DrawableShape?

the Fiddler's picture

Since the sphere is rendering correctly, the issue is likely some state that needs to be set before rendering.

DrawableShape is just a helper class, so you don't need to use it (it's not part of OpenTK.dll). However, it might be worth double-checking its implementation - does it set any additional OpenGL states that you'd need to copy to your code?

bigman's picture

I am checking the OpenGL state according to your suggestion.

bigman's picture

Hi!

The problem was in definition of sphere. My definition was incorrect but it rendered correctly. The problem appeared only when I was using render CSG procedure. Thank you for the support!