0

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);
    }
Dale Julian
  • 1,560
  • 18
  • 35

2 Answers2

0

You have GL_LIGHTING enabled, and GL_COLOR_MATERIAL disabled. In this combination, glColor calls have no effect.

enter image description here

See https://www.khronos.org/opengl/wiki/How_lighting_works

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • I removed the gl.Enable(OpenGL.GL_LIGHTING) as well as gl.Enable(OpenGL.GL_LIGHT0) and everything turned to purple and glColor still has no effect. Then I tried enabling the GL_COLOR_MATERIAL with the LIGHTING enabled. Same result, everyrthing turned to purple and glColor still has no effect. – Dale Julian Jan 24 '18 at 06:31
0

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);
Dale Julian
  • 1,560
  • 18
  • 35