“vb使用OpenGL 做3D游戏”运行无结果,请帮忙找出原因
网上下载解压的源代码运行正确(有可旋转的立方体),但我新建一个工程,复制代码,同样引用vbogl.tlb,窗体属性也完全一样,但运行结果只是空白窗体,没有立方体,百思不解,请帮忙找出原因。
'form1代码:+++++++++++++++++++++++++++++++++++++++++
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Keys(KeyCode) = True
End SubPrivate Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Keys(KeyCode) = False
End SubPrivate Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Quit = True
End Sub'module1代码:+++++++++++++++++++++++++++++++++++++++++
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Declare Function GetTickCount Lib "kernel32" () As LongGlobal Quit As Boolean
Global Keys(255) As Boolean
Global hrc&
Sub Main()
Dim frm As New Form1
'Dim OldTickCount&, NewTickCount&, FPS&OldTickCount = GetTickCount()
frm.Show
CreateGLWindow frm, 640, 480, 16, False
Do While Not Quit
DoEvents
If Keys(vbKeyEscape) Then
KillGLWindow
End
End If
DrawGLScene
SwapBuffers frm.hDC
'NewTickCount = GetTickCount()
'If NewTickCount - OldTickCount >= 1000 Then
' OldTickCount = NewTickCount
' frm.Caption = FPS & " fps"
' FPS = 0
'Else
' FPS = FPS + 1
'End If
Loop
Set frm = Nothing
End
End SubSub CreateGLWindow(frm As Form, Width As Integer, Height As Integer, Bits As Integer, FullsreenFlag As Boolean)
Dim PixelFormat As GLuint
Dim pfd As PIXELFORMATDESCRIPTOR
With pfd
.cColorBits = Bits
.cDepthBits = 16
.dwFlags = PFD_DRAW_TO_WINDOW Or PFD_SUPPORT_OPENGL Or PFD_DOUBLEBUFFER
.iLayerType = PFD_MAIN_PLANE
.iPixelType = PFD_TYPE_RGBA
.nSize = Len(pfd)
.nVersion = 1
End With
PixelFormat = ChoosePixelFormat(frm.hDC, pfd)
SetPixelFormat frm.hDC, PixelFormat, pfd
hrc = wglCreateContext(frm.hDC)
wglMakeCurrent frm.hDC, hrc
ResizeGLScene frm.ScaleWidth, frm.ScaleHeight
InitGL
End Sub
Sub InitGL()
glShadeModel smSmooth
glClearColor 0, 0, 0, 0
glClearDepth 1
glEnable glcDepthTest
glDepthFunc cfLEqual
glHint htPerspectiveCorrectionHint, hmNicest
End SubSub KillGLWindow()
If hrc Then
wglMakeCurrent 0, 0
wglDeleteContext hrc
hrc = 0
End If
End Sub
Sub DrawGLScene()
Static rQuadX As GLfloat, rQuadY As GLfloat, rQuadZ As GLfloat
Static mQuadX As GLfloat, mQuadY As GLfloat, mQuadZ As GLfloat
Static NotFirst As BooleanIf Not NotFirst Then
mQuadZ = -6
NotFirst = True
End IfglClear clrColorBufferBit Or clrDepthBufferBit
glLoadIdentityglTranslatef mQuadX, mQuadY, mQuadZglRotatef rQuadX, 1, 0, 0
glRotatef rQuadY, 0, 1, 0
glRotatef rQuadZ, 0, 0, 1glBegin bmQuads
glColor3f 0#, 1#, 0# ' Set The Color To Blue
glVertex3f 1#, 1#, -1# ' Top Right Of The Quad Top)
glVertex3f -1#, 1#, -1# ' Top Left Of The Quad Top)
glVertex3f -1#, 1#, 1# ' Bottom Left Of The Quad Top)
glVertex3f 1#, 1#, 1# ' Bottom Right Of The Quad Top)
glColor3f 1#, 0.5, 0# ' Set The Color To Orange
glVertex3f 1#, -1#, 1# ' Top Right Of The Quad Bottom)
glVertex3f -1#, -1#, 1# ' Top Left Of The Quad Bottom)
glVertex3f -1#, -1#, -1# ' Bottom Left Of The Quad Bottom)
glVertex3f 1#, -1#, -1# ' Bottom Right Of The Quad Bottom)
glColor3f 1#, 0#, 0# ' Set The Color To Red
glVertex3f 1#, 1#, 1# ' Top Right Of The Quad Front)
glVertex3f -1#, 1#, 1# ' Top Left Of The Quad Front)
glVertex3f -1#, -1#, 1# ' Bottom Left Of The Quad Front)
glVertex3f 1#, -1#, 1# ' Bottom Right Of The Quad Front)
glColor3f 1#, 1#, 0# ' Set The Color To Yellow
glVertex3f 1#, -1#, -1# ' Bottom Left Of The Quad Back)
glVertex3f -1#, -1#, -1# ' Bottom Right Of The Quad Back)
glVertex3f -1#, 1#, -1# ' Top Right Of The Quad Back)
glVertex3f 1#, 1#, -1# ' Top Left Of The Quad Back)
glColor3f 0#, 0#, 1# ' Set The Color To Blue
glVertex3f -1#, 1#, 1# ' Top Right Of The Quad Left)
glVertex3f -1#, 1#, -1# ' Top Left Of The Quad Left)
glVertex3f -1#, -1#, -1# ' Bottom Left Of The Quad Left)
glVertex3f -1#, -1#, 1# ' Bottom Right Of The Quad Left)
glColor3f 1#, 0#, 1# ' Set The Color To Violet
glVertex3f 1#, 1#, -1# ' Top Right Of The Quad Right)
glVertex3f 1#, 1#, 1# ' Top Left Of The Quad Right)
glVertex3f 1#, -1#, 1# ' Bottom Left Of The Quad Right)
glVertex3f 1#, -1#, -1# ' Bottom Right Of The Quad Right)
glEndIf Keys(vbKeyUp) Then rQuadX = rQuadX + 0.15
If Keys(vbKeyDown) Then rQuadX = rQuadX - 0.15
If Keys(vbKeyLeft) Then rQuadY = rQuadY + 0.15
If Keys(vbKeyRight) Then rQuadY = rQuadY - 0.15
If Keys(vbKeyPageUp) Then mQuadZ = mQuadZ + 0.005
If Keys(vbKeyPageDown) Then mQuadZ = mQuadZ - 0.005
If Keys(vbKeyA) Then mQuadX = mQuadX + 0.005
If Keys(vbKeyD) Then mQuadX = mQuadX - 0.005
If Keys(vbKeyW) Then mQuadY = mQuadY - 0.005
If Keys(vbKeyS) Then mQuadY = mQuadY + 0.005End SubSub ResizeGLScene(Width As GLsizei, Height As GLsizei)
If Height = 0 Then Height = 1
glViewport 0, 0, Width, Height
glMatrixMode mmProjection
glLoadIdentity
gluPerspective 45, Width / Height, 0.1, 100
glMatrixMode mmModelView
glLoadIdentity
End Sub
网上下载解压的源代码运行正确(有可旋转的立方体),但我新建一个工程,复制代码,同样引用vbogl.tlb,窗体属性也完全一样,但运行结果只是空白窗体,没有立方体,百思不解,请帮忙找出原因。
'form1代码:+++++++++++++++++++++++++++++++++++++++++
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Keys(KeyCode) = True
End SubPrivate Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Keys(KeyCode) = False
End SubPrivate Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Quit = True
End Sub'module1代码:+++++++++++++++++++++++++++++++++++++++++
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Declare Function GetTickCount Lib "kernel32" () As LongGlobal Quit As Boolean
Global Keys(255) As Boolean
Global hrc&
Sub Main()
Dim frm As New Form1
'Dim OldTickCount&, NewTickCount&, FPS&OldTickCount = GetTickCount()
frm.Show
CreateGLWindow frm, 640, 480, 16, False
Do While Not Quit
DoEvents
If Keys(vbKeyEscape) Then
KillGLWindow
End
End If
DrawGLScene
SwapBuffers frm.hDC
'NewTickCount = GetTickCount()
'If NewTickCount - OldTickCount >= 1000 Then
' OldTickCount = NewTickCount
' frm.Caption = FPS & " fps"
' FPS = 0
'Else
' FPS = FPS + 1
'End If
Loop
Set frm = Nothing
End
End SubSub CreateGLWindow(frm As Form, Width As Integer, Height As Integer, Bits As Integer, FullsreenFlag As Boolean)
Dim PixelFormat As GLuint
Dim pfd As PIXELFORMATDESCRIPTOR
With pfd
.cColorBits = Bits
.cDepthBits = 16
.dwFlags = PFD_DRAW_TO_WINDOW Or PFD_SUPPORT_OPENGL Or PFD_DOUBLEBUFFER
.iLayerType = PFD_MAIN_PLANE
.iPixelType = PFD_TYPE_RGBA
.nSize = Len(pfd)
.nVersion = 1
End With
PixelFormat = ChoosePixelFormat(frm.hDC, pfd)
SetPixelFormat frm.hDC, PixelFormat, pfd
hrc = wglCreateContext(frm.hDC)
wglMakeCurrent frm.hDC, hrc
ResizeGLScene frm.ScaleWidth, frm.ScaleHeight
InitGL
End Sub
Sub InitGL()
glShadeModel smSmooth
glClearColor 0, 0, 0, 0
glClearDepth 1
glEnable glcDepthTest
glDepthFunc cfLEqual
glHint htPerspectiveCorrectionHint, hmNicest
End SubSub KillGLWindow()
If hrc Then
wglMakeCurrent 0, 0
wglDeleteContext hrc
hrc = 0
End If
End Sub
Sub DrawGLScene()
Static rQuadX As GLfloat, rQuadY As GLfloat, rQuadZ As GLfloat
Static mQuadX As GLfloat, mQuadY As GLfloat, mQuadZ As GLfloat
Static NotFirst As BooleanIf Not NotFirst Then
mQuadZ = -6
NotFirst = True
End IfglClear clrColorBufferBit Or clrDepthBufferBit
glLoadIdentityglTranslatef mQuadX, mQuadY, mQuadZglRotatef rQuadX, 1, 0, 0
glRotatef rQuadY, 0, 1, 0
glRotatef rQuadZ, 0, 0, 1glBegin bmQuads
glColor3f 0#, 1#, 0# ' Set The Color To Blue
glVertex3f 1#, 1#, -1# ' Top Right Of The Quad Top)
glVertex3f -1#, 1#, -1# ' Top Left Of The Quad Top)
glVertex3f -1#, 1#, 1# ' Bottom Left Of The Quad Top)
glVertex3f 1#, 1#, 1# ' Bottom Right Of The Quad Top)
glColor3f 1#, 0.5, 0# ' Set The Color To Orange
glVertex3f 1#, -1#, 1# ' Top Right Of The Quad Bottom)
glVertex3f -1#, -1#, 1# ' Top Left Of The Quad Bottom)
glVertex3f -1#, -1#, -1# ' Bottom Left Of The Quad Bottom)
glVertex3f 1#, -1#, -1# ' Bottom Right Of The Quad Bottom)
glColor3f 1#, 0#, 0# ' Set The Color To Red
glVertex3f 1#, 1#, 1# ' Top Right Of The Quad Front)
glVertex3f -1#, 1#, 1# ' Top Left Of The Quad Front)
glVertex3f -1#, -1#, 1# ' Bottom Left Of The Quad Front)
glVertex3f 1#, -1#, 1# ' Bottom Right Of The Quad Front)
glColor3f 1#, 1#, 0# ' Set The Color To Yellow
glVertex3f 1#, -1#, -1# ' Bottom Left Of The Quad Back)
glVertex3f -1#, -1#, -1# ' Bottom Right Of The Quad Back)
glVertex3f -1#, 1#, -1# ' Top Right Of The Quad Back)
glVertex3f 1#, 1#, -1# ' Top Left Of The Quad Back)
glColor3f 0#, 0#, 1# ' Set The Color To Blue
glVertex3f -1#, 1#, 1# ' Top Right Of The Quad Left)
glVertex3f -1#, 1#, -1# ' Top Left Of The Quad Left)
glVertex3f -1#, -1#, -1# ' Bottom Left Of The Quad Left)
glVertex3f -1#, -1#, 1# ' Bottom Right Of The Quad Left)
glColor3f 1#, 0#, 1# ' Set The Color To Violet
glVertex3f 1#, 1#, -1# ' Top Right Of The Quad Right)
glVertex3f 1#, 1#, 1# ' Top Left Of The Quad Right)
glVertex3f 1#, -1#, 1# ' Bottom Left Of The Quad Right)
glVertex3f 1#, -1#, -1# ' Bottom Right Of The Quad Right)
glEndIf Keys(vbKeyUp) Then rQuadX = rQuadX + 0.15
If Keys(vbKeyDown) Then rQuadX = rQuadX - 0.15
If Keys(vbKeyLeft) Then rQuadY = rQuadY + 0.15
If Keys(vbKeyRight) Then rQuadY = rQuadY - 0.15
If Keys(vbKeyPageUp) Then mQuadZ = mQuadZ + 0.005
If Keys(vbKeyPageDown) Then mQuadZ = mQuadZ - 0.005
If Keys(vbKeyA) Then mQuadX = mQuadX + 0.005
If Keys(vbKeyD) Then mQuadX = mQuadX - 0.005
If Keys(vbKeyW) Then mQuadY = mQuadY - 0.005
If Keys(vbKeyS) Then mQuadY = mQuadY + 0.005End SubSub ResizeGLScene(Width As GLsizei, Height As GLsizei)
If Height = 0 Then Height = 1
glViewport 0, 0, Width, Height
glMatrixMode mmProjection
glLoadIdentity
gluPerspective 45, Width / Height, 0.1, 100
glMatrixMode mmModelView
glLoadIdentity
End Sub
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货