Author Topic: Interesting Result  (Read 4717 times)

0 Members and 1 Guest are viewing this topic.

Offline Craz1000

  • Forum Regular
  • Posts: 111
  • I'm OK
    • View Profile
    • Craz1000.net
Interesting Result
« on: March 02, 2019, 10:40:51 pm »
So I translated some OpenGL code from a program i started in BASIC4GL however the camera angle and movement isn't quite working right, anyone know what I am doing wrong?

Also I noticed to move the camera up you have to go negative, any way to flip the y axis?

Code: QB64: [Select]
  1. TYPE CAMERA
  2.     RotY AS _FLOAT
  3.     RotX AS _FLOAT
  4.     PosX AS _FLOAT
  5.     PosY AS _FLOAT
  6.     PosZ AS _FLOAT
  7.  
  8. TYPE Terrain
  9.     BlockSize AS INTEGER
  10.     Blocks AS INTEGER
  11.  
  12. DIM SHARED Cam AS CAMERA
  13. DIM SHARED Land AS Terrain
  14. DIM SHARED AllowSubGL
  15. Land.Blocks = 10
  16. Land.BlockSize = 10
  17. DIM SHARED Heightmap(Land.Blocks, Land.Blocks)
  18.  
  19.  
  20. Cam.PosX = (Land.Blocks * Land.BlockSize) / 2
  21. Cam.PosZ = (Land.Blocks * Land.BlockSize) / 2
  22. Cam.PosY = -2
  23.  
  24.  
  25.  
  26. SCREEN _NEWIMAGE(1024, 768, 32)
  27.  
  28. AllowSubGL = 1
  29.  
  30.     _LIMIT 100
  31.     LOCATE 1, 1
  32.     PRINT "Camera Position = CamPosX:"; Cam.PosX; "Cam.PosY"; Cam.PosY; "Cam.PosZ:"; Cam.PosZ
  33.     PRINT "Camera Aim = Cam.RotX:"; Cam.RotX; "Cam.RotY"; Cam.RotY
  34.     k$ = UCASE$(INKEY$)
  35.  
  36.     IF k$ = "W" THEN
  37.         Cam.PosX = Cam.PosX - SIN(Cam.RotY) * .3
  38.         Cam.PosZ = Cam.PosZ - COS(Cam.RotY) * .3
  39.         PRINT "W"
  40.     END IF
  41.     IF k$ = "S" THEN
  42.         Cam.PosX = Cam.PosX + SIN(Cam.RotY) * .3
  43.         Cam.PosZ = Cam.PosZ + COS(Cam.RotY) * .3
  44.         PRINT "S"
  45.     END IF
  46.  
  47.  
  48.         Cam.RotY = _MOUSEX
  49.         'XRotation = _MOUSEY
  50.     LOOP
  51.  
  52. LOOP UNTIL k$ = CHR$(27)
  53.  
  54.  
  55.     IF AllowSubGL = 0 THEN EXIT SUB
  56.  
  57.     T# = TIMER(0.001)
  58.     IF ETT# = 0 THEN ETT# = T#
  59.     ET# = T# - ETT#
  60.     ETT# = T#
  61.  
  62.     _glMatrixMode _GL_PROJECTION
  63.     _gluPerspective 45, _WIDTH(0) / _HEIGHT(0), 1, 100
  64.     _glEnable _GL_TEXTURE_2D
  65.     _glEnable _GL_BLEND
  66.     _glBlendFunc _GL_SRC_ALPHA, _GL_ONE_MINUS_SRC_ALPHA
  67.     _glEnable _GL_DEPTH_TEST
  68.     _glDepthMask _GL_TRUE
  69.     _glAlphaFunc _GL_GREATER, 0.5
  70.     _glEnable _GL_ALPHA_TEST
  71.     '_glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MAG_FILTER, _GL_LINEAR
  72.     '_glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MIN_FILTER, _GL_LINEAR
  73.     _glMatrixMode _GL_MODELVIEW
  74.     _glTranslatef Cam.PosX, Cam.PosY, Cam.PosZ
  75.     _glRotatef Cam.RotY, 0, 1, 0
  76.     _glRotatef Cam.RotX, 1, 0, 0
  77.  
  78.  
  79.     FOR Z = 0 TO Land.Blocks - 1
  80.         FOR X = 0 TO Land.Blocks - 1
  81.             _glBegin _GL_QUADS
  82.             _glColor3f 1, 0, 0: _glVertex3f Land.BlockSize * X, Heightmap(X + 1, Z + 1), Land.BlockSize * Z
  83.             _glColor3f 0, 1, 0: _glVertex3f Land.BlockSize * X, Heightmap(X + 1, Z + 1), Land.BlockSize * (Z + 1)
  84.             _glColor3f 0, 0, 1: _glVertex3f Land.BlockSize * (X + 1), Heightmap(X + 1, Z + 1), Land.BlockSize * (Z + 1)
  85.             _glColor3f 1, 1, 0: _glVertex3f Land.BlockSize * (X + 1), Heightmap(X + 1, Z + 1), Land.BlockSize * Z
  86.             _glEnd
  87.         NEXT X
  88.     NEXT Z
  89.  
