News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

3D Gouraud Shaded Torus (software rendering)

Started by kevin, March 14, 2022, 10:30:25 PM

Previous topic - Next topic

kevin

   3D Gouraud Shaded Torus  - 14th March 2022

    This example rendering  spinning gouraud shaded torus in 3D, purely in software.  The code is translation of an old Blitz BASIC program that was converted and tweaked to run in PlayBASIC.


   Note:  Download attachment for the FULL code it's too big for a snippet


 




 
PlayBASIC Code: [Select]
; PROJECT : 3d_v2_Torus_PB_Version
; AUTHOR : Paul Rene J?rgensen & Kev Picone
; CREATED : 12/03/2022
; EDITED : 14/03/2022
; ---------------------------------------------------------------------

;--------------------------------------------------------------------------------
#include "BlitzWrapper.pba"
;--------------------------------------------------------------------------------


; 3d
;
; Author : Paul Rene J?rgensen, <>
; Last update : 20. February, 2001
;
; 9 Muls rotator, Mergesort, Gourad shader,
; Backface-culling, Vector based lightsource


Global numvertex,numpoly
Global width=640
Global height=480

BB_Graphics width,height,32,2
BB_AppTitle("Gourad shading")

BB_SetBuffer BB_BackBuffer()

Dim xpos(height,2)
Dim zpos(height,2)
Dim rpos(height,2)
Dim gpos(height,2)
Dim bpos(height,2)

Type tlightsource
x,y,z
EndType

Type tvertex
x,y,z
xr,yr,zr
nx,ny,nz
nxr,nyr,nzr
x2d,y2d
EndType

Type tpoly
v0,v1,v2,v3
order
EndType

Restore MyObject
numvertex = Readdata()
numpoly =ReadData()

Dim zcenter(numpoly)
Dim zworking(numpoly)
Dim ordertable(numpoly)
Dim oworking(numpoly)

Restore coords
Dim vertex(numvertex) as tvertex
For n=0 To numvertex-1
vertex(n)=New tvertex
vertex(n).x = Readdata()
vertex(n).y= Readdata()
vertex(n).z= Readdata()
Next
Restore pnorms
For n=0 To numvertex-1
vertex(n).nx = Readdata()
vertex(n).ny = Readdata()
vertex(n).nz = Readdata()
Next

Restore polys
Dim poly(numpoly) as tpoly
For n=0 To numpoly-1
poly(n)=New tpoly
; BB_Read dum,dum
dum = Readdata()
dum = Readdata()
poly(n).v0 = Readdata()
poly(n).v1 = Readdata()
poly(n).v2 = Readdata()
poly(n).v3 = Readdata()
Next

Dim lightsource(2) as tlightsource
lightsource(0)=New tlightsource
lightsource(0).x=256
lightsource(0).y=256
lightsource(0).z=-256


x_angle=0
y_angle=0
z_angle=0

;setfps 31.7

global RenderMethod =0

While Not BB_KeyDown(1)

if enterKey()
RenderMethod++
if RenderMethod>2 then RenderMethod=0
flushkeys
endif

select RenderMethod
case 0
Render_Method_Name$ ="fastDot inner loop"
case 1
Render_Method_Name$ ="gouraud strip inner loop"
case 2
Render_Method_Name$ ="gouraud triangle"
endselect

rotate_transform_vertices(x_angle,y_angle,z_angle)
sort_polys()
BB_Cls()
draw_polys()
; draw_vertices()
lfps=(1000/(BB_MilliSecs()-t))
t=BB_MilliSecs()
Text 10,10,"Current FPS : "+str$(lfps)
Text 10,30,"Highest FPS : "+str$(hfps)
Text 10,40,"Average FPS : "+str$(afps#)
Text 10,50," Lowest FPS : "+str$(lfps)
Text 10,60," Render : "+Render_Method_Name$

Text 210,10," Points : "+str$(numvertex)
Text 210,20,"Polygons : "+str$(numpoly)


If hfps=0 Then hfps=lfps
If lfps=0 Then lfps=lfps
If afps#=0 Then afps#=lfps : afpscount=1
If lfps>hfps Then hfps=lfps
If lfps<lfps Then lfps=lfps
afps#=((afps#*afpscount)+lfps)/(afpscount+1)
afpscount=afpscount+1
BB_Flip()
x_angle=x_angle+1
y_angle=y_angle+2
Login required to view complete source code


  Related Links:

        -  Convert BlitzBASIC Source To PlayBASIC
        -  3D Development Forum


  Download:

       Code attached bellow