Home > Software engineering >  Unable to draw spheres with arbitrary color
Unable to draw spheres with arbitrary color

Time:06-27

I am absolutely new to OpenGL.

I am using enter image description here

However, I am getting output like the following -

enter image description here

In other words, the gl.Color() is having no effect on the drawn object.

  • How can I fix this?

Source Code:

public static class OpenGLhelper
{
    public static void Init(OpenGL gl)
    {            
        float[] mat_specular = { 1.0f, 1.0f, 1.0f, 1.0f };
        float[] mat_shininess = { 50.0f };
        float[] light_position = { 0.5f, 0.5f, 0.750f, 0.0f };
        gl.ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        gl.ShadeModel(OpenGL.GL_SMOOTH);

        gl.Material(OpenGL.GL_FRONT, OpenGL.GL_SPECULAR, mat_specular);
        gl.Material(OpenGL.GL_FRONT, OpenGL.GL_SHININESS, mat_shininess);
        gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_POSITION, light_position);

        gl.Enable(OpenGL.GL_LIGHTING);
        gl.Enable(OpenGL.GL_LIGHT0);
        gl.Enable(OpenGL.GL_DEPTH_TEST);

        //// Cull back faces.
        gl.Enable(OpenGL.GL_CULL_FACE);
        gl.CullFace(OpenGL.GL_BACK);
        gl.FrontFace(OpenGL.GL_CW);
    }

    private static void drawSphere(OpenGL gl, Point3d c, Rgb color, double r, int n)
    {
        int i, j;
        double theta1, theta2, theta3;
        Point3d e = new Point3d();
        Point3d p = new Point3d();

        if (c == null)
        {
            c = new Point3d(0, 0, 0);
        }

        double twoPi = Math.PI * 2;
        double piD2 = Math.PI / 2;
        if (r < 0)
            r = -r;
        if (n < 0)
            n = -n;
        if (n < 4 || r <= 0)
        {
            gl.Begin(OpenGL.GL_POINTS);
            gl.Color(color.Red, color.Green, color.Blue);
            gl.Vertex(c.X, c.Y, c.Z);
            gl.End();
            return;
        }

        for (j = 0; j < n / 2; j  )
        {
            theta1 = j * twoPi / n - piD2;
            theta2 = (j   1) * twoPi / n - piD2;

            gl.Begin(OpenGL.GL_QUAD_STRIP);
            for (i = 0; i <= n; i  )
            {
                theta3 = i * twoPi / n;

                e.X = Math.Cos(theta2) * Math.Cos(theta3);
                e.Y = Math.Sin(theta2);
                e.Z = Math.Cos(theta2) * Math.Sin(theta3);
                p.X = c.X   r * e.X;
                p.Y = c.Y   r * e.Y;
                p.Z = c.Z   r * e.Z;

                gl.Normal(e.X, e.Y, e.Z);
                gl.TexCoord(i / (double)n, 2 * (j   1) / (double)n);
                gl.Color(color.Red, color.Green, color.Blue);
                gl.Vertex(p.X, p.Y, p.Z);

                e.X = Math.Cos(theta1) * Math.Cos(theta3);
                e.Y = Math.Sin(theta1);
                e.Z = Math.Cos(theta1) * Math.Sin(theta3);
                p.X = c.X   r * e.X;
                p.Y = c.Y   r * e.Y;
                p.Z = c.Z   r * e.Z;

                gl.Normal(e.X, e.Y, e.Z);
                gl.TexCoord(i / (double)n, 2 * j / (double)n);
                gl.Color(color.Red, color.Green, color.Blue);
                gl.Vertex(p.X, p.Y, p.Z);
            }
            gl.End();
        }
    }

    public static void Display(OpenGL gl)
    {
        gl.Clear(OpenGL . GL_COLOR_BUFFER_BIT | OpenGL . GL_DEPTH_BUFFER_BIT);
        
        drawSphere(gl, new Point3d(0, 0, 0), new Rgb(1, 0, 0), 0.5, 20);
        drawSphere(gl, new Point3d(0, 0.5, 0), new Rgb(0, 1, 0), 0.5, 20);
        drawSphere(gl, new Point3d(0, -0.5, 0), new Rgb(0, 0, 1), 0.5, 20);

        gl.Flush();
    }

    public static void Reshape(OpenGL gl, int width, int height)
    {  
    }
}

public partial class SharpGLForm : Form
{
    private float rotation = 0.0f;

    public SharpGLForm()
    {
        InitializeComponent();

        OpenGL gl = openGLControl1.OpenGL;
        OpenGLhelper.Init(gl);
    }

    private void openGLControl_OpenGLDraw(object sender, RenderEventArgs e)
    {
        OpenGL gl = openGLControl1.OpenGL;

        gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);

        gl.LoadIdentity();
        gl.Translate(0.0f, 0.0f, -4.0f);  // Move into the screen
        gl.Rotate(rotation, 0.0f, 1.0f, 0.0f);

        OpenGLhelper.Display(gl);

        rotation  = 3.0f;
    }
}

CodePudding user response:

When lighting (GL_LIGHTING) is enabled, the render color is defined by the material parameters (glMaterial). If you want to define the color with glColor, you must enable GL_COLOR_MATERIAL and to set the color material parameters (glColorMaterial):

gl.Enable(OpenGL.GL_LIGHTING);
gl.Enable(OpenGL.GL_COLOR_MATERIAL);
gl.ColorMaterial(OpenGL.GL_FRONT_AND_BACK, OpenGL.GL_AMBIENT_AND_DIFFUSE);

Alternatively set the material color with GL.Material. e.g.:

float[] ambient_diffuse_color = { 1.0f, 0.0f, 0.0f, 1.0f }; // RED
GL.Material(OpenGL.GL_FRONT_AND_BACK, OpenGL.GL_AMBIENT_AND_DIFFUSE,
            ambient_diffuse_color);
  • Related