You are on page 1of 21

#include <glad/glad.

h>
#include <GLFW/glfw3.h>
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>

#include <learnopengl/shader.h>
#include <learnopengl/camera.h>

#include <iostream>
#define PI 3.14159265

void framebuffer_size_callback(GLFWwindow* window, int width, int height);


//void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
void processInput(GLFWwindow *window);

// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
const float impact_radius =4 ;

int count1 = 0;
int topToggle = 1;
// camera
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
float lastX = SCR_WIDTH / 2.0f;
float lastY = SCR_HEIGHT / 2.0f;
//bool firstMouse = true;

// toggle
int toggle = 1 ;
int win = 0;

// points
int Points[] = { 0,0 };

// world space positions of our cubes


glm::vec3 cubePositions[] = {
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 12.0f, 0.0f),

};

glm::vec3 initialcubePositions[] = {
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 12.0f, 0.0f),

};

glm::vec3 ballPositions[] = {
glm::vec3(0.0f, 0.0f, 0.5f),
glm::vec3(0.0f, 12.0f, 0.5f),
};

//toope
//player1
float yaw1 = 0.0f;
float pitch1 = 0.0f;
//player2
float yaw2 = 0.0f;
float pitch2 = 0.0f;

// timing
float deltaTime = 0.0f; // time between current frame and last frame
float lastFrame = 0.0f;

