在OGL的模块顶部,添加公共变量定义:
Public ShapeType As Integer Public Red As Double Public Green As Double Public Blue As Double Public Alpha As Double Public PtSize As Integer Public LineWidth As Integer
添加绘制过程:
Public Sub Display(WorkPic As PictureBox) glClearColor 1#, 1#, 1#, 0 glClear clrColorBufferBit GL.glBlendFunc sfSrcAlpha, dfOneMinusSrcAlpha GL.glEnable glcBlend glMatrixMode mmModelView glLoadIdentity GL.glOrtho 0, 1#, 0, 1#, -1#, 1# Select Case ShapeType Case 1: '点 GL.glPointSize OGL.PtSize GL.glColor4f Red, Green, Blue, Alpha GL.glBegin bmPoints GL.glVertex3f 0.5, 0.5, 0# GL.glEnd Case 2: '线 GL.glLineWidth OGL.LineWidth GL.glBegin bmLines GL.glColor4f Red, Green, Blue, Alpha GL.glVertex3f 0#, 0#, 0# GL.glVertex3f 1#, 1#, 0# GL.glEnd Case 3: '三角形 GL.glBegin bmTriangleFan GL.glColor4f Red, Green, Blue, Alpha GL.glVertex3f 0#, 0.2, 0# GL.glVertex3f 1#, 0.2, 0# GL.glVertex3f 0.5, 0.7, 0# GL.glEnd Case 4: '四边形 GL.glBegin bmQuads GL.glColor4f Red, Green, Blue, Alpha GL.glVertex2f 0.2, 0.2 GL.glVertex2f 0.8, 0.2 GL.glVertex2f 0.8, 0.8 GL.glVertex2f 0.2, 0.8 GL.glEnd Case 5: '多边形 GL.glBegin bmPolygon GL.glColor4f Red, Green, Blue, Alpha GL.glVertex2f 0.2, 0.2 GL.glVertex2f 0.8, 0.2 GL.glVertex2f 0.5, 0.5 GL.glVertex2f 0.8, 0.8 GL.glVertex2f 0.2, 0.8 GL.glVertex2f 0.2, 0.2 GL.glEnd End Select SwapBuffers WorkPic.hDC End Sub
双击点的按钮,添加以下代码:
Private Sub PointCmd_Click() GL.ShapeType = 1 Call OGL.Display(Me.WorkPic) End Sub
以此类推,补充完整直线、三角形、四边形、多边形的绘制命令代码,类型依次为2、3、4、5。
在窗体中,设计色彩设置和Alpha值设置的控件。为了使代码简洁,可以进入窗体代码区,增加一个代码如下的过程。滑块的最大值设置为100,最小值设置为0。
Public Sub IniColor() Red = Me.RedSlider.Value / 100 Green = Me.GreenSlider.Value / 100 Blue = Me.BlueSlider.Value / 100 Alpha = Me.AlphaSlider.Value / 100 End Sub
回到窗体,双击颜色Red的滑块,添加如下代码:
Private Sub RedSlider_Scroll() Call Me.IniColor Call OGL.Display(Me.WorkPic) End Sub
以此类推,双击Green滑块、Blue滑块、Alpha滑块,分别添加代码。
关于点的大小滑块和线的宽度滑块的设置,点的大小没有限制,而线宽最大值为10。设置好滑块后,进入如下的代码编写:
Private Sub PtSizeSlider_Scroll() OGL.PtSize = Me.PtSizeSlider.Value Call OGL.Display(Me.WorkPic) End Sub Private Sub LineWidthSlider_Scroll() OGL.LineWidth = Me.LineWidthSlider.Value Call OGL.Display(Me.WorkPic) End Sub
运行结果如图3-10所示。
图3-10 绘制点的运行结果