3D -> 2D Projektion & Rotationen

Übersicht BlitzMax, BlitzMax NG Codearchiv & Module

Neue Antwort erstellen

 

Tritium

Betreff: 3D -> 2D Projektion & Rotationen

BeitragDo, Aug 14, 2014 17:54
Antworten mit Zitat
Benutzer-Profile anzeigen
Moin,

hab mich eben für ein anderes Projekt ein wenig mit dem Zeichnen von 3D-Objekten nur mittels 2D-Funktionen (in diesem Fall nur DrawLine) beschäftigt. Außerdem enthalten sind noch Rotation und Translation (alles sehr simpel gehalten). Vielleicht kanns ja jemand brauchen:

BlitzMax: [AUSKLAPPEN]
Strict

Graphics 640,480
SetOrigin 320,240


Local focal# = 400
Local c1:Cube3D = C3D(P3D(0,0,201),300)

Local yaw# = 0.6
Local pitch# = 1.4
Local roll# = -1

Local count = 0


Repeat
Cls

count = (count + 1) Mod 360

c1.rotate(yaw,pitch,roll)
c1.move(Sin(count),Cos(count),0)
c1.updateProjection(focal)
c1.draw()

Flip
Until KeyHit(KEY_ESCAPE) Or AppTerminate()
End


' - - - - - -


Type Cube3D
Field origin:Point3D
Field edgeLength#

Field v:Point3D[8] 'Vertices
Field p:Point2D[8] 'Projected

Function CreateCube3D:Cube3D(origin:Point3D,edgeLength#)
Local temp:Cube3D = New Cube3D
temp.v[0] = P3D(-.5*edgeLength, -.5*edgeLength, -.5*edgeLength).add(origin) 'Oben, vorne, links 0
temp.v[1] = P3D( .5*edgeLength, -.5*edgeLength, -.5*edgeLength).add(origin) 'Oben, vorne, rechts 1
temp.v[2] = P3D(-.5*edgeLength, -.5*edgeLength, .5*edgeLength).add(origin) 'Oben, hinten, links 2
temp.v[3] = P3D( .5*edgeLength, -.5*edgeLength, .5*edgeLength).add(origin) 'Oben, hinten, rechts 3
temp.v[4] = P3D(-.5*edgeLength, .5*edgeLength, -.5*edgeLength).add(origin) 'Unten, vorne, links 4
temp.v[5] = P3D( .5*edgeLength, .5*edgeLength, -.5*edgeLength).add(origin) 'Unten, vorne, rechts 5
temp.v[6] = P3D(-.5*edgeLength, .5*edgeLength, .5*edgeLength).add(origin) 'Unten, hinten, links 6
temp.v[7] = P3D( .5*edgeLength, .5*edgeLength, .5*edgeLength).add(origin) 'Unten, hinten, rechts 7
temp.origin = origin
Return temp
End Function

Method updateProjection(focal#)
For Local i=0 To 7
p[i] = v[i].project(focal)
Next
End Method

Method draw()
SetColor 0,255,0

DrawLine p[0].x,p[0].y,p[1].x,p[1].y
DrawLine p[0].x,p[0].y,p[2].x,p[2].y
DrawLine p[2].x,p[2].y,p[3].x,p[3].y
DrawLine p[3].x,p[3].y,p[1].x,p[1].y

DrawLine p[0].x,p[0].y,p[4].x,p[4].y
DrawLine p[1].x,p[1].y,p[5].x,p[5].y
DrawLine p[2].x,p[2].y,p[6].x,p[6].y
DrawLine p[3].x,p[3].y,p[7].x,p[7].y

DrawLine p[4].x,p[4].y,p[5].x,p[5].y
DrawLine p[4].x,p[4].y,p[6].x,p[6].y
DrawLine p[6].x,p[6].y,p[7].x,p[7].y
DrawLine p[7].x,p[7].y,p[5].x,p[5].y
End Method

'xyz (aerospace rotation sequence)
Method rotate(yaw#,pitch#,roll#)
For Local i=0 To 7
v[i] = v[i].sub(origin)
v[i] = v[i].rotateAroundOrigin(yaw,pitch,roll)
v[i] = v[i].add(origin)
Next
End Method

Method move(x#,y#,z#)
For Local i=0 To 7
v[i] = v[i].add(P3D(x,y,z))
Next
origin = origin.add(P3D(x,y,z))
End Method
End Type



Type Point2D
Field x#,y#

Function CreatePoint2D:Point2D(x#,y#)
Local temp:Point2D = New Point2D
temp.x = x
temp.y = y
Return temp
End Function
End Type



Type Point3D
Field x#,y#,z#

Function CreatePoint3D:Point3D(x#,y#,z#)
Local temp:Point3D = New Point3D
temp.x = x
temp.y = y
temp.z = z
Return temp
End Function

'xyz (aerospace rotation sequence)
Method rotateAroundOrigin:Point3D(yaw#,pitch#,roll#)
Local x_# = Cos(pitch)*Cos(yaw)*x + Cos(pitch)*Sin(yaw)*y - Sin(pitch)*z
Local y_# = (Sin(roll)*Sin(pitch)*Cos(yaw) - Cos(roll)*Sin(yaw))*x + (Sin(roll)*Sin(pitch)*Sin(yaw) + Cos(roll)*Cos(yaw))*y + Sin(roll)*Cos(pitch)*z
Local z_# = (Cos(roll)*Sin(pitch)*Cos(yaw) + Sin(roll)*Sin(yaw))*x + (Cos(roll)*Sin(pitch)*Sin(yaw) - Sin(roll)*Cos(yaw))*y + Cos(roll)*Cos(pitch)*z
Return P3D(x_,y_,z_)
End Method

Method project:Point2D(focal#)
Local x_# = x * focal/(z+focal)
Local y_# = y * focal/(z+focal)
Return P2D(x_,y_)
End Method

Method add:Point3D(point:Point3D)
Return P3D(x+point.x,y+point.y,z+point.z)
End Method

Method sub:Point3D(point:Point3D)
Return P3D(x-point.x,y-point.y,z-point.z)
End Method
End Type


' - - - - - - -

Function P2D:Point2D(x#,y#)
Return Point2D.CreatePoint2D(x,y)
End Function

Function P3D:Point3D(x#,y#,z#)
Return Point3D.CreatePoint3D(x,y,z)
End Function

Function C3D:Cube3D(origin:Point3D,edgeLength#)
Return Cube3D.CreateCube3D(origin,edgeLength)
End Function


Der Code ist direkt ausführbar, enthält ein Beispiel.

Das ganze basiert auf http://en.wikipedia.org/wiki/3D_projection#Diagram und http://de.wikipedia.org/wiki/E...eugtechnik

Neue Antwort erstellen


Übersicht BlitzMax, BlitzMax NG Codearchiv & Module

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group