Sunteți pe pagina 1din 24

Reshape and Aspect Ratio 1

Reshape 2

 To register a Reshape Event


• glutReshapeFunc(reshape);
 Defining Reshape Call Back Function
• void reshape(GLsizei width, GLsizei height);
 When this function is called by glut windowing system?
• Whenever the window changes size (when it is stretched, maximized, and so on)
• Very first function that is called when a window is displayed
• Called even before display function
 Why bother window resizing?
• Our drawing is stretched or shrank to fit the Viewport (Results in distortion)
• Maintain Aspect Ration to prevent distortion (aspect ratio: ratio of width to height)
Aspect Ratio ( width / height) - I 3

In nearly all windowing environments, the user can at any time
change the size and dimensions of the window.
Even if you are writing a game that always runs in full-screen
mode, the window is still considered to change size once—when
it is created.
When this happens, the window usually responds by redrawing its
contents, taking into consideration the window’s new dimensions.
Sometimes, you might want to simply clip the drawing for smaller
windows or display the entire drawing at its original size in a
larger window.
Aspect Ratio ( width / height) - II 4

For our purposes, we usually want to scale the drawing to fit


within the window, regardless of the size of the drawing or
window.
Thus, a very small window would have a complete but very small
drawing, and a larger window would have a similar but larger
drawing.
You see this effect in most drawing programs when you stretch a
window as opposed to enlarging the drawing.
Stretching a window usually doesn’t change the drawing size, but
magnifying the image makes it grow.
Effects of Resizing windwos 5

World Window defined by glOrtho is Square


Viewport is Rectangular
Different Aspect Ratios
Shape Distortion
• Square <-> Rectangle
• Circle <-> Ellipse
Keeping a Square Square 6

// glOrth(left, right, bottom, top, near, far)

aspectRatio = (GLfloat)width / (GLfloat)height;

if (width <= height)


glOrtho (-100.0, 100.0, -100 / aspectRatio, 100.0 / aspectRatio, 1.0, -1.0);
else
glOrtho (-100.0 * aspectRatio, 100.0 * aspectRatio, -100.0, 100.0, 1.0, -1.0);
Example – Maintaining Aspect Ratio - I 7
Animation 8

To give life: virtual reality


We use parameterised transformations that change
over time t
Thus for each frame the object moves a little
further, just like a movie
Animation 9

Initialization :
1. Initialize OpenGL & GLUT
Initialization
2. Load models.
3. Load animation file.
Set Timer function Set Timer function :
1. Use glutTimerFunc() to set Timer function.

Timer function Timer function :


1. Increase time counter.
2. load animation data of this time.
3. if there is another frame,
use glutTimerFunc() again
Animation 10

 glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);


• Activate double-buffering operations
 glutSwapBuffers();
• Interchange front & back refresh buffers
 glutIdleFunc(animationFcn);
• Specify a function for incrementing animation parameters
 glutTimerFunc(msecs, func, value); // instead of glutIdleFunc
• registers a timer callback to be triggered in a specified number
of milliseconds.
 glutPostRedisplay();
• Can be the last statement of animationFcn, marks the current
window as needing to be redisplayed
Example 1/4 11

#include <stdlib.h>
#include <GL/glut.h>

//Animation control variable


static GLfloat spin = 0.0;

void init(void) {
glClearColor (0.0, 0.0, 0.0, 0.0);
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(spin, 0.0, 0.0, 1.0);
glColor3f(1.0, 0.0, 0.0);
glRectf(-25.0, -25.0, 25.0, 25.0);
glPopMatrix();
glutSwapBuffers(); // instead of glFlush()
}
Example 2/4 12

//Calculate next value of the animation controlling variables


void update(void) // for use in glutIdleFunc(update);
{
spin = spin + 0.01;
if (spin > 360.0)
spin = 0.0;
glutPostRedisplay();
}

void reshape(int w, int h)


{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
Example 3/4 13

//Stop animation at left mouse click


void mouse(int button, int state, int x, int y)
{
switch (button) {
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN)
glutIdleFunc(NULL);
break;
default:
break;
}
}
Example 4/4 14

int main(int argc, char** argv)


{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (250, 250);
glutInitWindowPosition (100, 100);
glutCreateWindow (“GLUT Window”);

init ();

glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutIdleFunc(update);

glutMainLoop();
return 0;
}
What is next? 15

1. Use glutTimerFunc instead of glutIdleFunc in previous example


2. Exit from program with Esc key
glutTimerFunction 16

Arguments
• msecs : ms.
• Func : Timer function pointer.
• Value : argument pass to Timer function.

Ex.
• glutTimerFunc(5000, TimerFunc, 0);
call TimerFunc(0) 5000 ms later.
Example 1/4 17

#include <GL/glut.h>
#include <stdlib.h>
static GLfloat spin = 0.0;
int timer_milliseconds = 34; // 34 milliseconds are approx. fps = 29.97
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(spin, 0.0, 0.0, 1.0);
glColor3f(1.0, 0.0, 0.0);
glRectf(-25.0, -25.0, 25.0, 25.0);
glPopMatrix();
glutSwapBuffers();
}
Example 2/4 18

void animationFcn(int value)


{
glutTimerFunc(timer_milliseconds, animationFcn, 0);
spin = spin + 1;
if (spin > 360.0) spin = 0.0;
glutPostRedisplay();
}

void reshape(int w, int h)


{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
Example 3/4 19

void keyboard(unsigned char key, int x, int y)


{
if (key=27) {
exit(0);
}
}
Example 4/4 20

int main(int argc, char** argv)


{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (250, 250);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutTimerFunc(timer_milliseconds, animationFcn, 0);
glutMainLoop();
return 0;
}
Menu 21

void glutMenuStatusFunc(void (*func)(int status, int x, int y));


void glutMenuStateFunc(void (*func)(int status));
Menu Example 22
Exercise 23

1. Add in previous program, start and stop


animation with left and right buttons of
mouse, resp. Slow and Fast movement with S
& F keyboard shortcuts.
2. Learn the use of 3rd parameter of
glutTimerFunc from [3]
References 24

1. Donald Hearn, M. Pauline Baker, Computer Graphics with OpenGL, Third Edition, Prentice Hall,
2004.
2. Hill, F. S., Kelly S. M., Computer Graphics Using OpenGL, Third Edition, Pearson Education, 2007,
http://www.4twk.com/shill/
3. http://ironbark.bendigo.latrobe.edu.au/~fran/int32gp/2006/wk04/tt_02.html

S-ar putea să vă placă și