Skip to content

Instantly share code, notes, and snippets.

@SohanChy
Created March 18, 2018 10:53
Show Gist options
  • Save SohanChy/92e1988ef33712f2f239fff63d2a081e to your computer and use it in GitHub Desktop.
Save SohanChy/92e1988ef33712f2f239fff63d2a081e to your computer and use it in GitHub Desktop.
glut_moving.cpp
#include <iostream>
#include<GL/gl.h>
#include <GL/glut.h>
#include <math.h>
using namespace std;
#define PI 3.1415926535897932384626433832795
float _move = 0.0f;
float _wave = 0.0f;
void drawScene() {
glClearColor(52, 152, 219,1.0);
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(-_wave, 0.0f, 0.0f);
//water
glColor3d(0.0f,0.8f,1.0f);
glBegin(GL_POLYGON);
glVertex3f(-50.0f, -1.0f, 0.0f);
glVertex3f(50.0f, -1.0f, 0.0f);
glVertex3f(50.0f, -0.5f, 0.0f);
float bend = 0.1;
for(float i = -49; i < 50; i = i + 0.3)
{
glVertex3f(i, -0.5 + bend, 0.0f);
glVertex3f(i+0.2, -0.5, 0.0f);
}
glVertex3f(-50.0f, -0.5f, 0.0f);
glEnd();
glPopMatrix();
glPushMatrix();
//Draw Circle
glBegin(GL_POLYGON);
glColor3d(0.9,0.9,0);
for(double i = 0; i < 2 * PI; i += PI / 8) {
glVertex3f(cos(i) * 0.15-0.7, sin(i) * 0.15, 0.0);
}
glEnd();
glTranslatef(-0.5, 0.0f, 0.0f);
glTranslatef(_move, 0.0f, 0.0f);
//ship
glColor3d(0.1,0,0.1);
glBegin(GL_POLYGON);
glVertex3f(-0.1f, -0.5f, 0.0f);
glVertex3f(-0.0f, -0.35f, 0.0f);
glVertex3f(-0.5f, -0.35f, 0.0f);
glVertex3f(-0.4f, -0.5f, 0.0f);
glEnd();
//chimney
glBegin(GL_POLYGON);
glVertex3f(-0.15f,-0.35f, 0.0f);
glVertex3f(-0.1f, -0.35f, 0.0f);
glVertex3f(-0.1f, -0.1f, 0.0f);
glVertex3f(-0.15f,-0.1f, 0.0f);
glEnd();
glBegin(GL_POLYGON);
glVertex3f(-0.25f,-0.35f, 0.0f);
glVertex3f(-0.2f, -0.35f, 0.0f);
glVertex3f(-0.2f, -0.1f, 0.0f);
glVertex3f(-0.25f,-0.1f, 0.0f);
glEnd();
glBegin(GL_POLYGON);
glVertex3f(-0.35f,-0.35f, 0.0f);
glVertex3f(-0.3f, -0.35f, 0.0f);
glVertex3f(-0.3f, -0.1f, 0.0f);
glVertex3f(-0.35f,-0.1f, 0.0f);
glEnd();
glPopMatrix();
glutSwapBuffers();
}
void update(int value) {
_move += .004;
_wave += .009;
if(_move-0.8 > 1.0)
{
_move = -1.0;
}
glutPostRedisplay(); //Notify GLUT that the display has changed
glutTimerFunc(50, 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);
glutTimerFunc(50, update, 0); //Add a timer
glutMainLoop();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment