Page 1 sur 1

probleme avec createmesh

Publié : lun. 06/mai/2013 18:55
par Patrick88
je pédale dans la semoule depuis un bon moment.
j'ai enfin :roll: réussi à mettre LA couleur que je veux sur mon cube avec l'éclairage qui va bien...
par contre impossible d'avoir le même resultat sur le mesh que je crée...

une idée ? qu'est ce que je fais mal ?

Merci

Patrick

Code : Tout sélectionner

; *****************************************************************************
;                  CONSTANTE FENETRE - BOITE DE DIALOGUE
; *****************************************************************************
Enumeration 0
  #FEN_PROGRAM
EndEnumeration


; *****************************************************************************
;                              CONSTANTE GADGET
; *****************************************************************************
Enumeration 0

EndEnumeration

Enumeration
  #PLAN_XY
  #PLAN_XZ
  #PLAN_YZ
EndEnumeration

#K_STATUSBAR = 0
#K_STATUSBAR_CHAMP_TEXT = 15
#K_STATUSBAR_CHAMP_VAL = 40

Enumeration 0
  #K_STATUSBAR_TEXT_SOURIS_X : #K_STATUSBAR_SOURIS_X
  #K_STATUSBAR_TEXT_SOURIS_Y : #K_STATUSBAR_SOURIS_Y
  ;#K_STATUSBAR_TEXT_SOURIS_Z : #K_STATUSBAR_SOURIS_Z
  #K_STATUSBAR_TEXT_ENTITY : #K_STATUSBAR_ENTITY
  #K_STATUSBAR_TEXT_ENTITY_POS_X:#K_STATUSBAR_ENTITY_POS_X
  #K_STATUSBAR_TEXT_ENTITY_POS_Y:#K_STATUSBAR_ENTITY_POS_Y
  #K_STATUSBAR_TEXT_ENTITY_POS_Z:#K_STATUSBAR_ENTITY_POS_Z
EndEnumeration

Enumeration
  #CAMERA_DESSOUS
  #CAMERA_DESSUS
  #CAMERA_DROITE
  #CAMERA_GAUCHE
  #CAMERA_FACE
  #CAMERA_ARRIERE
  #CAMERA_ISOMETRIQUE
  #CAMERA_CONIQUE
EndEnumeration

#N = 10


; *****************************************************************************
;                                 DECLARATION STRUCTURE
; *****************************************************************************
Structure STRU_POINT
  x.f : y.f : z.f
EndStructure

; *****************************************************************************
;                                     OBJET STANDARD
; *****************************************************************************

Structure STRU_OBJ_SPHERE ; Le numéro d'identification du nouveau mesh = position dans la liste
  F_COORD_X.f : F_COORD_Y.f : F_COORD_Z.f
  F_DIRECTION_X.f : F_DIRECTION_Y.f : F_DIRECTION_Z.f
  F_RAYON.f   ; Rayon de la sphère dans l'unité du monde
  L_RAYON.l   ; Nombre de segments de la sphère.
  L_ANNEAU.l  ; Nombre d'anneaux de la sphère.
EndStructure

Structure STRU_OBJ_CYLINDRE ; Le numéro d'identification du nouveau mesh = position dans la liste
  F_COORD_X.f : F_COORD_Y.f : F_COORD_Z.f
  F_DIRECTION_X.f : F_DIRECTION_Y.f : F_DIRECTION_Z.f
  F_RAYON.f   ; Rayon du cylindre dans l'unité du monde
  F_LONGUEUR.f   ; Longueur du cylindre dans l'unité du monde.
  F_TESSELATION.f  ; facteur de Tesselisation du cylindre
  F_DECALAGE.f ; Spécifie le décalage, dans l'unité du monde, à appliquer entre le fond et le dessus du cylindre
EndStructure

Structure STRU_OBJ_CUBE ; Le numéro d'identification du nouveau mesh = position dans la liste
  F_COORD_X.f : F_COORD_Y.f : F_COORD_Z.f
  F_DIRECTION_X.f : F_DIRECTION_Y.f : F_DIRECTION_Z.f
  F_TAILLE.f   ; Taille du cube dans l'unité du monde
EndStructure

Structure STRU_OBJ_LIGNE ; Le numéro d'identification du nouveau mesh = position dans la liste
  F_COORD_X1.f : F_COORD_Y1.f : F_COORD_Z1.f : L_COULEUR_DEBUT.l
  F_COORD_X2.f : F_COORD_Y2.f : F_COORD_Z2.f : L_COULEUR_FIN.l
EndStructure



; *****************************************************************************
;                                 DECLARATION VARIABLE GLOBALE
; *****************************************************************************

Global GU_Camera_courante.u

Global GL_Souris_X.l, GL_Souris_Y.l, GL_Souris_Z.l, GL_Entity_courante.l

; *****************************************************************************
;                                 DECLARATION PROCEDURE
; *****************************************************************************

Procedure CreateCone(Mesh_Id.l, Rayon.f, Longueur.f, Nombre_de_face.w, Angle_a_decrire_en_degre.f, Couleur.l,fil_de_fer.b) ;, Ferme.b, DecalageVertical.f)

  Protected indice.w,Angle_dune_facette.f, px.f,py.f,px2.f,py2.f,px0.f,py0.f,Mesh_Indice.l
  Protected NewList lst_vertex.STRU_POINT()

  If Angle_a_decrire_en_degre>360.0 :Angle_a_decrire_en_degre=360.0:EndIf
  If Angle_a_decrire_en_degre<0.0 :Angle_a_decrire_en_degre=Abs(Angle_a_decrire_en_degre):EndIf
  If Nombre_de_face<1:Nombre_de_face=1:EndIf
  
 ; Radian(Angle.f) = Convertit la valeur d'un 'Angle' en degré en radian
Angle_dune_facette = Angle_a_decrire_en_degre / Nombre_de_face
Angle_dune_facette = Radian(Angle_dune_facette)

Mesh_Indice = Mesh_Id
x0 = 0 : y0 = 0

; arc de cercle
; coordonnée X = Rayon x Cosinus de l'angle
; coordonnée Y = Rayon x Sinus de l'angle

