# GLONEMINUSSRCA LPHA 1 1 1 1 As As

• Slides: 76

블 렌 딩 GL_ONE_MINUS_SRC_A LPHA 입력값, 기존값 (1, 1, 1, 1)- (As, As, As) GL_DST_ALPHA 입력값, 기존값 (Ad, Ad, Ad) GL_ONE_MINUS_DST_AL 입력값, 기존값 PHA (1, 1, 1, 1)- (Ad, Ad, Ad) GL_SRC_ALPHA_SATUR ATE 입력값 (f, f, f, 1); f=min(As, 1 Ad) GL_CONSTANT_COLOR 입력값, 기존값 (Rc, Gc, Bc, Ac) GL_ONE_MINUS_CONST ANT_COLOR 입력값, 기존값 (1, 1, 1, 1)-(Rc, Gc, Bc, Ac) GL_CONSTANT_ALPHA 입력값, 기존값 (Ac, Ac, Ac) GL_ONE_MINUS_CONST ANT_ALPHA 입력값, 기존값 (1, 1, 1, 1)-(Ac, Ac, Ac) 2012 -2 학기 가상현실 7

예제 6 -1 블렌딩 예제 : alpha. c 서로 겹쳐진 두 개의 컬러 삼각형 include <GL/glut. h> #include <stdlib. h> static int left. First = GL_TRUE; /* 알파 블렌딩 함수 초기화 */ static void init(void) { gl. Enable (GL_BLEND); gl. Blend. Func (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); gl. Shade. Model (GL_FLAT); gl. Clear. Color (0. 0, 0. 0); } 2012 -2 학기 가상현실 11

static void draw. Left. Triangle(void){ // draw a yellow triangle gl. Begin (GL_TRIANGLES); gl. Color 4 f(1. 0, 0. 75); gl. Vertex 3 f(0. 1, 0. 9, 0. 0); gl. Vertex 3 f(0. 1, 0. 0); gl. Vertex 3 f(0. 7, 0. 5, 0. 0); gl. End(); } static void draw. Right. Triangle(void){ // draw a cyan triangle gl. Begin (GL_TRIANGLES); gl. Color 4 f(0. 0, 1. 0, 0. 75); gl. Vertex 3 f(0. 9, 0. 0); gl. Vertex 3 f(0. 3, 0. 5, 0. 0); gl. Vertex 3 f(0. 9, 0. 1, 0. 0); gl. End(); } 2012 -2 학기 가상현실 12

void display(void) { gl. Clear(GL_COLOR_BUFFER_BIT); if (left. First) { draw. Left. Triangle(); draw. Right. Triangle(); } else { draw. Right. Triangle(); draw. Left. Triangle(); } gl. Flush(); } 2012 -2 학기 가상현실 13

void reshape(int w, int h) { gl. Viewport(0, 0, (GLsizei) w, (GLsizei) h); gl. Matrix. Mode(GL_PROJECTION); gl. Load. Identity(); if (w <= h) glu. Ortho 2 D (0. 0, 1. 0, 0. 0, 1. 0*(GLfloat)h/(GLfloat)w); else glu. Ortho 2 D (0. 0, 1. 0*(GLfloat)w/(GLfloat)h, 0. 0, 1. 0); } 2012 -2 학기 가상현실 14

void keyboard(unsigned char key, int x, int y){ switch (key) { case 't': case 'T': left. First = !left. First; glut. Post. Redisplay(); break; case 27: /* Escape key */ exit(0); break; default: break; } } 2012 -2 학기 가상현실 15

int main(int argc, char** argv) { glut. Init(&argc, argv); glut. Init. Display. Mode (GLUT_SINGLE | GLUT_RGB); glut. Init. Window. Size (200, 200); glut. Create. Window (argv[0]); init(); glut. Reshape. Func (reshape); glut. Keyboard. Func (keyboard); glut. Display. Func (display); glut. Main. Loop(); return 0; } 2012 -2 학기 가상현실 16

예제 6 -2 3차원 블렌딩: alpha 3 D. c #include <GL/glut. h> #include <stdlib. h> #include <stdio. h> #define MAXZ 8. 0 #define MINZ -8. 0 #define ZINC 0. 4 static float solid. Z = MAXZ; static float transparent. Z = MINZ; static GLuint sphere. List, cube. List; static void init(void) { GLfloat mat_specular[] = { 1. 0, 0. 15 }; GLfloat mat_shininess[] = { 100. 0 }; GLfloat position[] = { 0. 5, 1. 0, 0. 0 }; 2012 -2 학기 가상현실 20

gl. Materialfv(GL_FRONT, GL_SPECULAR, mat_specular); gl. Materialfv(GL_FRONT, GL_SHININESS, mat_shininess); gl. Lightfv(GL_LIGHT 0, GL_POSITION, position); gl. Enable(GL_LIGHTING); gl. Enable(GL_LIGHT 0); gl. Enable(GL_DEPTH_TEST); sphere. List = gl. Gen. Lists(1); gl. New. List(sphere. List, GL_COMPILE); glut. Solid. Sphere (0. 4, 16); gl. End. List(); cube. List = gl. Gen. Lists(1); gl. New. List(cube. List, GL_COMPILE); glut. Solid. Cube (0. 6); gl. End. List(); } 2012 -2 학기 가상현실 21

