 |
Das deutsche QBasic- und FreeBASIC-Forum Für euch erreichbar unter qb-forum.de, fb-forum.de und freebasic-forum.de!
|
Vorheriges Thema anzeigen :: Nächstes Thema anzeigen |
Autor |
Nachricht |
Caran

Anmeldungsdatum: 11.03.2007 Beiträge: 290 Wohnort: Lörrach
|
Verfasst am: 28.01.2008, 17:03 Titel: Frustum Culling |
|
|
Hei,
ich habe mal versucht, das hier auf FB zu übertragen:
http://wiki.delphigl.com/index.php/Tutorial_Frustum_Culling
und der Code schaut so aus:
Code: |
Function IsPointInFrustum(ByVal x As Single, ByVal y As Single, ByVal z As Single) As Integer
Dim As Single Frustum(0 To 5, 0 To 3)
Dim As Single ProjM(0 To 15)
Dim As Single ModM(0 To 15)
Dim As Single Clip(0 To 15)
Dim As Integer i
glGetFloatv(GL_PROJECTION_MATRIX, @ProjM(0))
glGetFloatv(GL_MODELVIEW_MATRIX, @ModM(0))
Clip(0) = ModM(0)*ProjM(0) + ModM(1)*ProjM(4) + ModM(2)*ProjM(8) + ModM(3)*ProjM(12)
Clip(1) = ModM(0)*ProjM(1) + ModM(1)*ProjM(5) + ModM(2)*ProjM(9) + ModM(3)*ProjM(13)
Clip(2) = ModM(0)*ProjM(2) + ModM(1)*ProjM(6) + ModM(2)*ProjM(10) + ModM(3)*ProjM(14)
Clip(3) = ModM(0)*ProjM(3) + ModM(1)*ProjM(7) + ModM(2)*ProjM(11) + ModM(3)*ProjM(15)
Clip(4) = ModM(4)*ProjM(0) + ModM(5)*ProjM(4) + ModM(6)*ProjM(8) + ModM(7)*ProjM(12)
Clip(5) = ModM(4)*ProjM(1) + ModM(5)*ProjM(5) + ModM(6)*ProjM(9) + ModM(7)*ProjM(13)
Clip(6) = ModM(4)*ProjM(2) + ModM(5)*ProjM(6) + ModM(6)*ProjM(10) + ModM(7)*ProjM(14)
Clip(7) = ModM(4)*ProjM(3) + ModM(5)*ProjM(7) + ModM(6)*ProjM(11) + ModM(7)*ProjM(15)
Clip(8) = ModM(8)*ProjM(0) + ModM(9)*ProjM(4) + ModM(10)*ProjM(8) + ModM(11)*ProjM(12)
Clip(9) = ModM(8)*ProjM(1) + ModM(9)*ProjM(5) + ModM(10)*ProjM(9) + ModM(11)*ProjM(13)
Clip(10) = ModM(8)*ProjM(2) + ModM(9)*ProjM(6) + ModM(10)*ProjM(10) + ModM(11)*ProjM(14)
Clip(11) = ModM(8)*ProjM(3) + ModM(9)*ProjM(7) + ModM(10)*ProjM(11) + ModM(11)*ProjM(15)
Clip(12) = ModM(12)*ProjM(0) + ModM(13)*ProjM(4) + ModM(14)*ProjM(8) + ModM(15)*ProjM(12)
Clip(13) = ModM(12)*ProjM(1) + ModM(13)*ProjM(5) + ModM(14)*ProjM(9) + ModM(15)*ProjM(13)
Clip(14) = ModM(12)*ProjM(2) + ModM(13)*ProjM(6) + ModM(14)*ProjM(10) + ModM(15)*ProjM(14)
Clip(15) = ModM(12)*ProjM(3) + ModM(13)*ProjM(7) + ModM(14)*ProjM(11) + ModM(15)*ProjM(15)
Frustum(0,0) = clip(3) - clip(0)
Frustum(0,1) = clip(7) - clip(4)
Frustum(0,2) = clip(11) - clip(8)
Frustum(0,3) = clip(15) - clip(12)
'NormalizePlane(self, Right)
Frustum(1,0) = clip(3) + clip(0)
Frustum(1,1) = clip(7) + clip(4)
Frustum(1,2) = clip(11) + clip(8)
Frustum(1,3) = clip(15) + clip(12)
'NormalizePlane(self, Left)
Frustum(2,0) = clip(3) + clip(1)
Frustum(2,1) = clip(7) + clip(5)
Frustum(2,2) = clip(11) + clip(9)
Frustum(2,3) = clip(15) + clip(13)
'NormalizePlane(self, Bottom)
Frustum(3,0) = clip(3) - clip(1)
Frustum(3,1) = clip(7) - clip(5)
Frustum(3,2) = clip(11) - clip(9)
Frustum(3,3) = clip(15) - clip(13)
'NormalizePlane(self, Top)
Frustum(4,0) = clip(3) - clip(2)
Frustum(4,1) = clip(7) - clip(6)
Frustum(4,2) = clip(11) - clip(10)
Frustum(4,3) = clip(15) - clip(14)
'NormalizePlane(self, Back)
Frustum(5,0) = clip(3) + clip(2)
Frustum(5,1) = clip(7) + clip(6)
Frustum(5,2) = clip(11) + clip(10)
Frustum(5,3) = clip(15) + clip(14)
'NormalizePlane(self, Front)
Function = 1
For i = 0 To 5
if Frustum(i,0)*X + Frustum(i,1)*Y + Frustum(i,2)*Z + Frustum(i,3) <= 0 Then
Function = 0
EndIf
Next
End Function |
Ich habe mein Bestes bei Übersetzen gegeben, dennoch klappts nich. Vielleicht liegts auch daran, dass ich nicht weiß was NormalizePlane(self, ...) (aus dem DGL-Code) bedeutet.
Weiß jemand was man besser machen muss?
MfG Caran _________________ Eine Erkenntnis von heute kann die Tochter eines Irrtums von gestern sein. |
|
Nach oben |
|
 |
