Ejemplo n.º 1
0
//----------------------------------------------------------------------------
void Renderer::EnableLighting(const ColorRGB& rAmbient)
{
	const GXColor amb = { rAmbient.R()*255, rAmbient.G()*255,
		rAmbient.B()*255, 255 };
	GXSetChanAmbColor(GX_COLOR0A0, amb);

	GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGBA8, 0);
}
Ejemplo n.º 2
0
void SaveBmp(const string &aFileName,const Image<ColorRGB> &aImage)
{
    FILE* pFile = fopen(aFileName.c_str(),"wb+");
    
    BitmapFileHeader oFileHeader;
    BitmapInfoHeader oInfoHeader;

    oInfoHeader.biWidth  = aImage.GetWidth();
    oInfoHeader.biHeight = aImage.GetHeight();

    //We compute stride sizes. A stride must be a factor
    //of 4.
    int StrideSize = oInfoHeader.biWidth * 3;
    if((oInfoHeader.biWidth*3)%4)
    {
        StrideSize += 4-((oInfoHeader.biWidth*3)%4);
    }

    //Only 24 bit mot is encoded (1 char per color components)
    //other modes require a palette, which would be a big more
    //complicated here.
    oInfoHeader.biBitCount  = 24;
    oInfoHeader.biSizeImage = StrideSize * oInfoHeader.biHeight;
    
    //Offset & size. (File header = 14 bytes, File info = 40 bytes)
    oFileHeader.bfOffBits = 54;
    oFileHeader.bfSize =    54 + sizeof(char) * 
                            StrideSize * 
                            oInfoHeader.biHeight;

    unsigned char * pBuffer = new unsigned char[StrideSize];

    //Write header information in the bmp file.
    oFileHeader.Write(pFile);
    oInfoHeader.Write(pFile);

    //Write raster data in the bmp file.
    //The y axis is inverted in bmp files, this is why we're
    //copying lines backward.
    for(int i = oInfoHeader.biHeight-1 ;i >=0; --i)
    {
        for(int j = 0; j < oInfoHeader.biWidth; ++j)
        {
            ColorRGB PixelColor = 
                aImage.GetRasterData()[i*oInfoHeader.biWidth + j];
            PixelColor.Clamp();

            pBuffer[j*3]    =    unsigned char(PixelColor.B()*255);
            pBuffer[j*3+1]  =    unsigned char(PixelColor.G()*255);
            pBuffer[j*3+2]  =    unsigned char(PixelColor.R()*255);
        }
        fwrite(pBuffer,StrideSize,1,pFile);
    }

    delete[] pBuffer;

    fclose(pFile);
}
Ejemplo n.º 3
0
//----------------------------------------------------------------------------
Texture2D* Sample5::CreateTexture()
{
	if (mspTexture)
	{
		return mspTexture;
	}

	// define the properties of the image to be used as a texture
	const UInt width = 256;
	const UInt height = 256;
	const Image2D::FormatMode format = Image2D::FM_RGB888;
	const UInt bpp = Image2D::GetBytesPerPixel(format);
	ColorRGB* const pColorDst = WIRE_NEW ColorRGB[width*height];

	// create points with random x,y position and color
	TArray<Cell> cells;
	Random random;
	for (UInt i = 0; i < 10; i++)
	{
		Cell cell;
		cell.point.X() = random.GetFloat() * width;
		cell.point.Y() = random.GetFloat() * height;
		cell.color.R() = random.GetFloat();
		cell.color.G() = random.GetFloat();
		cell.color.B() = random.GetFloat();

		Float max = 0.0F;
		max = max < cell.color.R() ? cell.color.R() : max;
		max = max < cell.color.G() ? cell.color.G() : max;
		max = max < cell.color.B() ? cell.color.B() : max;
		max = 1.0F / max;
		cell.color *= max;
		cells.Append(cell);
	}

	// iterate over all texels and use the distance to the 2 closest random
	// points to calculate the texel's color
	Float max = 0;
	for (UInt y = 0; y < height; y++)
	{
		for (UInt x = 0; x < width; x++)
		{
			Float minDist = MathF::MAX_REAL;
			Float min2Dist = MathF::MAX_REAL;
			UInt minIndex = 0;

			for (UInt i = 0; i < cells.GetQuantity(); i++)
			{
				Vector2F pos(static_cast<Float>(x), static_cast<Float>(y));

				// Handle tiling
				Vector2F vec = cells[i].point - pos;
				vec.X() = MathF::FAbs(vec.X());
				vec.Y() = MathF::FAbs(vec.Y());
				vec.X() = vec.X() > width/2 ? width-vec.X() : vec.X();
				vec.Y() = vec.Y() > height/2 ? height-vec.Y() : vec.Y();

				Float distance = vec.Length();

				if (minDist > distance)
				{
					min2Dist = minDist;
					minDist = distance;
					minIndex = i;
				}
				else if (min2Dist > distance)
				{
					min2Dist = distance;
				}
			}

			Float factor = (min2Dist - minDist) + 3;
			ColorRGB color = cells[minIndex].color * factor;
			pColorDst[y*width+x] = color;

			max = max < color.R() ? color.R() : max;
			max = max < color.G() ? color.G() : max;
			max = max < color.B() ? color.B() : max;
		}
	}

	// convert and normalize the ColorRGBA float array to an 8-bit per
	// channel texture
	max = 255.0F / max;
	UChar* const pDst = WIRE_NEW UChar[width * height * bpp];
	for (UInt i = 0; i < width*height; i++)
	{
		ColorRGB color = pColorDst[i];
		pDst[i*bpp] = static_cast<UChar>(color.R() * max);
		pDst[i*bpp+1] = static_cast<UChar>(color.G() * max);
		pDst[i*bpp+2] = static_cast<UChar>(color.B() * max);
	}

	Image2D* pImage = WIRE_NEW Image2D(format, width, height, pDst);
	Texture2D* pTexture = WIRE_NEW Texture2D(pImage);
	// The texture tiles are supposed to be seamless, therefore
	// we need the UV set to be repeating.
	pTexture->SetWrapType(0, Texture2D::WT_REPEAT);
	pTexture->SetWrapType(1, Texture2D::WT_REPEAT);

	// save the texture for later reference
	mspTexture = pTexture;

	return pTexture;
}