| |
How to Scale Shapes with the OpenGL API
Back to OpenGL Tutorial Index
If you have worked with vectors before, you will know already that
scaling is simply stretching an object a certain amount.
Say we have an object, but we want it twice the height, we will
have to scale (stretch) it to twice the current height, along the y axis.
I am using:
glScalef(2, 0.5, 1);
in my display function.
If you look at the scale function on its own it takes:
The amount you want to stretch it on the:
x axis
y axis
z axis
In that order.
So I am stretching my cube, 2 times along the x axis, 0.5 times along the
y axis, and 1 time on the z axis.
So it will come out, twice as long, half as high, and the same depth.
And that is all there is to scaling, it is one of the very basic commands
in OpenGL.
If there are any problems with this code, please email me at swiftless@gmail.com
#include <GL/gl.h>
#include <GL/glut.h>
GLfloat angle = 0.0;
void cube (void) {
//scaled
glScalef( 2.0, 0.5, 1.0 ); //twice as wide, half the height, same depth
glRotatef(angle, 1.0, 0.0, 0.0);
glRotatef(angle, 0.0, 1.0, 0.0);
glRotatef(angle, 0.0, 0.0, 1.0);
glColor4f(1.0, 0.0, 0.0, 0.25); //25% visible
glutWireCube(2);
//non scaled
glRotatef(angle, 1.0, 0.0, 0.0);
glRotatef(angle, 0.0, 1.0, 0.0);
glRotatef(angle, 0.0, 0.0, 1.0);
glColor4f(0.0, 1.0, 0.0, 0.25); //25% visible
glutSolidCube(1);
}
void display (void) {
glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT);
glEnable(GL_BLEND); //enable the blending
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // set the blending
glLoadIdentity();
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
cube();
glutSwapBuffers();
angle ++;
}
void reshape (int w, int h) {
glViewport (0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
glMatrixMode (GL_MODELVIEW);
}
int main (int argc, char **argv) {
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA); //set up for the alpha channel
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("A basic OpenGL Window");
glutDisplayFunc (display);
glutIdleFunc (display);
glutReshapeFunc (reshape);
glutMainLoop ();
return 0;
}
Download C++ Source Code for this Tutorial
Download Visual Basic Source Code for this Tutorial |
 |