Omnimaga: The Coders Of Tomorrow
Welcome, Guest. Please login or register.
 
Omnimaga: The Coders Of Tomorrow
24 May, 2013, 21:31:29 *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   home   news downloads projects tutorials misc forums rules new posts irc about Login Register  
+-OmnomIRC

You must Register, be logged in and have at least 40 posts to use this shout-box! If it still doesn't show up afterward, it might be that OmnomIRC is disabled for your group or under maintenance.

Note: You can also use an IRC client like mIRC, X-Chat or Mibbit to connect to an EFnet server and #omnimaga.

Pages: [1] 2 3 4   Go Down
  Print  
Author Topic: [Lua] Tiny3D -  (Read 3032 times) Bookmark and Share
0 Members and 1 Guest are viewing this topic.
Chockosta
LV6 Super Member (Next: 500)
******
Offline Offline

Gender: Male
Last Login: 22 May, 2013, 20:19:59
Date Registered: 03 June, 2011, 20:14:17
Location: France
Posts: 440


Topic starter
Total Post Ratings: +159

View Profile
« on: 24 September, 2011, 15:01:10 »
+4

Hello guys !

I haven't done anything for two weeks, because of my homework Sad
But I have a new project ! This is a small 3D engine.
I know that there is already Make3D by Levak, which is amazing, but I have to train with 3D. Moreover, mine is supposed to be a game engine, because it is very small. (I don't know if Lua is fast enough to use it)

So if anyone's interested, I post my engine...
I also add screenshots of a little 3D viewer made with that engine.
For now, it only draw vertices and edges. I'll try to add triangles...

________________________________________________________________________________

EDIT

10/01/2011 : The engine is finished...
New code :
Spoiler for Hidden:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
--Lua Tiny3D - By Loic Pujet

function multiplyMatrix(matrix,vector)
 local x=matrix[1][1]*vector[1]+matrix[1][2]*vector[2]+matrix[1][3]*vector[3]
 local y=matrix[2][1]*vector[1]+matrix[2][2]*vector[2]+matrix[2][3]*vector[3]
 local z=matrix[3][1]*vector[1]+matrix[3][2]*vector[2]+matrix[3][3]*vector[3]
 return x,y,z
end

function width() return platform.window:width() end
function height() return platform.window:height() end

function reverseTable(tbl)
 local tbl2={}
 for i,e in ipairs(tbl) do
  tbl2[#tbl-i+1]=e
 end
 return tbl2
end

function replaceFaces(tbl1,tbl2,faces)
 local faces2,count={},1
 for i,e in pairs(tbl1) do
  for j,e2 in pairs(tbl2) do
   if e2 then
    if e==e2 then
     faces2[count]=faces[j]
     count=count+1
     tbl2[j]=nil
    end
   end
  end
 end
 return reverseTable(faces2)
end

function sortFaces(vertices,faces,offset)
 local faces2,distTbl,facesTbl={},{},{}
 local middle={}
 local dist,xsum,ysum,zsum=0,0,0,0
 for i,e in pairs(faces) do
  xsum,ysum,zsum=0,0,0,0
  for j,e2 in pairs(e) do
   xsum,ysum,zsum=xsum+vertices[e2][1],ysum+vertices[e2][2],zsum+vertices[e2][3]
  end
  middle={xsum/#e,ysum/#e+offset,zsum/#e}
   dist=middle[1]*middle[1]+middle[2]*middle[2]+middle[3]*middle[3]
  distTbl[i]=dist
  facesTbl[i]=dist
 end
 table.sort(distTbl)
 return replaceFaces(distTbl,facesTbl,faces)
end

function chooseColor(gc,vertices,face,color)
 if #face<3 then
  gc:setColorRGB(color[1],color[2],color[3])
 else
  local a,b,c=vertices[face[1]][1]-vertices[face[2]][1],vertices[face[1]][2]-vertices[face[2]][2],vertices[face[1]][3]-vertices[face[2]][3]
  local d,e,f=vertices[face[1]][1]-vertices[face[3]][1],vertices[face[1]][2]-vertices[face[3]][2],vertices[face[1]][3]-vertices[face[3]][3]
  local normale={math.abs(b*f-c*e),math.abs(c*d-a*f),math.abs(a*e-b*d)}
  local angle=math.atan(math.sqrt(normale[1]*normale[1]+normale[3]*normale[3])/normale[2])
  local R,G,B=color[1]+angle*-60+50,color[2]+angle*-60+50,color[3]+angle*-60+50
  R,G,B=R>255 and 255 or R,G>255 and 255 or G,B>255 and 255 or B
  R,G,B=R<0 and 0 or R,G<0 and 0 or G,B<0 and 0 or B
  gc:setColorRGB(R,G,B)
 end
end

function renderFaces(gc,vertices,faces,pos,mode,color,offset)
 local polygon,size,faces2={},0,{}
 if mode==4 or mode==5 or mode==6 then
  faces2=sortFaces(vertices,faces,offset)
 else
  faces2=faces
 end
 for i,e in pairs(faces2) do
  polygon,size={},0
  drawPoly=true
  for j,f in pairs(faces2[i]) do
   if not pos[f] then
    drawPoly=false
   else
    polygon[j*2-1]=pos[f][1]
    polygon[j*2]=pos[f][2]
    size=size+2
   end
  end
  if drawPoly then
   polygon[size+1]=pos[faces2[i][1]][1]
   polygon[size+2]=pos[faces2[i][1]][2]
   if mode==4 then
    gc:setColorRGB(color[1],color[2],color[3])
    gc:fillPolygon(polygon)
   elseif mode==5 or mode==6 then
    chooseColor(gc,vertices,e,color)
    gc:fillPolygon(polygon)
   end
   if mode==2 or mode==3 or mode==4 or mode==5 then
    gc:setColorRGB(0,0,0)
    gc:setPen("thin","smooth")
    gc:drawPolyLine(polygon)
   end
  end
 end
end

function renderVertices(gc,pos)
 gc:setColorRGB(0,0,0)
 for i,e in pairs(pos) do
  if e then
   gc:fillRect(e[1]-1,e[2]-1,3,3)
  end
 end
end

----------------------------------------------------------------------------

function rotate(vertices,angle,x,y,z)
 local sum=x+y+z
 x,y,z=x/sum,y/sum,z/sum
 local c,s=math.cos(angle),math.sin(angle)
 local matrix={{x*x+(1-x*x)*c,x*y*(1-c)-z*s,x*z*(1-c)+y*s},{x*y*(1-c)+z*s,y*y+(1-y*y)*c,y*z*(1-c)-x*s},{x*z*(1-c)-y*s,y*z*(1-c)+x*s,z*z+(1-z*z)*c}}
 for i,e in pairs(vertices) do
  vertices[i]={multiplyMatrix(matrix,e)}
 end
end

function translate(vertices,x,y,z)
 for i,e in pairs(vertices) do
  vertices[i]={e[1]+x,e[2]+y,e[3]+z}
 end
end

function scale(vertices,x,y,z)
 for i,e in pairs(vertices) do
  vertices[i]={e[1]*x,e[2]*y,e[3]*z}
 end
end

function render(gc,vertices,faces,mode,color,offset)
 local yDist,pos=0,{}
 for i,e in pairs(vertices) do
  if e[2]>-offset then
   yDist=offset/(e[2]+offset)
   pos[i]={e[1]*yDist*25+width()/2,height()/2-e[3]*yDist*25}
  else
   pos[i]=false
  end
 end
 if mode==1 or mode==2 then
  renderVertices(gc,pos)
 end
 if #faces>0 and (mode==2 or mode==3 or mode==4 or mode==5 or mode==6) then
  renderFaces(gc,vertices,faces,pos,mode,color,offset)
 end
end

----------------------------------------------------------------------------
New screenshots :


* ScreenShot050.png (10.1 KB, 320x240 - viewed 441 times.)

* ScreenShot051.png (9.25 KB, 320x240 - viewed 445 times.)
« Last Edit: 04 December, 2011, 15:26:01 by Chockosta » Logged

Chockosta (Loic Pujet) - Sorry for my poor English...
Look at my projects :
in C code : Periodic table, Space invaders, Fall, Snake, Minesweeper, nCraft (WIP)
in Lua : Snake, Space invaders, Bobby Carrot, Minesweeper, Mazes 3D, nSpaint, FreeCell, Tiny3D-Viewer, CubeField, Gravity Guy
Eiyeron
LV7 Elite (Next: 700)
*******
Offline Offline

Gender: Male
Last Login: 07 January, 2013, 20:29:04
Date Registered: 09 August, 2011, 16:51:22
Location: Err 404.
Posts: 552


Total Post Ratings: +18

View Profile WWW
« Reply #1 on: 24 September, 2011, 15:15:38 »
0

Great work! Is it enough fast?
Logged




Chockosta
LV6 Super Member (Next: 500)
******
Offline Offline

Gender: Male
Last Login: 22 May, 2013, 20:19:59
Date Registered: 03 June, 2011, 20:14:17
Location: France
Posts: 440


Topic starter
Total Post Ratings: +159

View Profile
« Reply #2 on: 24 September, 2011, 15:21:49 »
0

I think I can get 100 FPS...
If I draw 200 cubes, It's more like 5-10 FPS
Logged

Chockosta (Loic Pujet) - Sorry for my poor English...
Look at my projects :
in C code : Periodic table, Space invaders, Fall, Snake, Minesweeper, nCraft (WIP)
in Lua : Snake, Space invaders, Bobby Carrot, Minesweeper, Mazes 3D, nSpaint, FreeCell, Tiny3D-Viewer, CubeField, Gravity Guy
Levak
LV8 Addict (Next: 1000)
********
Online Online

Gender: Male
Last Login: Today at 21:08:51
Date Registered: 04 April, 2010, 23:42:49
Location: France
Posts: 844


Total Post Ratings: +148

View Profile WWW
« Reply #3 on: 24 September, 2011, 15:36:30 »
0

Note : it is fast without fill-face-algorithms Cheesy
If you're not sure of the speed (because 100fps is really fast) just make an animation of a turn around, like 10 turns, and calculate the time it took.

Then, calculate the number of frames it requires to make this turnaround.
Like if the increment is 5°, it will take 360/5 frames to make a complete turn.

And then : fps=(number of frames)/(seconds) Cheesy
« Last Edit: 24 September, 2011, 15:39:29 by Levak » Logged

Human always wants to survive and that's why he will fall one day.
My website - TI-Planet - iNspired-Lua
Chockosta
LV6 Super Member (Next: 500)
******
Offline Offline

Gender: Male
Last Login: 22 May, 2013, 20:19:59
Date Registered: 03 June, 2011, 20:14:17
Location: France
Posts: 440


Topic starter
Total Post Ratings: +159

View Profile
« Reply #4 on: 24 September, 2011, 15:59:00 »
0

48 FPS...
With "timer.start(0.01)" to repeat the rotation.
Logged

Chockosta (Loic Pujet) - Sorry for my poor English...
Look at my projects :
in C code : Periodic table, Space invaders, Fall, Snake, Minesweeper, nCraft (WIP)
in Lua : Snake, Space invaders, Bobby Carrot, Minesweeper, Mazes 3D, nSpaint, FreeCell, Tiny3D-Viewer, CubeField, Gravity Guy
Levak
LV8 Addict (Next: 1000)
********
Online Online

Gender: Male
Last Login: Today at 21:08:51
Date Registered: 04 April, 2010, 23:42:49
Location: France
Posts: 844


Total Post Ratings: +148

View Profile WWW
« Reply #5 on: 24 September, 2011, 16:07:56 »
0

On CX ? ClickPad/TouchPad ? Computer Software ?
Because each of those values are differents =)
« Last Edit: 24 September, 2011, 16:08:14 by Levak » Logged

Human always wants to survive and that's why he will fall one day.
My website - TI-Planet - iNspired-Lua
Chockosta
LV6 Super Member (Next: 500)
******
Offline Offline

Gender: Male
Last Login: 22 May, 2013, 20:19:59
Date Registered: 03 June, 2011, 20:14:17
Location: France
Posts: 440


Topic starter
Total Post Ratings: +159

View Profile
« Reply #6 on: 24 September, 2011, 16:09:44 »
0

On my Clickpad.
Which one is the slowest ?
Logged

Chockosta (Loic Pujet) - Sorry for my poor English...
Look at my projects :
in C code : Periodic table, Space invaders, Fall, Snake, Minesweeper, nCraft (WIP)
in Lua : Snake, Space invaders, Bobby Carrot, Minesweeper, Mazes 3D, nSpaint, FreeCell, Tiny3D-Viewer, CubeField, Gravity Guy
Levak
LV8 Addict (Next: 1000)
********
Online Online

Gender: Male
Last Login: Today at 21:08:51
Date Registered: 04 April, 2010, 23:42:49
Location: France
Posts: 844


Total Post Ratings: +148

View Profile WWW
« Reply #7 on: 24 September, 2011, 16:10:08 »
0

On my Clickpad.
Which one is the slowest ?
The CX =)

Computer Software > ClickPad/TouchPad > CX
« Last Edit: 24 September, 2011, 16:10:37 by Levak » Logged

Human always wants to survive and that's why he will fall one day.
My website - TI-Planet - iNspired-Lua
Chockosta
LV6 Super Member (Next: 500)
******
Offline Offline

Gender: Male
Last Login: 22 May, 2013, 20:19:59
Date Registered: 03 June, 2011, 20:14:17
Location: France
Posts: 440


Topic starter
Total Post Ratings: +159

View Profile
« Reply #8 on: 24 September, 2011, 16:13:50 »
0

And, by the way, what is the fastest way to draw faces ?
With a lot of gc:fillRect(x,y,1,height) ?
Or with gc:fillPolygon ?
Logged

Chockosta (Loic Pujet) - Sorry for my poor English...
Look at my projects :
in C code : Periodic table, Space invaders, Fall, Snake, Minesweeper, nCraft (WIP)
in Lua : Snake, Space invaders, Bobby Carrot, Minesweeper, Mazes 3D, nSpaint, FreeCell, Tiny3D-Viewer, CubeField, Gravity Guy
Levak
LV8 Addict (Next: 1000)
********
Online Online

Gender: Male
Last Login: Today at 21:08:51
Date Registered: 04 April, 2010, 23:42:49
Location: France
Posts: 844


Total Post Ratings: +148

View Profile WWW
« Reply #9 on: 24 September, 2011, 16:18:22 »
0

And, by the way, what is the fastest way to draw faces ?
With a lot of gc:fillRect(x,y,1,height) ?
Or with gc:fillPolygon ?

Hummm ... I really don't know, but let's think ...
 - Lua methods are implemented in C directly in the OS.
 - how a fillpolygon method works ? by filling horizontal or vertical lines. I don't know other methods.
 - How a fillRect method works ? by filling hozintal or vertical lines.

Basicly it uses the same underground methods don't you think ?
So I guess using fillPolygon is smarter.

It is the one I use with Make3D
« Last Edit: 24 September, 2011, 16:18:33 by Levak » Logged

Human always wants to survive and that's why he will fall one day.
My website - TI-Planet - iNspired-Lua
Chockosta
LV6 Super Member (Next: 500)
******
Offline Offline

Gender: Male
Last Login: 22 May, 2013, 20:19:59
Date Registered: 03 June, 2011, 20:14:17
Location: France
Posts: 440


Topic starter
Total Post Ratings: +159

View Profile
« Reply #10 on: 24 September, 2011, 16:20:42 »
0

Yes, I think you're right.
I'll try to add this to my engine... Tomorrow.
Logged

Chockosta (Loic Pujet) - Sorry for my poor English...
Look at my projects :
in C code : Periodic table, Space invaders, Fall, Snake, Minesweeper, nCraft (WIP)
in Lua : Snake, Space invaders, Bobby Carrot, Minesweeper, Mazes 3D, nSpaint, FreeCell, Tiny3D-Viewer, CubeField, Gravity Guy
Jim Bauwens
Lua! Nspire! Linux!
Editor
LV10 31337 u53r (Next: 2000)
*
Offline Offline

Gender: Male
Last Login: Today at 20:33:25
Date Registered: 28 February, 2011, 22:32:12
Location: Belgium
Posts: 1733


Total Post Ratings: +180

View Profile WWW
« Reply #11 on: 24 September, 2011, 17:36:10 »
0

Looks very nice, good job Smiley
Logged

Loulou 54
LV4 Regular (Next: 200)
****
Offline Offline

Gender: Male
Last Login: 19 May, 2013, 15:27:36
Date Registered: 22 July, 2011, 21:19:29
Location: Meurthe-et-Moselle FRANCE
Posts: 152


Total Post Ratings: +34

View Profile WWW
« Reply #12 on: 24 September, 2011, 19:59:28 »
0

Quote
but I have to train with 3D
lol %)

