up
This commit is contained in:
parent
da3f2ec685
commit
8bf452cc79
@ -1,20 +1,18 @@
|
|||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
//#include <stb_image.h>
|
#include <stb_image.h>
|
||||||
#define STB_IMAGE_IMPLEMENTATION // dodane do pliku z kursu
|
#define STB_IMAGE_IMPLEMENTATION // dodane do pliku z kursu
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
#include <glm/gtc/matrix_transform.hpp>
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
#include <glm/gtc/type_ptr.hpp>
|
#include <glm/gtc/type_ptr.hpp>
|
||||||
|
|
||||||
#include <learnopengl/filesystem.h>
|
#include <learnopengl/filesystem.h>
|
||||||
#include <learnopengl/shader.h>
|
#include <learnopengl/shader_m.h>
|
||||||
#include <learnopengl/camera.h>
|
#include <learnopengl/camera.h>
|
||||||
#include <learnopengl/model.h>
|
#include <learnopengl/model.h>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
GLFWwindow *initialize_glfw_window(void);
|
|
||||||
|
|
||||||
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
|
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
|
||||||
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
|
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
|
||||||
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
|
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
|
||||||
@ -36,16 +34,56 @@ bool firstMouse = true;
|
|||||||
float deltaTime = 0.0f;
|
float deltaTime = 0.0f;
|
||||||
float lastFrame = 0.0f;
|
float lastFrame = 0.0f;
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
float angle = 0;
|
||||||
|
|
||||||
|
|
||||||
int main()
|
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;
|
return -1;
|
||||||
|
}
|
||||||
|
glfwMakeContextCurrent(window);
|
||||||
|
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||||||
|
glfwSetCursorPosCallback(window, mouse_callback);
|
||||||
|
glfwSetScrollCallback(window, scroll_callback);
|
||||||
|
|
||||||
// load objects and shaders
|
// tell GLFW to capture our mouse
|
||||||
Shader kniedeShader("9.2.geometry_shader.vert", "9.2.geometry_shader.frag");
|
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||||
Model kniedeModel("../resources/objects/kniede/kniede.obj");
|
|
||||||
|
|
||||||
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");
|
Shader skyboxShader("6.2.skybox.vert", "6.2.skybox.frag");
|
||||||
|
|
||||||
// set up vertex data (and buffer(s)) and configure vertex attributes
|
// 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);
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
|
||||||
glEnableVertexAttribArray(1);
|
glEnableVertexAttribArray(1);
|
||||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
|
||||||
|
|
||||||
// skybox VAO
|
// skybox VAO
|
||||||
unsigned int skyboxVAO, skyboxVBO;
|
unsigned int skyboxVAO, skyboxVBO;
|
||||||
glGenVertexArrays(1, &skyboxVAO);
|
glGenVertexArrays(1, &skyboxVAO);
|
||||||
@ -163,24 +200,24 @@ int main()
|
|||||||
|
|
||||||
// load textures
|
// 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",
|
dir + "posx.jpg",
|
||||||
"../resources/textures/skybox/" + dir + "/negx.jpg",
|
dir + "negx.jpg",
|
||||||
"../resources/textures/skybox/" + dir + "/posy.jpg",
|
dir + "posy.jpg",
|
||||||
"../resources/textures/skybox/" + dir + "/negy.jpg",
|
dir + "negy.jpg",
|
||||||
"../resources/textures/skybox/" + dir + "/posz.jpg",
|
dir + "posz.jpg",
|
||||||
"../resources/textures/skybox/" + dir + "/negz.jpg",
|
dir + "negz.jpg"
|
||||||
};
|
};
|
||||||
|
|
||||||
unsigned int cubemapTexture = loadCubemap(faces);
|
unsigned int cubemapTexture = loadCubemap(faces);
|
||||||
|
|
||||||
// shader configuration
|
// shader configuration
|
||||||
// --------------------
|
// --------------------
|
||||||
cubemapShader.use();
|
shader.use();
|
||||||
cubemapShader.setInt("skybox", 0);
|
shader.setInt("skybox", 0);
|
||||||
|
|
||||||
skyboxShader.use();
|
skyboxShader.use();
|
||||||
skyboxShader.setInt("skybox", 0);
|
skyboxShader.setInt("skybox", 0);
|
||||||
@ -194,39 +231,42 @@ int main()
|
|||||||
float currentFrame = glfwGetTime();
|
float currentFrame = glfwGetTime();
|
||||||
deltaTime = currentFrame - lastFrame;
|
deltaTime = currentFrame - lastFrame;
|
||||||
lastFrame = currentFrame;
|
lastFrame = currentFrame;
|
||||||
|
angle += deltaTime;
|
||||||
|
|
||||||
|
// input
|
||||||
|
// -----
|
||||||
processInput(window);
|
processInput(window);
|
||||||
|
|
||||||
|
// render
|
||||||
|
// ------
|
||||||
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
// draw scene as normal
|
// draw scene as normal
|
||||||
|
shader.use();
|
||||||
glm::mat4 model = glm::mat4(1.0f);
|
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 view = camera.GetViewMatrix();
|
||||||
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
|
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
|
||||||
|
shader.setMat4("model", model);
|
||||||
// model = glm::translate(model, glm::vec3(0.0f, 0.0f, 0.0f)); // translate it down so it's at the center of the scene
|
shader.setMat4("view", view);
|
||||||
// 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
|
shader.setMat4("projection", projection);
|
||||||
model = glm::rotate(model, glm::radians(-90.0f), glm::vec3(1.0f, 0.0f, 0.0f));
|
shader.setVec3("cameraPos", camera.Position);
|
||||||
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);
|
|
||||||
|
|
||||||
// cubes
|
// cubes
|
||||||
glBindVertexArray(cubeVAO);
|
glBindVertexArray(cubeVAO);
|
||||||
|
|
||||||
glActiveTexture(GL_TEXTURE0);
|
glActiveTexture(GL_TEXTURE0);
|
||||||
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture);
|
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture);
|
||||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
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);
|
glBindVertexArray(0);
|
||||||
|
|
||||||
// draw skybox as last
|
// draw skybox as last
|
||||||
@ -235,7 +275,6 @@ int main()
|
|||||||
view = glm::mat4(glm::mat3(camera.GetViewMatrix())); // remove translation from the view matrix
|
view = glm::mat4(glm::mat3(camera.GetViewMatrix())); // remove translation from the view matrix
|
||||||
skyboxShader.setMat4("view", view);
|
skyboxShader.setMat4("view", view);
|
||||||
skyboxShader.setMat4("projection", projection);
|
skyboxShader.setMat4("projection", projection);
|
||||||
|
|
||||||
// skybox cube
|
// skybox cube
|
||||||
glBindVertexArray(skyboxVAO);
|
glBindVertexArray(skyboxVAO);
|
||||||
glActiveTexture(GL_TEXTURE0);
|
glActiveTexture(GL_TEXTURE0);
|
||||||
@ -250,58 +289,17 @@ int main()
|
|||||||
glfwPollEvents();
|
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();
|
glfwTerminate();
|
||||||
return 0;
|
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
|
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
|
||||||
// ---------------------------------------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------------------------------------
|
||||||
void processInput(GLFWwindow *window)
|
void processInput(GLFWwindow *window)
|
||||||
|
Loading…
Reference in New Issue
Block a user