Author Topic: OpenGL Lights & Material by Ashish  (Read 419 times)

Offline Qwerkey

  • Moderator
  • Forum Resident
  • Posts: 736
OpenGL Lights & Material by Ashish
« on: March 12, 2020, 05:37:47 AM »
OpenGL Lights & Material

Author: @ Ashish
Source: qb64.org Forum
URL: https://www.qb64.org/forum/index.php?topic=2323.0
A full repository of QB64_OpenGL_Demos by Ashish Kingdom and Petr Preclik, which contains all the supplementary files/resources as well as screenshots and many other GL programs:
https://github.com/AshishKingdom/QB64_OpenGL_Demos/archive/master.zip
Version: 1
Tags: [3D], [Graphics], [Open_GL]

Description:
Stunning 3D effect of moving lights with reflections off the surface of a sphere

Source Code:
Code: QB64: [Select]
  1. 'OpenGL Lights & Material By Ashish
  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())

 
« Last Edit: March 27, 2020, 05:55:24 AM by Qwerkey »