Mzsmunna
10/16/2019 - 3:17 PM

shipAnimation


#include <iostream>
#include<GL/gl.h>
#include <GL/glut.h>
using namespace std;
//opengl.org
float _move = 0.0f;


float getX(int x)
{
    int width= glutGet(GLUT_WINDOW_WIDTH);
    return (float)x/width/2;
}

float getY(int y)
{
    int height=glutGet(GLUT_WINDOW_HEIGHT);
    return (float)y/height/2;
}

void drawScene() {

    glClear(GL_COLOR_BUFFER_BIT);
    glColor3d(1,0,0);
	glLoadIdentity(); //Reset the drawing perspective
	glMatrixMode(GL_MODELVIEW); //GL_PROJECTION for 2D view! GL_MODELVIEW for 3D view! both are almost same but for 3D better use GL_MODELVIEW

    glPushMatrix(); //START
	glScalef(3.0, 3.0, 0); //Magnify the Image / scaling!
	glTranslatef(_move, 0.0f, 0.0f); //X,Y,Z axix!
	//glTranslatef(0.0f, _move, 0.0f); //X,Y,Z axix!
    /*glBegin(GL_QUADS);
        glVertex3f(-1.0f, -0.2f, 0.0f); //float getX(int x){return (float)x/width/2;}
        glVertex3f(-.30f, -0.2f, 0.0f);
        glVertex3f(-.30f, 0.2f, 0.0f);
        glVertex3f(-1.0f, 0.2f, 0.0f);
	glEnd();*/
	//
	//glColor3ub(0,255,0);
    glBegin(GL_QUADS); //Start from here....
        glVertex3f(getX(300),getY(100), 0.0f); //float getX(int x){return (float)x/width/2;}
        glVertex3f(getX(350),getY(50), 0.0f);
        glVertex3f(getX(550),getY(50), 0.0f);
        glVertex3f(getX(600),getY(100), 0.0f);
    glEnd();

    glBegin(GL_QUADS); //Start from here....
        glVertex3f(getX(400),getY(100), 0.0f); //float getX(int x){return (float)x/width/2;}
        glVertex3f(getX(420),getY(100), 0.0f);
        glVertex3f(getX(420),getY(200), 0.0f);
        glVertex3f(getX(400),getY(200), 0.0f);
    glEnd();

    glBegin(GL_QUADS); //Start from here....
        glVertex3f(getX(440),getY(100), 0.0f); //float getX(int x){return (float)x/width/2;}
        glVertex3f(getX(460),getY(100), 0.0f);
        glVertex3f(getX(460),getY(200), 0.0f);
        glVertex3f(getX(440),getY(200), 0.0f);
    glEnd();

    glBegin(GL_QUADS); //Start from here....
        glVertex3f(getX(480),getY(100), 0.0f); //float getX(int x){return (float)x/width/2;}
        glVertex3f(getX(500),getY(100), 0.0f);
        glVertex3f(getX(500),getY(200), 0.0f);
        glVertex3f(getX(480),getY(200), 0.0f);
    glEnd();
    glPopMatrix(); //END

	glutSwapBuffers();
}

void update(int value) {

    _move += .02;
    if(_move-1.3 > 1.0)
    {
        _move = -1.0;
    }
	glutPostRedisplay(); //Notify GLUT that the display view has changed

	glutTimerFunc(20, update, 0); //Notify GLUT to call update again in 25 milliseconds
}

int main(int argc, char** argv) {
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowSize(800, 800);
	glutCreateWindow("Transformation");
	glutDisplayFunc(drawScene); //call back function
	//glutGet();
	glutTimerFunc(20, update, 0); //Add a timer - 20mili second, update is the call back function, and 0 is the function parameter!!
	glutMainLoop();
	return 0;
}