终于进入纹理和光照部分了,我在网上看到有些人说这本书过时了,其实不然,SHADER也就是把这些东西怎么揉合在一起,而具体怎么揉合,这本书写的很清楚,可以说是力量之源。
这个DEMO主要是说颜色调制,就是RGB各项相乘,颜色与分量相成,不超过255
if ( ( ri = ( float ) ri * scalef ) > 255)
{
ri = 255;
}
if ( ( gi = ( float ) gi * scalef ) > 255)
{
gi = 255;
}
if ( ( bi = ( float ) bi * scalef ) > 255)
{
bi = 255;
}
先设定纹理大小和个数
#define TEXTSIZE 128
#define NUM_TEXT 12
纹理空间
BITMAP_IMAGE textures[12], temp_text;
初始化时建立位图。
for ( int itext = 0; itext < NUM_TEXT; itext++)
{
bitmap.Create_Bitmap( & textures[itext], ( SCREEN_WIDTH / 2) - ( TEXTSIZE / 2), ( SCREEN_HEIGHT / 2 ) - ( TEXTSIZE / 2), TEXTSIZE, TEXTSIZE, 16 );
bitmap.Load_Image_Bitmap16( & textures[itext],bitmap16bit, itext % 4, itext / 4, BITMAP_EXTRACT_MODE_CELL );
}
bitmap.Create_Bitmap( & temp_text, ( SCREEN_WIDTH / 2) - ( TEXTSIZE / 2), ( SCREEN_HEIGHT / 2) - ( TEXTSIZE / 2), TEXTSIZE, TEXTSIZE, 16 );
bitmap.Load_Image_Bitmap16( & temp_text, & bitmap16bit, 0, 0, BITMAP_EXTRACT_MODE_CELL );
bitmap.Unload_Bitmap_File( &bitmap16bit );
srand( mytool.Start_Clock());
初始化
bitmap.Load_Bitmap_File( bitmap16bit, "OMPTXT128_24.BMP");
for ( int itext = 0; itext < NUM_TEXT; itext++)
{
bitmap.Create_Bitmap( & textures[itext], ( SCREEN_WIDTH / 2) - ( TEXTSIZE / 2), ( SCREEN_HEIGHT / 2 ) - ( TEXTSIZE / 2), TEXTSIZE, TEXTSIZE, 16 );
bitmap.Load_Image_Bitmap16( & textures[itext],bitmap16bit, itext % 4, itext / 4, BITMAP_EXTRACT_MODE_CELL );
}
bitmap.Create_Bitmap( & temp_text, ( SCREEN_WIDTH / 2) - ( TEXTSIZE / 2), ( SCREEN_HEIGHT / 2) - ( TEXTSIZE / 2), TEXTSIZE, TEXTSIZE, 16 );
bitmap.Load_Image_Bitmap16( & temp_text, & bitmap16bit, 0, 0, BITMAP_EXTRACT_MODE_CELL );
bitmap.Unload_Bitmap_File( &bitmap16bit );
srand( mytool.Start_Clock());
加上个复制位图
int ddraw_bitmap::Copy_Bitmap( BITMAP_IMAGE_PTR dest_bitmap, int dest_x, int dest_y, BITMAP_IMAGE_PTR source_bitmap, int source_x, int source_y, int width, int height )
{
if ( !dest_bitmap || ! source_bitmap )
{
return ( 0 );
}
int bytes_per_pixel = ( source_bitmap->bpp >> 3 );
UCHAR * source_ptr = source_bitmap->buffer + ( source_x + source_y *source_bitmap->width ) * bytes_per_pixel;
UCHAR * dest_ptr = dest_bitmap->buffer + ( dest_x + dest_y * dest_bitmap->width ) * bytes_per_pixel;
for ( int y = 0 y < height; y++)
{
memcpy( dest_ptr, source_ptr, bytes_per_pixel );
source_ptr += ( source_bitmap->width * bytes_per_pixel );
dest_ptr += ( dest_bitmap->width * bytes_per_pixel );
}
return ( 1 );
}
在Game_Main()中,先拷贝位图
bitmap->Copy_Bitmap( & temp_text, 0 ,0, & textures[curr_texture], 0, 0, TEXTSIZE, TEXTSIZE );
加上个565的图像提取像素,
#define _RGB565FROM16BIT( RGB, r, g, b ) { * r = ((( RGB)>>11) & 0x1f); *g = ((( RGB)>>5) & 0x3f); *b=(( RGB) & 0x1f );
然后进行调制,也就是像素RGB*常数
USHORT * pbuffer = ( USHORT * )temp_text.buffer;
for ( int iy = 0; iy < temp_text.height; iy ++)
{
for ( int ix = 0; ix < temp_text.width; ix ++)
{
USHORT pixel = pbuffer[iy * temp_text.width + ix];
int ri, gi, bi;
_RGB565FROM16BIT( pixel, &ri, &gi, &bi );
if ( ( ri = ( float ) ri * scalef ) > 255)
{
ri = 255;
}
if ( ( gi = ( float ) gi * scalef ) > 255)
{
gi = 255;
}
if ( ( bi = ( float ) bi * scalef ) > 255)
{
bi = 255;
}
pbuffer[iy * temp_text.width + ix ] = _RGB16BIT565( ri, gi, bi );
}
}
根据键盘上下左右键进行调整。
if ( KEYDOWN( VK_RIGHT))
{
if (++curr_texture > ( NUM_TEXT - 1 ))
{
curr_texture = ( NUM_TEXT - 1 );
}
mytool.Wait_Clock( 100 );
}
if ( KEYDOWN( VK_LEFT))
{
if ( --curr_texture<0)
{
curr_texture = 0;
mytool.Wait_Clock( 10 );
}
}
if ( KEYDOWN( VK_UP))
{
scalef += 0.01;
if ( scalef > 10)
{
scalef = 10;
}
mytool.Wait_Clock(10);
}
if ( KEYDOWN( VK_DOWN))
{
scalef -= 0.01;
if ( scalef < 0)
{
scalef = 0;
}
mytool.Wait_Clock( 10 );
}
发现没达到预期目的
逐个排查,有问题的函数。
Load_Bitmap_File()在24位加载的时候有问题。
应该是
_lread( file_handle,temp_buffer, bitmap->bitmapinfoheader.biSizeImage );
USHORT COLOR;而不是UCHAR color;
本来想纠正下,但是拷贝源代码后,发现无法运行,只好先用目前的样子了,
不过觉得不对,因为在源位图的12个分图上,没有例子的那个图形