Author Topic: OpenGL rotations  (Read 336 times)

Offline Richard Frost

  • Forum Regular
  • Posts: 145
  • Needle nardle noo. - Peter Sellers
OpenGL rotations
« on: July 07, 2020, 03:22:36 PM »
How does one apply a rotation centered on an object instead of the middle of the screen?
I'd like these 6 objects to stay in one place and spin.

Ashish's plasma cube was the starting point for this experimentation.

Code: QB64: [Select]
  1. ' keys xyz to toggle rotations, arrow up/down speed
  2.  
  3. _TITLE "Bonkers"
  4.     SUB gluLookAt (BYVAL eyeX#, BYVAL eyeY#, BYVAL eyeZ#, BYVAL centerX#, BYVAL centerY#, BYVAL centerZ#, BYVAL upX#, BYVAL upY#, BYVAL upZ#)
  5. DIM SHARED glAllow AS _BYTE, glinit, s&(6), s2&(6), d(23, 4), d, n, x, y, z, r
  6. SCREEN _NEWIMAGE(500, 500, 32)
  7. d = 4: n = 23: sp = 60: x = 1: y = 1: z = 1
  8. FOR i = 0 TO n
  9.     FOR j = 0 TO 4
  10.         t$ = MID$("zpnppppppppzpnpzznnpzpnpnppppnpzpnnzznnnzpnpnppppnpzpppzznppzpnnnpppnnpzpnpzznnpzpnpnppnpppznnpzznnnzpppnppppppzpnpzzpnn", i * 5 + j + 1, 1)
  11.         d(i, j) = (INSTR("nzp", t$) - 2) / 4 '          n=-1,z=0,p=1
  12.     NEXT j
  13. FOR p = 1 TO 6 '                                        generate 6 textures, one for each piece
  14.     FOR i = 0 TO 250
  15.         j = 255 - i
  16.         IF p = 1 THEN c& = _RGB(i / 2, j, j) '          w
  17.         IF p = 2 THEN c& = _RGB(j / 2, j, i) '          t
  18.         IF p = 3 THEN c& = _RGB(j, i / 2, i) '          u
  19.         IF p = 4 THEN c& = _RGB(i / 4, i, j / 2) '      x
  20.         IF p = 5 THEN c& = _RGB(j / 2, i / 2, i) '      t+
  21.         IF p = 6 THEN c& = _RGB(i, j, i) '              z
  22.         LINE (i, i)-(_WIDTH - i, _HEIGHT - i), c&, B
  23.     NEXT i
  24.     s&(p) = _COPYIMAGE(0)
  25. SCREEN _NEWIMAGE(800, 800, 32)
  26. glAllow = -1
  27.     i$ = INKEY$
  28.     IF i$ = "x" THEN x = x XOR 1 '                      x axis on/off
  29.     IF i$ = "y" THEN y = y XOR 1 '                      y
  30.     IF i$ = "z" THEN z = z XOR 1 '                      z
  31.     IF LEN(i$) = 2 THEN '                               maybe arrow key
  32.         kk = ASC(RIGHT$(i$, 1))
  33.         sp = sp + (kk = 80) * 10 - (kk = 72) * 10 '     speed
  34.         IF sp < 10 THEN sp = 10 '                       lower speed limit
  35.     END IF
  36.     r = (r + 1) MOD 360 '                               master rotation
  37.     _PRINTSTRING (0, 0), STR$(x) + STR$(y) + STR$(z) + STR$(sp) + " "
  38. LOOP UNTIL i$ = CHR$(27)
  39. SCREEN 0, 0, 0, 0
  40.  
  41. SUB _GL ()
  42.     IF NOT glAllow THEN EXIT SUB
  43.     IF NOT glinit THEN
  44.         glinit = -1
  45.         DIM m AS _MEM
  46.         FOR p = 1 TO 6
  47.             _GLGENTEXTURES 1, _OFFSET(s2&(p))
  48.             m = _MEMIMAGE(s&(p))
  49.             _GLBINDTEXTURE _GL_TEXTURE_2D, s2&(p)
  50.             _GLTEXIMAGE2D _GL_TEXTURE_2D, 0, _GL_RGB, 500, 500, 0, _GL_BGRA_EXT, _GL_UNSIGNED_BYTE, m.OFFSET
  51.             _MEMFREE m
  52.             _GLTEXPARAMETERI _GL_TEXTURE_2D, _GL_TEXTURE_MAG_FILTER, _GL_LINEAR
  53.             _GLTEXPARAMETERI _GL_TEXTURE_2D, _GL_TEXTURE_MIN_FILTER, _GL_NEAREST
  54.             _FREEIMAGE s&(p)
  55.         NEXT p
  56.     END IF
  57.     FOR p = 1 TO 6 '                                                                          6 pieces
  58.         _GLLOADIDENTITY
  59.         _GLENABLE _GL_TEXTURE_2D
  60.         _GLENABLE _GL_DEPTH_TEST
  61.         _GLBINDTEXTURE _GL_TEXTURE_2D, s2&(p)
  62.         _GLUPERSPECTIVE 45, 1280 / 800, 1, 50
  63.         gluLookAt 4, 5, 12, 0, 1, 0, 0, 1, 0
  64.         _GLROTATEF r + p * 20, x, y, z
  65.         _GLTRANSLATEF p * 2 + (p > 3) * 6 - 3, 4 + (p < 4) * 2, p / 2 '                       2 lines of 3 pieces
  66.         _GLBEGIN _GL_QUADS
  67.         IF p = 1 THEN v 2, 0, 1, 0: v 2, .5, 1, .5: v 1, -.25, 1, -.5 '                       w
  68.         IF p = 2 THEN v 2, 0, 1, 0: v 1, .25, 1, .5: v 1, .25, 1, -.5 '                       t
  69.         IF p = 3 THEN v 3, 0, 1, 0: v 1, .5, 1, .5: v 1, -.5, 1, .5 '                         u
  70.         IF p = 4 THEN v 3, 0, 1, 0: v 1, 0, 1, .5: v 1, 0, 1, -.5 '                           x
  71.         IF p = 5 THEN v 2, 0, 1, 0: v 1, .25, 1, .5: v 1, .25, 1, -.5: v 1, .75, 1, .5 '      t+
  72.         IF p = 6 THEN v 2, 0, 1, 0: v 1, .25, 1, .5: v 1, -.25, 1, -.5 '                      z
  73.         _GLEND
  74.     NEXT p
  75.  
  76. SUB v (xm, xo, ym, yo) '                                                                      multipliers and offsets
  77.     FOR i = 0 TO n '
  78.         _GLTEXCOORD2F d(i, 0) * d, d(i, 1) * d '                                              from texture
  79.         _GLVERTEX3F d(i, 2) * xm + xo, d(i, 3) * ym + yo, d(i, 4) '                           make a box or rectangle
  80.     NEXT i
  81.  
« Last Edit: July 07, 2020, 04:20:30 PM by Richard Frost »
It works better if you plug it in.

Offline bplus

  • Forum Resident
  • Posts: 4589
  • B+ nots
Re: OpenGL rotations
« Reply #1 on: July 07, 2020, 04:33:26 PM »
Well I don't know dukey about GL but can you put them in separate destinations spin them in their own self centered world and project the images where you want them back on screen?

Offline Richard Frost

  • Forum Regular
  • Posts: 145
  • Needle nardle noo. - Peter Sellers
Re: OpenGL rotations
« Reply #2 on: July 07, 2020, 04:39:15 PM »
Yes, I'd thought of that, but surely there's a better (elegant) way.

I'm suprised you don't know, since you're into cool graphics.  You do your own rotations/projections/etc. without GL?
« Last Edit: July 07, 2020, 04:42:39 PM by Richard Frost »
It works better if you plug it in.

Offline bplus

  • Forum Resident
  • Posts: 4589
  • B+ nots
Re: OpenGL rotations
« Reply #3 on: July 07, 2020, 04:46:53 PM »
Rotations are easy in 2D with Rotozoom.

B+ gets D- in 3D. ;(

If you thought of that and I thought of that then that's it! :)
« Last Edit: July 07, 2020, 04:48:52 PM by bplus »

Offline Richard Frost

  • Forum Regular
  • Posts: 145
  • Needle nardle noo. - Peter Sellers
Re: OpenGL rotations
« Reply #4 on: July 07, 2020, 07:40:20 PM »
Found it, I think - moving the gluLookat above the rotate fixes it.  Not that I understand WHY it works.





 
It works better if you plug it in.

Offline bplus

  • Forum Resident
  • Posts: 4589
  • B+ nots
Re: OpenGL rotations
« Reply #5 on: July 07, 2020, 08:04:10 PM »
Is that "gluLookat above the rotate" above each object?

Offline Richard Frost

  • Forum Regular
  • Posts: 145
  • Needle nardle noo. - Peter Sellers
Re: OpenGL rotations
« Reply #6 on: July 07, 2020, 10:33:37 PM »
Ack.  I meant to say moving the _glTranslatef.   Switching between the old and new can be see by pressing the q key here.
There is only one instance of each of the translate, lookat, and rotate commands, and they're all executed before each redraw of the objects at new angles.

My replacing the DATA statements that defined the vertexes with one long string saved a few lines.    Ashish should take note
of how I eliminated multiple lines of _glTexCoord2f & _glVertex3f to draw a box.  :)

Code: QB64: [Select]
  1. ' keys xyz to toggle rotations, arrow up/down speed
  2.  
  3. _TITLE "Bonkers"
  4.     SUB gluLookAt (BYVAL eyeX#, BYVAL eyeY#, BYVAL eyeZ#, BYVAL centerX#, BYVAL centerY#, BYVAL centerZ#, BYVAL upX#, BYVAL upY#, BYVAL upZ#)
  5. DIM SHARED glAllow AS _BYTE, glinit, s&(6), s2&(6), d(23, 4), d, n, x, y, z, r, q
  6. SCREEN _NEWIMAGE(500, 500, 32)
  7. d = 4: n = 23: sp = 60: x = 1: y = 1: z = 1
  8. FOR i = 0 TO n
  9.     FOR j = 0 TO 4
  10.         t$ = MID$("zpnppppppppzpnpzznnpzpnpnppppnpzpnnzznnnzpnpnppppnpzpppzznppzpnnnpppnnpzpnpzznnpzpnpnppnpppznnpzznnnzpppnppppppzpnpzzpnn", i * 5 + j + 1, 1)
  11.         d(i, j) = (INSTR("nzp", t$) - 2) / 4 '          n=-1,z=0,p=1
  12.     NEXT j
  13. FOR p = 1 TO 6 '                                        generate 6 textures, one for each piece
  14.     FOR i = 0 TO 250
  15.         j = 255 - i
  16.         IF p = 1 THEN c& = _RGB(i / 2, j, j) '          w
  17.         IF p = 2 THEN c& = _RGB(j / 2, j, i) '          t
  18.         IF p = 3 THEN c& = _RGB(j, i / 2, i) '          u
  19.         IF p = 4 THEN c& = _RGB(i / 4, i, j / 2) '      x
  20.         IF p = 5 THEN c& = _RGB(j / 2, i / 2, i) '      t+
  21.         IF p = 6 THEN c& = _RGB(i, j, i) '              z
  22.         LINE (i, i)-(_WIDTH - i, _HEIGHT - i), c&, B
  23.     NEXT i
  24.     s&(p) = _COPYIMAGE(0)
  25. SCREEN _NEWIMAGE(800, 800, 32)
  26. glAllow = -1
  27.     i$ = INKEY$
  28.     IF i$ = "q" THEN q = q XOR 1
  29.     IF i$ = "x" THEN x = x XOR 1 '                      x axis on/off
  30.     IF i$ = "y" THEN y = y XOR 1 '                      y
  31.     IF i$ = "z" THEN z = z XOR 1 '                      z
  32.     IF LEN(i$) = 2 THEN '                               maybe arrow key
  33.         kk = ASC(RIGHT$(i$, 1))
  34.         sp = sp + (kk = 80) * 10 - (kk = 72) * 10 '     speed
  35.         IF sp < 10 THEN sp = 10 '                       lower speed limit
  36.     END IF
  37.     r = (r + 1) MOD 360 '                               master rotation
  38.     _PRINTSTRING (0, 0), STR$(x) + STR$(y) + STR$(z) + STR$(sp) + " "
  39. LOOP UNTIL i$ = CHR$(27)
  40. SCREEN 0, 0, 0, 0
  41.  
  42. SUB _GL ()
  43.     IF NOT glAllow THEN EXIT SUB
  44.     IF NOT glinit THEN
  45.         glinit = -1
  46.         DIM m AS _MEM
  47.         FOR p = 1 TO 6
  48.             _GLGENTEXTURES 1, _OFFSET(s2&(p))
  49.             m = _MEMIMAGE(s&(p))
  50.             _GLBINDTEXTURE _GL_TEXTURE_2D, s2&(p)
  51.             _GLTEXIMAGE2D _GL_TEXTURE_2D, 0, _GL_RGB, 500, 500, 0, _GL_BGRA_EXT, _GL_UNSIGNED_BYTE, m.OFFSET
  52.             _MEMFREE m
  53.             _GLTEXPARAMETERI _GL_TEXTURE_2D, _GL_TEXTURE_MAG_FILTER, _GL_LINEAR
  54.             _GLTEXPARAMETERI _GL_TEXTURE_2D, _GL_TEXTURE_MIN_FILTER, _GL_NEAREST
  55.             _FREEIMAGE s&(p)
  56.         NEXT p
  57.     END IF
  58.     FOR p = 1 TO 6 '                                                                          6 pieces
  59.         _GLLOADIDENTITY
  60.         _GLENABLE _GL_TEXTURE_2D
  61.         _GLENABLE _GL_DEPTH_TEST
  62.         _GLBINDTEXTURE _GL_TEXTURE_2D, s2&(p)
  63.         _GLUPERSPECTIVE 45, 1280 / 800, 1, 20
  64.         IF q = 0 THEN _GLTRANSLATEF p * 2 + (p > 3) * 6 - 4, 4 + (p < 4) * 2 - 2, p / 2 '                   2 lines of 3 pieces
  65.         gluLookAt 2, 5, 10, 0, 1, 0, 0, 1, 0
  66.         _GLROTATEF r + p * 30, x, y, z
  67.         IF q = 1 THEN _GLTRANSLATEF p * 2 + (p > 3) * 6 - 4, 4 + (p < 4) * 2 - 2, p / 2 '                   2 lines of 3 pieces
  68.         _GLBEGIN _GL_QUADS
  69.         IF p = 1 THEN v 2, 0, 1, 0: v 2, .5, 1, .5: v 1, -.25, 1, -.5 '                       w
  70.         IF p = 2 THEN v 2, 0, 1, 0: v 1, .25, 1, .5: v 1, .25, 1, -.5 '                       t
  71.         IF p = 3 THEN v 3, 0, 1, 0: v 1, .5, 1, .5: v 1, -.5, 1, .5 '                         u
  72.         IF p = 4 THEN v 3, 0, 1, 0: v 1, 0, 1, .5: v 1, 0, 1, -.5 '                           x
  73.         IF p = 5 THEN v 2, 0, 1, 0: v 1, .25, 1, .5: v 1, .25, 1, -.5: v 1, .75, 1, .5 '      t+
  74.         IF p = 6 THEN v 2, 0, 1, 0: v 1, .25, 1, .5: v 1, -.25, 1, -.5 '                      z
  75.         _GLEND
  76.     NEXT p
  77.  
  78. SUB v (xm, xo, ym, yo) '                                                                      multipliers and offsets
  79.     FOR i = 0 TO n '
  80.         _GLTEXCOORD2F d(i, 0) * d, d(i, 1) * d '                                              from texture
  81.         _GLVERTEX3F d(i, 2) * xm + xo, d(i, 3) * ym + yo, d(i, 4) '                           make a box or rectangle
  82.     NEXT i
  83.  
  84.  
« Last Edit: July 07, 2020, 10:44:04 PM by Richard Frost »
It works better if you plug it in.

Offline Ashish

  • Forum Resident
  • Posts: 623
  • The joy of coding is endless.
Re: OpenGL rotations
« Reply #7 on: July 08, 2020, 12:23:47 AM »
Hi @Richard Frost
You need to translate first and then do rotation in order to make object rotate on its own axis. Just like you did in your last reply.

Nice demo BTW! :D
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 Richard Frost

  • Forum Regular
  • Posts: 145
  • Needle nardle noo. - Peter Sellers
Re: OpenGL rotations
« Reply #8 on: July 08, 2020, 01:26:42 AM »
Also, why the LookAt goes before the rotate makes no sense to me, but that's the way it works.
It works better if you plug it in.

Offline Ashish

  • Forum Resident
  • Posts: 623
  • The joy of coding is endless.
Re: OpenGL rotations
« Reply #9 on: July 08, 2020, 01:41:10 AM »
Hi Richard, check out this code.
Code: QB64: [Select]
  1. ' keys xyz to toggle rotations, arrow up/down speed
  2.  
  3. _TITLE "Bonkers"
  4.     SUB gluLookAt (BYVAL eyeX#, BYVAL eyeY#, BYVAL eyeZ#, BYVAL centerX#, BYVAL centerY#, BYVAL centerZ#, BYVAL upX#, BYVAL upY#, BYVAL upZ#)
  5. DIM SHARED glAllow AS _BYTE, glinit, s&(6), s2&(6), d(23, 4), d, n, x, y, z, r, q
  6. SCREEN _NEWIMAGE(500, 500, 32)
  7. d = 4: n = 23: sp = 60: x = 1: y = 1: z = 1
  8. FOR i = 0 TO n
  9.     FOR j = 0 TO 4
  10.         t$ = MID$("zpnppppppppzpnpzznnpzpnpnppppnpzpnnzznnnzpnpnppppnpzpppzznppzpnnnpppnnpzpnpzznnpzpnpnppnpppznnpzznnnzpppnppppppzpnpzzpnn", i * 5 + j + 1, 1)
  11.         d(i, j) = (INSTR("nzp", t$) - 2) / 4 '          n=-1,z=0,p=1
  12.     NEXT j
  13. FOR p = 1 TO 6 '                                        generate 6 textures, one for each piece
  14.     FOR i = 0 TO 250
  15.         j = 255 - i
  16.         IF p = 1 THEN c& = _RGB(i / 2, j, j) '          w
  17.         IF p = 2 THEN c& = _RGB(j / 2, j, i) '          t
  18.         IF p = 3 THEN c& = _RGB(j, i / 2, i) '          u
  19.         IF p = 4 THEN c& = _RGB(i / 4, i, j / 2) '      x
  20.         IF p = 5 THEN c& = _RGB(j / 2, i / 2, i) '      t+
  21.         IF p = 6 THEN c& = _RGB(i, j, i) '              z
  22.         LINE (i, i)-(_WIDTH - i, _HEIGHT - i), c&, B
  23.     NEXT i
  24.     s&(p) = _COPYIMAGE(0)
  25. SCREEN _NEWIMAGE(800, 800, 32)
  26. glAllow = -1
  27.     i$ = INKEY$
  28.     IF i$ = "q" THEN q = q XOR 1
  29.     IF i$ = "x" THEN x = x XOR 1 '                      x axis on/off
  30.     IF i$ = "y" THEN y = y XOR 1 '                      y
  31.     IF i$ = "z" THEN z = z XOR 1 '                      z
  32.     IF LEN(i$) = 2 THEN '                               maybe arrow key
  33.         kk = ASC(RIGHT$(i$, 1))
  34.         sp = sp + (kk = 80) * 10 - (kk = 72) * 10 '     speed
  35.         IF sp < 10 THEN sp = 10 '                       lower speed limit
  36.     END IF
  37.     r = (r + 1) MOD 360 '                               master rotation
  38.     _PRINTSTRING (0, 0), STR$(x) + STR$(y) + STR$(z) + STR$(sp) + " "
  39. LOOP UNTIL i$ = CHR$(27)
  40. SCREEN 0, 0, 0, 0
  41.  
  42. SUB _GL ()
  43.     IF NOT glAllow THEN EXIT SUB
  44.     IF NOT glinit THEN
  45.         glinit = -1
  46.         DIM m AS _MEM
  47.         FOR p = 1 TO 6
  48.             _GLGENTEXTURES 1, _OFFSET(s2&(p))
  49.             m = _MEMIMAGE(s&(p))
  50.             _GLBINDTEXTURE _GL_TEXTURE_2D, s2&(p)
  51.             _GLTEXIMAGE2D _GL_TEXTURE_2D, 0, _GL_RGB, 500, 500, 0, _GL_BGRA_EXT, _GL_UNSIGNED_BYTE, m.OFFSET
  52.             _MEMFREE m
  53.             _GLTEXPARAMETERI _GL_TEXTURE_2D, _GL_TEXTURE_MAG_FILTER, _GL_LINEAR
  54.             _GLTEXPARAMETERI _GL_TEXTURE_2D, _GL_TEXTURE_MIN_FILTER, _GL_NEAREST
  55.             _FREEIMAGE s&(p)
  56.         NEXT p
  57.     END IF
  58.     _GLMATRIXMODE _GL_PROJECTION
  59.     _GLUPERSPECTIVE 45, 1280 / 800, 1, 20 'needs to be called in projection matrix mode
  60.  
  61.     _GLMATRIXMODE _GL_MODELVIEW
  62.     gluLookAt 2, 5, 10, 0, 1, 0, 0, 1, 0 'need to be only called once.
  63.  
  64.     FOR p = 1 TO 6 '                                                                          6 pieces
  65.         _GLPUSHMATRIX 'save previous state
  66.         _GLENABLE _GL_TEXTURE_2D
  67.         _GLENABLE _GL_DEPTH_TEST
  68.         _GLBINDTEXTURE _GL_TEXTURE_2D, s2&(p)
  69.  
  70.         IF q = 0 THEN _GLTRANSLATEF p * 2 + (p > 3) * 6 - 4, 4 + (p < 4) * 2 - 2, p / 2 '                   2 lines of 3 pieces
  71.         _GLROTATEF r + p * 30, x, y, z
  72.         IF q = 1 THEN _GLTRANSLATEF p * 2 + (p > 3) * 6 - 4, 4 + (p < 4) * 2 - 2, p / 2 '                   2 lines of 3 pieces
  73.         _GLBEGIN _GL_QUADS
  74.         IF p = 1 THEN v 2, 0, 1, 0: v 2, .5, 1, .5: v 1, -.25, 1, -.5 '                       w
  75.         IF p = 2 THEN v 2, 0, 1, 0: v 1, .25, 1, .5: v 1, .25, 1, -.5 '                       t
  76.         IF p = 3 THEN v 3, 0, 1, 0: v 1, .5, 1, .5: v 1, -.5, 1, .5 '                         u
  77.         IF p = 4 THEN v 3, 0, 1, 0: v 1, 0, 1, .5: v 1, 0, 1, -.5 '                           x
  78.         IF p = 5 THEN v 2, 0, 1, 0: v 1, .25, 1, .5: v 1, .25, 1, -.5: v 1, .75, 1, .5 '      t+
  79.         IF p = 6 THEN v 2, 0, 1, 0: v 1, .25, 1, .5: v 1, -.25, 1, -.5 '                      z
  80.         _GLEND
  81.         _GLPOPMATRIX 'restore previous state
  82.     NEXT p
  83.  
  84. SUB v (xm, xo, ym, yo) '                                                                      multipliers and offsets
  85.     FOR i = 0 TO n '
  86.         _GLTEXCOORD2F d(i, 0) * d, d(i, 1) * d '                                              from texture
  87.         _GLVERTEX3F d(i, 2) * xm + xo, d(i, 3) * ym + yo, d(i, 4) '                           make a box or rectangle
  88.     NEXT i
  89.  
  90.  
  91.  
  92.  
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 Unseen Machine

  • Forum Regular
  • Posts: 117
  • QB64 Elite
Re: OpenGL rotations
« Reply #10 on: July 08, 2020, 03:19:11 AM »
Hi folks,

So i figured i'd explain. It's all about pushing and poping!

If you want to perform a translation or rotation on an object and not effect the whole scene you need to make sure youre on in the right place on the stack. Once you've performed an action such as _gltranslate or _glrotatef the changes you made in position or rotation will continue, that is unless you push and pop the matrices.

As for the way you've implemented it.

gluLookAt : This is old and outdated and (in my opinion) should'nt be used. I find it best to make your own camera controls!

 _GLBEGIN : Again, this is a legacy command and really shouldnt be used either. Use the vertex and texture buffer instead.

https://nehe.gamedev.net/tutorial/lessons_01__05/22004/
It's an old reference (uses _GLBEGIN!) but still one of the best.

Just so you have an idea of how third person camera works
Code: QB64: [Select]
  1.  IF KB(0).Q THEN Cam.Rot.X = Cam.Rot.X + 1
  2.  IF Cam.Rot.X > 360 THEN Cam.Rot.X = Cam.Rot.X - 360
  3.  
  4.  IF KB(0).Z THEN Cam.Rot.X = Cam.Rot.X + 1
  5.  IF Cam.Rot.X < -360 THEN Cam.Rot.X = Cam.Rot.X + 360
  6.  
  7.  IF KB(0).W THEN
  8.   yrotrad = (Cam.Rot.Y / 180 * 3.141592654F)
  9.   xrotrad = (Cam.Rot.X / 180 * 3.141592654F)
  10.   Cam.POS.X = Cam.POS.X + SIN(yrotrad)
  11.   Cam.POS.Z = Cam.POS.Z - COS(yrotrad)
  12.   Cam.POS.Y = Cam.POS.Y - SIN(xrotrad)
  13.  
  14.  IF KB(0).S THEN
  15.   yrotrad = (Cam.Rot.Y / 180 * 3.141592654F)
  16.   xrotrad = (Cam.Rot.X / 180 * 3.141592654F)
  17.   Cam.POS.X = Cam.POS.X - SIN(yrotrad)
  18.   Cam.POS.Z = Cam.POS.Z + COS(yrotrad)
  19.   Cam.POS.Y = Cam.POS.Y + SIN(xrotrad)
  20.  
  21.  IF KB(0).D THEN
  22.   yrotrad = (Cam.Rot.Y / 180 * 3.141592654F)
  23.   Cam.POS.X = Cam.POS.X + COS(yrotrad) * 0.2
  24.   Cam.POS.Z = Cam.POS.Z + SIN(yrotrad) * 0.2
  25.  
  26.  IF KB(0).A THEN
  27.   yrotrad = (Cam.Rot.Y / 180 * 3.141592654F)
  28.   Cam.POS.X = Cam.POS.X - COS(yrotrad) * 0.2
  29.   Cam.POS.Z = Cam.POS.Z - SIN(yrotrad) * 0.2
  30.  
  31.  diffx = Mouse(0).X - Mouse(1).X '// check the difference between the current x and the last x position
  32.  diffy = Mouse(0).Y - Mouse(1).Y '// check the difference between the current y and the last y position
  33.  Cam.Rot.X = Cam.Rot.X + diffy '// set the Cam.Rot.x to Cam.Rot.x with the addition of the difference in the y position
  34.  Cam.Rot.Y = Cam.Rot.Y + diffx '// set the Cam.Rot.x to Cam.Rot.y with the addition of the difference in the x position
  35.  
  36.  
  37.  
  38.  _GLTRANSLATEF 0.0, 0.0, Radius
  39.  _GLROTATEF Cam.Rot.X, 1.0, 0.0, 0
  40.  '// Thinkthe thing the camera is following gets rendered here....
  41.  
  42.  _GLROTATEF Cam.Rot.Y, 0.0, 1.0, 0.0 '  //rotate our camera on the y-axis (up and down)
  43.  _GLTRANSLATED Cam.POS.X, 0.0F, Cam.POS.Z ' //translate the screen to the position of our camera

Unseen

Offline Richard Frost

  • Forum Regular
  • Posts: 145
  • Needle nardle noo. - Peter Sellers
Re: OpenGL rotations
« Reply #11 on: July 12, 2020, 12:23:43 AM »
Old and outdated is ok with me, if it works.

Unseen, why is your method better?   You don't say. 
It works better if you plug it in.

Offline Unseen Machine

  • Forum Regular
  • Posts: 117
  • QB64 Elite
Re: OpenGL rotations
« Reply #12 on: July 12, 2020, 04:32:40 AM »
Quote
Unseen, why is your method better?   You don't say. 

Cause its faster to render this way, hell using the index buffer over the vertex buffer is even faster but that method is to complicated for my liking. Secondly, my way should run on all modern o/s's (i.e android) which doesnt support legacy GL commands such as _GLBegin/_GLEnd

Unseen

Offline Richard Frost

  • Forum Regular
  • Posts: 145
  • Needle nardle noo. - Peter Sellers
Re: OpenGL rotations
« Reply #13 on: July 12, 2020, 07:46:32 AM »
Your reply refers to gluLookAt as well as Begin/End?
It works better if you plug it in.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1412
Re: OpenGL rotations
« Reply #14 on: July 12, 2020, 06:30:07 PM »
Hi Richard
here you can see an example from Ashish https://www.qb64.org/forum/index.php?topic=2652.msg118685#msg118685 in which different objects are rotating on the own axis.
Programming isn't difficult, only it's  consuming time and coffee