void display(void) { GLfloat mat_solid[] = { 0. 75, 0. 0, 1. 0 }; GLfloat mat_zero[] = { 0. 0, 1. 0 }; GLfloat mat_transparent[] = { 0. 0, 0. 8, 0. 6 }; GLfloat mat_emission[] = { 0. 0, 0. 3, 0. 6 }; gl. Clear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); gl. Push. Matrix (); gl. Translatef (-0. 15, solid. Z); gl. Materialfv(GL_FRONT, GL_EMISSION, mat_zero); gl. Materialfv(GL_FRONT, GL_DIFFUSE, mat_solid); gl. Call. List (sphere. List); gl. Pop. Matrix (); 2012 -2 학기 가상현실 22

gl. Push. Matrix (); gl. Translatef (0. 15, transparent. Z); gl. Rotatef (15. 0, 1. 0, 0. 0); gl. Rotatef (30. 0, 1. 0, 0. 0); gl. Materialfv(GL_FRONT, GL_EMISSION, mat_emission); gl. Materialfv(GL_FRONT, GL_DIFFUSE, mat_transparent); gl. Enable (GL_BLEND); gl. Depth. Mask (GL_FALSE); // Depth 버퍼가 읽기전용 상태로 됨 gl. Blend. Func (GL_SRC_ALPHA, GL_ONE); gl. Call. List (cube. List); gl. Depth. Mask (GL_TRUE); // Depth 버퍼가 쓰기전용 상태로 됨 gl. Disable (GL_BLEND); gl. Pop. Matrix (); glut. Swap. Buffers(); } 2012 -2 학기 가상현실 23

void reshape(int w, int h) { gl. Viewport(0, 0, (GLint) w, (GLint) h); gl. Matrix. Mode(GL_PROJECTION); gl. Load. Identity(); if (w <= h) gl. Ortho (-1. 5, -1. 5*(GLfloat)h/(GLfloat)w, -10. 0, 10. 0); else gl. Ortho (-1. 5*(GLfloat)w/(GLfloat)h, -1. 5, -10. 0, 10. 0); gl. Matrix. Mode(GL_MODELVIEW); gl. Load. Identity(); } 2012 -2 학기 가상현실 24

void animate(void) { if (solid. Z <= MINZ || transparent. Z >= MAXZ) glut. Idle. Func(NULL); else { solid. Z -= ZINC; transparent. Z += ZINC; glut. Post. Redisplay(); } } 2012 -2 학기 가상현실 25

