SphereMapping for 3D Objects with missing UV coords

Share your advanced PureBasic knowledge/code with the community.
mpz
Enthusiast
Enthusiast
Posts: 497
Joined: Sat Oct 11, 2008 9:07 pm
Location: Germany, Berlin > member German forum

SphereMapping for 3D Objects with missing UV coords

Post by mpz »

Hi,

i am working on "Sphere Mapping" and found a solution to create uv coords for "Sphere" Meshs with missing uv coords to map a texture on it. I found two methods and here comes it as little examples. I cant show you a Ogre mesh example, because i found not a way to mainpulate the PB Ogre Mesh vertices directly:

2D Example, Angle Calculation of two Points:

Code: Select all

; Easy Angle Calculation  of two Points

Procedure MP_AngleCalculation (x1.f,y1.f) ; Angle between 0,0 and x1,y1
If x1 = 0 
      If y1 > 0  
        AktTan.f = 350 
      Else 
        AktTan.f = -350 
      EndIf 
Else 
    AktTan.f = y1 / x1 
EndIf 

  Winkel=Round((180.0/#PI)*(ATan(AktTan)),#PB_Round_Nearest); 

  If x1 < 0 
     Winkel + 180 
  Else 
    If y1 < 0 
       Winkel + 360 
    EndIf    
  EndIf 

  ProcedureReturn Winkel 

EndProcedure 


; Two Points

x1 = 3 
y1 = 5 

x2 = 12 
y2 = -4 

; Find the 0,0 Point to Calculation the Angle 

Rx1 = x2 - x1 
Ry1 = y2 - y1 

Debug "Angle between x1,y1 and x2,y2 = " + Str(MP_AngleCalculation (Rx1,Ry1)) + " Grad" 
 

First Algorythmus, info here
http://www.mvps.org/directx/articles/spheremap.htm

Code: Select all

For n = 0 To MP_CountVertices(Sphere1)-1 ; Count of Vertices of the mesh 
        x.f = MP_VertexX (Sphere1,n) ; read x coord of a Vertices
        y.f = MP_VertexY (Sphere1,n) ; read x coord of a Vertices
        MP_SetVertexU (Sphere1,n, ASin(x)/#PI+0.5)  ; Alternate fast methodic = x*0.5+0.5 
        ;write U.f of the Vertices
        MP_SetVertexV (Sphere1,n, ASin(-y)/#PI+0.5) ; Alternate fast methodic = -y*0.5+0.5
        ;write v.f of the Vertices
Next
My own second Algorythmus

Code: Select all

For n = 0 To MP_CountVertices(Sphere2)-1

        x.f = MP_VertexX (Sphere2,n)  ; read x coord of a Vertices
        y.f = MP_VertexY (Sphere2,n)  ; read y coord of a Vertices
        z.f = MP_VertexZ (Sphere2,n)  ; read z coord of a Vertices
 
        MP_SetVertexU (Sphere2,n, MP_AngleCalculation(x,z)/360) ; write u.f, Angle / 360 =  from 0 to 1
        MP_SetVertexV (Sphere2,n, ASin(-y)/#PI+0.5)             ; write V.f
        
Next
Little "Studie" with the MP3D Library
http://rapidshare.de/files/46765015/Mapping.exe.html

Code: Select all


;////////////////////////////////////////////////////////////////
;//
;// Project Title: MP 3D Engine
;// File Title: Bewegende Objekte2.pb
;// Created On: 16.4.2008
;// Updated On: 
;// Author: Michael Paulwitz
;// OS:Windows
;// 
;// Objekte mit UV Koordinaten versehen
;//
;// http://www.mvps.org/directx/articles/spheremap.htm
;//
;////////////////////////////////////////////////////////////////

;-
;- ProgrammStart

Procedure MP_AngleCalculation (x1.f,y1.f) ; Winkelangabe von 0,0 Koodinaten aus gesehen

If x1 = 0 
   	If y1 > 0  
   	  AktTan.f = 350
   	Else
   	  AktTan.f = -350
   	EndIf
Else
    AktTan.f = y1 / x1
EndIf

  Winkel=Round((180.0/#PI)*(ATan(AktTan)),#PB_Round_Nearest);

  If x1 < 0 
     Winkel + 180
  Else
    If y1 < 0
       Winkel + 360
    EndIf   
  EndIf

  ProcedureReturn Winkel

EndProcedure

MP_Graphics3D (640,480,0,3) ; Erstelle ein WindowsFenster #Window = 0
SetWindowTitle(0, "2 Methodics for Spherical Mapping with x,y,z Coords") ; So soll es heissen

camera=MP_CreateCamera() ; Kamera erstellen
light=MP_CreateLight(1) ; Es werde Licht

If CreateImage(0, 255, 255)

    Font = LoadFont(#PB_Any, "Arial"  , 138) 
    StartDrawing(ImageOutput(0))

    Box(0, 0, 128, 128,RGB(255,0,0))
    Box(128, 0, 128, 128,RGB(0,255,0))
    Box(0, 128, 128, 128,RGB(0,0,255))
    Box(128, 128, 128, 128,RGB(255,255,0))

    DrawingFont(FontID(Font))
    DrawingMode(#PB_2DDrawing_Transparent)
    DrawText(73,35,"5")
  
    StopDrawing() ; This is absolutely needed when the drawing operations are finished !!! Never forget it !
    
EndIf

;Create Sphere
Sphere1 = MP_CreateSphere(12)
Sphere2 = MP_CreateSphere(12)
Teapot1 = MP_CreateTeapot()
Teapot2 = MP_CreateTeapot() ; Yes teapot not Sphere

MP_PositionEntity (Sphere1,-1.5,1.3,6 )
MP_PositionEntity (Sphere2,1.5,1.3,6 )
MP_PositionEntity (Teapot1,-1.5,-1.3,6 )
MP_PositionEntity (Teapot2,1.5,-1.3,6 )

;- Create Sphere1 Methodic 1, UV coordinate is calculated from the XY coordinate, with ASin   
For n = 0 To MP_CountVertices(Sphere1)-1
        x.f = MP_VertexX (Sphere1,n)
        y.f = MP_VertexY (Sphere1,n)
        MP_SetVertexU (Sphere1,n, ASin(x)/#PI+0.5)  ; Alternate fast methodic = x*0.5+0.5 
        MP_SetVertexV (Sphere1,n, ASin(-y)/#PI+0.5) ; Alternate fast methodic = -y*0.5+0.5
Next

;- Sphere2 with Methodic 2, UV coordinate is calculated with from the XY coordinate  
For n = 0 To MP_CountVertices(Sphere2)-1

        x.f = MP_VertexX (Sphere2,n)
        y.f = MP_VertexY (Sphere2,n)
        z.f = MP_VertexZ (Sphere2,n)
 
        MP_SetVertexU (Sphere2,n, MP_AngleCalculation(x,z)/360)
        MP_SetVertexV (Sphere2,n, ASin(-y)/#PI+0.5)
        
Next

;- Create Teapot and make you own uv Mapping
For n = 0 To MP_CountVertices(Teapot1)-1

        x.f = MP_VertexX (Teapot1,n)
        y.f = MP_VertexY (Teapot1,n)
        z.f = MP_VertexZ (Teapot1,n)
 
        MP_SetVertexU (Teapot1,n, ASin(x)/#PI+0.5) 
        MP_SetVertexV (Teapot1,n, ASin(-y)/#PI+0.5)
        
Next

;- Create Teapot and make you own uv Mapping
For n = 0 To MP_CountVertices(Teapot2)-1

        x.f = MP_VertexX (Teapot2,n)
        y.f = MP_VertexY (Teapot2,n)
        z.f = MP_VertexZ (Teapot2,n)
 
        MP_SetVertexU (Teapot2,n, MP_AngleCalculation(x,z)/360) 
        MP_SetVertexV (Teapot2,n, ASin(-y)/#PI+0.5)

Next

Texture = MP_ImageToTexture(0,0) ; Create Texture from image 

MP_EntityTexture (Sphere1, Texture ) ; textur to mesh
MP_EntityTexture (Sphere2, Texture ) ; textur to mesh
MP_EntityTexture (Teapot1, Texture ) ; textur to mesh
MP_EntityTexture (Teapot2, Texture ) ; textur to mesh

While Not MP_KeyDown(#PB_Key_Escape) And Not MP_WindowEvent() = #PB_Event_CloseWindow; Esc abfrage oder schliessen
  
    MP_TurnEntity (Sphere1,1,0,0.1) ; Moving Sphere
    MP_DrawText (10,10,MP_ARGB(255,255,255,255),"method 1"+Chr(10)+"Sphere") 

    MP_TurnEntity (Sphere2,1,0,0.1) ; Moving Sphere
    MP_DrawText (320,10,MP_ARGB(255,255,255,255),"method 2"+Chr(10)+"Sphere") 

    MP_TurnEntity (Teapot1,1,0,0.1) ; Moving Sphere
    MP_DrawText (10,240,MP_ARGB(255,255,255,255),"method 1 used on not Sphere Objekt") 

    MP_TurnEntity (Teapot2,1,0,0.1) ; Moving Sphere
    MP_DrawText (320,240,MP_ARGB(255,255,255,255),"method 2 used on not Sphere Objekt") 
    
    MP_RenderWorld () ; Hier gehts los
    MP_Flip () ; 

Wend

End

Working on - MP3D Library - PB 5.73 version ready for download