Date: 02-16-2022
Return to Index
created by gbSnippets
'This snippet shows the minimal code required to demonstrate texture
'mapping in OpenGL.
'Primary Code:
'There are 2 functions to add to your PowerBASIC app - one to create
'an array with the texture (image) data and one to initialize OpenGL
'for displaying textures:
Sub CreateTexture
Local i,j,imgW,imgH As Long, sFileName As String
sFileName = "cowgirl.bmp"
imgW=100 : imgH = 100
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, 100, 100, 0, %gl_rgba, %gl_unsigned_byte, BMPData(0)
End Sub
'Then each vertex of a polygon must be mapped (assigned coordinates) corresponding
'to the matching position on the texture (image):
glBegin %gl_quads
' Front Face
glTexCoord2f 0.0, 0.0: glVertex3f -1.0, -1.0, 1.0 ' Bottom Left Of The Texture And Quad
glTexCoord2f 1.0, 0.0: glVertex3f 1.0, -1.0, 1.0 ' Bottom Right Of The Texture And Quad
glTexCoord2f 1.0, 1.0: glVertex3f 1.0, 1.0, 1.0 ' Top Right Of The Texture And Quad
glTexCoord2f 0.0, 1.0: glVertex3f -1.0, 1.0, 1.0 ' Top Left Of The Texture And Quad
glEnd
'Compilable Example: (Jose Includes)
#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
Function PBMain() As Long
Dialog New Pixels, 0, "OpenGL Example",,, 320, 240,%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
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
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
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 = "cowgirl.bmp"
imgW=100 : imgH = 100
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, 100, 100, 0, %gl_rgba, %gl_unsigned_byte, BMPData(0)
End Sub
'gbs_00615
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm