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

0 Members and 1 Guest are viewing this topic.

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: WIP 3D Engine (Update : Mip Mapping) [v0.03]
« Reply #140 on: October 02, 2008 »
The edge bleeding is where the pixels from the opposite edge of a texture are used in part of the filtering, clamping is used to stop the pixels from the opposite edge being used.

if you have a 16*16 texture and you read from coords 15.3,12.7 then you'd read from 4 texels (15,12) (16,12) (15,13) (16,13)

but the 16 wraps around to 0 and reads from the other side of the texture so you'd really want anything over 15 to still equal 15.


Offline Hezad

  • Sponsor
  • Pentium
  • *******
  • Posts: 613
  • Karma: 44
  • I believe .. in Patrick.
    • View Profile
    • Hezad.com Web hosting
Re: WIP 3D Engine (Update : Mip Mapping) [v0.03]
« Reply #141 on: October 03, 2008 »
okay, got it, thanks :D

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: WIP 3D Engine (Update : Mip Mapping) [v0.03]
« Reply #142 on: October 04, 2008 »
Quote
clamping is used to stop the pixels from the opposite edge being used.
In case your texture is tilable it's totally valid to adress texel from the opposite side when filtering.
In the other case (usually needed for projective textures or lightmaps) I usually made my texture a pixel bigger in each direction, so that the texel (u+1,v+1) is always present and I don't have to handle special cases; or I make sure that the right/bottom-boundary texel will never be addressed by the uv-coords.


Challenge Trophies Won:

Offline Hezad

  • Sponsor
  • Pentium
  • *******
  • Posts: 613
  • Karma: 44
  • I believe .. in Patrick.
    • View Profile
    • Hezad.com Web hosting
Re: WIP 3D Engine (Update : Mip Mapping) [v0.03]
« Reply #143 on: October 07, 2008 »
Update :D

So this is the v0.04 and there are lots of new stuff in there ! Stuff like camera handling, new tore object, etc ... But I let you see the changes list :

Quote
v0.04 Changes List



MAJOR :

- Big Optimization with the Shaders calculations.
  > Shaders are now calculated only on visible triangles

- Big Optimization while computing mipMap level
  > MipMap level is now calculated only once per scanline

- Camera Rotation handling.
  > Camera can now rotate according to Y axis and X axis

- Added Init_Engine Sub.
  > User can now choose desired resolution

- Added GenerateTore Sub.
  > object.GenerateTore(x0,y0,z0,r1,r2,nbfaces)

- material Ambient is modified when Diffuse is modified.
  > Can still modify the ambiant after the diffuse modification.


Minor :

- Camera can now translate in world space or in View space
  > Move_Camera X,Y,Z, SPACE (IN_VIEWPOS or IN_WORLD)

- Fixed a bug with the Z-Buffer handling

- Fixed a bug in the untextured rendering


You can still move with the keyboard arrow keys but Left and right now handle the camera handle.

Filtering is still not done though, and I hope I will have some motivation to code it very soon (I can't stand the moiré patterns appearing on the checkered floor, even with mip-mapping)

You can DL it here : Download


Quote
Todo List :

- real time texture bilinear filtering
- simple obj loader (vertices/faces, UV coords, normals)


Wonderings :
- Implementing phong shading ?
- Is there a way to do fast super sampling or any type of FSAA without Open GL/direct X ?
- Implementing environment mapping ?
« Last Edit: October 07, 2008 by Hezad »

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17412
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
I cant really notice any speed improvement, it's still about the sam as before but.. It has much nicer colours and the ability to be able to spin the camera to look around makes a huge difference to this.

The torus is nifty too :)

Please do keep finding the motivation to improve it more and more :)
Every now and again we have one of these threads, someone will have a go at textures, raytracing or something, this has been one of the best threads of this sort we've had. People will learn a lot from it.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Well done!

Quote
Implementing phong shading ?
It's easy. Instead of calculating lighting per vertex and interpolating the resulting rgb-components, you interpolate the xyz-components of your normal-vector and apply the vertex-lighting per pixel.
But you won't use it because it's just too slow.
Better:
Quote
Implementing environment mapping ?
There are lots of ways to do it (spherical, cubic, dual-parabolic) and even several representations of spherical coordinates.
Do you have specific questions?

Quote
Is there a way to do fast super sampling
Accelerators go the brute-force way: render at higher resolution and afterwards sample down to the original resolution; slow.
Another method is to find the siluette edges and render those as anti-aliased lines; this requires transparency, thus sorting polygons and trouble with the zbuffer.
None of these is really fun.
Challenge Trophies Won:

Offline Hezad

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

Quote
I cant really notice any speed improvement, it's still about the sam as before

lol I gained at least 3 FPS and since it was running at ~9/10 FPS, I really have a gain in speed ^^ But maybe my computer is too old, idk :)

