glScalef() not Scaling for user define variable

Hey, I’m new to opengl and I’m trying to learn basics. Below is code where scaling is possible only when i use number directly (e.g: glScalef(5,5,5)) but when i use user-defined variable like ‘sx’ then output window is not showing Box. So can anyone help me where i’m going wrong…?

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

int sx;

void Special_key(int key, int x, int y)
{
    switch (key)
    {
    case GLUT_KEY_UP:sx += 5; break;
    case GLUT_KEY_DOWN:sx -= 5; break;
    default:break;
    }

    glutPostRedisplay();
}

void Box()
{
    glColor3f(1, 1, 1);
    glBegin(GL_QUADS);
    glVertex3f(-10, -10, 0);
    glVertex3f(10, -10, 0);
    glVertex3f(10, 10, 0);
    glVertex3f(-10, 10, 0);
    glEnd();
    glutSwapBuffers();
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glPushMatrix();
    glScalef(sx, 5, 5);
    Box();
    glPopMatrix();
}

void init()
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glColor3f(1, 1, 1);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-100, 100, -100, 100);
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
     glutInitWindowSize(500, 500);
     glutInitWindowPosition(100, 150);
     glutCreateWindow("Transforming point");
     glutDisplayFunc(display);
     init();
     glutSpecialFunc(Special_key);
     glutMainLoop();
     return 0;
}

The variable sy is initially zero and never changes. So the box will always have a height of zero.

Yaa, I saw that. That’s true. But still it after removing sy output window is not showing anything.

Note :Sorry :grin: i updated code.

You never initialize sx. In C and C++, variables that don’t have an initializer have arbitrary values. You should initialize it to 1 or something.

It also makes no real sense to add/subtract 5 (or any number) to a scaling factor.

Global variables are initialised to zero if not explicitly initialised to some other value.

1 Like