glut tutorial - vuwroma/glut.pdfglut tutorial roma july 26, 2012 what is glut? glut is the opengl...

27
GLUT Tutorial Roma July 26, 2012

Upload: others

Post on 20-Jan-2021

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: GLUT Tutorial - VUWroma/glut.pdfGLUT Tutorial Roma July 26, 2012 What is GLUT? GLUT is the OpenGL Utility Toolkit. It is a platform independant toolkit with utilty functions to help

GLUT Tutorial

Roma

July 26, 2012

Page 2: GLUT Tutorial - VUWroma/glut.pdfGLUT Tutorial Roma July 26, 2012 What is GLUT? GLUT is the OpenGL Utility Toolkit. It is a platform independant toolkit with utilty functions to help

What is GLUT?

GLUT is the OpenGL Utility Toolkit.It is a platform independant toolkit with utilty functions to helpwith basic window tasks.

Page 3: GLUT Tutorial - VUWroma/glut.pdfGLUT Tutorial Roma July 26, 2012 What is GLUT? GLUT is the OpenGL Utility Toolkit. It is a platform independant toolkit with utilty functions to help

Creating a Window

GLUT can be used to create a windows as follows

i n t main ( i n t argc , c h a r ∗∗ a r g v ) {g l u t I n i t (& argc , a r g v ) ;g l u t I n i t D i s p l a y M o d e (GLUT DEPTH | GLUT DOUBLE| GLUT RGBA) ;

g l u t I n i t W i n d o w S i z e ( 800 , 800 ) ;g lutCreateWindow ( ” Test Window” ) ;g lutMainLoop ( ) ; // Never r e t u r n s

r e t u r n EXIT SUCCESS ;}

Problem anyone?

Page 4: GLUT Tutorial - VUWroma/glut.pdfGLUT Tutorial Roma July 26, 2012 What is GLUT? GLUT is the OpenGL Utility Toolkit. It is a platform independant toolkit with utilty functions to help

But Empty Windows Are Pointless

We can tell GLUT to use a given drawing function

. . .g l u t D i s p l a y F u n c ( d i s p l a y ) ;. . .

with a function pointer!

v o i d d i s p l a y ( ) {g l C l e a r ( GL DEPTH BUFFER BIT |

GL COLOR BUFFER BIT ) ;}

Page 5: GLUT Tutorial - VUWroma/glut.pdfGLUT Tutorial Roma July 26, 2012 What is GLUT? GLUT is the OpenGL Utility Toolkit. It is a platform independant toolkit with utilty functions to help

But Nothing Happens

The window making code in the previous slide used a doublebuffered window so the display code doesn’t actually do anything.You need to swap the buffers before an image will appear at theend of display() using

g l u t S w a p B u f f e r s ( ) ;

Page 6: GLUT Tutorial - VUWroma/glut.pdfGLUT Tutorial Roma July 26, 2012 What is GLUT? GLUT is the OpenGL Utility Toolkit. It is a platform independant toolkit with utilty functions to help

The Black Screen of VictorySo now we have a blank window. By adding in OpenGL commandsinto the display function we can make the window display what wewant.

v o i d d i s p l a y ( ) {g l C l e a r ( GL DEPTH BUFFER BIT |

GL COLOR BUFFER BIT ) ;g l R e n d e r C o o l I m a g e ( ) ;g l u t S w a p B u f f e r s ( ) ;

}

Page 7: GLUT Tutorial - VUWroma/glut.pdfGLUT Tutorial Roma July 26, 2012 What is GLUT? GLUT is the OpenGL Utility Toolkit. It is a platform independant toolkit with utilty functions to help

Moving Pictures

Now that we have a static image on the screen, we want to moveto the next step. A changing image. We can specify a function forGLUT to use when nothing is happening to update the state of theworld.

. . .g l u t I d l e F u n c ( c h a n g i f y ) ;. . .

with a function pointer!

v o i d c h a n g i f y ( ) {// Update th e w o r l d s t a t e c o o l y

}

Page 8: GLUT Tutorial - VUWroma/glut.pdfGLUT Tutorial Roma July 26, 2012 What is GLUT? GLUT is the OpenGL Utility Toolkit. It is a platform independant toolkit with utilty functions to help

The Image Is Still Stationary

Even though we have updated what should be drawn we haven’tactually told anything that the screen needs to redraw. As far aseverything else is concerned redrawing would just draw the originalthing again.To get around this, we use the following command at the end ofchangify() to say that the screen needs to be redrawn because ithas actually changed.

v o i d c h a n g i f y ( ) {// Update th e w o r l d s t a t e c o o l yg l u t P o s t R e d i s p l a y ( ) ;

}

