573 lines
22 KiB
C++
573 lines
22 KiB
C++
#include <glad/glad.h>
|
|
#include <GLFW/glfw3.h>
|
|
#include <stb_image.h>
|
|
#define STB_IMAGE_IMPLEMENTATION // dodane do pliku z kursu
|
|
#include <glm/glm.hpp>
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
#include <glm/gtc/type_ptr.hpp>
|
|
|
|
#include <learnopengl/filesystem.h>
|
|
#include <learnopengl/shader_m.h>
|
|
#include <learnopengl/camera.h>
|
|
#include <learnopengl/model.h>
|
|
|
|
#include <iostream>
|
|
|
|
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);
|
|
unsigned int loadTexture(const char *path);
|
|
unsigned int loadCubemap(vector<std::string> faces);
|
|
|
|
// settings
|
|
const unsigned int SCR_WIDTH = 800 * 2;
|
|
const unsigned int SCR_HEIGHT = 600 * 2;
|
|
|
|
// camera
|
|
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
|
|
float lastX = (float)SCR_WIDTH / 2.0;
|
|
float lastY = (float)SCR_HEIGHT / 2.0;
|
|
bool firstMouse = true;
|
|
|
|
glm::vec3 spaceshipTranslate(1.0f);
|
|
|
|
// timing
|
|
float deltaTime = 0.0f;
|
|
float lastFrame = 0.0f;
|
|
|
|
glm::vec3 lightPos(-2.0f, 4.0f, -1.0f);
|
|
|
|
// positions of the point lights
|
|
glm::vec3 pointLightPositions[] = {
|
|
glm::vec3(-18.0f, 3.0f, 99999.0f),
|
|
glm::vec3( 0.8f, -16.6f, -0.6f),
|
|
glm::vec3( 6.8f, -7.4f, -7.2f),
|
|
glm::vec3( -11.8f, -7.889f, 15.0f),
|
|
glm::vec3(-14.0f, 3.0f, 2.0f),
|
|
};
|
|
size_t pointLightPositionsNum = sizeof(pointLightPositions) / sizeof(pointLightPositions[0]);
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
{
|
|
// 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);
|
|
|
|
// 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 shaders
|
|
// -------------------------
|
|
Shader cubeShader("6.2.cubemaps.vert", "6.2.cubemaps.frag");
|
|
Shader skyboxShader("6.2.skybox.vert", "6.2.skybox.frag");
|
|
Shader lightingShader("6.multiple_lights.vert", "6.multiple_lights.frag");
|
|
|
|
// build and compile models
|
|
// ------------------------
|
|
Model kniedeModel("../resources/objects/kniede/kniede.obj1");
|
|
Model spaceshipModel("../resources/objects/spaceship/Intergalactic_Spaceship.obj");
|
|
Model barrelModel("../resources/objects/barrel/barrel.obj");
|
|
|
|
stbi_set_flip_vertically_on_load(true);
|
|
Model backpackModel("../resources/objects/backpack/backpack.obj");
|
|
stbi_set_flip_vertically_on_load(false);
|
|
|
|
// 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
|
|
};
|
|
|
|
// 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
|
|
// -------------
|
|
string dir = "../resources/textures/SaintsPetersBasilica/";
|
|
|
|
vector<std::string> faces
|
|
{
|
|
dir + "posx.jpg",
|
|
dir + "negx.jpg",
|
|
dir + "posy.jpg",
|
|
dir + "negy.jpg",
|
|
dir + "posz.jpg",
|
|
dir + "negz.jpg"
|
|
};
|
|
|
|
unsigned int cubemapTexture = loadCubemap(faces);
|
|
|
|
// shader configuration
|
|
// --------------------
|
|
cubeShader.use();
|
|
cubeShader.setInt("skybox", 0);
|
|
skyboxShader.use();
|
|
skyboxShader.setInt("skybox", 0);
|
|
lightingShader.use();
|
|
lightingShader.setInt("material.diffuse", 0);
|
|
lightingShader.setInt("material.specular", 1);
|
|
|
|
// render loop
|
|
// -----------
|
|
float angle = 0;
|
|
while (!glfwWindowShouldClose(window))
|
|
{
|
|
// per-frame time logic
|
|
// --------------------
|
|
float currentFrame = glfwGetTime();
|
|
deltaTime = currentFrame - lastFrame;
|
|
lastFrame = currentFrame;
|
|
angle += deltaTime;
|
|
|
|
// input
|
|
// -----
|
|
processInput(window);
|
|
|
|
// render
|
|
// ------
|
|
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
// be sure to activate shader when setting uniforms/drawing objects
|
|
lightingShader.use();
|
|
lightingShader.setVec3("viewPos", camera.Position);
|
|
lightingShader.setFloat("material.shininess", 12.0f);
|
|
|
|
// directional light
|
|
lightingShader.setVec3("dirLight.direction", -0.2f, -1.0f, -0.3f);
|
|
lightingShader.setVec3("dirLight.ambient", 0.05f, 0.05f, 0.05f);
|
|
lightingShader.setVec3("dirLight.diffuse", 0.4f, 0.4f, 0.4f);
|
|
lightingShader.setVec3("dirLight.specular", 0.5f, 0.5f, 0.5f);
|
|
// point light 1
|
|
lightingShader.setVec3("pointLights[0].position", pointLightPositions[0]);
|
|
lightingShader.setVec3("pointLights[0].ambient", 8.0f, 8.0f, 5.0f);
|
|
lightingShader.setVec3("pointLights[0].diffuse", 0.8f, 0.8f, 0.8f);
|
|
lightingShader.setVec3("pointLights[0].specular", 1.0f, 1.0f, 1.0f);
|
|
lightingShader.setFloat("pointLights[0].constant", 1.0f);
|
|
lightingShader.setFloat("pointLights[0].linear", 0.09);
|
|
lightingShader.setFloat("pointLights[0].quadratic", 0.032);
|
|
// point light 2
|
|
lightingShader.setVec3("pointLights[1].position", pointLightPositions[1]);
|
|
lightingShader.setVec3("pointLights[1].ambient", 0.1f, 0.5f, 0.0f);
|
|
lightingShader.setVec3("pointLights[1].diffuse", 0.5f, 1.0f, 0.8f);
|
|
lightingShader.setVec3("pointLights[1].specular", 1.0f, 1.0f, 1.0f);
|
|
lightingShader.setFloat("pointLights[1].constant", 0.1f);
|
|
lightingShader.setFloat("pointLights[1].linear", 0.09);
|
|
lightingShader.setFloat("pointLights[1].quadratic", 0.032);
|
|
// point light 3
|
|
lightingShader.setVec3("pointLights[2].position", pointLightPositions[2]);
|
|
lightingShader.setVec3("pointLights[2].ambient", 0.7f, 0.3f, 0.0f);
|
|
lightingShader.setVec3("pointLights[2].diffuse", 4.0f, 1.0f, 0.0f);
|
|
lightingShader.setVec3("pointLights[2].specular", 0.5f, 0.5f, 0.0f);
|
|
lightingShader.setFloat("pointLights[2].constant", 0.3f);
|
|
lightingShader.setFloat("pointLights[2].linear", 0.09);
|
|
lightingShader.setFloat("pointLights[2].quadratic", 0.032);
|
|
// point light 4
|
|
// pointLightPositions[3].z = 14.0f + (glm::cos(glfwGetTime() * 3) * 2);
|
|
lightingShader.setVec3("pointLights[3].position", pointLightPositions[3]);
|
|
lightingShader.setVec3("pointLights[3].ambient", 1.0f, (pointLightPositions[3].z - 12) / 10, 0.0f);
|
|
lightingShader.setVec3("pointLights[3].diffuse", 1.0f, 1.0f, 0.0f);
|
|
lightingShader.setVec3("pointLights[3].specular", 0.5f, 0.5f, 0.0f);
|
|
lightingShader.setFloat("pointLights[3].constant", 0.3f);
|
|
lightingShader.setFloat("pointLights[3].linear", 0.09);
|
|
lightingShader.setFloat("pointLights[3].quadratic", 0.033);
|
|
// spotLight
|
|
lightingShader.setVec3("spotLight.position", pointLightPositions[4]);
|
|
lightingShader.setVec3("spotLight.direction", glm::vec3(0.82f, -0.24f, -0.5f));
|
|
lightingShader.setVec3("spotLight.ambient", 0.0f, 0.0f, 0.0f);
|
|
lightingShader.setVec3("spotLight.diffuse", 40.0f, 40.0f, 40.0f);
|
|
lightingShader.setVec3("spotLight.specular", 1.0f, 1.0f, 1.0f);
|
|
lightingShader.setFloat("spotLight.constant", 0.5f);
|
|
lightingShader.setFloat("spotLight.linear", 0.09);
|
|
lightingShader.setFloat("spotLight.quadratic", 0.032);
|
|
lightingShader.setFloat("spotLight.cutOff", glm::cos(glm::radians(15.5f)));
|
|
lightingShader.setFloat("spotLight.outerCutOff", glm::cos(glm::radians(40.0f + glm::sin(glfwGetTime()) * 8)));
|
|
|
|
// DEFAULTS
|
|
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
|
|
glm::mat4 view = camera.GetViewMatrix();
|
|
glm::mat4 model = glm::mat4(1.0f);
|
|
|
|
// ---------------------------------------------------------------------------------------------------
|
|
// --------------------------------------------------------------- R Y S O W A N I E M O D E L I ---
|
|
// ---------------------------------------------------------------------------------------------------
|
|
|
|
// wspólne dla modeli oteksturowanych
|
|
lightingShader.setMat4("projection", projection);
|
|
lightingShader.setMat4("view", view);
|
|
|
|
// plecak
|
|
// model = glm::translate(glm::mat4(1.0f), glm::vec3(-0.8f, -13.6f, -7.0f));
|
|
model = glm::translate(glm::mat4(1.0f), spaceshipTranslate);
|
|
// model = glm::translate(model, spaceshipTranslate);
|
|
model = glm::scale(model, glm::vec3(0.5f));
|
|
model = glm::translate(model, glm::vec3(2.2f, 0.0f, 0.0f));
|
|
model = glm::rotate(model, glm::radians((float)glfwGetTime() * 60) + 90, glm::vec3(0.0f, 1.0f, 0.0f));
|
|
model = glm::translate(model, glm::vec3(-2.2f, 0.0f, 0.0f));
|
|
lightingShader.setMat4("model", model);
|
|
backpackModel.Draw(lightingShader);
|
|
|
|
// pomnik
|
|
model = glm::translate(glm::mat4(1.0f), glm::vec3(0.4, -23, -7));
|
|
model = glm::rotate(model, glm::radians(-90.0f), glm::vec3(1.0f, 0.0f, 0.0f));
|
|
model = glm::translate(model, glm::vec3(-0.4, 0.0f, 0.0f));
|
|
model = glm::rotate(model, glm::radians((float)glfwGetTime() * 60), glm::vec3(0.0f, 0.0f, 1.0f));
|
|
lightingShader.setMat4("model", model);
|
|
kniedeModel.Draw(lightingShader);
|
|
|
|
// beczka
|
|
model = glm::rotate(glm::mat4(1.0f), glm::radians(-90.0f), glm::vec3(1.0f, 0.0f, 0.0f));
|
|
model = glm::translate(model, glm::vec3(10, -3, -7));
|
|
// model = glm::translate(glm::mat4(1.0f), spaceshipTranslate);
|
|
lightingShader.setMat4("model", model);
|
|
barrelModel.Draw(lightingShader);
|
|
|
|
// statek kosmiczny
|
|
// model = glm::translate(glm::mat4(1.0f), glm::vec3(15, -4, 9));
|
|
// model = glm::translate(model, spaceshipTranslate);
|
|
// lightingShader.setMat4("model", model);
|
|
// spaceshipModel.Draw(lightingShader);
|
|
|
|
// kostka z odbiciem
|
|
model = glm::rotate(glm::mat4(1.0f), angle, glm::vec3(0, 1, 0));
|
|
model = glm::translate(model, glm::vec3(5, 0, 0));
|
|
model = glm::rotate(model, -angle, glm::vec3(0, 1, 0));
|
|
model = glm::translate(model, glm::vec3(0, -5, 0));
|
|
model = glm::translate(model, lightPos);
|
|
cubeShader.use();
|
|
cubeShader.setMat4("projection", projection);
|
|
cubeShader.setMat4("view", view);
|
|
cubeShader.setMat4("model", model);
|
|
glBindVertexArray(cubeVAO);
|
|
glActiveTexture(GL_TEXTURE0);
|
|
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture);
|
|
glDrawArrays(GL_TRIANGLES, 0, 36);
|
|
|
|
// pointLightPositions[2] = spaceshipTranslate;
|
|
|
|
// światła (kostki)
|
|
for (unsigned int i = 0; i < pointLightPositionsNum; i++)
|
|
{
|
|
model = glm::translate(glm::mat4(1.0f), pointLightPositions[i]);
|
|
model = glm::scale(model, glm::vec3(0.2f));
|
|
cubeShader.setMat4("model", model);
|
|
glDrawArrays(GL_TRIANGLES, 0, 36);
|
|
}
|
|
|
|
// skybox
|
|
view = glm::mat4(glm::mat3(camera.GetViewMatrix()));
|
|
skyboxShader.use();
|
|
skyboxShader.setMat4("view", view);
|
|
skyboxShader.setMat4("projection", projection);
|
|
glBindVertexArray(skyboxVAO);
|
|
glActiveTexture(GL_TEXTURE0);
|
|
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture);
|
|
glDepthFunc(GL_LEQUAL);
|
|
// glDrawArrays(GL_TRIANGLES, 0, 36);
|
|
glDepthFunc(GL_LESS);
|
|
|
|
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
|
|
// -------------------------------------------------------------------------------
|
|
glfwSwapBuffers(window);
|
|
glfwPollEvents();
|
|
}
|
|
|
|
// optional: de-allocate all resources once they've outlived their purpose:
|
|
// ------------------------------------------------------------------------
|
|
glDeleteVertexArrays(1, &cubeVAO);
|
|
glDeleteVertexArrays(1, &skyboxVAO);
|
|
glDeleteBuffers(1, &cubeVBO);
|
|
glDeleteBuffers(1, &skyboxVAO);
|
|
|
|
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_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_LEFT) == GLFW_PRESS)
|
|
spaceshipTranslate.x += 0.02f;
|
|
if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS)
|
|
spaceshipTranslate.x -= 0.02f;
|
|
if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS)
|
|
spaceshipTranslate.y -= 0.02f;
|
|
if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS)
|
|
spaceshipTranslate.y += 0.02f;
|
|
if (glfwGetKey(window, GLFW_KEY_X) == GLFW_PRESS)
|
|
spaceshipTranslate.z += 0.01f;
|
|
if (glfwGetKey(window, GLFW_KEY_Z) == GLFW_PRESS)
|
|
spaceshipTranslate.z -= 0.01f;
|
|
|
|
if (glfwGetKey(window, GLFW_KEY_P) == GLFW_PRESS)
|
|
std::cout << spaceshipTranslate.x << " " << spaceshipTranslate.y << " " << spaceshipTranslate.z << std::endl;
|
|
if (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS)
|
|
spaceshipTranslate = glm::vec3(1.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);
|
|
}
|
|
|
|
// 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(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;
|
|
}
|