#define GLM_ENABLE_EXPERIMENTAL #include #include #include #include #include #include #include #include using namespace glm; using namespace std; #define MOVING_CONST 0.1 #define ROTATION_CONST 3.14f / 180.f #define LOOK_MOVEMENT_CONST 0.1f /*--------------------------------------------------*/ char title[] = "Prozor"; int FPS = 60; vec3 CameraPosition(1.0, 1.0, 1.0); vec3 LookAt_vector(0.0, 0.0, 0.0); vec3 LookUp_vector(0.0, 1.0, 0.0); vector coordinateSystem; const int circle_dots = 50; const float height = 480; const float ratio = 16.f / 9.f; double upDownAngle = 0; int currentSpeed = 1; float arrowRotate = 0.f; vec3 operator* (mat4x4 mat, vec3 vec) { vec4 v(vec.x, vec.y, vec.z, 1.f); v = mat * v; return vec3(v.x, v.y, v.z); } vector operator* (mat4x4 mat, vector vectors) { for (int i = 0; i < vectors.size(); i++) vectors[i] = mat * vectors[i]; return vectors; } //o GLUT_BITMAP_TIMES_ROMAN_24 //o GLUT_BITMAP_TIMES_ROMAN_10 //o GLUT_BITMAP_HELVETICA_18 void RenderString(float x, float y, void* font, double r, double g, double b) { glColor3f(r, g, b); glRasterPos2f(x, y); char s[100]; sprintf(s, "x = %.2lf\ny = %.2lf\nz = %.2lf", CameraPosition.x, CameraPosition.y, CameraPosition.z); glutBitmapString(font, (const unsigned char*)s); } /*--------------------------------------------------*/ void createCoordinates() { coordinateSystem.resize(4); coordinateSystem[0] = vec3(0.0, 0.0, 0.0); coordinateSystem[1] = vec3(1.0, 0.0, 0.0); coordinateSystem[2] = vec3(0.0, 1.0, 0.0); coordinateSystem[3] = vec3(0.0, 0.0, 1.0); } void drawCoordinates() { glLineWidth(2.0); glBegin(GL_LINES); // X axis glColor3f(1.0f, 0.0f, 0.0f); // Red glVertex3d(coordinateSystem[0].x, coordinateSystem[0].y, coordinateSystem[0].z); glVertex3d(coordinateSystem[1].x, coordinateSystem[1].y, coordinateSystem[1].z); // Y axis glColor3f(0.0f, 1.0f, 0.0f); // Green glVertex3d(coordinateSystem[0].x, coordinateSystem[0].y, coordinateSystem[0].z); glVertex3d(coordinateSystem[2].x, coordinateSystem[2].y, coordinateSystem[2].z); // Z axis glColor3f(0.0f, 0.0f, 1.0f); // Blue glVertex3d(coordinateSystem[0].x, coordinateSystem[0].y, coordinateSystem[0].z); glVertex3d(coordinateSystem[3].x, coordinateSystem[3].y, coordinateSystem[3].z); glEnd(); } void glVertexv(vec3 v){ glVertex3d(v.x, v.y, v.z); } // define colors vec3 WHITE_BASE = vec3(.9, .9, .9); vec3 GRAY_BASE = vec3(.4, .4, .4); vec3 ORANGE_BASE = vec3(.9, .4, .0); vec3 RED_BASE = vec3(.9, .0, .0); vec3 chrome = vec3(.7, .7, .7); const float PI = 3.14f; const int DETAIL = 32; vector GenerateCircle(int detail, vec3 center = vec3(0, 0, 0), float scale = 1, vec3 Xaxis = vec3(1, 0, 0), vec3 Yaxis = vec3(0, 1, 0), vec3 Zaxis = vec3(0, 0, 1)){ Xaxis *= scale; Yaxis *= scale; Zaxis *= scale; float move = 2 * PI / detail; vector circle; for(int i = 0; ipoly, int mode=GL_LINE_LOOP, vec3 color = vec3(.5,.5,.5)){ glBegin(mode); glColor3f(color.x, color.y, color.z); for(int i = 0; i> GenerateCylinder(vec3 center, float h, float baser, float topr, int detail = 16, vec3 Xaxis = vec3(1, 0, 0), vec3 Yaxis = vec3(0, 1, 0), vec3 Zaxis = vec3(0, 0, 1)){ // center = center + Yaxis * 2.5f; vector> cylinder; vector base = GenerateCircle(detail, center, baser); vector top = GenerateCircle(detail, center + Yaxis * h, topr); cylinder.push_back(base); cylinder.push_back(top); return cylinder; } void DrawSurface(vec3 a, vec3 b, vec3 c, vec3 d, vec3 color){ vec3 light = vec3(1, 0 ,0); float shade = dot(light, normalize(cross(b - a, b - c))); float ambient = .5; if(shade > 1) shade = 1; if(shade < 0) shade = 0; shade = (shade + ambient*2)/2; color = color * shade; glBegin(GL_POLYGON); glColor3f(color.x, color.y, color.z); glVertexv(a); glVertexv(b); glVertexv(c); glVertexv(d); glEnd(); } void DrawSurface(vector surface, vec3 color){ vec3 light = vec3(1, 0 ,0); float shade = dot(light, normalize(cross(surface[1] - surface[0], surface[1] - surface[2]))); float ambient = .5; if(shade > 1) shade = 1; if(shade < 0) shade = 0; shade = (shade + ambient*2)/2; color = color * shade; glBegin(GL_POLYGON); glColor3f(color.x, color.y, color.z); for(int i = 0; i>cylinder, vec3 color = vec3(.5,.5,.5), int mode=GL_POLYGON){ DrawPolygon(cylinder[0], mode, color); DrawPolygon(cylinder[1], mode, color); // glColor3f(color.x, color.y, color.z); for(int i = 0; i{ center + Xaxis * scale * .075f, center + Xaxis * scale * .075f + Yaxis * scale * .05f, center + Xaxis * scale * (.075f + .05f) + Yaxis * scale * .05f }, ORANGE_BASE); DrawSurface(vector{ center + Zaxis * scale * .075f, center + Zaxis * scale * .075f + Yaxis * scale * .05f, center + Zaxis * scale * (.075f + .05f) + Yaxis * scale * .05f }, ORANGE_BASE); DrawSurface(vector{ center - Xaxis * scale * .075f, center - Xaxis * scale * .075f + Yaxis * scale * .05f, center - Xaxis * scale * (.075f + .05f) + Yaxis * scale * .05f }, ORANGE_BASE); DrawSurface(vector{ center - Zaxis * scale * .075f, center - Zaxis * scale * .075f + Yaxis * scale * .05f, center - Zaxis * scale * (.075f + .05f) + Yaxis * scale * .05f }, ORANGE_BASE); // engine DrawCylinder(GenerateCylinder( center, scale * .01, scale * .075, scale * .075, DETAIL ), GRAY_BASE); DrawCylinder(GenerateCylinder( center + Yaxis * (scale * .01f), scale * .09, scale * .075, scale * .075, DETAIL ), ORANGE_BASE); DrawCylinder(GenerateCylinder( center + Yaxis * (scale * .1f), scale * .2f, scale * .075f, scale * .075f, DETAIL ), GRAY_BASE); DrawCylinder(GenerateCylinder( center + Yaxis * (scale * .3f), scale * .2f, scale * .075f, scale * .075f, DETAIL ), WHITE_BASE); DrawCylinder(GenerateCylinder( center + Yaxis * (scale * .5f), scale * .2f, scale * .075f, scale * .05f, DETAIL ), WHITE_BASE); DrawCylinder(GenerateCylinder( center + Yaxis * (scale * .7f), scale * .1f, scale * .05f, scale * .01f, DETAIL ), GRAY_BASE); } void DisplayRocket(vec3 Xaxis = vec3(1, 0, 0), vec3 Yaxis = vec3(0, 1, 0), vec3 Zaxis = vec3(0, 0, 1)){ // vec3 Xaxis = vec3(1, 0, 0); // vec3 Yaxis = vec3(0, 1, 0); // vec3 Zaxis = vec3(0, 0, 1); // vector> cylinder = GenerateCylinder( // vec3(0, 0, 0), // .5, // .075, // .075, // DETAIL // ); // DrawCylinder(cylinder); // DrawCylinder(GenerateCylinder( // vec3(0, 0, 0), // .5, // .075, // .075, // DETAIL // )); // engines // base DrawCylinder(GenerateCylinder( vec3(0, -.1, 0), .1, .075, .075, DETAIL, Xaxis, Yaxis, Zaxis ), GRAY_BASE); DrawEngine(vec3(0, -.6, 0), Xaxis, Yaxis, Zaxis); DrawEngine(vec3(.075 * 2, -.6, 0), Xaxis, Yaxis, Zaxis); DrawEngine(vec3(-.075 * 2, -.6, 0), Xaxis, Yaxis, Zaxis); DrawEngine(vec3(0, -.6, .075 * 2), Xaxis, Yaxis, Zaxis); DrawEngine(vec3(0, -.6, -.075 * 2), Xaxis, Yaxis, Zaxis); // main body vector> body = GenerateCylinder( vec3(0, 0, 0), .5, .075, .075, DETAIL, Xaxis, Yaxis, Zaxis ); DrawCylinder(body, WHITE_BASE); DrawCylinder( GenerateCylinder( vec3(0, .5, 0), .1, .075, .075, DETAIL, Xaxis, Yaxis, Zaxis ), GRAY_BASE); DrawCylinder( GenerateCylinder( vec3(0, .6, 0), .2, .09, .09, DETAIL, Xaxis, Yaxis, Zaxis ), ORANGE_BASE); DrawCylinder( GenerateCylinder( vec3(0, .8, 0), .1, .09, .09, DETAIL, Xaxis, Yaxis, Zaxis ), WHITE_BASE); DrawCylinder( GenerateCylinder( vec3(0, .9, 0), .1, .09, .09, DETAIL, Xaxis, Yaxis, Zaxis ), GRAY_BASE); DrawCylinder( GenerateCylinder( vec3(0, 1.0, 0), .1, .09, .12, DETAIL, Xaxis, Yaxis, Zaxis ), WHITE_BASE); DrawCylinder( GenerateCylinder( vec3(0, 1.1, 0), .25, .12, .12, DETAIL, Xaxis, Yaxis, Zaxis ), WHITE_BASE); DrawCylinder( GenerateCylinder( vec3(0, 1.35, 0), .2, .12, .05, DETAIL, Xaxis, Yaxis, Zaxis ), WHITE_BASE); } void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(80.f, 16.f / 9.f, 0.1f, 50.f); 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 ); drawCoordinates(); DisplayRocket(); // vector circle = GenerateCircle(16); // DrawPolygon(circle); // vector> cylinder = GenerateCylinder( // vec3(0, 0, 0), // .5, // .5, // .5, // 16 // ); // DrawCylinder(cylinder); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glDisable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST); glutSwapBuffers(); } #pragma region template void timer(int v) { 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 ); glutTimerFunc(1000 / FPS, timer, v); glutPostRedisplay(); } void reshape(GLsizei width, GLsizei height) { if (height * ratio <= width) width = ratio * height; else height = width / ratio; glViewport(0, 0, width, height); } void PrintVector(vec3 vec) { printf("%.2f %.2f %.2f\n", vec.x, vec.y, vec.z); } #pragma region movement void MoveForward() { mat4x4 mt; vec3 v = LookAt_vector - CameraPosition; v = normalize(v); v.y = 0.f; v = v * (currentSpeed * (float)MOVING_CONST); mt = translate(mat4x4(1.f), v); LookAt_vector = mt * LookAt_vector; CameraPosition = mt * CameraPosition; } void MoveBackward() { mat4x4 mt; vec3 v = LookAt_vector - CameraPosition; v.y = 0.f; v = normalize(v); v = -v * (currentSpeed * (float)MOVING_CONST); mt = translate(mat4x4(1.f), v); LookAt_vector = mt * LookAt_vector; CameraPosition = mt * CameraPosition; } void MoveLeft() { mat4x4 mt; vec3 f = LookAt_vector - CameraPosition; vec3 w = cross(LookUp_vector, f); w = normalize(w); w = w * (currentSpeed * (float)MOVING_CONST); mt = translate(mat4x4(1.f), w); CameraPosition = mt * CameraPosition; LookAt_vector = mt * LookAt_vector; } void MoveRight() { mat4x4 mt; vec3 f = LookAt_vector - CameraPosition; vec3 w = cross(LookUp_vector, f); w = normalize(w); w = -w * (currentSpeed * (float)MOVING_CONST); mt = translate(mat4x4(1.f), w); CameraPosition = mt * CameraPosition; LookAt_vector = mt * LookAt_vector; } void TurnLeft() { mat4x4 mt, identityMat, mt1, mt2, mtr; identityMat = mat4x4(1.f); mt1 = translate(identityMat, vec3(CameraPosition.x, CameraPosition.y, CameraPosition.z)); mtr = rotate(identityMat, ROTATION_CONST, vec3(0.f, 1.f, 0.f)); mt2 = translate(identityMat, vec3(-CameraPosition.x, -CameraPosition.y, -CameraPosition.z)); mt = mt1 * mtr * mt2; LookAt_vector = mt * LookAt_vector; arrowRotate += ROTATION_CONST; } void TurnRight() { mat4x4 mt, identityMat, mt1, mt2, mtr; identityMat = mat4x4(1.f); mt1 = translate(identityMat, vec3(CameraPosition.x, CameraPosition.y, CameraPosition.z)); mtr = rotate(identityMat, -ROTATION_CONST, vec3(0.f, 1.f, 0.f)); mt2 = translate(identityMat, vec3(-CameraPosition.x, -CameraPosition.y, -CameraPosition.z)); mt = mt1 * mtr * mt2; LookAt_vector = mt * LookAt_vector; arrowRotate -= ROTATION_CONST; } void TurnUp() { mat4x4 mt, mt1, mt2, mtr; vec3 f = LookAt_vector - CameraPosition; vec3 w = cross(f, LookUp_vector); w = normalize(w); if (upDownAngle + ROTATION_CONST < (float)M_PI_2) { mt1 = translate(mat4x4(1.f), vec3(-CameraPosition.x, -CameraPosition.y, -CameraPosition.z)); mtr = rotate(mat4x4(1.f), ROTATION_CONST, w); mt2 = translate(mat4x4(1.f), vec3(CameraPosition.x, CameraPosition.y, CameraPosition.z)); mt = mt2 * mtr * mt1; LookAt_vector = mt * LookAt_vector; upDownAngle += ROTATION_CONST; } } void TurnDown() { mat4x4 mt, mt1, mt2, mtr; vec3 f = LookAt_vector - CameraPosition; vec3 w = cross(f, LookUp_vector); w = normalize(w); if (upDownAngle - ROTATION_CONST > (float)-M_PI_2) { mt1 = translate(mat4x4(1.f), vec3(-CameraPosition.x, -CameraPosition.y, -CameraPosition.z)); mtr = rotate(mat4x4(1.f), -ROTATION_CONST, w); mt2 = translate(mat4x4(1.f), vec3(CameraPosition.x, CameraPosition.y, CameraPosition.z)); mt = mt2 * mtr * mt1; LookAt_vector = mt * LookAt_vector; // LookUp_vector = mt * LookUp_vector; upDownAngle -= ROTATION_CONST; } } void SpeedUp() { if (currentSpeed < 5) ++currentSpeed; } void SpeedDown() { if (currentSpeed > 0) --currentSpeed; } #pragma endregion movement void initGL(void) { glClearColor(1.0f, 1.0f, 1.0f, 1.0f); glShadeModel(GL_FLAT); glEnable(GL_DEPTH_TEST); } 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 27: //ESC key exit(0); break; case 'w': MoveForward(); break; case 's': MoveBackward(); break; case 'a': //FUNCTION MoveLeft(); break; case 'd': //FUNCTION MoveRight(); break; case 'u': SpeedUp(); break; case 'j': SpeedDown(); break; case 52: TurnLeft(); break; case 54: TurnRight(); break; case 50: TurnDown(); break; case 56: TurnUp(); break; } } /*--------------------------------------------------*/ int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_STENCIL); glutInitWindowSize(height * ratio, height); glutInitWindowPosition(150, 50); glutCreateWindow(title); createCoordinates(); glutDisplayFunc(display); glutTimerFunc(100, timer, 0); glutReshapeFunc(reshape); glutMouseFunc(mousePress); glutKeyboardFunc(keyPress); initGL(); glutMainLoop(); return 0; } #pragma endregion template