20
CS 4204 Computer Graphics GLUT (Continue) GLUT (Continue) More Interactions More Interactions Yong Cao Yong Cao Virginia Tech Virginia Tech References: References: Interactive Computer Graphics, Fourth Edition, Ed Angle Interactive Computer Graphics, Fourth Edition, Ed Angle

CS 4204 Computer Graphics

  • Upload
    others

  • View
    16

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS 4204 Computer Graphics

CS 4204 Computer Graphics

GLUT (Continue)GLUT (Continue)More InteractionsMore Interactions

Yong CaoYong CaoVirginia TechVirginia Tech

References:References:Interactive Computer Graphics, Fourth Edition, Ed AngleInteractive Computer Graphics, Fourth Edition, Ed Angle

Page 2: CS 4204 Computer Graphics

Orthographical Projection

Synthetic camera model

View angle and view volume

Clipping

Aspect ratio

Viewport mapping

Synthetic camera modelSynthetic camera model

View angle and view volumeView angle and view volume

ClippingClipping

Aspect ratioAspect ratio

Viewport mappingViewport mapping

Page 3: CS 4204 Computer Graphics

Synthetic Camera Model

center of projection

image plane

projector

p

projection of p

Page 4: CS 4204 Computer Graphics

View angle

Page 5: CS 4204 Computer Graphics

View volume

Page 6: CS 4204 Computer Graphics

Creating an orthographic view by moving the camera away from the projection plane.

Orthographical projection

Page 7: CS 4204 Computer Graphics

Orthographic projectors with projection plane z = 0.

Orthographical projection

The default camera and an orthographic view volume.

Page 8: CS 4204 Computer Graphics

Orthographical projection

Page 9: CS 4204 Computer Graphics

Clipping

(a) Objects before clipping. (b) Image after clipping.

Page 10: CS 4204 Computer Graphics

Aspect-ratio

(a) Viewing rectangle. (b) Display window.

Page 11: CS 4204 Computer Graphics

A mapping to the viewport.

Page 12: CS 4204 Computer Graphics

Reshaping the window

We can reshape and resize the OpenGL display window by pulling the corner of the window

What happens to the display?• Must redraw from application

• Two possibilities

– Display part of world

– Display whole world but force to fit in new window

Can alter aspect ratio

We can reshape and resize the OpenGL display We can reshape and resize the OpenGL display window by pulling the corner of the windowwindow by pulling the corner of the window

What happens to the display?What happens to the display?•• Must redraw from applicationMust redraw from application

•• Two possibilitiesTwo possibilities

–– Display part of worldDisplay part of world

–– Display whole world but force to fit in new windowDisplay whole world but force to fit in new window

Can alter aspect ratioCan alter aspect ratio

Page 13: CS 4204 Computer Graphics

Reshape possibilities

original

reshaped

Page 14: CS 4204 Computer Graphics

The Reshape callbackglutReshapeFunc(myreshape)

void myreshape( int w, int h)

• Returns width and height of new window (in pixels)• A redisplay is posted automatically at end of

execution of the callback • GLUT has a default reshape callback but you

probably want to define your own

The reshape callback is good place to put viewing functions because it is invoked when the window is first opened

glutReshapeFunc(myreshapeglutReshapeFunc(myreshape))

void void myreshapemyreshape( ( intint w, w, intint h)h)

•• Returns width and height of new window (in pixels)Returns width and height of new window (in pixels)•• A redisplay is posted automatically at end of A redisplay is posted automatically at end of

execution of the callbackexecution of the callback•• GLUT has a default reshape callback but you GLUT has a default reshape callback but you

probably want to define your ownprobably want to define your own

The reshape callback is good place to put The reshape callback is good place to put viewing functions because it is invoked viewing functions because it is invoked when the window is first openedwhen the window is first opened

Page 15: CS 4204 Computer Graphics

