渲染三角形:
private void OpenGLControl_OpenGLDraw(object sender, SharpGL.SceneGraph.OpenGLEventArgs args)
{
OpenGL gL = GLControl.OpenGL;
gL.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
gL.LoadIdentity();
maxX = Math.Max(AX, Math.Max(BX, CX));
maxY = Math.Max(AY, Math.Max(BY, CY));
minX = Math.Min(AX, Math.Min(BX, CX));
minY = Math.Min(AX, Math.Min(BX, CX));
maxZ = Math.Max(AZ, Math.Max(BZ, CZ));
minZ = Math.Min(AZ, Math.Min(BZ, CZ));
double figureWidht = maxX - minX;
double figureHeight = maxY - minY;
double figureSquare = figureWidht * figureHeight;
double viewPortSquare = GLControl.Width * GLControl.Height;
gL.Translate(-figureWidht / 2, -figureHeight / 2, -6);
gL.Begin(OpenGL.GL_LINES);
gL.Color(1.0F, 1.0F, 1.0F);
gL.Vertex((float)AX, (float)AY, (float)AZ);
gL.Vertex((float)BX, (float)BY, (float)BZ);
gL.Vertex((float)BX, (float)BY, (float)BZ);
gL.Vertex((float)CX, (float)CY, (float)CZ);
gL.Vertex((float)CX, (float)CY, (float)CZ);
gL.Vertex((float)AX, (float)AY, (float)AZ);
gL.End();
}
如何使图形始终适合OpenGLControl的视口?即如果图形大于opengl窗口,则调整到这个窗口,使所有轴的间距大致相同,如果小于,则相应变大。