我使用的是SharpGL。
我试着用三种不同的颜色画出三个重叠的球体,如下所示-
但是,我得到的输出如下所示-
换句话说,gl.Color()
对绘制的对象没有任何影响。
- "我该如何解决这个问题"
源代码:
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;
}
}
1条答案
按热度按时间tzcvj98z1#
当启用照明(
GL_LIGHTING
)时,渲染颜色由材质参数(glMaterial
)定义。如果要使用glColor
定义颜色,则必须启用GL_COLOR_MATERIAL
并设置颜色材质参数(glColorMaterial
):也可以使用
GL.Material
设置材质颜色,例如: