Archive Pages Design$type=blogging

Scaling in OpenGL

In this post I am going to show a simple way to learn how to do Scaling in OpenGL. Scaling is a property of transformation which helps incr...

In this post I am going to show a simple way to learn how to do Scaling in OpenGL. Scaling is a property of transformation which helps increasing and decreasing the size of an object in computer graphics. The alteration in the size of object is done in each of the axis - X,Y and Z.

For Scaling in OpenGL we have inbuilt function which can be used easily on any 2d or 3d objects. The function  is -

Prototype :

void glScaled (GLdouble x,GLdouble  y,GLdouble  z);
void glScalef (GLfloat  x,GLfloat  y, GLfloat  z);

Sample :

glScalef (GLfloat  1.0f,GLfloat 0.5f , GLfloat  2.0f);

Here, in above sample code we see the function will scale the object into half in y direction, twice in z axis while remain same in x.

One thing is to be note is that - the matrix mode should be either GL_MODELVIEW or GL_PROJECTION, for object to be scaled.

Here is a sample program Which helps you understand the how the scaling works in perspective of  Opengl  computer graphics. This is simple c program, use x,y,z to scale down and X,Y,Z to increase scale factor in respective axis.

#include <gl/glut.h>

int x1 = 20.0f;
int y1 = 30.0f;
int x2 = 40.0f;
int y2 = 50.0f;
int x3 = 60.0f;
int y3 = 10.0f;

void Display()
{
 glClear(GL_COLOR_BUFFER_BIT);
 glPushMatrix();

    glBegin(GL_TRIANGLES);
    glColor3f(0.0f,0.0f,1.0f);

    glVertex3f(x1, y1, 0.0f);
    glVertex3f(x2, y2, 0.0f);
    glVertex3f(x3, y3, 0.0f);


    glEnd( );
    glPopMatrix();
    glFlush();
}
void MyKeyboard(unsigned char key,int mouseX,int mouseY)
{
    if (key == 'x')
    {
        glScaled (0.5f ,1.0f ,1.0f);
        glutPostRedisplay();
        return;
    }
else if (key == 'y')
{
glScaled (1.0f ,0.5f ,1.0f);
        glutPostRedisplay();
        return;
}
else if (key == 'z')
{
glScaled (1.0f ,1.0f ,0.5f);
        glutPostRedisplay();
        return;
}
else if (key == 'X')
    {
        glScaled (1.5f ,1.0f ,1.0f);
        glutPostRedisplay();
        return;
    }
else if (key == 'Y')
{
glScaled (1.0f ,1.5f ,1.0f);
        glutPostRedisplay();
        return;
}
else if (key == 'Z')
{
glScaled (1.0f ,1.0f ,1.5f);
        glutPostRedisplay();
        return;
}
else
{
glScaled (1.0f ,1.0f ,1.0f);
        glutPostRedisplay();
        return;
}

}

void main(int argc,char** argr)
{
 glutInit(&argc,argr);
 glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
 glutInitWindowSize(1000,600);
 glutInitWindowPosition(50,50);
 glutCreateWindow("The Scaling");
 glutDisplayFunc(Display);
 glutKeyboardFunc(MyKeyboard);

 glClearColor(0.0f,0.0f,0.0f,0.0f);
 gluOrtho2D(0.0,1000.0,0.0,600.0);
 glutMainLoop();
}

I think you get to know about the scaling transformation in OpenGL. For any question, query or suggestion put you comments.

COMMENTS

Name

3D Bi-Cycle 3D Game 3D Laptop 3D Objects 3D Projects 3D Zoo Algorithm Android Aquarium Bellman-Ford Algorithm Block Breaker Blockshooting Bucket Sort C# Chess Clock Code Blocks Colors Complex Project Complex Projects Crab Pong Cross Zero DSAV Escapa First Come First Serve Fluids Font Demo Games Gear Motion GlutDino Glutplane Graphics Editor iOS iPhone Java Java OpenGL Graphics Programming Light Torus Linked List Ludo Board Game Mancala board games Memory blocks game Memory Game Menu Mickey Mouse Minesweeper Miniature Steam Engine moth Motion Blur MoveLight moving car Moving Ship Multiplex Networking Based Project Nuclear Power Plant Olympic opengl c++ examples OpenGL ES OpenGL Tutorial Origami OS Based Projects Paint Paper Folding Particles Drop Path Finding ping pong Pong Game Project Report Projects Report Puzzles qix like Random Flowing lines Rigid Body Rings Robot sample c++ opengl code Screen Saver Screen Saver Ship Shadow Cube Shadowfun Ship Iceberg Simple Drawing Simple Haloed Lined Wireframe Simple Move Light Simple Project Simulation SNAKE XENZIA GAME Solar system Sorting Sphere Spot Light Swing Stars Taj Mahal The Edge Game Tic Tac Toe Tower of Hanoi Traffic Signal Transformation Triangular Animation Tutorial water ripple effects Whirlpool
false
ltr
item
OpenGL Projects: Scaling in OpenGL
Scaling in OpenGL
OpenGL Projects
http://www.openglprojects.in/2014/05/scaling-in-opengl.html
http://www.openglprojects.in/
http://www.openglprojects.in/
http://www.openglprojects.in/2014/05/scaling-in-opengl.html
true
2020194800836807725
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago