D.J.Peters
05-03-2010, 09:01
i would create shadows with the stencil buffer
how to create a window/dialog with border but no caption ?
thank you
Joshy
' second try native OpenGL
' by D.J:Peters (Joshy)
#INCLUDE Once "%APP_INCLUDEPATH%\thinbasic_gl.inc"
#INCLUDE Once "%APP_INCLUDEPATH%\thinbasic_GLU.inc"
#INCLUDE Once "%APP_INCLUDEPATH%\thinbasic_WGL.inc"
Uses "UI"
' %APP_SOURCEPATH%
'
' consts
'
%PFD_TYPE_RGBA = 0
%PFD_MAIN_PLANE = 0
%PFD_DOUBLEBUFFER = 1
%PFD_STEREO = 2
%PFD_DRAW_TO_WINDOW = 4
%PFD_DRAW_TO_BITMAP = 8
%PFD_SUPPORT_GDI = 16
%PFD_SUPPORT_OPENGL = 32
%PFD_GENERIC_FORMAT = 64
%PM_NOREMOVE = 0
%PM_REMOVE = 1
%CLEAR_ALL = 0
%CLEAR_COLOR_BUFFER = 1
%CLEAR_STENCIL_BUFFER = 2
%CLEAR_ACCUM_BUFFER = 4
'
' types
'
Type PIXFORMAT
nSize As Word ' 2
nVersion As Word ' 4
dwFlags As DWord ' 8
iPixelType As Byte
cColorBits As Byte
cRedBits As Byte
cRedShift As Byte
cGreenBits As Byte
cGreenShift As Byte
cBlueBits As Byte
cBlueShift As Byte
cAlphaBits As Byte
cAlphaShift As Byte
cAccumBits As Byte
cAccumRedBits As Byte
cAccumGreenBits As Byte
cAccumBlueBits As Byte
cAccumAlphaBits As Byte
cDepthBits As Byte
cStencilBits As Byte
cAuxBuffers As Byte
iLayerType As Byte
bReserved As Byte ' 28
dwLayerMask As DWord
dwVisibleMask As DWord
dwDamageMask As DWord ' 40
End Type
Type GLDEVICE
PF As PIXFORMAT ' BufferBits Descriptor
hWin As Long ' Dialog Handle
hDC As Long ' Device Context
hRC As Long ' Render Context
Width As Long ' Curent Client Width
Height As Long ' Curent Client Height
End Type
'
' api proto types
'
Declare Function GetDC Lib "USER32.DLL" Alias "GetDC" (ByVal win As Long) As Long
Declare Function ReleaseDC Lib "USER32.DLL" Alias "ReleaseDC" (ByVal win As Long, ByVal dc As Long) As Long
Declare Function PeekMsg Lib "USER32.DLL" Alias "PeekMessageA" (ByRef msg tagMSG, ByVal win As Long, ByVal wMsgFilterMin As DWord, ByVal wMsgFilterMax As DWord, ByVal wRemoveMsg As DWord) As Long
Declare Function TranslateMessage Lib "USER32.DLL" Alias "TranslateMessage" (ByRef msg tagMSG) As Long
Declare Function DispatchMessage Lib "USER32.DLL" Alias "DispatchMessageA" (ByRef msg tagMSG) As Long
Declare Function DescribePixelFormat Lib "GDI32.dll" Alias "DescribePixelFormat" (ByVal dc As Long, ByVal iPixelFormat As Long,ByVal pfSize As Long, ByRef pf As PIXFORMAT) As Long
Declare Function ChoosePixelFormat Lib "GDI32.dll" Alias "ChoosePixelFormat" (ByVal dc As Long, ByRef pf As PIXFORMAT) As Long
Declare Function SetPixelFormat Lib "GDI32.dll" Alias "SetPixelFormat" (ByVal dc As Long, ByVal iPixelFormat As Integer, ByRef pf As PIXFORMAT) As Long
Declare Function SwapBuffers Lib "GDI32.dll" Alias "SwapBuffers" (ByVal dc As Long) As Long
'
' globals
'
Global GLDevices() As GLDEVICE
Global nGLDevices As Long
'
' sub's / functions
'
Sub InitPixFormat(ByRef PF As PIXFORMAT, _
ByVal ColorBits As Byte, _
ByVal DepthBits As Byte, _
ByVal StencilBits As Byte, _
ByVal AccumBits As Byte)
' 8/16/24/32
ColorBits = (ColorBits +7) And &H38
DepthBits = (DepthBits +7) And &H38
StencilBits = (StencilBits+7) And &H38
With PF
.nSize = 40
.nVersion = 1
.dwFlags = %PFD_DRAW_TO_WINDOW Or _
%PFD_SUPPORT_OPENGL Or _
%PFD_DOUBLEBUFFER
.iPixelType = %PFD_TYPE_RGBA ' 0
.cColorBits = ColorBits
.cDepthBits = DepthBits
.cStencilBits = StencilBits
.cAccumBits = AccumBits
.iLayerType = %PFD_MAIN_PLANE ' 0
End With
End Sub
Function AllocGLDevice() As Long
Dim found,i As Long
If nGLDevices>0 Then
For i=1 To nGLDevices
If GLDevices(i).hRC=NULL Then
found=i
End If
Next
End If
' return free device index
If found>0 Then Return found
' alloc new device
nGLDevices+=1
ReDim Preserve GLDevices(nGLDevices)
Return nGLDevices
End Function
Sub FreeGLDevice(ByRef index As Long)
If nGLDevices<1 Then Return
If index<1 Then Return
If index>nGLDevices Then Return
With GLDevices(index)
wglMakeCurrent(NULL,NULL)
wglDeleteContext(.hRC)
.hRC=NULL
ReleaseDC(.hWin,.hDC)
.hDC=NULL
Dialog End .hWin
.hWin=NULL
End With
index=0
End Sub
Function CreateGLDevice(Optional _
ByVal Width As DWord , _
ByVal Height As DWord , _
ByVal Title As String, _
ByVal ColorBits As Byte , _
ByVal DepthBits As Byte , _
ByVal StencilBits As Byte , _
ByVal AccumBits As Byte) As Long
Dim Style As DWord Value %WS_POPUP Or _
%WS_VISIBLE Or _
%WS_CLIPSIBLINGS Or _
%WS_CLIPCHILDREN Or _
%WS_DLGFRAME Or _
%WS_SYSMENU
Dim MaxWidth,MaxHeight,X,Y,index As Long
Dim tmpWin,tmpDC,tmpRC,iPixFormat As Long
Dim tmpPF As PIXFORMAT
Desktop Get Client To MaxWidth,MaxHeight
' adjust optional params
If ColorBits<8 Then ColorBits=8
If Width =0 Then Width = MaxWidth /2
If Height =0 Then Height = MaxHeight/2
If Len(Title) Then
Style = Style Or _
%WS_CAPTION Or _
%WS_MINIMIZEBOX Or _
%WS_MAXIMIZEBOX
End If
If Width >MaxWidth Then Width =MaxWidth
If Height>MaxHeight Then Height=MaxHeight
' centered
If Width <MaxWidth Then x=MaxWidth /2-Width /2
If Height<MaxHeight Then y=MaxHeight/2-Height/2
tmpWin = Dialog_New(Pixels,NULL, Title, _
x,y, width,height, _
Style,%WS_EX_APPWINDOW)
If tmpWin=NULL Then
MsgBox NULL,"error: create new window !"
Return 0
End If
Dialog Show Modeless tmpWin
Sleep(1000)
tmpDC = GetDC(tmpWin)
If tmpDC=NULL Then
MsgBox NULL,"error: get device context !"
Dialog End tmpWin
Return 0
End If
InitPixFormat(tmpPF, _
ColorBits, _
DepthBits, _
StencilBits, _
AccumBits)
iPixFormat=ChoosePixelFormat(tmpDC,tmpPF)
If iPixFormat<1 Then
MsgBox NULL,"error: choose pixel format !"
ReleaseDC(tmpWin,tmpDC)
Dialog End tmpWin
Return 0
End If
If SetPixelFormat(tmpDC,iPixFormat,tmpPF)=0 Then
MsgBox NULL,"error: set pixel format !"
ReleaseDC(tmpWin,tmpDC)
Dialog End tmpWin
Return 0
End If
tmpRC=wglCreateContext(tmpDC)
If tmpRC=NULL Then
MsgBox NULL,"error: create render context !"
ReleaseDC(tmpWin,tmpDC)
Dialog End tmpWin
Return 0
End If
If wglMakeCurrent(tmpDC,tmpRC)=0 Then
MsgBox NULL,"error: make render context as current !"
wglMakeCurent(NULL,NULL)
wglDeleteContext(tmpRC)
ReleaseDC(tmpWin,tmpDC)
Dialog End tmpWin
Return 0
End If
' ok :-)
index=AllocGLDevice()
With GLDevices(index)
.PF.nSize = 40
.PF.nVersion = 1
DescribePixelFormat(tmpDC,iPixFormat,.PF,40)
.hWin = tmpWin
.hDC = tmpDC
.hRC = tmpRC
.Width = Width
.Height = Height
End With
Return index
End Function
Sub GLDeviceSwap(ByVal index As Long)
If nGLDevices<1 Then Return
If index<1 Then Return
If index>nGLDevices Then Return
With GLDevices(index)
SwapBuffers(.hDC)
End With
End Sub
'
' main !!! close a border only window with [ALT] & [F4] !!!
'
Dim Msg As tagMsg
Dim RC As RECT
Dim DeviceID As Long
Dim rtri As Single
Dim rquad As Single
'DeviceID = CreateGLDevice() ' no border
DeviceID = CreateGLDevice(320,200) ' border no caption
'DeviceID = CreateGLDevice(,,"no size")
'DeviceID = CreateGLDevice(32000,20000,"large resizeable")
'DeviceID = CreateGLDevice(32000,20000,,24,16)
'DeviceID = CreateGLDevice(320,200,"16 BPP no DepthBits",16)
'DeviceID = CreateGLDevice(320,200,"16 BPP 16 DepthBits",16,16)
'DeviceID = CreateGLDevice(320,200,"24 BPP 32 DepthBits",24,32)
'DeviceID = CreateGLDevice(,,"24 BPP 32 DepthBits 8 StencilBits",24,32,8)
If DeviceID=0 Then
MsgBox "error: can't create GLDevice !"
Stop
End If
With GLDevices(DeviceID)
glViewport 0, 0, .Width,.Height
glMatrixMode %GL_PROJECTION
glLoadIdentity
gluPerspective 45.0, .Width/.Height, 0.1, 100.0
End With
glMatrixMode %GL_MODELVIEW
glLoadIdentity
glShadeModel %GL_SMOOTH
glClearColor 0.0, 0.0, 0.0, 1.0
glClearDepth 1.0
glEnable %GL_DEPTH_TEST
glDepthFunc %GL_LEQUAL
glHint %GL_PERSPECTIVE_CORRECTION_HINT, %GL_NICEST
Do
If PeekMsg(Msg, NULL, 0, 0, %PM_REMOVE) <> 0 Then
Select Case Msg.message
Case 161,274: Exit Do
Case %WM_SIZE
With GLDevices(DeviceID)
glViewport 0, 0, .Width,.Height
glMatrixMode %GL_PROJECTION
glLoadIdentity
gluPerspective 45.0, .Width/.Height, 0.1, 100.0
End With
End Select
TranslateMessage(Msg)
DispatchMessage(Msg)
Else
glClear %GL_COLOR_BUFFER_BIT Or %GL_DEPTH_BUFFER_BIT
glLoadIdentity
glTranslatef -1.5, 0.0, -6.0
glRotatef rtri, 0, 1, 0
glBegin %GL_TRIANGLES
glColor3f 1.0, 0.0, 0.0
glVertex3f 0.0, 1.0, 0.0
glColor3f 0.0, 1.0, 0.0
glVertex3f -1.0, -1.0, 0.0
glColor3f 0.0, 0.0, 1.0
glVertex3f 1.0, -1.0, 0.0
glEnd
glLoadIdentity
glTranslatef 1.5, 0.0, -6.0
glColor3f 0.5, 0.5, 1.0
glRotatef rquad, 1, 0, 0
glBegin %GL_QUADS
glVertex3f -1.0, 1.0, 0.0
glVertex3f 1.0, 1.0, 0.0
glVertex3f 1.0, -1.0, 0.0
glVertex3f -1.0, -1.0, 0.0
glEnd
rtri += 1
rquad += 2
SwapBuffers(GLDevices(DeviceID).hDC)
End If
Loop
FreeGLDevice(DeviceID)
how to create a window/dialog with border but no caption ?
thank you
Joshy
' second try native OpenGL
' by D.J:Peters (Joshy)
#INCLUDE Once "%APP_INCLUDEPATH%\thinbasic_gl.inc"
#INCLUDE Once "%APP_INCLUDEPATH%\thinbasic_GLU.inc"
#INCLUDE Once "%APP_INCLUDEPATH%\thinbasic_WGL.inc"
Uses "UI"
' %APP_SOURCEPATH%
'
' consts
'
%PFD_TYPE_RGBA = 0
%PFD_MAIN_PLANE = 0
%PFD_DOUBLEBUFFER = 1
%PFD_STEREO = 2
%PFD_DRAW_TO_WINDOW = 4
%PFD_DRAW_TO_BITMAP = 8
%PFD_SUPPORT_GDI = 16
%PFD_SUPPORT_OPENGL = 32
%PFD_GENERIC_FORMAT = 64
%PM_NOREMOVE = 0
%PM_REMOVE = 1
%CLEAR_ALL = 0
%CLEAR_COLOR_BUFFER = 1
%CLEAR_STENCIL_BUFFER = 2
%CLEAR_ACCUM_BUFFER = 4
'
' types
'
Type PIXFORMAT
nSize As Word ' 2
nVersion As Word ' 4
dwFlags As DWord ' 8
iPixelType As Byte
cColorBits As Byte
cRedBits As Byte
cRedShift As Byte
cGreenBits As Byte
cGreenShift As Byte
cBlueBits As Byte
cBlueShift As Byte
cAlphaBits As Byte
cAlphaShift As Byte
cAccumBits As Byte
cAccumRedBits As Byte
cAccumGreenBits As Byte
cAccumBlueBits As Byte
cAccumAlphaBits As Byte
cDepthBits As Byte
cStencilBits As Byte
cAuxBuffers As Byte
iLayerType As Byte
bReserved As Byte ' 28
dwLayerMask As DWord
dwVisibleMask As DWord
dwDamageMask As DWord ' 40
End Type
Type GLDEVICE
PF As PIXFORMAT ' BufferBits Descriptor
hWin As Long ' Dialog Handle
hDC As Long ' Device Context
hRC As Long ' Render Context
Width As Long ' Curent Client Width
Height As Long ' Curent Client Height
End Type
'
' api proto types
'
Declare Function GetDC Lib "USER32.DLL" Alias "GetDC" (ByVal win As Long) As Long
Declare Function ReleaseDC Lib "USER32.DLL" Alias "ReleaseDC" (ByVal win As Long, ByVal dc As Long) As Long
Declare Function PeekMsg Lib "USER32.DLL" Alias "PeekMessageA" (ByRef msg tagMSG, ByVal win As Long, ByVal wMsgFilterMin As DWord, ByVal wMsgFilterMax As DWord, ByVal wRemoveMsg As DWord) As Long
Declare Function TranslateMessage Lib "USER32.DLL" Alias "TranslateMessage" (ByRef msg tagMSG) As Long
Declare Function DispatchMessage Lib "USER32.DLL" Alias "DispatchMessageA" (ByRef msg tagMSG) As Long
Declare Function DescribePixelFormat Lib "GDI32.dll" Alias "DescribePixelFormat" (ByVal dc As Long, ByVal iPixelFormat As Long,ByVal pfSize As Long, ByRef pf As PIXFORMAT) As Long
Declare Function ChoosePixelFormat Lib "GDI32.dll" Alias "ChoosePixelFormat" (ByVal dc As Long, ByRef pf As PIXFORMAT) As Long
Declare Function SetPixelFormat Lib "GDI32.dll" Alias "SetPixelFormat" (ByVal dc As Long, ByVal iPixelFormat As Integer, ByRef pf As PIXFORMAT) As Long
Declare Function SwapBuffers Lib "GDI32.dll" Alias "SwapBuffers" (ByVal dc As Long) As Long
'
' globals
'
Global GLDevices() As GLDEVICE
Global nGLDevices As Long
'
' sub's / functions
'
Sub InitPixFormat(ByRef PF As PIXFORMAT, _
ByVal ColorBits As Byte, _
ByVal DepthBits As Byte, _
ByVal StencilBits As Byte, _
ByVal AccumBits As Byte)
' 8/16/24/32
ColorBits = (ColorBits +7) And &H38
DepthBits = (DepthBits +7) And &H38
StencilBits = (StencilBits+7) And &H38
With PF
.nSize = 40
.nVersion = 1
.dwFlags = %PFD_DRAW_TO_WINDOW Or _
%PFD_SUPPORT_OPENGL Or _
%PFD_DOUBLEBUFFER
.iPixelType = %PFD_TYPE_RGBA ' 0
.cColorBits = ColorBits
.cDepthBits = DepthBits
.cStencilBits = StencilBits
.cAccumBits = AccumBits
.iLayerType = %PFD_MAIN_PLANE ' 0
End With
End Sub
Function AllocGLDevice() As Long
Dim found,i As Long
If nGLDevices>0 Then
For i=1 To nGLDevices
If GLDevices(i).hRC=NULL Then
found=i
End If
Next
End If
' return free device index
If found>0 Then Return found
' alloc new device
nGLDevices+=1
ReDim Preserve GLDevices(nGLDevices)
Return nGLDevices
End Function
Sub FreeGLDevice(ByRef index As Long)
If nGLDevices<1 Then Return
If index<1 Then Return
If index>nGLDevices Then Return
With GLDevices(index)
wglMakeCurrent(NULL,NULL)
wglDeleteContext(.hRC)
.hRC=NULL
ReleaseDC(.hWin,.hDC)
.hDC=NULL
Dialog End .hWin
.hWin=NULL
End With
index=0
End Sub
Function CreateGLDevice(Optional _
ByVal Width As DWord , _
ByVal Height As DWord , _
ByVal Title As String, _
ByVal ColorBits As Byte , _
ByVal DepthBits As Byte , _
ByVal StencilBits As Byte , _
ByVal AccumBits As Byte) As Long
Dim Style As DWord Value %WS_POPUP Or _
%WS_VISIBLE Or _
%WS_CLIPSIBLINGS Or _
%WS_CLIPCHILDREN Or _
%WS_DLGFRAME Or _
%WS_SYSMENU
Dim MaxWidth,MaxHeight,X,Y,index As Long
Dim tmpWin,tmpDC,tmpRC,iPixFormat As Long
Dim tmpPF As PIXFORMAT
Desktop Get Client To MaxWidth,MaxHeight
' adjust optional params
If ColorBits<8 Then ColorBits=8
If Width =0 Then Width = MaxWidth /2
If Height =0 Then Height = MaxHeight/2
If Len(Title) Then
Style = Style Or _
%WS_CAPTION Or _
%WS_MINIMIZEBOX Or _
%WS_MAXIMIZEBOX
End If
If Width >MaxWidth Then Width =MaxWidth
If Height>MaxHeight Then Height=MaxHeight
' centered
If Width <MaxWidth Then x=MaxWidth /2-Width /2
If Height<MaxHeight Then y=MaxHeight/2-Height/2
tmpWin = Dialog_New(Pixels,NULL, Title, _
x,y, width,height, _
Style,%WS_EX_APPWINDOW)
If tmpWin=NULL Then
MsgBox NULL,"error: create new window !"
Return 0
End If
Dialog Show Modeless tmpWin
Sleep(1000)
tmpDC = GetDC(tmpWin)
If tmpDC=NULL Then
MsgBox NULL,"error: get device context !"
Dialog End tmpWin
Return 0
End If
InitPixFormat(tmpPF, _
ColorBits, _
DepthBits, _
StencilBits, _
AccumBits)
iPixFormat=ChoosePixelFormat(tmpDC,tmpPF)
If iPixFormat<1 Then
MsgBox NULL,"error: choose pixel format !"
ReleaseDC(tmpWin,tmpDC)
Dialog End tmpWin
Return 0
End If
If SetPixelFormat(tmpDC,iPixFormat,tmpPF)=0 Then
MsgBox NULL,"error: set pixel format !"
ReleaseDC(tmpWin,tmpDC)
Dialog End tmpWin
Return 0
End If
tmpRC=wglCreateContext(tmpDC)
If tmpRC=NULL Then
MsgBox NULL,"error: create render context !"
ReleaseDC(tmpWin,tmpDC)
Dialog End tmpWin
Return 0
End If
If wglMakeCurrent(tmpDC,tmpRC)=0 Then
MsgBox NULL,"error: make render context as current !"
wglMakeCurent(NULL,NULL)
wglDeleteContext(tmpRC)
ReleaseDC(tmpWin,tmpDC)
Dialog End tmpWin
Return 0
End If
' ok :-)
index=AllocGLDevice()
With GLDevices(index)
.PF.nSize = 40
.PF.nVersion = 1
DescribePixelFormat(tmpDC,iPixFormat,.PF,40)
.hWin = tmpWin
.hDC = tmpDC
.hRC = tmpRC
.Width = Width
.Height = Height
End With
Return index
End Function
Sub GLDeviceSwap(ByVal index As Long)
If nGLDevices<1 Then Return
If index<1 Then Return
If index>nGLDevices Then Return
With GLDevices(index)
SwapBuffers(.hDC)
End With
End Sub
'
' main !!! close a border only window with [ALT] & [F4] !!!
'
Dim Msg As tagMsg
Dim RC As RECT
Dim DeviceID As Long
Dim rtri As Single
Dim rquad As Single
'DeviceID = CreateGLDevice() ' no border
DeviceID = CreateGLDevice(320,200) ' border no caption
'DeviceID = CreateGLDevice(,,"no size")
'DeviceID = CreateGLDevice(32000,20000,"large resizeable")
'DeviceID = CreateGLDevice(32000,20000,,24,16)
'DeviceID = CreateGLDevice(320,200,"16 BPP no DepthBits",16)
'DeviceID = CreateGLDevice(320,200,"16 BPP 16 DepthBits",16,16)
'DeviceID = CreateGLDevice(320,200,"24 BPP 32 DepthBits",24,32)
'DeviceID = CreateGLDevice(,,"24 BPP 32 DepthBits 8 StencilBits",24,32,8)
If DeviceID=0 Then
MsgBox "error: can't create GLDevice !"
Stop
End If
With GLDevices(DeviceID)
glViewport 0, 0, .Width,.Height
glMatrixMode %GL_PROJECTION
glLoadIdentity
gluPerspective 45.0, .Width/.Height, 0.1, 100.0
End With
glMatrixMode %GL_MODELVIEW
glLoadIdentity
glShadeModel %GL_SMOOTH
glClearColor 0.0, 0.0, 0.0, 1.0
glClearDepth 1.0
glEnable %GL_DEPTH_TEST
glDepthFunc %GL_LEQUAL
glHint %GL_PERSPECTIVE_CORRECTION_HINT, %GL_NICEST
Do
If PeekMsg(Msg, NULL, 0, 0, %PM_REMOVE) <> 0 Then
Select Case Msg.message
Case 161,274: Exit Do
Case %WM_SIZE
With GLDevices(DeviceID)
glViewport 0, 0, .Width,.Height
glMatrixMode %GL_PROJECTION
glLoadIdentity
gluPerspective 45.0, .Width/.Height, 0.1, 100.0
End With
End Select
TranslateMessage(Msg)
DispatchMessage(Msg)
Else
glClear %GL_COLOR_BUFFER_BIT Or %GL_DEPTH_BUFFER_BIT
glLoadIdentity
glTranslatef -1.5, 0.0, -6.0
glRotatef rtri, 0, 1, 0
glBegin %GL_TRIANGLES
glColor3f 1.0, 0.0, 0.0
glVertex3f 0.0, 1.0, 0.0
glColor3f 0.0, 1.0, 0.0
glVertex3f -1.0, -1.0, 0.0
glColor3f 0.0, 0.0, 1.0
glVertex3f 1.0, -1.0, 0.0
glEnd
glLoadIdentity
glTranslatef 1.5, 0.0, -6.0
glColor3f 0.5, 0.5, 1.0
glRotatef rquad, 1, 0, 0
glBegin %GL_QUADS
glVertex3f -1.0, 1.0, 0.0
glVertex3f 1.0, 1.0, 0.0
glVertex3f 1.0, -1.0, 0.0
glVertex3f -1.0, -1.0, 0.0
glEnd
rtri += 1
rquad += 2
SwapBuffers(GLDevices(DeviceID).hDC)
End If
Loop
FreeGLDevice(DeviceID)