Kleine Helfer Funktionen und Fixes

Übersicht BlitzBasic Codearchiv

Neue Antwort erstellen

Xaymar

ehemals "Cgamer"

Betreff: Kleine Helfer Funktionen und Fixes

BeitragSa, Aug 28, 2010 20:21
Antworten mit Zitat
Benutzer-Profile anzeigen
~Helfer~
Math_Max/Min ( Value, Max/Min )
->Dasselbe wie If Value < Min then Value = Min ElseIf Value > Max Then Value = Max
Math_MaxMin ( Value, Max, Min )
->Kombination aus Math_Max und Math_Min.
Math_Clip ( Value, Low, High )
->Begrenzt einen Wert, so dass er nie über {High} und unter {Low} sein kann.
Math_RGBHSV ( R, G, B )
->Wandelt R G B in H S V um. Array HSV#[2], S und V sind von 0 bis 1
Math_HSVRGB ( H, S, V )
->Wandelt H S V in R G B um. Array RGB#[2].
EntityRenderToImage ( iCam, iEnt, iImg )
->Rendert ein Entity in ein Bild. ERTPos#[2] setzt die Renderposition(um der Welt aus dem Weg zu gehen), ERTRot#[2] die Rotation. iEnt darf nur per ScaleEntity skaliert werden. iCam muss eine Min-Range von 0.005 haben.
EntityRenderToTexture ( iCam, iEnt, iImg )
->Rendert ein Entity in eine Textur. Siehe EntityRenderToImage
EntityScale (Entity, Axis )
->Wird von EntityRenderTo* benötigt um die Distanz zu errechnen.
SplitString(String$, Splitter$)
->Splitted einen String an dem Zeichen Splitter$(Standard "|"). Mit SplitCount und SplittedString habt ihr Zugriff auf das was rauskommt.
SafeText(String$)
->Ersetzt nicht darstellbare Zeichen mit [ASCII-Code].

BlitzBasic: [AUSKLAPPEN]
;[Block] Rendering Functions
Global ERTPos#[2], ERTRot#[2]
Const ES_AxisX = 0, ES_AxisY = 1, ES_AxisZ = 2
;[End Block]
Function EntityRenderToImage(iCam, iEnt, iImg)
Local CP#[2], EP#[2], CR#[2], Buffer = GraphicsBuffer(), SX#, SY#, SZ#, Dist#, IW = ImageWidth(iImg), IH = ImageHeight(iImg), IB = ImageBuffer(iImg)

CP[0] = EntityX(iCam):CP[1] = EntityY(iCam):CP[2] = EntityZ(iCam)
CR[0] = EntityPitch(iCam):CR[1] = EntityYaw(iCam):CR[2] = EntityRoll(iCam)
EP[0] = EntityX(iEnt):EP[1] = EntityY(iEnt):EP[2] = EntityZ(iEnt)

SX = EntityScale(iEnt,ES_AxisX):SY = EntityScale(iEnt,ES_AxisY):SZ = EntityScale(iEnt,ES_AxisZ)
Dist# = Sqr((SX*SX)+(SY*SY)+(SZ*SZ))

PositionEntity iCam, ERTPos[0], ERTPos[1], ERTPos[2]
PositionEntity iEnt, ERTPos[0], ERTPos[1], ERTPos[2]+Dist
RotateEntity iCam, 0, 0, 0
EntityParent iEnt, iCam
RotateEntity iCam, ERTRot[0], ERTRot[1], ERTRot[2]

CameraViewport iCam, 0, 0, IW, IH
RenderWorld
CopyRect 0,0,IW,IH,0,0,Buffer,IB
CameraViewport iCam, 0, 0, GraphicsWidth(), GraphicsHeight()

RotateEntity iCam, 0, 0, 0
EntityParent iEnt, 0

PositionEntity iCam, CP[0],CP[1],CP[2]
PositionEntity iEnt, EP[0],EP[1],EP[2]
RotateEntity iCam, CR[0],CR[1],CR[2]
End Function
Function EntityRenderToTexture(iCam, iEnt, iTex)
Local CP#[2], EP#[2], CR#[2], Buffer = GraphicsBuffer(), SX#, SY#, SZ#, Dist#, IW = TextureWidth(iTex), IH = TextureHeight(iTex), IB = TextureBuffer(iTex)

CP[0] = EntityX(iCam):CP[1] = EntityY(iCam):CP[2] = EntityZ(iCam)
CR[0] = EntityPitch(iCam):CR[1] = EntityYaw(iCam):CR[2] = EntityRoll(iCam)
EP[0] = EntityX(iEnt):EP[1] = EntityY(iEnt):EP[2] = EntityZ(iEnt)

