[B3D] Farbiges Normalmapping / Bumpmapping

Übersicht BlitzBasic Codearchiv

Neue Antwort erstellen

NightPhoenix

Betreff: [B3D] Farbiges Normalmapping / Bumpmapping

BeitragMo, Mai 03, 2010 23:47
Antworten mit Zitat
Benutzer-Profile anzeigen
Ich möchte dem Codearchiv einen etwas älteren Code von mir zum Thema "Farbiges Bumpmapping mit Blitz3D" hinzufügen.
Die Funktion zum Berechnen des Bump Effects habe ich von blitzbasic.com als stabile Basis genommen und sie etwas ausgearbeitet um ein besseres Ergebnis zu erzielen. So fanden Korrekturrechnungen in die Funktion um unsaubere Kanten zu reduzieren.
Das Problem mit dem "farbigen Bumpmapping" habe ich über 2 sehr kleine Texturen gelöst.
Alles in allem ist es recht simpel, aber vielleicht für einige hilfreich. Smile

- Nur eine Lichtquelle
- Farbiges Licht
- Beste Ergebnisse mit direktionaler Lichtquelle [CreateLight(1)]
- Farbe des Ambienten und gestrahlten Lichtes getrennt veränderbar

Beispielbild mit grünem Direkten Licht und rotem Ambienten Licht (Quelle Texturen: Leadwerks-Engine Example, Original "Cobblestones.dds")
user posted image


Hier ein kleiner Beispielcode:

BlitzBasic: [AUSKLAPPEN]
Graphics3D 0, 0, 32, 1
SetBuffer BackBuffer()
AppTitle "Dot3 Bumpmapping"
timer=CreateTimer(60)



Global lnx#
Global lny#
Global lnz#
Global lx#
Global ly#
Global lz#
Global vred
Global vgreen
Global vblue
Global dotproduct#




font=LoadFont ("Arial",60,0,0,0)
font2=LoadFont ("Arial",18,0,0,0)

Camera = CreateCamera()
PositionEntity Camera, 0, 0, -2.5
CameraRange Camera,0.001,1000
CameraClsColor camera,100,100,100

pivo=CreatePivot()

licht=CreateSphere(4,pivo)
ScaleEntity licht,0.1,0.1,0.1
PositionEntity licht,0,2.5,-2.0
Light = CreateLight(1,licht)
LightColor light,180,180,180
RotateEntity light,40,0,0
;PositionEntity light,0,0,-2.0
;PositionEntity light,0,0,-2

lightR=255
lightG=255
lightB=255








colormap=CreateTexture(1,1)
amap=CreateTexture(1,1)
Normalmap = LoadTexture("mapn.dds",1);Normalmap.png", 1) ; normalmap
Texture = LoadTexture("mapd.dds",1);("Texture.png", 1) ; tetxure

TextureBlend amap,3
TextureBlend Colormap,2
TextureBlend Normalmap, 4 ; dot3 bumpmapping
TextureBlend Texture, 2 ; lightmapping

ScaleTexture Texture, 0.3, 0.3 ; dont must be this values
ScaleTexture Normalmap, 1.0, 1.0
ScaleTexture Normalmap, 0.3, 0.3



LockBuffer TextureBuffer (amap)
rgb=0*$1000000 + 50*$10000 + 50*$100 + 50
WritePixelFast 0,0,rgb,TextureBuffer (amap)
UnlockBuffer TextureBuffer (amap)



Dim sphere(5)
For i=1 To 5
sphere(i) = CreateSphere(14)
;EntityShininess sphere,0.9
;Sphere = CreateCube() ; mesh
;EntityParent light,sphere
EntityFX sphere(i), 2 ; vertexcolor

EntityTexture sphere(i), amap, 0, 1
EntityTexture sphere(i), colormap, 0, 2
EntityTexture sphere(i), Normalmap, 0, 0 ; layer 1
EntityTexture sphere(i), Texture, 0, 3 ; layer 2
PositionEntity sphere(i),Rnd(-1,1),Rnd(-1,1),Rnd(0,2)
;ScaleEntity sphere(i),0.8,0.8,0.8
Next









grenze=2


While Not KeyDown(1)
WaitTimer timer


;RotateEntity light, 0, 20, 0

MoveEntity licht, MouseXSpeed()/250.0, -MouseYSpeed()/250.0, 0

If EntityX(licht)>grenze Then PositionEntity licht,grenze,EntityY(licht),EntityZ(licht)
If EntityX(licht)<-grenze Then PositionEntity licht,-grenze,EntityY(licht),EntityZ(licht)
If EntityY(licht)>grenze Then PositionEntity licht,EntityX(licht),grenze,EntityZ(licht)
If EntityY(licht)<-grenze Then PositionEntity licht,EntityX(licht),-grenze,EntityZ(licht)

TurnEntity pivo,0,0,-1


If KeyHit(57) Then
blende=blende+1
If blende>1 Then blende=0

If blende=0 Then TextureBlend texture,2
If blende=1 Then TextureBlend texture,5
EndIf
If KeyHit(28) Then
lightR=Rand(50,255)
lightG=Rand(50,255)
lightB=Rand(50,255)
EndIf

MoveMouse 200,300

