kdb's picture

Display lists and quadrics

Hi,

I haven't programmed in OpenGL in a while, but I do remember the basics of creating display lists and quadrics. I've tried to use them using .NET calls and they're not working. Are they built yet? If they are, could someone provide an example (e.g. a call to GLu.Cylinder or something)?

Kyle


Comments

Comment viewing options

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

The code has been built, but it's untested.

What doesn't work? Is there an error message? Can you upload a small test case?

kdb's picture

I'm just not sure how to call the function properly - what does the first argument (quad) do?

the Fiddler's picture

The first parameter is a handle to a quadric object, which you generate with Glu.NewQuadric.

Edit: Check this tutorial on quadrics. It's for java, but the syntax is easy to translate.

Technolithic's picture

I'm having the same issue with this.
Glu.QuadraticDrawStyle(int quad, QuadricDrawStyle.Fill)

The first parameter appears to accept an integer.
I'm running OpenTK 0.9.0 with C# on Visual Studio and it's setup to do auto-lookup so it provides me with a list of all method and property declarations as I type.

How does a handle to an object work, if it's an integer?
Can someone provide an example (or a link) using C# and OpenTK?

Just to see if it would work anyhow, I compiled and ran the code, but the Glu.GluQuadric object I used for the 1st parameter caused a runtime failure.

Inertia's picture

Seems you are right, Glu.NewQuadric should return IntPtr rather than int. Glu is not very flexible when it comes to parameters (topology and texcoords), so it's way better to either generate geometry by a custom function or import it from a modelling application. However that answer isn't helping right away, so here's a quick port of pascal code to tesselate a sphere to C# (didn't test extensively, but the code does output a sphere so I guess the port is ok )

  public void DrawSphere( Vector3 Center, float Radius, uint Precision )
        {
            if ( Radius < 0f )
                Radius = -Radius;
            if ( Radius == 0f )
                throw new DivideByZeroException( "DrawSphere: Radius cannot be 0f." );
            if ( Precision == 0 )
                throw new DivideByZeroException( "DrawSphere: Precision of 8 or greater is required." );
 
            const float HalfPI = (float) ( Math.PI * 0.5 );
            float OneThroughPrecision = 1.0f / Precision;
            float TwoPIThroughPrecision = (float) ( Math.PI * 2.0 * OneThroughPrecision);
 
            float theta1, theta2, theta3;
            Vector3 Normal, Position;
 
            for ( uint j = 0 ; j < Precision / 2 ; j++ )
            {
                theta1 = ( j * TwoPIThroughPrecision ) - HalfPI;
                theta2 = ( ( j + 1 ) * TwoPIThroughPrecision ) - HalfPI;
 
                GL.Begin( BeginMode.TriangleStrip );
                for ( uint i = 0 ; i <= Precision ; i++ )
                {
                    theta3 = i * TwoPIThroughPrecision;
 
                    Normal.X = (float) ( Math.Cos( theta2 ) * Math.Cos( theta3 ) );
                    Normal.Y = (float) Math.Sin( theta2 );
                    Normal.Z = (float) ( Math.Cos( theta2 ) * Math.Sin( theta3 ) );
                    Position.X = Center.X + Radius * Normal.X;
                    Position.Y = Center.Y + Radius * Normal.Y;
                    Position.Z = Center.Z + Radius * Normal.Z;
 
                    GL.Normal3( Normal );
                    GL.TexCoord2( i * OneThroughPrecision, 2.0f * ( j + 1 ) * OneThroughPrecision );
                    GL.Vertex3( Position );
 
                    Normal.X = (float) ( Math.Cos( theta1 ) * Math.Cos( theta3 ) );
                    Normal.Y = (float) Math.Sin( theta1 );
                    Normal.Z = (float) ( Math.Cos( theta1 ) * Math.Sin( theta3 ) );
                    Position.X = Center.X + Radius * Normal.X;
                    Position.Y = Center.Y + Radius * Normal.Y;
                    Position.Z = Center.Z + Radius * Normal.Z;
 
                    GL.Normal3( Normal );
                    GL.TexCoord2( i * OneThroughPrecision, 2.0f * j * OneThroughPrecision );
                    GL.Vertex3( Position );
                }
                GL.End( );
            }
        }
Technolithic's picture

Thanks, Inertia, the spheres function works nicely. I just need to figure out how to change their colors and model them without showing the occasional facing artifacts where it appears to inversely texturize the spheres, leaving a funny effect when rotating.

Technolithic's picture

OK, to get rid of the facing artifacts, I enabled face culling using: GL.Enable(EnableCap.CullFace).
Now my project renders those spheres without goofing it up and they look great!

Thanks again, Inertia.

the Fiddler's picture

Hm, the typemap correctly maps QuadricObj to IntPtr - the int parameter looks like a bug in the generator.

I'll see into that, however I do think that using code like the above is better (more flexible, possibly faster) than using Glu quadrics.

Technolithic's picture

I think the OpenTK call lists are a bit faulty still, unless they can't be used for lighting. It seems that the first time I call the list for the default lighting on a sphere object, it works fine, but when my program changes the CallList value to use a different color matrix for particular spheres it goes crazy and does them all the same color. When I remove the call to GL.CallList and go back to setting each lighting matrix manually, it works fine; albeit probably less efficient than it could be.

Anyhow, I can get by with doing it this way for a while. Just letting you know there's probably an issue with GL.CallList() as well.

Technolithic's picture

I'd like to add that I'm using GL.CallList() to display tons of spheres and it seems to work ok for that... for now. I'm only using 1 object, spheres, in the list. I'll have to create other objects as well. I'm sure I'll find out if there's a problem after I generate the code for the graphics to represent the other objects on screen.