CreateMesh und Faces

Anfängerfragen zum Programmieren mit PureBasic.
Benutzeravatar
Makke
Beiträge: 156
Registriert: 24.08.2011 18:00
Computerausstattung: AMD Ryzen 7 5700X - AMD Radeon RX 6800 XT - 32 GB DDR4 SDRAM
Wohnort: Ruhrpott
Kontaktdaten:

CreateMesh und Faces

Beitrag von Makke »

Hallo zusammen,

ich habe in einer Procedure versucht ein Mesh selber zu erstellen (ein Quad), folgenden Beispielcode poste ich mal:

Code: Alles auswählen

Procedure.i CreateButton3D(hEntity.i, hMaterial.i, xSize.i, ySize.i)
  
  Protected.i mesh, entity
  
  mesh = CreateMesh(#PB_Any, #PB_Mesh_TriangleList, #PB_Mesh_Static)
  
  MeshVertexPosition(-xSize/2, ySize/2, 0)
  MeshVertexTextureCoordinate(0, 1)
  
  MeshVertexPosition(xSize/2, ySize/2, 0)
  MeshVertexTextureCoordinate(1, 1)
  
  MeshVertexPosition(xSize/2, -ySize/2, 0)
  MeshVertexTextureCoordinate(1, 0)
  
  MeshVertexPosition(-xSize/2, -ySize/2, 0)
  MeshVertexTextureCoordinate(0, 0)
  
  MeshFace(0, 1, 2)
  MeshFace(0, 3, 2)
  
  FinishMesh(#True)
  NormalizeMesh(mesh)
  
  entity = CreateEntity(hEntity, MeshID(mesh), MaterialID(hMaterial))
  
  If hEntity = #PB_Any
    ProcedureReturn entity
  Else
    ProcedureReturn EntityID(entity)
  EndIf
  
EndProcedure
Wenn ich das erstellte Entity mit WorldDebug(#PB_World_DebugEntity) anzeigen lasse, dann wird das auch richtig angezeigt. Die Oberfläche (Faces) sieht aber sehr merkwürdig aus. Egal in welcher Reihenfolge ich die Eckpunkte (Vertices) zu einer Dreieck-Oberfläche (Face) zusammenfasse, es kommt keine vernünftige Oberfläche heraus. Ich habe das Mesh mal abgespeichert und in ein XML umgewandelt, dabei kam dann raus das die Vertices so aussahen:

Code: Alles auswählen

                    <vertex>
                        <position x="2.75506e-040" y="1.83674e-040" z="1.4013e-045" />
                        <texcoord u="-1.46934e-039" v="1" />
                    </vertex>
                    <vertex>
                        <position x="-10" y="-10" z="0" />
                        <texcoord u="0" v="0" />
                    </vertex>
                    <vertex>
                        <position x="10" y="-10" z="0" />
                        <texcoord u="1" v="0" />
                    </vertex>
                    <vertex>
                        <position x="10" y="10" z="0" />
                        <texcoord u="1" v="1" />
                    </vertex>
Ich hatte als Parameter an die Procedure als X- und Y-Größe den Wert 20 übergeben. Die Vertices 1, 2 und 3 sehen soweit auch ok aus, aber der Vertex 0 sieht sehr komisch aus. Hat jemand eine Idee was hier schiefläuft ?

Wäre für jeden Tip dankbar.
---
Windows 11 (64 bit)
Benutzeravatar
STARGÅTE
Kommando SG1
Beiträge: 7031
Registriert: 01.11.2005 13:34
Wohnort: Glienicke
Kontaktdaten:

Re: CreateMesh und Faces

Beitrag von STARGÅTE »

So wie ich das kenne, müssen Faces immer gegen den Uhrzeigersinn definiert werden (damit die "richtige" Seite dargestellt wird), dann sollte die Quadrattextur richtig dargestellt werden.

Code: Alles auswählen

InitEngine3D()
InitSprite()

Enumeration
	#Window
	#Camera
EndEnumeration

OpenWindow(#Window, 0, 0, 800, 600, "ScreenTitle", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(#Window), 0, 0, WindowWidth(#Window), WindowHeight(#Window), 0, 0, 0)

CreateCamera(#Camera, 0, 0, 100, 100)
MoveCamera(#Camera, 0, 0, 40)
CameraLookAt(#Camera, 0, 0, 0)


CreateMesh(1)

MeshVertexPosition(-10, 10, 0)
MeshVertexTextureCoordinate(0, 1)
MeshVertexPosition(10, 10, 0)
MeshVertexTextureCoordinate(1, 1)
MeshVertexPosition(10, -10, 0)
MeshVertexTextureCoordinate(1, 0)
MeshVertexPosition(-10, -10, 0)
MeshVertexTextureCoordinate(0, 0)

MeshFace(3, 1, 0)
MeshFace(3, 2, 1)

FinishMesh(1)

CreateTexture(1, 16, 16)
StartDrawing(TextureOutput(1))
	Box(0, 0, 8, 8, $FF0000)
	Box(8, 0, 8, 8, $00FF00)
	Box(0, 8, 8, 8, $0000FF)
	Box(8, 8, 8, 8, $00FFFF)
StopDrawing()
CreateEntity(1, MeshID(1), CreateMaterial(1, TextureID(1)))


Repeat
	
	Repeat
		
		Select WindowEvent()
			Case #PB_Event_CloseWindow
				End
			Case #PB_Event_None
				Break
		EndSelect
		
	ForEver
	
	RenderWorld()
	
	FlipBuffers()
	
ForEver
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Aktuelles Projekt: Lizard - Skriptsprache für symbolische Berechnungen und mehr
Benutzeravatar
Makke
Beiträge: 156
Registriert: 24.08.2011 18:00
Computerausstattung: AMD Ryzen 7 5700X - AMD Radeon RX 6800 XT - 32 GB DDR4 SDRAM
Wohnort: Ruhrpott
Kontaktdaten:

Re: CreateMesh und Faces

Beitrag von Makke »

Hi Stargate,

also, Dein Code funktioniert, danke dafür und für die Erläuterung, wäre wohl auch ein Thema das in die Hilfe mit aufgenommen werden sollte.

ABER, wenn ich Deinen Code einbaue, geht es trotzdem nicht, hier das gesamte Beispiel:

Code: Alles auswählen

EnableExplicit

Define.i sprite, tempTexture, tempMaterial, tempMesh, font, mx, my, camera, light, entity, entBtn1, entBtn2, entBakgrd, picked

Declare.i CreateButton3D(hEntity.i, hMaterial.i, xSize.i, ySize.i)
Declare CreateStaticBackground(hEntity.i, hMaterial.i, hFarestEntity.i, TextureRepeatX.i=1, TextureRepeatY.i=1)

; init
InitEngine3D()
InitSprite()
InitKeyboard()
InitMouse()

OpenWindow(0,0,0,1024,768,"3D Buttons",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, WindowWidth(0), WindowHeight(0), 0, 0, 0, #PB_Screen_WaitSynchronization)

; create objects
font = LoadFont(#PB_Any, "Wingdings", 28, #PB_Font_HighQuality)

sprite = CreateSprite(#PB_Any, 32, 32)
StartDrawing(SpriteOutput(sprite))
DrawingMode(#PB_2DDrawing_Transparent)
DrawingFont(FontID(font))
DrawText(0, 0, Chr(245), RGB(192,192,192))
StopDrawing()

FreeFont(font)
font = LoadFont(#PB_Any, "Wingdings", 60, #PB_Font_HighQuality)

; create cube texture
tempTexture = CreateTexture(#PB_Any, 256, 256)
StartDrawing(TextureOutput(tempTexture))
Box(0, 0, 256, 256, 0)
DrawingMode(#PB_2DDrawing_Gradient)      
FrontColor(RGB(63, 0, 191))
BackColor(RGB(255, 128, 0))
BoxedGradient(0, 0, 256, 256)
Box(0, 0, 256, 256)
StopDrawing()

; create cube mesh and material
tempMaterial = CreateMaterial(#PB_Any, TextureID(tempTexture))
tempMesh     = CreateCube(#PB_Any, 50)

; create cube entity
entity = CreateEntity(#PB_Any, MeshID(tempMesh), MaterialID(tempMaterial), 0, 0, -50)
RotateEntity(entity, 0, 45, 0, #PB_Relative)
FreeMesh(tempMesh)
FreeMaterial(tempMaterial)
FreeTexture(tempTexture)

; create button texture
tempTexture = CreateTexture(#PB_Any, 128, 128)
StartDrawing(TextureOutput(tempTexture))
DrawingMode(#PB_2DDrawing_AlphaBlend|#PB_2DDrawing_Transparent)
DrawingFont(FontID(font))
Box(0, 0, 128, 128, RGBA(0,255,0,64))
Box(8, 8, 112, 112, RGBA(0,128,0,128))
DrawText(128/2-TextWidth(Chr(231))/2, 128/2-TextHeight(Chr(231))/2, Chr(231), RGBA(0,255,0,192))
StopDrawing()

; create button material
tempMaterial = CreateMaterial(#PB_Any, TextureID(tempTexture))
MaterialBlendingMode(tempMaterial, #PB_Material_AlphaBlend)

; create button mesh and entity
entBtn1 = CreateButton3D(#PB_Any, tempMaterial, 20, 20)
MoveEntity(entBtn1, -50, -25, -25)
FreeMaterial(tempMaterial)
FreeTexture(tempTexture)

; create button 2 texture
tempTexture = CreateTexture(#PB_Any, 128, 128)
StartDrawing(TextureOutput(tempTexture))
DrawingMode(#PB_2DDrawing_AlphaBlend|#PB_2DDrawing_Transparent)
DrawingFont(FontID(font))
Box(0, 0, 128, 128, RGBA(0,255,0,64))
Box(8, 8, 112, 112, RGBA(0,128,0,128))
DrawText(128/2-TextWidth(Chr(232))/2, 128/2-TextHeight(Chr(232))/2, Chr(232), RGBA(0,255,0,192))
StopDrawing()

; create button 2 material
tempMaterial = CreateMaterial(#PB_Any, TextureID(tempTexture))
MaterialBlendingMode(tempMaterial, #PB_Material_AlphaBlend)

; create button 2 mesh and entity
entBtn2 = CreateButton3D(#PB_Any, tempMaterial, 20, 20)
MoveEntity(entBtn2, 50, -25, -25)
FreeMaterial(tempMaterial)
FreeTexture(tempTexture)

; create background texture and material
tempTexture = CreateTexture(#PB_Any, 64, 64) 
StartDrawing(TextureOutput(tempTexture))
Box(0,0,64,64,RGB(65, 105, 225))
StopDrawing()
tempMaterial = CreateMaterial(#PB_Any, TextureID(tempTexture))

; create background mesh and entity
tempMesh = CreateMesh(#PB_Any)
MeshVertexPosition(-1000, 1000, 0)
MeshVertexTextureCoordinate(0, 1)
MeshVertexPosition(1000, 1000, 0)
MeshVertexTextureCoordinate(1, 1)
MeshVertexPosition(1000, -1000, 0)
MeshVertexTextureCoordinate(1, 0)
MeshVertexPosition(-1000, -1000, 0)
MeshVertexTextureCoordinate(0, 0)
MeshFace(3, 1, 0)
MeshFace(3, 2, 1)
FinishMesh(#True)
entBakgrd = CreateEntity(#PB_Any, MeshID(tempMesh), MaterialID(tempMaterial))
MoveEntity(entBakgrd, 0, 0, -500, #PB_Absolute)
FreeMesh(tempMesh)
FreeMaterial(tempMaterial)
FreeTexture(tempTexture)

; create light
light = CreateLight(#PB_Any, RGB(225, 225, 225), 0, 0, 250, #PB_Light_Directional)
LightLookAt(light, EntityX(entity), EntityY(entity), EntityZ(entity))

; create camera
camera = CreateCamera(#PB_Any, 0, 0, 100, 100)
MoveCamera(camera, 0, 0, 250, #PB_Absolute)

AmbientColor(RGB(32,32,32))

FreeFont(font)

MouseLocate(WindowWidth(0)/2, WindowHeight(0)/2)
Repeat
 
  While WindowEvent() : Wend
 
  ExamineKeyboard()
  
  If ExamineMouse()
    
    mx = MouseX()
    my = MouseY()
    
    If MouseButton(#PB_MouseButton_Left)
      
      picked = MousePick(camera, mx, my)
      
      If picked >= 0
        
        Select picked
            
          Case entBtn1 : RotateEntity(entity, 0, -1, 0, #PB_Relative)
          Case entBtn2 : RotateEntity(entity, 0, 1, 0, #PB_Relative)
            
        EndSelect
        
      EndIf
      
    EndIf
    
  EndIf
   
  RenderWorld()
  
  DisplayTransparentSprite(sprite, mx, my)
  
  FlipBuffers()
 
Until KeyboardReleased(#PB_Key_Escape)

End

Procedure.i CreateButton3D(hEntity.i, hMaterial.i, xSize.i, ySize.i)
  
  Protected.i mesh, entity
  
  mesh = CreateMesh(#PB_Any, #PB_Mesh_TriangleList, #PB_Mesh_Static)
  
  MeshVertexPosition(-xSize/2, ySize/2, 0)
  MeshVertexTextureCoordinate(0, 1)
  
  MeshVertexPosition(xSize/2, ySize/2, 0)
  MeshVertexTextureCoordinate(1, 1)
  
  MeshVertexPosition(xSize/2, -ySize/2, 0)
  MeshVertexTextureCoordinate(1, 0)
  
  MeshVertexPosition(-xSize/2, -ySize/2, 0)
  MeshVertexTextureCoordinate(0, 0)
  
  MeshFace(3, 1, 0)
  MeshFace(3, 2, 1)

  FinishMesh(#True)
  
  entity = CreateEntity(hEntity, MeshID(mesh), MaterialID(hMaterial))
  
  FreeMesh(mesh)
  
  If hEntity = #PB_Any
    ProcedureReturn entity
  Else
    ProcedureReturn EntityID(entity)
  EndIf
  
EndProcedure

Procedure CreateStaticBackground(hEntity.i, hMaterial.i, hFarestEntity.i, TextureRepeatX.i=1, TextureRepeatY.i=1)
  
  Protected.i mesh, entity
  
  
  
EndProcedure
Eigentlich wollte ich für einen anderen Thread ein kurzes Beispiel posten und "mal eben" was zusammenschreiben und die Meshes selber erstellen, bei mir wird aber immer nur die zweite Oberfläche (face) angezeigt. An 5.11 kann es nicht liegen, da wie gesagt Dein Code funktioniert. Was läuft bei mir falsch ? Noch eine Idee ?
---
Windows 11 (64 bit)
Antworten