Elektronix
Anmeldungsdatum: 29.06.2006 Beiträge: 742
|
Verfasst am: 28.01.2008, 18:00 Titel: |
|
|
NormalizePlane() wird als Sub oder als zweidimensionales Array benutzt (vermutlich eher als Sub). Auf jeden Fall ist es aber nicht vorher deklariert oder dimensioniert worden. Das muß gemacht werden. _________________ Und die Grundgebihr is aa scho drin- DOS is jo nett. |
|
Nach oben |
|
 |
RatsDevSoftware
Anmeldungsdatum: 22.11.2007 Beiträge: 48
|
|
Nach oben |
|
 |
dreael Administrator

Anmeldungsdatum: 10.09.2004 Beiträge: 2529 Wohnort: Hofen SH (Schweiz)
|
Verfasst am: 28.01.2008, 22:03 Titel: Re: Frustum Culling |
|
|
Caran hat Folgendes geschrieben: | Code: | Clip(0) = ModM(0)*ProjM(0) + ModM(1)*ProjM(4) + ModM(2)*ProjM(8) + ModM(3)*ProjM(12)
Clip(1) = ModM(0)*ProjM(1) + ModM(1)*ProjM(5) + ModM(2)*ProjM(9) + ModM(3)*ProjM(13)
Clip(2) = ModM(0)*ProjM(2) + ModM(1)*ProjM(6) + ModM(2)*ProjM(10) + ModM(3)*ProjM(14)
Clip(3) = ModM(0)*ProjM(3) + ModM(1)*ProjM(7) + ModM(2)*ProjM(11) + ModM(3)*ProjM(15)
Clip(4) = ModM(4)*ProjM(0) + ModM(5)*ProjM(4) + ModM(6)*ProjM(8) + ModM(7)*ProjM(12)
Clip(5) = ModM(4)*ProjM(1) + ModM(5)*ProjM(5) + ModM(6)*ProjM(9) + ModM(7)*ProjM(13)
Clip(6) = ModM(4)*ProjM(2) + ModM(5)*ProjM(6) + ModM(6)*ProjM(10) + ModM(7)*ProjM(14)
Clip(7) = ModM(4)*ProjM(3) + ModM(5)*ProjM(7) + ModM(6)*ProjM(11) + ModM(7)*ProjM(15)
Clip(8) = ModM(8)*ProjM(0) + ModM(9)*ProjM(4) + ModM(10)*ProjM(8) + ModM(11)*ProjM(12)
Clip(9) = ModM(8)*ProjM(1) + ModM(9)*ProjM(5) + ModM(10)*ProjM(9) + ModM(11)*ProjM(13)
Clip(10) = ModM(8)*ProjM(2) + ModM(9)*ProjM(6) + ModM(10)*ProjM(10) + ModM(11)*ProjM(14)
Clip(11) = ModM(8)*ProjM(3) + ModM(9)*ProjM(7) + ModM(10)*ProjM(11) + ModM(11)*ProjM(15)
Clip(12) = ModM(12)*ProjM(0) + ModM(13)*ProjM(4) + ModM(14)*ProjM(8) + ModM(15)*ProjM(12)
Clip(13) = ModM(12)*ProjM(1) + ModM(13)*ProjM(5) + ModM(14)*ProjM(9) + ModM(15)*ProjM(13)
Clip(14) = ModM(12)*ProjM(2) + ModM(13)*ProjM(6) + ModM(14)*ProjM(10) + ModM(15)*ProjM(14)
Clip(15) = ModM(12)*ProjM(3) + ModM(13)*ProjM(7) + ModM(14)*ProjM(11) + ModM(15)*ProjM(15)
Frustum(0,0) = clip(3) - clip(0)
Frustum(0,1) = clip(7) - clip(4)
Frustum(0,2) = clip(11) - clip(8)
Frustum(0,3) = clip(15) - clip(12)
'NormalizePlane(self, Right)
Frustum(1,0) = clip(3) + clip(0)
Frustum(1,1) = clip(7) + clip(4)
Frustum(1,2) = clip(11) + clip(8)
Frustum(1,3) = clip(15) + clip(12)
'NormalizePlane(self, Left)
Frustum(2,0) = clip(3) + clip(1)
Frustum(2,1) = clip(7) + clip(5)
Frustum(2,2) = clip(11) + clip(9)
Frustum(2,3) = clip(15) + clip(13)
'NormalizePlane(self, Bottom)
Frustum(3,0) = clip(3) - clip(1)
Frustum(3,1) = clip(7) - clip(5)
Frustum(3,2) = clip(11) - clip(9)
Frustum(3,3) = clip(15) - clip(13)
'NormalizePlane(self, Top)
Frustum(4,0) = clip(3) - clip(2)
Frustum(4,1) = clip(7) - clip(6)
Frustum(4,2) = clip(11) - clip(10)
Frustum(4,3) = clip(15) - clip(14)
'NormalizePlane(self, Back)
Frustum(5,0) = clip(3) + clip(2)
Frustum(5,1) = clip(7) + clip(6)
Frustum(5,2) = clip(11) + clip(10)
Frustum(5,3) = clip(15) + clip(14) |
|
Ein schönes Beispiel wieder einmal für
http://www.dreael.ch/Deutsch/BASIC-Knowhow-Ecke/Anfaengerfehler.html
d.h. die Codewiederholungen lassen sich problemlos eliminieren:
Code: | ' Dasselbe kürzer
Dim i, j As Integer
For i=0 To 15
Clip(i) = 0
For j=0 To 3
Clip(i) += ModM((i And 12)+j) * ProjM((i And 3)+(j Shl 2))
Next j
Next i
For i=0 To 5
For j=0 To 3
Frustum(i, j) = clip(3 + (j Shl 2)) - clip((i Shr 1) + (j Shl 2))
Next j
'NormalizePlane(self, Right)
Next i |
Hinweis: Inhaltlich habe ich mich mit den Algorithmen überhaupt nicht auseinandergesetzt. _________________ Teste die PC-Sicherheit mit www.sec-check.net |
|
Nach oben |
|
 |
