PROGRAM 8 newpaint.c E. Angel.

Slides:



Advertisements
Similar presentations
1 Gestione degli eventi Daniele Marini. 2 Pipe-line di OGL pixel data vertex data display list pixel operation evaluator rasterization per vertex op &
Advertisements

Gestione degli eventi Daniele Marini Davide Gadia Marco Ronchetti Davide Selmo Corso Di Programmazione Grafica aa2005/2006.
© 2004, Tom Duff and George Ledin Jr1 Lectures OpenGL Introduction By Tom Duff Pixar Animation Studios Emeryville, California and George Ledin Jr Sonoma.
CSC461 Lecture 10: Widgets and Picking Objectives Introduce menus in GLUT Picking –Select objects from the display –Three methods Put things together.
1 CSC461 Lecture 7: 3D Programming in OpenGL Objectives: Develop 2D and 3D examples -- Sierpinski gasket: a fractal Develop 2D and 3D examples -- Sierpinski.
InteractionHofstra University1 Graphics Programming Input and Interaction.
31/1/2006Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)1 CSC345: Advanced Graphics & Virtual Environments Lecture 3: Introduction.
InteractionHofstra University1 Graphics Programming Input and Interaction.
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 GLUT Callback Functions.
Sierpinski Gasket Program
CSC461 Lecture 9: GLUT Callbacks Objectives Introduce double buffering for smooth animations Programming event input with GLUT.
Gestione degli eventi Daniele Marini Corso Di Programmazione Grafica e Laboratorio.
1 Working with Callbacks Yuanfeng Zhou Shandong University.
More on Drawing in OpenGL: Examples CSC 2141 Introduction to Computer Graphics.
WORKING WITH CALLBACKS Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico Angel: Interactive.
Geometric Primitives Used in Open GL Polygons Polygon is : Flat shape with three or more straight sides. It could be either : convex or concave.
Interaction with Graphics System
Input and Interaction Chapter 3. CS 480/680 2Chapter 3 -- Input and Interaction Introduction: Introduction: We now turn to the development of interactive.
1 Figures are extracted from Angel's book (ISBN x) The Human Visual System vs The Pinhole camera Human Visual System Visible Spectrum Pinhole.
Introduction to GL Geb Thomas. Example Code int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
DREAM PLAN IDEA IMPLEMENTATION Introduction to Computer Graphics Dr. Kourosh Kiani
1 Programming with OpenGL Part 3: Three Dimensions Yuanfeng Zhou Shandong University.
Computer Graphics I, Fall 2010 Programming with OpenGL Part 3: Three Dimensions.
Draw a Simple Object. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending.
1 Chapter 2 Graphics Programming. 2 Using OpenGL in Visual C++ – 1/3 Opengl32.dll and glu32.dll should be in the system folder Opengl32.lib and glu32.lib.
Computer Graphics I, Fall 2010 Working with Callbacks.
Input and Interaction Yuanfeng Zhou Shandong University Software College 1.
Projections. Viewports Windows can have separate viewports void glViewport(GLint x, GLint y, GLsizei width, GLsizei height ) x, y - Specify the lower.
Doç. Dr. Cemil Öz SAÜ Bilgisayar Mühendisliği Dr. Cemil Öz.
Geometric Primitives Used in Open GL Drawing Triangles glBegin(GL_TRIANGELS); glVertex2i(p0); glVertex2i(p1); glVertex2i(p2); glVertex2i(p3); glVertex2i(p4);
CS552: Computer Graphics Lecture 6: Viewing in 2D.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 3: Three Dimensions Ed Angel Professor of Computer Science,
12/22/ :38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert.
CS559: Computer Graphics Lecture 12: OpenGL - Transformation Li Zhang Spring 2008.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Input and Interaction Ed Angel Professor of Computer Science, Electrical and Computer Engineering,
31/1/2006Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)1 CSC345: Advanced Graphics & Virtual Environments Lecture 2: Introduction.
CS559: Computer Graphics Lecture 12: OpenGL: ModelView Li Zhang Spring 2010.
OpenGL: Introduction #include main() { OpenWindow() ; glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0,
Introduction to OpenGL Muhammad Aamir Khan Lecturer, DCS, UOP.
Introduction to Graphics Programming. Graphics API.
Graphics Graphics Korea University kucg.korea.ac.kr Graphics Programming 고려대학교 컴퓨터 그래픽스 연구실.
Introduction to Graphics Programming. Graphics: Conceptual Model Real Object Human Eye Display Device Graphics System Synthetic Model Synthetic Camera.
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Working with Callbacks.
CSC Graphics Programming
Review GLUT Callback Functions
Computer Graphics - Chapter 3 Input and Interaction
Working with Callbacks
CS5500 Computer Graphics March 2, 2006.
Course code:10CS65 | Computer Graphics and Visualization
Programming with OpenGL Part 2: Complete Programs
Advanced Graphics Algorithms Ying Zhu Georgia State University
Programming with OpenGL Part 3: Three Dimensions
OpenGL (Open Graphics Library) Mr. B.A.Swamy Assistant Professor Dept of CSE.
Lab 3 Geometric Drawing Lab 3 Geometric Drawing.
Starting to draw dealing with Windows which libraries? clipping
גרפיקה ממוחשבת: מבוא ל-OpenGL
Input and Interaction Chapter 3.
Lighting and Shading Lab 8:.
Working with Callbacks
Programming with OpenGL Part 3: Three Dimensions
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Fundamentals of Computer Graphics Part 3
Lecture 12: OpenGL Li Zhang Spring 2008
OpenGL program.
Programming with OpenGL Part 2: Complete Programs
Starting to draw dealing with Windows which libraries? clipping
Programming with OpenGL Part 3: Three Dimensions
Angel: Interactive Computer Graphics5E © Addison-Wesley 2009
Programming with OpenGL Part 3: Three Dimensions
Angel: Interactive Computer Graphics5E © Addison-Wesley 2009
Presentation transcript:

PROGRAM 8 newpaint.c E. Angel

程式敘述 使用OpenGL 寫出一簡單的繪圖程式 在程式視窗的左上角有一列選單 點選選單上的圖形, 便可以繪出直線, 矩形, 三角型, 以及PROGRAM 7 點選滑鼠左鍵便畫出小方塊, 以及在視窗上可輸入文字

#define NULL 0 #define LINE 1 #define RECTANGLE 2 #define TRIANGLE 3 #define POINTS 4 #define TEXT 5 #include <stdlib.h> #include <GL/glut.h> void mouse(int, int, int, int); void key(unsigned char, int, int); void display(void); void drawSquare(int, int); void myReshape(GLsizei, GLsizei); void myinit(void); void screen_box(int, int, int); void right_menu(int); void middle_menu(int); void color_menu(int); void pixel_menu(int); void fill_menu(int); int pick(int, int);

/* globals */ GLsizei wh = 500, ww = 500; /* initial window size */ GLfloat size = 3.0; /* half side length of square */ int draw_mode = 0; /* drawing mode */ int rx, ry; /*raster position*/ GLfloat r = 1.0, g = 1.0, b = 1.0; /* drawing color */ int fill = 0; /* fill flag */

void drawSquare(int x, int y) { y=wh-y; glColor3ub( (char) rand()%256, (char) rand()%256, (char) rand()%256); /*glColor3ub中的ub代表此函式後面的三個參數為unsigned char*/ glBegin(GL_POLYGON); glVertex2f(x+size, y+size); glVertex2f(x-size, y+size); glVertex2f(x-size, y-size); glVertex2f(x+size, y-size); glEnd(); }

void myReshape(GLsizei w, GLsizei h) { /* adjust clipping box */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, (GLdouble)w, 0.0, (GLdouble)h, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); /* adjust viewport and clear */ glViewport(0,0,w,h); glClearColor (0.8, 0.8, 0.8, 1.0); glClear(GL_COLOR_BUFFER_BIT); display(); glFlush(); /* set global size for use by drawing routine */ ww = w; wh = h; }