int main()
{
// glfw: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // uncomment this
statement to fix compilation on OS X
#endif

// glfw window creation

// --------------------
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL",
NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
//glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);

// tell GLFW to capture our mouse

//glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);

// glad: load all OpenGL function pointers


// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
// configure global opengl state
// -----------------------------
glEnable(GL_DEPTH_TEST);

// build and compile our shader zprogram


// ------------------------------------
Shader ourShader("C:\\Users\\HP Pavilion\\Desktop\\OpenGLRoot\\External
Resouces\\vsforgame.txt", "C:\\Users\\HP Pavilion\\Desktop\\OpenGLRoot\\External
Resouces\\fsforgame.txt");

// set up vertex data (and buffer(s)) and configure vertex attributes


// ------------------------------------------------------------------

float field[] = {
-6.0f, -6.0f, -0.5f, 0.0f, 0.0f,
6.0f, -6.0f, -0.5f, 1.0f, 0.0f,
6.0f, 20.0f, -0.5f, 1.0f, 1.0f,
6.0f, 20.0f, -0.5f, 1.0f, 1.0f,
-6.0f, 20.0f, -0.5f, 0.0f, 1.0f,
-6.0f, -6.0f, -0.5f, 0.0f, 0.0f, };

float vertices[] = {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,

-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,


0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,

-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,


-0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,

0.5f, 0.5f, 0.5f, 1.0f, 0.0f,


0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,

-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,


0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,

-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,


0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f
};

float launcher[] = {
-0.025f, 0.0f, -0.025f, 0.0f, 0.0f,
0.025f, 0.0f, -0.025f, 1.0f, 0.0f,
0.025f, 0.75f, -0.025f, 1.0f, 1.0f,
0.025f, 0.75f, -0.025f, 1.0f, 1.0f,
-0.025f, 0.75f, -0.025f, 0.0f, 1.0f,
-0.025f, 0.0f, -0.025f, 0.0f, 0.0f,

-0.025f, 0.0f, 0.025f, 0.0f, 0.0f,


0.025f, 0.0f, 0.025f, 1.0f, 0.0f,
0.025f, 0.75f, 0.025f, 1.0f, 1.0f,
0.025f, 0.75f, 0.025f, 1.0f, 1.0f,
-0.025f, 0.75f, 0.025f, 0.0f, 1.0f,
-0.025f, 0.0f, 0.025f, 0.0f, 0.0f,

-0.025f, 0.75f, 0.025f, 1.0f, 0.0f,


-0.025f, 0.75f, -0.025f, 1.0f, 1.0f,
-0.025f, 0.0f, -0.025f, 0.0f, 1.0f,
-0.025f, 0.0f, -0.025f, 0.0f, 1.0f,
-0.025f, 0.0f, 0.025f, 0.0f, 0.0f,
-0.025f, 0.75f, 0.025f, 1.0f, 0.0f,

0.025f, 0.75f, 0.025f, 1.0f, 0.0f,


0.025f, 0.75f, -0.025f, 1.0f, 1.0f,
0.025f, 0.0f, -0.025f, 0.0f, 1.0f,
0.025f, 0.0f, -0.025f, 0.0f, 1.0f,
0.025f, 0.0f, 0.025f, 0.0f, 0.0f,
0.025f, 0.75f, 0.025f, 1.0f, 0.0f,

-0.025f, 0.0f, -0.025f, 0.0f, 1.0f,


0.025f, 0.0f, -0.025f, 1.0f, 1.0f,
0.025f,0.0f, 0.025f, 1.0f, 0.0f,
0.025f, 0.0f, 0.025f, 1.0f, 0.0f,
-0.025f, 0.0f, 0.025f, 0.0f, 0.0f,
-0.025f, 0.0f, -0.025f, 0.0f, 1.0f,

-0.025f, 0.75f, -0.025f, 0.0f, 1.0f,


0.025f, 0.75f, -0.025f, 1.0f, 1.0f,
0.025f, 0.75f, 0.025f, 1.0f, 0.0f,
0.025f, 0.75f, 0.025f, 1.0f, 0.0f,
-0.025f, 0.75f, 0.025f, 0.0f, 0.0f,
-0.025f, 0.75f, -0.025f, 0.0f, 1.0f
};

float missile[] = {
-0.025f, 0.0f, -0.025f, 0.0f, 0.0f,
0.025f, 0.0f, -0.025f, 1.0f, 0.0f,
0.025f, 0.025f, -0.025f, 1.0f, 1.0f,
0.025f, 0.025f, -0.025f, 1.0f, 1.0f,
-0.025f, 0.025f, -0.025f, 0.0f, 1.0f,
-0.025f, 0.0f, -0.025f, 0.0f, 0.0f,

-0.025f, 0.0f, 0.025f, 0.0f, 0.0f,


0.025f, 0.0f, 0.025f, 1.0f, 0.0f,
0.025f, 0.025f, 0.025f, 1.0f, 1.0f,
0.025f, 0.025f, 0.025f, 1.0f, 1.0f,
-0.025f, 0.025f, 0.025f, 0.0f, 1.0f,
-0.025f, 0.0f, 0.025f, 0.0f, 0.0f,

-0.025f, 0.025f, 0.025f, 1.0f, 0.0f,


-0.025f, 0.025f, -0.025f, 1.0f, 1.0f,
-0.025f, 0.0f, -0.025f, 0.0f, 1.0f,
-0.025f, 0.0f, -0.025f, 0.0f, 1.0f,
-0.025f, 0.0f, 0.025f, 0.0f, 0.0f,
-0.025f, 0.025f, 0.025f, 1.0f, 0.0f,

0.025f, 0.025f, 0.025f, 1.0f, 0.0f,


0.025f, 0.025f, -0.025f, 1.0f, 1.0f,
0.025f, 0.0f, -0.025f, 0.0f, 1.0f,
0.025f, 0.0f, -0.025f, 0.0f, 1.0f,
0.025f, 0.0f, 0.025f, 0.0f, 0.0f,
0.025f, 0.025f, 0.025f, 1.0f, 0.0f,

-0.025f, 0.0f, -0.025f, 0.0f, 1.0f,


0.025f, 0.0f, -0.025f, 1.0f, 1.0f,
0.025f,0.0f, 0.025f, 1.0f, 0.0f,
0.025f, 0.0f, 0.025f, 1.0f, 0.0f,
-0.025f, 0.0f, 0.025f, 0.0f, 0.0f,
-0.025f, 0.0f, -0.025f, 0.0f, 1.0f,

-0.025f, 0.025f, -0.025f, 0.0f, 1.0f,


0.025f, 0.025f, -0.025f, 1.0f, 1.0f,
0.025f, 0.025f, 0.025f, 1.0f, 0.0f,
0.025f, 0.025f, 0.025f, 1.0f, 0.0f,
-0.025f, 0.025f, 0.025f, 0.0f, 0.0f,
-0.025f, 0.025f, -0.025f, 0.0f, 1.0f
};

unsigned int VBO[4], VAO;


glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO[0]);
glGenBuffers(1, &VBO[1]);
glGenBuffers(1, &VBO[2]);
glGenBuffers(1, &VBO[3]);

glBindVertexArray(VAO);
/////tank
glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// texture coord attribute
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3
* sizeof(float)));
glEnableVertexAttribArray(1);
//////launcher
glBindBuffer(GL_ARRAY_BUFFER, VBO[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(launcher), launcher, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// texture coord attribute
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3
* sizeof(float)));
glEnableVertexAttribArray(1);
/////missile
glBindBuffer(GL_ARRAY_BUFFER, VBO[2]);
glBufferData(GL_ARRAY_BUFFER, sizeof(missile), missile, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// texture coord attribute
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3
* sizeof(float)));
glEnableVertexAttribArray(1);
////feild