ytwinky

Anmeldungsdatum: 28.05.2005 Beiträge: 2624 Wohnort: Machteburch
|
Verfasst am: 28.01.2008, 23:44 Titel: |
|
|
Im Prizip hat dreal Recht, leider ist es aber nicht immer 'RIGHT'..
..das ließe sich durch ein entsprechendes Array(o.Ä.) jedoch berücksichtigen..
Normalisieren ist eine mathematische Prozedur, deren Verständnis für die Benutzung der Funktion vonnöten scheint..
Gruß
ytwinky _________________
v1ctor hat Folgendes geschrieben: | Yeah, i like INPUT$(n) as much as PRINT USING.. | ..also ungefähr so, wie ich GOTO.. |
|
Nach oben |
|
 |
marzec
Anmeldungsdatum: 13.10.2004 Beiträge: 267
|
Verfasst am: 29.01.2008, 11:57 Titel: |
|
|
der code erstellt 6 ebenen die dein view frustum definieren. diese ebenen errechnet er aus der derzeitigen projection matrix. eine ebene hat einen offset ( kleinster abstand zum ursprung ) sowie einen normalvektor, bestehend aus 3 komponenten. dieser normalvektor sowie der offset erlauben es für einen punkt zu bestimmen auf welcher seite der ebene er sich befindet. beim frustum culling zeigen alle normalen der 6 ebenen entweder nach außen oder nach innen. um einen punkt zu akzeptieren oder verwerfen wird dieser mit allen ebenen getestet. ist der punkt für alle ebenen auf der vorderseite ist er sichtbar, anderenfalls nicht ( annahme: die normalen der ebenen zeigen in das frustum ). einfache gleichung hierzu ( ebenen gleichung... )
Code: |
result = n dot p + d = n.x * p.x + n.y * p.y + n.z * p.z + d
|
mit n ... normalvektor der ebene, p ... zu testender punkt, d ... kleinster abstand der ebene zum ursprung
result < 0 punkt liegt hinter der ebene.
result = 0 punkt liegt auf der ebene.
result > 0 punkt liegt vor der ebene.
NormalizePlane normalisiert diesen normalvektor, d.h. er skaliert ihn auf die länge 1 == einheitsvektor:
Code: |
normalize( vector v )
length = sqrt( v.x * v.x + v.y * v.y + v.z * v.z )
v.x /= length
v.y /= length
v.z /= length
|
man beachte, dass der code oben ein problem machen könnte im falle dass length == 0 ist.
hth _________________ Yagl - yet another gameprogramming library |
|
Nach oben |
|
 |
