Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
23 kB
3
Indexable
Never
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <stb_image.h>
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"

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

#include <../src/shader_s.h>
#include <../src/camera.h>
#include <../src/model.h>
#include <../src/Object.h>

#include <iostream>
#include <vector>
#include <filesystem>
#include "../src/GraphNode.h"
#include <glm/gtc/matrix_access.hpp>
#include <iostream>
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/string_cast.hpp>
#include <GLFW/glfw3.h>


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 mouse_button_callback(GLFWwindow* window, int button, int action, int mods);
void processInput(GLFWwindow* window);
unsigned int loadTexture(const char* path);
unsigned int loadCubemap(std::vector<std::string> faces);
void updateGun();

// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;

glm::vec3 deltaPosition = glm::vec3(0.0f, 0.0f, -1.0f);

// camera
Camera camera(glm::vec3(0.0f, 1.0f, 0.0f));
float lastX = (float)SCR_WIDTH / 2.0;
float lastY = (float)SCR_HEIGHT / 2.0;
bool firstMouse = true;

bool isKeyDown = false;

// timing
float deltaTime = 0.0f;
float lastFrame = 0.0f;

glm::vec3 viewVector = glm::vec3(1.0f); 

Object gunObject;
glm::vec4 gunColor = glm::vec4(0.0, 0.0, 0.0, 1.0);

glm::mat4* transformGunGraphNode = new glm::mat4(1);
glm::mat4* transformCameraGraphNode = new glm::mat4(1);
glm::mat4* transformBulletGraphNode = new glm::mat4(1);
GraphNode* cameraGraphNode = new GraphNode();
GraphNode* bulletGraphNode = new GraphNode();


int redBullets = 1000;
int blackBullets = 1000;
int greenBullets = 1000;
int blueBullets = 1000;