SX = EntityScale(iEnt,ES_AxisX):SY = EntityScale(iEnt,ES_AxisY):SZ = EntityScale(iEnt,ES_AxisZ)
Dist# = Sqr((SX*SX)+(SY*SY)+(SZ*SZ))*0.25 + Sqr(Sqr((SX*SX)+(SY*SY)+(SZ*SZ)))*0.75

PositionEntity iCam, ERTPos[0], ERTPos[1], ERTPos[2]
PositionEntity iEnt, ERTPos[0], ERTPos[1], ERTPos[2]+Dist
RotateEntity iCam, 0, 0, 0
EntityParent iEnt, iCam
RotateEntity iCam, ERTRot[0], ERTRot[1], ERTRot[2]

CameraViewport iCam, 0, 0, IW, IH
;SetBuffer IB ;FastEXT only
RenderWorld
CopyRect 0,0,IW,IH,0,0,Buffer,IB
CameraViewport iCam, 0, 0, GraphicsWidth(), GraphicsHeight()
;SetBuffer Buffer ;FastEXT only

RotateEntity iCam, 0, 0, 0
EntityParent iEnt, 0

PositionEntity iCam, CP[0],CP[1],CP[2]
PositionEntity iEnt, EP[0],EP[1],EP[2]
RotateEntity iCam, CR[0],CR[1],CR[2]
End Function
Function EntityScale#( Entity, Axis )
VX# = GetMatElement( Entity, Axis, 0 )
VY# = GetMatElement( Entity, Axis, 1 )
VZ# = GetMatElement( Entity, Axis, 2 )
Return Sqr( VX#*VX# + VY#*VY# + VZ#*VZ# )
End Function

;[Block] Math Functions
Global HSV#[2], RGB#[2]
;[End Block]
Function Math_MaxMin#(Value#, Max#, Min#)
If Value> Max Then Return Max
If Value < Min Then Return Min
Return Value
End Function
Function Math_Max#(Value#, Max#)
If Value> Max Then Return Max
Return Value
End Function
Function Math_Min#(Value#, Min#)
If Value < Min Then Return Min
Return Value
End Function
Function Math_Clip#(Value#, Low#, High#)
Local Out#, Diff#
Diff = High-Low:Out = Value-Low
If (Out >= Diff) Then Out = Out - Floor(Out/Diff)*Diff
If (Out < 0) Then Out = Out - Floor(Out/Diff)*Diff
Return Low+Out
End Function
Function Math_RGBHSV(R,G,B)
Local maxC#, minC#, delta#, dr#, dg#, db#

R = R/255.0:G = G/255.0:B = B/255.0
maxC = Math_Min(Math_Min(R,G),B)
minC = Math_Max(Math_Max(R,G),B)
delta = maxC - minC
HSV[0] = 0:HSV[1] = 0:HSV[2] = maxC

If delta = 0
HSV[0] = 0:HSV[1] = 0
Else
HSV[1] = delta / maxC
dr = 60*(maxC - R)/delta + 180
dg = 60*(maxC - G)/delta + 180
db = 60*(maxC - B)/delta + 180
If R = maxC
HSV[0] = db - dg
ElseIf G = maxC
HSV[0] = 120 + dr - db
Else
HSV[0] = 240 + dg - dr
EndIf
EndIf
HSV[0] = Math_Clp(HSV[0],0,360)
End Function
Function Math_HSVRGB(H#,S#,V#)
Local m#, n#, f#, i

H = Math_Clp(H,0,360)/60.0
If H = S And S = 0
RGB[0] = V
RGB[1] = V
RGB[2] = V
EndIf
i = Floor(H)
f = H - i
If Not (i Mod 2) Then f = 1 - f
m = V * (1-S)
n = V * (1-S*f)
Select i
Case 6,0
RGB[0] = V*255
RGB[1] = n*255
RGB[2] = m*255
Case 1
RGB[0] = n*255
RGB[1] = V*255
RGB[2] = m*255
Case 2
RGB[0] = m*255
RGB[1] = V*255
RGB[2] = n*255
Case 3
RGB[0] = m*255
RGB[1] = n*255
RGB[2] = V*255
Case 4
RGB[0] = n*255
RGB[1] = m*255
RGB[2] = V*255
Case 5
RGB[0] = V*255
RGB[1] = m*255
RGB[2] = n*255
End Select
End Function

;[Block] String Functions
Dim SplittedString$(1)
Global SplitCount
;[End Block]
Function SplitString(In$, StringSplitter$ = "|")
Local InLength% = Len(In)
Local SplitLength% = Len(StringSplitter)
Local CountPos%, InPos%, SplitIndex%
Local SplitTest$, LineText$

; Count how many Lines there are and resize Dim.
SplitCount = 0
For CountPos = 1 To InLength-(SplitLength-1)
SplitTest = Mid(In,CountPos,1)
If SplitTest = StringSplitter Then SplitCount = SplitCount + 1
Next
Dim SplittedString(SplitCount)

; Split the Text onto multiple lines.
While Not InPos = Len(In)
; Increment Position
InPos = InPos + 1

; Grab a piece of the text.
SplitTest = Mid(In, InPos, SplitLength)
Local Char$ = Left(SplitTest, 1)

; Check if the current Text matches the splitter or if we are near the end.
If SplitTest = StringSplitter Or InPos = InLength
; Append the current character if it doesn't match the Splitter.
If InPos = InLength And SplitTest <> StringSplitter Then LineText = LineText + Char

; Store the Line.
SplittedString(SplitIndex) = LineText

; Increment split index.
SplitIndex = SplitIndex + 1

; Reset LineText
LineText = ""
Else
LineText = LineText + Char
EndIf
Wend
End Function

Function SafeText$(sText$)
Local sSafeText$ = sText
For i = 0 To 31
sSafeText = Replace(sSafeText,Chr(i),"["+i+"]")
Next
Return sSafeText
End Function


~Fixes~
Replace$ ( String$, From$, To$ )
->Behebt den Chr(0) Bug.

BlitzBasic: [AUSKLAPPEN]
Function Replace$(S$,F$,T$,CaseSensitive=0)
Local LF = Len(F), Pos
Pos = 1
While Not Pos > Len(S)-LF+1
Local Check$ = Mid(S,Pos,LF)
If Lower(F) = Lower(Check) And (F = Check Or CaseSensitive=0)
S = Left(S,Pos-1)+T+Mid(S,Pos+LF,-1)
Pos = Pos + Len(T)
EndIf
Pos = Pos + 1
Wend
Return S
End Function


~Changelog~
08.02.2014 00:14 - SplitString ünterstützt nun längere Zeilentrenner
11.12.2011 10:34 - Fixed Math_Clip
29.08.2010 13:18 - Added (Helper) SplitString
29.08.2010 00:53 - Fixed (Helper) EntityScale
28.08.2010 20:58 - Fixed (Fix) Replace
  • Zuletzt bearbeitet von Xaymar am So, Feb 09, 2014 1:15, insgesamt 7-mal bearbeitet
 

n-Halbleiter

BeitragSo, Aug 29, 2010 0:49
Antworten mit Zitat
Benutzer-Profile anzeigen
Beim schnellen Überfliegen habe ich gemerkt, dass du die Funktion EntityScale nicht im Code eingebunden hast. So, wie ich das sehe, dürfte BlitzBasic: [AUSKLAPPEN]
End Function
Function EntityScale#( Entity, Axis )

;[Block] Math Functions
Probleme bereiten.
mfg, Calvin
Maschine: Intel Core2 Duo E6750, 4GB DDR2-Ram, ATI Radeon HD4850, Win 7 x64 und Ubuntu 12.04 64-Bit
Ploing!
Blog

"Die Seele einer jeden Ordnung ist ein großer Papierkorb." - Kurt Tucholsky (09.01.1890 - 21.12.1935)

Xaymar

ehemals "Cgamer"

BeitragSo, Aug 29, 2010 0:54
Antworten mit Zitat
Benutzer-Profile anzeigen
Danke für die Info. IDEal macht anscheinend bei eingeklappten Funktionen nicht ganz mit.
Warbseite

Xaymar

ehemals "Cgamer"

BeitragMi, Aug 15, 2012 6:14
Antworten mit Zitat
Benutzer-Profile anzeigen
Neue Funktion: SafeText - Ersetzt nicht darstellbare Zeichen so dass diese angezeigt werden können, wenn auch nicht schön.
Bugfix: Math_Clip hat vorher beim ausgeben sofern Low unter 0 lag immer herum gesponnen, dies wurde behoben.(Beispiel: 0,-180,180 = 180,-180,180 = 0,-180,180, ...)
Änderung: SplitString nimmt nun den Splitter aus den Parametern anstatt einer Globale.
Warbseite

Neue Antwort erstellen


Übersicht BlitzBasic Codearchiv

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group