그래픽스 프로그래밍

  • View
    120

  • Download
    7

Embed Size (px)

DESCRIPTION

그래픽스 프로그래밍. 그래픽스 프로그래밍 개요. 본 교재의 접근 방법 프로그래밍 지향적 내 용 최소한의 API 함수 소개 2 차원 프로그램 작성 시어핀스키 가스켓 예를 활용. 그래픽스 프로그래밍. 시어핀스키 가스켓 OpenGL API 기본요소와 속성 색상 관측 제어함수 가스켓 프로그램 다각형과 순환. 시어핀스키 가스켓. Sierpinski 라는 수학자가 만듦 시어핀스키 삼각형이라고도 함 프랙탈 기하학의 한 예 자기 유사성을 가짐. 알고리즘. 삼각형 내부에서 임의의 시작점 선택 - PowerPoint PPT Presentation

Text of 그래픽스 프로그래밍

  • *

    *

    API 2

    * OpenGL API

    * Sierpinski

    * 2

    * main(){ initialize_the_system(); for (some_number_of_points) { generate_a_point(); display_the_point(); } cleanup();}

    *OpenGL glVertex*()* = nt ntv n : t : ( : I, : f, double : d )v: )glVertex2i(GLint xi, GLint yi)glVertex3f(GLfloat x, GLfloat y, GLfloat z)GLfloat vertex[3]; glVertex3fv(vertex)

    * glBegin(GL_LINES); glVertex2f(x1, y1);glVertex2f(x2,y2);glEnd(); glBegin(GL_POINTS); glVertex2f(x1,y1); glVertex2f(x2,y2);glEnd();

    * typedef GLfloat point2[2];point2 p;

    GLfloat p[2];

    *Display void display(void){ typedef GLfloat point2[2]; // x, y point2 vertice[3]= {{0.0,0.0}, {250.0, 500.0}, {500.0,0.0}}; // static point2 p = {75.0, 50.0}; //

    int j, k; int rand(); //

    *Display (cont)for (k=0; k < 5000; k++) { j = rand() % 3; // 3 p[0] = (p[0] + vertice[j][0]) / 2.0; // X p[1] = (p[1] + vertice[j][1]) / 2.0; // Y glBegin(GL_POINTS); glVertex2fv(p); // glEnd(); } glFlush(); // = }

    *

    *

    ( ) ( , )

    *

    * OpenGL API

    *OpenGL API :

    * : , , , : , , : : , , : , : , ,

    *OpenGL GL : GLU : GL , (U = utility)GLUT : (UT = utility toolkit)

    X-

    * OpenGL API

    * , , , , , , ,

    OpenGL GL : GLU : GL

    * OpenGL

    type :

    glBegin(type); glVertex*( . . .); : glVertex*( . . .);glEnd();

    * (cont)

    *

    *

    * (convex object) ) , , , , ,

    *3

    3 : 3

    *OpenGL : :

    *OpenGL (cont) (strips and fans)

    * (stroke)

    ( )

    * (cont)

    * / : : n

    :

    *

    * (cont) : , : , , (, , ) : : , , ,

    * OpenGL API

    * (Color) RGB R, G, B ( : 1280 X 1024 X 24bit = 3.75MB)

    * (cont) 0.0 1.0 ) glColor3f(1.0, 0.0, 0.0); /* R, G, B */

    4 (RGBA)A = (alpha channel)0.0 : ---- 1.0 : , ) glClearColor(1.0, 1.0, 1.0, 1.0); glClear();

    * glClearColor(1.0, 1.0, 1.0, 1.0); glColor3f(1.0, 0.0, 0.0); glPointSize(2.0); /* : */

    * OpenGL API

    * (Viewing)2 : ( )

    *2 3 3 (viewing volume) z = 0 : 2 X 2 X 2

    * (x,y,z) (x,y,0)

    OpenGL void glOrtho(left, right, botom, top, near, far)void gluOrtho2D(left, right, bottom, top) /* near=-1.0, far=1.0 */

    * -

    : -

    *

    glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0, 500.0, 0.0, 500.0);glMatrixMode(GL_MODELVIEW);

    500 X 500

    * OpenGL API

    * : glutInit(int *argcp, char **argv) : glutCreateWindow(char *name) glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);GLUT_RGB : RGB (cf. GLUT_INDEX) GLUT_DEPTH : GLUT_DOUBLE : (cf. GLUT_SINGLE) glutInitWindowSize(480, 640); /* */glutInitWindowPosition(0,0); /* */ (0,0)

    * (Aspect Ratio)(Aspect Ratio)

    * (Viewport) void glViewport(GLint x, GLint y, GLsizei w, GLsizei h);

    *main void glutMainLoop(void)

    void glutDisplayFunc(void (*func) (void))

    * OpenGL API

    * #include

    void myinit(void){ glClearColor(1.0, 1.0, 1.0, 1.0); // glColor3f(1.0, 0.0, 0.0); // glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, 500.0, 0.0, 500.0); glMatrixMode(GL_MODELVIEW);}

    * (cont)void display(void){ typedef GLfloat point2[2]; // x, y point2 vertice[3]= {{0.0,0.0}, {250.0, 500.0}, {500.0,0.0}}; // int j, k; long rand(); // point2 p = {75.0, 50.0}; //

    glClear(GL_COLOR_BUFFER_BIT); //

    * (cont)for (k=0; k < 500000; k++) { j = rand() % 3; // 3 p[0] = (p[0] + vertice[j][0]) / 2.0; // X p[1] = (p[1] + vertice[j][1]) / 2.0; // Y glBegin(GL_POINTS); glVertex2fv(p); // glEnd(); } glFlush(); // = }

    * (cont)void main(int argc, char** argv){ void myinit(), display();

    glutInit(&argc,argv); // glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500,500); //500 X 500 glutInitWindowPosition(0,0); // glutCreateWindow("The Gasket Program"); // glutDisplayFunc(display); // Gasket myinit(); // glutMainLoop(); // Event Loop }

    *

    *Visual C++ OpenGL opengl32.lib, glu32.lib Visual C++ glut32.lib C:\Program Files\Microsoft Visual Studio\VC98\libDLL opengl32.dll, glu32.dll glut32.dll C:\windows\system32 gl.h, glu.hglut.h C:\Program Files\Microsoft Visual Studio\VC98\include\GL

    * Build Rebuild All *.dsw, *.dsp : Tools Directories Include Files Library Files

    * Project Settings Link opengl32.lib, glu32.lib, glut32.lib

    * (simple_use_defaults.c)#include void display(void){ glClear(GL_COLOR_BUFFER_BIT); /* clear window */

    /* draw unit square polygon */glBegin(GL_POLYGON); glVertex2f(-0.5, -0.5); glVertex2f(-0.5, 0.5); glVertex2f(0.5, 0.5); glVertex2f(0.5, -0.5);glEnd(); glFlush();}int main(int argc, char** argv){ glutCreateWindow("simple"); glutDisplayFunc(display); glutMainLoop();}

    * (simple.c)#include void display(void){ glClear(GL_COLOR_BUFFER_BIT); /* clear window */

    /* draw unit square polygon */glBegin(GL_POLYGON); glVertex2f(-0.5, -0.5); glVertex2f(-0.5, 0.5); glVertex2f(0.5, 0.5); glVertex2f(0.5, -0.5);glEnd(); glFlush();}void init(){ /* set clear color to black */ glClearColor (0.0, 0.0, 0.0, 0.0); /* set fill color to white */ glColor3f(1.0, 1.0, 1.0);

    /* set up standard orthogonal view with clipping */ /* box as cube of side 2 centered at origin */ /* This is default view and these statement could be removed */ glMatrixMode (GL_PROJECTION); glLoadIdentity (); glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); }

    *

    int main(int argc, char** argv){ /* Initialize mode and open a window in upper left corner of screen */ /* Window title is name of program (arg[0]) */ glutInit(&argc,argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500,500); glutInitWindowPosition(0,0); glutCreateWindow("simple"); glutDisplayFunc(display); init(); glutMainLoop();} (simple.c) (cont)

    * OpenGL API

    */* Recursive subdivision of triangle to form Sierpinski gasket */#include typedef float point2[2];

    /* initial triangle */point2 v[]={{-1.0, -0.58}, {1.0, -0.58}, {0.0, 1.15}};int n;

    void triangle( point2 a, point2 b, point2 c) /* display one triangle */{ glBegin(GL_TRIANGLES); glVertex2fv(a); glVertex2fv(b); glVertex2fv(c); glEnd();}

    *void divide_triangle(point2 a, point2 b, point2 c, int m){ /* triangle subdivision using vertex numbers */ point2 v0, v1, v2; int j; if(m>0) { for(j=0; j