Great job indeed ! Smiley
Logged

Some of my program available here.. Smiley
http://ti.bank.free.fr/index.php?mod=archives&ac=voir2&id=1471

     
mrmprog
LV7 Elite (Next: 700)
*******
Offline Offline

Last Login: 28 September, 2012, 05:58:25
Date Registered: 15 February, 2011, 01:35:36
Location: Somewhere
Posts: 559


Total Post Ratings: +34

View Profile
« Reply #13 on: 24 September, 2011, 20:04:32 »
0

Wow, that looks very good. 3d stuff on calc always amazes me.
Logged

Chockosta
LV6 Super Member (Next: 500)
******
Offline Offline

Gender: Male
Last Login: 22 May, 2013, 20:19:59
Date Registered: 03 June, 2011, 20:14:17
Location: France
Posts: 440


Topic starter
Total Post Ratings: +159

View Profile
« Reply #14 on: 25 September, 2011, 14:01:53 »
0

Quote
but I have to train with 3D
lol %)
Er... What's fun ?

__________________
Now my engine displays faces !

New code :
Spoiler for Hidden:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
function multiplyMatrix(matrix,vector)
 local x=matrix[1][1]*vector[1]+matrix[1][2]*vector[2]+matrix[1][3]*vector[3]
 local y=matrix[2][1]*vector[1]+matrix[2][2]*vector[2]+matrix[2][3]*vector[3]
 local z=matrix[3][1]*vector[1]+matrix[3][2]*vector[2]+matrix[3][3]*vector[3]
 return x,y,z
