Author Topic: WIP 3D Engine (Update : v0.07 screenie preview) [v0.06]  (Read 108819 times)

0 Members and 1 Guest are viewing this topic.

Offline Hezad

  • Sponsor
  • Pentium
  • *******
  • Posts: 613
  • Karma: 44
  • I believe .. in Patrick.
    • View Profile
    • Hezad.com Web hosting
Heya :) Thanks again for all this ! I already implemented the Material list and I am wondering about the z_buffer change :

I tried and I'm not sure to notice a change in speed, and it brings me to a question : Since the pointer points to an integer, when I do

Code: [Select]
*zbuff = zbuffer(it)*2147483647.0
*zval = Cur_Z*2147483647.0
       
If *zbuff > *zval then
[...]
,

There's still a single>integer conversion, no ? Maybe I shouldn't multiply by 2147483647.0 before, but if it's the case, I must admit there's something I didn't understand :S
Plus, I must allocate and deallocate the pointer, which makes me loose some cycles again  ???


Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
It's much simpler.
You are not doing any conversion and keep everything in floating point:
Code: [Select]
redim as single zBuffer(SCREEN_MEM)
dim as single Cur_Z, d_Z     ' the interpolated z-value and delta

Now you get pointers to both:
Code: [Select]
dim as integer ptr izbuf= @zbuffer(0)
dim as integer ptr izval= @Cur_Z

When addressing a value with the integer pointer, this is handled by the cpu.
All you have to do is replace the compare:
Code: [Select]
for i as integer = 1 to size
'  if Cur_Z > ZBuffer(it) then  ' old: the fpu handles the fairly slow single-compare
  if *izval > izbuf[it] then    ' new: the compiler thinks these values are integers and lets the cpu do the compare
    ...
  endif
  it+=1
  Cur_Z+=d_Z
  ...           
wend
This works for positive singles only.
Made me go from 21 to 23 fps (initial view, 1002 triangles).

Alternatively you multiply all "1/z" and the corresponding deltas with a fairly big number and make them (and your zbuffer) integer.
« Last Edit: November 10, 2008 by hellfire »
Challenge Trophies Won:

Offline Hezad

  • Sponsor
  • Pentium
  • *******
  • Posts: 613
  • Karma: 44
  • I believe .. in Patrick.
    • View Profile
    • Hezad.com Web hosting
ah ! Okay I get It now :) It works perfectly :D (excepted I have some "Suspicious pointer assignement" warnings)

Well I'm on all those optimizations then :)

edit : the v0.06 will also have a false phong shader :D (without a big FPS loss)
« Last Edit: November 11, 2008 by Hezad »

Offline benny!

  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4384
  • Karma: 228
  • in this place forever!
    • View Profile
    • bennyschuetz.com - mycroBlog
Unbelievalble ... project is very cool.

Just out of curiosity .. current version is v0.06.
What is the roadmap to V1.0 ???
[ mycroBLOG - POUET :: whatever keeps us longing - for another breath of air - is getting rare ]

Challenge Trophies Won:

Offline Hezad

  • Sponsor
  • Pentium
  • *******
  • Posts: 613
  • Karma: 44
  • I believe .. in Patrick.
    • View Profile
    • Hezad.com Web hosting
heya benny :) thanks mate !

Well, my objective for the v1.0 is to have a working, optimized, complete engine, easily usable and modifiable by anyone (with two modes : RT for realtime rendering (games ?) and CG for Computer graphics rendering (supersampling, maybe true phong, why not some reflections, refractions, env. map, Ambiant Occlusion, ...)) and all this, without any bug or flaw .. Of course it can take a veeery long time for those versions to be released ^^ Useless to say there's a huge probability we'll never see those versions :xmas:

However, The main roadmap for the next versions consists in Optimization, OBJ Loader and texture filtering (yeah ...  still have to code it lol)

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17422
  • Karma: 499
  • evil/good
    • View Profile
    • My Homepage
The image of the fake phong looks fabulous, nice one!

It sounds like you still have a lot of work planned for it too... I bet you never planned on doing all this when you started :)
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Hezad

  • Sponsor
  • Pentium
  • *******
  • Posts: 613
  • Karma: 44
  • I believe .. in Patrick.
    • View Profile
    • Hezad.com Web hosting
thanks shocky :D

Quote
I bet you never planned on doing all this when you started Smiley

uhuh totally ^^ When I started, I guess I just wanted to code some texture mapped stuff, maybe little intro with it and nothing more ..  ;D

