Environment Map Enable Cube Map gl EnableGLTEXTURECUBEMAPEXT gl
Environment Map
Enable การใช Cube Map • ใหสง gl. Enable(GL_TEXTURE_CUBE_MAP_EXT); • และอยาลมสง gl. Disable(GL_TEXTURE_CUBE_MAP_EXT); กอนใชงาน texture แบบอน
Bind Cube Map ทสรางขน • สง gl. Bind. Texture โดยใช target เปน GL_TEXTURE_CUBE_MAP_EXT gl. Bind. Texture(GL_TEXTURE_CUBE_MAP_EXT, cube. Map)
Target สำหรบ Download รป Target ดาน GL_TEXTURE_CUBE_MAP_POSITIVE_X_EXT ขวา ซาย บน ลาง หนา หลง GL_TEXTURE_CUBE_MAP_NEGATIVE_X_EXT GL_TEXTURE_CUBE_MAP_POSITIVE_Y_EXT GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT GL_TEXTURE_CUBE_MAP_POSITIVE_Z_EXT GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT
load. Cube. Map. Side void load. Cube. Map. Side(GLuint target, const char *image. Name) { ILuint image; il. Gen. Images(1, &image); il. Bind. Image(image); il. Load. Image((wchar_t *)image. Name); il. Convert. Image(IL_RGB, IL_UNSIGNED_BYTE) glu. Build 2 DMipmaps(target, il. Get. Integer(IL_IMAGE_BPP), il. Get. Integer(IL_IMAGE_WIDTH), il. Get. Integer(IL_IMAGE_HEIGHT), il. Get. Integer(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE, il. Get. Data()); il. Delete. Images(1, &image); }
init. Cube. Map void init. Cube. Map() { gl. Enable(GL_TEXTURE_CUBE_MAP_EXT); gl. Gen. Textures(1, &cube. Map); gl. Bind. Texture(GL_TEXTURE_CUBE_MAP_EXT, cube. Map); gl. Tex. Parameteri(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_WRAP_S, GL_CLAMP); GL_TEXTURE_WRAP_T, GL_CLAMP); GL_TEXTURE_MAG_FILTER, GL_LINEAR); GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); load. Cube. Map. Side(GL_TEXTURE_CUBE_MAP_POSITIVE_X_EXT, load. Cube. Map. Side(GL_TEXTURE_CUBE_MAP_NEGATIVE_X_EXT, load. Cube. Map. Side(GL_TEXTURE_CUBE_MAP_POSITIVE_Y_EXT, load. Cube. Map. Side(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT, load. Cube. Map. Side(GL_TEXTURE_CUBE_MAP_POSITIVE_Z_EXT, load. Cube. Map. Side(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT, gl. Disable(GL_TEXTURE_CUBE_MAP_EXT); } ". . /images/cm_right. jpg"); ". . /images/cm_left. jpg"); ". . /images/cm_top. jpg"); ". . /images/cm_bottom. jpg"); ". . /images/cm_front. jpg"); ". . /images/cm_back. jpg");
การสรางกระจกใน Open. GL • นอกจากนยงตองบอกดวยวาจะใหสราง ใหแบบใด ดวยคำสง gl. Tex. Geni texture coordinate • ในกรณการสรางกระจกเราตองสง gl. Tex. Geni(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_EXT); gl. Tex. Geni(GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_EXT); gl. Tex. Geni(GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_EXT);
ตวอยางโคด void display() { gl. Enable(GL_TEXTURE_CUBE_MAP_EXT); gl. Tex. Geni(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_EXT; ( gl. Tex. Geni(GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_EXT; ( gl. Tex. Geni(GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_EXT; ( gl. Enable(GL_TEXTURE_GEN_S); gl. Enable(GL_TEXTURE_GEN_T); gl. Enable(GL_TEXTURE_GEN_R); glut. Solid. Sphere(1. 5, 50); glut. Swap. Buffers(); }
ด demo
การใช Cube Map ใน GLSL • GLSL ใหผใชสามารถประกาศ uniform parameter ประเภท sampler. Cube ใน shader ได เชน uniform sampler. Cube env_map; void main() { : : }
Vertex Program สำหรบทำกระจก varying vec 3 normal; varying vec 3 position; void main() { gl_Position = ftransform(); position = (gl_Model. View. Matrix * gl_Vertex). xyz; normal = (gl_Normal. Matrix * gl_Normal). xyz; }
Fragment Program สำหรบทำกระจก uniform vec 3 eye_position; uniform sampler. Cube env; varying vec 3 position; varying vec 3 normal; void main() { vec 3 n = normalize(normal); vec 3 eye_to_point = normalize(position - eye_position); vec 3 reflected = reflect(eye_to_point, n); gl_Frag. Color = texture. Cube(env, reflected; ( {
ด demo
ด demo
Fragment Program สำหรบจำลองแกว uniform vec 3 eye; uniform sampler. Cube env; uniform float eta_ratio; varying vec 3 position; varying vec 3 normal; void main() { vec 3 n = normalize(normal); vec 3 eye_to_point = normalize(position - eye); vec 3 refracted = refract(eye_to_point, n, eta_ratio); vec 4 refraction = texture. Cube(env, refracted); { gl_Frag. Color = refraction;
ด demo
Fragment Program สำหรบจำลองปรากฏการณ Fresnel uniform uniform vec 3 eye; sampler. Cube env; float eta_ratio; float fresnel_bias; float fresnel_scale; float fresnel_power; varying vec 3 position; varying vec 3 normal; void main() { vec 3 n = normalize(normal); vec 3 eye_to_point = normalize(position - eye); vec 3 refracted = refract(eye_to_point, n, eta_ratio); vec 3 reflected = reflect(eye_to_point, n); vec 4 refraction = texture. Cube(env, refracted); vec 4 reflection = texture. Cube(env, reflected); float reflected_weight = fresnel_bias + fresnel_scale * pow(1. 0 + dot(n, eye_to_point), fresnel_power); reflected_weight = max(0. 0, min(reflected_weight, 1. 0)); gl_Frag. Color = reflected_weight * reflection + (1. 0 -reflected_weight) * refraction; }
ด demo
Chromatic Dispersion )ตอ (
Fragment Program สำหรบจำลอง Chromatic Dispersion uniform uniform vec 3 eye; sampler. Cube env; vec 3 eta_ratio; float fresnel_bias; float fresnel_scale; float fresnel_power; varying vec 3 position; varying vec 3 normal; void main() { vec 3 n = normalize(normal); vec 3 eye_to_point = normalize(position - eye); vec 3 refracted_r = refract(eye_to_point, n, eta_ratio. r); vec 3 refracted_g = refract(eye_to_point, n, eta_ratio. g); vec 3 refracted_b = refract(eye_to_point, n, eta_ratio. b); vec 3 refraction_r = texture. Cube(env, refracted_r). rgb * vec 3(1, 0, 0); vec 3 refraction_g = texture. Cube(env, refracted_g). rgb * vec 3(0, 1, 0); vec 3 refraction_b = texture. Cube(env, refracted_b). rgb * vec 3(0, 0, 1); vec 4 refraction = vec 4(refraction_r + refraction_g + refraction_b, 1);
Fragment Program สำหรบจำลอง Chromatic Dispersion vec 3 reflected = reflect(eye_to_point, n); vec 4 reflection = texture. Cube(env, reflected); float reflected_weight = fresnel_bias + fresnel_scale * pow(1. 0 + dot(n, eye_to_point), fresnel_power); reflected_weight = max(0. 0, min(reflected_weight, 1. 0)); gl_Frag. Color = reflected_weight * reflection + (1. 0 -reflected_weight) * refraction; }
ด demo
- Slides: 57