int main()
{
	const char* glsl_version = "#version 430";

	// 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);
#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);
	glfwSetMouseButtonCallback(window, mouse_button_callback);

	// tell GLFW to capture our mouse
	glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
	//glfwSetInputMode(window, GLFW_STICKY_MOUSE_BUTTONS, GLFW_TRUE);
	glfwSetInputMode(window, GLFW_STICKY_KEYS, GLFW_TRUE);

	// glad: load all OpenGL function pointers
	// ---------------------------------------
	if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
	{
		std::cout << "Failed to initialize GLAD" << std::endl;
		return -1;
	}


	// IMGUI
	//IMGUI_CHECKVERSION();
	ImGui::CreateContext();
	ImGuiIO& io = ImGui::GetIO(); (void)io;

	ImGui_ImplGlfw_InitForOpenGL(window, true);
	ImGui_ImplOpenGL3_Init(glsl_version);

	ImGui::StyleColorsDark();



	// configure global opengl state
	// -----------------------------
	glEnable(GL_DEPTH_TEST);

	// build and compile shaders
	// -------------------------
	Shader shaderRefract("res/shaders/cubemaps.vs", "res/shaders/refract.fs");
	Shader shaderReflect("res/shaders/cubemaps.vs", "res/shaders/reflect.fs");
	Shader skyboxShader("res/shaders/skybox.vs", "res/shaders/skybox.fs");

	// set up vertex data (and buffer(s)) and configure vertex attributes
	// ------------------------------------------------------------------
	float cubeVertices[] = {
		// positions          // normals
		-0.5f, -0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
		 0.5f, -0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
		 0.5f,  0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
		 0.5f,  0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
		-0.5f,  0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
		-0.5f, -0.5f, -0.5f,  0.0f,  0.0f, -1.0f,

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

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

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

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

		-0.5f,  0.5f, -0.5f,  0.0f,  1.0f,  0.0f,
		 0.5f,  0.5f, -0.5f,  0.0f,  1.0f,  0.0f,
		 0.5f,  0.5f,  0.5f,  0.0f,  1.0f,  0.0f,
		 0.5f,  0.5f,  0.5f,  0.0f,  1.0f,  0.0f,
		-0.5f,  0.5f,  0.5f,  0.0f,  1.0f,  0.0f,
		-0.5f,  0.5f, -0.5f,  0.0f,  1.0f,  0.0f
	};
	float skyboxVertices[] = {
		// positions          
		-1.0f,  1.0f, -1.0f,
		-1.0f, -1.0f, -1.0f,
		 1.0f, -1.0f, -1.0f,
		 1.0f, -1.0f, -1.0f,
		 1.0f,  1.0f, -1.0f,
		-1.0f,  1.0f, -1.0f,

		-1.0f, -1.0f,  1.0f,
		-1.0f, -1.0f, -1.0f,
		-1.0f,  1.0f, -1.0f,
		-1.0f,  1.0f, -1.0f,
		-1.0f,  1.0f,  1.0f,
		-1.0f, -1.0f,  1.0f,

		 1.0f, -1.0f, -1.0f,
		 1.0f, -1.0f,  1.0f,
		 1.0f,  1.0f,  1.0f,
		 1.0f,  1.0f,  1.0f,
		 1.0f,  1.0f, -1.0f,
		 1.0f, -1.0f, -1.0f,

		-1.0f, -1.0f,  1.0f,
		-1.0f,  1.0f,  1.0f,
		 1.0f,  1.0f,  1.0f,
		 1.0f,  1.0f,  1.0f,
		 1.0f, -1.0f,  1.0f,
		-1.0f, -1.0f,  1.0f,

		-1.0f,  1.0f, -1.0f,
		 1.0f,  1.0f, -1.0f,
		 1.0f,  1.0f,  1.0f,
		 1.0f,  1.0f,  1.0f,
		-1.0f,  1.0f,  1.0f,
		-1.0f,  1.0f, -1.0f,

		-1.0f, -1.0f, -1.0f,
		-1.0f, -1.0f,  1.0f,
		 1.0f, -1.0f, -1.0f,
		 1.0f, -1.0f, -1.0f,
		-1.0f, -1.0f,  1.0f,
		 1.0f, -1.0f,  1.0f
	};

	GraphNode* World = new GraphNode();

	Shader loadedModelShader("res/shaders/model_loading.vs", "res/shaders/model_loading.fs");

	Model* planeModel = new Model(("res/models/plane/planeBig.obj"));
	Model* gunModel = new Model(("res/models/GunModel/M9.obj"));
	Model* bulletModel = new Model(("res/models/Bullet/bullet.obj"));
	

	gunModel->SetShader(&loadedModelShader);
	bulletModel->SetShader(&loadedModelShader);
	planeModel->SetShader(&shaderReflect);

	GraphNode* planeGraphNode = new GraphNode(planeModel);
	glm::mat4* transformPlaneGraphNode = new glm::mat4(1);
	*transformPlaneGraphNode = glm::translate(*(transformPlaneGraphNode), glm::vec3(0.0f, 0.0f, 0.0f));
	*transformPlaneGraphNode = glm::scale(*transformPlaneGraphNode, glm::vec3(1.15f, 1.15f, 1.15f));

	GraphNode* gunGraphNode = new GraphNode(gunModel);
	*transformGunGraphNode = glm::translate(*(transformGunGraphNode), glm::vec3(0.0f, 8.0f, 0.0f));
	*transformGunGraphNode = glm::scale(*transformGunGraphNode, glm::vec3(0.5f, 0.5f, 0.5f));
	*transformGunGraphNode = glm::rotate(*transformGunGraphNode,glm::radians(-90.0f), glm::vec3(1, 0, 0));

	*transformCameraGraphNode = glm::translate(*(transformCameraGraphNode), camera.Position);

	bulletGraphNode->SetModel(bulletModel);
	*transformBulletGraphNode = glm::translate(*(transformGunGraphNode), glm::vec3(0.0f, 0.0f, -10.0f));
	*transformBulletGraphNode = glm::scale(*transformBulletGraphNode, glm::vec3(0.1f, 0.1f, 0.1f));

	GraphNode* bulletGraphNode1 = new GraphNode();
	*transformBulletGraphNode = glm::translate(*(transformBulletGraphNode), glm::vec3(0.0f, 0.0f, -10.0f));
	*transformBulletGraphNode = glm::scale(*transformBulletGraphNode, glm::vec3(0.1f, 0.1f, 0.1f));

	gunGraphNode->SetTransform(transformGunGraphNode);
	bulletGraphNode->SetTransform(transformBulletGraphNode);
	cameraGraphNode->SetTransform(transformCameraGraphNode);

	cameraGraphNode->AddChild(gunGraphNode);
	gunGraphNode->AddChild(bulletGraphNode1);
	//World->AddChild(planeGraphNode);
	World->AddChild(cameraGraphNode);

	//gunObject.model = nanosuitModel;
	//gunObject.shader = &loadedModelShader;
	//gunObject.transform.scale = glm::vec3(3);
	//gunObject.shader->use();
	//gunObject.shader->setVec4("color", gunColor);



	// cube VAO
	unsigned int cubeVAO, cubeVBO;
	glGenVertexArrays(1, &cubeVAO);
	glGenBuffers(1, &cubeVBO);
	glBindVertexArray(cubeVAO);
	glBindBuffer(GL_ARRAY_BUFFER, cubeVBO);
	glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVertices), &cubeVertices, GL_STATIC_DRAW);
	glEnableVertexAttribArray(0);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
	glEnableVertexAttribArray(1);
	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
	// skybox VAO
	unsigned int skyboxVAO, skyboxVBO;
	glGenVertexArrays(1, &skyboxVAO);
	glGenBuffers(1, &skyboxVBO);
	glBindVertexArray(skyboxVAO);
	glBindBuffer(GL_ARRAY_BUFFER, skyboxVBO);
	glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), &skyboxVertices, GL_STATIC_DRAW);
	glEnableVertexAttribArray(0);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);

	// load textures
	// -------------
	std::vector<std::string> faces
	{
		 ("res/textures/skybox/right.jpg"),
		 ("res/textures/skybox/left.jpg"),
		 ("res/textures/skybox/top.jpg"),
		 ("res/textures/skybox/bottom.jpg"),
		 ("res/textures/skybox/front.jpg"),
		 ("res/textures/skybox/back.jpg"),
	};
	unsigned int cubemapTexture = loadCubemap(faces);

	// shader configuration
	// --------------------
	shaderRefract.use();
	shaderRefract.setInt("skybox", 0);

	shaderReflect.use();
	shaderReflect.setInt("skybox", 0);

	skyboxShader.use();
	skyboxShader.setInt("skybox", 0);

	// render loop
	// -----------
	while (!glfwWindowShouldClose(window))
	{
		// per-frame time logic
		// --------------------
		float currentFrame = glfwGetTime();
		deltaTime = currentFrame - lastFrame;
		lastFrame = currentFrame;

		glfwPollEvents();

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



		ImGui_ImplOpenGL3_NewFrame();
		ImGui_ImplGlfw_NewFrame();
		ImGui::NewFrame();
		{
			ImGui::Begin("", 0, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize);
			ImGui::Text("BLACK BULLETS = %d", blackBullets);
			ImGui::TextColored(ImVec4(1.0, 0.0, 0.0, 1.0), "RED BULLETS = %d", redBullets);
			ImGui::TextColored(ImVec4(0.0, 1.0, 0.0, 1.0), "GREEN BULLETS = %d", greenBullets);
			ImGui::TextColored(ImVec4(0.0, 0.0, 1.0, 1.0), "BLUE BULLETS = %d", blueBullets);

			ImGui::End();
		}

		// render
		// ------
		int display_w, display_h;
		glfwMakeContextCurrent(window);
		glfwGetFramebufferSize(window, &display_w, &display_h);
		glViewport(0, 0, display_w, display_h);
		glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		// draw scene as normal

		glm::mat4 model = glm::mat4(1.0f);
		glm::mat4 view = camera.GetViewMatrix();
		glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 1000.0f);


		//gunObject.shader->use();
		//gunObject.shader->setMat4("model", model);
		//gunObject.shader->setMat4("projection", projection);
		//gunObject.shader->setMat4("view", glm::lookAt(gunObject.transform.position, camera.Position, glm::vec3(1, 0, 0)));
		//gunObject.shader->setVec4("color", gunColor);
		//gunObject.Draw();
	
		//z tym można ruszać modelem 






		World->Update();

		loadedModelShader.use();
		loadedModelShader.setMat4("model", model);
		loadedModelShader.setMat4("projection", projection);
		//z tym można kręcić 
		//loadedModelShader.setMat4("view", view);
		loadedModelShader.setMat4("view", glm::lookAt(glm::vec3(glm::column(*gunGraphNode->GetWorldTransform(), 3)) + glm::vec3(-20, -10, 0.0), camera.Position, glm::vec3(1, 0, 0)));
		loadedModelShader.setVec4("color", gunColor);





		if (isKeyDown) {
			if (gunColor == glm::vec4(1.0, 0.0, 0.0, 1.0)) {
				if (redBullets > 0) {
					std::cout << "red" << std::endl;
					bulletGraphNode->Draw();
					redBullets -= 1;
				}
			}
			if (gunColor == glm::vec4(0.0, 1.0, 0.0, 1.0)) {
				if (greenBullets > 0) {
					std::cout << "green" << std::endl;
					greenBullets -= 1;
					bulletGraphNode->Draw();

				}
			}
			if (gunColor == glm::vec4(0.0, 0.0, 1.0, 1.0)) {
				if (blueBullets > 0) {
					std::cout << "blue" << std::endl;
					blueBullets -= 1;

					bulletGraphNode->Draw();

				}
			}
			if (gunColor == glm::vec4(0.0, 0.0, 0.0, 1.0)) {
				if (blackBullets > 0) {
					std::cout << "black" << std::endl;

					bulletGraphNode->Draw();

					blackBullets -= 1;
				}
			}
			std::cout << glm::to_string(*bulletGraphNode->GetTransform()) << std::endl;
			isKeyDown = false;
		}

		World->Draw();



		shaderRefract.use();
		shaderRefract.setMat4("model", model);
		shaderRefract.setMat4("view", view);
		shaderRefract.setMat4("projection", projection);
		shaderRefract.setVec3("cameraPos", camera.Position);

		shaderReflect.use();
		shaderReflect.setMat4("model", model);
		shaderReflect.setMat4("view", view);
		shaderReflect.setMat4("projection", projection);
		shaderReflect.setVec3("cameraPos", camera.Position);
		// cubes
		glBindVertexArray(cubeVAO);
		glActiveTexture(GL_TEXTURE0);
		glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture);
		glDrawArrays(GL_TRIANGLES, 0, 36);
		glBindVertexArray(0);


		//std::cout << glm::to_string(cameraGraphNode->GetTransform()) << std::endl;
			//nanosuitGraphNode->Translate(viewVector);
		




		// draw skybox as last
		glDepthFunc(GL_LEQUAL);  // change depth function so depth test passes when values are equal to depth buffer's content
		skyboxShader.use();
		view = glm::mat4(glm::mat3(camera.GetViewMatrix())); // remove translation from the view matrix
		skyboxShader.setMat4("view", view);
		skyboxShader.setMat4("projection", projection);
		// skybox cube
		glBindVertexArray(skyboxVAO);
		glActiveTexture(GL_TEXTURE0);
		glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture);
		glDrawArrays(GL_TRIANGLES, 0, 36);
		glBindVertexArray(0);
		glDepthFunc(GL_LESS); // set depth function back to default

		// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
		// -------------------------------------------------------------------------------

		ImGui::Render();
		ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
		glfwMakeContextCurrent(window);
		glfwSwapBuffers(window);
	}

	// optional: de-allocate all resources once they've outlived their purpose:
	// ------------------------------------------------------------------------
	glDeleteVertexArrays(1, &cubeVAO);
	glDeleteVertexArrays(1, &skyboxVAO);
	glDeleteBuffers(1, &cubeVBO);
	glDeleteBuffers(1, &skyboxVAO);

	ImGui_ImplOpenGL3_Shutdown();
	ImGui_ImplGlfw_Shutdown();
	ImGui::DestroyContext();

		glfwDestroyWindow(window);

	glfwTerminate();
	return 0;
}

// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow* window)
{
	if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS) {
		isKeyDown = true;
	}
	if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_RELEASE) {
		isKeyDown = false;
		bulletGraphNode->Scale(glm::vec3(0.0f));


	}
	if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
		glfwSetWindowShouldClose(window, true);

	if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
		camera.ProcessKeyboard(FORWARD, deltaTime * 10); 
	}
		
	if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
		camera.ProcessKeyboard(BACKWARD, deltaTime * 10);
	}
	 if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
		camera.ProcessKeyboard(LEFT, deltaTime * 10); 
	}
	 if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
		camera.ProcessKeyboard(RIGHT, deltaTime * 10); 
	}
	 if (glfwGetKey(window, GLFW_KEY_1) == GLFW_PRESS)
		 gunColor = glm::vec4(1.0, 0.0, 0.0, 1.0);
	 if (glfwGetKey(window, GLFW_KEY_2) == GLFW_PRESS)
		 gunColor = glm::vec4(0.0, 1.0, 0.0, 1.0);
	 if (glfwGetKey(window, GLFW_KEY_3) == GLFW_PRESS)
		 gunColor = glm::vec4(0.0, 0.0, 1.0, 1.0);
	 if (glfwGetKey(window, GLFW_KEY_4) == GLFW_PRESS)
		 gunColor = glm::vec4(0.0, 0.0, 0.0, 1.0);
	 if (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS) {
		 if (gunColor == glm::vec4(1.0, 0.0, 0.0, 1.0)) redBullets = 10;
		 if (gunColor == glm::vec4(0.0, 1.0, 0.0, 1.0)) greenBullets = 10;
		 if (gunColor == glm::vec4(0.0, 0.0, 1.0, 1.0)) blueBullets = 10;
		 if (gunColor == glm::vec4(0.0, 0.0, 0.0, 1.0)) blackBullets = 10;
	 }
	updateGun();
}

