the purpose of this demo is to plot points according to the outcome of a function, then we cast a texture (and a color on condition) over the points.
1- using EncodeImage to get the address of a bmp (7 textures used (bmp, jpg, png)
this is my first exposure to the EncodeImage, and really it is alone a full product in the form of a function.
Code: Select all
*Buffer(i) = EncodeImage(i)
.....
glTexImage2D_(#GL_TEXTURE_2D,...., *Buffer(i)+57)
in the Shardik cube example http://purebasic.fr/english/viewtopic.p ... 30#p446600
if added ShowMemoryViewer(*BMPImage, MemorySize(*BMPImage))
Code: Select all
*BMPImage = AllocateMemory(ImageSize)
ReadData(0, *BMPImage, ImageSize)
CloseFile(0)
ShowMemoryViewer(*BMPImage, MemorySize(*BMPImage))
2- changing textures by unbinding and binding (use '+')
3- i have used glPointSize_( 3 ) as a cheat to give the illusion of compact solid texture
4-the glDrawArrays will draw the array in one shot

Code: Select all
Structure Point3D
x.f
y.f
z.f
tu.f
tv.f
r.f
g.f
b.f
EndStructure
UseJPEGImageDecoder()
UseJPEGImageEncoder()
UsePNGImageDecoder()
Define event, quit
#ImagePath = #PB_Compiler_Home + "examples/3d/Data/Textures/"
Dim *Buffer(7)
For i=1 To 7
Select i
Case 1
LoadImage(i, #ImagePath+"Geebee2.bmp")
Case 2
LoadImage(i, #ImagePath+"Wood.jpg")
Case 3
LoadImage(i, #ImagePath+"ValetCoeur.jpg")
Case 4
LoadImage(i, #ImagePath+"MRAMOR6X6.jpg")
Case 5
LoadImage(i, #ImagePath+"ogrelogo-small.jpg")
Case 6
LoadImage(i, #ImagePath+"clouds.jpg")
Case 7
LoadImage(i, #ImagePath+"Lensflare5.jpg")
EndSelect
*Buffer(i) = EncodeImage(i) ; default is: #PB_ImagePlugin_BMP : encode the image in BMP
Next
Dim TexID(7)
OpenWindow(0, 0, 0, 800, 600, "OpenGL .. glDrawArrays with vertices positions, colors, and textures ... press '+' to go to next texture")
SetWindowColor(0, RGB(200,220,200))
OpenGLGadget(0, 10, 10, WindowWidth(0) , WindowHeight(0) , #PB_OpenGL_Keyboard)
;- Generate textures
For i = 1 To 7
glGenTextures_(1, @TexID(i))
glBindTexture_(#GL_TEXTURE_2D, TexID(i))
glTexParameteri_(#GL_TEXTURE_2D, #GL_TEXTURE_MIN_FILTER, #GL_LINEAR)
glTexParameteri_(#GL_TEXTURE_2D, #GL_TEXTURE_MAG_FILTER, #GL_LINEAR)
glTexImage2D_(#GL_TEXTURE_2D, 0, 3, ImageWidth(i), ImageHeight(i), 0, #GL_BGR_EXT, #GL_UNSIGNED_BYTE, *Buffer(i)+57)
Next
For i=1 To 7
FreeMemory(*Buffer(i))
Next
glEnable_(#GL_TEXTURE_2D) ; Enable texture mapping
glBindTexture_(#GL_TEXTURE_2D, TexID(1))
glTexCoord2f_(1.0, 1.0)
glTexCoord2f_(0.0, 1.0)
glTexCoord2f_(0.0, 0.0)
glTexCoord2f_(1.0, 0.0)
glLoadIdentity_();
gluPerspective_(45.0, 800/600, 1.0, 60.0)
glTranslatef_(0, 0, -5)
glEnable_(#GL_DEPTH_TEST)
NbX = 600
NbZ = 600
Dim Point3D.Point3D(NbX,NbZ)
;xMin.f = -2 : yMin.f = -2: zMin.f = -2 : xMax.f = 2: yMax = 2 : zMax = 2
;xMin.f = -1 : yMin.f = -1: zMin.f = -1 : xMax.f = 1: yMax = 1 : zMax = 1
;xMin.f = -0.5 : zMin.f = -0.5 : xMax.f = 0.5: zMax = 0.5
;xMin.f = -30 : yMin.f = -30: zMin.f = -30 : xMax.f = 30: yMax = 30 : zMax = 30
;xMin.f = -12 : yMin.f = -12: zMin.f = -12 : xMax.f = 12: yMax = 12 : zMax = 12
xMin.f = -10 : yMin.f = -10: zMin.f = -10 : xMax.f = 10: yMax = 10 : zMax = 10
range = xMax - xMin
step1.f = range / NbX
x.f = xMin: z.f = zMin : y.f = yMin : tu.f: tv.f
For b=0 To NbZ
For a=0 To NbX
;formula to plot with thick points defined with glPointSize_( 3 )
;y.f =(1 - x*x -z*z) * Exp(-1/2 * (x*x + z*z))
y.f = Sin(Sqr(x*x+ z*z)) / Sqr(x*x + z*z)
; 3D 'A' letter:
;y.f = ((1-Sign(-x-0.9+Abs(z*2)))/3*(Sign(0.9-x)+1)/3)*(Sign(x+0.65)+1)/2 -((1-Sign(-x-0.39+Abs(z*2)))/3*(Sign(0.9-x)+1)/3) + ((1-Sign(-x-0.39+Abs(z*2)))/3*(Sign(0.6-x)+1)/3)*(Sign(x-0.35)+1)/2
;**********************************************************************
Point3D(a,b)\x = x*1
Point3D(a,b)\y = y*8
Point3D(a,b)\z = z*1
Point3D(a,b)\tu = a/NbX
Point3D(a,b)\tv = b/NbZ
If y>=0.35 ; just to color the points above 0.35 in green
Point3D(a,b)\r = 0.0 :Point3D(a,b)\g = 1.0 :Point3D(a,b)\b = 0
Else
Point3D(a,b)\r = 1 :Point3D(a,b)\g = 1 :Point3D(a,b)\b = 1
EndIf
x.f + step1
Next a
x = xMin
z.f + step1
Next b
;=================================================================================
rot.f = 1
glPointSize_( 3 )
glTranslatef_(0.0, 0.0, -30)
gluLookAt_( 0, 5, 4,
0, 0, 0,
0, 1, 0 )
SetActiveGadget(0) ; make the openGLgadget active
t=1
Repeat
Event = WindowEvent()
glViewport_(0, 0, WindowWidth(0), WindowHeight(0))
glClearColor_(0.9, 0.7, 0.0, 1) ; color of the background
glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
glEnableClientState_(#GL_VERTEX_ARRAY )
glEnableClientState_(#GL_COLOR_ARRAY)
glEnableClientState_(#GL_TEXTURE_COORD_ARRAY)
glRotatef_(rot, 0, 1, 0);
glVertexPointer_(3, #GL_FLOAT,SizeOf(Point3D),Point3D(0,0))
glColorPointer_(3, #GL_FLOAT, SizeOf(Point3D), @Point3D(0,0)\r)
glTexCoordPointer_(2, #GL_FLOAT, SizeOf(Point3D), @Point3D(0,0)\tu)
glDrawArrays_(#GL_POINTS, 1, NbX*NbX )
glDisableClientState_(#GL_TEXTURE_COORD_ARRAY)
glDisableClientState_(#GL_COLOR_ARRAY)
glDisableClientState_(#GL_VERTEX_ARRAY);
If Event = #PB_Event_Gadget And EventGadget() = 0
If EventType() = #PB_EventType_KeyUp ; like KeyboardReleased
key = GetGadgetAttribute(0,#PB_OpenGL_Key )
If key = #PB_Shortcut_Add
t+1: If t=8:t=1:EndIf ; go in round trip between textures 1 to 7
glBindTexture_(#GL_TEXTURE_2D, 0)
glBindTexture_(#GL_TEXTURE_2D, TexID(t))
ElseIf key = #PB_Shortcut_Escape ; Esc key to exit
quit = 1
EndIf
EndIf
EndIf
SetGadgetAttribute(0, #PB_OpenGL_FlipBuffers, #True)
Delay(10)
Until Event = #PB_Event_CloseWindow Or quit = 1
For i=1 To 7
glDeleteTextures_(1, @TexID(i))
Next