GLUT Tutorial. Keyboard

Similar documents
VTU EDUSAT PROGRAMME. Computer Graphics & Visualization

Precept 2 Aleksey Boyko February 18, 2011

GL_COLOR_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW

3D computer graphics: geometric modeling of objects in the computer and rendering them

Project Sketchpad. Ivan Sutherland (MIT 1963) established the basic interactive paradigm that characterizes interactive computer graphics:

Input, Interaction and Animation Week 2

Input and Interaction

CS 432 Interactive Computer Graphics

CSC Graphics Programming. Budditha Hettige Department of Statistics and Computer Science

Computer Graphics. OpenGL

Using OpenGL with CUDA

GLUT Tutorial. John Tsiombikas (Nuclear / The Lab Demos) May 6, Introduction to GLUT (OpenGL Utility Toolkit)

Announcements OpenGL. Computer Graphics. Spring CS4815

FAKULTI TEKNOLOGI MAKLUMAT DAN KOMUNIKASI BITM INTERACTIVE COMPUTER GRAPHICS LAB SESSION 4. C++ - OpenGL

// double buffering and RGB glutinitdisplaymode(glut_double GLUT_RGBA); // your own initializations

Input and Interaction. CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

CS 432 Interactive Computer Graphics

Introduction to OpenGL

Announcements OpenGL. Computer Graphics. Autumn 2009 CS4815

Computer Graphics (CS 543) Lecture 2b: 2D Graphics Systems (Drawing Polylines, tiling, & Aspect Ratio)

Teacher Assistant : Tamir Grossinger Reception hours: by - Building 37 / office -102 Assignments: 4 programing using

Lecture 3. Understanding of OPenGL programming

Comp 410/510 Computer Graphics Spring Input & Interaction

CS 543 Lecture 1 (Part II): Intro to OpenGL and GLUT (Part I) Emmanuel Agu

Computer Graphics Course 2005

Lecture 11: Callbacks. CITS 3003 Graphics & Animation

DONALD HOUSE: CPSC 8170, FALL 2018 DESIGN OF A REAL-TIME ANIMATION PROGRAM

C++ is Fun Part 13 at Turbine/Warner Bros.! Russell Hanson

Interaction Computer Graphics I Lecture 3

To Do. Computer Graphics (Fall 2008) Course Outline. Course Outline. Methodology for Lecture. Demo: Surreal (HW 3)

Computer Graphics. Bing-Yu Chen National Taiwan University

CS 4204 Computer Graphics

Introduction to Computer Graphics with OpenGL/GLUT

Lecture 2 CISC440/640 Spring Department of Computer and Information Science

VR-programming tools (procedural) More VRML later in this course! (declarative)

Computer Graphics (CS 4731) OpenGL/GLUT (Part 2)

CS 4731 Lecture 3: Introduction to OpenGL and GLUT: Part II. Emmanuel Agu

Drawing Primitives. OpenGL basics

Interaction. CSCI 480 Computer Graphics Lecture 3

Abel J. P. Gomes LAB. 1. INTRODUCTION TO OpenGL

Lecture 4. Interaction / Graphical Devices. CS 354 Computer Graphics Sunday, January 20, 13

Lectures OpenGL Introduction

C OMPUTER G RAPHICS Thursday

Programming using OpenGL: A first Introduction

ERKELEY DAVIS IRVINE LOS ANGELES RIVERSIDE SAN DIEGO SAN FRANCISCO EECS 104. Fundamentals of Computer Graphics. OpenGL

CMSC 425: Lecture 4 More about OpenGL and GLUT Tuesday, Feb 5, 2013

Introduction to OpenGL: Part 2

Modeling Transform. Chapter 4 Geometric Transformations. Overview. Instancing. Specify transformation for objects 李同益

Working with Callbacks

CS Computer Graphics: Intro to OpenGL

CS Computer Graphics: Intro to OpenGL

CS Computer Graphics: OpenGL, Continued

CS Computer Graphics: OpenGL, Continued

2/3/16. Interaction. Triangles (Clarification) Choice of Programming Language. Buffer Objects. The CPU-GPU bus. CSCI 420 Computer Graphics Lecture 3

OpenGL/GLUT Intro. Week 1, Fri Jan 12

Cameras (and eye) Ideal Pinhole. Real Pinhole. Real + lens. Depth of field

Interaction. CSCI 420 Computer Graphics Lecture 3

Basic Graphics Programming

Exercise 1 Introduction to OpenGL

20 GLuint objects; 36 Scale += 0.1; 37 break; 38 case GLUT_KEY_DOWN:

C++ - OpenGL - Setting up Visual Studio

Transformation, Input and Interaction. Hanyang University

Introduction to OpenGL

CS418 OpenGL & GLUT Programming Tutorial (I) Presented by : Wei-Wen Feng 1/30/2008

Computer Graphics (CS 543) Lecture 1 (Part 2): Introduction to OpenGL/GLUT (Part 1)

OpenGL refresher. Advanced Computer Graphics 2012

