Buchempfehlung
Mikrocomputertechnik mit Controllern der Atmel AVR-RISC-Familie
Mikrocomputertechnik mit Controllern der Atmel AVR-RISC-Familie
Umfassend, aber leicht verständlich führt dieses Buch in die Programmierung von ATMEL AVR Mikrocontrollern ein. [Mehr Infos...]
FreeBASIC-Chat
Es sind Benutzer im FreeBASIC-Chat online.
(Stand:  )
FreeBASIC bei Twitter
Twitter FreeBASIC-Nachrichten jetzt auch über Twitter erhalten. Follow us!

fb:porticula NoPaste

Info
Info / Hilfe
Liste
Übersicht / Liste
Neu
Datei hochladen
Suche
Quellcode suchen
Download
Dateidownload

TFrustum

Uploader:MitgliedEternal_Pain
Datum/Zeit:23.05.2012 09:24:11

#include once "gl/gl.bi"

#IfnDef TRUE
    #Define TRUE 1
    #DEFINE FALSE 0
#EndIf

enum E_FRUSTUM_SIDE
    E_RIGHT   = 0           '' The RIGHT side of the frustum
    E_LEFT    = 1           '' The LEFT  side of the frustum
    E_BOTTOM  = 2           '' The BOTTOM side of the frustum
    E_TOP     = 3           '' The TOP side of the frustum
    E_BACK    = 4           '' The BACK side of the frustum
    E_FRONT   = 5           '' The FRONT side of the frustum
end enum

enum E_PLANE_DATA
    A = 0                   '' The X value of the plane's normal
    B = 1                   '' The Y value of the plane's normal
    C = 2                   '' The Z value of the plane's normal
    D = 3                   '' The distance the plane is from the origin
end enum

Sub NormalizePlane(frustum() as single, byval side as integer)
    dim as single magnitude = sqr( frustum(side,A) * frustum(side,A) +_
                                   frustum(side,B) * frustum(side,B) +_
                                   frustum(side,C) * frustum(side,C) )
    frustum(side,A) /= magnitude
    frustum(side,B) /= magnitude
    frustum(side,C) /= magnitude
    frustum(side,D) /= magnitude
end sub

Type Frustum
   public:
    Declare Sub Update()

    Declare Function PointIn(byval x as single, byval y as single,byval z as single) as integer
    Declare Function QuadIn(byval x as single, byval z as single, byval sizex as single, byval sizez as single) as integer

   private:
    Planes(6,4) as single
end type