glBindBuffer(GL_ARRAY_BUFFER, VBO[3]);
glBufferData(GL_ARRAY_BUFFER, sizeof(field), field, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// texture coord attribute
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3
* sizeof(float)));
glEnableVertexAttribArray(1);
// load and create a texture
// -------------------------
unsigned int texture1, texture2, texture3,texture4,texture5,texture6;
// texture 1
// ---------
glGenTextures(1, &texture1);
glBindTexture(GL_TEXTURE_2D, texture1);
// set the texture wrapping parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// set texture filtering parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load image, create texture and generate mipmaps
int width, height, nrChannels;
stbi_set_flip_vertically_on_load(true); // tell stb_image.h to flip loaded
texture's on the y-axis.
unsigned char *data = stbi_load("C:\\Users\\HP Pavilion\\Desktop\\army
color.jpg", &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
// texture 2
// ---------
glGenTextures(1, &texture2);
glBindTexture(GL_TEXTURE_2D, texture2);
// set the texture wrapping parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// set texture filtering parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load image, create texture and generate mipmaps
data = stbi_load("C:\\Users\\HP Pavilion\\Desktop\\toope.jpg", &width,
&height, &nrChannels, 0);
if (data)
{
// note that the awesomeface.png has transparency and thus an alpha
channel, so make sure to tell OpenGL the data type is of GL_RGBA
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);

// texture3
glGenTextures(1, &texture3);
glBindTexture(GL_TEXTURE_2D, texture3);
// set the texture wrapping parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// set texture filtering parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load image, create texture and generate mipmaps

stbi_set_flip_vertically_on_load(true); // tell stb_image.h to flip loaded


texture's on the y-axis.
data = stbi_load("C:\\Users\\HP Pavilion\\Desktop\\missile.jpg", &width,
&height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);

//texture 4

glGenTextures(1, &texture4);
glBindTexture(GL_TEXTURE_2D, texture4);
// set the texture wrapping parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// set texture filtering parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load image, create texture and generate mipmaps

stbi_set_flip_vertically_on_load(true); // tell stb_image.h to flip loaded


texture's on the y-axis.
data = stbi_load("C:\\Users\\HP Pavilion\\Desktop\\ground.jpg", &width,
&height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);

//texture5
glGenTextures(1, &texture5);
glBindTexture(GL_TEXTURE_2D, texture5);
// set the texture wrapping parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// set texture filtering parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load image, create texture and generate mipmaps

//stbi_set_flip_vertically_on_load(true); // tell stb_image.h to flip loaded


texture's on the y-axis.
data = stbi_load("C:\\Users\\HP Pavilion\\Desktop\\1 WINS.png", &width,
&height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);

//texture6

glGenTextures(1, &texture6);
glBindTexture(GL_TEXTURE_2D, texture6);
// set the texture wrapping parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// set texture filtering parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load image, create texture and generate mipmaps
//stbi_set_flip_vertically_on_load(true); // tell stb_image.h to flip loaded
texture's on the y-axis.
data = stbi_load("C:\\Users\\HP Pavilion\\Desktop\\2 WINS.png", &width,
&height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);

// tell opengl for each sampler to which texture unit it belongs to (only has
to be done once)
//
-----------------------------------------------------------------------------------
--------
ourShader.use();
ourShader.setInt("texture1", 0);
ourShader.setInt("texture2", 1);
ourShader.setInt("texture3", 2);
ourShader.setInt("texture4", 3);
ourShader.setInt("texture5", 4);
ourShader.setInt("texture6", 5);