CS559: Computer Graphics. Lecture 12: OpenGL Li Zhang Spring 2008

OpenGL Tutorial. Ceng 477 Introduction to Computer Graphics

Intro to OpenGL III. Don Fussell Computer Science Department The University of Texas at Austin

CS559: Computer Graphics. Lecture 12: OpenGL Transformation Li Zhang Spring 2008

Programming with OpenGL Part 2: Complete Programs Computer Graphics I, Fall

COMPUTER GRAPHICS LAB # 3

Computer graphic -- Programming with OpenGL I

Chapter 10: Events and Event Handling for Computer Graphics

Lecture 2 Sections 3.6, 3.7. Fri, Aug 28, 2009

Duc Nguyen CSE 420 Computer Graphics 10/10/2018 Homework 1

Programming with OpenGL Part 3: Three Dimensions

Introduction to MS Visual C/C++

Books, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, PointClouds, and G3D

RECITATION - 1. Ceng477 Fall

Bob s Concise Introduction to Doxygen

Display Lists in OpenGL

CS 543 Lecture 1 (Part 3) Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

LECTURE 02 OPENGL API


Lecture 2 2D transformations Introduction to OpenGL

by modifying the glutinitwindowsize() function you can change the screen size to whatever you please.

CSE4030 Introduction to Computer Graphics

Visualizing Molecular Dynamics

Computer graphics MN1

Information Coding / Computer Graphics, ISY, LiTH. OpenGL! ! where it fits!! what it contains!! how you work with it 11(40)

Computer Graphics Anatomy of GUI. Computer Graphics CSC470 1

OpenGL JOGL. OpenGL & JOGL. Shaoting Zhang, or Tony. September 12, 2007

Computer Graphics (CS 4731) OpenGL/GLUT(Part 1)

Graphics Hardware and OpenGL

Draw the basic Geometry Objects. Hanyang University

Basic Graphics Programming

Philip Calderon CSE 520 Lab 3 Color Shader

Input and Interaction

Scientific Visualization Basics

Transcription:

GLUT Tutorial Keyboard GLUT allows us to build applications that detect keyboard input using either the "normal" keys, or the special keys like F1 and Up. In this section we'll see how to detect which key was pressed, what further information we get from GLUT, and how to deal with that. As you have probably noticed by now, whenever you want to take control of the processing of an event you have to tell GLUT in advance which function is going to perform such task. Up until now we used GLUT to tell the windows system which functions we wanted to do the rendering when the window needed to be repainted, which function to call when the system was idle, and which function to call when the window was resized. Similarly we must do the same thing for keyboard events. We must notify the windows system, using GLUT, which function(s) will perform the required processing when a key is pressed. This procedure of notifying that when an event occurs we want to execute a particular function is also called "register a callback function". GLUT provides two functions to register callbacks for keyboard events that occur when you press a key. The first one, glutkeyboardfunc, is used to tell the windows system which function we want to process the "normal" key events. By "normal" keys, we mean letters, numbers, anything that has an ASCII code. The syntax for this function is as follows: void glutkeyboardfunc(void (*func) (unsigned char key, int x, int y)); Parameters: func - The name of the function that will process the "normal" keyboard events. Passing NULL as an argument causes GLUT to ignore "normal" keys. The function used as an argument to glutkeyboardfunc needs to have three arguments. The first indicates the ASCII code of the key pressed, the remaining two arguments provide the mouse position when the key is pressed. The mouse position is relative to the top left corner of the client area of the window. A possible implementation for this function is to provide a way out of the application when the user presses the ESCAPE key. Note that when the glutmainloop function was presented we mentioned that it was an infinite loop, i.e. it never returns. The only way out of this loop is to call the system exit function. So that's exactly what our function will do, when the user presses escape it calls the system exit function causing the application to terminate (remember to include stdlib.h in the source code). Next we present the function code: Note that we are using exactly the same signature as the one specified in the syntax of glutkeyboardfunc. If you don't do this you'll get an error when compiling this in VC, and we don't want that, do we?

OK, ready to move on? Lets tackle the special keys now. GLUT provides the function glutspecialfunc so that you can register your function for special key events processing. The syntax for this function is as follows: void glutspecialfunc(void (*func) (int key, int x, int y)); Parameters: func - The name of the function that will process the special keyboard events. Passing NULL as an argument causes GLUT to ignore the special keys. We're going to write a function that changes the color of our triangle when some of the special keys are pressed. This function will paint the triangle using red if F1 is pressed, green if F2 is pressed, and blue if F3 is pressed. void processspecialkeys(int key, int x, int y) { switch(key) { case GLUT_KEY_F1 : red = 1.0; case GLUT_KEY_F2 : green = 1.0; case GLUT_KEY_F3 : blue = 1.0; break; The GLUT_KEY_* are predefined constants in glut.h. The full set of constants is presented next: GLUT_KEY_F1 GLUT_KEY_F2 GLUT_KEY_F3 GLUT_KEY_F4 GLUT_KEY_F5 GLUT_KEY_F6 GLUT_KEY_F7 GLUT_KEY_F8 GLUT_KEY_F9 GLUT_KEY_F10 GLUT_KEY_F11 GLUT_KEY_F12 GLUT_KEY_LEFT GLUT_KEY_RIGHT GLUT_KEY_UP GLUT_KEY_DOWN GLUT_KEY_PAGE_UP GLUT_KEY_PAGE_DOWN GLUT_KEY_HOME F1 function key F2 function key F3 function key F4 function key F5 function key F6 function key F7 function key F8 function key F9 function key F10 function key F11 function key F12 function key Left function key Up function key Right function key Down function key Page Up function key Page Down function key Home function key