For i=1 To 5
UpdateDot3Normals(sphere(i), light, colormap, lightR, LightG, LightB)

If KeyDown(28) Then
;TurnEntity sphere(i),0,0.5,0
;TurnEntity pivo,0,0,-0.2
EndIf
Next


If KeyHit(88) Then
SaveBuffer FrontBuffer(), ("SCREENS\screenshot_"+MilliSecs()+".bmp")
EndIf


RenderWorld()
SetFont font2
Text 0,20,"LEERTASTE drücken um Lichtintensität zu ändern"
Text 0,50,"ENTER drücken um Farbe zu ändern"
Text 0,80,TrisRendered()
Flip
Wend
FreeTimer timer
End

















Function UpdateDot3Normals(Mesh, Light, CM, r, g, b)
;Local LX#, LY#, LZ#, LNX#, LNY#, LNZ#
Local S, Surface, V, VX#, VY#, VZ#, VNX#, VNY#, VNZ#, a, rgb, rgb2
Local Length#, DotProduct2#,DotProduct3#;, DotProduct#, VRed, VGreen, VBlue

; get the lightcoords
lx# = EntityX#(Light, 1)
ly# = EntityY#(Light, 1)
lz# = EntityZ#(Light, 1)

For S = 1 To CountSurfaces(Mesh)
Surface = GetSurface(Mesh, S)
For V = 0 To CountVertices(Surface)-1
; get vertexcoords
VX# = VertexX#(Surface, V)
VY# = VertexY#(Surface, V)
VZ# = VertexZ#(Surface, V)

; get vertexnormalcoords
VNX# = VertexNX#(Surface, V)
VNY# = VertexNY#(Surface, V)
VNZ# = VertexNZ#(Surface, V)

; transform vertexcoords into worldcoords
TFormPoint VX#, VY#, VZ#, Mesh, 0
VX# = TFormedX#()
VY# = TFormedY#()
VZ# = TFormedZ#()

; transform vertexnormalcoords into worldnormalcoords
TFormNormal VNX#, VNY#, VNZ#, Mesh, 0
VNX# = TFormedX#()
VNY# = TFormedY#()
VNZ# = TFormedZ#()

; calculate a vector between vertex and light
lnx# = lx#-VX#
lny# = ly#-VY#
lnz# = lz#-VZ#

; normalize this vector
Length# = Sqr#(lnx#^2.0+lny#^2.0+lnz#^2.0)
lnx# = lnx#/Length#
lny# = lny#/Length#
lnz# = lnz#/Length#

; calculate the dotproduct between vertexnormal and the vector between light and vertex
dotproduct# = VNX#*lnx#+VNY#*lny#+VNZ#*lnz#
;dotproduct# = lnx#^2+lny#^2+lnz#^2
DotProduct2#=dotproduct#


; if the light behind the mesh...
If dotproduct# < 0.0 Then DotProduct2# = 0.0

;dotproduct#=Abs(dotproduct#)+1.38

; calculate the new vertexcolor
vred = ( lnx#*DotProduct2#+1.0)*127.0*1.0 ;*1.0/*0.7 ist hier sozusagen "EntityColor" als faktor -> 1.0 = 255
vgreen = ( lny#*DotProduct2#+1.0)*127.0*1.0 ;127.0 bei x/y = Bump-Tiefe!
vblue = (-lnz#*dotproduct#+1.0)*127.0*0.9 ;127.0 bei z ist Ambientlight

vred=vred^1.12-127
vgreen=vgreen^1.12-127
vblue=vblue^1.12-80

; update vertexcolor
VertexColor Surface, V, vred, vgreen, vblue


a=0


;Lightcolor
LockBuffer TextureBuffer (CM)
rgb=a*$1000000 + r*$10000 + g*$100 + b
WritePixelFast 0,0,rgb,TextureBuffer (CM)


UnlockBuffer TextureBuffer (CM)




Next
Next
End Function



Wie verändert man die Ambientcolor der Objekte?
Direkt nach dem Erstellen der "Amap" findet ihr im Beispielcode die entsprechende Stelle.
Code: [AUSKLAPPEN]
       LockBuffer TextureBuffer (amap)
       rgb=0*$1000000 + 250*$10000 + 5*$100 + 5
       WritePixelFast 0,0,rgb,TextureBuffer (amap)
       UnlockBuffer TextureBuffer (amap)


Wie verändert man die Lightcolor der Objekte?
Die Lichtfarbe wird direkt der Funktion zum berechnen der Bumpmap übergeben. In der Funktion nennt sich die entsprechende Textur dafür "Colormap". Der Name passt mehr oder weniger dazu.
Code: [AUSKLAPPEN]
       LockBuffer TextureBuffer (CM)
       rgb=a*$1000000 + r*$10000 + g*$100 + b
       WritePixelFast 0,0,rgb,TextureBuffer (CM)
      
      
       UnlockBuffer TextureBuffer (CM)






Und das komplette Beispiel als RAR-Archiv
https://www.blitzforum.de/upload/file.php?id=8471


Ich hoffe ich kann einigen Leuten damit helfen. Wink

MfG.

Neue Antwort erstellen


Übersicht BlitzBasic Codearchiv

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group