GLSL per-pixel lighting + spot texture (聚光灯贴图)
聚光灯贴图,就是聚光灯照射在表面上的那个光圈,准备一张spot texture的亮度图,采用“GLSL projective texture ”一文中介绍的投影贴图方法把spot texture 投射到聚光灯方向。
unsigned char* pixels = (unsigned char*)malloc( 64*64 );
for (int j = 0; j < 64; j++){
for (int i = 0; i < 64; i++){
float x = (i / 31.5f) - 1;
float y = (j / 31.5f) - 1;
float ls = max(1 - (x * x + y * y), 0.0f);
pixels[j * 64 + i] = (unsigned char) (255.0f * ls);
}
}
texid_spot = GL_create_texture( 64,64,GL_LUMINANCE,pixels );
glBindTexture( GL_TEXTURE_2D,texid_spot );
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D( GL_TEXTURE_2D,0,GL_LUMINANCE8,64,64,0,GL_LUMINANCE,GL_UNSIGNED_BYTE,pixels );
glBindTexture( GL_TEXTURE_2D,0);