void myinit(void) { glViewport(0,0,ww,wh); /* Pick 2D clipping window to match size of X window This choice avoids having to scale object coordinates each time window is resized */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, (GLdouble) ww , 0.0, (GLdouble) wh , -1.0, 1.0); /* set clear color to black and clear window */ glClearColor (0.8, 0.8, 0.8, 1.0); glClear(GL_COLOR_BUFFER_BIT); glFlush(); }

void mouse(int btn, int state, int x, int y) { static int count; int where; static int xp[2],yp[2]; if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN) glPushAttrib(GL_ALL_ATTRIB_BITS);/*將目前的狀態全部儲存到堆疊之中*/ where = pick(x,y); glColor3f(r, g, b); if(where != 0) count = 0; draw_mode = where; } else switch(draw_mode)

case(LINE): if(count==0) { count++; xp[0] = x; yp[0] = y; } else glBegin(GL_LINES); glVertex2i(x,wh-y); glVertex2i(xp[0],wh-yp[0]); glEnd(); draw_mode=0; count=0; break;

case(RECTANGLE): if(count == 0) { count++; xp[0] = x; yp[0] = y; } else if(fill) glBegin(GL_POLYGON); else glBegin(GL_LINE_LOOP); glVertex2i(x,wh-y); glVertex2i(x,wh-yp[0]); glVertex2i(xp[0],wh-yp[0]); glVertex2i(xp[0],wh-y); glEnd(); draw_mode=0; count=0; break;

case (TRIANGLE): switch(count) { case(0): count++; xp[0] = x; yp[0] = y; break; case(1): xp[1] = x; yp[1] = y; case(2): if(fill) glBegin(GL_POLYGON); else glBegin(GL_LINE_LOOP); glVertex2i(xp[0],wh-yp[0]); glVertex2i(xp[1],wh-yp[1]); glVertex2i(x,wh-y); glEnd(); draw_mode=0; count=0; }

case(POINTS): { drawSquare(x,y); count++; } break; case(TEXT): rx=x; ry=wh-y; glRasterPos2i(rx,ry); /*用來指定目前的點陣位址*/ count=0; glPopAttrib(); /*回覆繪圖狀態屬性的OpenGL函式*/ glFlush();

int pick(int x, int y) { y = wh - y; if(y < wh-ww/10) return 0; else if(x < ww/10) return LINE; else if(x < ww/5) return RECTANGLE; else if(x < 3*ww/10) return TRIANGLE; else if(x < 2*ww/5) return POINTS; else if(x < ww/2) return TEXT; else return 0; }

void screen_box(int x, int y, int s ) { glBegin(GL_QUADS); /*以四個座標畫出一矩形*/ glVertex2i(x, y); glVertex2i(x+s, y); glVertex2i(x+s, y+s); glVertex2i(x, y+s); glEnd(); } void right_menu(int id) if(id == 1) exit(0); else display(); void middle_menu(int id)

void color_menu(int id) { if(id == 1) {r = 1.0; g = 0.0; b = 0.0;} else if(id == 2) {r = 0.0; g = 1.0; b = 0.0;} else if(id == 3) {r = 0.0; g = 0.0; b = 1.0;} else if(id == 4) {r = 0.0; g = 1.0; b = 1.0;} else if(id == 5) {r = 1.0; g = 0.0; b = 1.0;} else if(id == 6) {r = 1.0; g = 1.0; b = 0.0;} else if(id == 7) {r = 1.0; g = 1.0; b = 1.0;} else if(id == 8) {r = 0.0; g = 0.0; b = 0.0;} }

void pixel_menu(int id) { if (id == 1) size = 2 * size; else if (size > 1) size = size/2; } void fill_menu(int id) if (id == 1) fill = 1; else fill = 0; void key(unsigned char k, int xx, int yy) if(draw_mode!=TEXT) return; glColor3f(0.0,0.0,0.0); glRasterPos2i(rx,ry); glutBitmapCharacter(GLUT_BITMAP_9_BY_15, k); /*產生一點陣文字,其大小為9*15,k所代表的為ASCII碼*/ rx+=glutBitmapWidth(GLUT_BITMAP_9_BY_15,k);/*這個函式用來求取點陣字體的寬度*/ /*把座標平移點陣字型寬度*/

