From 12f0ab8cb0c02c5165dc8ee237d7716c0604c010 Mon Sep 17 00:00:00 2001 From: Saksham Gupta Date: Tue, 6 Mar 2018 00:27:06 +0530 Subject: [PATCH] Finish --- CMakeLists.txt | 14 +-- CMakeLists.txt.user | 2 +- README.md | 17 ++-- src/barrel.cpp | 28 +++--- src/cube.cpp | 42 +++++++- src/cube.h | 5 + src/input.cpp | 5 +- src/main.cpp | 230 +++++++++++++++++++++++++++++++++++++------- src/main.h | 6 ++ src/rock.cpp | 12 +-- 10 files changed, 287 insertions(+), 74 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f87013c..2062ba5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,5 @@ project(graphics_asgn1) +set (CMAKE_CXX_STANDARD 11) cmake_minimum_required(VERSION 2.8) aux_source_directory(src SRC_LIST) add_executable(${PROJECT_NAME} ${SRC_LIST}) @@ -17,14 +18,13 @@ pkg_check_modules(GL REQUIRED gl) include_directories(${GL_INCLUDE_DIRS}) target_link_libraries (${PROJECT_NAME} ${GL_LIBRARIES}) -# pkg_check_modules(AO REQUIRED ao) -# include_directories(${AO_INCLUDE_DIRS}) -# target_link_libraries (${PROJECT_NAME} ${AO_LIBRARIES}) +pkg_check_modules(AO REQUIRED ao) +include_directories(${AO_INCLUDE_DIRS}) +target_link_libraries (${PROJECT_NAME} ${AO_LIBRARIES}) -# pkg_check_modules(LIBMPG123 REQUIRED libmpg123) -# include_directories(${LIBMPG123_INCLUDE_DIRS}) -# target_link_libraries (${PROJECT_NAME} ${LIBMPG123_LIBRARIES}) +pkg_check_modules(LIBMPG123 REQUIRED libmpg123) +include_directories(${LIBMPG123_INCLUDE_DIRS}) +target_link_libraries (${PROJECT_NAME} ${LIBMPG123_LIBRARIES}) configure_file(src/Sample_GL.vert Sample_GL.vert COPYONLY) configure_file(src/Sample_GL.frag Sample_GL.frag COPYONLY) -# configure_file(assets/song.mp3 assets/song.mp3 COPYONLY) diff --git a/CMakeLists.txt.user b/CMakeLists.txt.user index a14eaec..7d10a15 100644 --- a/CMakeLists.txt.user +++ b/CMakeLists.txt.user @@ -1,6 +1,6 @@ - + EnvironmentId diff --git a/README.md b/README.md index 7f31680..39da9e1 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,14 @@ -Graphics Boilerplate Code -========================= +OpenGL Assignment -This is boilerplate code for the assignments which might be helpful. +Installation Instructions +sudo apt install libglew-dev libglfw3-dev libglm-dev cmake pkg-config +Download QtCreator +Make and Run :) -License -------- -The MIT License https://meghprkh.mit-license.org/ +Controls -Copyright © 2018 Megh Parikh +Use Left arrow key to move left, Use Right arrow key to move right, Use Up arrow key to move forward, Use down arrow key to move backward. +Use f to fire cannon and mouse to aim, c for camera views , s to change sail. + +Scroll works on top-view mode. diff --git a/src/barrel.cpp b/src/barrel.cpp index 212ef49..8020b51 100644 --- a/src/barrel.cpp +++ b/src/barrel.cpp @@ -5,18 +5,14 @@ Barrel::Barrel(float x, float y, color_t color) { this->position = glm::vec3(x, y, 0); - this->size = size; - //rotation = 20; - // ngift = -1; - // Our vertices. Three consecutive floats give a 3D vertex; Three consecutive vertices give a triangle. - // A cube has 6 faces with 2 triangles each, so this makes 6*2=12 triangles, and 12*3 vertices + this->size = 10; int pos=0,i,k,n=100; float j=0; GLfloat vertex_buffer_data[180*n+18]; GLfloat g_vertex_buffer_data[180*n]; float pi = 3.14159, angle = 0, theta=(2*pi)/n; - for(j=0;j<4;j+=0.2){ + for(j=0;j<2;j+=0.1){ for(i=0;isize, 0.0, - 38.0, this->size, 0.0, - 38.0,-1*this->size, 0.0, + 0.0,0.0,-1*this->size, + 19.0,0.0, this->size, + 19.0,0.0,-1*this->size, - 0.0,-1*this->size, 0.0, - 38.0, this->size, 0.0, - 0.0, this->size, 0.0 + 0.0,0.0,-1*this->size, + 19.0,0.0, this->size, + 0.0,0.0, this->size, }; for(i=0;i<180*n+18;i++){ @@ -66,4 +62,10 @@ void Barrel::draw(glm::mat4 VP) { void Barrel::set_position(float x, float y) { this->position = glm::vec3(x, y, 0); -} \ No newline at end of file +} + +bounding_box_t Barrel::bounding_box() { + float x = this->position.x, y = this->position.y; + bounding_box_t bbox = { x, y, 9, 9}; + return bbox; +} diff --git a/src/cube.cpp b/src/cube.cpp index d0127be..5375f54 100644 --- a/src/cube.cpp +++ b/src/cube.cpp @@ -5,9 +5,12 @@ Cube::Cube(float x, float y, color_t color) { this->position = glm::vec3(x, y, 0); this->rotation = 0; this->launch_speed = 0; - speed = 1.0; - // Our vertices. Three consecutive floats give a 3D vertex; Three consecutive vertices give a triangle. - // A cube has 6 faces with 2 triangles each, so this makes 6*2=12 triangles, and 12*3 vertices + speed = 2.0; + this->angular_speed = 0.25; + this->speed_y = 0; + this->level_angle = 0; + this->acceleration_y = -0.0001; + static const GLfloat vertex_buffer_data[] = { 4.0f,-6.0f,-2.0f, 3.0f, 6.0f, 2.0f, @@ -78,9 +81,13 @@ void Cube::draw(glm::mat4 VP) { Matrices.model = glm::mat4(1.0f); glm::mat4 translate = glm::translate (this->position); // glTranslatef glm::mat4 rotate = glm::rotate((float) (this->rotation * M_PI / 180.0f), glm::vec3(0, 0, 1)); + glm::mat4 rotate1 = glm::rotate((float) (this->level_angle * M_PI / 180.0f), glm::vec3( + this->position.x, + this->position.y, + this->position.z - 2)); // No need as coords centered at 0, 0, 0 of cube arouund which we waant to rotate // rotate = rotate * glm::translate(glm::vec3(0, -0.6, 0)); - Matrices.model *= (translate * rotate); + Matrices.model *= (translate * rotate * rotate1); glm::mat4 MVP = VP * Matrices.model; glUniformMatrix4fv(Matrices.MatrixID, 1, GL_FALSE, &MVP[0][0]); draw3DObject(this->object); @@ -105,6 +112,33 @@ void Cube::tick() { // this->position.y -= speed; } +void Cube::shm(){ + if(this->acceleration_y > 0){ + if(this->position.y + speed_y > 60.7){ + acceleration_y = -acceleration_y; + speed_y = 0.02; + } + else{ + // this->position.y += speed_y; + speed_y -= acceleration_y; + } + } + if(this->acceleration_y < 0){ + if(this->position.y - speed_y < 60.1){ + acceleration_y = -acceleration_y; + speed_y = 0.02; + } + else{ + // this->position.y -= speed_y; + speed_y += acceleration_y; + } + } + if(level_angle < -5.0f || level_angle > 5.0f){ + angular_speed = -angular_speed; + } + this->level_angle += angular_speed; +} + bounding_box_t Cube::bounding_box() { float x = this->position.x, y = this->position.y; bounding_box_t bbox = { x, y, 9, 9}; diff --git a/src/cube.h b/src/cube.h index d3569d8..c281e77 100644 --- a/src/cube.h +++ b/src/cube.h @@ -13,7 +13,12 @@ class Cube { float launch_speed; void draw(glm::mat4 VP); void set_position(float x, float y); + float angular_speed; + float speed_y; + float level_angle; + float acceleration_y; void tick(); + void shm(); bounding_box_t bounding_box(); double speed; private: diff --git a/src/input.cpp b/src/input.cpp index 560f4ff..c01db3c 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -71,7 +71,10 @@ void mouseButton(GLFWwindow *window, int button, int action, int mods) { } void scroll_callback(GLFWwindow *window, double xoffset, double yoffset) { - // Do something + if(yoffset<0) + zoom_camera(1); + if(yoffset>0) + zoom_camera(-1); } void cursor_position_callback(GLFWwindow* window, double xpos, double ypos) { diff --git a/src/main.cpp b/src/main.cpp index 475829c..f0da431 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -28,12 +28,13 @@ Monster monster[100]; Rock gift; Barrel barrel[100]; float screen_zoom = 1, screen_center_x = 0, screen_center_y = 0 , gravity = 0.5 ,level = 3,pi=3.141; -float camera_x=0,camera_y=90,camera_z=100,target_x=0,target_y=90,target_z=0,health = 3; -int rockCount = 100,monsterCount = 100,barrelCount=40,view = 1, time_cnt = 0, time_blast = 0,time_gift=0; +float camera_x=0,camera_y=90,camera_z=100,target_x=0,target_y=90,target_z=0,health = 3,wind_rotation=0; +int rockCount = 100,monsterCount = 100,barrelCount=40,view = 1, time_cnt = 0, time_blast = 0,time_gift=0,fireCheck = 0,score = 0,wind_cnt = 0 ,wind = 0; Timer t60(1.0 / 60); int escape = 1; void change_camera(); void speed_camera(); +void monster_move(); /* Render the scene with openGL */ /* Edit this function according to your assignment */ void draw() { @@ -70,7 +71,7 @@ void draw() { for(int i = 0 ; i < rockCount ; i++){ rocks[i].draw(VP); } - for(int i = 0 ; i < rockCount ; i++){ + for(int i = 0 ; i < monsterCount ; i++){ monster[i].draw(VP); } boat.draw(VP); @@ -79,10 +80,21 @@ void draw() { sail.draw(VP); aim.draw(VP); gift.draw(VP); - for(int i = 0 ; i < rockCount ; i++){ + for(int i = 0 ; i < barrelCount ; i++){ barrel[i].draw(VP); } } +void displayScore(){ + string a = "Health : "; + string b = to_string(health); + b = a+b; + string c = " Score : "; + string d= to_string(score); + d = c + d; + b = b + d; + + glfwSetWindowTitle(window, const_cast(b.c_str())); +} void tick_input(GLFWwindow *window) { int left = glfwGetKey(window, GLFW_KEY_LEFT); @@ -92,6 +104,8 @@ void tick_input(GLFWwindow *window) { int jump = glfwGetKey(window, GLFW_KEY_SPACE); int fire = glfwGetKey(window, GLFW_KEY_F); int camera = glfwGetKey(window, GLFW_KEY_C); + int sails_s = glfwGetKey(window, GLFW_KEY_S); + fireCheck = fire; if (left) { int flag = 0; @@ -128,7 +142,7 @@ void tick_input(GLFWwindow *window) { boat.position.x -= boat.speed*sin(boat.rotation*M_PI/180.0f); int flag = 0; for (int i = 0 ; i < rockCount ; i++) { - if (detect_collision(boat.bounding_box(),rocks[i].bounding_box()) && rocks[i].position.z != -1000) { + if (detect_collision(boat.bounding_box(),rocks[i].bounding_box()) && rocks[i].position.z != -1000&& boat.position.z <= rocks[i].position.z+rocks[i].size) { if (escape == 1) { health -= 0.5; escape = 0; @@ -137,6 +151,25 @@ void tick_input(GLFWwindow *window) { break; } } + for (int i = 0 ; i < monsterCount ; i++) { + if (detect_collision(boat.bounding_box(),monster[i].bounding_box()) && monster[i].position.z != -1000 && boat.position.z <= monster[i].position.z+monster[i].size) { + if (escape == 1) { + health -= 1; + escape = 0; + } + flag = 1; + break; + } + } + for (int i = 0 ; i < barrelCount ; i++) { + if (detect_collision(boat.bounding_box(),barrel[i].bounding_box()) && barrel[i].position.z != -1000 && boat.position.z == level) { + if (escape == 1) { + escape = 0; + } + flag = 1; + break; + } + } if (flag == 0) { pool.position.y = boat.position.y; if (cannon.position.z == boat.position.z) { @@ -161,7 +194,7 @@ void tick_input(GLFWwindow *window) { boat.position.x += boat.speed*sin(boat.rotation*M_PI/180.0f); int flag = 0; for (int i = 0 ; i < rockCount ; i++) { - if (detect_collision(boat.bounding_box(),rocks[i].bounding_box()) && rocks[i].position.z != -1000) { + if (detect_collision(boat.bounding_box(),rocks[i].bounding_box()) && rocks[i].position.z != -1000&& boat.position.z <= rocks[i].position.z+rocks[i].size) { if (escape == 1) { health -= 0.5; escape = 0; @@ -170,6 +203,25 @@ void tick_input(GLFWwindow *window) { break; } } + for (int i = 0 ; i < monsterCount ; i++) { + if (detect_collision(boat.bounding_box(),monster[i].bounding_box()) && monster[i].position.z != -1000&& boat.position.z <= monster[i].position.z+monster[i].size) { + if (escape == 1) { + health -= 1; + escape = 0; + } + flag = 1; + break; + } + } + for (int i = 0 ; i < barrelCount ; i++) { + if (detect_collision(boat.bounding_box(),barrel[i].bounding_box()) && barrel[i].position.z != -1000 && boat.position.z == level) { + if (escape == 1) { + escape = 0; + } + flag = 1; + break; + } + } if (flag == 0) { pool.position.y = boat.position.y; if (cannon.position.z == boat.position.z) { @@ -196,18 +248,52 @@ void tick_input(GLFWwindow *window) { if (fire && cannon.position.z == boat.position.z && aim.rotation_h > 3 && time_blast == 0) { cannon.launch_speed = 10 * sin(aim.rotation_h*M_PI/180); cannon.launch_speed_x = -10 * cos(aim.rotation_h*M_PI/180) * sin(aim.rotation_z*M_PI/180); - cannon.launch_speed_y = 10 * cos(aim.rotation_h*M_PI/180) * cos(aim.rotation_z*M_PI/180); + cannon.launch_speed_y = 10 *cos(aim.rotation_h*M_PI/180) * cos(aim.rotation_z*M_PI/180); } if (camera && time_cnt > 30) { change_camera(); time_cnt = 0; } + + if (sails_s && time_cnt > 30) { + if (sail.position.z != -1000) { + sail.position.z = -1000; + boat.speed = 2; + } + else { + sail.position.z = level; + if (wind) + boat.speed = 4; + } + time_cnt = 0; + } } void tick_elements() { + if (sail.position.z != -1000 && wind == 1) { + boat.rotation = wind_rotation; + } + if (wind_cnt%600 == 0) { + if (boat.speed == 2) { + boat.speed = 4; + wind = 1; + wind_rotation = rand()%180; + boat.rotation = wind_rotation; + } + else { + boat.speed = 2; + wind = 0; + } + } boat.tick(); // printf("%lf %lf %lf",boat.position.x,boat.position.y,boat.position.z); // camera_rotation_angle += 1; + monster_move(); + boat.shm(); + sail.position.x = boat.position.x; + sail.position.y = boat.position.y; + aim.position.x = boat.position.x; + aim.position.y = boat.position.y; boat.position.z += boat.launch_speed; cannon.position.z += cannon.launch_speed; cannon.position.x += cannon.launch_speed_x; @@ -241,7 +327,8 @@ void tick_elements() { } speed_camera(); time_cnt ++; - sail.position.z = boat.position.z; + if (sail.position.z != -1000) + sail.position.z = boat.position.z; if (time_blast > 60) { time_blast = 0; blast.size = 25; @@ -249,38 +336,97 @@ void tick_elements() { blast.position.y = -1000; } for (int i = 0 ; i < rockCount ; i++) { - if(detect_collision(blast.bounding_box(),rocks[i].bounding_box())) { + if(detect_collision(blast.bounding_box(),rocks[i].bounding_box())&& rocks[i].position.z != -1000 ) { rocks[i].position.z = -1000; + score ++; + } + + if(detect_collision(cannon.bounding_box(),rocks[i].bounding_box()) && rocks[i].position.z != -1000 + && cannon.position.x != boat.position.x && cannon.position.y != boat.position.y) { + rocks[i].position.z = -1000; + score ++; + cannon.launch_speed = 0; + cannon.launch_speed_x = 0; + cannon.launch_speed_y = 0; + if (cannon.position.x != boat.position.x && cannon.position.y != boat.position.y) { + blast.position.y = cannon.position.y; + blast.position.x = cannon.position.x; + // blast.position.z = cannon.position.z; + time_blast = 1; + } + cannon.position.z = level; + cannon.position.x = boat.position.x; + cannon.position.y = boat.position.y; } } for (int i = 0 ; i < monsterCount ; i++) { - if(detect_collision(blast.bounding_box(),monster[i].bounding_box())) { + if(detect_collision(blast.bounding_box(),monster[i].bounding_box())&& monster[i].position.z != -1000) { monster[i].position.z = -1000; + if (i%10 != 0) + score += (i%3) + 1; if (i%10 == 0) { //mega-monster gift.position.x = monster[i].position.x; gift.position.y = monster[i].position.y; + score += 10; } } + + if(detect_collision(cannon.bounding_box(),monster[i].bounding_box()) && monster[i].position.z != -1000 + && cannon.position.x != boat.position.x && cannon.position.y != boat.position.y) { + monster[i].position.z = -1000; + if (i%10 != 0) + score += (i%3) + 1; + if (i%10 == 0) { + //mega-monster + gift.position.x = monster[i].position.x; + gift.position.y = monster[i].position.y; + score += 10; + } + cannon.launch_speed = 0; + cannon.launch_speed_x = 0; + cannon.launch_speed_y = 0; + if (cannon.position.x != boat.position.x && cannon.position.y != boat.position.y) { + blast.position.y = cannon.position.y; + blast.position.x = cannon.position.x; + // blast.position.z = cannon.position.z; + time_blast = 1; + } + cannon.position.z = level; + cannon.position.x = boat.position.x; + cannon.position.y = boat.position.y; + } } if(detect_collision(boat.bounding_box(),gift.bounding_box())) { gift.position.x = -1000; gift.position.y = -1000; time_gift++; - boat.speed *= 5; + if (boat.speed == 2) + boat.speed *= 3; + health++; + score += 5; + } + for (int i = 0 ; i < barrelCount ; i++){ + if(detect_collision(boat.bounding_box(),barrel[i].bounding_box()) && barrel[i].position.z != -1000 && boat.position.z!= level) { + score++; + barrel[i].position.z = -1000; + } } if (time_gift) { time_gift++; } if (time_gift >= 600) { time_gift = 0; - boat.speed /= 5; + boat.speed /= 3; } if (time_blast > 0) { time_blast ++; - blast.size -= 0.35; + blast.size -= 0.3; + } + if (health <= 0) { + quit(window); } - // printf("Health: %f\n",health); + wind_cnt++; } /* Initialize the OpenGL rendering properties */ @@ -365,7 +511,6 @@ int main(int argc, char **argv) { window = initGLFW(width, height); initGL (window, width, height); - /* Draw in loop */ while (!glfwWindowShouldClose(window)) { // Process timers @@ -379,6 +524,7 @@ int main(int argc, char **argv) { tick_elements(); tick_input(window); + displayScore(); } // Poll for Keyboard and mouse events @@ -401,6 +547,9 @@ void reset_screen() { } void change_camera(){ view = (view+1)%5; + if (view == 2) { + camera_z = boat.position.z + 300; + } } void speed_camera(){ @@ -438,7 +587,7 @@ void speed_camera(){ else if(view==2){ //top-view camera_x = boat.position.x+1; camera_y = boat.position.y; - camera_z = boat.position.z+300; + // camera_z = boat.position.z+300; target_x = boat.position.x; target_y = boat.position.y; @@ -448,8 +597,8 @@ void speed_camera(){ void heli_camera(float x, float y){ if(view == 0) { - target_y = boat.position.x+x/3-100; - target_x = boat.position.y+y/3-100; + target_y = boat.position.y+x/3-100; + target_x = boat.position.x+y/3-100; } } @@ -459,19 +608,32 @@ void cannon_pos(float x, float y) { aim.rotation_h = -(3*y/20) + 90; } } -// void zoom_camera(int type){ -// if(view==2){ -// float l = target_x-camera_x; -// float m = target_y-camera_y; -// float n = target_z-camera_z; -// if(type==1){ -// if(camera_z-10>target_z) -// camera_z-=10; -// } -// else if(type==-1){ -// camera_z+=10; -// } -// camera_x = l*(camera_z-target_z)/n+target_x; -// camera_y = m*(camera_z-target_z)/n+target_y; -// } -// } \ No newline at end of file +void monster_move(){ + for (int i = 0 ; i < monsterCount ; i++) { + if (abs(monster[i].position.x-boat.position.x) < 200 && abs(monster[i].position.y-boat.position.y) < 200) { + if (monster[i].position.x < boat.position.x) + monster[i].position.x += 0.1*((i%3)+1); + if (monster[i].position.x > boat.position.x) + monster[i].position.x -= 0.1*((i%3)+1); + if (monster[i].position.y < boat.position.y) + monster[i].position.y += 0.1*((i%3)+1); + if (monster[i].position.y > boat.position.y) + monster[i].position.y -= 0.1*((i%3)+1); + if (detect_collision(boat.bounding_box(),monster[i].bounding_box()) && monster[i].position.z != -1000) { + health -= 1; + monster[i].position.z = -1000; + } + } + } +} +void zoom_camera(int type){ + if(view==2){ + if(type==1){ + if(camera_z-10>target_z) + camera_z-=10; + } + else if(type==-1){ + camera_z+=10; + } + } +} \ No newline at end of file diff --git a/src/main.h b/src/main.h index 8ccb4b6..fa458c2 100644 --- a/src/main.h +++ b/src/main.h @@ -15,6 +15,7 @@ #include void heli_camera(float x, float y); void cannon_pos(float x, float y); +void zoom_camera(int type); struct color_t { int r; @@ -97,4 +98,9 @@ extern const color_t COLOR_CUBE9; extern const color_t COLOR_BLAST; extern const color_t COLOR_BROWN; +// Audio +void audio_init(); +void audio_play(); +void audio_close(); + #endif diff --git a/src/rock.cpp b/src/rock.cpp index c10a42d..644f0be 100644 --- a/src/rock.cpp +++ b/src/rock.cpp @@ -10,12 +10,12 @@ Rock::Rock(float x, float y, color_t color) { // Our vertices. Three consecutive floats give a 3D vertex; Three consecutive vertices give a triangle. // A cube has 6 faces with 2 triangles each, so this makes 6*2=12 triangles, and 12*3 vertices static const GLfloat vertex_buffer_data[] = { - -1.0f,-1.0f,-1.0f, // triangle 1 : begin + -1.0f,-1.0f,-1.0f, -1.0f,-1.0f, 1.0f, - -1.0f, 1.0f, 1.0f, // triangle 1 : end - 1.0f, 1.0f,-1.0f, // triangle 2 : begin + -1.0f, 1.0f, 1.0f, + 1.0f, 1.0f,-1.0f, -1.0f,-1.0f,-1.0f, - -1.0f, 1.0f,-1.0f, // triangle 2 : end + -1.0f, 1.0f,-1.0f, 1.0f,-1.0f, 1.0f, -1.0f,-1.0f,-1.0f, 1.0f,-1.0f,-1.0f, @@ -69,9 +69,7 @@ void Rock::set_position(float x, float y) { } void Rock::tick() { - this->rotation += speed; - // this->position.x -= speed; - // this->position.y -= speed; + // this->rotation += speed; } bounding_box_t Rock::bounding_box() {