Page 9: GLUT Tutorial - VUWroma/glut.pdfGLUT Tutorial Roma July 26, 2012 What is GLUT? GLUT is the OpenGL Utility Toolkit. It is a platform independant toolkit with utilty functions to help

So You Can Only Make Movies?

At the moment, we can show animations, but we can’t interactwith them. Essentially we have made a movie. As this is software,there should be a way of letting the user interact with yoursoftware somehow.

What sort of interaction should be supported?

Page 10: GLUT Tutorial - VUWroma/glut.pdfGLUT Tutorial Roma July 26, 2012 What is GLUT? GLUT is the OpenGL Utility Toolkit. It is a platform independant toolkit with utilty functions to help

How to Speak Computer

GLUT supports several different kinds of input which we will lookat

I Window re-size/shape

I Keyboard

I Mouse

I Right click menus

I Waiting

Page 11: GLUT Tutorial - VUWroma/glut.pdfGLUT Tutorial Roma July 26, 2012 What is GLUT? GLUT is the OpenGL Utility Toolkit. It is a platform independant toolkit with utilty functions to help

The Screen Ratio Diet

In terms of interaction dragging a window is pretty boring, butreshaping it might allow the user to change the shape of objects onthe screen.

Page 12: GLUT Tutorial - VUWroma/glut.pdfGLUT Tutorial Roma July 26, 2012 What is GLUT? GLUT is the OpenGL Utility Toolkit. It is a platform independant toolkit with utilty functions to help

Enforcing Behaviours

There isn’t a single correct option when dealing with changes inscreen size and shape. Thankfully GLUT gives us a way to tailorthe behaviour to our needs. We can assign a screen resize callbackto make any appropriate adjustments

