#define _USE_MATH_DEFINES #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(0.0, 0.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 int kCount=0; 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, "%d", kCount); 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); mat4 MT; MT = translate(vec3(0.0, -0.75, 0.0)); coordinateSystem = MT * coordinateSystem; } struct ghost { vec3 pos; bool alive; ghost() { pos = vec3(0); } ghost(vec3 p, vec3 d) { pos = p; alive = true; } }; int gCount = 0; ghost ghosts[1000]; 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 vert(vec3 v) { glVertex3f(v.x, v.y, v.z); } void drawGrid() { float step = 0.5f; int lines = 100; float yOffset = -1.0f; glColor3f(0, 1, 0); glLineWidth(0.05f); glBegin(GL_LINES); for (int i = 0; i < lines; i++) { glVertex3f(-step * lines/2, yOffset, i*step - step*lines/2); glVertex3f(step * lines/2, yOffset, i*step - step * lines / 2); glVertex3f(i * step - step * lines / 2, yOffset, -step * lines/2); glVertex3f(i * step - step * lines / 2, yOffset, step * lines/2); } glEnd(); } void circle(vec3 pos, mat3x3 rot, float radius, int points) { vec3 a(0, radius, 0); mat3x3 incr = rotate(6.28f / points, vec3(0, 0, 1)); vec3 b(a); for (int i = 0; i < points; i++) { vert(rot * b + pos); b = incr * b; } vert(rot * a + pos); } void eye(vec3 pos, vec3 offset, mat3x3 rot, float radius, int points) { vec3 a(0, radius, 0); mat3x3 incr = rotate(6.28f / points, vec3(0, 0, 1)); vec3 b(a); for (int i = 0; i < points; i++) { vert(rot * (b + offset) + pos); b = incr * b; vert(rot * (b + offset) + pos); } vert(rot * (a+offset) + pos); } void drawGhost(vec3 pos, mat3x3 rot) { glColor3f(1, 1, 1); glBegin(GL_POLYGON); circle(pos, rot, 0.5f, 32); glEnd(); vec3 up(0, 0.05f, 0); vec3 right(0.21f, 0, 0); vec3 forw(0, 0, 0.05f); glLineWidth(2.0f); glColor3f(0, 0, 0); glBegin(GL_LINES); eye(pos, right + up + forw, rot, 0.15f, 16); glEnd(); glBegin(GL_LINES); eye(pos, -right + up + forw, rot, 0.15f, 16); glEnd(); glColor3f(1, 1, 1); glBegin(GL_TRIANGLES); vec3 c1(-0.5f, 0, 0); vec3 c2(0.5f, 0, 0); for (int i = 0; i < 32; i+=1) { float t = (float)i / 64; float t2 = (1.0f + i) / 64; vec3 s(-0.5f + t, -0.5f - sin(t * 22.0f) * 0.051f , 0); vec3 s2(-0.5f + t2, -0.5f - sin(t2 * 22.0f) * 0.051f, 0); vert(pos + rot * c1); vert(pos + rot * s); vert(pos + rot * s2); } for (int i = 32; i < 64; i += 1) { float t = (float)i / 64; float t2 = (1.0f + i) / 64; vec3 s(-0.5f + t, -0.5f - sin(t * 22.0f) * 0.051f, 0); vec3 s2(-0.5f + t2, -0.5f - sin(t2 * 22.0f) * 0.051f, 0); vert(pos + rot * c2); vert(pos + rot * s); vert(pos + rot * s2); } glEnd(); } float crossSize = 0.3f; // 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(); drawGrid(); mat3x3 rot = rotate(0.0f, vec3(0, 1, 0)); for (int i = 0; i < gCount; i++) { ghost g = ghosts[i]; if (g.alive) { mat3x3 gRot = rotate(arrowRotate, vec3(0, 1, 0)); ghosts[i].pos += normalize(CameraPosition - g.pos) * 0.009f; ghosts[i].pos.y = 0.1f; drawGhost(g.pos, gRot); } } //drawGhost(vec3(0,0,0), rot); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-ratio, ratio, -1, 1); glDisable(GL_DEPTH_TEST); glColor3f(1, 0, 0); glLineWidth(0.2f); glBegin(GL_LINES); glVertex2f(0, crossSize / 2); glVertex2f(0, -crossSize / 2); glVertex2f(crossSize / 2, 0); glVertex2f(-crossSize / 2, 0); glEnd(); RenderString(-0.8f, -0.65f, GLUT_BITMAP_TIMES_ROMAN_24, 1, 0, 0); glColor3f(0, 0, 0); glBegin(GL_POLYGON); vec3 mapOff(0.8f, -0.65f, 0); circle(mapOff, rot, 0.3f, 48); glEnd(); glColor3f(0, 0.8f, 0); glBegin(GL_POLYGON); circle(mapOff, rot, 0.02f, 12); glEnd(); mat3x3 pRot = rotate(-arrowRotate, vec3(0,0,1)); glColor3f(1, 0, 0); for (int i = 0; i < gCount; i++) { float dist = distance(CameraPosition, ghosts[i].pos); if (ghosts[i].alive && dist < 10) { vec3 p = ghosts[i].pos - CameraPosition; vec3 ep(p.x/10 * 0.3f, -p.z/10 * 0.3f, 0); ep = pRot * ep; ep += mapOff; glBegin(GL_POLYGON); circle(ep, rot, 0.02f, 12); glEnd(); if (dist < 0.5f) { exit(0); } } } glEnable(GL_DEPTH_TEST); glutSwapBuffers(); } float spawner = 0.0f; float nextSpawn = 0.0f; int random(int max) { return rand() % max; } void spawnGhost() { vec3 pos(-10 + random(20),0.1f,-10 + random(20)); vec3 dir = pos - CameraPosition; dir = normalize(dir); ghost g(pos, dir); ghosts[gCount] = g; gCount++; } void timer(int v) { if (spawner >= nextSpawn) { spawnGhost(); spawner = 0.0f; nextSpawn = random(3.5f)+0.5f; } spawner += 1.0 / FPS; 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); } 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 = transpose(translate(mat4x4(1.f), vec3(-CameraPosition.x, -CameraPosition.y, -CameraPosition.z))); mtr = rotate(mat4x4(1.f), ROTATION_CONST, w); mt2 = transpose(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 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 = transpose(translate(mat4x4(1.f), vec3(-CameraPosition.x, -CameraPosition.y, -CameraPosition.z))); mtr = rotate(mat4x4(1.f), -ROTATION_CONST, w); mt2 = transpose(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; } void initGL(void) { glClearColor(0.0f, 0.0f, 0.0f, 0.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 shoot() { printf("Player fired.\n"); vec3 dir = (LookAt_vector - CameraPosition); for (int i = 0; i < gCount; i++) { ghost g = ghosts[i]; if (g.alive) { vec3 testPos = dir * distance(CameraPosition, g.pos) + CameraPosition; if (distance(testPos, g.pos) < 0.7f) { ghosts[i].alive = false; kCount++; printf("Ghost killed.\n"); } } } } void keyPress(unsigned char key, int x, int y) { switch (key) { case ' ': shoot(); break; 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) { srand(2546); 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; }