OpenGL examples with OpenGLGadget (modern and old demos)

Everything related to 3D programming
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: OpenGL examples with OpenGLGadget (modern and old demos)

Post by applePi »

at last it works, this is the grid i want to connect the 3D points with LINE_STRIP using #GL_LINE_STRIP key word
Image
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
Last edited by applePi on Mon Jul 10, 2017 9:04 am, edited 2 times in total.
User avatar
JHPJHP
Addict
Addict
Posts: 2257
Joined: Sat Oct 09, 2010 3:47 am

Re: OpenGL examples with OpenGLGadget (modern and old demos)

Post by JHPJHP »

Hi applePi,

Looks great... I'm just getting a taste for this GL stuff, wish I had more time to spend on it; application use seems endless.

Thank you.
Last edited by JHPJHP on Fri Jul 11, 2014 6:46 am, edited 1 time in total.

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: OpenGL examples with OpenGLGadget (modern and old demos)

Post by applePi »

thank you JHPJHP, yes it is too big graphics library, and needs the one to be dedicated to it , this is why i restrict my activities to a very tiny part, the worst in the whats called modern OpenGL is that it depends on shaders the C like programs to instruct the GPU, they said that this allow the user a full control of the GPU, this way of talk is exactly like saying: "" i suggest to use assembly language since you will have a full control of your programs"" thats completely a wrong logic in what is supposed a star trek computer age, unless the person have nothing to do in the world except OpenGL programming, and i prefer the PB Ogre3D since it can be considered a wrapper to opengl. as you can see the indexed gemetry in the above example is almost the same as the equivalent PB Ogre3D example i posted at the end of this page http://purebasic.fr/english/viewtopic.p ... 13#p447827
to complete the above example : here is the same example but using glDrawElements with glVertexPointer and glColorPointer. in a subsequent article i will post a version for the vertex buffer object method and will be my last tour on this subject.
and thanks to Fred and his team for introducing the OpenGLGadget which makes the code size very small comparing to the very lengthy equivalent code in other languages.

Code: Select all

Structure TVertex
  x.f
  y.f
  z.f
  r.f
  g.f
  b.f
EndStructure


Define event, quit

OpenWindow(0, 0, 0, 800, 600, "OpenGL.. 3D Curve with Grid , using glDrawElements with glVertexPointer and glColorPointer ")
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=70
NbZ=70
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.2 :Vertex(v)\g = 0.8 :Vertex(v)\b = 1 
      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)
;Debug indexsize
rot.f = 1

glTranslatef_(0.0, 0.0, -2)
Repeat
  
  
  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)
  glRotatef_(rot, 1, 1, 0);
  
  glVertexPointer_(3, #GL_FLOAT,SizeOf(TVertex),Vertex(0))
  glColorPointer_(3, #GL_FLOAT, SizeOf(TVertex), @Vertex(0)\r) 
  
  ;glPointSize_(12)
  glDrawElements_(#GL_LINE_STRIP,indexsize,#GL_UNSIGNED_INT, @index(0))

  glDisableClientState_(#GL_VERTEX_ARRAY);
  glDisableClientState_(#GL_COLOR_ARRAY)
  
 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
P.S: I have posted a Textured OpenGL 3D curve here http://purebasic.fr/english/viewtopic.php?f=13&t=59903
Image
Last edited by applePi on Sun Jul 20, 2014 12:28 pm, edited 1 time in total.
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

Re: OpenGL examples with OpenGLGadget (modern and old demos)

Post by Mythros »

@applepi: Can we somehow make it so we can "throw a cloth over a point cloud", and then use that to turn a pointcloud completely texture / 3d?
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: OpenGL examples with OpenGLGadget (modern and old demos)

Post by applePi »

sorry Mythros , i can't answer the question since i am completely beginner and naive in OpenGL and will stay like that because it is very big and complex library and the life are too short to study just part of it. add to that the modern OpenGL use the shaders which are C programs to instruct the GPU. the users of programming languages other than C/C++ don't want the C language to visit them from the backdoor else they will stay in C republic. this is why we don't see modern Opengl examples in other languages. as an example i don't see code for the powerbasic, even there is a great opengl examples in its forum but it is all for the old opengl, i have made a tremendous research in those forums. in blitzmax we can see a trivial one or too simple examples. but we can use modern opengl if we want using purebasic as explained in page one here with simple examples.
now i saw a book called "Learning Game Physics with Bullet Physics and OpenGL" http://www.packtpub.com/learning-game-p ... pengl/book
don't know how much it is useful
Post Reply