void updateGun()
{
	cameraGraphNode->Translate(camera.Position - glm::vec3(glm::column(*cameraGraphNode->GetTransform(), 3)));
	//std::cout << "update: " << std::endl;
	//glm::column(*transformCameraGraphNode, 3) = glm::vec4(camera.Position + glm::vec3(0, 0, 0), 1.0f);

	//glm::column(*transformGunGraphNode, 3) = glm::vec4(camera.Position + glm::vec3(0, 0, 0), 1.0f);
	//gunObject.transform.position = camera.Position + glm::vec3(-15, -9, 3.25);
	//bullet.transform.position = Model1.transform.position + glm::vec3(0, -15, -1);
}


// 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;
		firstMouse = false;
	}

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

	lastX = xpos;


	camera.ProcessMouseMovement(xoffset, yoffset);
}

void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
{
	if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS)
	{
		std::cout << "click" << std::endl;

		if (gunColor == glm::vec4(1.0, 0.0, 0.0, 1.0)) if (redBullets > 0) {
			if (redBullets > 0) {
				std::cout << "red" << std::endl;
				bulletGraphNode->Scale(glm::vec3(1.0f));
				redBullets -= 1;
			}
		}
		if (gunColor == glm::vec4(0.0, 1.0, 0.0, 1.0)) {
			if (greenBullets > 0) {
				std::cout << "green" << std::endl;

				//bullet.transform.scale = glm::vec3(0.033f);
				greenBullets -= 1;
			}
		}
		if (gunColor == glm::vec4(0.0, 0.0, 1.0, 1.0)) {
			if (blueBullets > 0) {
				std::cout << "blue" << std::endl;
				

				//bullet.transform.scale = glm::vec3(0.033f);
				blueBullets -= 1;
			}
		}
		if (gunColor == glm::vec4(0.0, 0.0, 0.0, 1.0)) {
			if (blackBullets > 0) {
				std::cout << "black" << std::endl;

				//bullet.transform.scale = glm::vec3(0.033f);
				blackBullets -= 1;
			}
		}
	}
	if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_RELEASE)
	{
		//bullet.transform.scale = glm::vec3(0);
	}
}

