UV coords modifying at runtime
Posted: Thu Aug 26, 2004 7:16 pm
				
				OK I am sorry for the first text in french.
I will so try to barguain English.
Hello
Does somebody know how to modify the UV maps coords at runtime ?
I want to use an user-selected image (undetermined width and heigth) as a texture on a rotating parallelepiped.
The image dimensions may not match a 2^x number (64,128,256,...) and a black band appears on the left and/or under the image on the faces of the mesh.
I tried to "poke" the correct UV coords directly in data section where they are defined, just before creating mesh but it seems to have no effect.
Here is the piece of code (most of it is taken from PB help):
I hope my english is good enough to clearly describe my problem (I already found it difficult to explain in french, so...)
Thank You
Regards
Alban
Merci d'avance
A+
Alban
			I will so try to barguain English.
Hello
Does somebody know how to modify the UV maps coords at runtime ?
I want to use an user-selected image (undetermined width and heigth) as a texture on a rotating parallelepiped.
The image dimensions may not match a 2^x number (64,128,256,...) and a black band appears on the left and/or under the image on the faces of the mesh.
I tried to "poke" the correct UV coords directly in data section where they are defined, just before creating mesh but it seems to have no effect.
Here is the piece of code (most of it is taken from PB help):
Code: Select all
If InitEngine3D()
   UseJPEGImageDecoder() 
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If OpenScreen(1024,768,32,"3d screen")
  
    Gosub createMeshes
    
    CreateCamera(0, 0, 0, 100, 100)
    CameraBackColor(0,RGB(255,0,0))
    CameraLocate(0,0,0,1000)
    
    Repeat
      ClearScreen(0, 0, 0)    
      ExamineKeyboard()
      RotateEntity(0, 1, 0, 0)
           
      RenderWorld()
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape)
  EndIf
    
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf
  
End
createMeshes:
    ; Create a cube, manually.. See the DataSetion, for more precisions
    ;
    im=LoadImage(1,"MY_IMAGE.jpg") ; this can be any JPEG image
    im_Id=ImageID()
    larg=ImageWidth()
    haut=ImageHeight()
    ; calculate the closest 2^x number matching the image dimensions
    ; for texture creating
    text_dim=64
    If larg>64 Or haut>64
      text_dim=128
    EndIf
    If larg>128 Or haut>128
      text_dim=256
    EndIf
    If larg>256 Or haut>256
      text_dim=512
    EndIf
    If larg>512 Or haut>512
      text_dim=1024
    EndIf
    ;calculate ratio image dimension / texture size (=U and V coords)
    percentw.f=larg/text_dim
    percenth.f=haut/text_dim
    ;poke result in data section for the concerned points only (front face)
    mem=?CubeTextureCoordinates
    PokeF(mem+16,percenth.f)
    PokeF(mem+48,percenth.f)
    PokeF(mem+52,-percentw.f)
    PokeF(mem+60,-percentw.f)
    
    ; Mesh creating
    CreateMesh(0)
    SetMeshData(0, 0, ?CubeVertices          , 8)
    SetMeshData(0, 1, ?CubeFacesIndexes      , 12)
    SetMeshData(0, 2, ?CubeTextureCoordinates, 8)
    i=CreateTexture(0,text_dim,text_dim)
    If StartDrawing(TextureOutput(0))
      DrawImage(im,0,0)
      StopDrawing()
    Else
      End
    EndIf
    CreateEntity(0, MeshID(0), CreateMaterial(0,TextureID(0)))
    ; aplatit le mesh façon fiche cartonnée
    ResizeEntity(0,larg/2,haut/2,8)
    
Return
DataSection
  CubeVertices:
    Data.f 0, 0, 0 ; Vertex index 0
    Data.f 1, 0, 0 ; Vertex index 1
    Data.f 1, 0, 1 ; Vertex index 2
    Data.f 0, 0, 1 ; Vertex index 3
    Data.f 0, 1, 0 ; Vertex index 4 - Note: exactly the same as above, but with 'y'
    Data.f 1, 1, 0 ; Vertex index 5
    Data.f 1, 1, 1 ; Vertex index 6
    Data.f 0, 1, 1 ; Vertex index 7
  CubeFacesIndexes:
    Data.w 0, 1, 2 ; bottom face (clockwise as it's reversed...)
    Data.w 2, 3, 0 
    Data.w 6, 5, 4 ; top face
    Data.w 4, 7, 6
    Data.w 1, 5, 6 ; right face
    Data.w 6, 2, 1
    Data.w 7, 4, 0 ; left face
    Data.w 0, 3, 7
    Data.w 5, 1, 0 ; back face
    Data.w 0, 4, 5
    Data.w 2, 6, 7 ; front face
    Data.w 7, 3, 2
  CubeTextureCoordinates:
    Data.f 0, 0 ; Vertex 0 ad 0,4
    Data.f 0, 0    ; Vertex 1 ad 8,12
    Data.f -1, 0    ; Vertex 2 ad 16,20
    Data.f 0, 0    ; Vertex 3 ad 24,28
;
    Data.f 0, 0    ; Vertex 4 ad 32,36
    Data.f 0, 0    ; Vertex 5 ad 40,44
    Data.f -1, 1    ; Vertex 6 ad 48,52
    Data.f 0, 1    ; Vertex 7 ad 56,60
      
EndDataSection
Thank You
Regards
Alban
Merci d'avance
A+
Alban