XNA Creators Club Online
Page 1 of 1 (10 items)
Sort Posts: Previous Next

Alphamaps, MipMaps, Selection

Last post 03/11/2009 15:25 by Plerion. 9 replies.
  • 02/11/2009 18:00

    Alphamaps, MipMaps, Selection

    Hello everybody!

    Im working on an implementation of a modelfile from a game. Its actually for learning purpose. Now im facing 3 problems. I dont know if these are simple problems or if its a harder one, but ill post'em here:

    1. http://www.imagr.eu/up/4aeea600602bc_agyria.JPG

    As you can see it has a problem when i go away from the texture. If i go away very far it gets a picture with only green and blue squares. I think its a problem with the Mipmaps but i dont know what exactly!


    2. Alphamaps:

    I have 4 layers of textures. every layer has its own 64 x 64 alphatexture. The first layer hast full opacity on every vertex but the next 3 layers have different alphavalues on the vertices. Now im trying to get the blending with the following rendering:

    1 dev->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE); 
    2 dev->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1); 
    3 dev->SetTexture(0, m_alphaMaps[i]); 
    4 dev->SetTextureStageState(1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE); 
    5 dev->SetTextureStageState(1, D3DTSS_ALPHAARG2, D3DTA_CURRENT); 
    6 dev->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG2); 
    7 m_3dMgr->SetTexture(m_textures[i], 1); 


    Now actually the textures dont have the alpha-value of the alphamap-texture but either 0 or 255.


    3. Selection

    I need to get the selectionpoint in my terrain of the mouse. I actually calculate the point on zNear and on zFar but now i got stuck and dont know what to do next to get the selectionpoint. I cant intersect it with a mesh because the terrain isnt rendered as a mesh but vertex-per-vertex.

    It would be really cool if someone has a lill hint bout that!

    Greetings
    Plerion
  • 02/11/2009 19:05 In reply to

    Re: Alphamaps, MipMaps, Selection

    1.    How do you create and load your textures?
    2.    Please use shaders or do you need to run on really old hardware?
    3.    In the 3D context selection is called picking. You should find plenty of samples when you use your favorite search engine with this.
  • 02/11/2009 19:24 In reply to

    Re: Alphamaps, MipMaps, Selection

    Hello!

    I load the textures out of memory with the following code:
    ui32 levelcount = 0; 
        for(ui32 i = 0; i < 16; ++i) 
            if(offsets[i] && sizes[i]) 
                ++levelcount; 
     
        IDirect3DTexture9* tex; 
     
        if(device->CreateTexture(w, h, levelcount, 0, format, D3DPOOL_MANAGED, &tex, 0) != D3D_OK) 
        { 
            return 0; 
        } 
     
        for(ui32 i = 0, a = 0; i < 16; ++i) 
        { 
            if(offsets[i] && sizes[i]) 
            { 
                ui8* data = new ui8[sizes[i]]; 
                f->Read(offsets[i], sizes[i], data); 
                IDirect3DSurface9* surf; 
                tex->GetSurfaceLevel(a, &surf); 
                D3DSURFACE_DESC desc; 
                surf->GetDesc(&desc); 
                RECT r; 
                r.left = r.top = 0; 
                r.right = desc.Width; 
                r.bottom = desc.Height; 
                if(D3DXLoadSurfaceFromMemory(surf, 0, 0, data, format, blocksize * r.right, 0, &r, D3DX_DEFAULT, 0xFF000000) != D3D_OK) 
                { 
                    MessageBox(0, "Load from Memory""Failed!", MB_OK); 
                    // return 0; // TODO: Figure out if we should cancle here or just try loading the next mipmap... 
                } 
                ++a; 
            } 
        } 

    where format is one of the following constants:
    D3DFMT_DXT1
    D3DFMT_DXT3
    D3DFMT_DXT5

    2. Hm, i dont know exactly what you mean with using shaders. I have the following situation:
    4 textures with colors and stuff like that, just normal textures with no specific alphavalue
    4 textures with alphavalues. just alphavalues for the texture in this terraintile.

    It is that you can use one texture for an arbitrary number of terrains. What i tried now was the following:
            dev->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE); 
            dev->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1); 
            dev->SetTexture(0, m_alphaMaps[i]); 
            dev->SetTextureStageState(1, D3DTSS_ALPHAARG1, D3DTA_CURRENT); 
            dev->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1); 
            dev->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_SELECTARG1); 
            dev->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE); 
            m_3dMgr->SetTexture(m_textures[i], 1); 
            dev->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); 
            dev->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); 
            dev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); 

    Now the image is pretty wierd, it has black stripes all over it.

    3. Ok, ill have a look, what i can find :D

    Greetings
  • 02/11/2009 23:28 In reply to

    Re: Alphamaps, MipMaps, Selection

    Shader make blending operations just easier to write. For example a modulation operation is just written as color1 * color2, additional blending will be color1 + color2, thats why we are asking please use shaders. So the question is do you have to support pre DX9.0 hardware if not you can just aswell do all this using shaders.

    For the picking techniques you really don't want to do a collision detection between all the triangles in the scene, you first want to do a AABB or Bounding Sphere collision detection so you end up with all the objects that are actually on the line you are picking on. This will improve your picking speed massively. It sounds like you want to restructure your terrain a little bit so you don't have to check all your triangles in the terrain even if your only checking the visible ones this will be a massive performance hit.
  • 03/11/2009 0:13 In reply to

    Re: Alphamaps, MipMaps, Selection

    Ok, that shading sounds interesting, im pretty new to DX, so i think i will have look on that. But i cant really understand why the code above isnt working and presents this image:
    http://www.imagr.eu/up/4aef7403d10bd_agerr.JPG

    Thats how i understood it:
    dev->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE); // push the alphamap as arg1
    dev->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1); // select arg1
    dev->SetTexture(0, m_alphaMaps[i]); // set the alphamap as stage 0

    dev->SetTextureStageState(1, D3DTSS_ALPHAARG1, D3DTA_CURRENT); // alphaarg1 is the alphamap
    dev->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1); // select alpha from alphamap
    dev->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_SELECTARG1); // select color from texture
    dev->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE); // take the texture as arg1
    m_3dMgr->SetTexture(m_textures[i], 1); // set the texture as stage 1

    m_3dMgr->DrawPrimitives(m_vbuffer, D3DPT_TRIANGLESTRIP, m_striplen, VERTEX_3FT2, m_indices, 145); // draw the composition

    But oviously it doesnt produce that!

  • 03/11/2009 8:11 In reply to

    Re: Alphamaps, MipMaps, Selection

    Try reading the section Creating Blending Stages (Direct3D 9) because you are setting your alpha and color stages in a wrong way.

    From the docs.
    #define SetTextureColorStage( dev, i, arg1, op, arg2 )     \
    dev->SetTextureStageState( i, D3DTSS_COLOROP, op);      \
    dev->SetTextureStageState( i, D3DTSS_COLORARG1, arg1 ); \
    dev->SetTextureStageState( i, D3DTSS_COLORARG2, arg2 );

    #define SetTextureAlphaStage( dev, i, arg1, op, arg2 )     \
    dev->SetTextureStageState( i, D3DTSS_ALPHAOP, op);      \
    dev->SetTextureStageState( i, D3DTSS_ALPHAARG1, arg1 );  \
    dev->SetTextureStageState( i  D3DTSS_ALPHAARG2, arg2 );
  • 03/11/2009 9:02 In reply to

    Re: Alphamaps, MipMaps, Selection

    Hmm, ok, does that mean, i performed the actions in the wrong order? I changed it to:

    dev->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1); // alphastage
    dev->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
    dev->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_TEXTURE);

    dev->SetTexture(0, m_alphaMaps[i]);

    dev->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1); // alphastage
    dev->SetTextureStageState(1, D3DTSS_ALPHAARG1, D3DTA_CURRENT);
    dev->SetTextureStageState(1, D3DTSS_ALPHAARG2, D3DTA_TEXTURE);

    dev->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_SELECTARG1); // colorstage
    dev->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
    dev->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);

    m_3dMgr->SetTexture(m_textures[i], 1);

    But it has no visible change.
  • 03/11/2009 11:40 In reply to

    Re: Alphamaps, MipMaps, Selection

    Call SetTexture() before SetTextureStageState().
    Make sure you have texture coordinates for all stages.
    If you're using same texture coordinates for all stages, you can call SetTextureStageState(Stage, D3DTSS_TEXCOORDINDEX, StageWithCoord).
    For example SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 0) set texture for stage 1 same as for stage 0.
    Set color operations before alpha operations.
    Make sure your textures have alphachannel.

    Why in one case you're using dev->SetTexture(0, m_alphaMaps[i]) and in other m_3dMgr->SetTexture(m_textures[i], 1)?
    Explain meaning of your texture stages. Something like: 0 - Base texture, 1 - Diffuse light map, 2 - Specular light map, 3 - Bump map and so on.

    Also, make simple project drawing a single quadrate with textures and experiment with it.


  • 03/11/2009 13:10 In reply to

    Re: Alphamaps, MipMaps, Selection

    Hello Erida!

    Thank you for your tips! I tried everything, but it still is the same picture. The code looks like that now:
    dev->SetTexture(0, m_textures[i]); 
    dev->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); // alphastage 
    dev->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); 
    dev->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_TEXTURE); 
     
    dev->SetTexture(1, m_alphaMaps[i]); 
    dev->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_SELECTARG2); // colorstage 
    dev->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE); 
    dev->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT); 
     
    dev->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG2); // alphastage 
    dev->SetTextureStageState(1, D3DTSS_ALPHAARG1, D3DTA_CURRENT); 
    dev->SetTextureStageState(1, D3DTSS_ALPHAARG2, D3DTA_TEXTURE); 


    The meaning of the textures is as following:
    Stage 0: Basetexture for the colors (no alpha should be taken from there)
    Stage 1: Alphatexture for the alphavalues (no color should be taken from here and together with the colors from stage 1 this should result in the ARGB)

    Now the alphatexture has only an alphachannel and no colors (D3DFMT_A8) and the basetexture is either DXT1, DXT3 or DXT5.

    The texcoords and alphacoords are correct as they work in my openGL-application:
    struct Vertex3FT2 : public Vertex 
        float u, v, u2, v2; 
    }; 


    vert.x = xbase + x; 
    vert.y = zbase + z; 
    vert.z = ybase + y; 
    vert.u = m_texcoords[counter][0]; 
    vert.v = m_texcoords[counter][1]; 
    vert.u2 = m_alphacoords[counter][0]; 
    vert.v2 = m_alphacoords[counter][1]; 
    m_vertices[counter++] = vert; 

  • 03/11/2009 15:25 In reply to

    Re: Alphamaps, MipMaps, Selection

    The alphating is fixed! I read the alphavalues from the wrong buffer. Im very sorry for the inconveniences...

    The problem that rests is the one with the blue and green squares that occure when i go away from the terrain!
Page 1 of 1 (10 items) Previous Next