void display(void) { int shift=0; glPushAttrib(GL_ALL_ATTRIB_BITS); glClearColor (0.8, 0.8, 0.8, 1.0); glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0, 1.0, 1.0); screen_box(0,wh-ww/10,ww/10); glColor3f(1.0, 0.0, 0.0); screen_box(ww/10,wh-ww/10,ww/10); glColor3f(0.0, 1.0, 0.0); screen_box(ww/5,wh-ww/10,ww/10); glColor3f(0.0, 0.0, 1.0); screen_box(3*ww/10,wh-ww/10,ww/10); glColor3f(1.0, 1.0, 0.0); screen_box(2*ww/5,wh-ww/10,ww/10); glColor3f(0.0, 0.0, 0.0); screen_box(ww/10+ww/40,wh-ww/10+ww/40,ww/20);

glBegin(GL_LINES); //在小控制格裡畫直線 glVertex2i(wh/40,wh-ww/20); glVertex2i(wh/40+ww/20,wh-ww/20); glEnd(); glBegin(GL_TRIANGLES); //在小控制格裡畫三角型 glVertex2i(ww/5+ww/40,wh-ww/10+ww/40); glVertex2i(ww/5+ww/20,wh-ww/40); glVertex2i(ww/5+3*ww/40,wh-ww/10+ww/40); glPointSize(3.0); glBegin(GL_POINTS); //在小控制格裡畫點 glVertex2i(3*ww/10+ww/20, wh-ww/20);

glRasterPos2i(2*ww/5,wh-ww/20); /*以下程式是在方塊裡寫上ABC三個字母*/ glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'A'); shift=glutBitmapWidth(GLUT_BITMAP_9_BY_15, 'A'); glRasterPos2i(2*ww/5+shift,wh-ww/20); glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'B'); shift+=glutBitmapWidth(GLUT_BITMAP_9_BY_15, 'B'); glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'C'); glFlush(); glPopAttrib(); }

int main(int argc, char** argv) { int c_menu, p_menu, f_menu; glutInit(&argc,argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500, 500); glutCreateWindow("square"); glutDisplayFunc(display); c_menu = glutCreateMenu(color_menu); /*建立稱為color_menu的選單*/ glutAddMenuEntry(“Red”,1); /*建立主選單的選項*/ glutAddMenuEntry(“Green”,2);/*前面參數為顯示名稱,後面參數為id*/ glutAddMenuEntry("Blue",3); glutAddMenuEntry("Cyan",4); glutAddMenuEntry("Magenta",5); glutAddMenuEntry("Yellow",6); glutAddMenuEntry("White",7); glutAddMenuEntry("Black",8);

p_menu = glutCreateMenu(pixel_menu); glutAddMenuEntry("increase pixel size", 1); glutAddMenuEntry("decrease pixel size", 2); f_menu = glutCreateMenu(fill_menu); glutAddMenuEntry("fill on", 1); glutAddMenuEntry("fill off", 2); glutCreateMenu(right_menu); glutAddMenuEntry("quit",1); glutAddMenuEntry("clear",2); glutAttachMenu(GLUT_RIGHT_BUTTON); //*將右鍵選單連結到滑鼠右鍵 glutCreateMenu(middle_menu); glutAddSubMenu(“Colors”, c_menu); /*建立次選單*/ glutAddSubMenu("Pixel Size", p_menu); glutAddSubMenu("Fill", f_menu); glutAttachMenu(GLUT_MIDDLE_BUTTON); //*將左鍵選單連結到滑鼠左鍵 myinit (); glutReshapeFunc (myReshape); glutKeyboardFunc(key); glutMouseFunc (mouse); glutMainLoop(); }

執行畫面