Author Topic: nGL - a fast (enough) 3D engine for the nspire  (Read 269737 times)

0 Members and 3 Guests are viewing this topic.

Offline Vogtinator

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1193
  • Rating: +108/-5
  • Instruction counter
    • View Profile
nGL - a fast (enough) 3D engine for the nspire
« on: November 26, 2013, 01:11:43 pm »
Hi,

I started this program a long while ago and it never really advanced. But now I'm making progress again!
It's basically a little library (but not intended to be a library to link with) which handles the basic stuff you need for 3D:
-Rotation
-Translation ;D
-Scaling
-Colors  :hyper:
-Drawing triangles and quads
-Perspective
-Z-Buffering
-Backface culling
-Bad wireframe mode
-Matrix stack (glPushMatrix, glPopMatrix)
-Textures O.O
-sin and cos LUT for the fixed point datatype with highest possible accuracy (degrees)
-OpenGL immediate-mode like API
-Written in C++ (operator overloading) but C-style API if needed

At first I tried to implement it with float, but that was ridiculously slow and laggy. Especially the z-buffering was painful to watch..
But after I implemented fixed point arithmetic things changed: The attached demo runs at ~100fps (1 degree rotation per frame) in the emulator and a bit slower on a real calc. I was curious how fast it really was, so I tried loading some .obj files and it was smooth until 2000 triangles. I'm sure it can be optimized further, it doesn't use a single line of assembler yet.

I tried to port glxgears but a quick and dirty port had some issues with the blue gear.

Known issues:
-No triangle-based clipping: pixel based is a bit slower (depends on the objects on screen) and if you're near a triangle, it may look weird or even crash
-Bad integer division algorithm (gcc's own): division through a small number takes ages
-Drawing triangles is still a bit slow, two increments, one multiplication and several comparisions per pixel (without textures)
-glRotatef not yet implemented
-Sometimes it tries to divide through zero and it hangs, I don't know why  >:(
-Textures lack perspective (difficult to optimize)
-Viewport is fixed: bottom: y=0 top: y=239 left: x=0 right: x=320, in OpenGL it's most times -1-1, so a scaling factor is needed

But nevertheless, I'm going to implement a simple 3D game (no, not minecraft) with this lib, but I don't know what kind of game:
- 3D Tetris (boring..)
- Motor race in 3D (how should the cars and the environment look like, also: collision detection)
- 3D snake (even more boring..)

Edit: Ok, it seems like I made a minecraft game nevertheless...
Latest release of crafti: v1.2 GitHub
Source code and tutorial for nGL are on GitHub as well

Demo controls:
-touchpad: camera rotation
-8-4-6-2: camera position
-x: rotate the four cubes

Do you think this is useful or will it stay an experiment forever?
« Last Edit: May 20, 2016, 05:55:15 pm by Vogtinator »

Offline Matrefeytontias

  • Axe roxxor (kinda)
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1982
  • Rating: +310/-12
  • Axe roxxor
    • View Profile
    • RMV Pixel Engineers
Re: nGL - a fast (enough) 3D engine for the nspire
« Reply #1 on: November 26, 2013, 01:15:58 pm »
O.O this is aaawesome !!! I hope it'll be fast enough to have actual games out of that :D

Offline TheMachine02

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 452
  • Rating: +105/-0
  • me = EF99+F41A
    • View Profile
Re: nGL - a fast (enough) 3D engine for the nspire
« Reply #2 on: November 26, 2013, 01:17:28 pm »
You made me wish to have a nspire CX with ndless. Super great job  :D
AXE/asm programmer - unleash the power of z80 //C++//C

epic 3D things http://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html

Offline Levak

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +208/-39
    • View Profile
    • My website
Re: nGL - a fast (enough) 3D engine for the nspire
« Reply #3 on: November 26, 2013, 01:22:26 pm »
Are you serious ? x)
I started nAk3D two months ago with an openGL like api !
Yours is obviously finished. ^^
Whats strange, is that I started using fixed point numbers and had a headache when multiplying matrices (int overflows) and then went to floating point and didn't seemed to lost any performances.

The only difference with nAk3D is that it uses nGC and an heap sort face algorithm instead or your zBuffer.
I do not get mad at people, I just want them to learn the way I learnt.
My website - TI-Planet - iNspired-Lua

Offline Vogtinator

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1193
  • Rating: +108/-5
  • Instruction counter
    • View Profile
Re: nGL - a fast (enough) 3D engine for the nspire
« Reply #4 on: November 26, 2013, 01:31:16 pm »
Are you serious ? x)
I started nAk3D two months ago with an openGL like api !
Yours is obviously finished. ^^
I started nGL some days before nCraft..

Quote
Whats strange, is that I started using fixed point numbers and had a headache when multiplying matrices (int overflows) and then went to floating point and didn't seemed to lost any performances.
Weird. I'm using int_fast32_t as storage and the lowest 8 bits as fractional part. (~0.004 accuracy).
Also weird that you didn't lose any performance. For me it was a significant difference (the CPU doesn't have a FPU)

Quote
The only difference with nAk3D is that it uses nGC and an heap sort face algorithm instead or your zBuffer.
Z-Buffer has some advantages: constant performance, overlapping triangles and you can maybe use the depth image for e.g. lighting.
And the most obvious one: It's very easy to implement.

Offline Matrefeytontias

  • Axe roxxor (kinda)
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1982
  • Rating: +310/-12
  • Axe roxxor
    • View Profile
    • RMV Pixel Engineers