Example Reshape (Demo)This reshape preserves shapes by making the viewport and world window have the same aspect ratio This reshape preserves shapes by making the viewport and This reshape preserves shapes by making the viewport and world window have the same aspect ratioworld window have the same aspect ratiovoid myReshape(int w, int h){

glViewport(0, 0, w, h);glMatrixMode(GL_PROJECTION); /* switch matrix mode */glLoadIdentity();if (w <= h)

gluOrtho2D(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w,2.0 * (GLfloat) h / (GLfloat) w);

else gluOrtho2D(-2.0 * (GLfloat) w / (GLfloat) h, 2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0);

glMatrixMode(GL_MODELVIEW); /* return to modelview mode */}

Page 16: CS 4204 Computer Graphics

Toolkits and Widgets

Most window systems provide a toolkit or library of functions for building user interfaces that use special types of windows called widgets

Widget sets include tools such as• Menus

• Slidebars

• Dials

• Input boxes

But toolkits tend to be platform dependent

GLUT provides a few widgets including menus

Most window systems provide a toolkit or library of Most window systems provide a toolkit or library of functions for building user interfaces that use special types functions for building user interfaces that use special types of windows called of windows called widgetswidgets

Widget sets include tools such asWidget sets include tools such as

•• MenusMenus

•• SlidebarsSlidebars

•• DialsDials

•• Input boxesInput boxes

But toolkits tend to be platform dependentBut toolkits tend to be platform dependent

GLUT provides a few widgets including menusGLUT provides a few widgets including menus

Page 17: CS 4204 Computer Graphics

Menus

GLUT supports pop-up menus• A menu can have submenus

Three steps• Define entries for the menu

• Define action for each menu item

– Action carried out if entry selected

• Attach menu to a mouse button

GLUT supports popGLUT supports pop--up menusup menus•• A menu can have submenusA menu can have submenus

Three stepsThree steps•• Define entries for the menuDefine entries for the menu

•• Define action for each menu itemDefine action for each menu item

–– Action carried out if entry selectedAction carried out if entry selected

•• Attach menu to a mouse buttonAttach menu to a mouse button

Page 18: CS 4204 Computer Graphics

Defining a simple menu

In main.cIn In main.cmain.cmenu_id = glutCreateMenu(mymenu);glutAddmenuEntry(“clear Screen”, 1);

gluAddMenuEntry(“exit”, 2);

glutAttachMenu(GLUT_RIGHT_BUTTON);

entries that appear whenright button depressed

identifiers

clear screen

exit

Page 19: CS 4204 Computer Graphics

Menu actions• Menu callback

• Note each menu has an id that is returned when it is created

• Add submenus by

glutAddSubMenu(char *submenu_name, submenu id)

•• Menu callbackMenu callback

•• Note each menu has an id that is returned when it is Note each menu has an id that is returned when it is createdcreated

•• Add submenus byAdd submenus by

glutAddSubMenu(charglutAddSubMenu(char *submenu_name, submenu id)*submenu_name, submenu id)

void mymenu(int id){

if(id == 1) glClear();if(id == 2) exit(0);

}

entry in parent menu

Page 20: CS 4204 Computer Graphics

Other functions in GLUTDynamic Windows• Create and destroy during execution

Multiple Windows (Demo)

Changing callbacks during execution

Timersvoid glutTimerFunc( unsigned int msecs, void (*func)(int value), value);

Portable fonts• glutBitmapCharacter (See demo)

• glutStrokeCharacter (See demo)

Dynamic WindowsDynamic Windows

•• Create and destroy during executionCreate and destroy during execution

Multiple Windows Multiple Windows (Demo)(Demo)

Changing callbacks during executionChanging callbacks during execution

TimersTimersvoid void glutTimerFuncglutTimerFunc(( unsigned unsigned intint msecsmsecs, void (*, void (*func)(intfunc)(int value), value);value), value);

Portable fontsPortable fonts•• glutBitmapCharacterglutBitmapCharacter (See demo)(See demo)

•• glutStrokeCharacterglutStrokeCharacter (See demo)(See demo)