Author Topic: Easy to use 3D Engine/Scene Manager using Map Triangle  (Read 4000 times)

0 Members and 1 Guest are viewing this topic.

Offline Mithra

  • Newbie
  • Posts: 2
    • View Profile
Easy to use 3D Engine/Scene Manager using Map Triangle
« on: September 12, 2020, 04:00:27 pm »
This is a 3d scene manager to easily display and move multiple 3d objects with texture mapping (supports wavefront obj files which you can create with most 3d programs like blender). It uses MAPTRIANGLE so theres no need to use OpenGL commands. It's great for games! Enjoy!

Description
A simple to use 3d library for qb64. Uses map triangle to display polygons. Simple shading on models. Load multiple 3d objects into the scene and move the camera. Supports loading of wavefront obj files and texture mapping

Download: https://github.com/creamcast/3D-Library-for-QB64

Functions and Subs:
* loadObj (filename AS STRING) load a WAVEFRONT obj file, returns the ID of the object as an integer to be used for the 3d commands
* createTexture (filename AS STRING, image_array() AS LONG) loads and creatures the texture for 3d objects. Provide an 1D array where the textures will be generated and stored
* DISPOBJ (objid AS INTEGER, texture() AS LONG) display the 3d object, provide an object id and texture array that was created using createTexture()
* SETOBJHIDDEN (objid AS INTEGER, n AS INTEGER) Set to 0 for object to be hidden, set to 1 to be rendered
* SETOBJX (objid AS INTEGER, x AS FLOAT) set object x coord on the 3d plane
* SETOBJY (objid AS INTEGER, y AS FLOAT) set object y coord on the 3d plane
* SETOBJZ (objid AS INTEGER, z AS FLOAT) set object z coord on the 3d plane
* SETOBJPOS (objid AS INTEGER, x AS FLOAT, y AS FLOAT, z AS FLOAT) set object x y z coord on the 3d plane
* SETOBJROT (objid AS INTEGER, xr AS FLOAT, yr AS FLOAT, zr AS FLOAT) set object x, y, z rotation
* ROTATEOBJX (objid AS INTEGER, deg AS FLOAT) rotate the object on its x axis by degrees
* ROTATEOBJY (objid AS INTEGER, deg AS FLOAT) rotate the object on its y axis by degrees
* ROTATEOBJZ (objid AS INTEGER, deg AS FLOAT) rotate the object on its z axis by degrees
* MOVEOBJX (objid AS INTEGER, n AS FLOAT) move object x position by n
* MOVEOBJY (objid AS INTEGER, n AS FLOAT) move object y position by n
* MOVEOBJZ (objid AS INTEGER, n AS FLOAT) move object z position by n
* SETOBJSCALE (objid AS INTEGER, x AS FLOAT, y AS FLOAT, z AS FLOAT) set object xyz scale. set all to 1 for original size.
* ROTATECAMX (deg AS FLOAT) rotate the camera x by degrees
* ROTATECAMY (deg AS FLOAT) rotate the camera y by degrees
* ROTATECAMZ (deg AS FLOAT) rotate the camera z by degrees
* MOVECAMX (n AS FLOAT) move cam x position
* MOVECAMY (n AS FLOAT) move cam y position
* MOVECAMZ (n AS FLOAT) move cam z position

You can also modify the object through the global variable g_objects:
TYPE OBJECT
    id AS INTEGER
    polygon_index_start AS INTEGER
    polygon_index_end AS INTEGER
    x AS FLOAT
    y AS FLOAT
    z AS FLOAT
    rotx AS FLOAT
    roty AS FLOAT
    rotz AS FLOAT
    scalex AS FLOAT
    scaley AS FLOAT
    scalez AS FLOAT
    billboard AS INTEGER
    hidden AS INTEGER
END TYPE


Sample Code:
Code: QB64: [Select]
  1. '$INCLUDE: '3d.bi'
  2.  
  3. DIM SHARED G_MAINSCREEN
  4. G_MAINSCREEN = NEWIMAGE(1024, 768, 32)
  5. SCREEN G_MAINSCREEN
  6.  
  7. DIM obj, obj2
  8. DIM tex(0) AS LONG
  9.  
  10. 'load and create texture and store in tex()
  11. createTexture "dice.png", tex()
  12.  
  13. 'load dice 3d model
  14. obj = loadObj("dice.obj")
  15.  
  16. 'load 3d model of an arm
  17. obj2 = loadObj("arm.obj")
  18.  
  19. 'place camera at -1000,0,1000 on 3d plane
  20. g_cam.x = -1000
  21. g_cam.z = 1000
  22. g_cam.y = 0
  23.  
  24. 'rotate camera on y axis by -45 degrees
  25. g_cam.roty = -45
  26.  
  27. 'move arm object to the left
  28. g_objects(2).x = -250
  29.  
  30.     CLS , RGB(0, 0, 0)
  31.    
  32.     'rotate objects every frame
  33.     ROTATEOBJX 1, -1
  34.     ROTATEOBJY 2, -2
  35.  
  36.     'fly into the objects at 45 degree angle
  37.     g_cam.x = g_cam.x + SIN(Deg2Rad(45)) * 3
  38.     g_cam.z = g_cam.z - COS(Deg2Rad(45)) * 3
  39.  
  40.     'display objects
  41.     DISPOBJ 1, tex()
  42.     DISPOBJ 2, tex()
  43.  
  44.     LIMIT 60
  45.     DISPLAY
  46.  
  47. '$INCLUDE: '3d.bm'
  48.  
  49.  

Screenshots:
 
screenshot1.png

 
screenshot2.png

« Last Edit: September 12, 2020, 06:18:16 pm by Mithra »

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Easy to use 3D Engine/Scene Manager using Map Triangle
« Reply #1 on: September 12, 2020, 09:05:41 pm »
Mithra -

Beautiful work. A project after my own heart! We have maybe a half-dozen (non-silent at least) members who dabble in proper 3D projections - this work is definitely a good asset for us. In fact, it may be worth opening a new sub-section on the forums that has to do with 3D without explicit _GL calls (i.e. MapTriangle only).
You're not done when it works, you're done when it's right.

Offline Mithra

  • Newbie
  • Posts: 2
    • View Profile
Re: Easy to use 3D Engine/Scene Manager using Map Triangle
« Reply #2 on: September 12, 2020, 10:52:03 pm »
Mithra -

Beautiful work. A project after my own heart! We have maybe a half-dozen (non-silent at least) members who dabble in proper 3D projections - this work is definitely a good asset for us. In fact, it may be worth opening a new sub-section on the forums that has to do with 3D without explicit _GL calls (i.e. MapTriangle only).

hey I'm glad it will help and contribute to the 3d community. I was able to dig up other models with textures to create a better example scene, the texture mapping seems to be working really well.

 
screenshot3.png
« Last Edit: September 12, 2020, 10:57:56 pm by Mithra »

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Easy to use 3D Engine/Scene Manager using Map Triangle
« Reply #3 on: September 13, 2020, 01:46:36 am »
This is very impressive work (without the use of GL calls) !! Thank you for sharing this @Mithra! :)
If you are interested, you can also have a look at my OBJ-loader (especially the loader, not the GL rendering part).
https://github.com/AshishKingdom/OpenGL-OBJ-Loader
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 Unseen Machine

  • Forum Regular
  • Posts: 158
  • Make the game not the engine!
    • View Profile
Re: Easy to use 3D Engine/Scene Manager using Map Triangle
« Reply #4 on: September 13, 2020, 03:00:44 am »
Nice work @Mithra

The load/render code we've had for ages so i'll gloss over that. What really impresses me is all the other functions, especially the rotating of objects, that's always been a pain in my butt! So hats of to you, i'll defo play around with this in the future.

Attached are Quake 1, Quake 2 and Quake 3 model loaders (the Quake 3 one is still very much a work in progress) that all use OpenGL, all you'll have to do it convert the render functions to Maptriangle and they should work. Fell free to convert them and add to your engine. I'm happy to help anyway i can.

Unseen
* MDL.bm (Filesize: 17.94 KB, Downloads: 247)
* MD2.bm (Filesize: 13.61 KB, Downloads: 256)
* Models.bi (Filesize: 1.55 KB, Downloads: 238)
* md3.bas (Filesize: 16.02 KB, Downloads: 275)