Thread: U V Texture coordinate translation?

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    73

    U V Texture coordinate translation?

    Translating texture coordinates..? I'm not getting this to work and I'm not sure if I'm even going down the right path.. I've run out of ideas to try.. so I'll just post what I'm doing and see if any of you bright minds can assist me..
    All I get is my static background image drawn to the screen.. No movement or attempted movement (i.e. corrupt image)

    Below should be any code of relevance.. thnx for any help..

    Oh yeah I originally had rhw in my Custom vertex but I deleted it because I thought I read somewhere that texture transformations can't be done on transformed vertices. This true?

    Code:
    struct SCustomVertex
    {
    	float x;
    	float y;
    	float z;
    	DWORD color;
    	float tu;
    	float tv;
    };
    
    #define CustomVertexFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1)
    Code:
    SCustomVertex vertices[4] = 
    {
      {0.0f, 0.0f, 0.5f, 0xFFFFFFFF, 0.0f, 0.0f},
      {640.0f, 0.0f, 0.5f, 0xFFFFFFFF, 1.0f, 0.0f},
      {0.0f, 480.0f, 0.5f, 0xFFFFFFFF, 0.0f, 1.0f},
      {640.0f, 480.0f, 0.5f, 0xFFFFFFFF, 1.0f, 1.0f}
    };
    	
    HRESULT result;
    	
    result = g_Direct3dDevice->CreateVertexBuffer(sizeof(SCustomVertex) * 4, D3DUSAGE_WRITEONLY, CustomVertexFVF, D3DPOOL_DEFAULT, &g_VertexBuffer, NULL);
    	
    if(result != D3D_OK)
      return false;
    	
    void *tempPtr;
    
    result = g_VertexBuffer->Lock(0, sizeof(vertices), (void**)&tempPtr, 0);
    	
    if(result != D3D_OK)
      return false;
    
    memcpy(tempPtr, vertices, sizeof(vertices));
    	
    tempPtr = NULL;
    
    result = g_VertexBuffer->Unlock();
    	
    if(result != D3D_OK)
      return false;
    Code:
    g_Direct3dDevice->SetStreamSource(0, g_VertexBuffer, 0,	sizeof(SCustomVertex));
    
    g_Direct3dDevice->SetFVF(CustomVertexFVF);
    g_Direct3dDevice->SetTexture(0, g_BackgroundTexture.GetTexture());
    
    g_Direct3dDevice->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS,  D3DTTFF_COUNT2);
    		
    D3DXMATRIX backgroundScroll;
    				
    static float deltaU = 0.0f;
    deltaU++;
    		
    D3DXMatrixTranslation(&backgroundScroll, deltaU, 0.0f, 0.0f);
    		
    g_Direct3dDevice->SetTransform(D3DTS_TEXTURE0, &backgroundScroll);
    
    g_Direct3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0,	2);
    
    g_Direct3dDevice->EndScene();
    Last edited by Deo; 06-11-2005 at 09:30 PM.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Try using the following code:

    Code:
            D3DXMATRIXA16 backgroundScroll;
            				
            static float deltaU = 0.0f;
            deltaU += 0.001f;
            
            if(deltaU >= 1.0f)
              deltaU = 0.0f;
    
            D3DXMatrixIdentity(&backgroundScroll);
            backgroundScroll._31 = deltaU;
            pDev->SetTransform(D3DTS_TEXTURE0, &backgroundScroll);
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    73
    Well.. making some progress... the background is now scrolling.. albeit a bit corrupt... Very wavy on the side that is actually displaying the image followed by corruption on the right side... Tried messing with some texture wrapping and stuff as I can't think of anything else to do.. of course tht didnt' work

    I'll give a link to my

    .exe
    .bmp
    .cpp

    File in a zip file if it will help diagnose my problem.
    (Note: .cpp won't compile but I assume the .exe will run fine.. I never tried sharing a .exe before.. it was compiled under Win directx9.0c SDK)

    183k

    http://www.deo.hostvan.com/BackgroundScroll.zip

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Quote Originally Posted by Deo
    Well.. making some progress... the background is now scrolling.. albeit a bit corrupt... Very wavy on the side that is actually displaying the image followed by corruption on the right side... Tried messing with some texture wrapping and stuff as I can't think of anything else to do.. of course tht didnt' work

    I'll give a link to my

    .exe
    .bmp
    .cpp

    File in a zip file if it will help diagnose my problem.
    (Note: .cpp won't compile but I assume the .exe will run fine.. I never tried sharing a .exe before.. it was compiled under Win directx9.0c SDK)

    183k

    http://www.deo.hostvan.com/BackgroundScroll.zip
    Well, would've been nice to build the whole project but that's ok. It's most likely a problem in your texture addressing mode (which isn't found in your main.cpp). You should just be using the default which is D3DTADDRESS_WRAP for the u and v directions. I think that should fix your problem.

    edit: Looks like you are using D3DTADDRESS_CLAMP.
    Last edited by MrWizard; 06-12-2005 at 03:00 PM.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    73
    oop when I said I tried some texturing wrapping I was wrong.. I actually tried to set the texture addressing..

    Code:
    g_Direct3dDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP);
    I misread the documentation and thought D3DADDRESS_WRAP was texture wrapping..

    Anyways that didn't seem to do anything.

    Also tried setting the V but that didn't do anything..

    I'll zip up my entire project so ye' can compile it if yah want..
    (148k)
    http://www.deo.hostvan.com/EntireProject.zip

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Code:
    result = D3DXCreateTextureFromFileEx(device, filename,
    640, 
    480, 
    1, 
    D3DPOOL_DEFAULT, 
    D3DFMT_UNKNOWN, 
    D3DPOOL_DEFAULT, 
    D3DX_DEFAULT, 
    D3DX_DEFAULT, 
    D3DCOLOR_COLORVALUE(0.0f, 0.0f, 0.0f, 1.0f), 
    NULL, 
    NULL, 
    &m_Texture);
    That's your code. You need to specify D3DX_DEFAULT or 0 for the width and height. You're specifying non-power of 2 widths and heights which might not be supported for the device. Just use the default param and it'll round up to a power of 2 for you.
    Last edited by MrWizard; 06-12-2005 at 07:56 PM.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  7. #7
    Registered User
    Join Date
    May 2005
    Posts
    73
    ahhh I c... yep had to directly specify the width and height because if I don't then the LPD3DXSPRITE has a hissy fit and goes bonkers... though I guess I'll work around it...

    still get quite a bit of hiccups and stuttering when the background scrolling gets any decent speed...? tried meddling with different variables but still will give me a headache.. guess ill just keep the speed low for now..

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    It is warping like that because the aspect ratio is not correct. You can't stick a square texture in a rectangular quad the size of the screen, which has an aspect ratio of 1024/768 or 800/600 or about 1.6:1 and expect for there to be no distortion. Change the aspect ratio of your texture to match that of the screen or the quad.

  9. #9
    Registered User
    Join Date
    May 2005
    Posts
    73
    that thought never entered my mind..

    went back and changed aspect ratio ..

    1.6:1??

    or 1.33:1?

  10. #10
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Divide them and see.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. d3d9 c++ texture wrapper... again...
    By yaya in forum Game Programming
    Replies: 0
    Last Post: 04-01-2009, 01:08 PM
  2. D3d Texture Wrapper == ARRRGGGGHHH
    By yaya in forum Game Programming
    Replies: 1
    Last Post: 03-25-2009, 06:41 PM
  3. Replies: 4
    Last Post: 02-27-2008, 03:10 PM
  4. OpenGL - look trough surface -nehe lesson
    By GanglyLamb in forum Game Programming
    Replies: 8
    Last Post: 08-24-2006, 11:06 PM
  5. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM