10. OpenGL Scaling




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;
}

Related posts:

  1. 8. OpenGL Double Buffering
  2. 7. OpenGL Rotation and Translation (Version 2.0)
  3. 30. OpenGL Circle Drawing
  4. 9. OpenGL Blending
  5. 6. OpenGL Cube (Version 2.0)

Tags: , , , , ,


9 Responses to "10. OpenGL Scaling"

  • kropcke says:
  • andrew says:
    • Swiftless says:
  • a man says:
  • Imrn says:
    • Swiftless says:
  • Swiftless says:
  • pennypacker says:
Leave a Comment

Improve the web with Nofollow Reciprocity.