#include #include #include #include #include #include #include #include using namespace glm; using namespace std; /*--------------------------------------------------*/ char title[] = "Prozor"; int FPS = 60; vec3 CameraPosition(10.0, 17.0, 10.0); vec3 LookAt_vector(0.0, 0.0, 0.0); vec3 LookUp_vector(0.0, 1.0, 0.0); vec3 rocketPos(0, 0, 0); /*--------------------------------------------------*/ vec3 yellow(1.0, 0.9, 0.8); vec3 grey(0.05, 0.05, 0.03); vec3 white(1.0, 1.0, 1.0); vec3 orange(1.0, .7, .1); vec3 darkOrange(.4, .1, .0); vec3 lblue(0, .7, 1); void setColor(vec3 color) { glColor3f(color.r, color.g, color.b); } vec3 lerp(vec3 a, vec3 b, float t) { vec3 v; v.x = b.x * t + a.x * (1.0 - t); v.y = b.y * t + a.y * (1.0 - t); v.z = b.z * t + a.z * (1.0 - t); return v; } void DrawCylinder(vec3 pos, float radius, float height, int n, vec3 color2, vec3 color1, float xOffset = 0.0) { vec3 a(radius + xOffset, 0, 0); vec3 b(radius, height, 0); vec3 lastA(a), lastB(b); float angle = 6.28 / n; mat3 rotation = rotate(angle, vec3(0, 1, 0)); glBegin(GL_QUAD_STRIP); for (int i = 0; i < n; i++) { setColor(lerp(color1, color2, abs((float)i / n - 0.5))); /*vec3 c1 = lerp(color1, color2, (float)i / n); vec3 c2 = lerp(color2, color3, (float)i / n); setColor(lerp(c1, c2, abs((float)i / n - 0.5)));*/ glVertex3f(lastA.x + pos.x, lastA.y + pos.y, lastA.z + pos.z); glVertex3f(lastB.x + pos.x, lastB.y + pos.y, lastB.z + pos.z); lastA = rotation * lastA; lastB = rotation * lastB; } glVertex3f(a.x + pos.x, a.y + pos.y, a.z + pos.z); glVertex3f(b.x + pos.x, b.y + pos.y, b.z + pos.z); glEnd(); } mat3 camRot; mat3 effectScale; float effectSpeed, effectT; mat3 defaultScale = scale(vec3(1, 1, 1)); void animInit() { camRot = rotate(-0.005f, vec3(0, 1, 0)); effectT = 0.0f; effectSpeed = 0.2f / 60; } void DrawCone(vec3 pos, float radius, float height, int n, vec3 color2, vec3 color1, mat3 scale = defaultScale) { vec3 a(radius, 0, 0); vec3 b(0, height, 0); a = scale * a; b = scale * b; vec3 lastA(a); float angle = 6.28 / n; mat3 rotation = rotate(angle, vec3(0, 1, 0)); glBegin(GL_TRIANGLE_STRIP); for (int i = 0; i < n; i++) { setColor(lerp(color1, color2, abs((float)i / n - 0.5))); glVertex3f(lastA.x + pos.x, lastA.y + pos.y, lastA.z + pos.z); glVertex3f(b.x + pos.x, b.y + pos.y, b.z + pos.z); lastA = rotation * lastA; } glVertex3f(a.x + pos.x, a.y + pos.y, a.z + pos.z); glEnd(); } /*--------------------------------------------------*/ void DrawAxis(void) { glLineWidth(2.0); glColor3f(1.0, 0.0, 0.0); glBegin(GL_LINES); glVertex3f(0.0, 0.0, 0.0); glVertex3f(3.0, 0.0, 0.0); glEnd(); glColor3f(0.0, 1.0, 0.0); glBegin(GL_LINES); glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 3.0, 0.0); glEnd(); glColor3f(0.0, 0.0, 1.0); glBegin(GL_LINES); glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 0.0, 3.0); glEnd(); } vec3 points[] = { vec3(0.0, 1.0, 0.0), vec3(.2, .5, 0.0), vec3(0.0, 0.0, 0.0), vec3(.2, .5, 0.0), vec3(.23, .55, 0.0), vec3(0.0, 0.0, 0.0), vec3(.23, .55, 0.0), vec3(.8, .35, 0.0), vec3(0.0, 0.0, 0.0), vec3(.8, .35, 0.0), vec3(1.0, .15, 0.0), vec3(0.0, 0.0, 0.0) }; void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPolygonMode(GL_NONE, GL_LINE); glEnable(GL_POLYGON_SMOOTH); //DrawAxis(); //Big tank //DrawRoundedCylinder(vec3(0.5, 6.0, 0.0), 1.1, 1.5, 1, 48, 8, orange, darkOrange); /*setColor(orange); glLineWidth(4.0); glBegin(GL_LINES); glVertex3f(0.5, 8.0, 0.0); glVertex3f(0.5, 8.5, 0.0); glEnd();*/ setColor(white); glBegin(GL_QUADS); glVertex3f(-4.0, -1.0, -4.0); glVertex3f(-4.0, -1.0, 4.0); glVertex3f(4.0, -1.0, 4.0); glVertex3f(4.0, -1.0, -4.0); glEnd(); DrawCone(vec3(0.55, 9.0, 0.0) + rocketPos, 1.1, 2.0, 48, orange, darkOrange); DrawCylinder(vec3(0.55, 1.0, 0.0) + rocketPos, 1.1, 8.0, 48, orange, darkOrange); //Right tank DrawCone(vec3(2, 7.2, 0) + rocketPos, .5, 1.0, 36, yellow, grey); DrawCylinder(vec3(2, -0.3, 0) + rocketPos, .5, 7.5, 36, yellow, grey); DrawCylinder(vec3(2, -0.3, 0) + rocketPos, .5, 0.6, 36, yellow, grey, .4); DrawCylinder(vec3(2, -0.8, 0) + rocketPos, .3, 0.6, 36, white, grey, .4); //Left tank DrawCone(vec3(-1, 7.2, 0) + rocketPos, .5, 1.0, 36, yellow, grey); DrawCylinder(vec3(-1, -0.3, 0) + rocketPos, .5, 7.5, 36, yellow, grey); DrawCylinder(vec3(-1, -0.3, 0) + rocketPos, .5, 0.6, 36, yellow, grey, .4); DrawCylinder(vec3(-1, -0.8, 0) + rocketPos, .3, 0.6, 36, white, grey, .4); //Rocket DrawCone(vec3(0.5, 6.0, 1.6) + rocketPos, .7, 1.5, 42, white, grey); DrawCylinder(vec3(0.5, 0.8, 1.6) + rocketPos, .7, 5.2, 42, white, grey); //Effect //DrawCone(vec3(0.5, 0.8, 1.6) + rocketPos, .4, -.5, 42, lblue, lblue, effectScale); DrawCone(vec3(-1, -0.8, 0) + rocketPos, .4, -.5, 42, lblue, lblue, effectScale); DrawCone(vec3(2, -0.8, 0) + rocketPos, .4, -.5, 42, lblue, lblue, effectScale); DrawCone(vec3(0.55, 1.0, 0.0) + rocketPos, .4, -.5, 42, lblue, lblue, effectScale); //Wings float wingWidth = 2.0; float wingHeight = 4.5; int n = 12; vec3 wingPos(1.2, 1.0, 1.7); for (int i = 0; i < n; i += 3) { setColor(lerp(white, grey, .2)); glBegin(GL_TRIANGLES); glVertex3f(points[i].x * wingWidth + wingPos.x + rocketPos.x, points[i].y * wingHeight + wingPos.y + rocketPos.y, points[i].z + wingPos.z + rocketPos.z); glVertex3f(points[i + 1].x * wingWidth + wingPos.x + rocketPos.x, points[i + 1].y * wingHeight + wingPos.y + rocketPos.y, points[i + 1].z + wingPos.z + rocketPos.z); glVertex3f(points[i + 2].x * wingWidth + wingPos.x + rocketPos.x, points[i + 2].y * wingHeight + wingPos.y + rocketPos.y, points[i + 1].z + wingPos.z + rocketPos.z); glEnd(); } for (int i = 0; i < n; i += 3) { setColor(lerp(white, grey, .2)); glBegin(GL_TRIANGLES); glVertex3f(-points[i].x * wingWidth - wingPos.x + 1.0 + rocketPos.x, points[i].y * wingHeight + wingPos.y + rocketPos.y, points[i].z + wingPos.z + rocketPos.z); glVertex3f(-points[i + 1].x * wingWidth - wingPos.x + 1.0 + rocketPos.x, points[i + 1].y * wingHeight + wingPos.y + rocketPos.y, points[i + 1].z + wingPos.z + rocketPos.z); glVertex3f(-points[i + 2].x * wingWidth - wingPos.x + 1.0 + rocketPos.x, points[i + 2].y * wingHeight + wingPos.y + rocketPos.y, points[i + 1].z + wingPos.z + rocketPos.z); glEnd(); } glutSwapBuffers(); } void timer(int v) { glMatrixMode(GL_MODELVIEW); glLoadIdentity(); rocketPos.y += 0.05; CameraPosition = camRot * CameraPosition; CameraPosition.y = rocketPos.y + 3.0; gluLookAt( CameraPosition.x, CameraPosition.y, CameraPosition.z, rocketPos.x, rocketPos.y + 5.0, rocketPos.z, //LookAt_vector.x, LookAt_vector.y, LookAt_vector.z, LookUp_vector.x, LookUp_vector.y, LookUp_vector.z ); effectT += effectSpeed; float t = clamp(effectT, 0.0f, 1.0f); //printf("%f\n", t); effectScale = scale(lerp(vec3(0.7, 0.1, 0.7), vec3(1.1, 4.5, 1.1), t)); glutTimerFunc(1000 / FPS, timer, v); glutPostRedisplay(); } void reshape(GLsizei width, GLsizei height) { if (height == 0) { height = 1; } GLfloat aspect = (GLfloat)width / (GLfloat)height; glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(80.0f, aspect, 0.1f, 50.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt( CameraPosition.x, CameraPosition.y, CameraPosition.z, LookAt_vector.x, LookAt_vector.y, LookAt_vector.z, LookUp_vector.x, LookUp_vector.y, LookUp_vector.z ); } void initGL(void) { glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glShadeModel(GL_FLAT); glEnable(GL_DEPTH_TEST); glEnable(GL_SMOOTH); animInit(); } void mousePress(int button, int state, int x, int y) { switch (button) { case GLUT_LEFT_BUTTON: if (state == GLUT_DOWN) { //FUNKCIJA } break; case GLUT_RIGHT_BUTTON: if (state == GLUT_DOWN) { //FUNKCIJA } break; default: break; } } void keyPress(unsigned char key, int x, int y) { switch (key) { case 'A': case 'a': //FUNCTION break; case 'S': case 's': //FUNCTION break; case 'D': case 'd': //FUNCTION break; case 'W': case 'w': //FUNCTION break; default: break; } } /*--------------------------------------------------*/ int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE); glutInitWindowSize(950, 650); glutInitWindowPosition(150, 50); glutCreateWindow(title); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt( CameraPosition.x, CameraPosition.y, CameraPosition.z, LookAt_vector.x, LookAt_vector.y, LookAt_vector.z, LookUp_vector.x, LookUp_vector.y, LookUp_vector.z ); glutDisplayFunc(display); glutTimerFunc(100, timer, 0); glutReshapeFunc(reshape); initGL(); glutMouseFunc(mousePress); glutKeyboardFunc(keyPress); glutMainLoop(); return 0; }