Demo - Morphing

Everything related to 3D programming
User avatar
pf shadoko
Enthusiast
Enthusiast
Posts: 280
Joined: Thu Jul 09, 2015 9:07 am

Demo - Morphing

Post by pf shadoko »

a little morphing demo
(I was inspired by I don't know which code)
I was waiting to have the shaders ready to post it

Code: Select all

Procedure ColorBlend(color1.l, color2.l, blend.f)
    Protected r.w,g.w,b.w,a.w
    r=  Red(color1) + (Red(color2)     - Red(color1)) * blend
    g=Green(color1) + (Green(color2) - Green(color1)) * blend
    b= Blue(color1) + (Blue(color2) -   Blue(color1)) * blend
    a=Alpha(color1) + (Alpha(color2) - Alpha(color1)) * blend
    ProcedureReturn  RGBA(r,g,b,a)
EndProcedure

ExamineDesktops()
InitEngine3D():InitSprite():InitKeyboard():InitMouse()
ex=DesktopWidth (0)*1
ey=DesktopHeight(0)*1
OpenWindow(0, 0,0, ex,ey, "Test 3d - [F1, F2, F3, F4] - Change sample - [F12] Wireframe -  [Esc] quit",#PB_Window_BorderLess)
OpenWindowedScreen(WindowID(0), 0, 0, ex, ey, 0, 0, 0)
 
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures",#PB_3DArchive_FileSystem)
Parse3DScripts()

CreateCamera(0, 0, 0, 100, 100):MoveCamera(0,0,0,-18):CameraLookAt(0,0,0,0)
CreateLight(0,$ffffff, 100, 100, -100):SetLightColor(0,#PB_Light_SpecularColor,$ffffff)
AmbientColor($777777)

vert_pg.s="%%%#version 130%%uniform mat4 worldviewproj_matrix;//+24%uniform vec4 camera_pos_os;//+77%uniform vec4 light_pos_os;//+44 0%uniform vec4 light_att;//+41 0%uniform vec4 fog_params;//+31%%varying vec3 oviewdir;%varying vec3 olightdir;%varying vec3 onormal;%varying vec4 ovcolor;%varying float olightatt;%varying vec2 ouv;%%void main()%{%oviewdir=normalize(camera_pos_os.xyz-gl_Vertex.xyz);%olightdir=normalize(light_pos_os.xyz-gl_Vertex.xyz);%gl_Position=worldviewproj_matrix*gl_Vertex;%onormal=gl_Normal;%float Dist=distance(light_pos_os,gl_Vertex);%olightatt=1.0/(light_att.y+light_att.z*Dist+light_att.w*Dist*Dist);%ouv=(gl_TextureMatrix[0]*gl_MultiTexCoord0).xy;%ovcolor=gl_Color;%}%%%%"
frag_pg.s="%%%#version 130%%uniform vec4 diffuse_ml;//+69 0%uniform vec4 specular_ml;//+70 0%uniform vec4 ambient_ml;//+67 0%uniform float shininess;//+36%uniform float blend;//0.5%%uniform sampler2D tx1;//0%uniform sampler2D tx2;//1%%varying vec3 oviewdir;%varying vec3 olightdir;%varying vec3 onormal;%varying vec4 ovcolor;%varying float olightatt;%varying vec2 ouv;%%void main()%{%vec3 normal=normalize(onormal);if (gl_FrontFacing==false) normal*=-1;%vec3 viewdir=normalize(oviewdir);%vec3 lightdir=normalize(olightdir);%%vec4 tcolor=vec4(mix(texture(tx1,ouv),texture(tx2,ouv),blend))*ovcolor;%%float dif=max(dot(lightdir,normal),0)*olightatt;%float spe=pow(max(dot(normalize(lightdir+viewdir),normal),0),shininess);%gl_FragColor=vec4(tcolor.rgb,1)*(ambient_ml+diffuse_ml*dif)+specular_ml*tcolor.a*spe;%}%%%"

CreateShader(0,vert_pg,frag_pg)

LoadTexture(0, "Dirt.jpg")
LoadTexture(1, "soil_wall.jpg")  
LoadTexture(2, "MRAMOR6X6.jpg")
LoadTexture(3, "Wood.jpg")
LoadTexture(4, "DosCarte.png")
LoadTexture(5, "RustySteel.jpg")

Procedure material(n,tx,scale,culling,tx0=0,tx1=0)
	CreateShaderMaterial(n,0):MaterialShaderTexture(n,TextureID(tx0),TextureID(tx1),0,0)
  MaterialShininess(n,256,$ffffff)
  If culling:MaterialCullingMode(n,#PB_Material_AntiClockWiseCull):EndIf
  MaterialFilteringMode(n,#PB_Material_Anisotropic,4)
  ScaleMaterial(n,1/scale,1/scale)
EndProcedure

Structure PB_MeshVertexV
  p.vector3
  n.vector3
  t.vector3
  u.f
  v.f
  color.l
EndStructure

Procedure Mix3D(*R.Vector3, *V1.Vector3, *V2.Vector3, r.f)
  *R\x = *V1\x + r * (*V2\x - *V1\x)
  *R\y = *V1\y + r * (*V2\y - *V1\y)
  *R\z = *V1\z + r * (*V2\z - *V1\z)
EndProcedure

Global n=64,n2=n/2
Global Dim p0.vector3(n,n)
Global Dim p1.vector3(n,n)
Global Dim t.PB_MeshVertexv(n,n)
For j=0 To n
	For i=0 To n
		With t(i,j)
			\u=i/n
			\v=j/n
			\color=$ffffffff
		EndWith
	Next
Next

Procedure mesh_morph(r.f)
  For j=0 To n
    For i=0 To n
        Mix3D(t(i,j)\p,p0(i,j),p1(i,j) ,r)
    Next
  Next
  CreateDataMesh(0,t(),8+2)
  CreateEntity(0,MeshID(0),MaterialID(0),0,0,0)
  CreateEntity(1,MeshID(0),MaterialID(1),0,0,0)
EndProcedure

Procedure geometrie(Array p.vector3(2),num,a.f,b.f,c.f,m=0)
  Protected.f u,v
  For j=0 To n
    For i=0 To n
      With p(i,j)  
        u=(j-n2*m)/n*2*#PI
        v=(i-n2*m)/n*2*#PI   
        Select num
          Case 0; double torre
            \x=(a+b*(Cos(u)*Sin(v/2)-Sin(u)*Sin(v)))*Cos(u*2)
            \y=(a+b*(Cos(u)*Sin(v/2)-Sin(u)*Sin(v)))*Sin(u*2)
            \z=-b*4*(Sin(u)*Sin(v/2)+Cos(u)*Sin(v))
          Case 1; sphere
            u/2
            \x=a*Sin(u)*Cos(v)
            \y=b*Sin(u)*Sin(v)
            \z=c*Cos(u)
          Case 2; coquille
            u*4
            \x=(a+b*Cos(v))*Exp(c*u)*Cos(u)
            \y=(a+b*Cos(v))*Exp(c*u)*Sin(u)
            \z=(3*a+b*Sin(v))*Exp(c*u)-5
          Case 3; coussin
            u/2
            \x=a*Sin(u)
            \y=b*Sin(v)
            \z=c*Cos(u)*Cos(v)/Sqr(2)
          Case 4; tetraedre
            \x=a*Cos(u)
            \y=b*Cos(v)
            \z=-c*Cos(u+v)
           Case 5; fleur
            \x=u*Cos(v)
            \y=u*Sin(v)
            \z=a*Cos(b*v)
          Case 6; dé creux
            \x=a*Sin(u)
            \y=a*Sin(v)
            \z=a*Sin(u+v)
          Case 7; truc
            \x=a*(u/(1+u*u+v*v))
            \y=b*(v/(1+u*u+v*v))
            \z=-c*(u*v/(1+u*u+v*v))
          Case 8; goutte
            u/2
            \x=b/2*a*Sin(u)*Cos(v)
            \y=b/2*a*Sin(u)*Sin(v)
            \z=8-a*4*Sin(u/2)
          Case 9; octaedre
            v/2
            \x=a*Pow(Cos(v),b)*Pow(Cos(u),c)
            \y=a*Pow(Cos(v),b)*Pow(Sin(u),c)
            \z=a*Pow(Sin(v),b)
          Case 10; berlingo
            u/#PI
            \x=b*a*(1+u)*Cos(v)
            \y=b*a*(1-u)*Sin(v)
            \z=-a*u
          Case 11 ;tri truc
            u/(2*#PI)
            b=3*Cos(u)/(Sqr(2)-a*Sin(2*u)*Sin(3*v))
            \x=b*(Cos(u)*Cos(2*v)+Sqr(2)*Sin(u)*Cos(v))
            \y=b*(Cos(u)*Sin(2*v)-Sqr(2)*Sin(u)*Sin(v))
            \z=-b*3*Cos(u)+8
        EndSelect
     EndWith 
    Next
  Next
EndProcedure

Procedure sample(Array p.vector3(2),num)
  ;num=1;If Random(1):num=12:Else:num=2:EndIf
  Select num
    Case 0:geometrie(p(),0,4,1,0,0)
    Case 1:geometrie(p(),1,8,6,3,0)
    Case 2:geometrie(p(),2,3,4,-0.1,0)
    Case 3:geometrie(p(),3,6,6,4,1)
    Case 4:geometrie(p(),4,4,4,5,1)
    Case 5:geometrie(p(),5,1,7,0,0)
    Case 6:geometrie(p(),6,4,0,0,0)
    Case 7:geometrie(p(),7,10,10,10,1)
    Case 8:geometrie(p(),8,3,4,0,0)
    Case 9:geometrie(p(),9,8,3,3,1)
    Case 10:geometrie(p(),10,6,0.5,0,1)
    Case 11:geometrie(p(),11,1,0,0,0)
    Case 12:geometrie(p(),2,4,3,-0.15,0)
    Case 13:geometrie(p(),5,2,3,0,0)
    Case 14:geometrie(p(),9,7,3,1,1)
    Case 15:geometrie(p(),9,6,1,3,1)
  EndSelect
EndProcedure

Define cpt,co0,co1,co,cf,cf0,cf1,tx0,tx1, dt_trans=60*4:dt_tot=60*6
Define.f rx,ry,rz,trans

Repeat
	If cpt=0
		CopyArray(p1(),p0()):sample(p1(),Random(15))
		co0=co1:co1=RGB(Random(127),Random(127),Random(127))
		cf0=cf1:cf1=RGB(Random(255),Random(255),Random(255))
		tx0=tx1:tx1=Random(5)
		material(0,0,4,0,tx0,tx1)
		material(1,1,4,1,tx0,tx1)
	EndIf
	If cpt<=dt_trans
		trans=(1-Cos(cpt/dt_trans*#PI))/2
		mesh_morph(trans)
		MaterialShaderParameter(0,1,"blend",#PB_Shader_Float,trans,0,0,0)
		MaterialShaderParameter(1,1,"blend",#PB_Shader_Float,trans,0,0,0)
		co=ColorBlend(co0,co1,trans)
		SetMaterialColor(0,#PB_Material_AmbientColor|#PB_Material_DiffuseColor,$808080+co)
		SetMaterialColor(1,#PB_Material_AmbientColor|#PB_Material_DiffuseColor,$ffffff-co)
		cf=ColorBlend(cf0,cf1,trans)
    CameraBackColor(0,cf)
	EndIf
	ExamineMouse()
	ExamineKeyboard()
	If KeyboardReleased(#PB_Key_F12):fdf=1-fdf:If fdf:CameraRenderMode(0,#PB_Camera_Wireframe):Else:CameraRenderMode(0,#PB_Camera_Textured):EndIf:EndIf
	If KeyboardPushed(#PB_Key_Space)=0 And MouseButton(2)=0:cpt=(cpt+1)%dt_tot:EndIf
	
	rx+0.8:ry+0.5:rz+0.6
	RotateEntity(0,rx,ry,rz,0)
	RotateEntity(1,rx,ry,rz,0)
	RenderWorld()
	FlipBuffers() 
	While  WindowEvent():Wend
Until KeyboardReleased(#PB_Key_Escape) Or MouseButton(3)
Last edited by pf shadoko on Wed Mar 29, 2023 9:09 pm, edited 2 times in total.
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Demo - Morphing

Post by idle »

very cool thanks for sharing
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Demo - Morphing

Post by BarryG »

Amazing! You're the undisputed king of 3D for PureBasic!
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Demo - Morphing

Post by luis »

Pretty cool :)
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
chi
Addict
Addict
Posts: 1028
Joined: Sat May 05, 2007 5:31 pm
Location: Linz, Austria

Re: Demo - Morphing

Post by chi »

Very nice! Now add some beat detection :lol:
Et cetera is my worst enemy
User avatar
Psychophanta
Addict
Addict
Posts: 4968
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Re: Demo - Morphing

Post by Psychophanta »

It crashes here without debugger.
With debugger it returns error at line 27, while shader creating.
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
User avatar
pf shadoko
Enthusiast
Enthusiast
Posts: 280
Joined: Thu Jul 09, 2015 9:07 am

Re: Demo - Morphing

Post by pf shadoko »

@Psychophanta
what is your :
- error message
- pb version
- environment
User avatar
ar-s
Enthusiast
Enthusiast
Posts: 340
Joined: Sat Oct 06, 2007 11:20 pm
Location: France

Re: Demo - Morphing

Post by ar-s »

That's amaaaaaaazing !
Work so well here (pb6.1x64 W11 1080gtx) with or without debugger
:D
~Ar-S~
My Image Hoster for PB users
My webSite (french) with PB apps : LDVMULTIMEDIA
PB - 3.x / 5.7x / 6 - W11 x64 - Ryzen 7 3700x / #Rpi4

Code: Select all

r3p347 : 7ry : un71l d0n3 = 1
User avatar
Psychophanta
Addict
Addict
Posts: 4968
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Re: Demo - Morphing

Post by Psychophanta »

pf shadoko wrote: Mon Mar 27, 2023 9:09 am what is your :
- error message
- pb version
- environment
With debugger message is the typical "try to write in address 0". Without debugger it just hangs.
PB 6.01 64bit in win 8.1 on a AMD A8-7410 radeon R5.
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
AZJIO
Addict
Addict
Posts: 1312
Joined: Sun May 14, 2017 1:48 am

Re: Demo - Morphing

Post by AZJIO »

Code: Select all

CreateShaderMaterial(n,0):MaterialShaderTexture(n,TextureID(tx0),TextureID(tx1),0,0)

Code: Select all

[20:19:12] Waiting for an executable to run...
[20:19:12] Executable type: Windows - x64 (64bit, Unicode)
[20:19:12] An executable has been launched.
[20:19:14] [ERROR] Line: 37
[20:19:14] [ERROR] Specified #Material is not initialized.
[20:19:17] The program has been destroyed.
User avatar
pf shadoko
Enthusiast
Enthusiast
Posts: 280
Joined: Thu Jul 09, 2015 9:07 am

Re: Demo - Morphing

Post by pf shadoko »

thanks
can you give me the ogre log (cf. InitEngine3D(...))
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Demo - Morphing

Post by mk-soft »

Filename corrected so that it runs under Linux ;)

- VM Win 10 running
- VM Mint Linux LMDE5 runs extremely fast
- Host system macOS runs with white areas ?

Code: Select all

LoadTexture(0, "Dirt.jpg")
LoadTexture(1, "soil_wall.jpg")  
LoadTexture(2, "MRAMOR6X6.jpg")
LoadTexture(3, "Wood.jpg")
LoadTexture(4, "DosCarte.png")
LoadTexture(5, "RustySteel.jpg")
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
pf shadoko
Enthusiast
Enthusiast
Posts: 280
Joined: Thu Jul 09, 2015 9:07 am

Re: Demo - Morphing

Post by pf shadoko »

thanks, i crorrected the file name for linux
can you give me the macos ogre log (cf. InitEngine3D(...))
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Demo - Morphing

Post by mk-soft »

Ogre.log file to big

Save one OnDrive: OneDrive Folder Report
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
pf shadoko
Enthusiast
Enthusiast
Posts: 280
Joined: Thu Jul 09, 2015 9:07 am

Re: Demo - Morphing

Post by pf shadoko »

@mk-soft
thanks:
" GLSL compile log: sh0VPERROR: 0:4: '' : version '130' is not supported"
annoying...
Post Reply