void keyboard(unsigned char key, int x, int y){ switch (key) { case 'a': case 'A': solid. Z = MAXZ; transparent. Z = MINZ; glut. Idle. Func(animate); break; case 'r': case 'R': solid. Z = MAXZ; transparent. Z = MINZ; glut. Post. Redisplay(); break; case 27: exit(0); } } 2012 -2 학기 가상현실 26

int main(int argc, char** argv) { glut. Init(&argc, argv); glut. Init. Display. Mode (GLUT_SINGLE | GLUT_RGB |GLUT_DEPTH); glut. Init. Window. Size(500, 500); glut. Create. Window(argv[0]); init(); glut. Reshape. Func(reshape); glut. Keyboard. Func(keyboard); glut. Display. Func(display); glut. Main. Loop(); return 0; } 2012 -2 학기 가상현실 27

예제 6 -3 안티앨리어싱 선분: aargb. c 안티앨리어싱 된 선분 [ 6 – 3 ] #include <GL/glut. h> #include <stdlib. h> #include <stdio. h> l static float rot. Angle = 0. ; /* 알파 블렌딩, 힌트, 선분의 너비를 포함, RGBA 모드를 위한 * 안티앨리어싱 초기화, 선분의 너비입상(line width granularity)과 * 너비에 관해서 Open. GL의 구현에 따라 달라지는 내용 출력 */ void init(void) { GLfloat values[2]; gl. Get. Floatv (GL_LINE_WIDTH_GRANULARITY, values); printf ("GL_LINE_WIDTH_GRANULARITY value is %3. 1 fn", values[0]); 2012 -2 학기 가상현실 33

gl. Get. Floatv (GL_LINE_WIDTH_RANGE, values); printf ("GL_LINE_WIDTH_RANGE values are %3. 1 fn", values[0], values[1]); gl. Enable (GL_LINE_SMOOTH); gl. Enable (GL_BLEND); gl. Blend. Func (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); gl. Hint (GL_LINE_SMOOTH_HINT, GL_DONT_CARE); gl. Line. Width (1. 5); gl. Clear. Color(0. 0, 0. 0); } 2012 -2 학기 가상현실 34

void display(void) { gl. Clear(GL_COLOR_BUFFER_BIT); gl. Color 3 f (0. 0, 1. 0, 0. 0); gl. Push. Matrix(); gl. Rotatef(-rot. Angle, 0. 0, 0. 1); gl. Begin (GL_LINES); gl. Vertex 2 f (-0. 5, 0. 5); gl. Vertex 2 f (0. 5, -0. 5); gl. End (); gl. Pop. Matrix(); 2012 -2 학기 가상현실 35

gl. Color 3 f (0. 0, 1. 0); gl. Push. Matrix(); gl. Rotatef(rot. Angle, 0. 0, 0. 1); gl. Begin (GL_LINES); gl. Vertex 2 f (0. 5, 0. 5); gl. Vertex 2 f (-0. 5, -0. 5); gl. End (); gl. Pop. Matrix(); gl. Flush(); } 2012 -2 학기 가상현실 36

void reshape(int w, int h) { gl. Viewport(0, 0, w, h); gl. Matrix. Mode(GL_PROJECTION); gl. Load. Identity(); if (w <= h) glu. Ortho 2 D (-1. 0, -1. 0*(GLfloat)h/(GLfloat)w, 1. 0*(GLfloat)h/(GLfloat)w); else glu. Ortho 2 D (-1. 0*(GLfloat)w/(GLfloat)h, -1. 0, 1. 0); gl. Matrix. Mode(GL_MODELVIEW); gl. Load. Identity(); } 2012 -2 학기 가상현실 37

void keyboard(unsigned char key, int x, int y) { switch (key) { case 'r': case 'R': rot. Angle += 20. ; if (rot. Angle >= 360. ) rot. Angle = 0. ; glut. Post. Redisplay(); break; case 27: /* Escape Key */ exit(0); break; default: break; } } 2012 -2 학기 가상현실 38