Quote
It has much nicer colours and the ability to be able to spin the camera to look around makes a huge difference to this.

The torus is nifty too Smiley

Please do keep finding the motivation to improve it more and more Smiley
Every now and again we have one of these threads, someone will have a go at textures, raytracing or something, this has been one of the best threads of this sort we've had. People will learn a lot from it.

Thanks :) 'glad you like it, and 'glad it'd be useful in any way. Plus, if people can enjoy the awesome help and advises I got here, it's really cool !


@hellfire
Quote
Well done!
Kiitos :D

Quote
Implementing phong shading ?
[...] you won't use it because it's just too slow.

That's exactly what I wanted to know :) Well, no phong shading then.

Quote
Implementing environment mapping ?
There are lots of ways to do it (spherical, cubic, dual-parabolic) and even several representations of spherical coordinates.
Do you have specific questions?

Well not really, I mainly wanted to know if it'd be useful or if it'd really enhance the graphics :) I thought about using either spherical or cubic mapping and using the provided texture to make a skybox/skysphere besides the environment mapping.


Quote
Is there a way to do fast super sampling
Accelerators go the brute-force way: render at higher resolution and afterwards sample down to the original resolution; slow.
Another method is to find the siluette edges and render those as anti-aliased lines; this requires transparency, thus sorting polygons and trouble with the zbuffer.
None of these is really fun.

Hum got it .. So, am I bounded to see crenelated lines ? or maybe the bilinear filtering (if well coded) will greatly improve the graphics and then make the viewer to forget the crenelated lines ?



Thanks for your replies mates :)

btw, could you say me how many FPS you get when you're looking from some distance to the objects and almost see them all (average) ?

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17412
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
You know what, there is really very little difference here between a scene with over 600 polygons to one with very few.

Always between 9 and 12fps
P4 3.0Ghz, ATI X1650 SE

:)
Shockwave ^ Codigos
Challenge Trophies Won:

Offline stormbringer

  • Time moves by fast, no second chance
  • Amiga 1200
  • ****
  • Posts: 453
  • Karma: 73
    • View Profile
    • www.retro-remakes.net
@Hezad: yeah, Phong is very expensive... but you can fake it a lot. Have a look at these:

http://groups.csail.mit.edu/graphics/pubs/thesis_drdaniel2.pdf
http://igad.nhtv.nl/~bikker/articles/various/tut_lighting.htm
We once had a passion
It all seemed so right
So young and so eager
No end in sight
But now we are prisoners
In our own hearts
Nothing seems real
It's all torn apart

Offline Hezad

  • Sponsor
  • Pentium
  • *******
  • Posts: 613
  • Karma: 44
  • I believe .. in Patrick.
    • View Profile
    • Hezad.com Web hosting
@Shockwave :
oh okay :P Well there must me some calculations done even on clipped triangles.. Or maybe the clipping function must be drastically optimized :)

@Stormbringer :

hey cool :) very interesting links, didn't red all the pdf yet but I'm doing it right now, thanks for pointing me on this !

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Quote
I thought about using [...] cubic mapping
good luck ;D
Challenge Trophies Won:

Offline Hezad

  • Sponsor
  • Pentium
  • *******
  • Posts: 613
  • Karma: 44
  • I believe .. in Patrick.
    • View Profile
    • Hezad.com Web hosting
I feel an ounce of irony ^^

Offline Hezad

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

edit : added another screenshot
« Last Edit: October 09, 2008 by Hezad »

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17412
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Psychedelic :)

Bottom picture looks like the sun is coming up over the scene
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Hezad

  • Sponsor
  • Pentium
  • *******
  • Posts: 613
  • Karma: 44
  • I believe .. in Patrick.
    • View Profile
    • Hezad.com Web hosting
Quote
Bottom picture looks like the sun is coming up over the scene

hum.. now I wanna try some sunset moods :p I'll try env. mapping for that :D


before that, I just tried super sampling ... Well, ok it runs at 2 FPS but wow..the graphics look bloody smooth oO

Offline Hezad

  • Sponsor
  • Pentium
  • *******
  • Posts: 613
  • Karma: 44
  • I believe .. in Patrick.
    • View Profile
    • Hezad.com Web hosting
Update :) I'm working on the 0.05 right now, and here are some news :)

Mainly : Supersampling, HeightMap handling, Fog     :D

still a temp version though

v0.05 Changes List

Quote

MAJOR :

- Added Supersampling !
  > Switch it ON or OFF before Initiating the engine.  (Set_Super_Sampling ON/OFF)
  > Not for Real-time use

- Added Height-map loader.

- Smoothed Mip-map generation.

- Added Linear Fog Handling.

- Added Exponential (density) Fog Handling

- Working on Mouse camera handling


Minor :

- Added a skysphere in the example

