A simple 3D render method

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
AndT
Posts: 26
Joined: Aug 01, 2007 10:05

A simple 3D render method

Postby AndT » Jan 10, 2008 20:08

Over 100 FPS (ca. 300-600) and highres ;)

Code: Select all

' A simple 3D demo written by AndT

type xycor
    x as double
    y as double
end type

type polygon
    p1 as xycor
    p2 as xycor
    p3 as xycor
end type

dim as polygon poly(1 to 2)

sub drawpolygon(byval p1 as xycor,byval p2 as xycor,byval p3 as xycor,byval col as uinteger)
    line (p1.x,p1.y)-(p2.x,p2.y),col
    line (p2.x,p2.y)-(p3.x,p3.y),col
    line (p3.x,p3.y)-(p1.x,p1.y),col
end sub


'Main programm

resx=1024
resy=768


screenres resx,resy,,,1
dim as integer mode,mode2,mode3,mode4,mode5,mode6,mode7,mode8

' FOR FPSVIEW
DIM AS INTEGER frame
DIM AS SINGLE t = TIMER
DIM AS INTEGER FPS

' Main loop
do

' Draw
drawpolygon (Poly(1).p1,Poly(1).p2,Poly(1).p3,15) ' Polygon1
drawpolygon (Poly(2).p1,Poly(1).p2,Poly(1).p3,15) ' Polygon2



sleep 1,1 ' remove this to see the real FPS xD

' Remove
drawpolygon (Poly(1).p1,Poly(1).p2,Poly(1).p3,0) ' Polygon1
drawpolygon (Poly(2).p1,Poly(1).p2,Poly(1).p3,0) ' Polygon2

' Simple Animation Code ;)
if poly(1).p1.x > resx then mode2 = 0
if poly(1).p1.x < 1 then mode2 = 1
if poly(1).p2.y > resy then mode3 = 0
if poly(1).p2.y < 1 then mode3 = 1
if poly(1).p2.x > resx then mode4 = 0
if poly(1).p2.x < 1 then mode4 = 1
if poly(1).p1.y > resy then mode5 = 0
if poly(1).p1.y < 1 then mode5 = 1
if poly(1).p3.x > resx then mode6 = 0
if poly(1).p3.x < 1 then mode6 = 1
if poly(1).p3.y > resy then mode7 = 0
if poly(1).p3.y < 1 then mode7 = 1
if poly(2).p1.x > resx then mode = 0
if poly(2).p1.x < 1 then mode = 1
if poly(2).p1.y > resy then mode8 = 0
if poly(2).p1.y < 1 then mode8 = 1
if mode = 1 then poly(2).p1.x + = 1 else poly(2).p1.x - = 1
if mode2 = 1 then poly(1).p1.x + = 2 else poly(1).p1.x - = 2
if mode3 = 1 then poly(1).p2.y + = 3 else poly(1).p2.y - = 3
if mode4 = 1 then poly(1).p2.x + = 2 else poly(1).p2.x - = 2
if mode5 = 1 then poly(1).p1.y + = 1 else poly(1).p1.y - = 1
if mode6 = 1 then poly(1).p3.x + = 2 else poly(1).p3.x - = 3
if mode7 = 1 then poly(1).p3.y + = 2 else poly(1).p3.y - = 2
if mode8 = 1 then poly(2).p1.y + = 1 else poly(2).p1.y - = 1

' FPSVIEW
frame += 1
  IF frame >= 50 THEN
    cls
    FPS = INT(frame / (TIMER - t))
    t = TIMER
    frame = 0
    locate 2,2
    print "FPS : ";FPS
    end if
   
loop until inkey <> ""
notthecheatr
Posts: 1759
Joined: May 23, 2007 21:52
Location: Cut Bank, MT
Contact:

Postby notthecheatr » Jan 10, 2008 21:27

It only compiles with -lang deprecated :P

Well not bad, but it doesn't really do a whole lot. Drawing wireframe is one thing. drawing shaded models is entirely different (due to the complications raised by HSR and other things - and if you want proper lighting, materials, and texturing - forget about it in real time.) Nice example, but in the end for real 3d OpenGL or DirectX are pretty much the best options.
elsairon
Posts: 207
Joined: Jul 02, 2005 14:51

Postby elsairon » Jan 10, 2008 22:04

Nice!

Add texture support for the poly's and non visible poly culling you're set ... :P
notthecheatr
Posts: 1759
Joined: May 23, 2007 21:52
Location: Cut Bank, MT
Contact:

Postby notthecheatr » Jan 10, 2008 22:09

That's not a very simple thing to do, besides as I said by the time he does that the FPS will plummet.
1000101
Posts: 2556
Joined: Jun 13, 2005 23:14
Location: SK, Canada

Postby 1000101 » Jan 11, 2008 1:51

I don't know why people insist on doing 3d in software anymore.

Welcome to 1993.
Mysoft
Posts: 778
Joined: Jul 28, 2005 13:56
Location: Brazil, Santa Catarina, Indaial (ouch!)
Contact:

