Ejemplo n.º 1
0
void PixelProcessing::ProcessTexture(Texture* in, Texture* out)
{
	const UINT width = in->GetSurfaceWidth();
	const UINT height = in->GetSurfaceHeight();

	DWORD *inSrf = (DWORD*)in->LockRect();
	DWORD *outSrf = (DWORD*)out->LockRect();

	for (int y=0; y<(int)height; y++)
	{
		for (int x=0; x<(int)width; x++)
		{
			ProcessPixel(&inSrf[y*width+x], &outSrf[y*width+x]);
		}
	}

	out->UnlockRect();
	in->UnlockRect();
}
Ejemplo n.º 2
0
void PixelProcessing::ProcessTexture(Texture* texture)
{
	const UINT width = texture->GetSurfaceWidth();
	const UINT height = texture->GetSurfaceHeight();

	DWORD *srf = (DWORD*)texture->LockRect();
	DWORD *tempSrf = new DWORD[texture->GetSurfaceWidth()*texture->GetSurfaceHeight()];

	for (int y=0; y<(int)height; y++)
	{
		for (int x=0; x<(int)width; x++)
		{
			ProcessPixel(&srf[y*width+x], &tempSrf[y*width+x]);
		}
	}

	for (int i=0; i<(int)(height*width); i++)
		srf[i] = tempSrf[i];

	texture->UnlockRect();
	SAFE_DELETE_ARR(tempSrf);
}
Ejemplo n.º 3
0
//=================================================================================================================================
/// Computes overdraw from a particular viewpoint
/// \param pCameraPosition  Camera position to use for this viewpoint.  The camera will be looking at the origin
/// \param nImageSize       Size of the pixel grid on each axis
/// \param bCullCCW         Set to true to cull CCW faces, otherwise cull CW faces.
/// \param pODArray         A table that will be updated with per-cluster overdraw
/// \return            False if out of memory.  True otherwise
//=================================================================================================================================
bool TootleRaytracer::ProcessViewpoint(const float* pCameraPosition, UINT nImageSize, bool bCullCCW, TootleOverdrawTable* pODArray)
{
    assert(pCameraPosition);

    if (nImageSize < 1)
    {
        nImageSize = 1;   // a strange 1x1 image
    }

    // build camera basis vectors
    Vec3f position(pCameraPosition);
    position      = position;
    Vec3f viewDir = Normalize(position) * -1.;
    Vec3f up;

    // Compute the up vector by performing 90 degree 2D rotation on the position vector
    //  (choose two good component vectors).
    if ((position[ 1 ] * position[ 1 ]) < (position[ 0 ] * position[ 0 ]))
    {
        up[ 0 ] = -position[ 2 ];
        up[ 1 ] =  0;
        up[ 2 ] =  position[ 0 ];
    }
    else
    {
        up[ 0 ] =  0;
        up[ 1 ] =  position[ 2 ];
        up[ 2 ] = -position[ 1 ];
    }

    up = Normalize(up);

    // choose viewport size:
    // transform bounding box corners into viewing space
    // as we do this, track the bounding square of the x and y coordinates
    // we will take the size of the larger dimension to be the viewport size
    Vec3f corners[8];
    m_pCore->GetSceneBB().GetCorners(corners);

    Matrix4f mLookAt = MatrixLookAt(position, Vec3f(0, 0, 0), up);
    float xmin = FLT_MAX, xmax = -FLT_MAX, ymin = FLT_MAX, ymax = -FLT_MAX;

    for (int i = 0; i < 8; i++)
    {
        TransformVector(&corners[i], &mLookAt, &corners[i]);
        xmin = Min(xmin, corners[i].x);
        xmax = Max(xmax, corners[i].x);
        ymin = Min(ymin, corners[i].y);
        ymax = Max(ymax, corners[i].y);
    }

    float fViewSize = Max(xmax - xmin, ymax - ymin) * 2;
    //float fViewSize = sqrt(pow(xmax-xmin,2) + pow(ymax-ymin,2)); //Max( xmax - xmin, ymax - ymin );

    // build the camera
    JRTOrthoCamera camera(position, viewDir, up, fViewSize);

    // cull backfaces
    m_pCore->CullBackfaces(viewDir, bCullCCW);

    // iterate over the pixels that we're interested in
    float delta = 1.0f / nImageSize;
    float s = 0;
    float t = 0;

#ifdef DEBUG_IMAGES
    JRTPPMImage img(nImageSize, nImageSize);
#endif

    for (int i = 0; i < (int)nImageSize; i++)
    {
        for (int j = 0; j < (int)nImageSize; j++)
        {
            // compute the camera ray for this pixel
            Vec3f rayOrigin, rayDirection;
            camera.GetRay(s, t, &rayOrigin, &rayDirection);

            // trace through the scene data structures to find all hits
            TootleRayHit* pHitArray = 0;
            UINT nHits = 0;

            if (!m_pCore->FindAllHits(rayOrigin, rayDirection, &pHitArray, &nHits))
            {
                // ran out of memory
                return false;
            }



#ifdef DEBUG_IMAGES
            float clr = nHits / 8.f;

            img.SetPixel(j, i, clr, clr, clr);

            /*if( nHits > 0 )
            {
               UINT nTriIndex = pHitArray[0].nFaceID;
               Vec3f normal = m_pMesh->GetFaceNormal( nTriIndex );
               normal /= 2;
               normal += Vec3f(0.5,0.5,0.5);
               img.SetPixel( j, i, normal.x, normal.y, normal.z );
            }*/

#endif

            ProcessPixel(pHitArray, nHits, pODArray);

            s += delta;
        }

        t += delta;
        s = 0;
    }

#ifdef DEBUG_IMAGES
    static int nFrameNum = 0;
    char filename[100];
    sprintf(filename, "C:\\images\\view_%d.ppm", nFrameNum);
    img.SaveFile(filename);
    nFrameNum++;
#endif

    return true;
}