Super-simple OpenGL setup...no triangle visible?

BlitzMax Forums/BlitzMax OpenGL Programming/Super-simple OpenGL setup...no triangle visible?

I can't figure out why there is no triangle visible here. I believe I did everything I need to set it up properly.

I need to set up the whole OpenGL environment myself, so I can access all the features I want. Any help pointing out an error in this code would be appreciated:

This BlitzPlus code works:
;Constants
Const off_nSize=0;  WORD
Const off_nVersion=2;  WORD
Const off_dwFlags=4;  DWORD
Const off_iPixelType=8;  Byte
Const off_cColorBits=9;  Byte
Const off_cRedBits=10;  Byte
Const off_cRedShift=11;  Byte
Const off_cGreenBits=12;  Byte
Const off_cGreenShift=13;  Byte
Const off_cBlueBits=14;  Byte
Const off_cBlueShift=15;  Byte
Const off_cAlphaBits=16;  Byte
Const off_cAlphaShift=17;  Byte
Const off_cAccumBits=18;  Byte
Const off_cAccumRedBits=19;  Byte
Const off_cAccumGreenBits=20;  Byte
Const off_cAccumBlueBits=21;  Byte
Const off_cAccumAlphaBits=22;  Byte
Const off_cDepthBits=23;  Byte
Const off_cStencilBits=24;  Byte
Const off_cAuxBuffers=25;  Byte
Const off_iLayerType=26;  Byte
Const off_bReserved=27;  Byte
Const off_dwLayerMask=28;  DWORD
Const off_dwVisibleMask=32;  DWORD
Const off_dwDamageMask=36;  DWORD
Const PFD_DOUBLEBUFFER = $00000001
Const PFD_DRAW_TO_WINDOW = $00000004
Const PFD_SUPPORT_OPENGL = $00000020

;Const PFD_TYPE_RGBA=

Include "constants\win32.const"
Include "constants\opengl.const"

;Create Window
win=CreateWindow("OpenGL Window",200,200,640,480,0,3)

;Setup pixel format
hwnd=QueryObject(win,1)
hdc=GetDC(hwnd)
lpPixelFormat=CreateBank(40)
dwFlags=PFD_SUPPORT_OPENGL Or PFD_DOUBLEBUFFER Or PFD_DRAW_TO_WINDOW
PokeShort lpPixelFormat,off_nSize,BankSize(lpPixelFormat)
PokeShort lpPixelFormat,off_nVersion,1
PokeInt lpPixelFormat,off_dwFlags,dwFlags
PokeByte lpPixelFormat,off_iPixelType,PFD_TYPE_RGBA
PokeByte lpPixelFormat,off_cColorBits,16
PokeByte lpPixelFormat,off_cDepthBits,16
PokeByte lpPixelFormat,off_cAccumBits,16
PokeByte lpPixelFormat,off_cStencilBits,8
PokeInt lpPixelFormat,28,PFD_MAIN_PLANE
PokeByte lpPixelFormat,off_cBlueBits,8
PokeByte lpPixelFormat,off_cGreenBits,8
PokeByte lpPixelFormat,off_cRedBits,8
lPixelFormat=ChoosePixelFormat(hDC,lpPixelFormat)
If Not lPixelFormat RuntimeError "ChoosePixelFormat() failed"
result=SetPixelFormat(hDC,lPixelFormat,lpPixelFormat)

;Create OpenGL Context
hrc=wglCreateContext(hdc)
If Not hrc Notify "Failed to create context."
wglMakeCurrent hdc,hrc

