Date: 02-16-2022
Return to Index
'Compilable Example: (Jose Includes)
created by gbSnippets
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "win32api.inc"
#Include "gl.inc"
#Include "glu.inc"
Global hDlg, hDC, hRC, hBMP As Dword
Global BMPData() As Long
Global anglex As Single
Function PBMain() As Long
Dialog New Pixels, 0, "OpenGL Example",,, 500,500,%WS_OverlappedWindow To hDlg
Dialog Show Modal hdlg Call dlgproc
End Function
CallBack Function dlgproc()
Select Case Cb.Msg
Case %WM_InitDialog : GetRenderContext
CreateTexture
InitializeMapping
Case %WM_Size : ResizeScene Lo(Word, Cb.LParam), Hi(Word, Cb.LParam)
DrawScene
Case %WM_Paint
DrawScene
Case %WM_MouseWheel
Select Case Hi(Integer,Cb.WParam)
Case > 0 : anglex = anglex + 0.1 : DrawScene
Case < 0 : anglex = anglex - 0.1 : DrawScene
End Select
End Select
End Function
Sub GetRenderContext
Local pfd As PIXELFORMATDESCRIPTOR
pfd.dwFlags = %PFD_DRAW_TO_WINDOW Or %PFD_SUPPORT_OPENGL Or %PFD_DOUBLEBUFFER
hDC = GetDC(hDlg) 'DC for dialog
SetPixelFormat(hDC, ChoosePixelFormat(hDC, pfd), pfd) 'set properties of device context
hRC = wglCreateContext (hDC) 'get rendering context
wglMakeCurrent hDC, hRC 'make the RC current
ReleaseDC hDC, hDlg
End Sub
Sub ResizeScene (w As Long, h As Long)
glViewport 0, 0, w, h 'resize viewport to match window size
glLoadIdentity 'reset the projection matrix
gluPerspective 45, w/h, 0.1, 100 'calculate the aspect ratio of the Window
End Sub
Sub DrawScene
glEnable %gl_line_smooth 'enable antialiasing
glHint %gl_line_smooth_hint, %gl_nicest 'best quality
glRotatef anglex, 0,0,1
glBegin %gl_quads
glTexCoord2f 0.0, 0.0: glVertex3f -1.0, -1.0, -5.0 ' Bottom Left Of The Texture And Quad
glTexCoord2f 1.0, 0.0: glVertex3f 1.0, -1.0, -5.0 ' Bottom Right Of The Texture And Quad
glTexCoord2f 1.0, 1.0: glVertex3f 1.0, 1.0, -5.0 ' Top Right Of The Texture And Quad
glTexCoord2f 0.0, 1.0: glVertex3f -1.0, 1.0, -5.0 ' Top Left Of The Texture And Quad
glEnd
SwapBuffers hDC 'display the buffer (image)
End Sub
Sub CreateTexture
Local i,j,imgW,imgH As Long, sFileName As String
sFileName = "ruler.bmp"
imgW=300 : imgH = 300
Graphic Bitmap Load sFileName, imgW, imgH To hBMP
Graphic Attach hBMP, 0
Dim BMPData(imgW-1,imgH-1)
For i = 0 To imgW-1
For j = 0 To imgH-1
Graphic Get Pixel (i,j) To BMPData(i,imgH-1-j)
Next j
Next i
End Sub
Sub InitializeMapping
glEnable %gl_texture_2D
glTexEnvf %gl_texture_env, %gl_texture_env_mode, %gl_modulate
glBindTexture %gl_texture_2D, 1
glTexParameteri %gl_texture_2d,%gl_texture_mag_filter,%gl_linear
glTexParameteri %gl_texture_2d,%gl_texture_min_filter,%gl_linear
glTexImage2D %gl_texture_2D, 0, %gl_rgb, 300, 300, 0, %gl_rgba, %gl_unsigned_byte, BMPData(0)
End Sub
'gbs_00931
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm