Page 1 of 1

UV coords modifying at runtime

Posted: Thu Aug 26, 2004 7:16 pm
by banban73
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):

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
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

Posted: Thu Aug 26, 2004 8:04 pm
by KarLKoX
Hi !
It seems you didn't take attention that this is a ENGLISH ONLY forum, if you want to talk inf french, you must go here :lol:

Posted: Thu Aug 26, 2004 8:23 pm
by J. Baker
Pouvez-vous fournir un lien à l'image ?

Posted: Thu Aug 26, 2004 9:09 pm
by gilles_k
J'aimerais comprendre un peu mieux.
Que cherches tu à faire, appliquer l'image sur toute la surface ou simplement la centrer sur la surface?

Sorry for the English, but in french this is more simple. :lol:

Francais and english

Posted: Thu Aug 26, 2004 9:20 pm
by banban73
Salut à tous,

Pour le lien vers l'image, n'importe quelle image placée dans le même dossier que le source et au format JPEG peut faire l'affaire.

En d'autres termes, je veux afficher une image de dimensions quelconque sur toute la surface d'une face, en conservant les porportions. Le problème étant cette limitation de la taille des textures à une puissance de 2 (à en croire l'aide de Pure Basic).
J'espère que c'est plus clair comme ça.

Hello english speakers,

In other words, I want to display an image of any dimensions on the entire surface of a face, with proportions kept. The matter comes from the texture size limitation (must be a 2^x number according to PB help)

I hope this is clearer

Alban

Posted: Thu Aug 26, 2004 9:37 pm
by J. Baker
You would need to make a model in MilkShape and get the exporter plug-in from ogre.org in order to get the effect you want.

http://www.ogre3d.org/modules.php?op=mo ... load&cid=3

http://www.swissquake.ch/chumbalum-soft/

Hope this helps you some.

Posted: Fri Aug 27, 2004 12:03 pm
by banban73
Thank you for the links.
At the moment, I need to modify uv coords while running the program, so loading a predefined mesh with predefined texture coordinates will not match the user-defined image (in source code, it is defined but it is only for debug purpose, in the final version user will be able to load ANY image from a requester.)
Anyway, the links will be useful for others projects I have in mind.

Regards

Alban