读书人

“vb施用OpenGL 做3D游戏”运行无结果

发布时间: 2012-12-27 10:17:10 作者: rapoo

“vb使用OpenGL 做3D游戏”运行无结果,请帮忙找出原因
“vb使用OpenGL 做3D游戏”运行无结果,请帮忙找出原因
网上下载解压的源代码运行正确(有可旋转的立方体),但我新建一个工程,复制代码,同样引用vbogl.tlb,窗体属性也完全一样,但运行结果只是空白窗体,没有立方体,百思不解,请

帮忙找出原因。
'form1代码:+++++++++++++++++++++++++++++++++++++++++
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Keys(KeyCode) = True
End Sub

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Keys(KeyCode) = False
End Sub

Private 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 Long

Global 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 Sub

Sub 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 Sub

Sub 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 Boolean

If Not NotFirst Then
mQuadZ = -6
NotFirst = True
End If

glClear clrColorBufferBit Or clrDepthBufferBit
glLoadIdentity

glTranslatef mQuadX, mQuadY, mQuadZ

glRotatef rQuadX, 1, 0, 0
glRotatef rQuadY, 0, 1, 0
glRotatef rQuadZ, 0, 0, 1

glBegin 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)
glEnd

If 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.005

End Sub

Sub 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
[解决办法]
我终于知到错在哪里了!!!!!!
[解决办法]
我终于知到错在哪里了!!!!!!
[解决办法]
AutoRedraw?
[解决办法]
哪里错了?

读书人网 >VB

热点推荐