Sub Frustum.Update()
    dim as single proj(0 to 15)          '' This will hold our projection matrix
    dim as single modl(0 to 15)          '' This will hold our modelview matrix
    dim as single clip(0 to 15)          '' This will hold the clipping planes

    glGetFloatv( GL_PROJECTION_MATRIX, @proj(0) )
    glGetFloatv( GL_MODELVIEW_MATRIX, @modl(0) )

    clip( 0) = modl( 0) * proj( 0) + modl( 1) * proj( 4) + modl( 2) * proj( 8) + modl( 3) * proj(12)
    clip( 1) = modl( 0) * proj( 1) + modl( 1) * proj( 5) + modl( 2) * proj( 9) + modl( 3) * proj(13)
    clip( 2) = modl( 0) * proj( 2) + modl( 1) * proj( 6) + modl( 2) * proj(10) + modl( 3) * proj(14)
    clip( 3) = modl( 0) * proj( 3) + modl( 1) * proj( 7) + modl( 2) * proj(11) + modl( 3) * proj(15)

    clip( 4) = modl( 4) * proj( 0) + modl( 5) * proj( 4) + modl( 6) * proj( 8) + modl( 7) * proj(12)
    clip( 5) = modl( 4) * proj( 1) + modl( 5) * proj( 5) + modl( 6) * proj( 9) + modl( 7) * proj(13)
    clip( 6) = modl( 4) * proj( 2) + modl( 5) * proj( 6) + modl( 6) * proj(10) + modl( 7) * proj(14)
    clip( 7) = modl( 4) * proj( 3) + modl( 5) * proj( 7) + modl( 6) * proj(11) + modl( 7) * proj(15)

    clip( 8) = modl( 8) * proj( 0) + modl( 9) * proj( 4) + modl(10) * proj( 8) + modl(11) * proj(12)
    clip( 9) = modl( 8) * proj( 1) + modl( 9) * proj( 5) + modl(10) * proj( 9) + modl(11) * proj(13)
    clip(10) = modl( 8) * proj( 2) + modl( 9) * proj( 6) + modl(10) * proj(10) + modl(11) * proj(14)
    clip(11) = modl( 8) * proj( 3) + modl( 9) * proj( 7) + modl(10) * proj(11) + modl(11) * proj(15)

    clip(12) = modl(12) * proj( 0) + modl(13) * proj( 4) + modl(14) * proj( 8) + modl(15) * proj(12)
    clip(13) = modl(12) * proj( 1) + modl(13) * proj( 5) + modl(14) * proj( 9) + modl(15) * proj(13)
    clip(14) = modl(12) * proj( 2) + modl(13) * proj( 6) + modl(14) * proj(10) + modl(15) * proj(14)
    clip(15) = modl(12) * proj( 3) + modl(13) * proj( 7) + modl(14) * proj(11) + modl(15) * proj(15)

    Planes(E_RIGHT,A) = clip( 3) - clip( 0) : Planes(E_RIGHT,B) = clip( 7) - clip( 4)
    Planes(E_RIGHT,C) = clip(11) - clip( 8) : Planes(E_RIGHT,D) = clip(15) - clip(12)
    NormalizePlane(Planes(), E_RIGHT)

    Planes(E_LEFT,A) = clip( 3) + clip( 0) : Planes(E_LEFT,B) = clip( 7) + clip( 4)
    Planes(E_LEFT,C) = clip(11) + clip( 8) : Planes(E_LEFT,D) = clip(15) + clip(12)
    NormalizePlane(Planes(), E_LEFT)

    Planes(E_BOTTOM,A) = clip( 3) + clip( 1) : Planes(E_BOTTOM,B) = clip( 7) + clip( 5)
    Planes(E_BOTTOM,C) = clip(11) + clip( 9) : Planes(E_BOTTOM,D) = clip(15) + clip(13)
    NormalizePlane(Planes(), E_BOTTOM)

    Planes(E_TOP,A) = clip( 3) - clip( 1) : Planes(E_TOP,B) = clip( 7) - clip( 5)
    Planes(E_TOP,C) = clip(11) - clip( 9) : Planes(E_TOP,D) = clip(15) - clip(13)
    NormalizePlane(Planes(), E_TOP)

    Planes(E_BACK,A) = clip( 3) - clip( 2) : Planes(E_BACK,B) = clip( 7) - clip( 6)
    Planes(E_BACK,C) = clip(11) - clip(10) : Planes(E_BACK,D) = clip(15) - clip(14)
    NormalizePlane(Planes(), E_BACK)

    Planes(E_FRONT,A) = clip( 3) + clip( 2) : Planes(E_FRONT,B) = clip( 7) + clip( 6)
    Planes(E_FRONT,C) = clip(11) + clip(10) : Planes(E_FRONT,D) = clip(15) + clip(14)
    NormalizePlane(Planes(), E_FRONT)
End Sub

function Frustum.PointIn( byval x as single, byval y as single, byval z as single ) as integer
    for i as integer = 0 to 5
        if (Planes(i,A) * x + Planes(i,B) * y + Planes(i,C) * z + Planes(i,D) <= 0) then
            return FALSE
        end if
    next i
    return TRUE
End Function

Function Frustum.QuadIn(byval x as single, byval z as single, byval sizex as single, byval sizez as single) as integer
    Dim as single xx,yy,zz
    For i as integer = 0 to 5
        '' E_TOP_RIGHT
            xx = x + sizex
            yy = 0
            zz = z - sizez
            if (Planes(i,A) * xx + Planes(i,B) * yy + Planes(i,C) * zz + Planes(i,D) > 0) then
                continue for
            end if

        '' E_TOP_LEFT
            xx = x - sizex
            yy = 0
            zz = z - sizez
            if (Planes(i,A) * xx + Planes(i,B) * yy + Planes(i,C) * zz + Planes(i,D) > 0) then
                continue for
            end if

        '' E_BOTTOM_RIGHT
            xx = x + sizex
            yy = 0
            zz = z + sizez
            if (Planes(i,A) * xx + Planes(i,B) * yy + Planes(i,C) * zz + Planes(i,D) > 0) then
                continue for
            end if

        '' E_BOTTOM_LEFT
            xx = x - sizex
            yy = 0
            zz = z + sizez
            if (Planes(i,A) * xx + Planes(i,B) * yy + Planes(i,C) * zz + Planes(i,D) > 0) then
                continue for
            end if

        return false
    next i
    return TRUE
End Function