int main(int argc, char** argv) { glut. Init(&argc, argv); glut. Init. Display. Mode (GLUT_SINGLE | GLUT_RGB); glut. Init. Window. Size (200, 200); glut. Create. Window (argv[0]); init(); glut. Reshape. Func (reshape); glut. Keyboard. Func (keyboard); glut. Display. Func (display); glut. Main. Loop(); return 0; } 2012 -2 학기 가상현실 39

예제 6 -5 RGBA 에서 안개 효과를 적용하여 그린 다섯 개의 구: fog. c #include <GL/glut. h> <math. h> <stdlib. h> <stdio. h> static GLint fog. Mode; static void init(void) { GLfloat position[] = { 0. 5, 3. 0, 0. 0 }; gl. Enable(GL_DEPTH_TEST); gl. Lightfv(GL_LIGHT 0, GL_POSITION, position); gl. Enable(GL_LIGHTING); 2012 -2 학기 가상현실 45

gl. Enable(GL_LIGHT 0); { GLfloat mat[3] = {0. 1745, 0. 01175}; gl. Materialfv (GL_FRONT, GL_AMBIENT, mat); mat[0] = 0. 61424; mat[1] = 0. 04136; mat[2] = 0. 04136; gl. Materialfv (GL_FRONT, GL_DIFFUSE, mat); mat[0] = 0. 727811; mat[1] = 0. 626959; mat[2] = 0. 626959; gl. Materialfv (GL_FRONT, GL_SPECULAR, mat); gl. Materialf (GL_FRONT, GL_SHININESS, 0. 6*128. 0); } 2012 -2 학기 가상현실 46

gl. Enable(GL_FOG); { GLfloat fog. Color[4] = {0. 5, 1. 0}; fog. Mode = GL_EXP; gl. Fogi (GL_FOG_MODE, fog. Mode); gl. Fogfv (GL_FOG_COLOR, fog. Color); gl. Fogf (GL_FOG_DENSITY, 0. 35); gl. Hint (GL_FOG_HINT, GL_DONT_CARE); gl. Fogf (GL_FOG_START, 1. 0); gl. Fogf (GL_FOG_END, 5. 0); } gl. Clear. Color(0. 5, 1. 0); /* fog color */ } 2012 -2 학기 가상현실 47

static void render. Sphere (GLfloat x, GLfloat y, GLfloat z){ gl. Push. Matrix(); gl. Translatef (x, y, z); glut. Solid. Sphere(0. 4, 16); gl. Pop. Matrix(); } void display(void){ gl. Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); render. Sphere (-2. , -0. 5, -1. 0); render. Sphere (-1. , -0. 5, -2. 0); render. Sphere (0. , -0. 5, -3. 0); render. Sphere (1. , -0. 5, -4. 0); render. Sphere (2. , -0. 5, -5. 0); gl. Flush(); } 2012 -2 학기 가상현실 48

void reshape(int w, int h) { gl. Viewport(0, 0, (GLsizei) w, (GLsizei) h); gl. Matrix. Mode(GL_PROJECTION); gl. Load. Identity(); if (w <= h) gl. Ortho (-2. 5, -2. 5*(GLfloat)h/(GLfloat)w, -10. 0, 10. 0); else gl. Ortho (-2. 5*(GLfloat)w/(GLfloat)h, -2. 5, -10. 0, 10. 0); gl. Matrix. Mode(GL_MODELVIEW); gl. Load. Identity (); } 2012 -2 학기 가상현실 49

void keyboard(unsigned char key, int x, int y) { switch (key) { case 'f': case 'F': if (fog. Mode == GL_EXP) { fog. Mode = GL_EXP 2; printf ("Fog mode is GL_EXP 2n"); } else if (fog. Mode == GL_EXP 2) { fog. Mode = GL_LINEAR; printf ("Fog mode is GL_LINEARn"); } 2012 -2 학기 가상현실 50

else if (fog. Mode == GL_LINEAR) { fog. Mode = GL_EXP; printf ("Fog mode is GL_EXPn"); } gl. Fogi (GL_FOG_MODE, fog. Mode); glut. Post. Redisplay(); break; case 27: exit(0); break; default: break; } } 2012 -2 학기 가상현실 51