end

function rotate(vertices,angle,x,y,z)
 local sum=x+y+z
 x,y,z=x/sum,y/sum,z/sum
 local c,s=math.cos(angle),math.sin(angle)
 local matrix={{x*x+(1-x*x)*c,x*y*(1-c)-z*s,x*z*(1-c)+y*s},{x*y*(1-c)+z*s,y*y+(1-y*y)*c,y*z*(1-c)-x*s},{x*z*(1-c)-y*s,y*z*(1-c)+x*s,z*z+(1-z*z)*c}}
 for i,e in pairs(vertices) do
  vertices[i]={multiplyMatrix(matrix,e)}
 end
end

function translate(vertices,x,y,z)
 for i,e in pairs(vertices) do
  vertices[i]={e[1]+x,e[2]+y,e[3]+z}
 end
end

function scale(vertices,x,y,z)
 for i,e in pairs(vertices) do
  vertices[i]={e[1]*x,e[2]*y,e[3]*z}
 end
end

function width() return platform.window:width() end
function height() return platform.window:height() end

function reverseTable(tbl)
 local tbl2={}
 for i,e in ipairs(tbl) do
  tbl2[#tbl-i+1]=e
 end
 return tbl2
end

function replaceFaces(tbl1,tbl2,faces)
 local faces2={}
 for i,e in pairs(tbl1) do
  for j,e2 in pairs(tbl2) do
   if e2 then
    if e==e2 then
     table.insert(faces2,faces[j])
     tbl2[j]=nil
    end
   end
  end
 end
 return reverseTable(faces2)
end

function sortFaces(vertices,faces)
 local faces2,distTbl,facesTbl={},{},{}
 local middle={}
 local dist,xsum,ysum,zsum=0,0,0,0
 for i,e in pairs(faces) do
  xsum,ysum,zsum=0,0,0,0
  for j,e2 in pairs(e) do
   xsum,ysum,zsum=xsum+vertices[e2][1],ysum+vertices[e2][2],zsum+vertices[e2][3]
  end
  middle={xsum/#e,ysum/#e-5,zsum/#e}
   dist=math.sqrt(middle[1]*middle[1]+middle[2]*middle[2]+middle[3]*middle[3])
  table.insert(distTbl,dist)
  table.insert(facesTbl,dist)
 end
 table.sort(distTbl)
 return replaceFaces(distTbl,facesTbl,faces)
end

function renderFaces(gc,vertices,faces,pos,mode)
 local polygon={}
 local faces2=sortFaces(vertices,faces)
 for i,e in pairs(faces2) do
  polygon={}
  for j,f in pairs(faces2[i]) do
   table.insert(polygon,pos[f][1])
   table.insert(polygon,pos[f][2])
  end
  table.insert(polygon,pos[faces2[i][1]][1])
  table.insert(polygon,pos[faces2[i][1]][2])
  if mode==4 or mode==5 then
   gc:setColorRGB(200,200,200)
   gc:fillPolygon(polygon)
  end
  if mode==2 or mode==3 or mode==4 then
   gc:setColorRGB(0,0,0)
   gc:drawPolyLine(polygon)
  end
 end
end

function renderVertices(gc,pos)
 gc:setColorRGB(0,0,0)
 for i,e in pairs(pos) do
  if e then
   gc:fillRect(e[1]-1,e[2]-1,3,3)
  end
 end
end

function render(gc,vertices,faces,mode)
 local ray,dist,nb,pos={},0,0,{}
 for i,e in pairs(vertices) do
  if e[2]<5 then
   ray={e[1],e[2]-5,e[3]}
    dist=math.sqrt(ray[1]*ray[1]+ray[2]*ray[2]+ray[3]*ray[3])
   nb=((dist*5)/(ray[2]))/dist
   table.insert(pos,{ray[1]*nb*25+width()/2,ray[3]*nb*25+height()/2})
  else
   table.insert(pos,false)
  end
 end
 if mode==1 or mode==2 then
  renderVertices(gc,pos)
 end
 if mode==2 or mode==3 or mode==4 or mode==5 then
  renderFaces(gc,vertices,faces,pos,mode)
 end
end


* ScreenShot052.png (10.8 KB, 320x240 - viewed 366 times.)

* ScreenShot053.png (8.87 KB, 320x240 - viewed 366 times.)
Logged

Chockosta (Loic Pujet) - Sorry for my poor English...
Look at my projects :
in C code : Periodic table, Space invaders, Fall, Snake, Minesweeper, nCraft (WIP)
in Lua : Snake, Space invaders, Bobby Carrot, Minesweeper, Mazes 3D, nSpaint, FreeCell, Tiny3D-Viewer, CubeField, Gravity Guy
Pages: [1] 2 3 4   Go Up
  Print  
 
Jump to:  

Powered by EzPortal
Powered by MySQL Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Powered by PHP
Page created in 0.313 seconds with 31 queries.
Skin by DJ Omnimaga edited from SMF default theme with the help of tr1p1ea.
All programs, games and songs avaliable on this website are property of their respective owners.
Best viewed in Opera, Firefox, Chrome and Safari with a resolution of 1024x768 or above.