;Program Loop
Repeat
	
	;Setup Viewport
	w=ClientWidth(win)
	h=ClientHeight(win)
	glViewport 0.0,0.0,w,h
	glClearColor 0.0,0.0,1.0,0.0
	glClear GL_DEPTH_BUFFER_BIT+GL_COLOR_BUFFER_BIT

	glMatrixMode GL_PROJECTION
	glLoadIdentity()
	glPushMatrix()
	gluPerspective 45.0,Float(w)/Float(h),1.0,100.0

	;Draw Triangle
	glBegin GL_TRIANGLES
	glVertex3f 0.0,0.0,-10.0
	glVertex3f 1.0,1.0,-10.0
	glVertex3f 1.0,0.0,-10.0
	glEnd

	glPopMatrix()

	;Flip buffer
	SwapBuffers hdc

	;Check For WindowClose Event	
	Select WaitEvent()
		Case EVENT_KEYDOWN
			End
		Case $803
			End
	End Select
	
Forever


This BlitzMax code does not work:
Include "constants\opengl.bmx"
Include "constants\win32.bmx"

'Constants
Const off_nSize=0'  WORD
Const off_nVersion=2'  WORD
Const off_dwFlags=4'  DWORD
Const off_iPixelType=8'  Byte
Const off_cColorBits=9'  Byte
Const off_cRedBits=10'  Byte
Const off_cRedShift=11'  Byte
Const off_cGreenBits=12'  Byte
Const off_cGreenShift=13'  Byte
Const off_cBlueBits=14'  Byte
Const off_cBlueShift=15'  Byte
Const off_cAlphaBits=16'  Byte
Const off_cAlphaShift=17'  Byte
Const off_cAccumBits=18'  Byte
Const off_cAccumRedBits=19'  Byte
Const off_cAccumGreenBits=20'  Byte
Const off_cAccumBlueBits=21'  Byte
Const off_cAccumAlphaBits=22'  Byte
Const off_cDepthBits=23'  Byte
Const off_cStencilBits=24'  Byte
Const off_cAuxBuffers=25'  Byte
Const off_iLayerType=26'  Byte
Const off_bReserved=27'  Byte
Const off_dwLayerMask=28'  DWORD
Const off_dwVisibleMask=32'  DWORD
Const off_dwDamageMask=36'  DWORD
Const PFD_DOUBLEBUFFER = $00000001
Const PFD_DRAW_TO_WINDOW = $00000004
Const PFD_SUPPORT_OPENGL = $00000020
Const PFD_TYPE_RGBA=0

'Create Window
win:tgadget=CreateWindow("OpenGL Window",200,200,640,480,Null,3)

'Setup pixel format
hwnd=QueryGadget(win,QUERY_HWND_CLIENT)
hdc=GetDC(hwnd)
lpPixelFormat:tbank=CreateBank(40)
dwFlags=PFD_SUPPORT_OPENGL Or PFD_DOUBLEBUFFER Or PFD_DRAW_TO_WINDOW
PokeShort lpPixelFormat,off_nSize ,BankSize(lpPixelFormat)
PokeShort lpPixelFormat,off_nVersion,1
PokeInt lpPixelFormat,off_dwFlags,dwFlags
PokeByte lpPixelFormat,off_iPixelType,PFD_TYPE_RGBA
PokeByte lpPixelFormat,off_cColorBits,16
PokeByte lpPixelFormat,off_cDepthBits,16
PokeByte lpPixelFormat,off_cAccumBits,16
PokeByte lpPixelFormat,off_cStencilBits,8
PokeInt lpPixelFormat,28,PFD_MAIN_PLANE
PokeByte lpPixelFormat,off_cBlueBits,8
PokeByte lpPixelFormat,off_cGreenBits,8
PokeByte lpPixelFormat,off_cRedBits,8
lPixelFormat=ChoosePixelFormat(hDC,BankBuf(lpPixelFormat))
If Not lPixelFormat RuntimeError "ChoosePixelFormat() failed"
result=SetPixelFormat(hDC,lPixelFormat,BankBuf(lpPixelFormat))
lpPixelFormat=Null

'Create OpenGL Context
hrc=wglCreateContext(hdc)
If Not hrc Notify "Failed to create context."
wglMakeCurrent hdc,hrc

