|
GLUT Basics
with Examples |
|
Prof. David Bernstein |
| Computer Science Department |
| bernstdh@jmu.edu |
glutDisplayFunc(void (*f)(void))
glutPostRedisplay()
/**
* The entry point of the application.
*
* This function contains calls to GLUT.
*
* @param argc The number of command line arguments
* @param argv The array of command line arguments
* @return A status code
*/
int main(int argc, char** argv) {
// Initialize GLUT
glutInit(&argc, argv);
// Set the display mode. In this case, tell GLUT to
// use single buffering and an RGB color model
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
// Set the size of the window (in pixels)
glutInitWindowSize(640, 480);
// Set the position of the window (in screen coordinates)
glutInitWindowPosition(0, 0);
// Create a window with the given title
glutCreateWindow("A GLUT Window");
// Register the display callback function
glutDisplayFunc(display);
// Display window and start event processing
glutMainLoop();
return 0;
}
/**
* The display callback (i.e., the function that is called
* each time the window needs to be displayed).
*
* This function would normally contain calls to OpenGL.
*/
void display() {
printf("+display()\n");
// Calls to OpenGL go here
printf("-display()\n");
}
glutReshapeFunc(void (*f) (int width, int height))
/**
* The reshape callback (i.e., the function that is called
* each time the window is re-sized).
*
* @param width The new width
* @param height The new height
*/
void reshape(int width, int height) {
printf("+reshape(%d,%d)\n", width, height);
// Appropriate code goes here
printf("+reshape(%d,%d)\n", width, height);
}
main()] // Register the reshape callback
glutReshapeFunc(reshape);
glutKeyboardFunc(void (*f)(unsigned char key, int x, int y))
int glutGetModifiers()
GLUT_ACTIVE_SHIFT,
GLUT_ACTIVE_CTRL, and
GLUT_ACTIVE_ALT
glutSpecialFunc(void (*f)(int key, int x, int y))
GLUT_KEY_F1 thru 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,
GLUT_KEY_END, and
GLUT_KEY_INSERT
/**
* The keyboard callback (i.e., the function that is called
* each time a "normal" key is pressed).
*
* @param key The ASCII code of the key that was pressed
* @param x The x-position of the mouse when the key was pressed
* @param y The y-position of the mouse when the key was pressed
*/
void keyPressed(unsigned char key, int x, int y) {
int alt, ctrl, modifiers, shift;
printf("+keyPressed(%d,%d,%d)\n", key, x, y);
modifiers = glutGetModifiers();
alt = modifiers & GLUT_ACTIVE_ALT;
ctrl = modifiers & GLUT_ACTIVE_CTRL;
shift = modifiers & GLUT_ACTIVE_SHIFT;
printf(" %d %d %d \n", alt, ctrl, shift);
// Appropriate code goes here
printf("-keyPressed(%d,%d,%d)\n", key, x, y);
}
/**
* The special-key callback (i.e., the function that is called
* each time a "special" key is pressed).
*
* @param key The key that was pressed (e.g., GLUT_KEY_UP)
* @param x The x-position of the mouse when the key was pressed
* @param y The y-position of the mouse when the key was pressed
*/
void specialKeyPressed(int key, int x, int y) {
int alt, ctrl, modifiers, shift;
printf("+specialKeyPressed(%d,%d,%d)\n", key, x, y);
modifiers = glutGetModifiers();
alt = modifiers & GLUT_ACTIVE_ALT;
ctrl = modifiers & GLUT_ACTIVE_CTRL;
shift = modifiers & GLUT_ACTIVE_SHIFT;
printf(" %d %d %d \n", alt, ctrl, shift);
// Appropriate code goes here
printf("-specialKeyPressed(%d,%d,%d)\n", key, x, y);
}
glutMouseFunc(void (*f)(int button, int state, int x, int y))
button is either GLUT_LEFT_BUTTON,
GLUT_MIDDLE_BUTTON, or
GLUT_RIGHT_BUTTON
state is either GLUT_UP or
GLUT_DOWN
glutPassiveMotionFunc(void (*f)(int x, int y))
glutMotionFunc(void (*f)(int x, int y))
/**
* The mouse callback (i.e., the function that is called
* each time a mouse button is pressed or released).
*
* @param button The button (e.g., GLUT_LEFT_BUTTON)
* @param state The state (e.g., GLUT_UP or GLUT_DOWN)
* @param x The x-position of the mouse
* @param y The y-position of the mouse
*/
void mouseClicked(int button, int state, int x, int y) {
printf("+mouseClicked(%d,%d,%d,%d)\n", button, state, x, y);
// Appropriate code goes here
printf("-mouseClicked(%d,%d,%d,%d)\n", button, state, x, y);
}
/**
* The motion callback (i.e., the function that is called
* each time the mouse is dragged).
*
* @param x The x-position of the mouse
* @param y The y-position of the mouse
*/
void mouseDragged(int x, int y) {
printf("+mouseDragged(%d,%d)\n", x, y);
// Appropriate code goes here
printf("-mouseDragged(%d,%d)\n", x, y);
}
/**
* The passive-motion callback (i.e., the function that is called
* each time the mouse is moved).
*
* @param x The x-position of the mouse
* @param y The y-position of the mouse
*/
void mouseMoved(int x, int y) {
printf("+mouseMoved(%d,%d)\n", x, y);
// Appropriate code goes here
printf("-mouseMoved(%d,%d)\n", x, y);
}
glutIdleFunc(void (*f) (void))
int glutCreateSubWindow(int parentWindow, int x, int y, int width, int height)
int glutDestroyWindow(int window)
glutDisplayFunc() must be called for each
window (when it is "current")
void glutSetWindowTitle(char *name)
void glutSetCursor(int cursor)
cursor is one of:
GLUT_CURSOR_INFO, GLUT_CURSOR_HELP,
GLUT_CURSOR_WAIT, GLUT_CURSOR_TEXT,
GLUT_CURSOR_CROSSHAIR, GLUT_CURSOR_NONE
(or many others)
int glutCreateMenu(void (*callback)(int value))
void glutDestroyMenu(int menu)
void setMenu(int menu)
void glutAddSubMenu(char *name, int menu)
void glutAddMenuEntry(char *name, int valueToReturn)
void glutAttachMenu(int button)
void glutDetachMenu(int button)
button is either
GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON, or
GLUT_RIGHT_BUTTON
main()] // Create the main menu
mainMenu = glutCreateMenu(menuClicked);
// Create the "File" menu
fileMenu = glutCreateMenu(menuClicked);
// Add menu items to the "File" menu (which is the current menu)
glutAddMenuEntry("Copy", MENU_COPY);
glutAddMenuEntry("Cut", MENU_CUT);
glutAddMenuEntry("Paste", MENU_PASTE);
// Make the main menu the current menu
glutSetMenu(mainMenu);
// Add the "File" menu to the main menu (which is again the current menu)
glutAddSubMenu("File", fileMenu);
// Add a menu item to the main menu (which is still the current menu)
glutAddMenuEntry("Help", MENU_HELP);
// Attach the menu to the right button
glutAttachMenu(GLUT_RIGHT_BUTTON);