Thursday, September 22, 2016

"MidSem" break Development

Goals:
* Finish Bitmap stack reading.
#tested previous code under VS2013

* Get Cuda software tested and installed

Make sure Linnet is on track with interface stuff.
Possibly help her with

problems:

Downloaded VS 2013 Community Edition which is not supported on "cross x86_32 on x86_64" architectures

Got a CUDA card - but my power supply is not compatible with it.
Switched to Dell Inspiron lap top and CUDA is running

found the "picking" demo that handles mouse interaction with a 3D model
code format is different to other simpler example file s- ie the spinning cubes - classes are defined.

some code to work through:
//--------------------------------------------------------------------------------------
// Checks if mouse point hits geometry the scene.
//--------------------------------------------------------------------------------------
HRESULT Pick()
{
    HRESULT hr;
    D3DXVECTOR3 vPickRayDir;
    D3DXVECTOR3 vPickRayOrig;
    const DXGI_SURFACE_DESC* pd3dsdBackBuffer = DXUTGetDXGIBackBufferSurfaceDesc();

    g_nNumIntersections = 0L;

    // Get the pick ray from the mouse position
    if( GetCapture() )
    {
        const D3DXMATRIX* pmatProj = g_Camera.GetProjMatrix();

        POINT ptCursor;
        GetCursorPos( &ptCursor );
        ScreenToClient( DXUTGetHWND(), &ptCursor );

        // Compute the vector of the pick ray in screen space
        D3DXVECTOR3 v;
        v.x = ( ( ( 2.0f * ptCursor.x ) / pd3dsdBackBuffer->Width ) - 1 ) / pmatProj->_11;
        v.y = -( ( ( 2.0f * ptCursor.y ) / pd3dsdBackBuffer->Height ) - 1 ) / pmatProj->_22;
        v.z = 1.0f;

        // Get the inverse view matrix
        const D3DXMATRIX matView = *g_Camera.GetViewMatrix();
        const D3DXMATRIX matWorld = *g_Camera.GetWorldMatrix();
        D3DXMATRIX mWorldView = matWorld * matView;
        D3DXMATRIX m;
        D3DXMatrixInverse( &m, NULL, &mWorldView );

        // Transform the screen space pick ray into 3D space
        vPickRayDir.x = v.x * m._11 + v.y * m._21 + v.z * m._31;
        vPickRayDir.y = v.x * m._12 + v.y * m._22 + v.z * m._32;
        vPickRayDir.z = v.x * m._13 + v.y * m._23 + v.z * m._33;
        vPickRayOrig.x = m._41;
        vPickRayOrig.y = m._42;
        vPickRayOrig.z = m._43;
    }

    // Get the picked triangle
    if( GetCapture() )
    {
        ID3DX10Mesh* pMesh;
        const D3D10_INPUT_ELEMENT_DESC* pDesc = NULL;
        UINT nDeclCount;
        g_pD3DXMesh->GetVertexDescription( &pDesc, &nDeclCount);
        g_pD3DXMesh->CloneMesh( D3DX10_MESH_32_BIT, pDesc[0].SemanticName, pDesc, nDeclCount, &pMesh);

        DWORD* pIndices;
        D3DVERTEX* pVertices;
        pVertices = (D3DVERTEX*)g_Mesh.GetRawVerticesAt(0);
        pIndices = (DWORD*)g_Mesh.GetRawIndicesAt(0);

        if( g_bUseD3DXIntersect )
        {
            // When calling D3DXIntersect, one can get just the closest intersection and not
            // need to work with a D3DXBUFFER.  Or, to get all intersections between the ray and
            // the mesh, one can use a D3DXBUFFER to receive all intersections.  We show both
            // methods.
            if( !g_bAllHits )
            {
                // Collect only the closest intersection
                UINT nFace;
                FLOAT fBary1, fBary2, fDist;
                UINT nHitCount = 0;
                g_pD3DXMesh->Intersect( &vPickRayOrig, &vPickRayDir, &nHitCount, &nFace, &fBary1, &fBary2, &fDist, NULL);
                if( nHitCount > 0 )
                {
                    g_nNumIntersections = 1;
                    g_IntersectionArray[0].dwFace = nFace;
                    g_IntersectionArray[0].fBary1 = fBary1;
                    g_IntersectionArray[0].fBary2 = fBary2;
                    g_IntersectionArray[0].fDist = fDist;
                }
                else
                {
                    g_nNumIntersections = 0;
                }
            }
            else
            {
                // Collect all intersections
                ID3D10Blob* pBuffer = NULL;
                D3DXINTERSECTINFO* pIntersectInfoArray;
                if( FAILED( hr = g_pD3DXMesh->Intersect( &vPickRayOrig, &vPickRayDir, &g_nNumIntersections, NULL,
                                                         NULL, NULL, NULL, &pBuffer) ) )
                {
                    SAFE_RELEASE( pMesh );
                    return hr;
                }
                if( g_nNumIntersections > 0 )
                {
                    pIntersectInfoArray = ( D3DXINTERSECTINFO* )pBuffer->GetBufferPointer();
                    if( g_nNumIntersections > MAX_INTERSECTIONS )
                        g_nNumIntersections = MAX_INTERSECTIONS;
                    for( DWORD iIntersection = 0; iIntersection < g_nNumIntersections; iIntersection++ )
                    {
                        g_IntersectionArray[iIntersection].dwFace = pIntersectInfoArray[iIntersection].FaceIndex;
                        g_IntersectionArray[iIntersection].fBary1 = pIntersectInfoArray[iIntersection].U;
                        g_IntersectionArray[iIntersection].fBary2 = pIntersectInfoArray[iIntersection].V;
                        g_IntersectionArray[iIntersection].fDist = pIntersectInfoArray[iIntersection].Dist;
                    }
                }
                SAFE_RELEASE( pBuffer );
            }

        }
        else
        {
            // Not using D3DX
            DWORD dwNumFaces = (DWORD)g_Mesh.GetNumIndices(0)/3;
            FLOAT fBary1, fBary2;
            FLOAT fDist;
            for( DWORD i = 0; i < dwNumFaces; i++ )
            {
                D3DXVECTOR3 v0 = pVertices[pIndices[3 * i + 0]].p;
                D3DXVECTOR3 v1 = pVertices[pIndices[3 * i + 1]].p;
                D3DXVECTOR3 v2 = pVertices[pIndices[3 * i + 2]].p;

                // Check if the pick ray passes through this point
                if( IntersectTriangle( vPickRayOrig, vPickRayDir, v0, v1, v2,
                                       &fDist, &fBary1, &fBary2 ) )
                {
                    if( g_bAllHits || g_nNumIntersections == 0 || fDist < g_IntersectionArray[0].fDist )
                    {
                        if( !g_bAllHits )
                            g_nNumIntersections = 0;
                        g_IntersectionArray[g_nNumIntersections].dwFace = i;
                        g_IntersectionArray[g_nNumIntersections].fBary1 = fBary1;
                        g_IntersectionArray[g_nNumIntersections].fBary2 = fBary2;
                        g_IntersectionArray[g_nNumIntersections].fDist = fDist;
                        g_nNumIntersections++;
                        if( g_nNumIntersections == MAX_INTERSECTIONS )
                            break;
                    }
                }
            }
        }

        // Now, for each intersection, add a triangle to g_pVB and compute texture coordinates
        if( g_nNumIntersections > 0 )
        {
            D3DVERTEX* v;
            D3DVERTEX* vThisTri;
            DWORD* iThisTri;
            D3DVERTEX v1, v2, v3;
            INTERSECTION* pIntersection;

            g_pVB->Map( D3D10_MAP_WRITE_DISCARD, 0, ( void** )&v );

            for( DWORD iIntersection = 0; iIntersection < g_nNumIntersections; iIntersection++ )
            {
                pIntersection = &g_IntersectionArray[iIntersection];

                vThisTri = &v[iIntersection * 3];
                iThisTri = &pIndices[3 * pIntersection->dwFace];
                // get vertices hit
                vThisTri[0] = pVertices[iThisTri[0]];
                vThisTri[1] = pVertices[iThisTri[1]];
                vThisTri[2] = pVertices[iThisTri[2]];

                // If all you want is the vertices hit, then you are done.  In this sample, we
                // want to show how to infer texture coordinates as well, using the BaryCentric
                // coordinates supplied by D3DXIntersect
                FLOAT dtu1 = vThisTri[1].tu - vThisTri[0].tu;
                FLOAT dtu2 = vThisTri[2].tu - vThisTri[0].tu;
                FLOAT dtv1 = vThisTri[1].tv - vThisTri[0].tv;
                FLOAT dtv2 = vThisTri[2].tv - vThisTri[0].tv;
                pIntersection->tu = vThisTri[0].tu + pIntersection->fBary1 * dtu1 + pIntersection->fBary2 * dtu2;
                pIntersection->tv = vThisTri[0].tv + pIntersection->fBary1 * dtv1 + pIntersection->fBary2 * dtv2;
            }

            g_pVB->Unmap();
        }


        SAFE_RELEASE( pMesh );
       
    }

    return S_OK;
}







No comments:

Post a Comment