Author Topic: A question about OpenGL shapes and applying a texture?  (Read 2777 times)

0 Members and 1 Guest are viewing this topic.

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
A question about OpenGL shapes and applying a texture?
« on: March 18, 2019, 11:43:18 pm »
More so a question for Ashish and his example from this post;
https://www.qb64.org/forum/index.php?topic=1137.msg103321#msg103321
actually 2 questions,
A: How is the GL rountine called? I don't see any reference calls in the Main loop in this example
B: Is it possible to apply a Texture map to the Cube? to say make it look like a 6 sided Dice?

Code: QB64: [Select]
  1. '##########################
  2. '  OpenGL 3D Shapes
  3. '    By Ashish Kushwaha
  4. '##########################
  5. ' :)
  6.  
  7. _TITLE "OpenGL 3D Shapes"
  8. SCREEN _NEWIMAGE(600, 600, 32)
  9.  
  10.  
  11. 'Sphere
  12.     SUB glutSolidSphere (BYVAL radius AS DOUBLE, BYVAL slices AS LONG, BYVAL stack AS LONG)
  13.     SUB glutWireSphere (BYVAL radius AS DOUBLE, BYVAL slices AS LONG, BYVAL stack AS LONG)
  14.  
  15.     'Cube
  16.     SUB glutWireCube (BYVAL dsize AS DOUBLE)
  17.     SUB glutSolidCube (BYVAL dsize AS DOUBLE)
  18.  
  19.     'Torus
  20.     SUB glutWireTorus (BYVAL dInnerRadius AS DOUBLE, BYVAL dOuterRadius AS DOUBLE, BYVAL nSides AS LONG, BYVAL nRings AS LONG)
  21.     SUB glutSolidTorus (BYVAL dInnerRadius AS DOUBLE, BYVAL dOuterRadius AS DOUBLE, BYVAL nSides AS LONG, BYVAL nRings AS LONG)
  22.  
  23.     'Cone
  24.     SUB glutWireCone (BYVAL base AS DOUBLE, BYVAL height AS DOUBLE, BYVAL slices AS LONG, BYVAL stack AS LONG)
  25.     SUB glutSolidCone (BYVAL base AS DOUBLE, BYVAL height AS DOUBLE, BYVAL slices AS LONG, BYVAL stack AS LONG)
  26.  
  27.     'Cylinder
  28.     SUB glutWireCylinder (BYVAL base AS DOUBLE, BYVAL height AS DOUBLE, BYVAL slices AS LONG, BYVAL stack AS LONG)
  29.     SUB glutSolidCylinder (BYVAL base AS DOUBLE, BYVAL height AS DOUBLE, BYVAL slices AS LONG, BYVAL stack AS LONG)
  30.  
  31.  
  32. 'Used by GLH RGB/etc helper functions
  33. DIM SHARED DONT_USE_GLH_COL_RGBA(1 TO 4) AS SINGLE
  34.  
  35. DIM SHARED shapeId
  36. DIM SHARED walk 'this will hold value about how close we are to our object
  37. walk = 5
  38.     k& = _KEYHIT
  39.     IF k& = ASC(" ") THEN
  40.         IF shapeId = 4 THEN shapeId = 0 ELSE shapeId = shapeId + 1
  41.     END IF
  42.     'Hi Sir!
  43.     'try to press 'w' or 's'
  44.     IF k& = ASC("s") THEN walk = walk + .05
  45.     IF k& = ASC("w") THEN walk = walk - .05
  46.     _LIMIT 30
  47.  
  48.  
  49.     'Enable Z-Buffer read/write
  50.     _glEnable _GL_DEPTH_TEST
  51.    
  52.     _glEnable _GL_LIGHTING
  53.     _glEnable _GL_LIGHT0
  54.    
  55.     _glLightfv _GL_LIGHT0, _GL_AMBIENT, GLH_RGB(.15, .15, .15)
  56.     _glLightfv _GL_LIGHT0, _GL_SPECULAR, GLH_RGB(.8, .8, .8)
  57.     _glLightfv _GL_LIGHT0, _GL_POSITION, GLH_RGBA(COS(clock#), 0, SIN(clock#), 0)
  58.    
  59.     'clears the depth
  60.     _glClearDepth 1.0
  61.    
  62.     'swich Projection matrix mode
  63.     _glMatrixMode _GL_PROJECTION
  64.    
  65.     'setting up perpective
  66.     aspect# = _WIDTH / _HEIGHT
  67.     _gluPerspective 40, aspect#, 1, 200
  68.    
  69.     'Now, we'll use ModelView
  70.     _glMatrixMode _GL_MODELVIEW
  71.    
  72.     _glClear _GL_COLOR_BUFFER_BIT OR _GL_DEPTH_BUFFER_BIT
  73.     'this clock# variable increases everytime as it is  used for rotation.
  74.     clock# = clock# + .01
  75.    
  76.     'note that  here walk value should be negative.
  77.     'Lesser the walk, object become closes, and vise versa
  78.     _glTranslatef 0, 0, -walk
  79.    
  80.     _glRotatef 30 * clock#, 1, 0, 0
  81.     _glRotatef 60 * clock#, 0, 1, 0
  82.     _glRotatef 90 * clock#, 0, 0, 1
  83.  
  84.     _glColor3f 1, 1, 1
  85.  
  86.     SELECT CASE shapeId
  87.         CASE 0
  88.             glutSolidSphere .5, 50, 50
  89.         CASE 1
  90.             glutSolidTorus .2, .5, 50, 50
  91.         CASE 2
  92.             glutSolidCube .5
  93.         CASE 3
  94.             glutSolidCone .4, .5, 30, 30
  95.         CASE 4
  96.             glutSolidCylinder .2, .8, 40, 40
  97.     END SELECT
  98.  
  99.     _glFlush
  100.  
  101. 'used opengl rgba functions
  102. FUNCTION GLH_RGB%& (r AS SINGLE, g AS SINGLE, b AS SINGLE)
  103.     DONT_USE_GLH_COL_RGBA(1) = r
  104.     DONT_USE_GLH_COL_RGBA(2) = g
  105.     DONT_USE_GLH_COL_RGBA(3) = b
  106.     DONT_USE_GLH_COL_RGBA(4) = 1
  107.     GLH_RGB = _OFFSET(DONT_USE_GLH_COL_RGBA())
  108.  
  109. FUNCTION GLH_RGBA%& (r AS SINGLE, g AS SINGLE, b AS SINGLE, a AS SINGLE)
  110.     DONT_USE_GLH_COL_RGBA(1) = r
  111.     DONT_USE_GLH_COL_RGBA(2) = g
  112.     DONT_USE_GLH_COL_RGBA(3) = b
  113.     DONT_USE_GLH_COL_RGBA(4) = a
  114.     GLH_RGBA = _OFFSET(DONT_USE_GLH_COL_RGBA())
  115.  
  116.  

or if anybody else might have a Cube routine that could have each side texture mapped as such and rotated in a 'tumbling' kind of way. I could do it in QB45 using DirectQB but not sure how to accomplish the same results with QB64.
Granted after becoming radioactive I only have a half-life!

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: A question about OpenGL shapes and applying a texture?
« Reply #1 on: March 23, 2019, 09:35:29 am »
Hi Cobalt!
A : GL routine is a special subroutine. It called automatically at 60 or 48 times a second depending on the refreshrate of display. I usually add a loop so that program can be hold and GL can be called continuously in the background.
B : It is possible. You can apply texture to any type of object/shape, whether it is 2D or 3D. You can even apply 3D textures as well as multiple textures.
Using textures in OpenGL is a bit cumbersome. I have started a tutorial here - https://ashishkingdom.github.io/OpenGL-Tutorials/ (not completed yet). You can either go through the tutorial and understand the behind the scenes thing about GL. For now, I can provide you a simple routine which can render a cube with the texture map as an arguement soon.

« Last Edit: March 23, 2019, 09:37:01 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