Re: nGL - a fast (enough) 3D engine for the nspire
« Reply #5 on: November 26, 2013, 01:33:27 pm »
Are you serious ? x)
I started nAk3D two months ago with an openGL like api !
Yours is obviously finished. ^^
I started nGL some days before nCraft..
What ? nCraft is by Chockosta ...

Offline Vogtinator

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1193
  • Rating: +108/-5
  • Instruction counter
    • View Profile
Re: nGL - a fast (enough) 3D engine for the nspire
« Reply #6 on: November 26, 2013, 01:34:59 pm »
Yeah, right. I started nGL some days before Chockosta published nCraft here.
The same that happened to Levak now :P

Offline Matrefeytontias

  • Axe roxxor (kinda)
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1982
  • Rating: +310/-12
  • Axe roxxor
    • View Profile
    • RMV Pixel Engineers
Re: nGL - a fast (enough) 3D engine for the nspire
« Reply #7 on: November 26, 2013, 01:35:31 pm »
Oh okay, sorry :P

So this is a long work indeed ;)

Offline SpiroH

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 729
  • Rating: +153/-23
    • View Profile
Re: nGL - a fast (enough) 3D engine for the nspire
« Reply #8 on: November 26, 2013, 01:39:33 pm »
This looks like a real good job! Anxiously awaiting a simple 3D game. BTW, i don't mind if we start with one of the boring ones 3D Tetris, 3D Snake, etc.:P
How compatible is nGL with openGL? How difficult would it be to port simple openGL demos to npsire?

Offline Vogtinator

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1193
  • Rating: +108/-5
  • Instruction counter
    • View Profile
Re: nGL - a fast (enough) 3D engine for the nspire
« Reply #9 on: November 26, 2013, 01:57:17 pm »
That depends entirely on the features it uses, if it's only immediate mode or vertex arrays/VBOs a simple abstraction could be enough.
Then there's the problem with the viewport, nGL doesn't have a function to define the viewport or the frustrum yet.
The accuracy of my fixed-point type is also too bad for most programs which use a 1 unit = 1 meter base. Sometimes a glScale3f is sufficient, but if it isn't, you'll have to scale manually or increase the accuracy of the fixed-point type (which may lead to strange errors if it overflows).
Textures aren't bound by ids anymore and other major features are missing (lighting, materials, transparency) mostly for performance reasons.

tl;dr: It depends.

Offline Levak

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +208/-39
    • View Profile
    • My website
Re: nGL - a fast (enough) 3D engine for the nspire
« Reply #10 on: November 26, 2013, 05:53:16 pm »
nGL doesn't have a function to define the viewport or the frustrum yet.
I've seen http://www.scratchapixel.com/lessons/3d-advanced-lessons/perspective-and-orthographic-projection-matrix/opengl-perspective-projection-matrix/ a couple days ago that works well.
the entire tutorial did learn me a lot of things actually :)

Quote
The accuracy of my fixed-point type is also too bad for most programs which use a 1 unit = 1 meter base
I experimented the same problem, too large numbers explodes integer capacity when matrices are involved.
« Last Edit: November 26, 2013, 05:55:13 pm by Levak »
I do not get mad at people, I just want them to learn the way I learnt.
My website - TI-Planet - iNspired-Lua

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: nGL - a fast (enough) 3D engine for the nspire
« Reply #11 on: November 27, 2013, 01:10:49 am »
I love how fast this seems to be and the texture details. I can't wait to see how well this runs with many more things on the screen.

Offline Vogtinator

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1193
  • Rating: +108/-5
  • Instruction counter
    • View Profile
Re: nGL - a fast (enough) 3D engine for the nspire
« Reply #12 on: November 28, 2013, 03:47:51 pm »
So, I fixed a few bugs in nGL (fewer crashes) and converted my snake game (I made exactly one year ago) to 3D.
No special features, just plain snake.
You can still move around and look.
The apple is just a cube with apple texture (I found on the internet), I couldn't find a suitable 3D model of an apple and I can't use blender.
Every block on the screen is drawn and textured completely, the game isn't optimized at all (walls between two wall blocks) but still runs smooth.
Sorry, but I'm more programmer than game designer and I just couldn't do it any better...

Controls: 8-6-4-2: Move snake
                WASD: Move around (for testing with nspire_emu)
                touchpad: Look around

Offline Streetwalrus

  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3821
  • Rating: +80/-8
    • View Profile
Re: nGL - a fast (enough) 3D engine for the nspire
« Reply #13 on: November 28, 2013, 03:49:37 pm »
O.O This looks pretty epic.

Offline ben_g

  • Hey cool I can set a custom title now :)
  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +125/-4
  • Asm noob
    • View Profile
    • Our programmer's team: GameCommandoSquad
Re: nGL - a fast (enough) 3D engine for the nspire
« Reply #14 on: November 28, 2013, 03:56:05 pm »
This looks nice. It makes me want to have an nspire.

Can it handle large triangles as well as smaller ones?
My projects
 - The Lost Survivors (Unreal Engine) ACTIVE [GameCommandoSquad main project]
 - Oxo, with single-calc multiplayer and AI (axe) RELEASED (screenshot) (topic)
 - An android version of oxo (java)  ACTIVE
 - A 3D collision detection library (axe) RELEASED! (topic)(screenshot)(more recent screenshot)(screenshot of it being used in a tilemapper)
Spoiler For inactive:
- A first person shooter with a polygon-based 3d engine. (z80, will probably be recoded in axe using GLib) ON HOLD (screenshot)
 - A java MORPG. (pc) DEEP COMA(read more)(screenshot)
 - a minecraft game in axe DEAD (source code available)
 - a 3D racing game (axe) ON HOLD (outdated screenshot of asm version)

This signature was last updated on 20/04/2015 and may be outdated