Texturing a 3D Points Curve in OpenGL (with OpenGLGadget)

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

Texturing a 3D Points Curve in OpenGL (with OpenGLGadget)

Post by applePi »

graphics engine used classic opengl
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)
why should i use 57 ?? i have found it by experiment. 54 is the size of the bmp Header, using other than 57 the image either shifted or colored wrongly
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))
we will see a bmp without header so using it with glTexImage2D_ works okay, while in my example i should add 57 to the *Buffer. just issue for contemplation .

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

Image

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
  
Last edited by applePi on Mon Jan 11, 2016 12:32 pm, edited 2 times in total.
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Texturing a 3D Points Curve in OpenGL

Post by davido »

@applePi,

Thank you for a very nice example of the OpenGLGadget. :D
Tested on MacBook Pro i7
DE AA EB
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Texturing a 3D Points Curve in OpenGL

Post by applePi »

MutiTextures transparent Cube
Thank you davido, now a demo with the same line of thought as above.
all the credit to JHPJHP for his multitextures cube for the example in his "PureBasic Interface to OpenCV" look the thread http://purebasic.fr/english/viewtopic.p ... 55#p447832 merging OpenCV with OpenGL examples found in the download OpenCV_32/64.zip Page 1: pb_gl_photo_cube.pb, pb_cam_gl_cube.pb
also i have used the transparency functions posted by hagibaba in Nehe Lesson 8 http://purebasic.fr/english/viewtopic.p ... 2&p=176465
i have packaged a working version of hagibaba version for newer PB's (very little changes, but can't remember, it is a year ago, compare the file size with the sizes with OpenGLGadget)
http://wikisend.com/download/682460/Lesson08.rar (rotate the cube with arrow keys, press 'B' for transparent cube, don't know if it will work in other than windows).


MutiTextures transparent Cube with OpenGLGadget
Image

Code: Select all

EnableExplicit


UseJPEGImageDecoder()
UsePNGImageDecoder()

Define.l i, Event, key, quit, t
Define Rot.f

#ImagePath = #PB_Compiler_Home + "examples/3d/Data/Textures/"

Dim *Buffer(6)

For i=1 To 6
  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")
   EndSelect 
   *Buffer(i) = EncodeImage(i)
Next

Dim TexID(6)

