References Lab Lectures Computer Graphics Taif University Faculty


References • Lab Lectures, Computer Graphics, Taif University, Faculty Of Computers And Information Technology, TA. Maha Thafar &TA. Haifa Alshehri, TA. Sohair Soliman & L. Shakila Bano. • Open. GL Programming Guide: The Official Guide to Learning Open. GL, Versions 4. 3, 8 th edition, Dave Shreiner, Graham Sellers, John Kessenich, Bill Licea-Kane & The Khronos Open. GL ARB Working Group, Addison-Wesley.

Lecture Ten Interaction in Open. GL

Content 1. Interactive in Open. GL 2. User Interaction Devices 3. GLUT Event Call. Backs 4. Using Keyboard 5. Key Types 6. Simple Example 7. Interactive 3 D Example Using Open. GL 8. Mouse Callbacks 9. Glut. Idle. Function 10. Another Open. GL Program

Interactive in Open. GL • The Open. GL graphics mode allows interactive rotation and scaling of the displayed objects. • The GLUT library is used for the interaction between Open. GL and the computer windowing system. ü NOTE: we can design a game, and we can use the Keyboard and Mouse for creating interactions with user.

User Interaction Devices

Glut Event Call. Backs

Using Keyboard

Using Keyboard

Key Types

Simple Example #include<windows. h> #include<GL/glut. h> void keyboard(unsigned char key, int x, int y) { if (key == 27) { //27 is the ASSCI code for ESC key exit(0); } } void Draw() { gl. Clear(GL_COLOR_BUFFER_BIT); gl. Color 3 f(1. 0, 0. 6); glu. Look. At(0, 3, 5, 0, 1, 0, 0, 1, 2); glut. Solid. Teapot(2. 0); gl. Flush(); } void init() { gl. Clear. Color(1. 0, 0. 0); gl. Shade. Model(GL_FLAT); gl. Matrix. Mode(GL_PROJECTION); gl. Load. Identity(); gl. Frustum(-2, 2, -3, 3, 1, 20); } int main() { glut. Init. Display. Mode(GLUT_SINGLE | GLUT_RGB); glut. Init. Window. Size(500, 500); glut. Init. Window. Position(100, 100); glut. Create. Window("A Simple Open. GL Program"); init(); glut. Display. Func(Draw); glut. Keyboard. Func(keyboard); glut. Main. Loop(); return 0; }

Simple Example

Interactive 3 D Example Using Open. GL #include<windows. h> #include<GL/glut. h> #include<GL/glu. h> void Init(void); void Display(void); void Keyboard(unsigned char, int); int main(){ glut. Init. Display. Mode(GLUT_DOUBLE | GLUT_RGB); glut. Init. Window. Size(600, 600); glut. Init. Window. Position(0, 0); glut. Create. Window("Interaction with a 3 D Object Program!"); Init(); glut. Display. Func(Display); glut. Keyboard. Func(Keyboard); glut. Main. Loop(); return 0; } void Init(void) { gl. Clear. Color(1. 0, 0. 8, 0. 0); gl. Matrix. Mode(GL_PROJECTION); gl. Load. Identity(); gl. Ortho(-15. 0, -15. 0, 15. 0); }

Interactive 3 D Example Using Open. GL void Display() { gl. Clear(GL_COLOR_BUFFER_BIT); gl. Begin(GL_QUADS); //The Square gl. Color 3 f(1. 0, 0. 4); gl. Vertex 3 f(-3. 0, 0. 0); gl. Vertex 3 f(3. 0, 0. 0); gl. Vertex 3 f(-3. 0, 0. 0); gl. End(); gl. Begin(GL_TRIANGLES); //The Around Triangles gl. Color 3 ub(255, 255); gl. Vertex 3 f(0. 0, 6. 5, 0. 0); gl. Color 3 ub(255, 176); gl. Vertex 3 f(-3. 0, 0. 0); gl. Vertex 3 f(3. 0, 0. 0); gl. Color 3 ub(255, 255); gl. Vertex 3 f(-3. 0, 0. 0); gl. Color 3 ub(255, 176); gl. Vertex 3 f(-3. 0, 0. 0); gl. Vertex 3 f(-6. 5, 0. 0); gl. Color 3 ub(255, 255); gl. Vertex 3 f(0. 0, -6. 5, 0. 0); gl. Color 3 ub(255, 176); gl. Vertex 3 f(3. 0, -3. 0, 0. 0); gl. Vertex 3 f(-3. 0, 0. 0); } gl. Color 3 ub(255, 255); gl. Vertex 3 f(6. 5, 0. 0); gl. Color 3 ub(255, 176); gl. Vertex 3 f(3. 0, 0. 0); gl. Vertex 3 f(3. 0, -3. 0, 0. 0); gl. End(); glut. Swap. Buffers();

Interactive 3 D Example Using Open. GL void Keyboard(unsigned char key, int x, int y) { switch(key) { case 'u': case 'U': gl. Rotatef(3. 0, 1. 0, 0. 0); //Rotate Up break; case 'd': case 'D': gl. Rotatef(-3. 0, 1. 0, 0. 0); //Rotate Down case 'l': case 'L': gl. Rotatef(3. 0, 0. 0, 1. 0, 0. 0); //Rotate left break; case 'r': case 'R': gl. Rotatef(-3. 0, 0. 0, 1. 0, 0. 0); //Rotate Right } glut. Post. Redisplay(); }

Interactive 3 D Example Using Open. GL

Interactive 3 D Example Using Open. GL

Interactive 3 D Example Using Open. GL

Mouse Call. Backs

Glut. Idle. Function

Another Open. GL Program #include<windows. h> #include<GL/glut. h> #include<GL/glu. h> void Display() { gl. Clear(GL_COLOR_BUFFER_BIT); gl. Begin(GL_QUADS); //The Square gl. Vertex 3 f(-3. 0, 0. 0); gl. Vertex 3 f(3. 0, 0. 0); gl. Vertex 3 f(-3. 0, 0. 0); gl. End(); glut. Swap. Buffers(); } void Init(void) { gl. Clear. Color(1. 0, 0. 9, 0. 0); gl. Matrix. Mode(GL_PROJECTION); gl. Load. Identity(); gl. Ortho(-15. 0, -15. 0, 15. 0); } void Mouse(int button, int state, int x, int y) { switch(button) { case GLUT_LEFT_BUTTON: if (state == GLUT_DOWN) gl. Color 3 f(1. 0, 0. 0); glut. Idle. Func(Display); break; case GLUT_MIDDLE_BUTTON: gl. Color 3 f(0. 0, 1. 0, 0. 0); glut. Idle. Func(Display); break; case GLUT_RIGHT_BUTTON: if (state == GLUT_DOWN) gl. Color 3 f(0. 0, 1. 0); glut. Idle. Func(Display); break; default: break; glut. Post. Redisplay(); } }

Another Open. GL Program int main(){ glut. Init. Display. Mode(GLUT_DOUBLE | GLUT_RGB); glut. Init. Window. Size(500, 500); glut. Init. Window. Position(0, 0); glut. Create. Window("Interaction with a Square Program Using a Mouse!"); Init(); glut. Display. Func(Display); glut. Mouse. Func(Mouse); glut. Main. Loop(); return 0; }

Another Open. GL Program

Another Open. GL Program

Another Open. GL Program

Another Open. GL Program

- Slides: 27