aklev's picture

Glu.gluSphere and Glu.gluСylinder

I alter old programs on new alternatives with usage OpenTK.
Where to take clones of Glu.gluSphere objects and Glu.gluСylinder so not to alter a great many of the subprograms linked to a full-sphere and the cylinder?
It is necessary to save all old modes of colouring...



Comment viewing options

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

Add a reference to OpenTK.Compatibility.dll. This contains Glu bindings and Tao bindings so you can continue to use existing programs without modifications (this is why it's called "compatibility").

aklev's picture

And what to do if it would be desirable to do without OpenTK.Compatibility.dll ?


the Fiddler's picture

It is very easy to construct spheres and cylinders using simple math. GLU is officially deprecated in OpenGL 3.0, so that's the best way forward.

There is code available for spheres in these forums (please use the search function, I don't have the link handy) and cylinders are very simple to derive from that.

aklev's picture

Yes. I saw these codes at a forum and in other places...
Here common versions (VB.NET) for a Sphere and Cylinder(cone).
They work quickly enough, draw correctly, BUT NOT SO INTERREACT With IRRADIATING AS Glu.gluSphere and Glu.gluСylinder ...


I do not have initial codes Glu.gluSphere and Glu.gluСylinder ... I do not know where to discover them ...
But I know that many tasks use it.

Can we joint force of a forum we will discover old codes Glu.gluSphere and Glu.gluСylinder or let's update that I show here (it a little works):

    Public Sub DrawSphere(ByVal Center As Vector3, ByVal Radius As Single, ByVal Precision As Integer)
        Dim i, j As Integer
        Dim ai, aj, aj1 As Double
        Dim  HalfPI As Double = Math.PI * 0.5
        If (Precision Mod 2) > 0 Then Precision = Precision + 1
        If Radius < 0.0F Then
            Radius = -Radius
        End If
        If Radius = 0.0F Then Radius = 1.0F
        If Precision < 8 Then Precision = 8
        Dim OneThroughPrecision As Double = 1.0 / Convert.ToDouble(Precision)
        Dim TwoPIThroughPrecision As Double = 2.0 * Math.PI * OneThroughPrecision
        Dim theta1, theta2, theta3 As Double
        Dim Normal As Vector3
        Dim Position As Vector3
        For j = 0 To Precision \ 2 - 1
            aj = Convert.ToDouble(j)
            aj1 = Convert.ToDouble(j + 1)
            theta1 = (aj * TwoPIThroughPrecision) - HalfPI
            theta2 = (aj1 * TwoPIThroughPrecision) - HalfPI
            For i = 0 To Precision
                ai = Convert.ToDouble(i)
                theta3 = ai * TwoPIThroughPrecision
                Normal.X = Convert.ToSingle(Math.Cos(theta2) * Math.Cos(theta3))
                Normal.Y = Convert.ToSingle(Math.Sin(theta2))
                Normal.Z = Convert.ToSingle(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.TexCoord2(ai * OneThroughPrecision, 2.0 * aj1 * OneThroughPrecision)
                Normal.X = Convert.ToSingle(Math.Cos(theta1) * Math.Cos(theta3))
                Normal.Y = Convert.ToSingle(Math.Sin(theta1))
                Normal.Z = Convert.ToSingle(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.TexCoord2(ai * OneThroughPrecision, 2.0 * aj * OneThroughPrecision)
            Next i
        Next j
    End Sub
    '   Create a cone/cylinder uncapped between end points p1, p2
    '   radius r1, r2, and precision m
    '   Create the cylinder between theta1 and theta2 in radians
    Public Sub CreateCone(ByVal p1 As XYZ, ByVal p2 As XYZ, ByVal r1 As Double, ByVal r2 As Double, ByVal m As Integer, ByVal theta1 As Double, ByVal theta2 As Double)
        Dim i As Integer
        Dim j As Integer
        Dim theta, am, ai As Double
        Dim n As XYZd
        Dim p As XYZd
        Dim q As XYZd
        Dim perp As XYZd
        am = Convert.ToDouble(m)
        ' Normal pointing from p1 to p2 
        n.x = Convert.ToDouble(p1.x - p2.x)
        n.y = Convert.ToDouble(p1.y - p2.y)
        n.z = Convert.ToDouble(p1.z - p2.z)
        '      Create two perpendicular vectors perp and q
        '      on the plane of the disk
        perp = n
        If n.x = 0.0 AndAlso n.z = 0.0 Then
            n.x += 1.0
            n.y += 1.0
        End If
        Call CROSSPROD(perp, n, q)
        Call CROSSPROD(n, q, perp)
        Call Normalise(perp)
        Call Normalise(q)
        For i = 0 To m
            ai = Convert.ToDouble(i)
            theta = theta1 + ai * (theta2 - theta1) / am
            n.x = Math.Cos(theta) * perp.x + Math.Sin(theta) * q.x
            n.y = Math.Cos(theta) * perp.y + Math.Sin(theta) * q.y
            n.z = Math.Cos(theta) * perp.z + Math.Sin(theta) * q.z
            Call Normalise(n)
            p.x = p2.x + r2 * n.x
            p.y = p2.y + r2 * n.y
            p.z = p2.z + r2 * n.z
            GL.Normal3(n.x, n.y, n.z)
            GL.TexCoord2(ai / am, 1.0)
            GL.Vertex3(p.x, p.y, p.z)
            p.x = p1.x + r1 * n.x
            p.y = p1.y + r1 * n.y
            p.z = p1.z + r1 * n.z
            GL.Normal3(n.x, n.y, n.z)
            GL.TexCoord2(ai / am, 0.0)
            GL.Vertex3(p.x, p.y, p.z)
        Next i
    End Sub
    Public Sub CROSSPROD(ByVal a As XYZd, ByVal b As XYZd, ByRef c As XYZd)
        c.x = a.y * b.z - a.z * b.y
        c.y = a.z * b.x - a.x * b.z
        c.z = a.x * b.y - a.y * b.x
    End Sub
    Public Sub Normalise(ByRef c As XYZd)
        Dim v As Double
        v = c.x * c.x + c.y * c.y + c.z * c.z
        If v = 0.0 Then Exit Sub
        v = Math.Sqrt(v)
        c.x = c.x / v
        c.y = c.y / v
        c.z = c.z / v
    End Sub


the Fiddler's picture

Mesa3d contains an open source implementation of GLU. The code is written in C and is licensed under the Library General Public License (an older version of the LGPL).

You can re-implement Glu.gluSphere with a small modification to your code: use two parameters (int slices, int stacks) instead of int precision.

aklev's picture

I will try.