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);
    }
4

2 回答 2

0

当我对此进行实验时,当我这样做时

gl.Color(1, 1, 1);

SharpGL 将其读取为字节而不是整数。所以我尝试像这样放入 float 或 double ,它工作得很好:

gl.Color(1.0f, 1.0f, 1.0f);
// or gl.Color(1.0, 1.0, 1.0);
于 2018-02-28T02:29:14.993 回答
0

您已GL_LIGHTING启用和GL_COLOR_MATERIAL禁用。在这种组合中,glColor调用无效。

在此处输入图像描述

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

于 2018-01-24T06:24:29.843 回答