'Program Loop
Repeat
	
	'Setup Viewport
	w=ClientWidth(win)
	h=ClientHeight(win)
	glViewport 0.0,0.0,w,h
	glClearColor 0.0,0.0,1.0,0.0
	glClear GL_DEPTH_BUFFER_BIT+GL_COLOR_BUFFER_BIT

	glMatrixMode GL_PROJECTION
	glLoadIdentity()
	glPushMatrix()
	gluPerspective 45.0,Float(w)/Float(h),1.0,100.0

	'Draw Triangle
	glBegin GL_TRIANGLES
	glVertex3f 0.0,0.0,-10.0
	glVertex3f 1.0,1.0,-10.0
	glVertex3f 1.0,0.0,-10.0
	glEnd

	glpopmatrix()

	'Flip buffer
	SwapBuffers hdc

	'Check for WindowClose Event	
	Select WaitEvent()
		Case EVENT_KEYDOWN
			End
		Case EVENT_WINDOWCLOSE
			End
	End Select
	
Forever


If I use a PollEvent() instead of a WaitEvent() in the BlitzMax source the triangle shows up, though it is badly distorted. I don't think the pixel format is getting set up correctly.

I replaced the bank with a PIXELFORMATDESCRIPTOR structure. The screen still flashes badly, looking like the pixelformat is not getting set correctly.
Include "constants\opengl.bmx"
Include "constants\win32.bmx"

'Constants
Const off_nSize=0'  WORD
Const off_nVersion=2'  WORD
Const off_dwFlags=4'  DWORD
Const off_iPixelType=8'  Byte
Const off_cColorBits=9'  Byte
Const off_cRedBits=10'  Byte
Const off_cRedShift=11'  Byte
Const off_cGreenBits=12'  Byte
Const off_cGreenShift=13'  Byte
Const off_cBlueBits=14'  Byte
Const off_cBlueShift=15'  Byte
Const off_cAlphaBits=16'  Byte
Const off_cAlphaShift=17'  Byte
Const off_cAccumBits=18'  Byte
Const off_cAccumRedBits=19'  Byte
Const off_cAccumGreenBits=20'  Byte
Const off_cAccumBlueBits=21'  Byte
Const off_cAccumAlphaBits=22'  Byte
Const off_cDepthBits=23'  Byte
Const off_cStencilBits=24'  Byte
Const off_cAuxBuffers=25'  Byte
Const off_iLayerType=26'  Byte
Const off_bReserved=27'  Byte
Const off_dwLayerMask=28'  DWORD
Const off_dwVisibleMask=32'  DWORD
Const off_dwDamageMask=36'  DWORD

Const PFD_DOUBLEBUFFER = $00000001
Const PFD_DRAW_TO_WINDOW = $00000004
Const PFD_SUPPORT_OPENGL = $00000020
Const PFD_TYPE_RGBA=0
Const PFD_MAIN_PLANE=0

'Create Window
win:tgadget=CreateWindow("OpenGL Window",200,200,640,480,Null,3)

'Setup pixel format
hwnd=QueryGadget(win,QUERY_HWND_CLIENT)
hdc=GetDC(hwnd)

Local lpPixelFormat:PIXELFORMATDESCRIPTOR
lpPixelFormat.nSize=40
lpPixelFormat.nVersion=1
lpPixelFormat.dwFlags=PFD_SUPPORT_OPENGL Or PFD_DOUBLEBUFFER Or PFD_DRAW_TO_WINDOW
lpPixelFormat.iPixelType=PFD_TYPE_RGBA
lpPixelFormat.cColorBits=16
lpPixelFormat.cDepthBits=16
lpPixelFormat.cAccumBits=16
lpPixelFormat.cStencilBits=8
lpPixelFormat.dwLayerMask=PFD_MAIN_PLANE
lpPixelFormat.cBlueBits=8
lpPixelFormat.cGreenBits=8
lpPixelFormat.cRedBits=8
lPixelFormat=ChoosePixelFormat(hDC,lpPixelFormat)
If Not lPixelFormat RuntimeError "ChoosePixelFormat() failed"
result=SetPixelFormat(hDC,lPixelFormat,lpPixelFormat)
lpPixelFormat=Null

