Author Topic: OpenGL Context Capture Demo  (Read 18299 times)

0 Members and 1 Guest are viewing this topic.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
OpenGL Context Capture Demo
« on: June 18, 2018, 05:30:30 am »
Hi everyone!
Now finally, it is possible to make OpenGL frame retain on screen.
The program code below does this -

Controls -  Press Space to capture OpenGL context and 'c' to clear software screen.


Code: QB64: [Select]
  1. 'OpenGL context capture example
  2. 'by Ashish Kushwaha
  3.  
  4. _TITLE "Hit *Space* to capture GL context and 'c' to clear software screen"
  5. SCREEN _NEWIMAGE(600, 600, 32)
  6.  
  7. DIM SHARED glAllow AS _BYTE
  8. DIM SHARED GL_Color_Buffer~%%(_WIDTH * _HEIGHT * 3)
  9. DIM SHARED keyHit AS LONG
  10.  
  11. DIM SHARED GL_Context&, buffer_done
  12. buffer_done = 0
  13. 'CLS
  14. glAllow = -1
  15.     keyHit = _KEYHIT
  16.     IF keyHit = ASC("c") THEN CLS , 1
  17.     IF buffer_done THEN
  18.         _CLEARCOLOR _RGB(0, 0, 0), GL_Context&
  19.         _PUTIMAGE , GL_Context&
  20.         _FREEIMAGE GL_Context&
  21.         buffer_done = 0
  22.     END IF
  23.     _DISPLAY
  24.     _LIMIT 30
  25.  
  26.  
  27. SUB _GL ()
  28.     STATIC fps AS LONG
  29.     IF NOT glAllow THEN EXIT SUB
  30.  
  31.     '_glDisable _GL_MULTISAMPLE <<<< uncomment this line if you are using latest (06/18/2018) build of QB64.
  32.  
  33.  
  34.     _glClearColor 0, 0, 0, 1
  35.     _glClear _GL_COLOR_BUFFER_BIT
  36.  
  37.     _glMatrixMode _GL_MODELVIEW
  38.  
  39.     _glRotatef fps, 0, 0, 1
  40.  
  41.     _glBegin _GL_TRIANGLES
  42.  
  43.     _glColor3f 1, 0, 0
  44.     _glVertex2f 0, 1
  45.     _glColor3f 0, 1, 0
  46.     _glVertex2f -1, -1
  47.     _glColor3f 0, 0, 1
  48.     _glVertex2f 1, -1
  49.     _glEnd
  50.  
  51.     IF keyHit = ASC(" ") AND NOT buffer_done THEN GL_Context& = getOpenGLContextImage: buffer_done = 1
  52.  
  53.     _glFlush
  54.  
  55.     fps = fps + 1
  56.  
  57. FUNCTION getOpenGLContextImage& ()
  58.     'storing GL Color Buffer in our  GL_Color_Buffer() array
  59.     _glReadBuffer _GL_BACK
  60.     _glPixelStorei _GL_UNPACK_ALIGNMENT, 1
  61.     _glReadPixels 0, 0, _WIDTH, _HEIGHT, _GL_RGB, _GL_UNSIGNED_BYTE, _OFFSET(GL_Color_Buffer~%%())
  62.  
  63.     getOpenGLContextImage& = _NEWIMAGE(_WIDTH, _HEIGHT, 32) 'create an image handle
  64.     DIM m AS _MEM
  65.     m = _MEMIMAGE(getOpenGLContextImage&) 'store it in memory
  66.     i& = 0
  67.     FOR y = _HEIGHT(getOpenGLContextImage&) - 1 TO 0 STEP -1
  68.         FOR x = 0 TO _WIDTH(getOpenGLContextImage&) - 1
  69.             index& = 4 * (x + y * _WIDTH(getOpenGLContextImage&))
  70.             _MEMPUT m, m.OFFSET + index&, GL_Color_Buffer~%%(i& + 2) AS _UNSIGNED _BYTE 'blue
  71.             _MEMPUT m, m.OFFSET + index& + 1, GL_Color_Buffer~%%(i& + 1) AS _UNSIGNED _BYTE 'green
  72.             _MEMPUT m, m.OFFSET + index& + 2, GL_Color_Buffer~%%(i&) AS _UNSIGNED _BYTE 'red
  73.             _MEMPUT m, m.OFFSET + index& + 3, 255 AS _UNSIGNED _BYTE 'alpha
  74.             i& = i& + 3
  75.     NEXT x, y
  76.     _MEMFREE m
  77.  
  78.  