// glfw: whenever the mouse scroll wheel scrolls, this callback is called
// ----------------------------------------------------------------------
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
	camera.ProcessMouseScroll(yoffset);
}

// utility function for loading a 2D texture from file
// ---------------------------------------------------
unsigned int loadTexture(char const* path)
{
	unsigned int textureID;
	glGenTextures(1, &textureID);

	int width, height, nrComponents;
	unsigned char* data = stbi_load(path, &width, &height, &nrComponents, 0);
	if (data)
	{
		GLenum format;
		if (nrComponents == 1)
			format = GL_RED;
		else if (nrComponents == 3)
			format = GL_RGB;
		else if (nrComponents == 4)
			format = GL_RGBA;

		glBindTexture(GL_TEXTURE_2D, textureID);
		glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
		glGenerateMipmap(GL_TEXTURE_2D);

		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

		stbi_image_free(data);
	}
	else
	{
		std::cout << "Texture failed to load at path: " << path << std::endl;
		stbi_image_free(data);
	}

	return textureID;
}

// loads a cubemap texture from 6 individual texture faces
// order:
// +X (right)
// -X (left)
// +Y (top)
// -Y (bottom)
// +Z (front) 
// -Z (back)
// -------------------------------------------------------
unsigned int loadCubemap(std::vector<std::string> faces)
{
	unsigned int textureID;
	glGenTextures(1, &textureID);
	glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);

	int width, height, nrComponents;
	for (unsigned int i = 0; i < faces.size(); i++)
	{
		unsigned char* data = stbi_load(faces[i].c_str(), &width, &height, &nrComponents, 0);
		if (data)
		{
			glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
			stbi_image_free(data);
		}
		else
		{
			std::cout << "Cubemap texture failed to load at path: " << faces[i] << std::endl;
			stbi_image_free(data);
		}
	}
	glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);

	return textureID;
}