- User can use fbgfx flags with the init_engine sub
  > e.g. GFX_FULLSCREEN   

- Fixed a bug in tiling

- Changed textures in the example.



In the zip attached, you'll find the code and a demo (Real_time.exe or Super_Sampled.exe). Warning, the Super_sampled version is incredibly slow. It's definitely not for real time purposes.

You can rotate view with mouse (well .. almost, there's something about the camera rotation I haven't resolved yet :p), move the camera with the UP/DOWN arrow keys.
You can change some options with the function keys.


Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17412
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Props to you for including all three fog modes supported by Opengl :) I guess that it would be easy to alter the fog density too?

Using the terrain to test the engine looks better, it looks very accomplished, the supersampled version ran at 3fps here, which surprisingly was quite useable. In fact it reminded me of flying around in the game "FA18 Interceptor" on the Amiga, sometimes when you were flying around and there were several things on the screen at once, it used to drop to less than 2fps! Still, that game really rocked.

The way that you have your mouse controls set up at the moment it's quite similar to the controls of an aircraft.

Am I right in thinking that all the triangles are both textured and gourad shaded?
Also I saw things like this in the source;

Code: [Select]

Memcpy(Destlist,TmpList,(tempNbV)*Sizeof(VertexT))

Does this mean that you are rendering things in a temporary buffer and then copying them to the framebuffer?

I really am impressed at how good the scenery looks though Hezad, you made a great job of it :)
Shockwave ^ Codigos
Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Really nice!
The "normal" version runs at 18fps while the supersampled one does 3, which is basically unusable ;)
If you have a look at the supersampled image you can see how few pixels actually benefit from it and how "global" supersampling merely wastes power.
The sky-sphere works amazingly well and integrates perfectly into the terrain!
Also the lighting achieves a very natural look.
I noticed you used gouraud shading on the sky, too, which probably doesn't make sense because the texture captures the surrounding (including the light-source) - actually you would set up your light-sources to match the sky-texture and local lights won't reach the sky.
I'm also pretty sure that you can easily optimize your code to do twice the framerate :)
Challenge Trophies Won:

Offline Hezad

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

Quote
all three fog modes supported by Opengl Smiley I guess that it would be easy to alter the fog density too?

An other fog mathod is planned : Perlin Noise Fog (I saw some methods using it on the net and it looks bloody realistic :D) And yes, the fog density is easily alterable (even in real-time if wanted). The fog's density can be changed when setting the fog :

Code: [Select]
Set_Fog METHOD, DENSITY=0

Method : Linear (Density has no effect) / Exponential (you just have to select the desired density)



Quote
Using the terrain to test the engine looks better, it looks very accomplished, the supersampled version ran at 3fps here, which surprisingly was quite useable. In fact it reminded me of flying around in the game "FA18 Interceptor" on the Amiga, sometimes when you were flying around and there were several things on the screen at once, it used to drop to less than 2fps! Still, that game really rocked.

The way that you have your mouse controls set up at the moment it's quite similar to the controls of an aircraft.

Well, in fact the aircraft-style-mouse-control wasn't wanted ^^ 'still working on it :)


Quote
Am I right in thinking that all the triangles are both textured and gourad shaded?

You're right :) But the engine can still handle flat shaded and non shaded polys.

Quote
[...] Does this mean that you are rendering things in a temporary buffer and then copying them to the framebuffer?
Well the line of code you copied is used when clipping : it copies a temporary list of vertices to the one which will be returned. The only "temporary" graphics buffer I use is the one used for super sampling


Quote
I really am impressed at how good the scenery looks though Hezad, you made a great job of it Smiley
Thanks  :cheers:



@hellfire

Quote
The "normal" version runs at 18fps while the supersampled one does 3, which is basically unusable Wink
Sure :D In fact, I start to know what I want with this engine : Since I want to experiment a lot of stuff (even, non real time stuff), I'll work on two versions of the engine : A real-time one, and a CG one, the real-time could be used for games and the CG version could be used for some kind of a CG renderer.


Quote
If you have a look at the supersampled image you can see how few pixels actually benefit from it and how "global" supersampling merely wastes power.

Well here, you're totally right. The super sampling doesn't really affects the rendering quality. But when objects are more "curved" or complex (spheres, tores, future loaded objects :p), the super-sampling brought a huge difference. I'll try to make a test image to compare both versions.


Quote
The sky-sphere works amazingly well and integrates perfectly into the terrain!
Also the lighting achieves a very natural look.
Cool :D 'glad you liked the lighting, I wondered some time to find the good blend between fog color, ambiant color and light color :p


Quote
I noticed you used gouraud shading on the sky, too, which probably doesn't make sense because the texture captures the surrounding (including the light-source)
actually you would set up your light-sources to match the sky-texture and local lights won't reach the sky.
In fact, the sky in not shaded :) wherever the lights are, whatever the number of lights is, the sky will always look the same (normally), excepted when fog changes its apparent color of course :)