. . .g lu tReshapeFunc ( r e s h a p e ) ;. . .v o i d r e s h a p e ( i n t width , i n t h e i g h t ) {

//Do someth ing s e n s i b l e}

What different ways are there of sensibly dealing with a change isscreen size and shape?

Page 13: GLUT Tutorial - VUWroma/glut.pdfGLUT Tutorial Roma July 26, 2012 What is GLUT? GLUT is the OpenGL Utility Toolkit. It is a platform independant toolkit with utilty functions to help

Keyboard Input - ASCIISuppose we want to go to the extreme of having keyboard input.First we shall consider how to deal with events which correspondto the typing of an ASCII character. Note that you don’t know atwhich point in the press/release cycle this is triggered. Also notethat information about the mouse location is also provided.

. . .g lu tKeyboardFunc ( k e y p r e s s ) ;. . .v o i d k e y p r e s s ( u n s i g n e d c h a r key , i n t mouseX ,

i n t mouseY ) {i n t mod = g l u t G e t M o d i f i e r s ( ) ;

i f ( key == ’ i ’ && mod == GLUT ACTIVE SHIFT ){

p r i n t f ( ”The mouse i s a t %d %d\n” , mouseX, mouseY ) ;

}}

Page 14: GLUT Tutorial - VUWroma/glut.pdfGLUT Tutorial Roma July 26, 2012 What is GLUT? GLUT is the OpenGL Utility Toolkit. It is a platform independant toolkit with utilty functions to help

What About The Arrow Keys?

GLUT has a separate callback for non ASCII keypresses. Similar tothe previous, it also gives you the mouse position, but now youneed to compare the result to special constants for each key. Youstill need glutGetModifiers() to find out about ALT, CTRL andSHIFT.

. . .g l u t S p e c i a l F u n c ( s p e c i a l p r e s s ) ;. . .v o i d s p e c i a l p r e s s ( i n t key , i n t mouseX , i n t

mouseY ) {i f ( key == GLUT KEY RIGHT) {

p r i n t f ( ”The mouse i s a t %d %d\n” , mouseX, mouseY ) ;

}}

Page 15: GLUT Tutorial - VUWroma/glut.pdfGLUT Tutorial Roma July 26, 2012 What is GLUT? GLUT is the OpenGL Utility Toolkit. It is a platform independant toolkit with utilty functions to help

So Seriously, What About the Mouse?

Just like we have callbacks for the keyboard, we have similar onesfor the mouse. Much like how the keyboard can see the mouse,calling glutGetModifiers() is used in the mouse callback function tofind out about the state of certain keyboard keys.

. . .gluMouseFunc ( mouse ) ;

. . .v o i d mouse ( i n t button , i n t s t a t e , i n t x , i n t y

) {i f ( but ton == GLUT LEFT BUTTON && s t a t e ==

GLUT DOWN) {p r i n t f ( ”The mouse i s a t %d %d\n” , x , y ) ;

}}

Page 16: GLUT Tutorial - VUWroma/glut.pdfGLUT Tutorial Roma July 26, 2012 What is GLUT? GLUT is the OpenGL Utility Toolkit. It is a platform independant toolkit with utilty functions to help

What About Mouse Movement

The previous function told us only when mouse buttons arepushed. We need two different functions in order to track whenthe mouse is moving

. . .g lutMot ionFunc ( mouseMove ) ;g l u t P a s s i v e M o t i o n F u n c ( pass iveMove ) ;

. . .v o i d mouseMove ( i n t x , i n t y ) {

p r i n t f ( ”A button i s down and th e mouse i s a t%d %d\n” , x , y ) ;

}

v o i d pass iveMove ( i n t x , i n t y ) {p r i n t f ( ”No b u t t o n s a r e down and t he mouse i s

a t %d %d\n” , x , y ) ;}

Page 17: GLUT Tutorial - VUWroma/glut.pdfGLUT Tutorial Roma July 26, 2012 What is GLUT? GLUT is the OpenGL Utility Toolkit. It is a platform independant toolkit with utilty functions to help

Deja Vu?

Looking at all these functions. Surely they remind you ofsomething. Have you seen anything that looks similar to thisbefore?

Page 18: GLUT Tutorial - VUWroma/glut.pdfGLUT Tutorial Roma July 26, 2012 What is GLUT? GLUT is the OpenGL Utility Toolkit. It is a platform independant toolkit with utilty functions to help

Context Menus: The One True Path

So we have mouse and keyboard interaction, but what about rightclick menus? These are standard things and yet making them withthe existing functions would be a lot of work.To deal with this GLUT has its own mechanism for creating rightclick menus.

i n t submenu = glutCreateMenu ( v i o l e n c eM e n u ) ;glutAddMenuEntry ( ” Shoot ” , 0 ) ;glutAddMenuEntry ( ” Exp lode ” , 1 ) ;

i n t menu = glutCreateMenu ( mainMenu ) ;glutAddMenuEntry ( ” F i s h ” , 0 ) ;glutAddMenuEntry ( ”Meat” , 1 ) ;glutAddMenuEntry ( ” Chicken ” , 2 ) ;glutAddSubMenu ( ” E x t r a F u n c t i o n s ” , submenu ) ;

g lutAttachMenu (GLUT RIGHT BUTTON) ;

Page 19: GLUT Tutorial - VUWroma/glut.pdfGLUT Tutorial Roma July 26, 2012 What is GLUT? GLUT is the OpenGL Utility Toolkit. It is a platform independant toolkit with utilty functions to help

The Menus - They Do Nothing!So we have a menu, but we don’t actually have any code to dealwith the menu. However, in hte previous slide we did specify whichfunctions would be used to respond to each menu being clicked.So all we need is to define those functions.

v o i d mainMenu ( i n t i tem ) {i f ( i tem == 0 ) p r i n f ( ”You c l i c k e d f i s h \n” ) ;i f ( i tem == 1 ) p r i n f ( ”Moo\n” ) ;i f ( i tem == 2 ) p r i n f ( ”Bawk\n” ) ;

}

v o i d v i o l e n c e M en u ( i n t i tem ) {i f ( i tem == 0 ) p r i n f ( ”Peow Peow\n” ) ;i f ( i tem == 1 ) {

i n t ∗ f ;∗∗ f = 500 ;}

}

Page 20: GLUT Tutorial - VUWroma/glut.pdfGLUT Tutorial Roma July 26, 2012 What is GLUT? GLUT is the OpenGL Utility Toolkit. It is a platform independant toolkit with utilty functions to help

Waiting

Suppose we want to have something happen after a specifiedamount of time? Well GLUT has a function for that too. It takes atime in milliseconds, a function and a value that lets you identifywhere it came from. The function will be called at some pointafter the requested amount of time has passed. There is nomechanism for cancelling a request, but the value parametersallows you to work out that it should be ignored.

. . .i n t m i l l i s = 1000 ;g lutT imerFunc ( m i l l i s , c o o l S t u f f , 0 ) ;. . .v o i d c o o l S t u f f ( i n t v a l u e ) {

i f ( v a l u e ==0 ) {//Do someth ing r e l e v a n t

}}

Page 21: GLUT Tutorial - VUWroma/glut.pdfGLUT Tutorial Roma July 26, 2012 What is GLUT? GLUT is the OpenGL Utility Toolkit. It is a platform independant toolkit with utilty functions to help

Stoke Fonts

Stroke fonts are pretty much like everything else with GL. You usea transform to set where you want them to be drawn, and draw asnormal. They will draw from where the translation moved to, anddrawing each character translates the model view matrix to fit thenext characted into place.

v o i d d i s p l a y ( ) {g l T r a n s l a t e f ( 0 , 10 , 0 ) ;g l S c a l e f ( 0 . 05 , 0 . 05 , 0 . 05 ) ;g l C o l o r 3 f ( 0 , 1 . 0 , 0 ) ;f o r ( u n s i g n e d i n t i = 0 ; i < l e n g t h ; i ++) {

g l u t S t r o k e C h a r a c t e r (GLUT STROKE ROMAN, s [ i ] ) ;}g l u t P o s t R e d i s p l a y ( ) ;}

Page 22: GLUT Tutorial - VUWroma/glut.pdfGLUT Tutorial Roma July 26, 2012 What is GLUT? GLUT is the OpenGL Utility Toolkit. It is a platform independant toolkit with utilty functions to help

Stroke Fonts Killed Santa

There are in fact only 2 stroke fonts supported by GLUT:GLUT STROKE ROMAN and GLUT STROKE MONO ROMAN.On top of that, each drawing operation affects the model viewmatrix. This means that you need to undo all of the changes afteryou finish drawing, otherwise you may not be sure where exactlyyou are in the world.Also you are writing words in the world. What if you just wantyour writing to be in a fixed position of the screen?

Page 23: GLUT Tutorial - VUWroma/glut.pdfGLUT Tutorial Roma July 26, 2012 What is GLUT? GLUT is the OpenGL Utility Toolkit. It is a platform independant toolkit with utilty functions to help

Journey to Flatland

We can also draw raster text to the screen. Raster cooridinates aretransformed the same way that normal vertex coordinates are usingthe model view and projection matricies. Raster coordinates arehowever tracked independantly.

g l C o l o r 3 f ( 1 . 0 , 0 , 0 ) ;g l R a s t e r P o s 2 f ( 0 , 0 ) ;f o r ( u n s i g n e d i n t i = 0 ; i < l e n g t h ; i ++) {

g l u t B i t m a p C h a r a c t e r (GLUT BITMAP TIMES ROMAN24 , s [ i ] ) ;

}

Page 24: GLUT Tutorial - VUWroma/glut.pdfGLUT Tutorial Roma July 26, 2012 What is GLUT? GLUT is the OpenGL Utility Toolkit. It is a platform independant toolkit with utilty functions to help

Life in Flatland

Every call to glutBitmapCharacter(. . . ) results in the current offsetfor drawing being moved by the exact amount of space necessaryfor that character to be drawn. This is good as it means that youcan draw strings with an easy loop.It has the following fonts

GLUT BITMAP 8 BY 13GLUT BITMAP 9 BY 15GLUT BITMAP TIMES ROMAN 10GLUT BITMAP TIMES ROMAN 24GLUT BITMAP HELVETICA 10GLUT BITMAP HELVETICA 12GLUT BITMAP HELVETICA 18

Page 25: GLUT Tutorial - VUWroma/glut.pdfGLUT Tutorial Roma July 26, 2012 What is GLUT? GLUT is the OpenGL Utility Toolkit. It is a platform independant toolkit with utilty functions to help

But What Can We Actually Do?

We have looked at a number of GLUT commands which allow usto respond to input and write text to the screen, but we still can’tactually draw anything.To this end we need OpenGL and GLU to actually have output onthe screen. So lets recap some basic OpenGL and have a look at abasic program.

Page 26: GLUT Tutorial - VUWroma/glut.pdfGLUT Tutorial Roma July 26, 2012 What is GLUT? GLUT is the OpenGL Utility Toolkit. It is a platform independant toolkit with utilty functions to help

Look Mum - A Triangle!

We give co-ordinates for points inside of glBegin(). These arecoordinates in the rendering world, and will be mapped back to thereal world by OpenGL.

g l B e g i n ( GL TRIANGLES ) ;g l V e r t e x 3 f ( 0 , 1 , 0 ) ;g l V e r t e x 3 f (−1 , 0 , 0 ) ;g l V e r t e x 3 f ( 1 , 0 , 0 ) ;g lEnd ( ) ;

Page 27: GLUT Tutorial - VUWroma/glut.pdfGLUT Tutorial Roma July 26, 2012 What is GLUT? GLUT is the OpenGL Utility Toolkit. It is a platform independant toolkit with utilty functions to help

Drawing Shapes

Diffent parameters result in different shapes being drawn

GL POINTSGL LINESGL LINE STRIPGL LINE LOOPGL TRIANGLESGL TRIANGLE STRIPGL TRIANGLE FANGL QUADSGL QUAD STRIPGL POLYGON