Author Topic: OpenGl Lights & Material  (Read 4312 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
OpenGl Lights & Material
« on: March 10, 2020, 07:16:04 pm »
This one is from Ashish I (bplus) probably picked up from [abandoned, outdated and now likely malicious qb64 dot net website - don’t go there] (my date is 2017-07-30)

Code: QB64: [Select]
  1. 'OpenGL Lights & Material By Ashish update 2017-07-30
  2.  
  3. _TITLE "OpenGL Lights & Material"
  4.  
  5. SCREEN _NEWIMAGE(800, 600, 32)
  6.  
  7. DIM SHARED glAllow AS _BYTE
  8.     SUB gluLookAt (BYVAL eyeX#, BYVAL eyeY#, BYVAL eyeZ#, BYVAL centerX#, BYVAL centerY#, BYVAL centerZ#, BYVAL upX#, BYVAL upY#, BYVAL upZ#)
  9.     SUB glutSolidSphere (BYVAL radius AS DOUBLE, BYVAL slices AS LONG, BYVAL stack AS LONG)
  10.  
  11. 'Used by GLH RGB/etc helper functions
  12. DIM SHARED DONT_USE_GLH_COL_RGBA(1 TO 4) AS SINGLE
  13.  
  14. 'Used to manage textures
  15. TYPE DONT_USE_GLH_Handle_TYPE
  16.     in_use AS _BYTE
  17.     handle AS LONG
  18.  
  19. TYPE vec3
  20.     x AS SINGLE
  21.     y AS SINGLE
  22.     z AS SINGLE
  23.  
  24.  
  25. 'Used by GLH RGB/etc helper functions
  26. REDIM SHARED DONT_USE_GLH_Handle(1000) AS DONT_USE_GLH_Handle_TYPE
  27.  
  28. DIM SHARED redLight AS vec3
  29. DIM SHARED greenLight AS vec3
  30. DIM SHARED blueLight AS vec3
  31.  
  32.  
  33.  
  34. glAllow = -1
  35.     _LIMIT 40
  36. LOOP UNTIL k& = ASC(CHR$(27))
  37.  
  38.  
  39. SUB _GL () STATIC
  40.     IF NOT glAllow THEN EXIT SUB
  41.  
  42.     _glEnable _GL_DEPTH_TEST
  43.     _glEnable _GL_LIGHTING
  44.  
  45.     _glEnable _GL_LIGHT0 'we need three lights, each for red, green & blue.
  46.     _glEnable _GL_LIGHT1
  47.     _glEnable _GL_LIGHT2
  48.  
  49.     _glLightfv _GL_LIGHT0, _GL_AMBIENT, GLH_RGB(0, 0, 0)
  50.     _glLightfv _GL_LIGHT0, _GL_DIFFUSE, GLH_RGB(.5, 0, 0)
  51.     _glLightfv _GL_LIGHT0, _GL_SPECULAR, GLH_RGB(1, 0, 0)
  52.     _glLightfv _GL_LIGHT0, _GL_POSITION, GLH_RGBA(redLight.x, redLight.y, redLight.z, 0)
  53.  
  54.     _glLightfv _GL_LIGHT1, _GL_AMBIENT, GLH_RGB(0, 0, 0)
  55.     _glLightfv _GL_LIGHT1, _GL_DIFFUSE, GLH_RGB(0, .5, 0)
  56.     _glLightfv _GL_LIGHT1, _GL_SPECULAR, GLH_RGB(0, 1, 0)
  57.     _glLightfv _GL_LIGHT1, _GL_POSITION, GLH_RGBA(greenLight.x, greenLight.y, greenLight.z, 0)
  58.  
  59.     _glLightfv _GL_LIGHT2, _GL_AMBIENT, GLH_RGB(0, 0, 0)
  60.     _glLightfv _GL_LIGHT2, _GL_DIFFUSE, GLH_RGB(0, 0, .5)
  61.     _glLightfv _GL_LIGHT2, _GL_SPECULAR, GLH_RGB(0, 0, 1)
  62.     _glLightfv _GL_LIGHT2, _GL_POSITION, GLH_RGBA(blueLight.x, blueLight.y, blueLight.z, 0)
  63.  
  64.     _glMatrixMode _GL_PROJECTION
  65.  
  66.     IF NOT glSetup THEN
  67.         aspect# = _WIDTH / _HEIGHT
  68.         glSetup = -1
  69.         _glViewport 0, 0, _WIDTH, _HEIGHT
  70.     END IF
  71.  
  72.     _gluPerspective 45.0, aspect#, 1.0, 100.0
  73.  
  74.     _glMatrixMode _GL_MODELVIEW
  75.  
  76.     gluLookAt 0, 0, 5, 0, 0, 0, 0, 1, 0
  77.  
  78.     _glColor3f 0, 0, 0
  79.  
  80.     _glMaterialfv _GL_FRONT_AND_BACK, _GL_AMBIENT, GLH_RGB(0, 0, 0)
  81.     _glMaterialfv _GL_FRONT_AND_BACK, _GL_DIFFUSE, GLH_RGB(0.8, 0.8, 0.8)
  82.     _glMaterialfv _GL_FRONT_AND_BACK, _GL_SPECULAR, GLH_RGB(.86, .86, .86)
  83.     _glMaterialfv _GL_FRONT_AND_BACK, _GL_SHININESS, GLH_RGB(128 * .566, 0, 0)
  84.  
  85.     glutSolidSphere 1, 100, 100
  86.  
  87.     _glDisable _GL_LIGHTING
  88.  
  89.     _glTranslatef redLight.x, redLight.y, redLight.z
  90.     _glColor3f 1, 0, 0
  91.     glutSolidSphere .05, 20, 20
  92.  
  93.     _glTranslatef greenLight.x, greenLight.y, greenLight.z
  94.     _glColor3f 0, 1, 0
  95.     glutSolidSphere .05, 20, 20
  96.  
  97.     _glTranslatef blueLight.x, blueLight.y, .1
  98.     _glColor3f 0, 0, 1
  99.     glutSolidSphere .05, 20, 20
  100.  
  101.     _glFlush
  102.  
  103.     clock# = clock# + .01
  104.  
  105.     redLight.x = SIN(clock# * 1.5) * 1.5
  106.     redLight.z = COS(clock# * 1.5) * 1.5
  107.  
  108.     greenLight.y = COS(clock# * .8) * 1.5
  109.     greenLight.z = SIN(clock# * .8) * 1.5
  110.  
  111.     blueLight.x = SIN(clock#) * 1.5
  112.     blueLight.y = COS(clock#) * 1.5
  113.  
  114.  
  115. FUNCTION map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  116.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  117.  
  118.  
  119. 'below, all functions are coded by Galleon
  120. FUNCTION GLH_Image_to_Texture (image_handle AS LONG) 'turn an image handle into a texture handle
  121.     IF image_handle >= 0 THEN ERROR 258: EXIT FUNCTION 'don't allow screen pages
  122.     DIM m AS _MEM
  123.     m = _MEMIMAGE(image_handle)
  124.     DIM h AS LONG
  125.     h = DONT_USE_GLH_New_Texture_Handle
  126.     GLH_Image_to_Texture = h
  127.     _glBindTexture _GL_TEXTURE_2D, DONT_USE_GLH_Handle(h).handle
  128.     _glTexImage2D _GL_TEXTURE_2D, 0, _GL_RGBA, _WIDTH(image_handle), _HEIGHT(image_handle), 0, &H80E1&&, _GL_UNSIGNED_BYTE, m.OFFSET
  129.     _MEMFREE m
  130.  
  131. FUNCTION DONT_USE_GLH_New_Texture_Handle
  132.     handle&& = 0
  133.     _glGenTextures 1, _OFFSET(handle&&)
  134.     DONT_USE_GLH_New_Texture_Handle = handle&&
  135.     FOR h = 1 TO UBOUND(DONT_USE_GLH_Handle)
  136.         IF DONT_USE_GLH_Handle(h).in_use = 0 THEN
  137.             DONT_USE_GLH_Handle(h).in_use = 1
  138.             DONT_USE_GLH_Handle(h).handle = handle&&
  139.             DONT_USE_GLH_New_Texture_Handle = h
  140.             EXIT FUNCTION
  141.         END IF
  142.     NEXT
  143.     REDIM _PRESERVE DONT_USE_GLH_Handle(UBOUND(DONT_USE_GLH_HANDLE) * 2) AS DONT_USE_GLH_Handle_TYPE
  144.     DONT_USE_GLH_Handle(h).in_use = 1
  145.     DONT_USE_GLH_Handle(h).handle = handle&&
  146.     DONT_USE_GLH_New_Texture_Handle = h
  147.  
  148. SUB GLH_Select_Texture (texture_handle AS LONG) 'turn an image handle into a texture handle
  149.     IF texture_handle < 1 OR texture_handle > UBOUND(DONT_USE_GLH_HANDLE) THEN ERROR 258: EXIT FUNCTION
  150.     IF DONT_USE_GLH_Handle(texture_handle).in_use = 0 THEN ERROR 258: EXIT FUNCTION
  151.     _glBindTexture _GL_TEXTURE_2D, DONT_USE_GLH_Handle(texture_handle).handle
  152.  
  153.  
  154. FUNCTION GLH_RGB%& (r AS SINGLE, g AS SINGLE, b AS SINGLE)
  155.     DONT_USE_GLH_COL_RGBA(1) = r
  156.     DONT_USE_GLH_COL_RGBA(2) = g
  157.     DONT_USE_GLH_COL_RGBA(3) = b
  158.     DONT_USE_GLH_COL_RGBA(4) = 1
  159.     GLH_RGB = _OFFSET(DONT_USE_GLH_COL_RGBA())
  160.  
  161. FUNCTION GLH_RGBA%& (r AS SINGLE, g AS SINGLE, b AS SINGLE, a AS SINGLE)
  162.     DONT_USE_GLH_COL_RGBA(1) = r
  163.     DONT_USE_GLH_COL_RGBA(2) = g
  164.     DONT_USE_GLH_COL_RGBA(3) = b
  165.     DONT_USE_GLH_COL_RGBA(4) = a
  166.     GLH_RGBA = _OFFSET(DONT_USE_GLH_COL_RGBA())
  167.  
  168.  
  169.  

 
OpenGL Lights & Materials.PNG

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: OpenGl Lights & Material
« Reply #1 on: March 11, 2020, 01:26:09 am »
This is SO cool... But I noticed the lack of aliens! Nah. Kidding. Great example!!

J
Logic is the beginning of wisdom.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: OpenGl Lights & Material
« Reply #2 on: March 11, 2020, 08:20:49 am »
Torus MOD by Petr is also cool!

 
OpenGL Lights & Material Torus (Solid).png

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: OpenGl Lights & Material
« Reply #3 on: March 11, 2020, 12:26:43 pm »
Is it just me? That torus lighting looks off.

Where is blue light for left, top edge, red for right edge and yellow for ring around center?

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Re: OpenGl Lights & Material
« Reply #4 on: March 11, 2020, 01:51:20 pm »
When I get through Ashish's full repository, I'll run Petr's version and if OK move it to Samples.  It'll be good to have some in SUB_GL which are other than Ashish's.  I may be a few days.  Good things come to those who wait.

Edit:  Tried Petr's modification.  Although a very good demonstration of Open_GL, it somehow lacks the wow factor of Ashish's original.  I'll use Ashishs's in Samples.
« Last Edit: March 12, 2020, 05:25:10 am by Qwerkey »