;//////////////////////////////////////////////////////////////// ;// ;// Project Title: MP 3D Engine ;// File Title: MP_ShaderEditor.pb ;// Created On: 16.02.2012 ;// Updated On: ;// Author: Michael Paulwitz ;// OS:Windows ;// ;// Shadereditor to edit/create Shader ;// ;//////////////////////////////////////////////////////////////// ;- Gadget Constants ; Enumeration #Button_0 #Button_1 #Button_2 #Button_3 #Button_4 #Button_5 #Button_6 #Button_7 #Editor_0 #TrackBar_0 #TrackBar_1 #TrackBar_2 #Text_0 #Text_1 #Text_2 #Text_3 #Text_4 #Text_5 #OptionG_0 #OptionG_1 #Statusbar EndEnumeration Dim Texture (9) Var1.f = 0.5 Var2.f = 0.5 Var3.f = 0.5 Dim Mouse.f (2) Dim Worldmat.f(16) Procedure checkFloatInput(gadget) SendMessage_(GadgetID(gadget),#EM_GETSEL,@start,0) txt$ = GetGadgetText(gadget) *p.Character = @txt$ While *p\c = '.' Or (*p\c >= '0' And *p\c <= '9') If *p\c = '.' pointcount+1 If pointcount > 1 : *p + SizeOf(Character) : start-1 : Continue : EndIf EndIf new$ + Chr(*p\c) : *p + SizeOf(Character) Wend SetGadgetText(gadget,new$) SendMessage_(GadgetID(gadget),#EM_SETSEL,start,start) EndProcedure Procedure ChangeVertexColor (mesh) For n = 0 To MP_CountVertices(mesh)-1 Step 8 MP_VertexSetColor(mesh, n ,MP_ARGB (255, 0,0,0)) MP_VertexSetColor(mesh, n+1 ,MP_ARGB (255, 255,0,0)) MP_VertexSetColor(mesh, n+2 ,MP_ARGB (255, 0,255,0)) MP_VertexSetColor(mesh, n+3 ,MP_ARGB (255, 0,0,255)) MP_VertexSetColor(mesh, n+4 ,MP_ARGB (255, 255,255,0)) MP_VertexSetColor(mesh, n+5 ,MP_ARGB (255, 255,0,255)) MP_VertexSetColor(mesh, n+6 ,MP_ARGB (255, 0, 255,255)) MP_VertexSetColor(mesh, n+7 ,MP_ARGB (255, 255, 255,255)) Next EndProcedure Procedure StringGadgetCursorX(Gadget) SendMessage_(GadgetID(Gadget),#EM_GETSEL,@Min,@Max) ProcedureReturn Max-SendMessage_(GadgetID(Gadget),#EM_LINEINDEX,SendMessage_(GadgetID(Gadget),#EM_LINEFROMCHAR,Min,0),0)+1 EndProcedure Procedure StringGadgetCursorY(Gadget) SendMessage_(GadgetID(Gadget),#EM_GETSEL,@Min,@Max) ProcedureReturn SendMessage_(GadgetID(Gadget),#EM_LINEFROMCHAR,Min,0)+1 EndProcedure Procedure EditorGadgetLocate(Gadget,x,y) ; Set cursor position REG = GadgetID(Gadget) CharIdx = SendMessage_(REG,#EM_LINEINDEX,y-1,0) LLength = SendMessage_(REG,#EM_LINELENGTH,CharIdx,0) If LLength >= x-1 CharIdx + x-1 EndIf Range.CHARRANGE Range\cpMin = CharIdx Range\cpMax = CharIdx SendMessage_(REG,#EM_EXSETSEL,0,Range) EndProcedure MP_Graphics3D (933,511,0,2) ; Erstelle ein WindowsFenster mit 3D Funktion #Window = 0 SetWindowTitle(0, "Editor to test HLSL Shader V0.4") ; Setzt einen Fensternamen ButtonGadget(#Button_0, 510, 20, 140, 30, "Load Mesh") ButtonGadget(#Button_1, 510, 70, 100, 30, "Load Texture Nr:") SpinGadget(#Button_5, 620, 74, 30, 24, 0, 9 , #PB_Spin_Numeric ) SetGadgetState (#Button_5, 0) ;: SetGadgetText(0, "0") ButtonGadget(#Button_2, 510, 140, 140, 30, "Load Shader") ButtonGadget(#Button_3, 510, 190, 140, 30, "Save Shader") ButtonGadget(#Button_4, 680, 20, 130, 30, "Help") OptionGadget(#OptionG_0, 680, 70, 130, 20, "Vertex / Pixel Shader") OptionGadget(#OptionG_1,680, 90, 130, 20, "Postprocessing Shader") SetGadgetState(#OptionG_0, 1) ;ButtonGadget(#Button_5, 680, 70, 130, 30, "TextureA on Screen") ButtonGadget(#Button_6, 680, 140, 130, 30, "Reset") ButtonGadget(#Button_7, 680, 190, 130, 30, "Start Shader") EditorGadget(#Editor_0, 510, 250, 400, 220) TextGadget(#Text_0, 820, 20, 40, 20, "Var1") TrackBarGadget(#TrackBar_0, 825, 40, 30, 180, 0, 100, #PB_TrackBar_Vertical);#PB_TrackBar_Ticks | #PB_TrackBar_Vertical) SetGadgetState(#TrackBar_0, 50) TextGadget(#Text_1, 860, 20, 40, 20, "Var2") TrackBarGadget(#TrackBar_1, 860, 40, 30, 180, 0, 100, #PB_TrackBar_Vertical);#PB_TrackBar_Ticks | #PB_TrackBar_Vertical) SetGadgetState(#TrackBar_1, 50) TextGadget(#Text_2, 900, 20, 40, 20, "Var3") TrackBarGadget(#TrackBar_2, 895, 40, 30, 180, 0, 100, #PB_TrackBar_Vertical);#PB_TrackBar_Ticks | #PB_TrackBar_Vertical) SetGadgetState(#TrackBar_2, 50) TextGadget(#Text_3, 510, 230, 400, 20, "Shadername: ") CreateStatusBar(#Statusbar, WindowID(0)) AddStatusBarField(90) AddStatusBarField(100) AddStatusBarField(#PB_Ignore ) MP_Viewport(20,20,470,470) ;directory$ = "C:\Programme\PureBasic\Examples\MP3D Demos\Texture_Shadereffecte\" camera=MP_CreateCamera() light=MP_CreateLight(1) Mesh = MP_CreateCube() ChangeVertexColor (Mesh) ;MP_EntitySetColor (Mesh,MP_ARGB(255,Random(155)+100,Random(155)+100,Random(255)+100)) MP_PositionEntity (Mesh,0,0,4) MP_RotateEntity(mesh,24,-76,44) BackTexture = MP_CreateBackBufferTexture() While Not abfrage; Not MP_KeyDown(#PB_Key_Escape) ; Esc abfrage / SC pushed? Select WindowEvent() Case #PB_Event_Gadget If EventType()=#PB_EventType_Change If EventGadget() = #Text_4 checkFloatInput(#Text_4) EndIf EndIf Select EventGadget() Case #Button_0 Pattern$ = "3D mesh files|*.x;*.3ds;*.b3d|.x files (*.x)|*.x|3DS files (*.3ds)|*.3ds|B3D files (*.b3d)|*.b3d" ;directory$ = "C:\Programme\PureBasic\media\" File.s = OpenFileRequester("Load mesh file", directory$, Pattern$, 0) If File.s MP_FreeEntity (mesh) mesh = MP_LoadMesh (File.s) max.f = MP_MeshGetHeight(mesh) ; find Maximum of Mesh If MP_MeshGetWidth(mesh) > max : max = MP_MeshGetWidth(mesh) : EndIf If MP_MeshGetDepth(mesh) > max : max = MP_MeshGetDepth(mesh) : EndIf scale.f = 3 / max ; MP_ScaleEntity (mesh,scale,scale,scale) ; Auf Bildschirm maximieren / maximum to Screen MP_PositionEntity (Mesh,0,0,4) EndIf Case #Button_1 Pattern$ = "Image File (bmp,jpg,tga,png,dds,ppm,dib,hdr,pfm|*.bmp;*.jpg;*.tga;*.png;*.dds;*.ppm;*.dib;*.hdr;*.pfm" TexturFile.s = OpenFileRequester("Load Texture", directory$, Pattern$, 0) If TexturFile MP_FreeTexture (Texture(GetGadgetState (#Button_5))) Texture(GetGadgetState (#Button_5)) = MP_LoadTexture(TexturFile) If GetGadgetState (#Button_5) = 0 MP_EntitySetTexture (mesh, Texture(0)) ; MP_MaterialDiffuseColor (Texture(0),255,128,128,128) ; MP_MaterialAmbientColor (Texture(0), 255, 155 , 255, 255) ; ; MP_MaterialEmissiveColor (Texture(0),155,15,25,25) ; ; MP_MaterialSpecularColor (Texture(0), 255, 255 ,255, 255,20) EndIf EndIf Case #Button_2 File.s = OpenFileRequester("Load Shader FX file", directory$, "Shader file (fx)|*.fx", 0) If File If ReadFile(0, File) ; wenn die Datei geöffnet werden konnte, setzen wir fort... MyEffect.s = "" While Eof(0) = 0 ; sich wiederholende Schleife bis das Ende der Datei ("end of file") erreicht ist MyEffect.s + ReadString(0) + Chr(10) ; Zeile für Zeile im Debugger-Fenster anzeigen Wend CloseFile(0) ClearGadgetItems(#Editor_0) ; schließen der zuvor geöffneten Datei AddGadgetItem(#Editor_0, 0,MyEffect) EndIf SetGadgetText(#Text_3, "Shadername: "+GetFilePart(file)) MP_FreeShader(MyShader) SetGadgetText(#Button_7, "Start Shader") MyShader = 0 MP_ShaderSetEntity (0, Mesh) EndIf Case #Button_3 File.s = SaveFileRequester("Save Shader file", directory$+"\"+File, "Shader files (*.fx)|*.fx", 0) If File MyEffect.s = GetGadgetText(#Editor_0) If CreateFile(0, File) WriteString(0, MyEffect) CloseFile(0) EndIf SetGadgetText(#Text_3, "Shadername: "+GetFilePart(file)) EndIf Case #Button_4 If OpenWindow(1, 0, 0, 360, 480, "Help", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) EditorGadget(22, 10, 10, 340, 460,#PB_Editor_ReadOnly) While Helptxt.s <> "End" Text.s + Helptxt Read.s Helptxt.s Wend AddGadgetItem(22, 0, Text) Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow CloseWindow(1) EndIf ; Case #Button_5 Case #Button_6 For n = 0 To 9 MP_FreeTexture (Texture(n)) Texture(n) = 0 Next n MP_EntitySetTexture (mesh, 0) MP_FreeShader(MyShader) SetGadgetText(#Button_7, "Start Shader") MyShader = 0 Postprocessing = 0 MP_ShaderSetEntity (0, Mesh) MP_FreeEntity (mesh) Mesh = MP_CreateCube() ChangeVertexColor (Mesh) ;MP_EntitySetColor (Mesh,MP_ARGB(255,Random(155)+100,Random(155)+100,Random(255)+100)) MP_PositionEntity (Mesh,0,0,4) MP_RotateEntity(mesh,24,-76,44) Case #Button_7 If MyShader MP_FreeShader(MyShader) SetGadgetText(#Button_7, "Start Shader") MyShader = 0 Postprocessing = 0 MP_ShaderSetEntity (0, Mesh) Else SetGadgetText(#Button_7, "Stop Shader") MyEffect.s = GetGadgetText(#Editor_0) MyShader = MP_CreateMyShader (MyEffect.s) If Not MyShader ; Bei einem Fehler springe an position errortxt.s = MP_GetLastError() found = FindString(errortxt, "memory(", 1) If found start = FindString(errortxt, ",", found) stop = FindString(errortxt, ")", found) ;Debug errortxt SetActiveGadget(#Editor_0) EditorGadgetLocate(#Editor_0,Val(Mid(errortxt,start + 1, stop - 1 - start)),Val(Mid(errortxt,found + 7, start - 7 - found))) StatusBarText(#StatusBar, 2, "Last error: "+ Mid (errortxt, found+6)) EndIf EndIf If MyShader If GetGadgetState(#OptionG_0) MP_ShaderSetEntity (MyShader,Mesh) MP_SetTechniqueMyShader (MyShader,"Technique0") Else Postprocessing = 1 EndIf StatusBarText(#StatusBar, 2, "") Else MP_ShaderSetEntity (0, Mesh) ;MessageRequester("Shader Error", "No Shader loaded") SetGadgetText(#Button_7, "Start Shader") EndIf EndIf Case #TrackBar_0 Var1.f = GetGadgetState(#TrackBar_0)/100 Case #TrackBar_1 Var2.f = GetGadgetState(#TrackBar_1)/100 Case #TrackBar_2 Var3.f = GetGadgetState(#TrackBar_2)/100 EndSelect Case #PB_Event_CloseWindow If MessageRequester(" program end", "You want to close the program?:", #PB_MessageRequester_YesNo) = #PB_MessageRequester_Yes End EndIf EndSelect StatusBarText(#StatusBar, 0, " line = "+Str( StringGadgetCursorY(#Editor_0))) StatusBarText(#StatusBar, 1, " pos = "+Str( StringGadgetCursorX(#Editor_0))) If MP_KeyDown(#PB_Key_Escape) ; Esc abfrage / SC pushed? If MessageRequester(" program end", "You want to close the program?:", #PB_MessageRequester_YesNo) = #PB_MessageRequester_Yes End EndIf EndIf If MP_KeyDown(#PB_Key_Left)=1 : MP_TurnEntity (mesh,1,0,0) : EndIf ;links Debug #PB_Key_Left If MP_KeyDown(#PB_Key_Right)=1 : MP_TurnEntity (mesh,-1,0,0) : EndIf ;rechts #PB_Key_Right If MP_KeyDown(#PB_Key_Down)=1 : MP_TurnEntity (mesh,0,-1,0) : EndIf ;Runter #PB_Key_Down If MP_KeyDown(#PB_Key_Up)=1 : MP_TurnEntity (mesh,0,1,0) : EndIf ;rauf #PB_Key_Up MP_ShaderSet_D3DMATRIX (MyShader,"worldViewProjI",MP_ShaderGetWorldViewI (Mesh)) MP_ShaderSet_D3DMATRIX (MyShader,"worldViewProj",MP_ShaderGetWorldView (Mesh)) MP_ShaderSet_D3DMATRIX (MyShader,"matWorld",MP_ShaderGetWorld(Mesh)) MP_ShaderSet_D3DMATRIX (MyShader,"matWorldI",MP_ShaderGetWorldI(Mesh)) MP_ShaderSet_Float3 (MyShader,"lightDir",MP_ShaderGetLightDirection(Light)) MP_ShaderSet_D3DMATRIX (MyShader,"matLightWorld", MP_ShaderGetLightWorld(Light)) MP_ShaderSet_D3DMATRIX (MyShader,"matCamView ", MP_ShaderGetView (camera)) MP_ShaderSet_D3DMATRIX (MyShader,"matCamViewI", MP_ShaderGetViewI (camera)) MP_ShaderSet_D3DMATRIX (MyShader,"matCamProj", MP_ShaderGetCamProjection(camera)) MP_ShaderSet_Float3(MyShader,"vecEye",MP_ShaderGetCamPos(camera) ) MP_ShaderSetTexture(MyShader,"texture0",Texture(0)) MP_ShaderSetTexture(MyShader,"texture1",Texture(1)) MP_ShaderSetTexture(MyShader,"texture2",Texture(2)) MP_ShaderSetTexture(MyShader,"texture3",Texture(3)) MP_ShaderSetTexture(MyShader,"texture4",Texture(4)) MP_ShaderSetTexture(MyShader,"texture5",Texture(5)) MP_ShaderSetTexture(MyShader,"texture6",Texture(6)) MP_ShaderSetTexture(MyShader,"texture7",Texture(7)) MP_ShaderSetTexture(MyShader,"texture8",Texture(8)) MP_ShaderSetTexture(MyShader,"texture9",Texture(9)) MP_ShaderSetVar_f(Myshader,"time",MP_ElapsedMicroseconds()/100000) MP_ShaderSetVar_f (MyShader,"Var1",Var1) MP_ShaderSetVar_f (MyShader,"Var2",Var2) MP_ShaderSetVar_f (MyShader,"Var3",Var3) postfx.f = 0 MP_ShaderSetVar_f (MyShader,"postfx",postfx) MouseX.i = WindowMouseX(0) MP_ShaderSetVar_I (MyShader,"mousex",MouseX) MouseY.i = WindowMouseY(0) MP_ShaderSetVar_I (MyShader,"mousey",MouseY) Mouse(0) = MouseX / 450 ; Viewportx 20-470 Mouse(1) = MouseY / 450 ; Viewporty 20-470 MP_ShaderSet_Float3(MyShader,"mouse",@Mouse(0)) MP_RenderWorld () If Postprocessing = 1 MP_BackBufferToTexture (BackTexture) postfx.f = 1 MP_ShaderSetVar_f (MyShader,"postfx",postfx) MP_SetTechniqueMyShader (MyShader,"postFX") MP_ShaderSetTexture (MyShader,"texture0",BackTexture) MP_UsePixelShader(BackTexture, MyShader) ;MP_UseBackbufferShader(BackTexture, MyShader) MP_TextureToBackBuffer (BackTexture) EndIf MP_Flip () Wend ; *D3DDevice\SetSamplerState(0,#D3DSAMP_ADDRESSU,#D3DTADDRESS_WRAP) ; *D3DDevice\SetSamplerState(0,#D3DSAMP_ADDRESSV,#D3DTADDRESS_WRAP) ;D3DSAMP_ADDRESSU ;D3DSAMP_ADDRESSV ; *D3DDevice\SetSamplerState(0, #D3DSAMP_MINFILTER, #D3DTEXF_LINEAR) ; *D3DDevice\SetSamplerState(0, #D3DSAMP_MAGFILTER, #D3DTEXF_LINEAR) DataSection Helptxt: Data.s " Helpfile"+Chr(10) Data.s ""+Chr(10) Data.s "Here comes all variables of the Shadereditor"+Chr(10) Data.s ""+ Chr(10) Data.s "mesh object:"+ Chr(10) Data.s "worldViewProj -> typ matrix -> WorldView Matrix of Mesh"+ Chr(10) Data.s "worldViewProjI -> typ matrix -> inverse WorldView Matrix of Mesh"+ Chr(10) Data.s "matWorld -> typ matrix -> WorldView Matrix of Mesh"+ Chr(10) Data.s "matWorldI -> typ matrix -> inverse WorldView Matrix of Mesh"+ Chr(10) Data.s ""+ Chr(10) Data.s "light object:"+ Chr(10) Data.s "lightDir -> typ 3 x float -> light direction "+ Chr(10) Data.s "matLightWorld -> typ matrix -> World Matrix of light"+ Chr(10) Data.s ""+ Chr(10) Data.s "camera object:"+ Chr(10) Data.s "matCamView -> typ matrix -> camera view matrix"+ Chr(10) Data.s "matCamViewI -> typ matrix -> inverse camera view matrix"+ Chr(10) Data.s "matCamProj -> typ matrix -> camera projections matrix"+ Chr(10) Data.s "vecEye -> typ 3 x float -> position of camera"+ Chr(10) Data.s ""+ Chr(10) Data.s "texture object:"+ Chr(10) Data.s "texture0 -> typ texture -> texture"+ Chr(10) Data.s "texture1 -> typ texture -> texture"+ Chr(10) Data.s "texture2 -> typ texture -> texture"+ Chr(10) Data.s "texture3 -> typ texture -> texture"+ Chr(10) Data.s "texture4 -> typ texture -> texture"+ Chr(10) Data.s "texture5 -> typ texture -> texture"+ Chr(10) Data.s "texture6 -> typ texture -> texture"+ Chr(10) Data.s "texture7 -> typ texture -> texture"+ Chr(10) Data.s "texture8 -> typ texture -> texture"+ Chr(10) Data.s "texture9 -> typ texture -> texture"+ Chr(10) Data.s ""+ Chr(10) Data.s "time object:"+ Chr(10) Data.s "time -> typ float -> time in ms"+ Chr(10) Data.s ""+ Chr(10) Data.s "slider object:"+ Chr(10) Data.s "Var1 -> typ float(0 To 1) -> see gui of program"+ Chr(10) Data.s "Var2 -> typ float(0 To 1) -> see gui of program"+ Chr(10) Data.s "Var3 -> typ float(0 To 1) -> see gui of program"+ Chr(10) Data.s ""+ Chr(10)+ Chr(10) Data.s "Intrinsic Functions (DirectX HLSL)"+ Chr(10) Data.s "http://msdn.microsoft.com/en-us/library/ff471376.aspx"+ Chr(10) Data.s ""+Chr(10) Data.s "smalest possibles Shader example:"+Chr(10) Data.s ""+Chr(10) Data.s "technique Technique0"+Chr(10) Data.s "{"+Chr(10) Data.s " pass p0"+Chr(10) Data.s " {"+Chr(10) Data.s " FillMode = wireframe;"+Chr(10) Data.s " }"+Chr(10) Data.s "}"+Chr(10) Data.s "End" EndDataSection ; IDE Options = PureBasic 4.61 Beta 1 (Windows - x86) ; CursorPosition = 375 ; FirstLine = 342 ; Folding = - ; UseIcon = ..\MP3D Demos\mp3d.ico ; Executable = L:\temp\MP_31\16\MP_ShaderEditor.exe ; SubSystem = dx9 ; Manual Parameter S=DX9 ; EnableCustomSubSystem