Offline Hotshot

  • DBF Aficionado
  • ******
  • Posts: 2114
  • Karma: 91
    • View Profile
Very nice and I love those Checkboard  8) 8)

Will there be any shadow reflections to the checkboard? i.e. Ball shadow that relections to the checkboard :) :)
« Last Edit: November 11, 2008 by Hotshot »

Offline Hezad

  • Sponsor
  • Pentium
  • *******
  • Posts: 613
  • Karma: 44
  • I believe .. in Patrick.
    • View Profile
    • Hezad.com Web hosting
thanks :)

Shadows/Reflections/Refraction .. I'd like to implement them :) But I don't think it'll be real-time .. We'll see :)

A little screen of an improvement of the fake phong shading (highlights follow your view)

« Last Edit: November 11, 2008 by Hezad »

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Because the "highlight" is a reflection, it doesn't interact with the texture-color (add instead of multiply).
For all the round stuff, environment mapping probably works better and gives some amount of "reflections" for free (compare here).

Shadows and reflections were very well managable in realtime over a decade ago, just have a look at this or that - refraction is a little bit trickier, though.
« Last Edit: November 12, 2008 by hellfire »
Challenge Trophies Won:

Offline Hezad

  • Sponsor
  • Pentium
  • *******
  • Posts: 613
  • Karma: 44
  • I believe .. in Patrick.
    • View Profile
    • Hezad.com Web hosting
Quote
Because the "highlight" is a reflection, it doesn't interact with the texture-color (add instead of multiply).

Yeah, but I didn't find how to change it for now (since highlight is calculated per vertex and directly fed in the (Blinn I guess) lighting equation )

Quote
For all the round stuff, environment mapping probably works better and gives some amount of "reflections" for free (compare here).

That .. is .. bloody .. awesome o___O Yeah, I'd like to add some environment mapping, but the bump mapping does a huge work too in "awesomizing" this torus :D


Quote
Shadows and reflections were very well managable in realtime over a decade ago, just have a look at this or that - refraction is a little bit trickier, though.

Both didn't run on my PC :S




The v0.06 is almost ready :D Now I'm just wondering if I must release it and add next optimizations/changes in the v0.07 or wait for those changes to be done ..

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17422
  • Karma: 499
  • evil/good
    • View Profile
    • My Homepage
Release it! :)
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Hezad

  • Sponsor
  • Pentium
  • *******
  • Posts: 613
  • Karma: 44
  • I believe .. in Patrick.
    • View Profile
    • Hezad.com Web hosting
uhuh I'm glad to see your enthusiasm ^^ Okay, I'll release it this evening, when I'll be back home  :)

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Quote
I didn't find how to change it for now (since highlight is calculated per vertex and directly fed in the lighting equation)
Usually diffuse and specular components are interpolated individually across the triangle.
Since the specular highlight (with large exponent) covers only a few triangles, you can either draw those in a separate pass (with additive transparency) or call a different polyfiller if one of the vertices has specular>EPS.

Quote
That .. is .. bloody .. awesome o___O
thanks :)
Notice the two profiling counters (measured in million-cpu-cycles)
The first shows the time required for vertex-transformation & clipping, the second for filling the polygons.
This way it's easy to find good places to start optimization.

Quote
Both didn't run on my PC :S
You can try dosbox.
« Last Edit: November 13, 2008 by hellfire »
Challenge Trophies Won:

Offline Hezad

  • Sponsor
  • Pentium
  • *******
  • Posts: 613
  • Karma: 44
  • I believe .. in Patrick.
    • View Profile
    • Hezad.com Web hosting
Heya :)

Quote
Usually diffuse and specular components are interpolated individually across the triangle.
Since the specular highlight (with large exponent) covers only a few triangles, you can either draw those in a separate pass (with additive transparency) or call a different polyfiller if one of the vertices has specular>EPS.
oh yeah okay, I'll change that in the next release, thanks :)

Quote
You can try dosbox.
ah yep, didn't think about it ^^




Well, as promised, here is the v0.06 :D Lots of stuff changed : new example files, faster, a new fake phong shader, ...


 - Download Rar(Src + bin + tex + mipmaps) -