'Create OpenGL Context
hrc=wglCreateContext(hdc)
If Not hrc Notify "Failed to create context."
wglMakeCurrent hdc,hrc

'Program Loop
Repeat
	
	'Setup Viewport
	w=ClientWidth(win)
	h=ClientHeight(win)
	glViewport 0.0,0.0,w,h
	glClearColor 0.0,0.0,1.0,0.0
	glClear GL_DEPTH_BUFFER_BIT+GL_COLOR_BUFFER_BIT

	glMatrixMode GL_PROJECTION
	glLoadIdentity()
	glPushMatrix()
	gluPerspective 45.0,Float(w)/Float(h),1.0,100.0

	'Draw Triangle
	glBegin GL_TRIANGLES
	glVertex3f 0.0,0.0,-10.0
	glVertex3f 1.0,1.0,-10.0
	glVertex3f 1.0,0.0,-10.0
	glEnd

	glpopmatrix()

	'Flip buffer
	SwapBuffers hdc

	'Check for WindowClose Event	
	Select PollEvent()
		Case EVENT_KEYDOWN
			End
		Case EVENT_WINDOWCLOSE
			End
	End Select
	
Forever


Okay, I had to get rid of the OR statements in the dwflags parameter. Here it is, a super-simple OpenGL setup on a BlitzMax window. That's pretty nice that it is so simple, even when you are creating a context from scratch:
'Create Window
win:tgadget=CreateWindow("OpenGL Window",200,200,640,480,Null,3)

'Setup pixel format
hwnd=QueryGadget(win,QUERY_HWND_CLIENT)
hdc=GetDC(hwnd)

Local lpPixelFormat:PIXELFORMATDESCRIPTOR
lpPixelFormat.nSize=40
lpPixelFormat.nVersion=1
lpPixelFormat.dwFlags=PFD_SUPPORT_OPENGL+PFD_DOUBLEBUFFER+PFD_DRAW_TO_WINDOW
lpPixelFormat.iPixelType=PFD_TYPE_RGBA
lpPixelFormat.cColorBits=16
lpPixelFormat.cRedBits=8
lpPixelFormat.cGreenBits=8
lpPixelFormat.cBlueBits=8
lpPixelFormat.cAccumBits=16
lpPixelFormat.cDepthBits=16
lpPixelFormat.cStencilBits=8
lpPixelFormat.dwLayerMask=PFD_MAIN_PLANE

lPixelFormat=ChoosePixelFormat(hDC,lpPixelFormat)

If Not lPixelFormat RuntimeError "ChoosePixelFormat() failed"
result=SetPixelFormat(hDC,lPixelFormat,lpPixelFormat)
If Not result RuntimeError "SetPixelFormat() failed"
lpPixelFormat=Null

'Create OpenGL Context
hrc=wglCreateContext(hdc)
If Not hrc Notify "Failed to create context."
wglMakeCurrent hdc,hrc

'Program Loop
Repeat
	
	'Setup Viewport
	w=ClientWidth(win)
	h=ClientHeight(win)
	glViewport 0.0,0.0,w,h
	glClearColor 0.0,0.0,1.0,0.0
	glClear GL_DEPTH_BUFFER_BIT+GL_COLOR_BUFFER_BIT

	glMatrixMode GL_PROJECTION
	glLoadIdentity()
	glPushMatrix()
	gluPerspective 45.0,Float(w)/Float(h),1.0,100.0

	'Draw Triangle
	glBegin GL_TRIANGLES
	glVertex3f 0.0,0.0,-10.0
	glVertex3f 1.0,1.0,-10.0
	glVertex3f 1.0,0.0,-10.0
	glEnd

	glpopmatrix()

	'Flip buffer
	SwapBuffers hdc

	'Check for WindowClose Event	
	If WaitEvent()=EVENT_WINDOWCLOSE End

Forever


In BMax 'AND' and 'OR' are logical operators, their binary counterparts being '&' and '|'...
lpPixelFormat.dwFlags = PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_DRAW_TO_WINDOW
...ETC...