GLUT_KEY_END GLUT_KEY_INSERT End function key Insert function key In order for the code defined above on processspecialkeys to compile we must add the declaration of the red, green, and blue variables to the beginning of our code. Furthermore, for the code to have the desired effect we must change the function responsible for the rendering, renderscene.... // all variables initialized to 1.0, meaning // the triangle will initially be white float red=1.0, blue=1.0, green=1.0; void renderscene(void) { glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT); glpushmatrix(); glrotatef(angle,0.0,1.0,0.0); // this is where we set the actual color // glcolor specifies the color of all further drawings glcolor3f(red,green,blue); glbegin(gl_triangles); glvertex3f(-0.5,-0.5,0.0); glvertex3f(0.5,0.0,0.0); glvertex3f(0.0,0.5,0.0); glend(); glpopmatrix(); angle++; glutswapbuffers(); OK, now we're ready to tell GLUT that the functions we just defined are the ones that will process keyboard events. In other words it is time to call GLUT's glutkeyboardfunc and glutspecialfunc. The call to these functions can be made anywhere, meaning that we may change the processing function for keyboard event processing at any time. However this is not an usual feature, so we'll place it on the main function. Next we present the new main function, with keyboard processing is presented (note that this function is based on the previous sections of this tutorial): void main(int argc, char **argv) { glutinit(&argc, argv); // This is where we say that we want a double buffer glutinitdisplaymode(glut_depth GLUT_DOUBLE GLUT_RGBA); glutinitwindowposition(100,100); glutinitwindowsize(320,320); glutcreatewindow("3d Tech - GLUT Tutorial"); glutdisplayfunc(renderscene); glutidlefunc(renderscene); glutreshapefunc(changesize);

// here are the new entries glutkeyboardfunc(processnormalkeys); glutspecialfunc(processspecialkeys); // enable depth testing glenable(gl_depth_test); glutmainloop(); The Visual Basic project can be downloaded here (glut3.zip). CTRL, ALT and SHIFT Sometimes we may want to know if one modifier key, i.e. CTRL, ALT or SHIFT is being pressed. GLUT provides a function that detects if any modifier is being pressed. This function should only be called inside the functions that process keyboard or mouse input events. The syntax for this function is: int glutgetmodifiers(void); The return value for this function is either one of three predefined constants (in glut.h), or any bitwise OR combination of them. The constants are: GLUT_ACTIVE_SHIFT - Set if either you press the SHIFT key, or Caps Lock is on. Note that if they are both on then the constant is not set. GLUT_ACTIVE_CTRL - Set if you press the CTRL key. GLUT_ACTIVE_ALT - Set if you press the ALT key. Beware that the windows system may intercept some modifiers, no callback is generated in these cases. So lets extent our processnormalkeys a little bit to see how to handle these modifier keys. Suppose that you want the variable red to be 0.0 when the user presses r, and 1.0 when the user presses ALT + r. The following piece of code will do the trick: else if (key=='r') { int mod = glutgetmodifiers(); if (mod == GLUT_ACTIVE_ALT) else red = 1.0; Notice that if we wanted to do something if 'R' is pressed, then the following code will not work. This is because the key is 'R' and not 'r', therefore you should not use the SHIFT modifier to turn lowercase into uppercase or to achieve any symbol that has a defined ASCII code. You can use SHIFT with F1, though.

// this is incorrect if we're looking for an 'R' else if (key=='r') { int mod = glutgetmodifiers(); if (mod == GLUT_ACTIVE_SHIFT)... One last thing, how do you detect CTRL+ALT+F1? In this case we must detect two modifiers at the same time. In order to achieve this we do a bitwise OR with the desired constants. The following piece of code only changes the color to red if combination CTRL+ALT+F1 is pressed. void processspecialkeys(int key, int x, int y) { int mod; switch(key) { case GLUT_KEY_F1 : mod = glutgetmodifiers(); if (mod == (GLUT_ACTIVE_CTRL GLUT_ACTIVE_ALT)) { red = 1.0; blue = 0.0; break; case GLUT_KEY_F2 : green = 1.0; case GLUT_KEY_F3 : blue = 1.0; break; One final note about registering callbacks for keyboard and mouse events: when you register a callback for these events there maybe a performance penalty because the system is looking for these events. If you don't want to handle these events avoid registering callback functions to handle them.