SharpGL无法在Button Click上呈现

时间:2017-07-28 23:42:09

标签: c# winforms opengl sharpgl

我正在winform上测试SharpGL样本,它在openGLDraw事件上渲染三角形。但是,当我尝试使用按钮渲染它时,单击它不起作用。是否应该在openGLDraw事件之外渲染?以下是代码:

private void DrawScene()
{
    OpenGL gl = openGLControl.OpenGL;
    gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
    gl.LoadIdentity();
    gl.Begin(OpenGL.GL_TRIANGLES);
    gl.Color(1.0f, 0.0f, 0.0f);
    gl.Vertex(0.0f, 1.0f, 0.0f);
    gl.Color(0.0f, 1.0f, 0.0f);
    gl.Vertex(-1.0f, -1.0f, 1.0f);
    gl.Color(0.0f, 0.0f, 1.0f);
    gl.Vertex(1.0f, -1.0f, 1.0f);
    gl.Color(1.0f, 0.0f, 0.0f);
    gl.Vertex(0.0f, 1.0f, 0.0f);
    gl.Color(0.0f, 0.0f, 1.0f);
    gl.Vertex(1.0f, -1.0f, 1.0f);
    gl.Color(0.0f, 1.0f, 0.0f);
    gl.Vertex(1.0f, -1.0f, -1.0f);
    gl.Color(1.0f, 0.0f, 0.0f);
    gl.Vertex(0.0f, 1.0f, 0.0f);
    gl.Color(0.0f, 1.0f, 0.0f);
    gl.Vertex(1.0f, -1.0f, -1.0f);
    gl.Color(0.0f, 0.0f, 1.0f);
    gl.Vertex(-1.0f, -1.0f, -1.0f);
    gl.Color(1.0f, 0.0f, 0.0f);
    gl.Vertex(0.0f, 1.0f, 0.0f);
    gl.Color(0.0f, 0.0f, 1.0f);
    gl.Vertex(-1.0f, -1.0f, -1.0f);
    gl.Color(0.0f, 1.0f, 0.0f);
    gl.Vertex(-1.0f, -1.0f, 1.0f);
    gl.End();
}

private void openGLControl_OpenGLDraw(object sender, RenderEventArgs e)
{
    //this.DrawScene(); //Working
}

private void button1_Click(object sender, EventArgs e)
{
    this.DrawScene(); //Not working
}

1 个答案:

答案 0 :(得分:0)

只需添加openGLControl.Refresh();在DrawScene()之后。过去几天这让我很愚弄。我在阅读完源代码后发现了它。 :d

private void button1_Click(object sender, EventArgs e)
{
    this.DrawScene(); //Not working
    openGLControl.Refresh(); 
}