Author Topic: Pyramid with tile texture  (Read 4560 times)

0 Members and 1 Guest are viewing this topic.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Pyramid with tile texture
« on: March 26, 2021, 10:25:51 am »
Hey everyone! I've a new opengl program for you!

Code: QB64: [Select]
  1. _Title "Pyramid with tile texture"
  2. 'Coded by Ashish
  3. '26 march, 2021
  4. Screen _NewImage(600, 600, 32)
  5.  
  6. Type vec3
  7.     As Single x, y, z
  8.  
  9.     'for camera
  10.     Sub gluLookAt (ByVal eyeX#, Byval eyeY#, Byval eyeZ#, Byval centerX#, Byval centerY#, Byval centerZ#, Byval upX#, Byval upY#, Byval upZ#)
  11.  
  12.  
  13. Dim Shared glAllow As _Byte
  14. Dim Shared texture_data As Long, pyramid_coord(3) As vec3, texcoord(2) As vec3
  15.  
  16. 'coordinate data
  17. pyramid_coord(0).x = 0: pyramid_coord(0).y = 2: pyramid_coord(0).z = 0
  18. pyramid_coord(1).x = -2: pyramid_coord(1).y = -2: pyramid_coord(1).z = -2
  19. pyramid_coord(2).x = 2: pyramid_coord(2).y = -2: pyramid_coord(2).z = -2
  20. pyramid_coord(3).x = 0: pyramid_coord(3).y = -2: pyramid_coord(3).z = 2
  21. texcoord(0).x = 0: texcoord(0).y = 0
  22. texcoord(1).x = 0.5: texcoord(1).y = 1
  23. texcoord(2).x = 1: texcoord(2).y = 0
  24.  
  25. texture_data = _NewImage(400, 400, 32)
  26. texture_data_r = _CopyImage(texture_data)
  27. glAllow = 1
  28. 'set these shades values as per your requirement
  29. shade1& = _RGB(15, 190, 200)
  30. shade2& = _RGB(240, 10, 70)
  31.     _Dest texture_data_r
  32.     Line (0, 0)-(_Width(texture_data_r), _Height(texture_data_r)), shade1&, BF
  33.     i = 1
  34.     For y = 0 To _Height(texture_data_r) Step 40
  35.         i = i * -1
  36.         For x = -40 + 20 * i To _Width(texture_data_r) + 40 Step 80
  37.             CircleFill x + offx, y, 20, shade2&
  38.         Next
  39.     Next
  40.     offx = offx + 3
  41.     If offx > 80 Then offx = 0
  42.     _Dest 0
  43.     _PutImage , texture_data_r, texture_data
  44.     Cls , shade1&
  45.     For x = -100 To 700 Step 100
  46.         Line (x + ax, 0)-(x + ax, 700), shade2&
  47.         Line (x + ax + 50, 0)-(x + ax + 50, 700), shade2&
  48.         Paint (x + ax + 49, 0), shade2&, shade2&
  49.         Paint (x + ax + 1, 0), shade2&, shade2&
  50.     Next
  51.     If ax > 100 Then ax = 0
  52.     ax = ax + 2
  53.     _Limit 60
  54.     _Display
  55.  
  56. Sub _GL ()
  57.     If glAllow = 0 Then Exit Sub
  58.     Static glInit, tex As Long, m As _MEM, t
  59.     If glInit = 0 Then
  60.         glInit = 1
  61.         _glViewport 0, 0, _Width, _Height
  62.         'texture
  63.         _glGenTextures 1, _Offset(tex)
  64.         m = _MemImage(texture_data)
  65.  
  66.     End If
  67.  
  68.     _glEnable _GL_TEXTURE_2D
  69.     _glEnable _GL_DEPTH_TEST
  70.     _glEnable _GL_BLEND
  71.  
  72.     'projection settings
  73.     _glMatrixMode _GL_PROJECTION
  74.     _gluPerspective 60, _Width / _Height, 0.1, 10
  75.  
  76.     _glMatrixMode _GL_MODELVIEW
  77.     gluLookAt 0, 0, 6, 0, 0, 0, 0, 1, 0
  78.     _glRotatef t, 0, 1, 0.8
  79.  
  80.     'drawing pyramid
  81.     _glBindTexture _GL_TEXTURE_2D, tex
  82.     _glTexImage2D _GL_TEXTURE_2D, 0, _GL_RGB, _Width(texture_data), _Height(texture_data), 0, _GL_BGRA_EXT, _GL_UNSIGNED_BYTE, m.OFFSET
  83.     _glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MAG_FILTER, _GL_LINEAR
  84.     _glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MIN_FILTER, _GL_NEAREST
  85.  
  86.     For i = 0 To 3
  87.         _glBegin _GL_TRIANGLES
  88.         For j = i To i + 2
  89.             _glTexCoord2f texcoord(j Mod 3).x, texcoord(j Mod 3).y
  90.             _glVertex3f pyramid_coord(j Mod 4).x, pyramid_coord(j Mod 4).y, pyramid_coord(j Mod 4).z
  91.         Next
  92.         _glEnd
  93.     Next
  94.     t = t + 1
  95.  
  96.  
  97. Sub CircleFill (x As Long, y As Long, R As Long, C As _Unsigned Long)
  98.     Dim x0 As Single, y0 As Single
  99.     Dim e As Single
  100.  
  101.     x0 = R
  102.     y0 = 0
  103.     e = -R
  104.     Do While y0 < x0
  105.         If e <= 0 Then
  106.             y0 = y0 + 1
  107.             Line (x - x0, y + y0)-(x + x0, y + y0), C, BF
  108.             Line (x - x0, y - y0)-(x + x0, y - y0), C, BF
  109.             e = e + 2 * y0
  110.         Else
  111.             Line (x - y0, y - x0)-(x + y0, y - x0), C, BF
  112.             Line (x - y0, y + x0)-(x + y0, y + x0), C, BF
  113.             x0 = x0 - 1
  114.             e = e - 2 * x0
  115.         End If
  116.     Loop
  117.     Line (x - R, y)-(x + R, y), C, BF
  118.  
  119.  
  120.  
« Last Edit: March 27, 2021, 08:38:56 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 SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Pyramid with tile texture
« Reply #1 on: March 26, 2021, 10:28:47 am »
Awesome program, Ashish!
Shuwatch!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Pyramid with tile texture
« Reply #2 on: March 26, 2021, 12:40:12 pm »
Well it got me to download v1.5 to run it.

This stuff was cutting edge back in 1980's. Reminds me of something that could make a video game like Zaxxon. It was not something one could easily do in QBasic, so it's neat to see it working via OpenGL in QB64.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Pyramid with tile texture
« Reply #3 on: March 26, 2021, 06:13:54 pm »
That's pretty cool... Definitely a workout for the old optic nerves... Nicely done... Does it come in blue? lol
Logic is the beginning of wisdom.

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
Re: Pyramid with tile texture
« Reply #4 on: March 26, 2021, 07:41:06 pm »
It works better if you plug it in.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Pyramid with tile texture
« Reply #5 on: March 27, 2021, 01:48:56 am »
This is nice, @Ashish.

I am wondering, this can be done without GL, I am fairly certain.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Pyramid with tile texture
« Reply #6 on: March 27, 2021, 08:50:02 am »
Thanks @SpriggsySpriggs , @Pete, @johnno56, @Richard Frost and @bplus
@johnno56 , I have updated the code. You can change the shades color as per your requirement on line 32 and 33.

@bplus without _GL? are you going to make one? that would be awesome! :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 TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Pyramid with tile texture
« Reply #7 on: March 27, 2021, 11:33:16 am »
@Ashish
Cool !
Mmmmmh you have missed the right color, light blue and pink and not red!
:-)
Good job.
Programming isn't difficult, only it's  consuming time and coffee