« Last Edit: June 18, 2018, 07:58:20 am by Ashish »
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: OpenGL Context Capture Demo
« Reply #1 on: June 18, 2018, 04:13:39 pm »
Thanks for sharing. This is very useful, knowing how to do it.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: OpenGL Context Capture Demo
« Reply #2 on: June 19, 2018, 03:43:03 am »
Thanks for sharing. This is very useful, knowing how to do it.
I'm glad that it is useful to you. :)
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: OpenGL Context Capture Demo
« Reply #3 on: June 23, 2018, 11:47:53 am »
Here's a fading effect achieved by software screen -
Code: QB64: [Select]
  1. 'OpenGL context capture example
  2. 'by Ashish Kushwaha
  3.  
  4. _TITLE "Rendering OpenGL to software screen"
  5. SCREEN _NEWIMAGE(600, 600, 32)
  6.  
  7. DIM SHARED glAllow AS _BYTE
  8. DIM SHARED GL_Color_Buffer~%%((_WIDTH * _HEIGHT * 4) - 1)
  9. DIM SHARED keyHit AS LONG
  10.  
  11. DIM SHARED GL_Context&, buffer_done
  12.  
  13. buffer_done = 0
  14. 'CLS
  15. glAllow = -1
  16.     keyHit = _KEYHIT
  17.  
  18.     IF buffer_done THEN
  19.         _CLEARCOLOR _RGB(0, 0, 0), GL_Context&
  20.         _PUTIMAGE (0, _HEIGHT - 1)-(_WIDTH - 1, 0), GL_Context& 'flip again the image (opengl returns a vertically fliped image)
  21.         _FREEIMAGE GL_Context&
  22.         buffer_done = 0
  23.     END IF
  24.     LINE (0, 0)-(600, 600), _RGBA(0, 0, 0, 10), BF
  25.     _DISPLAY
  26.     _LIMIT 60
  27.  
  28.  
  29. SUB _GL ()
  30.     STATIC fps AS LONG, tt#
  31.     IF NOT glAllow THEN EXIT SUB
  32.     IF fps = 0 THEN tt# = TIMER
  33.  
  34.  
  35.     _glMatrixMode _GL_MODELVIEW
  36.  
  37.     _glRotatef fps, 0, 0, 1
  38.  
  39.     _glBegin _GL_TRIANGLES
  40.  
  41.     _glColor3f 1, 0, 0
  42.     _glVertex2f 0, 1
  43.     _glColor3f 0, 1, 0
  44.     _glVertex2f -1, -1
  45.     _glColor3f 0, 0, 1
  46.     _glVertex2f 1, -1
  47.     _glEnd
  48.  
  49.  
  50.     IF NOT buffer_done THEN GL_Context& = getOpenGLContextImage: buffer_done = 1
  51.  
  52.     _glFlush
  53.  
  54.     fps = fps + 1
  55.  
  56. FUNCTION getOpenGLContextImage& ()
  57.     'storing GL Color Buffer in our  GL_Color_Buffer() array
  58.     _glReadBuffer _GL_BACK
  59.     _glPixelStorei _GL_UNPACK_ALIGNMENT, 1
  60.     _glReadPixels 0, 0, _WIDTH, _HEIGHT, _GL_BGRA_EXT, _GL_UNSIGNED_BYTE, _OFFSET(GL_Color_Buffer~%%())
  61.  
  62.  
  63.     ' the below one will store vertically flip image
  64.     getOpenGLContextImage& = _NEWIMAGE(_WIDTH, _HEIGHT, 32) 'create an image handle
  65.     DIM m AS _MEM, m2 AS _MEM
  66.  
  67.     m = _MEMIMAGE(getOpenGLContextImage&) 'store it in memory
  68.     m2 = _MEM(GL_Color_Buffer~%%())
  69.  
  70.     _MEMCOPY m2, m2.OFFSET, m2.SIZE TO m, m.OFFSET 'using _MEMCOPY instead of nested loop
  71.     _MEMFREE m
  72.     _MEMFREE m2
  73.  
  74.  
  75.  
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

FellippeHeitor

  • Guest
Re: OpenGL Context Capture Demo
« Reply #4 on: November 01, 2020, 07:34:39 am »
Old topic but somehow I missed this: great job, Ashish!