glDrawElements_(#GL_QUADS,indexsize,#GL_UNSIGNED_INT, @MeshDataInd(0)\Index)
but i can't see how quads is possible in Ogre engine.
first example using cartesian equations, we use structured 2D array to fill the values of height y. press 'W' to toggle between wire/solid frame
note you need to change the plot range in line 180 to suit other functions
i have used the OpenGLGadget special keys. tested in winxp/32 and ubuntu/x64
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()
Declare FillArray()
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+"Wood.jpg")
Case 2
LoadImage(i, #ImagePath+"terrain_texture.jpg")
Case 3
LoadImage(i, #ImagePath+"ValetCoeur.jpg")
Case 4
LoadImage(i, #ImagePath+"MRAMOR6X6.jpg")
Case 5
LoadImage(i, #ImagePath+"Geebee2.bmp")
Case 6
LoadImage(i, #ImagePath+"clouds.jpg")
Case 7
LoadImage(i, #ImagePath+"Wood.jpg")
EndSelect
*Buffer(i) = EncodeImage(i) ; default is: #PB_ImagePlugin_BMP : encode the image in BMP
Next
Dim TexID(7)
ExamineDesktops()
OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), "press '+' to go to next texture, ..... Up/Down: zoom in/out... 'W': wire frame... 'A'/'Z': change Camera view")
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(2)) ; use texture number 1
glLoadIdentity_();
gluPerspective_(45.0, DesktopWidth(0)/DesktopHeight(0), 1.0, 60.0)
glTranslatef_(0, 0, -5)
glEnable_(#GL_DEPTH_TEST)
Global NbX = 100
Global NbZ = 100
Global Dim Point3D.Point3D(NbX,NbZ)
Global Dim MeshDataInd.PB_MeshFace(0)
Global indexsize ;= ArraySize(MeshDataInd()) + 1
;Debug indexsize
FillArray()
;Debug indx
;=================================================================================
rot.f = 1
glPointSize_( 3 ) ; tick points in case we use points
;glTranslatef_(0.0, 0.0, -30)
SetActiveGadget(0) ; make the openGLgadget active
glPushMatrix_(); to store the current state
glMatrixMode_(#GL_PROJECTION)
glLoadIdentity_();
gluPerspective_(45.0, WindowWidth(0)/WindowHeight(0), 1.0, 200.0)
glMatrixMode_(#GL_MODELVIEW)
;glTranslatef_(0, 0, -5)
glShadeModel_(#GL_SMOOTH)
glEnable_(#GL_DEPTH_TEST)
glViewport_(0, 0, WindowWidth(0), WindowHeight(0))
gluLookAt_( 0, 20, 30, ; the camera looking from position 0,0.3,0.5 to 0,0,0 from above
0, 0, 0,
0, 1, 0 )
glPolygonMode_(#GL_FRONT_AND_BACK, #GL_LINE )
glPopMatrix_()
glPushMatrix_()
;t=1:
Repeat
Event = WindowEvent()
glViewport_(0, 0, WindowWidth(0), WindowHeight(0))
glClearColor_(0.9, 0.9, 0.9, 1)
glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
glEnableClientState_(#GL_VERTEX_ARRAY )
glEnableClientState_(#GL_COLOR_ARRAY)
glEnableClientState_(#GL_TEXTURE_COORD_ARRAY)
glRotatef_(rot/10, 0, 1, 0);
glVertexPointer_(3, #GL_FLOAT,SizeOf(Point3D),@Point3D(0,0)\x)
glColorPointer_(3, #GL_FLOAT, SizeOf(Point3D), @Point3D(0,0)\r)
glTexCoordPointer_(2, #GL_FLOAT, SizeOf(Point3D), @Point3D(0,0)\tu)
;glDrawElements_(#GL_LINES,indx,#GL_UNSIGNED_INT, @MeshDataInd(0)\Index)
;glDrawElements_(#GL_POINTS,indx,#GL_UNSIGNED_INT, @MeshDataInd(0)\Index)
;glDrawElements_(#GL_TRIANGLES,indexsize,#GL_UNSIGNED_INT, @MeshDataInd(0)\Index)
glDrawElements_(#GL_QUADS,indexsize,#GL_UNSIGNED_INT, @MeshDataInd(0)\Index)
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_KeyDown ; 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
ElseIf Key = #PB_Shortcut_W; display wire Frame or solid frame
If fill
glPolygonMode_(#GL_FRONT_AND_BACK, #GL_LINE )
fill ! 1
Else
glPolygonMode_(#GL_FRONT_AND_BACK, #GL_FILL)
fill ! 1
EndIf
ElseIf Key = #PB_Shortcut_Up
glScalef_(1.05,1.05,1.05)
ElseIf Key = #PB_Shortcut_Down
glScalef_(0.95,0.95,0.95)
ElseIf Key = #PB_Shortcut_A
glMatrixMode_(#GL_MODELVIEW);
glLoadIdentity_() ;
gluLookAt_(0, 40, 14, 0,0,0, 0,1,0 )
ElseIf Key = #PB_Shortcut_Z
glMatrixMode_(#GL_MODELVIEW);
glLoadIdentity_();
gluLookAt_(0, 20, 30, 0,0,0, 0,1,0 )
EndIf
EndIf
EndIf
SetGadgetAttribute(0, #PB_OpenGL_FlipBuffers, #True)
Delay(5)
Until Event = #PB_Event_CloseWindow Or quit = 1
For i=1 To 7
glDeleteTextures_(1, @TexID(i))
Next
Procedure FillArray()
;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 = -20 : yMin.f = -20: zMin.f = -20 : xMax.f = 20: yMax = 20 : zMax = 20
;xMin.f = -10 : yMin.f = -10: zMin.f = -10 : xMax.f = 10: yMax = 10 : zMax = 10
range.f = 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
;y.f =5*(1 - x*x -z*z) * Exp(-1/2 * (x*x + z*z))
;y.f = ATan ((z*z*z)/4 - (x*x))
;y.f = 1/(15*(x*x+z*z))
;y.f = Sin(5*x)*Cos(5*z)/5 ; hills
;y.f = 2/Exp((x*5)*(x*5)*(z*5)*(z*5)) ; like '+'
;y.f = Log(x*x+z*z)+0.6 ; horn
;y.f = Sin(10*(x*x+z*z))/5 ; like waves
;y.f = x * x
;y.f = 1/(x*x+z*z)
;y.f = Cos(x*x+z*z)
;y.f = (x*x+z*z)/6
;y.f =6*(1 - x*x -z*z) * Exp(-1/2 * (x*x + z*z))
y.f = Sin(Sqr(x*x+ z*z))*10 / Sqr(x*x + z*z)
;y.f =Sqr(5*5-z*z-x*x); sphere real part; x2 + y2 + z2 = r2
;**********************************************************************
Point3D(a,b)\x = x*1
Point3D(a,b)\y = y*1 ; just amplify the height
Point3D(a,b)\z = z*1
Point3D(a,b)\tu = a/NbX
Point3D(a,b)\tv = b/NbZ
If y>=0.05 ; just to color the points above 0.05 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
;ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
Nb=NbX+1
For b=0 To NbZ-1
For a=0 To NbX-1
P1=a+(b*Nb)
P2=P1+1
P3=a+(b+1)*Nb
P4=P3+1
MeshDataInd(indx)\Index = P3
ReDim MeshDataInd.PB_MeshFace(indx+2)
MeshDataInd(indx+1)\Index = P4
ReDim MeshDataInd.PB_MeshFace(indx+3)
MeshDataInd(indx+2)\Index = P2
ReDim MeshDataInd.PB_MeshFace(indx+4)
MeshDataInd(indx+3)\Index = P1;P2
indx+4
Next
Next
indexsize = ArraySize(MeshDataInd()) + 1
EndProcedure
display a tear drop
uncomment lines from 186 to 188 to see a sphere
change line 213 to While v <= 1 * #PI to get semi sphere or semi tear drop
**note: if you don't like the OpenGLGadget special keys (i don't like it also) then use the PB Ogre usual keys by loading the 3d engine together with openglGadget: look my previous example http://purebasic.fr/english/viewtopic.php?f=36&t=65658
tested in winxp/32 and ubunto/x64
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()
Declare FillArray()
Global.f txu, txv
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+"axes.png")
Case 2
LoadImage(i, #ImagePath+"terrain_texture.jpg")
Case 3
LoadImage(i, #ImagePath+"ValetCoeur.jpg")
Case 4
LoadImage(i, #ImagePath+"MRAMOR6X6.jpg")
Case 5
LoadImage(i, #ImagePath+"Geebee2.bmp")
Case 6
LoadImage(i, #ImagePath+"clouds.jpg")
Case 7
LoadImage(i, #ImagePath+"Wood.jpg")
EndSelect
*Buffer(i) = EncodeImage(i) ; default is: #PB_ImagePlugin_BMP : encode the image in BMP
Next
Dim TexID(7)
ExamineDesktops()
OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), "press '+' to go to next texture, ..... Up/Down: zoom in/out... 'W': wire frame... 'A'/'Z': change Camera view")
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))
glLoadIdentity_();
gluPerspective_(45.0, DesktopWidth(0)/DesktopHeight(0), 1.0, 60.0)
glTranslatef_(0, 0, -5)
glEnable_(#GL_DEPTH_TEST)
Global NbX = 100
Global NbZ = 100
Global Dim Point3D.Point3D(0)
Global Dim MeshDataInd.PB_MeshFace(0)
Global indexsize ;= ArraySize(MeshDataInd()) + 1
;Debug indexsize
;FillArray()
FillArray()
;Debug VertexIndex
;=================================================================================
rot.f = 1
;glPointSize_( 3 )
;glTranslatef_(0.0, 0.0, -30)
SetActiveGadget(0) ; make the openGLgadget active
glPushMatrix_(); to store the current state
glMatrixMode_(#GL_PROJECTION)
glLoadIdentity_();
gluPerspective_(45.0, WindowWidth(0)/WindowHeight(0), 1.0, 800.0)
glMatrixMode_(#GL_MODELVIEW)
glTranslatef_(0, 0, -1)
glShadeModel_(#GL_SMOOTH)
glEnable_(#GL_DEPTH_TEST)
glViewport_(0, 0, WindowWidth(0), WindowHeight(0))
gluLookAt_( 0, 1, 1, ; the camera looking from position 0,0.3,0.5 to 0,0,0 from above
0, 0, 0,
0, 1, 0 )
glPolygonMode_(#GL_FRONT_AND_BACK, #GL_LINE )
glPopMatrix_()
glPushMatrix_()
t=1: zz=30
;Debug indexsize
Repeat
Event = WindowEvent()
glViewport_(0, 0, WindowWidth(0), WindowHeight(0))
glClearColor_(0.9, 0.9, 0.9, 1)
;glClearColor_(0.3, 0.3, 0.3, 1)
glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
glEnableClientState_(#GL_VERTEX_ARRAY )
glEnableClientState_(#GL_COLOR_ARRAY)
glEnableClientState_(#GL_TEXTURE_COORD_ARRAY)
glRotatef_(rot/5, 0, 1, 0);
glVertexPointer_(3, #GL_FLOAT,SizeOf(Point3D),@Point3D(0)\x)
glColorPointer_(3, #GL_FLOAT, SizeOf(Point3D), @Point3D(0)\r)
glTexCoordPointer_(2, #GL_FLOAT, SizeOf(Point3D), @Point3D(0)\tu)
;glDrawElements_(#GL_LINES,indexsize,#GL_UNSIGNED_INT, @MeshDataInd(0)\Index)
;glDrawElements_(#GL_POINTS,indexsize,#GL_UNSIGNED_INT, @MeshDataInd(0)\Index)
;glDrawElements_(#GL_TRIANGLES,indexsize,#GL_UNSIGNED_INT, @MeshDataInd(0)\Index)
glDrawElements_(#GL_QUADS,indexsize,#GL_UNSIGNED_INT, @MeshDataInd(0)\Index)
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_KeyDown ; 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
ElseIf Key = #PB_Shortcut_W; display wire Frame or solid frame
If fill
glPolygonMode_(#GL_FRONT_AND_BACK, #GL_LINE)
fill ! 1
Else
glPolygonMode_(#GL_FRONT_AND_BACK, #GL_FILL )
fill ! 1
EndIf
ElseIf Key = #PB_Shortcut_Up
glScalef_(1.05,1.05,1.05)
ElseIf Key = #PB_Shortcut_Down
glScalef_(0.95,0.95,0.95)
ElseIf Key = #PB_Shortcut_A
glMatrixMode_(#GL_MODELVIEW);
glLoadIdentity_() ;
gluLookAt_(0, 3, 1, 0,0,0, 0,1,0 )
ElseIf Key = #PB_Shortcut_Z
glMatrixMode_(#GL_MODELVIEW);
glLoadIdentity_();
gluLookAt_(0, 1, 2, 0,0,0, 0,1,0 )
EndIf
EndIf
EndIf
SetGadgetAttribute(0, #PB_OpenGL_FlipBuffers, #True)
Delay(5)
Until Event = #PB_Event_CloseWindow Or quit = 1
For i=1 To 7
glDeleteTextures_(1, @TexID(i))
Next
Macro Vertex(u, v, txu, txv)
;this produce very beautiful shape
x.f = 0.5 * (1 - Cos(u)) * Sin(u) * Cos(v)
y.f = 0.5 * (1 - Cos(u)) * Sin(u) * Sin(v)
z.f = Cos(u)
;this produce a sphere shape
;x.f = 0.5 * Cos(u)* Sin(v)
;y.f = 0.5 * Sin(u)* Sin(v)
;z.f = 0.5 * Cos(v)
ReDim Point3D.Point3D(a)
Point3D(a)\x = x*1
Point3D(a)\y = y*1
Point3D(a)\z = z*1
Point3D(a)\tu = txu
Point3D(a)\tv = txv
Point3D(a)\r = 1.0 :Point3D(a)\g = 1.0 :Point3D(a)\b = 1
a+1
EndMacro
Procedure FillArray()
Protected.f u, v, Delta = 4*#PI/NbX ; change 4 to 2 to get more quads
Protected.f x, y, z
txu.f : txv.f
;Debug delta
u = 0
v = 0
While v <= 2 * #PI
While u <= 1 * #PI ; change 2 to 1 and will get half of the object
Vertex(u, v, txu, txv)
Vertex(u + Delta, v, txu+u/NbX*#PI, txv)
Vertex(u + Delta, v + Delta, txu+u/NbX*#PI, txv+v/NbX*#PI)
Vertex(u, v + Delta, txu, txv+v/NbX*#PI)
txu = txu + u/NbX*#PI ; for texturing
ReDim MeshDataInd.PB_MeshFace(VertexIndex+4)
MeshDataInd(VertexIndex)\Index = VertexIndex + 0
MeshDataInd(VertexIndex+1)\Index = VertexIndex + 1
MeshDataInd(VertexIndex+2)\Index = VertexIndex + 2
MeshDataInd(VertexIndex+3)\Index = VertexIndex + 3
VertexIndex + 4
u + Delta
Wend
txu = 0
txv = txv + v/NbX*1*#PI ; for texturing
u = 0
v + Delta
Wend
indexsize = ArraySize(MeshDataInd())
;Debug indexsize
;Debug VertexIndex
EndProcedure