Postby Mysoft » Jan 11, 2008 2:50

year 2057: "we need to make a new graphic card... but no one in the world remember how to program the 3D software for the graphic card..."

maybe this can be a reason... (the same with ASM), i guess in my opinion forget those things only will make a few BAD try to control people knowing those things that everyone else dont care to know... (welcome to matrix)...

ofcourse that is a Exaggeration, but in a scale of time... it's clearly true... (gladly we have books and... storage devices... but... this happened before)
counting_pine
Site Admin
Posts: 6219
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Postby counting_pine » Jan 11, 2008 3:25

1000101 wrote:I don't know why people insist on doing 3d in software anymore.
There are people who like to mess around with ideas and algorithms, to get to know their way around basic concepts and build up their knowledge around them.
There are also people who prefer to maximise what they can accomplish, by using established ideas and methods. They're not as interested in "how does it work?", so much as "how can I use it?"
Both are perfectly acceptable schools of thought, but without the former, there wouldn't be the latter.
redcrab
Posts: 619
Joined: Feb 07, 2006 15:29
Location: France / Luxemburg
Contact:

Postby redcrab » Jan 18, 2008 14:59

Sorry but I do not see anything in 3D ...
Just bouncing tied lines ...

Did I miss something ???
dabooda
Posts: 112
Joined: Aug 28, 2006 21:35
Location: Texas
Contact:

Postby dabooda » Jan 18, 2008 15:25

You didn't miss anything, there is no 3-d there. Just some tied lines with pong bouncing ends. There is not rotation, scale or rather perspective here. This is not 3-d, just an optical illusion if even that. The only true thing shown here is how to draw a triangle. I was wondering why your DrawPolygon() routine isn't set up where you simply pass a pointer to your polygon instead of feeding all 3 corners?

Code: Select all

'DrawPolygon Sub
Sub drawpolygon( poly as polygon ptr, col as UInteger)
     Line (poly->p1.x, poly->p1.y)-(poly->p2.x, poly->p2.y), col
     Line (poly->p2.x, poly->p2.y)-(poly->p3.x, poly->p3.y), col
     Line (poly->p3.x, poly->p3.y)-(poly->p1.x, poly->p1.y), col
End Sub

'To call it simply do this
drawpolygon @poly(0), 15
drawpolygon @poly(1), 15


You could probably even set it up to pass the polygon by reference, anything is better than feeding it each corner. That kind of defeats the purpose of setting it up as a type.

DaBooda out...
vdecampo
Posts: 2982
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Postby vdecampo » Jan 18, 2008 16:08

It is what it is. I don't understand the harsh criticism. This wasn't advertised as a new 3D rendering system. Just a very simple raster gfx example. Jeez!

Thanks for the submission AndT. I'm sure some will find it intersting.

-Vince
Dr_D
Posts: 2398
Joined: May 27, 2005 4:59
Contact:

Postby Dr_D » Jan 18, 2008 22:40

I also think that doing 3d in software mode is excellent practice and keeps your math skills sharp. Also, it's just plain fun. I think most people that do neat things with hardware accelerated libraries, like OpenGL or DirectX, have some background in software rendering.

Speaking of software...
Now for the obligatory FBEXT reference! :D We've recently added a flat shaded triangle primitive rendering function, and I made a demo using it. You can hit space to toggle vsync... and that's pretty much it.

http://dave-stanley.dyndns.org/programs/fbext_demo1.rar
dabooda
Posts: 112
Joined: Aug 28, 2006 21:35
Location: Texas
Contact:

Postby dabooda » Jan 19, 2008 0:17

'A Simple 3D Render Method' implies that it is 3D, which it is not. That isn't criticism but rather pointing out a fact. It simply needs to be renamed.

I also agree doing software 3d is essential for anyone wishing to program 3d with hardware. Not to mention its just fun. Who cares if it is nowhere near today's standards. I'm more impressed with someone who renders a shaded polygon with code, than simply feeding a description to HAL.

DaBooda out...
D.J.Peters
Posts: 8122
Joined: May 28, 2005 3:28
Contact:

Postby D.J.Peters » Jan 19, 2008 8:57

hello Dr_D
nice example but why are the fps so low?

Joshy
DaveUnit
Posts: 239
Joined: Apr 20, 2006 15:47
Location: Central MA
Contact:

Postby DaveUnit » Jan 19, 2008 14:00

awesome example Dr_D. FB Ext is really cool.
dabooda
Posts: 112
Joined: Aug 28, 2006 21:35
Location: Texas
Contact:

Postby dabooda » Jan 19, 2008 14:14

I was being critical earlier and I apologize, don't want to come off like some around here. It was counter productive and again I apologize.

If you are interested in software 3d I can recommend a great book: Flights of Fantasy by Christopher Lampton. Its main concept is the building of a flight simulator and the code snippets are in c. But it is an excellent source and teaches you about 3d rendering from the ground up. Good Stuff.

DaBooda out...

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 2 guests