Author Topic: OpenGL rotations  (Read 4260 times)

0 Members and 1 Guest are viewing this topic.

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
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

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
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

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
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

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
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

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
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

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
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

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
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: 630
  • Never Give Up!
    • View Profile
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

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
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: 630
  • Never Give Up!
    • View Profile
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: 158
  • Make the game not the engine!
    • View Profile
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

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
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: 158
  • Make the game not the engine!
    • View Profile
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

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
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: 1792
    • View Profile
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