在 SharpGL 中着色

Coloring in SharpGL

我有一个球体,我想给它上色并在上面加上光效,所以当我给它打光时,球体原来的颜色没有了,取而代之的是照明光的颜色。

不知道是什么问题,帮帮我

这是我的代码:

enter code here
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using SharpGL;
using SharpGL.SceneGraph;

namespace SharpGLWinformsApplication1
{
    public partial class SharpGLForm : Form
    {
        public SharpGLForm()
        {
            InitializeComponent();
        }

        private void openGLControl_OpenGLDraw(object sender, RenderEventArgs e)
        {
            //  Get the OpenGL object.
            OpenGL gl = openGLControl.OpenGL;
            IntPtr quad=gl.NewQuadric();
            //  Clear the color and depth buffer.
            gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);

            gl.LoadIdentity();

            gl.Translate(0.0f, 0.0f, 12.0f);
            gl.Scale(0.1,0.1,0.1);


            //*
            gl.PushMatrix();
            gl.Color(1.0f, 1.0f, 0.0f); 
            gl.Sphere(quad, 15, 20, 20);
            gl.PopMatrix();


            //------------------------------------//

            gl.PushMatrix();
            gl.Color(0.0f, 1.0f, 0.0f);
            gl.Rotate(rotation, 0.0f, 1.0f, 0.0f);
            gl.Translate(0, 1, 30);
            gl.Sphere(quad, 12, 20, 20);
            gl.PopMatrix();

           rotation += 3.0f;
}
        private void openGLControl_OpenGLInitialized(object sender, EventArgs e)
    {
        //  TODO: Initialise OpenGL here.

        //  Get the OpenGL object.
        OpenGL gl = openGLControl.OpenGL;

        //  Set the clear color.
        gl.ClearColor(0, 0, 0, 0);
        gl.Enable(OpenGL.GL_LIGHTING);

        gl.Light(OpenGL.GL_LIGHT1, OpenGL.GL_AMBIENT, ambient);
        gl.Light(OpenGL.GL_LIGHT1, OpenGL.GL_DIFFUSE, diffuse);
        gl.Light(OpenGL.GL_LIGHT1, OpenGL.GL_SPECULAR, specular);
        gl.Light(OpenGL.GL_LIGHT1, OpenGL.GL_POSITION, new float[] { 0.0f, 2f, -30f, 0 });
        gl.Enable(OpenGL.GL_LIGHT1);
        gl.Light(OpenGL.GL_LIGHT1, OpenGL.GL_SPOT_CUTOFF, 180.0f);
    }

    private void openGLControl_Resized(object sender, EventArgs e)
    {
        //  TODO: Set the projection matrix here.

        //  Get the OpenGL object.
        OpenGL gl = openGLControl.OpenGL;

        //  Set the projection matrix.
        gl.MatrixMode(OpenGL.GL_PROJECTION);

        //  Load the identity.
        gl.LoadIdentity();

        gl.Viewport(0, 0, (int)Width, (int)Height);
        //  Create a perspective transformation.
        gl.Perspective(45.0f, (double)Width / (double)Height, 1, 200.0);

        //  Use the 'look at' helper function to position and aim the camera.
        gl.LookAt(0, -3, -10, 0, 0, 0, 0, 1, 0);

        //  Set the modelview matrix.
        gl.MatrixMode(OpenGL.GL_MODELVIEW);
    }

    private float rotation = 0.0f;        

    private GLColor ambient = new GLColor(1, 1, 1, 1f);
    private GLColor diffuse = new GLColor(1, 1, 1, 1);
    private GLColor specular = new GLColor(1, 1, 1, 1);


    private GLColor shadowColor = new GLColor(0, 0, 0, 0.4f);
}

}

我认为您需要使用 gl.Materials() 设置材质。否则 opengl 将采用表面不反射光的球体。