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

Since we do not have native C compilers on the Oric, this forum will be mostly be used by people using CC65 or the OSDK. But any general C related post will be welcome !
User avatar
jbperin
Flight Lieutenant
Posts: 480
Joined: Wed Nov 06, 2019 11:00 am
Location: Valence, France

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

Post 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 ...
User avatar
jbperin
Flight Lieutenant
Posts: 480
Joined: Wed Nov 06, 2019 11:00 am
Location: Valence, France

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

Post 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.
User avatar
jbperin
Flight Lieutenant
Posts: 480
Joined: Wed Nov 06, 2019 11:00 am
Location: Valence, France

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

Post 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 
User avatar
Symoon
Archivist
Posts: 2301
Joined: Sat Jan 14, 2006 12:44 am
Location: Paris, France

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

Post by Symoon »

Looking good! (on a small screen at least!)
Interesting and impressive.
User avatar
Chema
Game master
Posts: 3013
Joined: Tue Jan 17, 2006 10:55 am
Location: Gijón, SPAIN
Contact:

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

Post 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!
User avatar
kenneth
Squad Leader
Posts: 514
Joined: Fri Nov 26, 2010 9:11 pm
Location: France PdD
Contact:

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

Post by kenneth »

Nice result ! 8)
User avatar
ibisum
Wing Commander
Posts: 1643
Joined: Fri Apr 03, 2009 8:56 am
Location: Vienna, Austria
Contact:

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

Post 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..
User avatar
jbperin
Flight Lieutenant
Posts: 480
Joined: Wed Nov 06, 2019 11:00 am
Location: Valence, France

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

Post 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
Attachments
PopulateMe.TAP
glimpse of glOric with colors
(34.49 KiB) Downloaded 295 times
User avatar
Chema
Game master
Posts: 3013
Joined: Tue Jan 17, 2006 10:55 am
Location: Gijón, SPAIN
Contact:

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

Post 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 11738 times
Great work!!!
User avatar
jbperin
Flight Lieutenant
Posts: 480
Joined: Wed Nov 06, 2019 11:00 am
Location: Valence, France

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

Post 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 !!
User avatar
jbperin
Flight Lieutenant
Posts: 480
Joined: Wed Nov 06, 2019 11:00 am
Location: Valence, France

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

Post 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 301 times
Thanks you all for your feedback.
User avatar
jbperin
Flight Lieutenant
Posts: 480
Joined: Wed Nov 06, 2019 11:00 am
Location: Valence, France

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

Post 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 318 times
The source package (including glOric v1.1) :
walkthrough.zip
(30.75 KiB) Downloaded 291 times
Image

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

User avatar
jbperin
Flight Lieutenant
Posts: 480
Joined: Wed Nov 06, 2019 11:00 am
Location: Valence, France

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

Post 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 ..
User avatar
Chema
Game master
Posts: 3013
Joined: Tue Jan 17, 2006 10:55 am
Location: Gijón, SPAIN
Contact:

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

Post 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!
User avatar
8bit-Dude
Flying Officer
Posts: 141
Joined: Tue Mar 14, 2017 1:33 pm
Location: Japan

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

Post 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)
Post Reply