Quote
I'm also pretty sure that you can easily optimize your code to do twice the framerate Smiley
That would be bloody awesome ^^ But my coding skills appear really weak to myself when it has to do with optimization. I will do it, but it will take time I guess :)
First, I should use the stuff you wrote about : Using Vertex listings and triangle list instead of 3 vertices per triangle, it should drastically improve the speed, but I'm not very sure about how to convert my code to this yet.



Thanks a lot to both of you, your commentaries always motive me :)

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
You'll gain the most by optimizing those functions which are called most often.
Since the number of pixels to process is by some magnitude larger than the number of vertices, you should start optimizing at your polyfillers, namely the TSLine-Macro.

Just have a look at the code you run for each pixel:  :o
Code: [Select]
        tmp_Z = 1/Cur_Z
       
        If tmp_z < ZBuffer(it) then
           
            ZBuffer(it) = tmp_Z
               
            ValU = (U_z*tmp_Z)
            ValV = (V_z*tmp_Z)
           
            if ValU>=1 then ValU = ValU-int(ValU)
            if ValU<0 then ValU = 0
            if ValV>=1 then ValV = ValV-int(ValV)
            if ValV<0 then ValV = 0
           
            If UseMipmapping = 0 then
               
                CurTex = Texture.GFX[int(ValU*Texture.SizeX)+Texture.SizeX*int(ValV*Texture.SizeY)]
               
                Cb = (CurTex and 255)
                Cg = ((CurTex shr 8) and 255)
                Cr = ((CurTex shr 16) and 255)
               
            else
               
                LODVal = iif(mipU>mipV,MipU,mipV)
               
                If LODVal>Texture.NbLOD then LODVal = Texture.NbLod
                If LodVal<0 then LODVal = 0
               
                If LODVal = 0 then
                       
                    CurTex = Texture.GFX[int(ValU*Texture.SizeX)+Texture.SizeX*int(ValV*Texture.SizeY)]
                   
                    Cb = CurTex and 255
                    Cg = (CurTex shr 8) and 255
                    Cr = (CurTex shr 16) and 255
                   
                else
                     
                    CurTex = Texture.MipMap(LODVal)[int(ValU*Texture.MipMapX(LODVal))+Texture.MipMapX(LODVal)*int(ValV*Texture.MipMapY(LODVal))]
                   
                    Cb = CurTex and 255
                    Cg = (CurTex shr 8) and 255
                    Cr = (CurTex shr 16) and 255
                   
                end if
               
            end if
               
            Cb *= b*0.003921  '' 1/255
            Cg *= g*0.003921
            Cr *= r*0.003921
           
            Select Case Render_Option_Val
            Case RENDER_NO_OPTION
               
                if Use_Fog then
                    select case fog_method
                    case LINEAR
                       
                        dim as single blendfunc = (MAX_Z-tmp_Z)/(MAX_Z-1)
                       
                        Cr = blendfunc * Cr + (1-blendfunc) * Fog_Color.r
                        Cg = blendfunc * Cg + (1-blendfunc) * Fog_Color.g
                        Cb = blendfunc * Cb + (1-blendfunc) * Fog_Color.b
                       
                    case EXPONENTIAL
                       
                        dim as single blendfunc = exp(-(tmp_Z/MAX_Z) * fog_Density)
                       
                        Cr = blendfunc * Cr + (1-blendfunc) * Fog_Color.r
                        Cg = blendfunc * Cg + (1-blendfunc) * Fog_Color.g
                        Cb = blendfunc * Cb + (1-blendfunc) * Fog_Color.b
                       
                    case HETEROGENOUS
                        '' placeholder
                    end select
                end if
               
                ScrPtr[it] = rgb(Cr,Cg,Cb)
               
            Case RENDER_MIPMAP
                dim as integer ldv = LODVal*127/Texture.NbLOD
                ScrPtr[it] = rgb(255-2*Ldv,255-ldv,255-ldv)
            Case RENDER_ZBUFFER
                dim as integer zc = tmp_Z*255/MAX_Z
                ScrPtr[it] = rgb(zc,zc,zc)
            end Select
           
        end if
       
        it+=1
           
        U_z += dU_z
        V_z += dV_z
           
        Cur_Z += d_Z
           
        r += dR
        g += dG
        b += dB
First of all you shouldn't be checking render-settings on a per-pixel basis, but do an optimized loop for each of them.
Secondly you can store the interpolated 1/z in your zbuffer (reasonably scaled to fill an integer) so you don't have to do a divsion even if the zbuffer-test fails.
Remove all floating-point-operations when dealing with colours.
Then have a look what I already wrote about subdividing your scanlines and interpolating linearly across these segments.
When that's done, your framerate has atleast doubled.
Challenge Trophies Won: