Search code examples
c#.netwpfopenglsharpgl

SharpGL.WPF gl.Color not working


I am using SharpGL.WPF for displaying graphs of several mathematical equations. However, the gl.Color() is not working for me.

Here is my draw method which I call in the OpenGLDraw event:

    private void DrawLinearFunction(OpenGL gl)
    {
        /*
         * f(x) = x + 2;
         * Let x be 4, then y = 6 (4, 6)
         * Let x be -5, then y = -3 (-5, -3)
         * */

        gl.PointSize(1.0f);
        gl.Begin(OpenGL.GL_POINTS);
        gl.Color(255, 0, 0); // <---- this doesn't work
        for (float x = -(GRAPH_LIMIT - 5); x <= (GRAPH_LIMIT - 5); x+=LINE_SMOOTHNESS)
        {
            gl.Vertex(x, x + 2);
        }
        gl.End();
    }

And here is my initialization code:

    private void OpenGLControl_OpenGLInitialized(object sender, SharpGL.SceneGraph.OpenGLEventArgs args)
    {
        OpenGL glInstance = args.OpenGL;

        glInstance.Enable(OpenGL.GL_DEPTH_TEST);
        //glInstance.Enable(OpenGL.GL_COLOR_MATERIAL);

        float[] global_ambient = new float[] { 0.5f, 0.5f, 0.5f, 1.0f };
        float[] light0pos = new float[] { 0.0f, 5.0f, 10.0f, 1.0f };
        float[] light0ambient = new float[] { 0.2f, 0.2f, 0.2f, 1.0f };
        float[] light0diffuse = new float[] { 0.3f, 0.3f, 0.3f, 1.0f };
        float[] light0specular = new float[] { 0.8f, 0.8f, 0.8f, 1.0f };

        float[] lmodel_ambient = new float[] { 0.2f, 0.2f, 0.2f, 1.0f };
        glInstance.LightModel(OpenGL.GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);

        glInstance.LightModel(OpenGL.GL_LIGHT_MODEL_AMBIENT, global_ambient);
        glInstance.Light(OpenGL.GL_LIGHT0, OpenGL.GL_POSITION, light0pos);
        glInstance.Light(OpenGL.GL_LIGHT0, OpenGL.GL_AMBIENT, light0ambient);
        glInstance.Light(OpenGL.GL_LIGHT0, OpenGL.GL_DIFFUSE, light0diffuse);
        glInstance.Light(OpenGL.GL_LIGHT0, OpenGL.GL_SPECULAR, light0specular);
        glInstance.Enable(OpenGL.GL_LIGHTING);
        glInstance.Enable(OpenGL.GL_LIGHT0);

        glInstance.ShadeModel(OpenGL.GL_SMOOTH);
    }

Solution

  • As I was experimenting on this, when I do

    gl.Color(1, 1, 1);
    

    SharpGL reads it as bytes and not integers. So I tried putting in float or double like this and it works just fine:

    gl.Color(1.0f, 1.0f, 1.0f);
    // or gl.Color(1.0, 1.0, 1.0);