int main(int argc, char** argv) { glut. Init(&argc, argv); glut. Init. Display. Mode (GLUT_SINGLE | GLUT_RGB|GLUT_DEPTH); glut. Init. Window. Size(500, 500); glut. Create. Window(argv[0]); init(); glut. Reshape. Func (reshape); glut. Keyboard. Func (keyboard); glut. Display. Func (display); glut. Main. Loop(); return 0; } 2012 -2 학기 가상현실 52

안개 효과 방정식 l l void gl. Fog{if}(Glenum pname, TYPE param); void gl. Fog{if}v(Glenum pname, TYPE *params); l l l 2012 -2 학기 이 함수들은 안개 효과를 계산하기 위한 매개변수 방정식 설 정 pname 을 GL_FOG_MODE로 설정할 경우 param은 GL_EXP, GL_EXP 2, GL_LINEAR 중에서 설정 가상현실 54

예제: gl. Enable(GL_LIGHTING); gl. Enable(GL_LIGHT 0); gl. Enable(GL_POLYGON_OFFSET_FILL); gl. Polygon. Offset(1. 0, 1. 0); gl. Call. List(list); gl. Disable(GL_POLYGON_OFFSET_FILL); gl. Disable(GL_LIGHTING); gl. Disable(GL_LIGHT 0); gl. Color 3 f(1. 0, 1. 0); gl. Polygon. Mode(GL_FRONT_AND_BACK, GL_LINE); gl. Call. List(list); gl. Polygon. Mode(GL_FRONT_AND_BACK, GL_FILL); 2012 -2 학기 가상현실 60

예제 6 -7 눈에 띄는 결함을 없애는 다각형 오프셋: polyoff. c #include <GL/glut. h> #include <stdio. h> #include <stdlib. h> #ifdef GL_VERSION_1_1 GLuint list; GLint spinx = 0; GLint spiny = 0; GLfloat tdist = 0. 0; GLfloat polyfactor = 1. 0; GLfloat polyunits = 1. 0; 2012 -2 학기 가상현실 61

void display (void) { GLfloat mat_ambient[] = { 0. 8, 1. 0 }; GLfloat mat_diffuse[] = { 1. 0, 0. 5, 1. 0 }; GLfloat mat_specular[] = { 1. 0, 1. 0 }; GLfloat gray[] = { 0. 8, 1. 0 }; GLfloat black[] = { 0. 0, 1. 0 }; gl. Clear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); gl. Push. Matrix (); gl. Translatef (0. 0, tdist); gl. Rotatef ((GLfloat) spinx, 1. 0, 0. 0); gl. Rotatef ((GLfloat) spiny, 0. 0, 1. 0, 0. 0); 2012 -2 학기 가상현실 62

gl. Materialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray); gl. Materialfv(GL_FRONT, GL_SPECULAR, black); gl. Materialf(GL_FRONT, GL_SHININESS, 0. 0); gl. Enable(GL_LIGHTING); gl. Enable(GL_LIGHT 0); gl. Enable(GL_POLYGON_OFFSET_FILL); gl. Polygon. Offset(polyfactor, polyunits); gl. Call. List (list); gl. Disable(GL_POLYGON_OFFSET_FILL); 2012 -2 학기 가상현실 63

gl. Disable(GL_LIGHTING); gl. Disable(GL_LIGHT 0); gl. Color 3 f (1. 0, 1. 0); gl. Polygon. Mode(GL_FRONT_AND_BACK, GL_LINE); gl. Call. List (list); gl. Polygon. Mode(GL_FRONT_AND_BACK, GL_FILL); gl. Pop. Matrix (); gl. Flush (); } 2012 -2 학기 가상현실 64