« Last Edit: March 02, 2019, 10:47:12 pm by Craz1000 »

Offline Craz1000

  • Forum Regular
  • Posts: 111
  • I'm OK
    • View Profile
    • Craz1000.net
Re: Interesting Result
« Reply #1 on: March 03, 2019, 10:26:03 am »
I got the position and the rotation working, still haven't figured out the forward movement

Code: QB64: [Select]
  1. TYPE CAMERA
  2.     RotY AS DOUBLE
  3.     RotX AS DOUBLE
  4.     PosX AS DOUBLE
  5.     PosY AS DOUBLE
  6.     PosZ AS DOUBLE
  7.  
  8. TYPE Terrain
  9.     BlockSize AS INTEGER
  10.     Blocks AS INTEGER
  11.     MaxHeight AS INTEGER
  12.     MaxHeightChange AS INTEGER
  13.  
  14. DIM SHARED Cam AS CAMERA
  15. DIM SHARED Land AS Terrain
  16. Land.Blocks = 10
  17. Land.BlockSize = 10
  18.  
  19. DIM SHARED Heightmap(Land.Blocks, Land.Blocks)
  20. DIM SHARED AllowSubGL
  21.  
  22.  
  23. Cam.PosX = ((Land.Blocks * Land.BlockSize) / 2) - (Land.Blocks * Land.BlockSize)
  24. Cam.PosZ = ((Land.Blocks * Land.BlockSize) / 2) - (Land.Blocks * Land.BlockSize)
  25. Cam.PosY = -2
  26. Cam.RotY = 1
  27.  
  28. SCREEN _NEWIMAGE(1024, 768, 32)
  29. AllowSubGL = 1
  30.  
  31.     _LIMIT 100
  32.     LOCATE 1, 1
  33.     PRINT "Camera Position = CamPosX:"; Cam.PosX; "Cam.PosY"; Cam.PosY; "Cam.PosZ:"; Cam.PosZ
  34.     PRINT "Camera Aim = Cam.RotX:"; Cam.RotX; "Cam.RotY"; Cam.RotY; " Rad:"; SIN(Cam.RotY); " Deg:"; SIN(Cam.RotY) * 180 / (4 * ATN(1)); "                        "
  35.     k$ = UCASE$(INKEY$)
  36.  
  37.     IF k$ = "W" THEN
  38.         Cam.PosX = Cam.PosX + SIN(Cam.RotY)
  39.         Cam.PosZ = Cam.PosZ + COS(Cam.RotY)
  40.         PRINT "W"
  41.     END IF
  42.     IF k$ = "S" THEN
  43.         Cam.PosX = Cam.PosX - COS(Cam.RotY) * .3
  44.         Cam.PosZ = Cam.PosZ - SIN(Cam.RotY) * .3
  45.         PRINT "S"
  46.     END IF
  47.  
  48.         Cam.RotY = Cam.RotY + _MOUSEMOVEMENTX
  49.         'Cam.RotX = _MOUSEY / 10
  50.         IF Cam.RotY > 359 THEN Cam.RotY = 0
  51.         IF Cam.RotY < 0 THEN Cam.RotY = 359
  52.  
  53.     LOOP
  54.  
  55. LOOP UNTIL k$ = CHR$(27)
  56.  
  57.  
  58.     IF AllowSubGL = 0 THEN EXIT SUB
  59.  
  60.     T# = TIMER(0.001)
  61.     IF ETT# = 0 THEN ETT# = T#
  62.     ET# = T# - ETT#
  63.     ETT# = T#
  64.  
  65.     '_glMatrixMode _GL_PROJECTION
  66.     '_glLoadIdentity
  67.     _glMatrixMode _GL_MODELVIEW
  68.     _gluPerspective 45, _WIDTH(0) / _HEIGHT(0), 1, 100
  69.     _glEnable _GL_TEXTURE_2D
  70.     _glEnable _GL_BLEND
  71.     _glBlendFunc _GL_SRC_ALPHA, _GL_ONE_MINUS_SRC_ALPHA
  72.     _glEnable _GL_DEPTH_TEST
  73.     _glDepthMask _GL_TRUE
  74.     _glAlphaFunc _GL_GREATER, 0.5
  75.     _glEnable _GL_ALPHA_TEST
  76.     '_glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MAG_FILTER, _GL_LINEAR
  77.     '_glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MIN_FILTER, _GL_LINEAR
  78.     'load identity used to be here
  79.     _glRotatef Cam.RotY, 0, 1, 0
  80.     _glRotatef Cam.RotX, 1, 0, 0
  81.     _glTranslatef Cam.PosX, Cam.PosY, Cam.PosZ
  82.  
  83.  
  84.     FOR Z = 0 TO Land.Blocks - 1
  85.         FOR X = 0 TO Land.Blocks - 1
  86.             _glBegin _GL_QUADS
  87.             _glColor3f 1, 0, 0: _glVertex3f Land.BlockSize * X, Heightmap(X + 1, Z + 1), Land.BlockSize * Z
  88.             _glColor3f 0, 1, 0: _glVertex3f Land.BlockSize * X, Heightmap(X + 1, Z + 1), Land.BlockSize * (Z + 1)
  89.             _glColor3f 0, 0, 1: _glVertex3f Land.BlockSize * (X + 1), Heightmap(X + 1, Z + 1), Land.BlockSize * (Z + 1)
  90.             _glColor3f 1, 1, 0: _glVertex3f Land.BlockSize * (X + 1), Heightmap(X + 1, Z + 1), Land.BlockSize * Z
  91.             _glEnd
  92.         NEXT X
  93.     NEXT Z
  94.  
  95.  
  96.  

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Interesting Result
« Reply #2 on: March 03, 2019, 11:58:46 am »
Hi Craz1000! I would recommend you to use inbuilt gluLookAt() for camera related purpose. Your calculation about camera is correct
but it is resulting different due to rotation.
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: Interesting Result
« Reply #3 on: March 03, 2019, 12:00:19 pm »
Here's a working example in which I modified your code (I've also cleaned some unwanted code) -
Use W,A,S,D for movement.
Code: QB64: [Select]
  1. TYPE CAMERA
  2.     RotY AS _FLOAT
  3.     RotX AS _FLOAT
  4.     PosX AS _FLOAT
  5.     PosY AS _FLOAT
  6.     PosZ AS _FLOAT
  7.  
  8. TYPE Terrain
  9.     BlockSize AS INTEGER
  10.     Blocks AS INTEGER
  11.  
  12. DIM SHARED Cam AS CAMERA
  13. DIM SHARED Land AS Terrain
  14. DIM SHARED AllowSubGL
  15. Land.Blocks = 10
  16. Land.BlockSize = 10
  17. DIM SHARED Heightmap(Land.Blocks, Land.Blocks)
  18.  
  19.  
  20. DECLARE LIBRARY 'camera related subroutine
  21.     SUB gluLookAt (BYVAL eyeX#, BYVAL eyeY#, BYVAL eyeZ#, BYVAL centerX#, BYVAL centerY#, BYVAL centerZ#, BYVAL upX#, BYVAL upY#, BYVAL upZ#)
  22.  
  23. Cam.PosX = (Land.Blocks * Land.BlockSize) / 2
  24. Cam.PosZ = (Land.Blocks * Land.BlockSize)
  25. Cam.PosY = 3
  26.  
  27.  
  28.  
  29. SCREEN _NEWIMAGE(1024, 768, 32)
  30.  
  31. AllowSubGL = 1
  32.  
  33.     _LIMIT 60
  34.     LOCATE 1, 1
  35.     PRINT "Camera Position = CamPosX:"; Cam.PosX; "Cam.PosY:"; Cam.PosY; "Cam.PosZ:"; Cam.PosZ
  36.     PRINT "Camera Aim = Cam.RotX:"; Cam.RotX; "Cam.RotY"; Cam.RotY
  37.     k$ = UCASE$(INKEY$)
  38.  
  39.     IF k$ = "W" THEN
  40.         Cam.PosZ = Cam.PosZ - 0.5
  41.         PRINT "W"
  42.     END IF
  43.     IF k$ = "S" THEN
  44.         Cam.PosZ = Cam.PosZ + 0.5
  45.         PRINT "S"
  46.     END IF
  47.     IF k$ = "A" THEN
  48.         Cam.PosX = Cam.PosX - 0.5
  49.         PRINT "A"
  50.     END IF
  51.     IF k$ = "D" THEN
  52.         Cam.PosX = Cam.PosX + 0.5
  53.         PRINT "D"
  54.     END IF
  55.  
  56.  
  57.         Cam.RotY = 270 + (_MOUSEX / _WIDTH) * 180
  58.         'Cam.RotX = _MOUSEx
  59.     LOOP
  60.  
  61. LOOP UNTIL k$ = CHR$(27)
  62.  
  63.  
  64.     IF AllowSubGL = 0 THEN EXIT SUB
  65.  
  66.  
  67.     _glMatrixMode _GL_PROJECTION
  68.  
  69.     _gluPerspective 45, _WIDTH(0) / _HEIGHT(0), 1, 100
  70.     _glEnable _GL_DEPTH_TEST
  71.     _glDepthMask _GL_TRUE
  72.  
  73.     _glMatrixMode _GL_MODELVIEW
  74.  
  75.     '_glTranslatef Cam.PosX, Cam.PosY, Cam.PosZ
  76.     '_glRotatef Cam.RotY, 0, 1, 0
  77.     '_glRotatef Cam.RotX, 1, 0, 0
  78.     _glRotatef Cam.RotY, 0, 1, 0
  79.  
  80.  
  81.     gluLookAt Cam.PosX, Cam.PosY, Cam.PosZ, Cam.PosX, 0, Cam.PosZ - 30, 0, 1, 0
  82.  
  83.  
  84.     FOR Z = 0 TO Land.Blocks - 1
  85.         FOR X = 0 TO Land.Blocks - 1
  86.             _glBegin _GL_QUADS
  87.             _glColor3f 1, 0, 0: _glVertex3f Land.BlockSize * X, Heightmap(X + 1, Z + 1), Land.BlockSize * Z
  88.             _glColor3f 0, 1, 0: _glVertex3f Land.BlockSize * X, Heightmap(X + 1, Z + 1), Land.BlockSize * (Z + 1)
  89.             _glColor3f 0, 0, 1: _glVertex3f Land.BlockSize * (X + 1), Heightmap(X + 1, Z + 1), Land.BlockSize * (Z + 1)
  90.             _glColor3f 1, 1, 0: _glVertex3f Land.BlockSize * (X + 1), Heightmap(X + 1, Z + 1), Land.BlockSize * Z
  91.             _glEnd
  92.         NEXT X
  93.     NEXT Z
  94.  
  95.  
« Last Edit: March 03, 2019, 12:01:32 pm 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 bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Interesting Result
« Reply #4 on: March 03, 2019, 12:55:23 pm »
That is a very nice demo of GL!

Offline Craz1000

  • Forum Regular
  • Posts: 111
  • I'm OK
    • View Profile
    • Craz1000.net
Re: Interesting Result
« Reply #5 on: March 03, 2019, 01:07:08 pm »
got it working after lots of trial and error, and added height

Code: QB64: [Select]
  1. TYPE CAMERA
  2.     RotY AS INTEGER
  3.     RotX AS SINGLE
  4.     PosX AS SINGLE
  5.     PosY AS SINGLE
  6.     PosZ AS SINGLE
  7.     Xmod AS SINGLE
  8.     Zmod AS SINGLE
  9.  
  10. TYPE Terrain
  11.     BlockSize AS INTEGER
  12.     Blocks AS INTEGER
  13.     MaxHeight AS INTEGER
  14.     MaxHeightChange AS INTEGER
  15.  
  16. DIM SHARED Cam AS CAMERA
  17. DIM SHARED Land AS Terrain
  18. Land.Blocks = 100
  19. Land.BlockSize = 10
  20.  
  21. DIM SHARED Heightmap(Land.Blocks + 1, Land.Blocks + 1) AS SINGLE
  22. DIM SHARED AllowSubGL
  23.  
  24.  
  25. SCREEN _NEWIMAGE(1024, 768, 32)
  26. Cam.PosX = ((Land.Blocks * Land.BlockSize) / 2) - (Land.Blocks * Land.BlockSize)
  27. Cam.PosZ = ((Land.Blocks * Land.BlockSize) / 2) - (Land.Blocks * Land.BlockSize)
  28. Cam.PosY = -2.5
  29. Cam.RotY = 0
  30. Cam.Xmod = 0
  31. Cam.Zmod = -1
  32. Land.MaxHeight = 2
  33.  
  34. CALL RandomizeHeightMap
  35.  
  36. AllowSubGL = 1
  37.  
  38.     _LIMIT 100
  39.     LOCATE 1, 1
  40.     PRINT "Camera Position = CamPosX:"; Cam.PosX; "Cam.PosY"; Cam.PosY; "Cam.PosZ:"; Cam.PosZ
  41.     PRINT "Camera Aim = Cam.RotX:"; Cam.RotX; "Cam.RotY"; Cam.RotY; " XMod:"; Cam.Xmod; " Zmod:"; Cam.Zmod; "                        "
  42.     k$ = UCASE$(INKEY$)
  43.  
  44.     IF k$ = "W" THEN
  45.         Cam.PosX = Cam.PosX - Cam.Xmod
  46.         Cam.PosZ = Cam.PosZ - Cam.Zmod
  47.     END IF
  48.  
  49.     IF k$ = "S" THEN
  50.         Cam.PosX = Cam.PosX + Cam.Xmod
  51.         Cam.PosZ = Cam.PosZ + Cam.Zmod
  52.     END IF
  53.  
  54.         Cam.RotY = Cam.RotY + _MOUSEMOVEMENTX
  55.         IF Cam.RotY > 359 THEN Cam.RotY = 0
  56.         IF Cam.RotY < 0 THEN Cam.RotY = 359
  57.  
  58.         IF Cam.RotY = 0 THEN Cam.Xmod = 0: Cam.Zmod = -1
  59.         IF Cam.RotY > 0 AND Cam.RotY < 91 THEN Cam.Xmod = Cam.RotY / 90
  60.         IF Cam.RotY > 90 AND Cam.RotY < 181 THEN Cam.Xmod = 1 - ((Cam.RotY - 90) / 90)
  61.         IF Cam.RotY > 180 AND Cam.RotY < 271 THEN Cam.Xmod = 2 - (Cam.RotY / 90)
  62.         IF Cam.RotY > 270 AND Cam.RotY < 360 THEN Cam.Xmod = -1 + ((Cam.RotY - 270) / 90)
  63.  
  64.  
  65.         IF Cam.RotY > 90 AND Cam.RotY < 181 THEN Cam.Zmod = (Cam.RotY - 90) / 90
  66.         IF Cam.RotY > 180 AND Cam.RotY < 271 THEN Cam.Zmod = 1 - ((Cam.RotY - 180) / 90)
  67.         IF Cam.RotY > 270 AND Cam.RotY < 360 THEN Cam.Zmod = 2 - ((Cam.RotY - 90) / 90)
  68.         IF Cam.RotY > 0 AND Cam.RotY < 91 THEN Cam.Zmod = -1 + (Cam.RotY / 90)
  69.  
  70.         'Cam.RotX = _MOUSEY / 10
  71.     LOOP
  72.  
  73. LOOP UNTIL k$ = CHR$(27)
  74.  
  75.  
  76. SUB RandomizeHeightMap
  77.     FOR X = 1 TO Land.Blocks + 1
  78.         FOR Z = 1 TO Land.Blocks + 1
  79.             Heightmap(X, Z) = RND * Land.MaxHeight
  80.         NEXT Z
  81.     NEXT X
  82.  
  83.  
  84.     IF AllowSubGL = 0 THEN EXIT SUB
  85.  
  86.     T# = TIMER(0.001)
  87.     IF ETT# = 0 THEN ETT# = T#
  88.     ET# = T# - ETT#
  89.     ETT# = T#
  90.  
  91.     '_glMatrixMode _GL_PROJECTION
  92.     '_glLoadIdentity
  93.     _glMatrixMode _GL_MODELVIEW
  94.     _gluPerspective 45, _WIDTH(0) / _HEIGHT(0), 1, 100
  95.     _glEnable _GL_TEXTURE_2D
  96.     _glEnable _GL_BLEND
  97.     _glBlendFunc _GL_SRC_ALPHA, _GL_ONE_MINUS_SRC_ALPHA
  98.     _glEnable _GL_DEPTH_TEST
  99.     _glDepthMask _GL_TRUE
  100.     _glAlphaFunc _GL_GREATER, 0.5
  101.     _glEnable _GL_ALPHA_TEST
  102.     '_glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MAG_FILTER, _GL_LINEAR
  103.     '_glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MIN_FILTER, _GL_LINEAR
  104.     'load identity used to be here
  105.     _glRotatef Cam.RotY, 0, 1, 0
  106.     _glRotatef Cam.RotX, 1, 0, 0
  107.     _glTranslatef Cam.PosX, Cam.PosY, Cam.PosZ
  108.  
  109.  
  110.     FOR Z = 0 TO Land.Blocks - 1
  111.         FOR X = 0 TO Land.Blocks - 1
  112.             _glBegin _GL_QUADS
  113.             _glColor3f 1, 0, 0: _glVertex3f Land.BlockSize * X, Heightmap(X + 1, Z + 1), Land.BlockSize * Z
  114.             _glColor3f 0, 1, 0: _glVertex3f Land.BlockSize * X, Heightmap(X + 1, Z + 2), Land.BlockSize * (Z + 1)
  115.             _glColor3f 0, 0, 1: _glVertex3f Land.BlockSize * (X + 1), Heightmap(X + 2, Z + 2), Land.BlockSize * (Z + 1)
  116.             _glColor3f 1, 1, 0: _glVertex3f Land.BlockSize * (X + 1), Heightmap(X + 2, Z + 1), Land.BlockSize * Z
  117.             _glEnd
  118.         NEXT X
  119.     NEXT Z
  120.  
  121.  
  122.  
« Last Edit: March 03, 2019, 01:29:28 pm by Craz1000 »

Offline Craz1000

  • Forum Regular
  • Posts: 111
  • I'm OK
    • View Profile
    • Craz1000.net
Re: Interesting Result
« Reply #6 on: March 03, 2019, 05:37:04 pm »
Added better height mapping, water, and Y axis collision detection. Also removed some stuff Ashish removed on his post.

Need to figure out how to tilt camera up and down.

Code: QB64: [Select]
  1. TYPE CAMERA
  2.     RotX AS INTEGER
  3.     RotY AS INTEGER
  4.     RotZ AS INTEGER
  5.     PosX AS SINGLE
  6.     PosY AS SINGLE
  7.     PosZ AS SINGLE
  8.     Xmod AS SINGLE
  9.     Zmod AS SINGLE
  10.     Mode AS INTEGER
  11.  
  12. TYPE Terrain
  13.     BlockSize AS INTEGER
  14.     Blocks AS INTEGER
  15.     MaxHeight AS INTEGER
  16.     MinHeight AS INTEGER
  17.     MaxHeightChange AS INTEGER
  18.     EnableWater AS INTEGER
  19.     WaterHeight AS SINGLE
  20.     WaterSpeed AS SINGLE
  21.     MaxWaterHeight AS SINGLE
  22.     MinWaterHeight AS SINGLE
  23.     InitHeight AS INTEGER
  24.     MainRnd AS INTEGER
  25.  
  26. DIM SHARED Cam AS CAMERA
  27. DIM SHARED Land AS Terrain
  28.  
  29. Land.Blocks = 100
  30. Land.BlockSize = 10
  31.  
  32. DIM SHARED Heightmap(Land.Blocks + 1, Land.Blocks + 1) AS SINGLE
  33. DIM SHARED AllowSubGL
  34.  
  35.  
  36. SCREEN _NEWIMAGE(1024, 768, 32)
  37.  
  38. CALL InitTerrain
  39. CALL RandomizeHeightMap
  40. CALL SetCamera
  41.  
  42. AllowSubGL = 1
  43.  
  44.     junk = GetHeight(Cam.PosX, Cam.PosZ)
  45.     _LIMIT 100
  46.     LOCATE 1, 1
  47.     PRINT "Camera Position = CamPosX:"; Cam.PosX; "Cam.PosY"; Cam.PosY; "Cam.PosZ:"; Cam.PosZ
  48.     PRINT "Camera Aim = Cam.RotX:"; Cam.RotX; "Cam.RotY"; Cam.RotY; " Mode:"; Cam.Mode; "                        "
  49.     k$ = UCASE$(INKEY$)
  50.  
  51.     IF k$ = "1" THEN Cam.Mode = 1
  52.     IF k$ = "2" THEN Cam.Mode = 2
  53.     IF k$ = "3" THEN Cam.Mode = 3
  54.  
  55.     IF k$ = "W" THEN
  56.         Cam.PosX = Cam.PosX - Cam.Xmod
  57.         Cam.PosZ = Cam.PosZ - Cam.Zmod
  58.         CALL CheckCameraBoundary
  59.     END IF
  60.  
  61.     IF k$ = "S" THEN
  62.         Cam.PosX = Cam.PosX + Cam.Xmod
  63.         Cam.PosZ = Cam.PosZ + Cam.Zmod
  64.         CALL CheckCameraBoundary
  65.     END IF
  66.  
  67.         Cam.RotY = Cam.RotY + _MOUSEMOVEMENTX
  68.         IF Cam.RotY > 359 THEN Cam.RotY = 0
  69.         IF Cam.RotY < 0 THEN Cam.RotY = 359
  70.  
  71.         IF Cam.RotY = 0 THEN Cam.Xmod = 0: Cam.Zmod = -1
  72.         IF Cam.RotY > 0 AND Cam.RotY < 91 THEN Cam.Xmod = Cam.RotY / 90
  73.         IF Cam.RotY > 90 AND Cam.RotY < 181 THEN Cam.Xmod = 1 - ((Cam.RotY - 90) / 90)
  74.         IF Cam.RotY > 180 AND Cam.RotY < 271 THEN Cam.Xmod = 2 - (Cam.RotY / 90)
  75.         IF Cam.RotY > 270 AND Cam.RotY < 360 THEN Cam.Xmod = -1 + ((Cam.RotY - 270) / 90)
  76.  
  77.  
  78.         IF Cam.RotY > 90 AND Cam.RotY < 181 THEN Cam.Zmod = (Cam.RotY - 90) / 90
  79.         IF Cam.RotY > 180 AND Cam.RotY < 271 THEN Cam.Zmod = 1 - ((Cam.RotY - 180) / 90)
  80.         IF Cam.RotY > 270 AND Cam.RotY < 360 THEN Cam.Zmod = 2 - ((Cam.RotY - 90) / 90)
  81.         IF Cam.RotY > 0 AND Cam.RotY < 91 THEN Cam.Zmod = -1 + (Cam.RotY / 90)
  82.  
  83.         'Cam.RotX = _MOUSEY / 10
  84.         IF _MOUSEWHEEL = -1 THEN Cam.PosY = Cam.PosY - 1
  85.         IF _MOUSEWHEEL = 1 THEN Cam.PosY = Cam.PosY + 1
  86.         CALL CheckCameraBoundary
  87.     LOOP
  88.  
  89.     CALL AnimateWater
  90.  
  91. LOOP UNTIL k$ = CHR$(27)
  92.  
  93. SUB CheckCameraBoundary
  94.     IF Cam.PosX > -1 THEN Cam.PosX = -1
  95.     IF Cam.PosZ > -1 THEN Cam.PosZ = -1
  96.     IF Cam.PosX < -1 * (Land.Blocks * Land.BlockSize) THEN Cam.PosX = -1 * (Land.Blocks * Land.BlockSize)
  97.     IF Cam.PosZ < -1 * (Land.Blocks * Land.BlockSize) THEN Cam.PosZ = -1 * (Land.Blocks * Land.BlockSize)
  98.  
  99.     IF Cam.Mode = 1 THEN
  100.         IF (-1 * GetHeight!(Cam.PosX, Cam.PosZ)) < Land.WaterHeight THEN
  101.             Cam.PosY = -1 * (GetHeight!(Cam.PosX, Cam.PosZ)) - 2.5
  102.         ELSE
  103.             Cam.PosY = (-1 * Land.WaterHeight) - 2.5
  104.         END IF
  105.     END IF
  106.  
  107.     IF Cam.Mode = 2 THEN
  108.         IF Cam.PosY > -1 * (GetHeight!(Cam.PosX, Cam.PosZ)) - 2.5 THEN Cam.PosY = -1 * (GetHeight!(Cam.PosX, Cam.PosZ)) - 2.5
  109.     END IF
  110.  
  111. SUB InitTerrain
  112.     TerrainType = 1 'INT(RND * 3) + 1
  113.     IF TerrainType = 1 THEN
  114.         'Wetlands
  115.         Land.InitHeight = 15
  116.         Land.MaxHeightChange = 3
  117.         Land.MainRnd = 100
  118.         Land.EnableWater = 1
  119.         Land.MaxHeight = 50
  120.         Land.MinHeight = -15
  121.         Land.WaterHeight = 0
  122.         Land.MaxWaterHeight = 1
  123.         Land.WaterSpeed = 0.001
  124.     ELSEIF TerrainType = 2 THEN
  125.         'Mountains
  126.         Land.InitHeight = -50
  127.         Land.MaxHeightChange = 15
  128.         Land.MainRnd = 1000
  129.         Land.EnableWater = 0
  130.         Land.MaxHeight = -50
  131.         Land.MinHeight = 15
  132.     ELSEIF TerrainType = 3 THEN
  133.         'Graslands
  134.         Land.InitHeight = -15
  135.         Land.MaxHeightChange = 3
  136.         Land.MainRnd = 100
  137.         Land.EnableWater = 0
  138.         Land.MaxHeight = -50
  139.         Land.MinHeight = 15
  140.     END IF
  141.  
  142. SUB SetCamera
  143.     Cam.PosX = -1 '-1 * ((Land.Blocks * Land.BlockSize) / 2)
  144.     Cam.PosZ = -1 '-1 * ((Land.Blocks * Land.BlockSize) / 2)
  145.     Cam.PosY = (-1 * GetHeight!(Cam.PosX, Cam.PosZ)) - 2.5
  146.     Cam.RotY = 150
  147.     Cam.Xmod = 0
  148.     Cam.Zmod = -1
  149.     Cam.Mode = 1
  150.  
  151.  
  152. SUB RandomizeHeightMap
  153.     Heightmap(1, 1) = 1.1 * Land.InitHeight
  154.     FOR x = 2 TO Land.Blocks + 1
  155.         Direction = INT(RND * 2) + 1
  156.         IF Direction = 2 THEN
  157.             HeightChange = RND * Land.MaxHeightChange
  158.         ELSE
  159.             HeightChange = -1 * (RND * Land.MaxHeightChange)
  160.         END IF
  161.         Heightmap(x, 1) = Heightmap(x - 1, 1) + HeightChange
  162.         IF Heightmap(x, 1) > Land.MaxHeight THEN Heightmap(x, 1) = Land.MaxHeight
  163.         IF Heightmap(x, 1) < Land.MinHeight THEN Heightmap(x, 1) = Land.MinHeight
  164.     NEXT x
  165.     FOR z = 2 TO Land.Blocks + 1
  166.         Direction = INT(RND * 2) + 1
  167.         IF Direction = 2 THEN
  168.             HeightChange = RND * Land.MaxHeightChange
  169.         ELSE
  170.             HeightChange = -1 * (RND * Land.MaxHeightChange)
  171.         END IF
  172.         Heightmap(1, z) = Heightmap(1, z - 1) + HeightChange
  173.         IF Heightmap(1, z) > Land.MaxHeight THEN Heightmap(1, z) = Land.MaxHeight
  174.         IF Heightmap(1, z) < Land.MinHeight THEN Heightmap(1, z) = Land.MinHeight
  175.     NEXT z
  176.     FOR x = 2 TO Land.Blocks + 1
  177.         FOR z = 2 TO Land.Blocks + 1
  178.             Direction = INT(RND * 2) + 1
  179.             IF Direction = 2 THEN
  180.                 HeightChange = RND * Land.MaxHeightChange
  181.             ELSE
  182.                 HeightChange = -1 * (RND * Land.MaxHeightChange)
  183.             END IF
  184.             Heightmap(x, z) = ((Heightmap(x, z - 1) + Heightmap(x - 1, z)) / 2) + HeightChange
  185.             IF Heightmap(x, z) > Land.MaxHeight THEN Heightmap(x, z) = Land.MaxHeight
  186.             IF Heightmap(x, z) < Land.MinHeight THEN Heightmap(x, z) = Land.MinHeight
  187.         NEXT z
  188.     NEXT x
  189.  
  190.  
  191. SUB AnimateWater
  192.     Land.WaterHeight = Land.WaterHeight + Land.WaterSpeed
  193.     IF Land.WaterHeight >= Land.MaxWaterHeight OR Land.WaterHeight <= Land.MinWaterHeight THEN Land.WaterSpeed = Land.WaterSpeed * -1
  194.  
  195.  
  196.     IF AllowSubGL = 0 THEN EXIT SUB
  197.  
  198.  
  199.     '_glMatrixMode _GL_PROJECTION
  200.     '_glLoadIdentity
  201.     _glMatrixMode _GL_MODELVIEW
  202.     '_glPushMatrix
  203.     _gluPerspective 45, _WIDTH(0) / _HEIGHT(0), 1, 100
  204.     '_glEnable _GL_TEXTURE_2D
  205.     '_glEnable _GL_BLEND
  206.     '_glBlendFunc _GL_SRC_ALPHA, _GL_ONE_MINUS_SRC_ALPHA
  207.     _glEnable _GL_DEPTH_TEST
  208.     _glDepthMask _GL_TRUE
  209.     '_glAlphaFunc _GL_GREATER, 0.5
  210.     '_glEnable _GL_ALPHA_TEST
  211.     '_glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MAG_FILTER, _GL_LINEAR
  212.     '_glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MIN_FILTER, _GL_LINEAR
  213.     'load identity used to be here
  214.     _glRotatef Cam.RotY, 0, 1, 0
  215.     _glRotatef Cam.RotX, 1, 0, 0
  216.     _glRotatef Cam.RotZ, 0, 0, 1
  217.     _glTranslatef Cam.PosX, Cam.PosY, Cam.PosZ
  218.  
  219.  
  220.     FOR z = 0 TO Land.Blocks - 1
  221.         FOR x = 0 TO Land.Blocks - 1
  222.             _glBegin _GL_QUADS
  223.             _glColor3f 0, 1, 0: _glVertex3f Land.BlockSize * x, Heightmap(x + 1, z + 2), Land.BlockSize * (z + 1)
  224.             _glColor3f 0, 0, 1: _glVertex3f Land.BlockSize * (x + 1), Heightmap(x + 2, z + 2), Land.BlockSize * (z + 1)
  225.             _glColor3f 1, 1, 0: _glVertex3f Land.BlockSize * (x + 1), Heightmap(x + 2, z + 1), Land.BlockSize * z
  226.             _glColor3f 1, 0, 0: _glVertex3f Land.BlockSize * x, Heightmap(x + 1, z + 1), Land.BlockSize * z
  227.             _glEnd
  228.         NEXT x
  229.     NEXT z
  230.  
  231.     IF Land.EnableWater = 1 THEN
  232.         _glBegin _GL_QUADS
  233.         _glColor3f 0, 0, 1: _glVertex3f 0, Land.WaterHeight, 0
  234.         _glColor3f 0, 0, 1: _glVertex3f Land.BlockSize * Land.Blocks, Land.WaterHeight, 0
  235.         _glColor3f 0, 0, 1: _glVertex3f Land.BlockSize * Land.Blocks, Land.WaterHeight, Land.BlockSize * Land.Blocks
  236.         _glColor3f 0, 0, 1: _glVertex3f 0, Land.WaterHeight, Land.BlockSize * Land.Blocks
  237.         _glEnd
  238.     END IF
  239.  
  240.  
  241. FUNCTION GetHeight! (Tx, Tz)
  242.     MatrixX = ABS(INT(Tx / Land.BlockSize))
  243.     MatrixZ = ABS(INT(Tz / Land.BlockSize))
  244.  
  245.     SquareX = 1 - ((Tx / Land.BlockSize) + MatrixX)
  246.     SquareZ = 1 - ((Tz / Land.BlockSize) + MatrixZ)
  247.  
  248.     Triangle = 1
  249.     IF SquareZ < 1 - SquareX THEN Triangle = 2
  250.  
  251.     IF Triangle = 2 THEN
  252.         x1 = -1 * ((MatrixX - 1) * Land.BlockSize)
  253.         x2 = -1 * (MatrixX * Land.BlockSize)
  254.         x3 = -1 * ((MatrixX - 1) * Land.BlockSize)
  255.         z1 = -1 * ((MatrixZ - 1) * Land.BlockSize)
  256.         z2 = -1 * ((MatrixZ - 1) * Land.BlockSize)
  257.         z3 = -1 * (MatrixZ * Land.BlockSize)
  258.         y1 = Heightmap(MatrixX, MatrixZ)
  259.         y2 = Heightmap(MatrixX + 1, MatrixZ)
  260.         y3 = Heightmap(MatrixX, MatrixZ + 1)
  261.     END IF
  262.  
  263.     IF Triangle = 1 THEN
  264.         x1 = -1 * (MatrixX * Land.BlockSize)
  265.         x2 = -1 * (MatrixX * Land.BlockSize)
  266.         x3 = -1 * ((MatrixX - 1) * Land.BlockSize)
  267.         z1 = -1 * (MatrixZ * Land.BlockSize)
  268.         z2 = -1 * ((MatrixZ - 1) * Land.BlockSize)
  269.         z3 = -1 * (MatrixZ * Land.BlockSize)
  270.         y1 = Heightmap(MatrixX + 1, MatrixZ + 1)
  271.         y2 = Heightmap(MatrixX + 1, MatrixZ)
  272.         y3 = Heightmap(MatrixX, MatrixZ + 1)
  273.     END IF
  274.  
  275.     A = y1 * (z2 - z3) + y2 * (z3 - z1) + y3 * (z1 - z2)
  276.     B = z1 * (x2 - x3) + z2 * (x3 - x1) + z3 * (x1 - x2)
  277.     C = x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)
  278.     D = 0 - (x1 * (y2 * z3 - y3 * z2) + x2 * (y3 * z1 - y1 * z3) + x3 * (y1 * z2 - y2 * z1))
  279.     GetHeight = 0 - (D + A * Tx + C * Tz) / B
  280.  
« Last Edit: March 03, 2019, 11:32:38 pm by Craz1000 »

Offline Craz1000

  • Forum Regular
  • Posts: 111
  • I'm OK
    • View Profile
    • Craz1000.net
Re: Interesting Result
« Reply #7 on: March 03, 2019, 09:06:53 pm »
Delete this post, figured out the issue.
« Last Edit: March 03, 2019, 10:36:53 pm by Craz1000 »