Page 1 of 3

glOric - a Fast 3D Engine for C and Assembler projects

Posted: Mon Dec 23, 2019 2:02 pm
by jbperin
Hi every body,

My name is Jean-Baptiste.

I'm a developer on glOric project at Oric Software and I want to inform oric'developers that a very nice function was recently pushed on the gloric repository at Oric Software.

It is what could be called an "Hyper Fast 3D Projection Routine" named `glProject`.

This function is of interest for people intending to render 3D scene on Oric in real time context because it computes screen coordinates from 3D coordinates very fast (about 500 cycles per point).

It is not yet totally packaged but here's its SoW:

Given the camera situation (position and orientation) and a screen configuration, this function gives screen 2d-coordinates of up to 64 projected 3d points.

Code: Select all

// Camera Position
extern int CamPosX;
extern int CamPosY;
extern int CamPosZ;

// Camera Orientation
extern char CamRotZ;
extern char CamRotX;

void glProject (char points2D[], char points3D[], unsigned char nbPoints);

As input of the function, array `points3D` contains list of `nbPoints` 3D points stored on 4 bytes: 3 bytes for X, Y and Z coordinates and a spare bytes for futur use.

As output of function, array `points2D` if filled with `nbPoints` 2D points stored on 4 bytes: 2 bytes for L and C (line and column coordinates) and 2 bytes for distance (between camera and point).

The 2d-coordinates thus obtained can either be used to simply display points on screen, draw lines and faces between them, or be used as base coordinates for drawing tiles.

For exemple, here's a piece of code showing how to project a 3D cube:

Code: Select all

#include <glOric.h>

#define CUBE_SIZE	4
#define NB_POINTS_CUBE		8

const char ptsCube3D[]={
	 -CUBE_SIZE, -CUBE_SIZE, +CUBE_SIZE, 0 // P0
	,-CUBE_SIZE, -CUBE_SIZE, -CUBE_SIZE, 0 // P1
	,+CUBE_SIZE, -CUBE_SIZE, -CUBE_SIZE, 0 // P2
	,+CUBE_SIZE, -CUBE_SIZE, +CUBE_SIZE, 0 // P3
	,-CUBE_SIZE, +CUBE_SIZE, +CUBE_SIZE, 0 // P4
	,-CUBE_SIZE, +CUBE_SIZE, -CUBE_SIZE, 0 // P5
	,+CUBE_SIZE, +CUBE_SIZE, -CUBE_SIZE, 0 // P6
	,+CUBE_SIZE, +CUBE_SIZE, +CUBE_SIZE, 0 // P7
};
char ptsCube2D [NB_POINTS_CUBE	* SIZEOF_2DPOINT];

glProject (points2D, points3D, NB_POINTS_CUBE);
after the call to `glProject` array `points2D` contains 2D coordinates of points in `points3D`

Provided you have a function `drawLine (X1, Y1, X2, Y2)` that draws a segment between points [X1, Y1] and [X2, Y2], it is possible to draw the cube with following instructions:

Code: Select all

void lineBetweenPoints (idxPt1, idxPt2 ){
  drawLine (
	// L , C Point 1
    ptsCube2D [idxPt1*SIZEOF_2DPOINT + 0],
    ptsCube2D [idxPt1*SIZEOF_2DPOINT + 1],
	// L , C Point 2
    ptsCube2D [idxPt2*SIZEOF_2DPOINT + 0],
    ptsCube2D [idxPt2*SIZEOF_2DPOINT + 1]
	);
}

