Author Topic: OpenGL flag  (Read 7803 times)

0 Members and 1 Guest are viewing this topic.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
OpenGL flag
« on: July 26, 2017, 11:28:29 am »
Hi. This is the solution of the flag movement through OpenGL. I suppose this can be done better, and any suggestions and remarks are welcome.
Please Odin to consider whether it would be appropriate to post OpenGL contributions to a special thread in the future. Thank you.

Code: QB64: [Select]
  1. posuvMx = .12 'X sinus speed
  2. posuvbmx = .32 'Y sinus speed
  3.  
  4. z = 10
  5. MoX = .5
  6. MoY = .5
  7.  
  8. my& = _NEWIMAGE(1024, 768, 32): _FULLSCREEN
  9. s& = _LOADIMAGE("czv.png", 32)
  10.  
  11. '_DISPLAYORDER _GLRENDER: ' _SCREENMOVE _MIDDLE  'FAILS if is this uncommented, because screen position create _glOrtho in this case!!!!
  12.  
  13. TYPE points
  14.     R AS _FLOAT
  15.     G AS _FLOAT
  16.     B AS _FLOAT
  17.  
  18.  
  19. DIM SHARED points(1024 * 768) AS points
  20. H = _HEIGHT(s&)
  21. w = _WIDTH(s&)
  22.  
  23. FOR yp = 0 TO H - 1 STEP 1
  24.     FOR xp = w - 1 TO 0 STEP -1
  25.         points(pc).R = _RED32(POINT(xp, yp)) / 256 'for correct color to use in opengl muss be values converted to range 0 - 1 for statement _glColor2f
  26.         points(pc).G = _GREEN32(POINT(xp, yp)) / 256
  27.         points(pc).B = _BLUE32(POINT(xp, yp)) / 256
  28.         pc = pc + 1
  29. NEXT xp, yp
  30.  
  31.  
  32.  
  33.  
  34.         MoX = _MOUSEX / 2
  35.         MoY = _MOUSEY / 4
  36.     LOOP
  37.     i$ = INKEY$
  38.     IF i$ = CHR$(27) THEN END
  39.     IF i$ = "z" THEN z = z + .1
  40.     IF i$ = "x" THEN z = z - .1
  41.     _DEST 0
  42.     LOCATE 20, 1: PRINT TIME$
  43.     LOCATE 23, 1: PRINT "The flag of the Czech Republic. OpenGL demo by Petr. Use mouse, z or x for zoom."
  44.     _DEST my&
  45.     _DISPLAY
  46.  
  47.  
  48. SUB _GL () STATIC
  49. SHARED posuvMx, posuvbmx, posuvb, x, y, sinus, posuv, H, w, s&, MoX, MoY
  50.  
  51. _glMatrixMode _GL_PROJECTION
  52. _glOrtho (-w / 2) * z, (w / 2) * z * (2 * MoX), (-H / 3) * z, (H / 3) * (2 * MoY) * z, -1, 1
  53. _glEnable _GL_DEPTH_TEST
  54.  
  55.  
  56. _glBegin _GL_POINTS
  57. posuv = posuv + posuvMx
  58. IF posuv > 1.7 OR posuv < -1.7 THEN posuvMx = posuvMx * -1
  59. posuvb = posuvb + posuvbmx
  60. IF posuvb > 1.7 OR posuvb < -1.7 THEN posuvbmx = posuvbmx * -1
  61.  
  62.  
  63. pc = 0
  64.  
  65. _glClear _GL_COLOR_BUFFER_BIT
  66. FOR y = 1 TO H
  67.     bs = bs - .034
  68.     FOR x = 1 TO w
  69.         sinus = sinus + .024 'sinus + periode
  70.         _glColor3f points(pc).R, points(pc).G, points(pc).B
  71.         _glVertex2f H - (x - 0) + SIN(bs) * posuvb * 12, w - y - 0 + SIN(sinus) * 22 * posuv '_RGB32(_RED32(bod&), _GREEN32(bod&), _BLUE32(bod&)) '   cislo urcuje vysku oblouku
  72.         pc = pc + 1
  73.     NEXT x
  74.     sinus = 0
  75.     _glClear _GL_COLOR_BUFFER_BIT
  76.     _glClear _GL_DEPTH_BUFFER_BIT
  77. bs = 0
  78.  
  79.  
  80.  
czv.png
* czv.png (Filesize: 14.17 KB, Dimensions: 300x170, Views: 766)

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: OpenGL flag
« Reply #1 on: July 27, 2017, 06:09:28 am »
Wow! :O Impressive work! I noticed that the flag failed to follow the mouse. :)
Also, it get shrink near the left & become wide at the right.
I suggest you to use _glTranslatef instead of _glOrtho.
By the way, You have good progress in OpenGL :D
« Last Edit: July 27, 2017, 09:10:15 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 flag
« Reply #2 on: July 27, 2017, 11:56:34 am »
Hi Ashish. I wanted to do the flying flag a long time ago, but only now I made it. For more sophisticated graphics, I will try in future to do the next waveform, which varies with the distance from the end of the flag. OpenGL coordinates -1 0 1 and their origin in the lower right corner, and the idea of the Z axis makes me pretty confused in my head. But it's fun. Mouse reaction - i made it better in next time.
  Thank you for the reaction when no one reaction is going, so I say - is that stupid that no one cares or is it too complicated? If is none reactions, is difficult to judge it.

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: OpenGL flag
« Reply #3 on: July 27, 2017, 02:19:53 pm »
Heya Petr,

I like the demo so far. I also had a spot of trouble controlling the view at first, but once I got the hang of it, things were fine. I posted this demo on *.net on July 4th right before it went down, and then again on N54, and also like 5 times in the past around this time of year - which is why we need a good Samples/Archives section... But if you want to study waves through a flag, I recommend having a look my non-gl QB45 code:

https://qb64.org/basbin/5CC7.txt

(Note the flag is not technically fabric, but a stretchy membrane.)
You're not done when it works, you're done when it's right.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: OpenGL flag
« Reply #4 on: July 27, 2017, 03:05:48 pm »
Hi STxAxTIC, thank for the link, this long code muss i studying. Your code is big work! I like it.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: OpenGL flag
« Reply #5 on: July 28, 2017, 05:02:45 am »
@STxAxTIC
That demo is awesome! :)
if (Me.success) {Me.improve()} else {Me.tryAgain()}


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