Page 1 of 1

glDrawRangeElements example

Posted: Tue May 03, 2016 4:05 pm
by applePi
the example is using OpenGLGadget
glDrawRangeElements is all in one function, its declaration needs to be added to the PB for easier usage, like glDrawElements
for its description look:
http://docs.gl/gl2/glDrawRangeElements
http://www.ibm.com/support/knowledgecen ... ements.htm

Image

to remove the small black triangle (the bird hat) change in line 111 ArraySize(Vertex()) to ArraySize(Vertex()) - 3

Code: Select all

Structure Point3D
  x.f
  y.f
  z.f
  r.f ;red
  g.f ;green
  b.f ;blue
EndStructure
; the macro purpose is to write the array like in C in one shot approx
Macro MakeArray(tempArr, Vertex, items) ; adapted from expert PB example
  For i=1 To CountString(items,",")+1
    tempArr(i-1)\x=ValF(StringField(items,i,","))
    tempArr(i-1)\y=ValF(StringField(items,i+1,","))
    tempArr(i-1)\z=ValF(StringField(items,i+2,","))
    tempArr(i-1)\r=ValF(StringField(items,i+3,","))
    tempArr(i-1)\g=ValF(StringField(items,i+4,","))
    tempArr(i-1)\b=ValF(StringField(items,i+5,","))
  Next
  
  For i=0 To CountString(items,",") / 6 ; number of vertices
  Vertex(i)\x = tempArr(i*6)\x
  Vertex(i)\y = tempArr(i*6)\y
  Vertex(i)\z = tempArr(i*6)\z
  Vertex(i)\r = tempArr(i*6)\r
  Vertex(i)\g = tempArr(i*6)\g
  Vertex(i)\b = tempArr(i*6)\b
Next
EndMacro

OpenWindow(0, 0, 0, 800, 600, "OpenGL demo ... glDrawRangeElements")
SetWindowColor(0, RGB(200,220,200))
OpenGLGadget(0, 20, 10, WindowWidth(0)-40 , WindowHeight(0)-20)

Prototype glDrawRangeElements (mode, start, end_, count, type, indices)
Global glDrawRangeElements_.glDrawRangeElements
glDrawRangeElements_ = wglGetProcAddress_("glDrawRangeElements")

glMatrixMode_(#GL_PROJECTION)
glLoadIdentity_();
gluPerspective_(45.0, WindowWidth(0)/WindowHeight(0), 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, WindowWidth(0), WindowHeight(0))

numOfVertices = 13
Dim Vertex.Point3D(numOfVertices - 1) ; 0 to 12 points
Dim tempArr.Point3D(numOfVertices * 6 -1) ;
Define event, quit
; vertex number...........0                 1               2               3                 4               5                 6              7                8               9              10                    11                 12               
MakeArray(tempArr , Vertex, "-1,0,-0.5,0,1,1,  -1,0,0.5,1,1,0,  1,0,0.5,0,1,1,  1,0,-0.5,1,1,0,  -1.5,0,0,1,0,0,  1.5,0.5,0,0,0,1,  0,0,-1,1,0,0,  0,0,-0.5,1,0,0,  0,0,0.5,1,0,0,  0,0,1,1,0,0,  -1.2,0.1,-0.2,0,0,0,  -1.4,0.1,0,0,0,0,  -1.2,0.1,0.2,0,0,0")

;=================================================================================

Dim index.l(20) ; the connections between points refered to by these index array values
index(0) = 0 : index(1) = 1:  index(2) = 2 ; triangle 0
index(3) = 0 : index(4) = 2:  index(5) = 3
index(6) = 0 : index(7) = 4:  index(8) = 1
index(9) = 3 : index(10) = 2: index(11) = 5
index(12) = 6: index(13) = 0: index(14) = 7
index(15) = 1: index(16) = 9: index(17) = 8
index(18) = 10:index(19) = 11:index(20) = 12 ; triangle 6

indexsize = ArraySize(index()) + 1
;Debug indexsize

gluLookAt_( 0, 0.3, 0.5, ; we are looking from position 0,0.3,0.5  to 0,0,0 from above
              0,  0, 0,
              0, 1,  0 ) ;change this vector to 0,-1,0 and we wil; look from below
Global signo=1
;glRotatef_(70, 0, 1, 0)
Repeat
  Repeat
    event = WindowEvent()
    If event = #PB_Event_CloseWindow
      quit = #True
    EndIf
  Until event = 0 Or quit = #True
   
  yy.f+ signo*0.02
  If Abs(yy) >= 1
    signo * -1 ; this change the yy increment or decrement, so vertex(6)\y and vertex(9)\y will go up and down for animation
  EndIf
    
  Vertex(6)\x = 0
  Vertex(6)\y = yy
  Vertex(6)\z = -1
  Vertex(9)\x = 0
  Vertex(9)\y = yy
  Vertex(9)\z = 1

  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)
  
  glVertexPointer_(3, #GL_FLOAT,SizeOf(Point3D),@Vertex(0)\x)
  glColorPointer_(3, #GL_FLOAT, SizeOf(Point3D), @Vertex(0)\r) 
  
  ;glPointSize_(12) ; used if you choose #GL_POINTS
  ;glLineWidth_(5)  ; used if you choose #GL_LINES
  
  glRotatef_(0.5, 0, 1, 0)
  ;glDrawElements_(#GL_TRIANGLES,indexsize,#GL_UNSIGNED_INT, @index(0))
  ;glDrawRangeElements_(#GL_POINTS, 0, ArraySize(Vertex()), indexsize, #GL_UNSIGNED_INT, @index(0))
  glDrawRangeElements_(#GL_TRIANGLES, 0, ArraySize(Vertex()), indexsize, #GL_UNSIGNED_INT, @index(0))

  glDisableClientState_(#GL_VERTEX_ARRAY)
  glDisableClientState_(#GL_COLOR_ARRAY)

  SetGadgetAttribute(0, #PB_OpenGL_FlipBuffers, #True)

Until quit = #True