/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////

float currentFrame = glfwGetTime();


deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;

// input
// -----
processInput(window);

// render
// ------
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// bind textures on corresponding texture units


glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, texture3);
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, texture4);
glActiveTexture(GL_TEXTURE4);
glBindTexture(GL_TEXTURE_2D, texture5);
glActiveTexture(GL_TEXTURE5);
glBindTexture(GL_TEXTURE_2D, texture6);
// activate shader
ourShader.use();

// pass projection matrix to shader (note that in this case it could change
every frame)
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom),
(float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
ourShader.setMat4("projection", projection);

// camera/view transformation
glm::mat4 view = camera.GetViewMatrix();
camera.viewsetter(toggle, cubePositions[0], cubePositions[1]);
ourShader.setMat4("view", view);

// render boxes
glBindVertexArray(VAO);
for (unsigned int i = 0; i < 2; i++)
{

if (i == 0) {
glm::mat4 model = glm::mat4(1.0f); // make sure to initialize
matrix to identity matrix first
model = glm::translate(model, cubePositions[i]);
model = glm::rotate(model, glm::radians(0.0f), glm::vec3(1.0f,
0.0f, 0.0f));
ourShader.setMat4("model", model);

}
if (i == 1) {
glm::mat4 model1 = glm::mat4(1.0f);
model1 = glm::translate(model1, cubePositions[i]);
model1 = glm::rotate(model1, glm::radians(180.0f),
glm::vec3(1.0f, 0.0f, 0.0f));
ourShader.setMat4("model", model1);
}
ourShader.setInt("id", 1);
glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float),
(void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float),
(void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glDrawArrays(GL_TRIANGLES, 0, 36);
ourShader.setInt("id", 2);
glBindBuffer(GL_ARRAY_BUFFER, VBO[1]);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float),
(void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float),
(void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved
etc.)
//
-------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();

int count = 0;
int ball1 = 0;
int ball2 = 0;
int ballspeed = 1;
float g = 9.80;

glm::vec3 ball1velocity = glm::vec3(0.0f, 0.0f, 0.0f);


glm::vec3 ball2velocity = glm::vec3(0.0f, 0.0f, 0.0f);
// -----------
while (!glfwWindowShouldClose(window))
{
// per-frame time logic
// --------------------
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;

// input
// -----
processInput(window);

// render
// ------
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// bind textures on corresponding texture units


glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, texture3);
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, texture4);
glActiveTexture(GL_TEXTURE4);
glBindTexture(GL_TEXTURE_2D, texture5);
glActiveTexture(GL_TEXTURE5);
glBindTexture(GL_TEXTURE_2D, texture6);

// activate shader
ourShader.use();

// pass projection matrix to shader (note that in this case it could