CreateMesh(Mesh_Id, #PB_Mesh_TriangleList, #PB_Mesh_Dynamic)

AddElement(lst_vertex())
lst_vertex()\x = Rayon:px0=lst_vertex()\x
lst_vertex()\y = 0:py0=lst_vertex()\y
lst_vertex()\z = 0

MeshVertexPosition(lst_vertex()\x,lst_vertex()\y,lst_vertex()\z)
;MeshVertexColor(couleur)
;MeshVertexTextureCoordinate(0, 0)


For indice = 1 To Nombre_de_face
  AddElement(lst_vertex())  
  lst_vertex()\x = Rayon * Cos(Angle_dune_facette*indice):px0=lst_vertex()\x
  lst_vertex()\y = Rayon * Sin(Angle_dune_facette*indice):py0=lst_vertex()\y
  lst_vertex()\z = 0
  
  MeshVertexPosition(lst_vertex()\x,lst_vertex()\y,lst_vertex()\z)
  ;MeshVertexColor(couleur)
  ;MeshVertexTextureCoordinate(0, 0)
Next

MeshVertexPosition(0,0,0) ; Nombre_de_face+1 = centre 
  ;MeshVertexColor(Couleur)
  ;MeshVertexTextureCoordinate(0, 0)
MeshVertexPosition(0,0,Longueur) ; Nombre_de_face+2 = pointe
  ;MeshVertexColor(Couleur)
  ;MeshVertexTextureCoordinate(0, 0)

indice = 0
ForEach lst_vertex()
  MeshFace(Nombre_de_face+1,indice,indice+1)
  MeshFace(Nombre_de_face+2,indice,indice+1)
  indice+1
Next

FinishMesh(#True)
;NormalizeMesh(Mesh_Id) 
BuildMeshShadowVolume(Mesh_Id)
UpdateMeshBoundingBox(Mesh_Id)
EndProcedure

; *****************************************************************************
;                                 PROGRAMME PRINCIPAL
; *****************************************************************************


OpenWindow(#FEN_PROGRAM,0,0,800,600,"Tiny modeler",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
  

  
  If CreateStatusBar(#K_STATUSBAR,WindowID(#FEN_PROGRAM)) 
    AddStatusBarField(#K_STATUSBAR_CHAMP_TEXT) ; #K_STATUSBAR_TEXT_SOURIS_X
    AddStatusBarField(#K_STATUSBAR_CHAMP_VAL) ; #K_STATUSBAR_SOURIS_X
    AddStatusBarField(#K_STATUSBAR_CHAMP_TEXT) ; #K_STATUSBAR_TEXT_SOURIS_Y
    AddStatusBarField(#K_STATUSBAR_CHAMP_VAL) ; #K_STATUSBAR_SOURIS_Y
    ;AddStatusBarField(#K_STATUSBAR_CHAMP_TEXT) ; #K_STATUSBAR_TEXT_SOURIS_Z
    ;AddStatusBarField(#K_STATUSBAR_CHAMP_VAL) ; #K_STATUSBAR_SOURIS_Z
    AddStatusBarField(#K_STATUSBAR_CHAMP_VAL) ; #K_STATUSBAR_TEXT_ENTITY 
    AddStatusBarField(#K_STATUSBAR_CHAMP_TEXT*2) ; #K_STATUSBAR_ENTITY
    AddStatusBarField(#K_STATUSBAR_CHAMP_TEXT) ; #K_STATUSBAR_TEXT_ENTITY_POS_X
    AddStatusBarField(#K_STATUSBAR_CHAMP_VAL) ; #K_STATUSBAR_ENTITY_POS_X
    AddStatusBarField(#K_STATUSBAR_CHAMP_TEXT) ; #K_STATUSBAR_TEXT_ENTITY_POS_Y
    AddStatusBarField(#K_STATUSBAR_CHAMP_VAL) ; #K_STATUSBAR_ENTITY_POS_Y
    AddStatusBarField(#K_STATUSBAR_CHAMP_TEXT) ; #K_STATUSBAR_TEXT_ENTITY_POS_Z
    AddStatusBarField(#K_STATUSBAR_CHAMP_VAL) ; #K_STATUSBAR_ENTITY_POS_Z
  EndIf
  StatusBarText(#K_STATUSBAR,#K_STATUSBAR_TEXT_SOURIS_X,"X")
  StatusBarText(#K_STATUSBAR,#K_STATUSBAR_TEXT_SOURIS_Y,"Y")
  ;StatusBarText(#K_STATUSBAR,#K_STATUSBAR_TEXT_SOURIS_Z,"Z")
  StatusBarText(#K_STATUSBAR,#K_STATUSBAR_TEXT_ENTITY,"Entity") 
  StatusBarText(#K_STATUSBAR,#K_STATUSBAR_TEXT_ENTITY_POS_X,"X")
  StatusBarText(#K_STATUSBAR,#K_STATUSBAR_TEXT_ENTITY_POS_Y,"Y")
  StatusBarText(#K_STATUSBAR,#K_STATUSBAR_TEXT_ENTITY_POS_Z,"Z")
  
  LeftOffset.w = 0 
  TopOffset.w  = 0;MenuHeight()
  ScreenW.w    = WindowWidth(#FEN_PROGRAM)-LeftOffset
  ScreenH.w    = WindowHeight(#FEN_PROGRAM)-StatusBarHeight(#K_STATUSBAR)-5
  
  

InitEngine3D():InitSprite():;InitMouse()
   
OpenWindowedScreen(WindowID(#FEN_PROGRAM),LeftOffset,TopOffset,ScreenW,ScreenH,0,0,0) 
; 
; CreateSprite(0,32,32) 
; LoadSprite(0,"IMAGES\Souris.bmp")

;Debug GetCurrentDirectory()+"images\texture-bw.jpg"

Add3DArchive(GetCurrentDirectory()+"images", #PB_3DArchive_FileSystem)

;CreateMaterial(0, LoadTexture(1, "jaune-pale.jpg"))
CreateMaterial(0, LoadTexture(1, "texture-bw.jpg"))
; voir MaterialShadingMode(#Matiere, Mode) pour les modes de rendu



; Texture
;
CreateTexture(0, 256, 256)
StartDrawing(TextureOutput(0))
  Box(0, 0, 256, 256, RGB(240, 172, 30)) ;$1EACF0
StopDrawing()

CreateMaterial(0, TextureID(0))

CreateCamera(#CAMERA_ISOMETRIQUE,0,0,100,50) ; Par défaut, une caméra nouvellement crée a une couleur de fond noire.
CameraBackColor(#CAMERA_ISOMETRIQUE,$FFFFFF) ; Change la couleur de fond d'une caméra.
MoveCamera(#CAMERA_ISOMETRIQUE,15,15,15, #PB_Absolute)
CameraLookAt(#CAMERA_ISOMETRIQUE,0,0,0)

CreateCamera(#CAMERA_ARRIERE,0,50,100,50)
CameraBackColor(#CAMERA_ARRIERE,$FFFFFF)
MoveCamera(#CAMERA_ARRIERE,0,0,15, #PB_Absolute)
CameraLookAt(#CAMERA_ARRIERE,0,0,0)

;- Light
;
CreateLight(0, RGB(255, 255, 255), -40, 100, 80)
AmbientColor(RGB(50, 50, 50))

;     ^ Y     
;     |   
;     |   
;     +----> X
;   /    
; Z       

CreateLine3D(0, 0, 0, 0, RGB(255,0,0),1000, 0, 0, RGB(255,0,0))
CreateLine3D(1, 0, 0, 0, RGB(0,255,0),0, 1000, 0, RGB(0,255,0))
CreateLine3D(2, 0, 0, 0, RGB(0,0,255),0, 0, 1000, RGB(255,0,255))

CreateCone(4,5,10,6,270,RGB(240, 172, 30),0)
CreateCube(5,5)
;SaveMesh(4,"Cone.txt") ; http://wiki.unity3d.com/index.php?title=MeshSerializer

CreateEntity(4,MeshID(4),MaterialID(0))
;EntityRenderMode(4,#PB_Entity_CastShadow)

CreateEntity(5,MeshID(5),MaterialID(0))
MoveEntity(5,0,0,-12)



GU_Camera_courante = #CAMERA_ISOMETRIQUE

Quit = 0

Repeat
  Select WaitWindowEvent()
  
  
    Case #PB_Event_CloseWindow
      Quit = 2
      
  EndSelect ;WaitWindowEvent()
  
  GL_Souris_X = WindowMouseX(#FEN_PROGRAM)
  GL_Souris_Y = WindowMouseY(#FEN_PROGRAM)
  GL_Souris_Z = 0
  
  StatusBarText(#K_STATUSBAR,#K_STATUSBAR_SOURIS_X,Str(GL_Souris_X))
  StatusBarText(#K_STATUSBAR,#K_STATUSBAR_SOURIS_Y,Str(GL_Souris_Y))
  
  StatusBarText(#K_STATUSBAR,#K_STATUSBAR_ENTITY,Str(GL_Entity_courante))
  
  GL_Entity_courante = MouseRayCast(GU_Camera_courante, GL_Souris_X, GL_Souris_Y,-1);MousePick(GU_Camera_courante,GL_Souris_X,GL_Souris_Y) ; -> quand la souris passe sur le cube = renvoie le numero de l'entity
  
  If GL_Entity_courante>-1 
    StatusBarText(#K_STATUSBAR,#K_STATUSBAR_ENTITY_POS_X,Str(PickX()))
    StatusBarText(#K_STATUSBAR,#K_STATUSBAR_ENTITY_POS_Y,Str(PickX()))
    StatusBarText(#K_STATUSBAR,#K_STATUSBAR_ENTITY_POS_Z,Str(PickX()))
  
    CreateLine3D(3, PickX(), PickY(), PickZ(), RGB(255,0,0), 
                     PickX() + NormalX()*#N, 
                     PickY() + NormalY()*#N, 
                     PickZ() + NormalZ()*#N, RGB(255,0,0))
  
  EndIf  
  
  RenderWorld()
  FlipBuffers()
    
Until quit <> 0

Re: probleme avec createmesh

Publié : mer. 08/mai/2013 15:05
par comtois
Voila un exemple de création de cône, ça pourra peut-être t'inspirer ?

Code : Tout sélectionner

IncludeFile "Screen3DRequester.pb"

#CameraSpeed = 1

Declare CreateCone(BaseSegments.f, HeightSegments.f, Radius.f, Height.f, R, G, B, fadeAmount.f)

Structure Vector3
  x.f
  y.f
  z.f
EndStructure

Structure Quaternion
  x.f
  y.f
  z.f
  w.f
EndStructure

Define.f KeyX, KeyY, MouseX, MouseY

If InitEngine3D(3)
  
  Add3DArchive("Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive("Data/Models", #PB_3DArchive_FileSystem)
  Add3DArchive("Data/Scripts", #PB_3DArchive_FileSystem)
  Add3DArchive("Data/Packs/skybox.zip", #PB_3DArchive_Zip)
  Parse3DScripts()
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
    
    WorldShadows(#PB_Shadow_Modulative)
    
    FontMaterial1 = GetScriptMaterial(-1, "MyMaterial")
    CreateMaterial(1, LoadTexture(1,"Dirt.jpg"))
    
    ; create ground
    CreatePlane(0, 500, 500, 50, 50, 1, 1)
    CreateEntity(0, MeshID(0), MaterialID(1))
    EntityRenderMode(0, 0)
    
    LoadMesh(1, "robot.mesh")
    CreateEntity(1, MeshID(1), #PB_Material_None)
    
    Cone = CreateCone(24, 24, 70, 140, 155, 155, 0, 0.3)
    CreateEntity(2, MeshID(Cone), MaterialID(FontMaterial1))
    EntityRenderMode(2, 0)
    
    ; SkyBox
    SkyBox("stevecube.jpg")
    
    ; Camera
    ;
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 0, 200, 200, #PB_Absolute)
    
    AmbientColor(0)
    CreateLight(0, RGB(255, 255, 255), EntityX(2), EntityY(2)+150, EntityZ(2), #PB_Light_Spot)
    SpotLightRange(0, 1, 60, 3)
    
    Repeat
      Screen3DEvents()
      
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
      EndIf
      
      If ExamineKeyboard()
        
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -#CameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = #CameraSpeed
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -#CameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = #CameraSpeed
        Else
          KeyY = 0
        EndIf
        
      EndIf
      
      RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera  (0, KeyX, 0, KeyY)
      
      
      RenderWorld()
      Screen3DStats()
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf

End

Procedure QuaternionFromAngleAxis(*Q.Quaternion, Angle.f, *Axis.Vector3)
  HalfAngle.f = 0.5*Angle
  Sin.f = Sin(HalfAngle)
  *Q\w  = Cos(HalfAngle)
  *Q\x  = Sin**Axis\x
  *Q\y  = Sin**Axis\y
  *Q\z  = Sin**Axis\z
EndProcedure

Procedure CreateCone(BaseSegments.f, HeightSegments.f, Radius.f, Height.f, R, G, B, fadeAmount.f)
  Protected	Quaternion.Quaternion 
  Protected.Vector3 Normal, Axis
  
  Cone = CreateMesh(#PB_Any)
  deltaAngle.f = 2*#PI / BaseSegments
  deltaHeight.f = Height/HeightSegments
  
  Normal\x = Radius
  Normal\y = Height
  Normal\z = 0.0
  ;Normalize(@Normal)
  
  Axis\x = 0
  Axis\y = -1
  Axis\z = 0
  
  Offset = 0
  
  For HeightSegmentIndex = 0 To HeightSegments
    
    DeltaRadius.f = Radius * (1.0 - HeightSegmentIndex / HeightSegments)
    
    For BaseSegmentIndex = 0 To BaseSegments
      
      x.f = DeltaRadius * Cos(BaseSegmentIndex * deltaAngle)
      z.f = DeltaRadius * Sin(BaseSegmentIndex * deltaAngle)
      MeshVertexPosition(x, HeightSegmentIndex * deltaHeight, z)
      
      If(fadeAmount > 0.0) 
        MeshVertexColor(RGBA(R, G, B, (HeightSegmentIndex / HeightSegments * fadeAmount)*255))
      Else 
        MeshVertexColor(RGBA(R, G, B, 255))
      EndIf
      
      QuaternionFromAngleAxis(@Quaternion, -BaseSegmentIndex*deltaAngle, @Axis)
      ;MeshVertexNormal(Cone->normal(Quaternion * Normal);
      MeshVertexNormal(x * 4, 0, z * 4)
      MeshVertexTextureCoordinate(BaseSegmentIndex / BaseSegments, HeightSegmentIndex / m_HeightSegments);
      
      If HeightSegmentIndex <> HeightSegments And BaseSegmentIndex <> BaseSegments
        nbFace + 2
        MeshFace(Offset + BaseSegments + 2, Offset    , Offset + BaseSegments + 1)
        MeshFace(Offset + BaseSegments + 2, Offset + 1, Offset)
      EndIf
      
      Offset + 1
    Next
  Next
  
  centerIndex = Offset
  
  MeshVertexPosition(0,0,0)
  MeshVertexNormal(0, -1, 0)
  MeshVertexTextureCoordinate(0.0,1)
  Offset + 1
  
  For BaseSegmentIndex=0 To BaseSegments
    
    x.f = Radius * Cos(BaseSegmentIndex*deltaAngle)
    z.f = Radius * Sin(BaseSegmentIndex*deltaAngle)
    
    MeshVertexPosition(x, 0.0, z)
    If(fadeAmount > 0.0) 
      MeshVertexColor(RGBA(R, G, B, 0.01 * 255))
    Else 
      MeshVertexColor(RGBA(R, G, B, 255))
    EndIf
    MeshVertexNormal(0, -1, 0)
    MeshVertexTextureCoordinate(BaseSegmentIndex/BaseSegments,0.0)
    If (BaseSegmentIndex <> BaseSegments)
      nbFace + 1
      MeshFace(centerIndex, Offset, Offset+1)
    EndIf
    Offset + 1
  Next
  
  FinishMesh(1)
  
  ProcedureReturn Cone
EndProcedure

Re: probleme avec createmesh

Publié : mer. 08/mai/2013 18:54
par Patrick88
mouais... mééé, à la ligne, FontMaterial1 = GetScriptMaterial(-1, "MyMaterial")
Je n'ai pas le script "MyMaterial"...

bon, néanmoins, j'ai plus ou moins trouvé ce qui coince

en bidouillant l'exemple "meshManualCube", et en le collant dans mon code.

1/ je m'aperçoit que si j'enlève les lignes MeshVertexColor, le materiel est quand même appliqué sur le mesh
2/ si j'enlève les lignes MeshVertexNormal, le mesh a une couleur "noiratre" voir "brunatre" très foncé.
3/ si je laisse la ligne MeshVertexNormal avec n'importe quel valeur après le premier MeshVertexPosition et je vire les autres
et que je rajoute un NormalizeMesh après le FinishMesh alors le mesh est correctement éclairé...

aaaaahhhh...

Patrick

Re: probleme avec createmesh

Publié : mer. 08/mai/2013 19:42
par comtois
Patrick88 a écrit :mouais... mééé, à la ligne, FontMaterial1 = GetScriptMaterial(-1, "MyMaterial")
Je n'ai pas le script "MyMaterial"...
exact, voici le script du material à créer dans le répertoire data\scripts (la texture White.jpg existe dans le répertoire data/textures)

Code : Tout sélectionner

material MyMaterial
{
   technique
   {
      pass
      {
         lighting off
         scene_blend alpha_blend
         depth_write off

         texture_unit
         {
            texture White.jpg
         }
      }
   }
}
2/ si j'enlève les lignes MeshVertexNormal, le mesh a une couleur "noiratre" voir "brunatre" très foncé.
l'éclairage est calculé en fonction des normales du mesh, donc sans normales, pas d'éclairage.
3/ si je laisse la ligne MeshVertexNormal avec n'importe quel valeur après le premier MeshVertexPosition et je vire les autres
et que je rajoute un NormalizeMesh après le FinishMesh alors le mesh est correctement éclairé...
Oui c'est le premier 'vertex' qui indique à ogre comment construire le mesh , les autres 'vertices' doivent avoir exactement le même format. Donc si tu mets une normale pour le premier vertex , automatiquement ogre l'ajoute pour les autres, même si tu n'as rien défini.
Et NormalizeMesh () fait le calcul à ta place.

Re: probleme avec createmesh

Publié : jeu. 09/mai/2013 9:35
par Patrick88
okay merci. :wink:
que veux tu dire par "NormalizeMesh fait le calcul" ?
je vois dans l'exemple "meshManualCube", une constante #SQRT13 = 0.57735026
qui est ajouté au même position que le MeshVertex, mais je ne m'explique pas "cette valeur"
et à quoi sert le MeshVertexColor, vu qu'il n'a pas d'incidence sur le dessin des faces, peut-être si le rendu est en fil-de-fer ?

Code : Tout sélectionner

    MeshVertexPosition(-100, 100, -100)
    MeshVertexNormal(-#SQRT13,#SQRT13,-#SQRT13)
    MeshVertexColor(RGB(255, 0, 0))
Image

dans l'image ci dessus, le mesh de droite est un CreateCube est à gauche un CreateMesh avec un NormalizeMesh, le rendu est différent ?
(cela ne provient pas de la position de la lumière)

Code : Tout sélectionner

    MeshVertexNormal(-#SQRT13,#SQRT13,-#SQRT13)
    MeshVertexNormal(0,0,0)
que ce soit l'un ou l'autre du MeshVertexNormal, le rendu est le même...
(flat sur cube ogre et gouraud sur le mien)

Merci

patrick

Re: probleme avec createmesh

Publié : ven. 10/mai/2013 19:44
par comtois
Patrick88 a écrit :okay merci. :wink:
que veux tu dire par "NormalizeMesh fait le calcul" ?
J'avais donné un exemple ici
je vois dans l'exemple "meshManualCube", une constante #SQRT13 = 0.57735026
qui est ajouté au même position que le MeshVertex, mais je ne m'explique pas "cette valeur"
Chaque vertex est commun à 3 surfaces , la normale de la première surface est 0,0,1, la normale de la seconde surface est 0,1,0 et la normale de la troisième surface est 1,0,0
donc pour ce vertex la normale serait 1,1,1. Et pour normaliser le vecteur voici le calcul

Code : Tout sélectionner

Structure Vector3
  x.f
  y.f
  z.f
EndStructure

t.Vector3
t\x = 1
t\y = 1
t\z = 1

Procedure Normalize(*V.Vector3)
  Define.f magSq, oneOverMag 
  
  magSq = *V\x * *V\x + *V\y * *V\y + *V\z * *V\z 
  If magsq > 0
    oneOverMag = 1.0 / Sqr(magSq)
    *V\x * oneOverMag
    *V\y * oneOverMag
    *V\z * oneOverMag
  EndIf  
  
EndProcedure

Normalize(@t)

Debug t\x
Debug t\y
Debug t\z 
tu retrouves bien 0.57735026 .
et à quoi sert le MeshVertexColor, vu qu'il n'a pas d'incidence sur le dessin des faces, peut-être si le rendu est en fil-de-fer ?
pour voir l'effet il faut modifier le material ainsi

Code : Tout sélectionner

    SetMaterialColor(0, #PB_Material_AmbientColor, #PB_Material_AmbientColors)
Et MeshVertexColor est combiné en fonction de AmbientColor(), donc si AmbientColor() est à zéro tu ne verras rien.

Voici l''exemple MeshManual.pb modifié pour que tu vois l'effet que ça donne (j'ai coloré la base de la pyramide en bleu)

Code : Tout sélectionner

;
; ------------------------------------------------------------
;
;   PureBasic - Manual Mesh
;
;    (c) 2003 - Fantaisie Software
;
; ------------------------------------------------------------
;

#CameraSpeed = 1

IncludeFile "Screen3DRequester.pb"

Define.f KeyX, KeyY, MouseX, MouseY

Define.f x, y, z, nx, ny, nz, u, v
Define.l Co
Define.w t1, t2, t3

If InitEngine3D()
  
  Add3DArchive("Data/Textures", #PB_3DArchive_FileSystem)
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
    
    ; Create a pyramid, manually.. See the DataSection, for more precisions
    ;
    
    Restore Pyramid
    
    CreateMesh(0, #PB_Mesh_TriangleList)
    
    ;Base
    For i = 0 To 3
      Read.f x : Read.f y : Read.f z
      Read.l Co
      Read.f u : Read.f v
      MeshVertexPosition(x, y, z)
      MeshVertexNormal(0, 0, 0)
      MeshVertexColor(Co)
      MeshVertexTextureCoordinate(u, v)
    Next
    
    For i = 0 To 1
      Read.w t1 : Read.w t2 : Read.w t3
      MeshFace(t1, t2, t3)
    Next
    
    ;Side
    For k=0 To 3
      
      AddSubMesh(#PB_Mesh_TriangleList)
      For i = 0 To 2
        Read.f x : Read.f y : Read.f z
        Read.l Co
        Read.f u : Read.f v
        MeshVertexPosition(x, y, z)
        MeshVertexNormal(0, 0, 0) 
        MeshVertexColor(Co)
        MeshVertexTextureCoordinate(u, v)
      Next i
      Read.w t1 : Read.w t2 : Read.w t3
      MeshFace(t1, t2, t3)
      
    Next
    
    FinishMesh(#True)
    NormalizeMesh(0) 
    
    UpdateMeshBoundingBox(0)
    
    CreateMaterial(0, LoadTexture(0, "Geebee2.bmp"))
    SetMaterialColor(0, #PB_Material_AmbientColor, #PB_Material_AmbientColors)
    
    
    CreateEntity(0, MeshID(0), MaterialID(0))
    ScaleEntity(0, 400, 200, 400)
    
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 0, 0, 1000, #PB_Absolute)
    
    CreateLight(0, RGB(255,255,255), 300, 600, -100)
    AmbientColor(RGB(80, 80, 80))
    
    Repeat
      Screen3DEvents()
      
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
      EndIf
      
      If ExamineKeyboard()
        
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -#CameraSpeed 
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = #CameraSpeed 
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -#CameraSpeed 
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = #CameraSpeed 
        Else
          KeyY = 0
        EndIf
        
      EndIf
      
      RotateEntity(0, 1, 1, 1, #PB_Relative)
      
      RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera  (0, KeyX, 0, KeyY)
      
      RenderWorld()
      Screen3DStats()      
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End

DataSection
  Pyramid:
  ;Base
  Data.f -0.5,-0.5,0.5  ; position
  Data.l $FF0000        ; color
  Data.f 0,0            ; UVCoordinate
  
  Data.f 0.5,-0.5,0.5   ; position
  Data.l $FF0000        ; color 
  Data.f 0,1            ; UVCoordinate
  
  Data.f 0.5,-0.5,-0.5  ; position
  Data.l $FF0000        ; color
  Data.f 1,1            ; UVCoordinate
  
  Data.f -0.5,-0.5,-0.5 ; position
  Data.l $FF0000        ; color
  Data.f 1,0            ; UVCoordinate
  
  Data.w 2,1,0          ; Face 
  Data.w 0,3,2          ; Face 
  
  ;-Front
  Data.f 0.5,-0.5,0.5   ; position
  Data.l $FFFFFF          ; color
  Data.f 1,0            ; UVCoordinate
  
  Data.f 0.0,0.5,0.0
  Data.l $FFFFFF
  Data.f 0.5,0.5
  
  Data.f -0.5,-0.5,0.5
  Data.l $FFFFFF
  Data.f 0,0
  
  Data.w 0,1,2         ; Face
  
  ;-Back
  Data.f -0.5,-0.5,-0.5
  Data.l $FFFFFF
  Data.f 0,1
  
  Data.f 0.0,0.5,0.0
  Data.l $FFFFFF
  Data.f 0.5,0.5
  
  Data.f 0.5,-0.5,-0.5
  Data.l $FFFFFF
  Data.f 1,1
  
  Data.w 0,1,2
  
  ;-Left
  Data.f -0.5,-0.5,0.5
  Data.l $FFFFFF
  Data.f 0,0
  
  Data.f 0.0,0.5,0.0
  Data.l $FFFFFF
  Data.f 0.5,0.5
  
  Data.f -0.5,-0.5,-0.5
  Data.l $FFFFFF
  Data.f 0,1
  
  Data.w 0,1,2
  
  ;-Right
  Data.f 0.5,-0.5,-0.5
  Data.l $FFFFFF
  Data.f 1,1
  
  Data.f 0.0,0.5,0.0
  Data.l $FFFFFF
  Data.f 0.5,0.5
  
  Data.f 0.5,-0.5,0.5
  Data.l $FFFFFF
  Data.f 1,0
  
  Data.w 0,1,2
  
EndDataSection
dans l'image ci dessus, le mesh de droite est un CreateCube est à gauche un CreateMesh avec un NormalizeMesh, le rendu est différent ?
(cela ne provient pas de la position de la lumière)

que ce soit l'un ou l'autre du MeshVertexNormal, le rendu est le même...
(flat sur cube ogre et gouraud sur le mien)
C'est parce que le cube de l'exemple utilise 8 vertices, alors que le CreateCube en utilise 24 ( 4 par surface)
Comme déjà indiqué plus haut, la normale dans le premier cas est SQRT13 ,SQRT13 ,SQRT13 car le vertex est commun à 3 surfaces
la normale dans le second cas sera 1,0, 0 ou 0,1,0 bref tu as 6 côté, donc 6 normales différentes.

voir aussi ce vieux tuto que j'avais fait, plus du tout au goût du jour, mais y'a peut-être quelques infos à grapiller par ci par là