| |
 |
How to scale shapes in OpenGL
|
|
If you would like to see this site updated, please help out and
|
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
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
| | | #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
Comments:
| Name: Simon |
| Date: 2010-02-14 11:53:30 |
Comment:
Hi Ronald,
If I copy the above codes, both cubes will be stretched. Should you not put the gScalef function after the first cube is created (so teh first one is actually the non stretched and the second one is the stretched one) or am I missing something? Also, on another topic, what does the 'f' at the end of every function stands for? "function"?
Thanks,
Simon
|
| Name: Yeti |
| Date: 2010-02-15 22:23:08 |
Comment:
I think 'f' stands for float.
|
| Name: Donald Urquhart |
| Date: 2010-02-17 16:49:30 |
Comment:
Hi Simon,
As Yeti has mentioned, the 'f' stands for 'float'.
And there is indeed a problem with the code, move the code for the non-scaled cube to above that of the scaled cube. And also change the glutWireCube to glutWireCube(1).
Another thing to add to my to fix list :)
Cheers, Donald
|
Name Email
|
 |