Sorry, too big to post it here :S (Next time, I'll suppress the mipmaps to gain some space, the engine generates them itself)

Quote
v0.06 Changes List



MAJOR :

 - Camera is totally handled by matrices (movement + rotation).

 - Huge Render Optimization (Won almost 10 FPS)

 - NEW SHADER :D => FAKE_PHONG_SHADER : lighting is more realistic



Minor :

 - replaced Materials copy by a List of Materials

 - Different optimizations (GenerateSphere(), Handle_Super_Sampling, ZBuffer, ..)

 - Added Generate_Klein_Bottle Sub

 - New Material property : Mat_No_Culling : Will render this material without backface culling (useful to render open shapes/surfaces)

 - New Examples



Known bugs :

 - Objects with Opacity <> 1 must be projected at last.

 - There's a problem with the Klein Bottle normals

 - PreCalc_Object_Normals() sub anormaly slow

Attached *.rar :)

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Really nice!
The camera-handling works really well now and even the supersampled versions have "interactive framerates" :)
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17422
  • Karma: 499
  • evil/good
    • View Profile
    • My Homepage
Definately a huge improvement in framerate Hezad, this is quite a leap ahead from the last version you made :)

My favourite part of your engine is the way that you have implimented the fog. It's excellent and looks exactly the same as the standard opengl implimentations of it.

I know that I said abou the fog looking great before, but it really does look good with all the nice rendermodes now.

Fake phong is fast as well. 
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Hezad

  • Sponsor
  • Pentium
  • *******
  • Posts: 613
  • Karma: 44
  • I believe .. in Patrick.
    • View Profile
    • Hezad.com Web hosting
thanks a lot mates :)

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
I noticed that all triangles have a flag "IsInverted" which is used to flip orientation and causes special-case-handling at many places.
If you want to flip a triangle which is made up by the vertices v1,v2,v3, you can simply change the order of the vertices, for example v1,v3,v2.

In Culling_Visible you don't need to normalize viewpos and viewposnormal.
You can also simplify the computation for ViewPosNormal:
Code: [Select]
ViewPosNormal.x = Tri.Vertex(1).Viewpos.y*Tri.Vertex(2).Viewpos.z - Tri.Vertex(1).Viewpos.z*Tri.Vertex(2).Viewpos.y
ViewPosNormal.y = Tri.Vertex(1).Viewpos.z*Tri.Vertex(2).Viewpos.x - Tri.Vertex(1).Viewpos.x*Tri.Vertex(2).Viewpos.z
ViewPosNormal.z = Tri.Vertex(1).Viewpos.x*Tri.Vertex(2).Viewpos.y - Tri.Vertex(1).Viewpos.y*Tri.Vertex(2).Viewpos.x
A better way is to test culling in object-space, but I guess that's a bit too far out at the moment.

Objects with higher tesselation won't benefit much from perspective interpolation.
For rather small triangles (and those with small delta-z) you can use linear interpolation.

Try to separate your vertex-data from the triangles. At the moment you're still transforming 6 times as many vertices as would be required.
Also try to do as few transformations on your vertices as absolutely necessary.
Actually there's no need to touch vertices in Rotate_Object.
Just build a rotation-matrix, multiply it with the camera-matrix and do the complete transform (with a single matrix) in RotateViewPos (already mentioned here).
When calculating lighting (where you require .pos3d) you can transform the lights into camera-space (or object-space).

You can generally speed up vector-normalization (extensively used in the lighting-calculation) with the method described here.
Since the specular exponent is constant, you can find an lower bound for (N*H) so that (N*H)^e<EPS.

You're now storing an id of the material in your triangle-structure. It's more efficient to do it the other way round:
In your material-structure store a list of pointers to all meshes which got this material applied.
This way you save the (rather complex) test which material is currently active for every triangle.

You might consider upgrading to fbc 0.2 and use command-line parameter "-fpu sse" to gain ~5fps.

The more I look at freebasic's asm-output, the more I think this will become a good opportunity for you to learn assembler ;)
« Last Edit: November 14, 2008 by hellfire »
Challenge Trophies Won:

Offline Hezad

  • Sponsor
  • Pentium
  • *******
  • Posts: 613
  • Karma: 44
  • I believe .. in Patrick.
    • View Profile
    • Hezad.com Web hosting
well, thanks (again) for those advises :) I'll think about each of them.

Concerning learning Assembler .. I'd like to, but I won't have time lol It will also be more and more difficult for me to upgrade this engine (I found a job lol, and I have lots of projects aside).

But of course, I stay in the course :p I'll continue to work on it but releases will be even more spread in time :S


@hellfire : a question : If I turn on the fpu option when compiling the examples, will they run on most/every machines ?


@shockie : by the way, thanks for the fog :) I red a really useful (and bloody interresting) paper on the subject, you can find it here :
http://www.cescg.org/CESCG-2004/web/Zdrojewska-Dorota/
« Last Edit: November 14, 2008 by Hezad »