void gfxinit (void) { GLfloat light_ambient[] = { 0. 0, 1. 0 }; GLfloat light_diffuse[] = { 1. 0, 1. 0 }; GLfloat light_specular[] = { 1. 0, 1. 0 }; GLfloat light_position[] = { 1. 0, 0. 0 }; GLfloat global_ambient[] = { 0. 2, 1. 0 }; gl. Clear. Color (0. 0, 1. 0); list = gl. Gen. Lists(1); gl. New. List (list, GL_COMPILE); glut. Solid. Sphere(1. 0, 20, 12); gl. End. List (); 2012 -2 학기 가상현실 65

gl. Enable(GL_DEPTH_TEST); gl. Lightfv (GL_LIGHT 0, GL_AMBIENT, light_ambient); gl. Lightfv (GL_LIGHT 0, GL_DIFFUSE, light_diffuse); gl. Lightfv (GL_LIGHT 0, GL_SPECULAR, light_specular); gl. Lightfv (GL_LIGHT 0, GL_POSITION, light_position); gl. Light. Modelfv (GL_LIGHT_MODEL_AMBIENT, global_ambient); } 2012 -2 학기 가상현실 66

void reshape(int width, int height) { gl. Viewport (0, 0, width, height); gl. Matrix. Mode (GL_PROJECTION); gl. Load. Identity (); glu. Perspective(45. 0, (GLdouble)width/(GLdouble)height, 1. 0, 10. 0); gl. Matrix. Mode (GL_MODELVIEW); gl. Load. Identity (); glu. Look. At (0. 0, 5. 0, 0. 0, 1. 0, 0. 0); } 2012 -2 학기 가상현실 67

void mouse(int button, int state, int x, int y) { switch (button) { case GLUT_LEFT_BUTTON: switch (state) { case GLUT_DOWN: spinx = (spinx + 5) % 360; glut. Post. Redisplay(); break; default: break; } break; 2012 -2 학기 가상현실 68

case GLUT_MIDDLE_BUTTON: switch (state) { case GLUT_DOWN: spiny = (spiny + 5) % 360; glut. Post. Redisplay(); break; default: break; } break; 2012 -2 학기 가상현실 69

case GLUT_RIGHT_BUTTON: switch (state) { case GLUT_UP: exit(0); break; default: break; } } 2012 -2 학기 가상현실 70

void keyboard (unsigned char key, int x, int y) { switch (key) { case 't': if (tdist < 4. 0) { tdist = (tdist + 0. 5); glut. Post. Redisplay(); } break; case 'T': if (tdist > -5. 0) { tdist = (tdist - 0. 5); glut. Post. Redisplay(); } break; 2012 -2 학기 가상현실 71

case 'F': polyfactor = polyfactor + 0. 1; printf ("polyfactor is %fn", polyfactor); glut. Post. Redisplay(); break; case 'f': polyfactor = polyfactor - 0. 1; printf ("polyfactor is %fn", polyfactor); glut. Post. Redisplay(); break; 2012 -2 학기 가상현실 72

case 'U': polyunits = polyunits + 1. 0; printf ("polyunits is %fn", polyunits); glut. Post. Redisplay(); break; case 'u': polyunits = polyunits - 1. 0; printf ("polyunits is %fn", polyunits); glut. Post. Redisplay(); break; default: break; } } 2012 -2 학기 가상현실 73

int main(int argc, char** argv) { glut. Init(&argc, argv); glut. Init. Display. Mode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); glut. Create. Window(argv[0]); glut. Reshape. Func(reshape); glut. Display. Func(display); glut. Mouse. Func(mouse); glut. Keyboard. Func(keyboard); gfxinit(); glut. Main. Loop(); return 0; } 2012 -2 학기 가상현실 74

#else int main(int argc, char** argv) { fprintf (stderr, "This program demonstrates a feature which is not in Open. GL Version 1. 0. n"); fprintf (stderr, "If your implementation of Open. GL Version 1. 0 has the right extensions, n"); fprintf (stderr, "you may be able to modify this program to make it run. n"); return 0; } #endif 2012 -2 학기 가상현실 75