
the grid are made from horizontal and vertical zig-zag lines, look at the end of my post http://purebasic.fr/english/viewtopic.p ... 13#p447827 for an explanation
i find it complex to draw a grid, it may be much simpler, but i have imitated my pencil and paper method on a small 3X3 points and when it worked applied it to this demo call it "OpenGL.. 3D Curve with Grid , using glDrawElements with glInterleavedArrays" and coloring the summit with red color.
use any function you want to draw, but remember to change the ranges in line 38:
xMin.f = -2 : yMin.f = -2: zMin.f = -2 : xMax.f = 2: yMax = 2 : zMax = 2
so to fit to the ranges of the function.
the resolution here is 31 X 31.
ie: in line 32-33
NbX=30
NbZ=30
change it to other resolutions such as 81 X 81
NbX=80
NbZ=80
the exact equivalent of this file in PB Ogre3D is at the remote end of this page:
http://www.purebasic.fr/english/viewtop ... 13#p447827
Code: Select all
Structure TVertex
r.f
g.f
b.f
x.f
y.f
z.f
EndStructure
Define event, quit
OpenWindow(0, 0, 0, 800, 600, "OpenGL.. 3D Curve with Grid , using glDrawElements with glInterleavedArrays")
SetWindowColor(0, RGB(200,220,200))
OpenGLGadget(0, 20, 10, WindowWidth(0)-40 , WindowHeight(0)-20)
glMatrixMode_(#GL_PROJECTION)
glLoadIdentity_();
gluPerspective_(45.0, 800/600, 1.0, 60.0)
glMatrixMode_(#GL_MODELVIEW)
glTranslatef_(0, 0, -5)
glShadeModel_(#GL_SMOOTH)
glEnable_(#GL_DEPTH_TEST)
;glEnable_(#GL_CULL_FACE)
glColor3f_(1.0, 0.3, 0.0)
glViewport_(0, 0, 800, 600)
NbX=30
NbZ=30
size = (NbX+1)*(NbZ+1)
Dim Vertex.TVertex(size)
;xMin.f = -1 : yMin.f = -1: zMin.f = -1 : xMax.f = 1: yMax = 1 : zMax = 1
xMin.f = -2 : yMin.f = -2: zMin.f = -2 : xMax.f = 2: yMax = 2 : zMax = 2
;xMin.f = -10 : yMin.f = -10: zMin.f = -10 : xMax.f = 10: yMax = 10 : zMax = 10
;xMin.f = -0.5 : zMin.f = -0.5 : xMax.f = 0.5: zMax = 0.5
range = xMax - xMin
step1.f = range / NbX
x.f = xMin: z.f = zMin : y.f = yMin : v.l = 0
For b=0 To NbZ
For a=0 To NbX
;y.f = Sin(10*(x*x+z*z))/5
y.f =(1 - x*x -z*z) * Exp(-1/2 * (x*x + z*z)) ; Mexican Hat
Vertex(v)\x = x*1
Vertex(v)\y = y*1
Vertex(v)\z = z*1
If y>=0
Vertex(v)\r = 1.0 :Vertex(v)\g = 0.3 :Vertex(v)\b = 0
Else
Vertex(v)\r = 0.7 :Vertex(v)\g = 0.8 :Vertex(v)\b = 0
EndIf
x.f + step1
v+1
Next a
x = xMin
z.f + step1
Next b
;Debug v
;=================================================================================
indexsize = (NbX+1)*(NbZ+1)*2
Dim index.l(indexsize)
;wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
direction = 0
For yy=0 To NbX
If direction = 1
direction ! 1
xx + NbX + 1
For i=xx To xx-NbX Step -1
index(ind)=i
ind+1
Next
xx+1
Else
direction ! 1
For i=xx To xx+NbX
index(ind)=i
ind+1
Next
xx = i - 1
EndIf
Next
xx=(NbX+1)*(NbZ+1) -1
direction = 0
For yy=0 To NbX
If direction = 1
direction ! 1
i=xx
x2 = xx+(NbX+1)*NbZ
While i<= xx+(NbX+1)*NbZ
index(ind)=i
ind+1
i+NbX+1
Wend
i-(NbX+1)
xx = i-1
Else
direction ! 1
i=xx
x2 = xx-(NbX+1)*NbZ
While i>= x2
index(ind)=i
ind+1
i-(NbX+1)
Wend
i+NbX+1
xx = i - 1
EndIf
Next
;indexsize = ind
rot.f = 1
glTranslatef_(0.0, 0.0, -2)
Repeat
glViewport_(0, 0, WindowWidth(0), WindowHeight(0))
;glClearColor_(0.1, 0.1, 0.5, 1)
glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
glRotatef_(rot, 1, 1, 0);
glInterleavedArrays_(#GL_C3F_V3F, 0, Vertex(0))
glDrawElements_(#GL_LINE_STRIP,indexsize,#GL_UNSIGNED_INT, @index(0))
Repeat
event = WindowEvent()
If event = #PB_Event_CloseWindow
quit = #True
EndIf
Until event = 0 Or quit = #True
SetGadgetAttribute(0, #PB_OpenGL_FlipBuffers, #True)
Delay(10)
Until quit = #True