Main Menu

Building a basic 3D Engine

Started by stevmjon, December 28, 2016, 03:43:19 AM

Previous topic - Next topic

kevin



Steve,

     dragged some super old code out and made a version  for PB that supports Z buffered rendering at least..  if nothing more it might be handy as reference.  Actually runs OK in PBV1.65, but we are talking about plotting pixels here so it'll never be extreme in plain PB.. 


stevmjon

cool kev, i would like to see the code for reference.

i have looked at a few videos about this, and it seems you need to be able to code in parallel for the graphics card, for this to work effectively, so that several pixels are calculated at the same time. has playbasic got commands to do this?

   thanks, stevmjon
It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.

kevin

#17
Quote
i have looked at a few videos about this, and it seems you need to be able to code in parallel for the graphics card, for this to work effectively, so that several pixels are calculated at the same time. has playbasic got commands to do this?

 They're most likely talkings about shaders..  Old hardware renders are fixed function pipeline,  which is what it sounds like, the old device has a chunk of logic to render a triangle (or a DOT/LINE etc etc or each surface stype).   The issue with those is the more effects they need to jam in the bigger foot print of the GPU..

  So they move to the shaders about DX9..  These are user defined code code fragments that run on the GPU.  They used to be very limited in terms of size and speed..  but today high end GPU's are like like a rats nest of shader units..  So  shader code runs in parallel across as many many shader units at time as possible. So rather doing one texel at time they can do many.

 You could do shader via OpenGL ..    In other words do all your scene and just render meshes...  Which is how GL apps work anyway..  


  G2D - 2D OpenGL library for PlayBASIC V1.64P (login required)

 

kevin

#18
 Z buffered gouraud shading in PB V1.65 runtime  :  get Source Code


stevmjon

#19
that looks good kev.

can you post the code?

just a question, i am not too sure about part 3 in my pic. it seems you need to squish the frustum down the z to form a cube. that i understand, but the z values are too high and bunched up(0.8 to 1.0). only the points very close to the camera get lower values. not sure if you are supposed to center part 2 in the 3D world then do part 3?

NOTE: the formulas in the pic are only there as reference. i don't expect you to calculate them (i know you get busy). just wondering if my theory is correct.

thanks,  stevmjon






It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.

kevin


  The code was posted to the source code board ..  :)

kevin

#21

Here's some OLD examples you might find useful,


  - Direct X (ASCII) loader
  - ASC II 3D Object Loader / Viewer V0.02



stevmjon

thanks kev, you read my mind.
i was thinking about loading .obj files in soon. i can make them with 3D software i have(Lightwave 3D).

i am tinkering with the UV maps at the moment. i have re-made my clipping routines with a more efficient one(i am clipping in two locations), and i am just getting the UV maps to match the clip, then i will subdivide the closer polygons for a better looking texture. i am also reading up on perspective correct texture routines too.

i hope to release another update very soon.

stevmjon

It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.

kevin

#23
 Here's a site with some 3D game model rips

https://www.models-resource.com/


something like StarFox64 could be fun

https://www.models-resource.com/nintendo_64/starfox64/


Could prolly do somthing like the SNES version at least.







stevmjon

#24
hello

   about time i put up another update. this one has all polygons textured, and i have modified the clipping routine to clip in 2 locations in the game engine.

1> clip early in the camera space by clipping everything behind the near border (this gets rid of everything behind the camera).
2> then clip later in the perspective space (frustum transformed into a cube) by clipping on the remaining borders because here is a faster clipping operation.

   the reason for this split gives the advantage of 1 clip done early in the engine with 1 efficient border check, and the remainder of the clips done in the more efficient perspective cube stage, preventing the need to interrupt the matrix calculations at this perspective stage if all borders were checked here.
    this way all the clips are done on frustum borders that are perpendicular to an axis, so eliminating the need for dot product calculations in the border checks. all i need to do is simply check the position of a point/vector and compare this with the border location. eg. is X<-1(left border) , is X>1(right border) etc. even the camera space near border is perpendicular to the z axis so is just a location check too(eg. is Z<0.1(near border).

i have applied 1 formula for all polygon clipping routines regardless of where in the game engine, and also applied this to the clipped UV texture too. the UV's seems to work well with clipped quad polygons, but i don't think the UV is completely accurate with clipped tri polygons(mainly on the inside edge). i may have play around with this a little more.
i want to work out this UV clip before i move on to subdividing poly's etc.

Version 0.5.2

   enjoy this early update, stevmjon

It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.

stevmjon

here is a pic for the above post. it wouldn't let me post both the image and the pic, saying over maximum allowed size. i thought each attachment was up to 3,500k each? it must be a total of 3,500k for all attachments combined size?

pic for above post v0.5.2

It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.

stevmjon

yay, another update.

this update has subdivided polygons and subdivided textures, depending on where the polygon is located from the camera.

> polygons that are clipped, get the texture subdivided first, then clipped to give smoother textures.
> polygons that are whole, but very close to the camera, get the polygon subdivided to give smoother textures.
> polygons that are whole, but a little further away from the camera get the texture subdivided the give smoother textures.

so now when you look around the world, the polygon textures look smooth, especially beneath the camera.
this took me quite some time to experiment with and get right. it is still not perfect, but looks better anyway.

this one can be called the experimental update:
* press " i " key  =  change the texture (total 4)
* press " L " key  =  show sub-divide / clipping mode (total 6)
* press " R Arrow " key  =  turn ON outline all polygons _ press "L Arrow" key = turn OFF outline

there are plenty of other key presses you can do by looking at the text on the bottom of the screen for the letter in brackets next to the name.
well, i hope you like this, it took a while to do with all the experimenting, so the code is pretty big. i will probably reduce this down, now i know the technique i like (subdivide mode = 6).
i did a lot of copy / paste then edit of functions, and spent a lot of time in debug mode. it would probably have been quicker to re-write code instead.

NOTE: the only bug i can obviously see is small gaps between the border of subdivided polygons next to whole polygons.

the next update i will work on a render method.

Version 0.6.1

   enjoy, stevmjon

It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.

kevin


stevmjon

howdy

in this update i have added a light source (sun) so the textures will change depending on the angle they are from the sun.
so, it's not incidence angle from the camera to sun glancing off the object surface, but it still looks cool anyway.

also added merge sort method for the draw order of polygons (suits artists render method). it is not accurate when polygons are very close, or overlap.
still, i had fun calculating this. i may tweak this a little more though.

NOTE: the only bug i can obviously see is small gaps between the border of subdivided polygons next to whole polygons.

Version 0.6.2

   enjoy, stevmjon
It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.

stevmjon

hello

update time again.
> added 3D textures (manually drawn).

the textures look great, with all lines in the texture nice and straight, finally.
since each pixel is manually calculated for 3D, it runs a lot slower.
i should write new code to make it more optimised, as there is now 12 different draw modes. it's what you get when your learning and experimenting at the same time.

next i would like to work on loading objects into the engine, and also adding collision for the player (so you can't walk through walls etc).

Version 0.7.1

   enjoy, stevmjon
It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.