Irrlicht -> 3D Model Alpha blending

Fragen zu Grafik- & Soundproblemen und zur Spieleprogrammierung haben hier ihren Platz.
Benutzeravatar
Max_der_Held
Beiträge: 595
Registriert: 18.04.2006 17:01
Wohnort: Bavaria
Kontaktdaten:

Irrlicht -> 3D Model Alpha blending

Beitrag von Max_der_Held »

Hallo Folks :)

habe eine Methode entdeckt mit der man 3D objekte schön transparent machen kann, in Echtzeit, für Irrlicht.
Diese will ich hier jetzt einmal mit euch teilen. vielleicht kann mans ja noch optimieren ;) :)

das ganze sieht so aus: (Ist von mir schnell zusammengeschustert -> nicht unbedingt auf Codeleslichkeit etc. optimiert.. läuft nur mit irrlicht)

Ein problem gibts allerdings dabei: mit dem materialtype #irr_emt_vertex_alpha wird im mesh der mesh- z-buffer nicht mehr richtig gehandhabt.. wer will kann mal sydney laden und sieht was das heist.

Code: Alles auswählen

XIncludeFile #PB_Compiler_Home + "includes\irrlichtwrapper_include.pbi"

If Not IrrStartEx( #irr_edt_opengl , 800 , 600 , 0 , 0 , 0 , 32,1,0,1,0)
  End
EndIf

   IrrSetWindowCaption( "Aigner, Max -> Alpha irrlicht test." )
   SetCurrentDirectory("media/") 
   
;----------------------------------------
; create light and set position
*light = IrrAddLight(1,1,23 ,1,1,1, 2000)
*light = IrrAddLight(23,1,1 ,1,1,1, 2000)
*light = IrrAddLight(1,23,1 ,1,1,1, 2000)

; create Texture
   CreateImage ( 0 , 256,256,32)
   If StartDrawing( ImageOutput(0))
      Box ( 0 , 0 , ImageWidth( 0 ) , ImageHeight ( 0 ) , 255+255<<8 + 255<<16) ; weißer hintergrund.
      For x = 1 To 500
         Line(Random(256) , Random(256), Random(512) -Random(256) , Random(512) - Random(256)  , RGB( Random(128) + 128 , Random(128) +128 , Random(255)))
      Next 
      StopDrawing()
   EndIf 

   texture = IrrPBImage2Texture( 0 , "hanse" , 1)
   FreeImage ( 0 )
   
; create 2. texture 
   
   CreateImage ( 0 , 256,256,32)
   If StartDrawing( ImageOutput(0))
      Box ( 0 , 0 , ImageWidth( 0 ) , ImageHeight ( 0 ) , 255+255<<8 + 255<<16) ; weißer hintergrund.
      For x = 1 To 256
         Line ( x , Random(256) ,Random(300) , Random(300) ,RGB( Random(128)+128,Random(255),255))
      Next
      StopDrawing()
   EndIf 

   texture2 = IrrPBImage2Texture( 0 , "hanse" , 1)
   FreeImage ( 0 )
   
; create hintergrund mesh
*cube = IrrAddCubeSceneNode ( 1 )
IrrSetNodePosition          ( *cube , 0,0,10)
IrrSetNodeScale             ( *cube , 100,100,1)
IrrSetNodeMaterialTexture   ( *cube , texture , 0)
IrrSetNodeMaterialTexture   ( *cube , texture2 , 1)
IrrSetNodeMaterialType      ( *cube , #IRR_EMT_REFLECTION_2_LAYER)

IrrSetNodeMaterialFlag      ( *cube , #IRR_EMF_LIGHTING ,0)

; 2. cube.. der transparent wird
 *cube = IrrAddCubeSceneNode(1.0)
IrrSetNodeScale(*cube, 3,3,3)
IrrSetNodeMaterialTexture(*cube, texture, 0) 

*material = IrrGetNodeMaterial(*cube, 0)
IrrSetNodeMaterialType(*cube, #irr_EMT_TRANSPARENT_VERTEX_ALPHA )
IrrSetMaterialDiffuseColor( *material , 255,255,255,255)
; IrrSetNodeMaterialFlag(*cube, #IRR_EMF_LIGHTING , 1)
*cam = IrrAddFPSCamera(0,200,10 )
IrrSetNodePosition(*cam, 0,3,-12)

*text = IrrGuiAddStaticText(  "Drücke T oder G, um den Alpha Wert zu ändern.",5,5,200,20)
Alpha = 128
IrrSetMaterialDiffuseColor( *material , Alpha , 255,255,255)
; ---------------------------------------
;           main loop
; ---------------------------------------

Repeat

  rotx.f + 0.05
  IrrSetNodeRotation (*cube, 0,rotx,0)
  
  ; settransparenz
  If GetAsyncKeyState_( #VK_T) Or GetAsyncKeyState_(#VK_ADD) Or GetAsyncKeyState_(#VK_SPACE)
    Alpha + 5
    If Alpha > 255: Alpha = 255 : EndIf 
    IrrSetMaterialDiffuseColor( *material , Alpha , 255,255,255)
  EndIf
  If GetAsyncKeyState_(#VK_G) Or GetAsyncKeyState_( #VK_SUBTRACT) Or GetAsyncKeyState_(#VK_RETURN)
    Alpha - 5
    If Alpha < 0: Alpha = 0 : EndIf 
    IrrSetMaterialDiffuseColor( *material , Alpha , 255,255,255)
  EndIf
  

	; if Escape Key, exit	
  If GetAsyncKeyState_(#VK_ESCAPE)
    Quit=1
  EndIf



  	; ---------------
  	;      Render
  	; --------------
   
      IrrRunning    ( )
      IrrBeginScene ( 255,255,255 )
      IrrDrawScene  ( )
      IrrDrawGUI    ( )
      IrrEndScene   ( )

Until Quit=1
; end
IrrStop()
-mfg max