Caran

Anmeldungsdatum: 11.03.2007 Beiträge: 290 Wohnort: Lörrach
|
Verfasst am: 29.01.2008, 16:04 Titel: |
|
|
Hoi,
Danke für die Antworten, nun klappts.
Aaaaaaaber hier nicht
Code: |
If IsPointInFrustum(pM->m_pVertices[index].m_location(0),pM->m_pVertices[index].m_location(1),pM->m_pVertices[index].m_location(2)) = 1 Then
glNormal3fv (VarPtr(pTri->m_vertexNormals(k,0)))
glTexCoord2f (pTri->m_s(k), pTri->m_t(k))
glVertex3fv (VarPtr(pM->m_pVertices[index].m_location(0)))
EndIf
|
für alle die den Codeausschnitt nicht kennen: siehe examples\gl\nehe\milkshapemodel.bi
Mache ich da Pointer-mäßig was falsch oder woran liegts?
MfG Caran _________________ Eine Erkenntnis von heute kann die Tochter eines Irrtums von gestern sein. |
|
Nach oben |
|
 |
|
|
Du kannst keine Beiträge in dieses Forum schreiben. Du kannst auf Beiträge in diesem Forum nicht antworten. Du kannst deine Beiträge in diesem Forum nicht bearbeiten. Du kannst deine Beiträge in diesem Forum nicht löschen. Du kannst an Umfragen in diesem Forum nicht mitmachen.
|
|