我想在 SharpGl 中画线,但这段代码不起作用!
void Line_DDA(OpenGL gl,int X0, int Y0, int Xend, int Yend)
{
gl.LineWidth(2.5f);
gl.Color(1.0, 0.0, 0.0);
gl.Begin(OpenGL.GL_LINES);
int dx = Xend - X0;
int dy = Yend - Y0;
int steps, k;
float Xinc, Yinc;
float x = X0;
float y = Y0;
if (Math.Abs(dx) > Math.Abs(dy))
steps = Math.Abs(dx);
else
steps = Math.Abs(dy);
float fdx = (float)dx;
float fdy = (float)dy;
float fsteps = (float)steps;
Xinc = fdx / fsteps;
Yinc = fdy / fsteps;
gl.Vertex((int)x, (int)y);
for (k = 0; k < steps; k++)
{
x += Xinc;
y += Yinc;
gl.Vertex((int)x, (int)y);
}
gl.End();
}
当我使用
gl.Vertex(10, 100);
gl.Vertex(110, 110);
这是工作!
编辑:
这是我的代码中的调用块:
private void openGLControl_OpenGLDraw(object sender, PaintEventArgs e)
{
// Get the OpenGL object.
OpenGL gl = openGLControl.OpenGL;
// Clear the color and depth buffer.
gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
// Load the identity matrix.
gl.LoadIdentity();
Line_DDA(gl, int.Parse(txtLineX1.Text), int.Parse(txtLineY1.Text), int.Parse(txtLineX2.Text), int.Parse(txtLineY2.Text));
//drawLine(gl, 110, 120, 100, 100);
}
为什么会这样?