lineBetweenPoints (0, 1);
lineBetweenPoints (1, 2);
lineBetweenPoints (2, 3);
lineBetweenPoints (3, 0);
lineBetweenPoints (4, 5);
lineBetweenPoints (5, 6);
lineBetweenPoints (6, 7);
lineBetweenPoints (7, 4);
lineBetweenPoints (0, 4);
lineBetweenPoints (1, 5);
lineBetweenPoints (2, 6);
lineBetweenPoints (3, 7);
```
The code available on glOric repository is under development, but two little demo are provided on this thread.


Image

Please share with me your impressions, advices, recommandations, comments ...

Happy Christmas to you All ...

Re: glOric - a Fast 3D Engine for C and Assembler projects

Posted: Wed Dec 25, 2019 11:20 pm
by jbperin
The glProject function is in the glOric.s file in the glOric source directory.
And it is usable from C code as described in this Quick Introduction to glProject.

For more examples on how to use this routine, look at the main.c file.

Re: glOric - a Fast 3D Engine for C and Assembler projects

Posted: Thu Feb 27, 2020 4:54 pm
by jbperin
It is still under development at Oric Software Studios but it may soon be possible to have this kind of render by using glOric:

Image

BEWARE : Video recorded with OVERCLOCK *2 ..

For now the code is being actively profiled and some C part requires to be ported to assembly in order to reach this kind of performance.
DBug's profiler, described here, reveals that some severe optimisations remain to be done:

Code: Select all

Frame:0004 Time=079C08
0x01 079B9D Global
1x01 052F83 glDrawFaces
2x01 00F43F glDrawSegments
3x01 000086 glDrawParticules
4x01 00357C glProjectArrays
5x01 002707 initScreenBuffers
6x01 010682 buffer2screen
7x00 000000 

Re: glOric - a Fast 3D Engine for C and Assembler projects

Posted: Thu Feb 27, 2020 8:37 pm
by Symoon
Looking good! (on a small screen at least!)
Interesting and impressive.

Re: glOric - a Fast 3D Engine for C and Assembler projects

Posted: Thu Feb 27, 2020 10:19 pm
by Chema
It looks really great. I was toying with the idea of using text mode for some simple raycasting engine, but never actually did anything.

Great work!

Re: glOric - a Fast 3D Engine for C and Assembler projects

Posted: Thu Feb 27, 2020 10:29 pm
by kenneth
Nice result ! 8)

Re: glOric - a Fast 3D Engine for C and Assembler projects

Posted: Fri Feb 28, 2020 10:47 am
by ibisum
Very nice! Culling in the demo was very nice to see.

I imagine with a slightly tweaked CHARSET and maybe using only a quarter of the screen, some pretty performant effects could be had. BATTLEZONE Oric sure seems incoming..

Re: glOric - a Fast 3D Engine for C and Assembler projects

Posted: Fri Feb 28, 2020 7:04 pm
by jbperin
Thank you all for your feedback.

I attach a TAP file of the stuff for those who want to gives a try on real screen.

BE CAREFUL : Think of setting Overclock to at least 2

2 functions was translated to assembly for a 25 000 cycles gain.
But still far from enough :?

Code: Select all

0x01 07377A Global
1x01 04CC08 glDrawFaces
2x01 00F431 glDrawSegments
3x01 000087 glDrawParticules
4x01 003579 glProjectArrays
5x01 002707 initScreenBuffers
6x01 0107C2 buffer2screen
If someone is able to test on a real Oric and tell me if it works .. I would be very grateful

Re: glOric - a Fast 3D Engine for C and Assembler projects

Posted: Fri Feb 28, 2020 8:04 pm
by Chema
Hi! I have just tested it in my Atmos. It looks really nice. Sure it is a bit slow, but not as much as I'd expected.

I notice two things, though. One is that sometimes you are setting a 60Hz attribute somewhere, which is well noticed in my TV as the picture gets higher and makes a small "jump". The second is that the program halted after some seconds running.
20200228_195210.jpg
20200228_195210.jpg (590.31 KiB) Viewed 11831 times
Great work!!!

Re: glOric - a Fast 3D Engine for C and Assembler projects

Posted: Fri Feb 28, 2020 8:41 pm
by jbperin
Chema wrote: Fri Feb 28, 2020 8:04 pm Hi! I have just tested it in my Atmos.
WAOU !! It's the first time in my life that my code runs on a real machine !! Thank you very much !!
I bought an Oric Atmos when I started programming on Oric (4 months ago) but i still haven't managed to put my code in it.
So that's the very first time my code runs on a real machine. :D
Chema wrote: Fri Feb 28, 2020 8:04 pm I notice two things, though. One is that sometimes you are setting a 60Hz attribute somewhere, which is well noticed in my TV as the picture gets higher and makes a small "jump".
Yes .. I noticed the same .. and was wondering if it was a problem that could prevent the program to work on a real machine.
I didn't know it was a problem of 60 Hz attribute. Thanks.
There's also a problem with the last lines of the screen .. as though the top of the tower was relected on the bottom line of the screen.
Chema wrote: Fri Feb 28, 2020 8:04 pm The second is that the program halted after some seconds running.
Once it stops .. it should be possible to navigate into the scene with arrow keys or A/Q/w/x/P/;
It is very slow ... but you should ear the key-pressed sound if you press an arrow key.

Well .. in any case .. thank you so much for this photo !!

Re: glOric - a Fast 3D Engine for C and Assembler projects

Posted: Mon Mar 02, 2020 10:07 pm
by jbperin
After optimisation, the number of cycle is divided by two. So the use of overclocking is no longer required to reach the level of performance as in the animated gif.

Code: Select all

0x01 0314C5 Global
1x01 01BF67 glDrawFaces
2x01 00A911 glDrawSegments
3x01 000086 glDrawParticules
4x01 003583 glProjectArrays
5x01 0026E3 initScreenBuffers
6x01 003F50 buffer2screen
Moreover, the issue about the 60Hz seem to be solved ..

Before the forthcoming official release of glOric, source files of this demo are available in the develop branch of glOric.

and here's an updated TAP file:
PopulateMeCozAmFast.TAP
Accelerated color demo.
(30.51 KiB) Downloaded 325 times
Thanks you all for your feedback.

Re: glOric - a Fast 3D Engine for C and Assembler projects

Posted: Fri Mar 20, 2020 11:56 am
by jbperin
Hi C Oric developpers ,

Please find attached to this post a 3D walkthrough template source code build upon glOric.

The scene is the following one:

Code: Select all

          |----------------                                                   
          |             //|                                                   
          |           //  |                                                   
          |         //    |                                                   
          |       //      |                                                   
 ---------------//        |                                                   
 |              |         |                       *\                          
 |              |         |                     //  \\\                       
 |              |         |                   //       \\                     
 |              |         |                  /           \\                   
 |              |         |                //            /|                   
 |              |         |               /            // |                   
 |              |         |             // \\       ///   |                   
 |              |         |           //     \    //      |          *        
 |              |         |          /        \\//        |         //\\      
 |              |         |         |       *   \        /         / /  \     
 |              |         |         |     //    |      //         / /    \    
 |              |         |         |   //      |    /            / /     \\  
 |              |         |         |  /        |  //            /  /       \ 
 |              |         |         |//         |//             --- /  ------\
 |              |         |         /           /                  /// |      
 |              |         |                                        /   |      
 |              |       //             HOUSE                       |      
 |              |     //                                               |      
 |              |    /                                                 |      
 |              |  //                                                  *      
 |               //                                               PINE            
 -------------- /                                                             
         TOWER                                                                     
But it can be easily be modified and adapted to your needs

Please note that it only requires 600 lines of C code to render this scene.

The TAP file :
3DWALKTHROUGH.tap
(21.56 KiB) Downloaded 334 times
The source package (including glOric v1.1) :
walkthrough.zip
(30.75 KiB) Downloaded 303 times
Image

Feel free to use it in your own production (and please put "glOric by JiBe" in your credits .. I need advertising ;-)


Re: glOric - a Fast 3D Engine for C and Assembler projects

Posted: Sat Mar 21, 2020 9:41 pm
by jbperin
Chema wrote: Fri Feb 28, 2020 8:04 pm The second is that the program halted after some seconds running.
Oups I hadn't met this bug ..
As a matter of fact, I allways do the same routine to check for non regression .. and then I hadn't detect that I had introduced some infinite loop in some circunstances.

I think I solved this issue in the version i released here.

Thank you again for your help and sorry for I did not understand properly you remark.

I want to take advantage of this message to ask you if you could have a look at this source code which fits in less than 600 lines of C and which is the clean code for the scene in the demo. I would love to have your opinion ..

Re: glOric - a Fast 3D Engine for C and Assembler projects

Posted: Mon Mar 30, 2020 11:51 am
by Chema
jbperin wrote: Sat Mar 21, 2020 9:41 pm I want to take advantage of this message to ask you if you could have a look at this source code which fits in less than 600 lines of C and which is the clean code for the scene in the demo. I would love to have your opinion ..
I have taken a look and your code is clean and neat! I am not sure what I could comment on it on the technical side. Dbug is much much better than me spotting things on other people's code :) and all I know about 3D is what I learnt (and also moslty forgot) when developing 1337 :cry:

What I can asssure you is that there is plenty of room for optimization if you take the asm path. Clean code with lots of functions and such things, trends to be fat and slow when compiled for the Oric. When I was developing Pinforic (all in C in the first version) I noticed a huge amount of optimization both in size and speed just by removing the use of parameters, replacing them by global variables (I know, argh!!). But the came Fabrice and started rewriting functions in asm and everything changed... the improvement in speed and size was completely unexpected!

I'd say that there could be a 10 factor in speed and 1/10 in size. If your code is already optimized for speed, maybe a bit less.

To be honest, I just gave the code a quick look, but I promise I will look into it deeper as soon as I can.

Congratulations!

Re: glOric - a Fast 3D Engine for C and Assembler projects

Posted: Mon May 18, 2020 8:06 am
by 8bit-Dude
Very interesting work!
Looking at your code quickly, I imagine that it would run faster if you used look up tables for the math functions.
I have a request by the way: Could you add a function that specifies the area of screen for rendering? (so that an interface can be placed around it)