粗略看了下,这个DEMO加上了个光照图,
首先定义了个灯光数量,
#define NUM_LAMP 4
以及光照图数组
BITMAP_IMAGE lightmaps[NUM_LAMP];
初始化时,位置不同了
{
bitmap->Create_Bitmap( & textures[itext], ( SCREEN_WIDTH / 2) - 4 *( TEXTSIZE / 2), ( SCREEN_HEIGHT / 2 ) - 2 *( TEXTSIZE / 2), TEXTSIZE, TEXTSIZE, 16 );
bitmap->Create_Bitmap( & temp_text, ( SCREEN_WIDTH / 2) - ( TEXTSIZE / 2), ( SCREEN_HEIGHT / 2) + ( TEXTSIZE / 2), TEXTSIZE, TEXTSIZE, 16 );
光照图的初始化
bitmap->Load_Bitmap_File( & bitmap16bit, "lightmaps128_24.BMP");
for ( int itext = 0; itext < NUM_LAMP; itext++)
{
bitmap->Create_Bitmap( & lightmaps[itext], ( SCREEN_WIDTH / 2) + 2 *( TEXTSIZE / 2), ( SCREEN_HEIGHT / 2 ) - 2 *( TEXTSIZE / 2), TEXTSIZE, TEXTSIZE, 16 );
bitmap->Load_Image_Bitmap16( & lightmaps[itext],&bitmap16bit, itext % 4, itext / 4, BITMAP_EXTRACT_MODE_CELL );
}
bitmap->Unload_Bitmap_File( &bitmap16bit );
把数值改了下
int curr_texture = 7;
float scalef = 0.5;
int curr_lightmap = 1;
这个DEMO是用了下面方式调制
Pixel_dest[x,y]rgb = pixel_source[x,y]rgb * ambient +
// pixel_source[x,y]rgb * light_map[x,y]rgb
其实,知道原理了,剩下的都顺理成章了
USHORT * sbuffer = ( USHORT * )textures[curr_texture].buffer;
USHORT * lbuffer = ( USHORT * )lightmaps[curr_lightmap].buffer;
USHORT * tbuffer = ( USHORT * )temp_text.buffer;
for ( int iy = 0; iy < temp_text.height; iy ++)
{
for ( int ix = 0; ix < temp_text.width; ix ++)
{
int rs, gs, bs;
int rl, gl, bl;
int rf, gf, bf;
USHORT spixel = sbuffer[iy * temp_text.width + ix];
_RGB565FROM16BIT( spixel, &rs, &gs, &bs );
USHORT lpixel = lbuffer[iy * temp_text.width + ix];
_RGB565FROM16BIT( lpixel, &rs, &gs, &bs );
rf = ( scalef * (float) rs ) + ( ( float) rs * (float )rl/(float)64 );
gf = ( scalef * (float) gs ) + ( ( float) gs * (float )rl/(float)64 );
bf = ( scalef * (float) bs ) + ( ( float) bs * (float )rl/(float)64 );
if ( rf > 255 )
{
rf = 255;
}
if ( gf > 255 )
{
gf = 255;
}
if ( bf > 255 )
{
bf = 255;
}
tbuffer[iy * temp_text.width + ix] = _RGB16BIT565( rf, gf, bf );
}
}
bitmap->Draw_Bitmap16( &temp_text, ddraw->getbackbuffer(), ddraw->getbacklpitch(), 0);
bitmap->Draw_Bitmap16( &textures[curr_texture], ddraw->getbackbuffer(), ddraw->getbacklpitch(), 0);
bitmap->Draw_Bitmap16( &lightmaps[curr_lightmap], ddraw->getbackbuffer(), ddraw->getbacklpitch(), 0);
结果如下图