change every frame)
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom),
(float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
ourShader.setMat4("projection", projection);

// camera/view transformation

glm::mat4 view = camera.GetViewMatrix();


ourShader.setMat4("view", view);

// render boxes
glBindVertexArray(VAO);

ourShader.setInt("id", 4);

glm::mat4 model5 = glm::mat4(1.0f); // make sure to initialize matrix


to identity matrix first
model5 = glm::translate(model5, glm::vec3(0.0f, 0.0f, 0.0f));
model5 = glm::rotate(model5, glm::radians(0.0f), glm::vec3(1.0f, 0.0f,
0.0f));
ourShader.setMat4("model", model5);
glBindBuffer(GL_ARRAY_BUFFER, VBO[3]);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float),
(void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float),
(void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glDrawArrays(GL_TRIANGLES, 0, 36);

for (unsigned int i = 0; i < 2; i++)


{

if (i == 0) {

ourShader.setInt("id", 1);

glm::mat4 model = glm::mat4(1.0f); // make sure to


initialize matrix to identity matrix first
model = glm::translate(model, cubePositions[i]);
model = glm::rotate(model, glm::radians(0.0f),
glm::vec3(1.0f, 0.0f, 0.0f));
ourShader.setMat4("model", model);
glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 *
sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 *
sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glDrawArrays(GL_TRIANGLES, 0, 36);

ourShader.setInt("id", 2);
glm::mat4 modelt = glm::mat4(1.0f);// make sure to
initialize matrix to identity matrix first
modelt = glm::translate(modelt, glm::vec3(cubePositions[i]
[0], cubePositions[i][1], cubePositions[i][2]+0.5f));
modelt = glm::rotate(modelt, glm::radians(float(yaw1)),
glm::vec3(0.0f, 0.0f, 1.0f));
modelt = glm::rotate(modelt, glm::radians(float(pitch1)),
glm::vec3(1.0f, 0.0f, 0.0f));
ourShader.setMat4("model", modelt);

glBindBuffer(GL_ARRAY_BUFFER, VBO[1]);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 *
sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 *
sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glDrawArrays(GL_TRIANGLES, 0, 36);

}
if (i == 1) {
ourShader.setInt("id", 1);

glm::mat4 model2 = glm::mat4(1.0f); // make sure to


initialize matrix to identity matrix first
model2 = glm::translate(model2, cubePositions[i]);
model2 = glm::rotate(model2, glm::radians(180.0f),
glm::vec3(1.0f, 0.0f, 0.0f));
ourShader.setMat4("model", model2);
glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 *
sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 *
sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glDrawArrays(GL_TRIANGLES, 0, 36);

ourShader.setInt("id", 2);
glm::mat4 modelt1 = glm::mat4(1.0f);
modelt1 = glm::translate(modelt1,
glm::vec3(cubePositions[i][0], cubePositions[i][1], cubePositions[i][2] + 0.5f));
modelt1 = glm::rotate(modelt1, glm::radians(float(yaw2)),
glm::vec3(0.0f, 0.0f, 1.0f));
modelt1 = glm::rotate(modelt1, glm::radians(float(pitch2)),
glm::vec3(1.0f, 0.0f, 0.0f));
modelt1 = glm::rotate(modelt1, glm::radians(180.0f),
glm::vec3(1.0f, 0.0f, 0.0f));

ourShader.setMat4("model", modelt1);

glBindBuffer(GL_ARRAY_BUFFER, VBO[1]);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 *
sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 *
sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glDrawArrays(GL_TRIANGLES, 0, 36);
}

}
//std::cout << Points[0];
//std::cout << Points[1];
//std::cout << "\n\0";
if (ball1)
{

ourShader.setInt("id", 3);
glm::mat4 model3 = glm::mat4(1.0f); // make sure to initialize matrix
to identity matrix first
model3 = glm::translate(model3, ballPositions[0]);
model3 = glm::rotate(model3, glm::radians(0.0f), glm::vec3(1.0f,
0.0f, 0.0f));
ourShader.setMat4("model", model3);
glBindBuffer(GL_ARRAY_BUFFER, VBO[2]);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 *
sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 *
sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glDrawArrays(GL_TRIANGLES, 0, 36);
ballPositions[0][0] += ball1velocity[0] * deltaTime;
ballPositions[0][1] += ball1velocity[1] * deltaTime;
ballPositions[0][2] += ball1velocity[2] * deltaTime;
ball1velocity[2] -= g*deltaTime ;

//std::cout << ballPositions[0][2];


//std::cout <<"\n\0";
if (ballPositions[0][2] < -0.5)
{
camera.viewsetter(toggle, cubePositions[0],
cubePositions[1]);
ball1 = 0;
if (sqrt(pow((ballPositions[0][0] - cubePositions[1][0]),
2.0) + pow((ballPositions[0][1] - cubePositions[1][1]), 2.0))< impact_radius)
{
Points[0] += 1;
std::cout << Points[0];
std::cout << Points[1];
std::cout << sqrt(pow((ballPositions[0][0] -
cubePositions[1][0]), 2.0) + pow((ballPositions[0][1] - cubePositions[1][1]),
2.0));
std::cout << '\n\0';
if (Points[0] >= 5)
{
win = 1;
std:: cout << win;

}
}

ballPositions[0] = initialcubePositions[0];

if (ball2) {
ourShader.setInt("id", 3);
glm::mat4 model4 = glm::mat4(1.0f); // make sure to initialize
matrix to identity matrix first
model4 = glm::translate(model4, ballPositions[1]);
model4 = glm::rotate(model4, glm::radians(180.0f),
glm::vec3(1.0f, 0.0f, 0.0f));
ourShader.setMat4("model", model4);
glBindBuffer(GL_ARRAY_BUFFER, VBO[2]);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 *
sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 *
sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glDrawArrays(GL_TRIANGLES, 0, 36);
ballPositions[1][0] += ball2velocity[0] * deltaTime;
ballPositions[1][1] += ball2velocity[1] * deltaTime;
ballPositions[1][2] += ball2velocity[2] * deltaTime;
ball2velocity[2] -= g * deltaTime;

//std::cout << ballPositions[1][2];

//std::cout << "\n\0";


if (ballPositions[1][2] < -0.5)
{
camera.viewsetter(toggle, cubePositions[0],
cubePositions[1]);
ball2 = 0;
if (sqrt(pow((ballPositions[1][0] - cubePositions[0][0]),
2.0) + pow((ballPositions[1][1] - cubePositions[0][1]), 2.0))< impact_radius)
{
Points[1] += 1;
std::cout << Points[0];
std::cout << Points[1];
std::cout << sqrt(pow((ballPositions[0][0] -
cubePositions[1][0]), 2.0) + pow((ballPositions[0][1] - cubePositions[1][1]),
2.0));
std::cout << '\n\0';
if (Points[1] >= 5)
{
win = 2;
std :: cout << win;
}
}

ballPositions[1] = initialcubePositions[1];

if (win == 1)
{
ourShader.setInt("id", 5);
glm::mat4 model6 = glm::mat4(1.0f); // make sure to initialize
matrix to identity matrix first
model6 = glm::translate(model6, glm::vec3(0.0f, 6.0f, 2.0f));
model6 = glm::rotate(model6, glm::radians(0.0f), glm::vec3(1.0f,
0.0f, 0.0f));
ourShader.setMat4("model", model6);
glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 *
sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 *
sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
if (win == 2)
{

ourShader.setInt("id", 6);
glm::mat4 model7 = glm::mat4(1.0f); // make sure to initialize
matrix to identity matrix first
model7 = glm::translate(model7, glm::vec3(0.0f, 6.0f, 2.0f));
model7 = glm::rotate(model7, glm::radians(0.0f), glm::vec3(1.0f,
0.0f, 0.0f));
ourShader.setMat4("model", model7);
glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 *
sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 *
sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glDrawArrays(GL_TRIANGLES, 0, 36);
}

// glfw: swap buffers and poll IO events (keys pressed/released, mouse


moved etc.)
//
-------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
if ((glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS) && !(win) && !(ball1
|| ball2))
{
count++;
if (count > 150) {

ballspeed = 20;
if (toggle == int(1))
{
ball1velocity = glm::vec3(-cos(PI*pitch1 /
180.0)*sin(PI*yaw1 / 180.0)*ballspeed, cos(PI*pitch1 / 180.0)*cos(PI*yaw1 /
180.0)*ballspeed, sin(PI*pitch1/180.0)*ballspeed);

ball1 = 1;
toggle = int(2);

}
else if (toggle == int(2))
{
ball2velocity = glm::vec3(cos(-PI*pitch2 /
180.0)*sin(PI*yaw2 / 180.0)*ballspeed, -cos(-PI*pitch2 / 180.0)*cos(PI*yaw2 /
180.0)*ballspeed, sin(-PI*pitch2/ 180.0)*ballspeed);

ball2 = 1;
toggle = int(1);

//std::cout << toggle;


count = 0;
}

// optional: de-allocate all resources once they've outlived their purpose:


// ------------------------------------------------------------------------
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO[0]);
glDeleteBuffers(1, &VBO[1]);
glDeleteBuffers(1, &VBO[2]);
glDeleteBuffers(1, &VBO[3]);
// glfw: terminate, clearing all previously allocated GLFW resources.
// ------------------------------------------------------------------
glfwTerminate();
return 0;
}

// process all input: query GLFW whether relevant keys are pressed/released this
frame and react accordingly
//
-----------------------------------------------------------------------------------
----------------------
void processInput(GLFWwindow *window)
{

float speedt = 5.0f;


float speed = 5.0f;
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);

if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)


camera.ProcessKeyboard(FORWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
camera.ProcessKeyboard(BACKWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
camera.ProcessKeyboard(LEFT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
camera.ProcessKeyboard(RIGHT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS)
camera.ProcessKeyboard(TURN_LEFT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS)
camera.ProcessKeyboard(TURN_RIGHT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_Z) == GLFW_PRESS)
camera.ProcessKeyboard(LOOK_UP, deltaTime);
if (glfwGetKey(window, GLFW_KEY_X) == GLFW_PRESS)
camera.ProcessKeyboard(LOOK_DOWN, deltaTime);
if (glfwGetKey(window, GLFW_KEY_V) == GLFW_PRESS)
{
count1++;
if (count1 > 150) {
if (topToggle)
{
camera.ProcessKeyboard(TOP_VIEW, deltaTime);
topToggle = 0;
}
else
{
camera.viewsetter(toggle, cubePositions[0],
cubePositions[1]);
topToggle = 1;
}
count1 = 0;
}
}
if ((glfwGetKey(window, GLFW_KEY_Y) == GLFW_PRESS) && (!(win)))
{
float velocity = speedt * deltaTime;
if (toggle == 1)
{
pitch1 += velocity;
}
if (toggle == 2)
{
pitch2 -= velocity;
}
}
if ((glfwGetKey(window, GLFW_KEY_H) == GLFW_PRESS) && (!(win)))
{
float velocity = speedt * deltaTime;
if (toggle == 1)
{
pitch1 -= velocity;
}
if (toggle == 2)
{
pitch2 += velocity;
}

}
if ((glfwGetKey(window, GLFW_KEY_J) == GLFW_PRESS) && (!(win)))
{
float velocity = speedt * deltaTime;
if (toggle == 1)
{
yaw1 -= velocity;
}
if (toggle == 2)
{
yaw2 -= velocity;
}
}

if ((glfwGetKey(window, GLFW_KEY_G) == GLFW_PRESS) && (!(win)))


{
float velocity = speedt * deltaTime;
if (toggle == 1)
{
yaw1 += velocity;
}
if (toggle == 2)
{
yaw2 += velocity;
}
}

if ((glfwGetKey(window, GLFW_KEY_U) == GLFW_PRESS) && (!(win)))


{
float velocity = speed * deltaTime;
if (toggle == 1)
{
cubePositions[0] += glm::vec3(0.0f, velocity, 0.0f);
if ((cubePositions[0][1] - initialcubePositions[0][1]) > 3.0f) {
cubePositions[0] -= glm::vec3(0.0f, 2*velocity, 0.0f);
}
}
if (toggle == 2)
{
cubePositions[1] += glm::vec3(0.0f, -velocity, 0.0f);
if ((initialcubePositions[1][1]-cubePositions[1][1] ) > 3.0f) {
cubePositions[1] += glm::vec3(0.0f, 2*velocity, 0.0f);
}
}
}

if ((glfwGetKey(window, GLFW_KEY_N) == GLFW_PRESS) && (!(win)))


{
float velocity = speed * deltaTime;
if (toggle == 1)
{
cubePositions[0] -= glm::vec3(0.0f, velocity, 0.0f);
if ((initialcubePositions[0][1] - cubePositions[0][1]) > 3.0f) {
cubePositions[0] += glm::vec3(0.0f, 2*velocity, 0.0f);
}
}
if (toggle == 2)
{
cubePositions[1] += glm::vec3(0.0f, velocity, 0.0f);
if ((cubePositions[1][1] - initialcubePositions[1][1]) >
3.0f) {
cubePositions[1] += glm::vec3(0.0f, -2*velocity,
0.0f);
}
}
}

if ((glfwGetKey(window, GLFW_KEY_M) == GLFW_PRESS) && (!(win)))


{
float velocity = speed * deltaTime;
if (toggle == 1)
{
cubePositions[0] += glm::vec3(velocity, 0.0f, 0.0f);
if ((cubePositions[0][0] - initialcubePositions[0][0]) > 3.0f) {
cubePositions[0] -= glm::vec3(2*velocity, 0.0f, 0.0f);
}
}
if (toggle == 2)
{
cubePositions[1] += glm::vec3(-velocity, 0.0f, 0.0f);
if (-(cubePositions[1][0] - initialcubePositions[1][0]) > 3.0f) {
cubePositions[1] += glm::vec3(2 * velocity, 0.0f, 0.0f);
}
}
}

if ((glfwGetKey(window, GLFW_KEY_B) == GLFW_PRESS) && (!(win)))


{
float velocity = speed * deltaTime;
if (toggle == 1)
{
cubePositions[0] -= glm::vec3(velocity, 0.0f, 0.0f);
if (-(cubePositions[0][0] - initialcubePositions[0][0]) > 3.0f) {
cubePositions[0] += glm::vec3(2 * velocity, 0.0f, 0.0f);
}
}
if (toggle == 2)
{
cubePositions[1] -= glm::vec3(-velocity, 0.0f, 0.0f);
if ((cubePositions[1][0] - initialcubePositions[1][0]) > 3.0f) {
cubePositions[1] -= glm::vec3(2 * velocity, 0.0f, 0.0f);
}

}
}

// glfw: whenever the window size changed (by OS or user resize) this callback
function executes
//
-----------------------------------------------------------------------------------
----------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
// make sure the viewport matches the new window dimensions; note that width
and
// height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height);
}

// glfw: whenever the mouse moves, this callback is called


// -------------------------------------------------------
/*void mouse_callback(GLFWwindow* window, double xpos, double ypos)
{
if (firstMouse)
{
lastX = xpos;
lastY = ypos;
firstMouse = false;
}

float xoffset = xpos - lastX;


float yoffset = lastY - ypos; // reversed since y-coordinates go from bottom
to top
lastX = xpos;
lastY = ypos;

camera.ProcessMouseMovement(xoffset, yoffset);
}*/

// glfw: whenever the mouse scroll wheel scrolls, this callback is called
// ----------------------------------------------------------------------
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
camera.ProcessMouseScroll(yoffset);
}
//"C:\\Users\\HP Pavilion\\Desktop\\OpenGLRoot\\External
Resouces\\textureshadervs.txt", "C:\\Users\\HP
Pavilion\\Desktop\\OpenGLRoot\\External Resouces\\textureshaderfs.txt"
//"C:\\Users\\HP Pavilion\\Desktop\\container.jpg"
//"C:\\Users\\HP Pavilion\\Pictures\\Saved Pictures\\images.jpg"
//#define STB_IMAGE_IMPLEMENTATION

You might also like