This commit is contained in:
Artur Tamborski 2021-02-16 19:18:33 +01:00
parent da3f2ec685
commit 8bf452cc79

View File

@ -1,20 +1,18 @@
#include <glad/glad.h>
#include <GLFW/glfw3.h>
//#include <stb_image.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.h>
#include <learnopengl/shader_m.h>
#include <learnopengl/camera.h>
#include <learnopengl/model.h>
#include <iostream>
GLFWwindow *initialize_glfw_window(void);
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);
@ -36,16 +34,56 @@ bool firstMouse = true;
float deltaTime = 0.0f;
float lastFrame = 0.0f;
//
float angle = 0;
int main()
{
if ((GLFWwindow *window = initialize_glfw_window()) == nullptr)
// 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);
// load objects and shaders
Shader kniedeShader("9.2.geometry_shader.vert", "9.2.geometry_shader.frag");
Model kniedeModel("../resources/objects/kniede/kniede.obj");
// tell GLFW to capture our mouse
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
Shader cubemapShader("6.2.cubemaps.vert", "6.2.cubemaps.frag");
// 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 shader("6.2.cubemaps.vert", "6.2.cubemaps.frag");
Shader skyboxShader("6.2.skybox.vert", "6.2.skybox.frag");
// set up vertex data (and buffer(s)) and configure vertex attributes
@ -150,7 +188,6 @@ int main()
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);
@ -163,24 +200,24 @@ int main()
// load textures
// -------------
string dir = "mountain-skyboxes/Ryfjallet";
string dir = "../resources/textures/SaintsPetersBasilica/";
vector<string> faces
vector<std::string> faces
{
"../resources/textures/skybox/" + dir + "/posx.jpg",
"../resources/textures/skybox/" + dir + "/negx.jpg",
"../resources/textures/skybox/" + dir + "/posy.jpg",
"../resources/textures/skybox/" + dir + "/negy.jpg",
"../resources/textures/skybox/" + dir + "/posz.jpg",
"../resources/textures/skybox/" + dir + "/negz.jpg",
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
// --------------------
cubemapShader.use();
cubemapShader.setInt("skybox", 0);
shader.use();
shader.setInt("skybox", 0);
skyboxShader.use();
skyboxShader.setInt("skybox", 0);
@ -194,39 +231,42 @@ int main()
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);
// draw scene as normal
shader.use();
glm::mat4 model = glm::mat4(1.0f);
model = glm::rotate(model, angle/10.f, glm::vec3(0, 1, 0));
glm::mat4 view = camera.GetViewMatrix();
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
// model = glm::translate(model, glm::vec3(0.0f, 0.0f, 0.0f)); // translate it down so it's at the center of the scene
// model = glm::scale(model, glm::vec3(1.0f, 1.0f, 1.0f)); // it's a bit too big for our scene, so scale it down
model = glm::rotate(model, glm::radians(-90.0f), glm::vec3(1.0f, 0.0f, 0.0f));
kniedeShader.use();
kniedeShader.setMat4("model", model);
kniedeShader.setMat4("view", view);
kniedeShader.setMat4("projection", projection);
kniedeModel.Draw(kniedeShader);
// draw scene as normal
model = glm::mat4(1.0f);
cubemapShader.use();
cubemapShader.setMat4("model", model);
cubemapShader.setMat4("view", view);
cubemapShader.setMat4("projection", projection);
cubemapShader.setVec3("cameraPos", camera.Position);
shader.setMat4("model", model);
shader.setMat4("view", view);
shader.setMat4("projection", projection);
shader.setVec3("cameraPos", camera.Position);
// cubes
glBindVertexArray(cubeVAO);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture);
glDrawArrays(GL_TRIANGLES, 0, 36);
model = glm::mat4(1.0f);
model = glm::rotate(model, 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));
shader.setMat4("model", model);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
// draw skybox as last
@ -235,7 +275,6 @@ int main()
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);
@ -250,58 +289,17 @@ int main()
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;
}
// initialize GLFW window and OpenGL context with glew
// ---------------------------------------------------------------------------------------------------------
GLFWwindow *initialize_glfw_window(void)
{
// 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 nullptr;
}
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 nullptr;
}
// configure global opengl state
// -----------------------------
glEnable(GL_DEPTH_TEST);
return window;
}
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow *window)