OpenWindow(0, 0, 0, 800, 600, "OpenGL demo .. Transparent MultiTextures Cube")
SetWindowColor(0, 0)
OpenGLGadget(0, 0, 0, WindowWidth(0) , WindowHeight(0), #PB_OpenGL_Keyboard)

; ----- Generate texture
For i = 1 To 6
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 6
  FreeMemory(*Buffer(i))
Next

glMatrixMode_(#GL_PROJECTION)
glLoadIdentity_() ;Reset The Projection Matrix
gluPerspective_(30.0, Abs(WindowWidth(0) / WindowHeight(0)), 0.1, 500.0)
gluLookAt_( 0, 5, 0,   ; camera positioned in 0,5,0 looking at 0,0,-7.5
            0, 0, -7.5,
            0, 1, 0 ) 
glMatrixMode_(#GL_MODELVIEW)
glLoadIdentity_() ;Reset The Modelview Matrix
glColor4f_(1.0,1.0,1.0,  0.50) ;Full Brightness. 50% Alpha ( NEW )
glBlendFunc_(#GL_SRC_ALPHA,#GL_ONE) ;Blending Function For Translucency Based On Source Alpha Value ( NEW )
 
SetActiveGadget(0) ; make the openGLgadget active
Repeat
  Event = WindowEvent()
  Rot.f+0.7
  ;glEnable_(#GL_CULL_FACE) 

  glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)   ; Clear screen and depth buffer
  glLoadIdentity_()                                       ; Reset current modelview matrix
  glTranslatef_(0.0, 0.0, -7.5)
  
  glRotatef_(Rot, 0.0, 1.0, 0.0)
  glEnable_(#GL_TEXTURE_2D)                               ; Enable texture mapping 
  glEnable_(#GL_BLEND);
  glDisable_(#GL_DEPTH_TEST)
  
  glBindTexture_(#GL_TEXTURE_2D, TexID(1))  ; Start drawing the cube
  glBegin_(#GL_QUADS)                                     
    ; Front Face 
    glNormal3f_(0.0, 0.0, 1.0)
    glTexCoord2f_( 0.0, 0.0) : glVertex3f_( -1.0, -1.0,  1.0)
    glTexCoord2f_( 1.0, 0.0): glVertex3f_(  1.0, -1.0,  1.0)
    glTexCoord2f_( 1.0, 1.0) : glVertex3f_(  1.0,  1.0,  1.0)
    glTexCoord2f_( 0.0, 1.0) : glVertex3f_( -1.0,  1.0,  1.0)
  glEnd_()
      glBindTexture_(#GL_TEXTURE_2D, TexID(2))
      glBegin_(#GL_QUADS)
    ;Back Face
    glNormal3f_(0.0, 0.0, -1.0)
    glTexCoord2f_( 1.0, 0.0) : glVertex3f_( -1.0, -1.0, -1.0)
    glTexCoord2f_( 1.0, 1.0) : glVertex3f_( -1.0,  1.0, -1.0)
    glTexCoord2f_( 0.0, 1.0) : glVertex3f_(  1.0,  1.0, -1.0)
    glTexCoord2f_( 0.0, 0.0) : glVertex3f_(  1.0, -1.0, -1.0)
  
  glEnd_()
  
  glBindTexture_(#GL_TEXTURE_2D, TexID(3))
    glBegin_(#GL_QUADS)
    ;Top Face
    glNormal3f_(0.0, 1.0, 0.0)
    glTexCoord2f_( 0.0, 1.0) : glVertex3f_( -1.0,  1.0, -1.0)
    glTexCoord2f_( 0.0, 0.0) : glVertex3f_( -1.0,  1.0,  1.0)
    glTexCoord2f_( 1.0, 0.0) : glVertex3f_(  1.0,  1.0,  1.0)
    glTexCoord2f_( 1.0, 1.0) : glVertex3f_(  1.0,  1.0, -1.0)
    glEnd_()
    ;glEnable_(#GL_CULL_FACE)
      glBindTexture_(#GL_TEXTURE_2D, TexID(4))
      glBegin_(#GL_QUADS)
    ;Bottom Face
    glNormal3f_(0.0, -1.0, 0.0)
    glTexCoord2f_( 1.0, 1.0) : glVertex3f_( -1.0, -1.0, -1.0)
    glTexCoord2f_( 0.0, 1.0) : glVertex3f_(  1.0, -1.0, -1.0)
    glTexCoord2f_( 0.0, 0.0) : glVertex3f_(  1.0, -1.0,  1.0)
    glTexCoord2f_( 1.0, 0.0) : glVertex3f_( -1.0, -1.0,  1.0)
  glEnd_()
      glBindTexture_(#GL_TEXTURE_2D, TexID(5))
      glBegin_(#GL_QUADS)
    ;Right face 
    glNormal3f_(1.0, 0.0, 0.0)
    glTexCoord2f_( 1.0, 0.0) : glVertex3f_(  1.0, -1.0, -1.0)
    glTexCoord2f_( 1.0, 1.0) : glVertex3f_(  1.0,  1.0, -1.0)
    glTexCoord2f_( 0.0, 1.0) : glVertex3f_(  1.0,  1.0,  1.0)
    glTexCoord2f_( 0.0, 0.0) : glVertex3f_(  1.0, -1.0,  1.0)
    glEnd_()
      glBindTexture_(#GL_TEXTURE_2D, TexID(6))
      glBegin_(#GL_QUADS)
    ;Left Face
    glNormal3f_(-1.0, 0.0, 0.0)
    glTexCoord2f_( 0.0, 0.0) : glVertex3f_( -1.0, -1.0, -1.0)
    glTexCoord2f_( 1.0, 0.0) : glVertex3f_( -1.0, -1.0,  1.0)
    glTexCoord2f_( 1.0, 1.0) : glVertex3f_( -1.0,  1.0,  1.0)
    glTexCoord2f_( 0.0, 1.0) : glVertex3f_( -1.0,  1.0, -1.0)
    
  glEnd_()
  
  If Event = #PB_Event_Gadget And EventGadget() = 0 
   If EventType() = #PB_EventType_KeyDown ; like KeyboardPush
            key = GetGadgetAttribute(0,#PB_OpenGL_Key )
            If 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 6
      glDeleteTextures_(1, @TexID(i))
Next
  
Post Reply