Added Skybox back!

This commit is contained in:
Nigel Barink 2023-05-19 21:07:44 +02:00
parent 8890b4d973
commit 4be70e132d
6 changed files with 50 additions and 83 deletions

View File

@ -2,11 +2,13 @@
#include <vector> #include <vector>
#include "../model.h" #include "../model.h"
#include "camera.h" #include "camera.h"
#include "Skybox.h"
class Scene { class Scene {
public: public:
Camera cameras; Camera MainCamera;
Skybox skybox;
std::vector <Model> entities; std::vector <Model> entities;
// std::vector<Material> materials; // std::vector<Material> materials;

View File

@ -1,38 +1,5 @@
#include "Skybox.h" #include "Skybox.h"
Skybox::Skybox()
{
glGenTextures(1, &id);
// Create SkyboxVAO
glGenVertexArrays(1, &m_skyboxVAO);
glBindVertexArray(m_skyboxVAO);
glGenBuffers(1, &m_skyboxVBO);
glBindBuffer(GL_ARRAY_BUFFER, m_skyboxVBO);
glBufferData(GL_ARRAY_BUFFER, m_skyboxVertices.size() * sizeof(float), &m_skyboxVertices[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0,3, GL_FLOAT,GL_FALSE, 3 * sizeof(float),(void*) 0);
glBindVertexArray(0);
std::cout << "Skybox Created" << std::endl;
}
Skybox::~Skybox()
{
glDeleteTextures(1, &id);
std::cout << "Skybox destroyed!" << std::endl;
}
void Skybox::Bind() void Skybox::Bind()
{ {
glBindVertexArray(m_skyboxVAO); glBindVertexArray(m_skyboxVAO);
@ -45,8 +12,33 @@ void Skybox::Unbind()
glBindTexture(GL_TEXTURE_CUBE_MAP, 0); glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
} }
void Skybox::Destroy() {
glDeleteTextures(1, &id);
std::cout << "Skybox destroyed!" << std::endl;
}
void Skybox::loadCubeTextures(const std::vector<std::string>& texture_faces) void Skybox::loadCubeTextures(const std::vector<std::string>& texture_faces)
{ {
glGenTextures(1, &id);
// Create SkyboxVAO
glGenVertexArrays(1, &m_skyboxVAO);
glBindVertexArray(m_skyboxVAO);
glGenBuffers(1, &m_skyboxVBO);
glBindBuffer(GL_ARRAY_BUFFER, m_skyboxVBO);
glBufferData(GL_ARRAY_BUFFER, m_skyboxVertices.size() * sizeof(float), &m_skyboxVertices[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glBindVertexArray(0);
std::cout << "Skybox Created" << std::endl;
Bind(); Bind();
int width, height, nrChannels; int width, height, nrChannels;
unsigned char* data; unsigned char* data;

View File

@ -9,14 +9,14 @@
class Skybox { class Skybox {
public: public:
Skybox (); Skybox () = default ;
~Skybox ();
GLuint m_skyboxVAO; GLuint m_skyboxVAO;
void Bind(); void Bind();
void Unbind(); void Unbind();
void Destroy();
void loadCubeTextures(const std::vector<std::string>& texture_faces); void loadCubeTextures(const std::vector<std::string>& texture_faces);
private: private:

View File

@ -68,13 +68,11 @@ void Renderer::Render(Scene& scene)
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glm::mat4 projection = glm::perspective(glm::radians(scene.cameras.Zoom), (float)800 / (float)600, 0.1f, 100.0f); glm::mat4 projection = glm::perspective(glm::radians(scene.MainCamera.Zoom), (float)800 / (float)600, 0.1f, 100.0f);
auto view = scene.cameras.GetViewMatrix(); auto view = scene.MainCamera.GetViewMatrix();
auto model = glm::mat4(1.0f); auto model = glm::mat4(1.0f);
// 1. Skybox pass // 1. Skybox pass
/*
Shader cubemap; Shader cubemap;
cubemap.Load("../Shaders/skybox.vs", "../Shaders/Cubemap.fs"); cubemap.Load("../Shaders/skybox.vs", "../Shaders/Cubemap.fs");
@ -83,17 +81,16 @@ void Renderer::Render(Scene& scene)
cubemap.use(); cubemap.use();
cubemap.setMat4("projection", projection); cubemap.setMat4("projection", projection);
auto centeredView = glm::mat4(glm::mat3(scene.cameras.GetViewMatrix())); auto centeredView = glm::mat4(glm::mat3(scene.MainCamera.GetViewMatrix()));
cubemap.setMat4("view", centeredView); cubemap.setMat4("view", centeredView);
skybox.Bind(); scene.skybox.Bind();
glDrawArrays(GL_TRIANGLES, 0, 36); glDrawArrays(GL_TRIANGLES, 0, 36);
skybox.Unbind(); scene.skybox.Unbind();
glDepthMask(GL_TRUE); glDepthMask(GL_TRUE);
*/
// 2. Scene pass // 2. Scene pass
@ -101,14 +98,7 @@ void Renderer::Render(Scene& scene)
blinnPhong.Load("../Shaders/shader.vs", "../Shaders/shader.fs"); blinnPhong.Load("../Shaders/shader.vs", "../Shaders/shader.fs");
blinnPhong.use(); blinnPhong.use();
blinnPhong.setVec3("cameraPos", scene.cameras.Position); blinnPhong.setVec3("cameraPos", scene.MainCamera.Position);
/*
glActiveTexture(GL_TEXTURE11);
m_skybox.Bind(); // Segmentation Fault HERE!!
glActiveTexture(GL_TEXTURE0);
*/
blinnPhong.setInt("skybox", 11); blinnPhong.setInt("skybox", 11);
@ -120,8 +110,6 @@ void Renderer::Render(Scene& scene)
entity.Draw(blinnPhong); entity.Draw(blinnPhong);
} }
// m_skybox.Unbind();
// 3. outline pass // 3. outline pass
//Shader OutlineShader; //Shader OutlineShader;
//OutlineShader.Load("../Shaders/shader.vs", "../Shaders/outlineshader.fs"); //OutlineShader.Load("../Shaders/shader.vs", "../Shaders/outlineshader.fs");
@ -161,17 +149,3 @@ void Renderer::Shutdown() {
framebuffer.Unbind(); framebuffer.Unbind();
} }
Renderer::Renderer() {
// auto outlinepass = OutlinePass(scene , model, projection, camera);
}
Renderer::~Renderer()
{
}

View File

@ -10,8 +10,7 @@
class Renderer class Renderer
{ {
public: public:
Renderer(); Renderer() = default ;
~Renderer();
void Setup(); void Setup();
void resize(int width, int height); void resize(int width, int height);
void Render(Scene& scene); void Render(Scene& scene);

View File

@ -26,8 +26,7 @@ float deltaTime = 0.0f; ; // Time between current frame and last frame
float lastFrame = 0.0f; // Time of last frame float lastFrame = 0.0f; // Time of last frame
bool firstMouse = true; bool firstMouse = true;
float lastx = 400, lasty = 300; float lastx = 400, lasty = 300;
Scene scene;
Camera camera(glm::vec3(0.0f,0.0f,8.0f));
class LearnOpenGL : Application class LearnOpenGL : Application
{ {
@ -50,10 +49,8 @@ public:
stbi_set_flip_vertically_on_load(true); stbi_set_flip_vertically_on_load(true);
Model backpack("../Models/backpack.obj"); Model backpack("../Models/backpack.obj");
Scene scene;
scene.entities.push_back(backpack); scene.entities.push_back(backpack);
scene.MainCamera = Camera(glm::vec3(0.0f, 0.0f, 8.0f));
scene.cameras = camera;
std::vector<std::string> faces = { std::vector<std::string> faces = {
@ -64,8 +61,7 @@ public:
"../Textures/skybox/front.jpg", "../Textures/skybox/front.jpg",
"../Textures/skybox/back.jpg" "../Textures/skybox/back.jpg"
}; };
Skybox skybox = Skybox(); scene.skybox.loadCubeTextures(faces);
skybox.loadCubeTextures(faces);
Renderer renderer = Renderer(); Renderer renderer = Renderer();
renderer.resize(WIDTH, HEIGHT); renderer.resize(WIDTH, HEIGHT);
@ -97,6 +93,8 @@ public:
window.SwapBuffers(); window.SwapBuffers();
} }
scene.skybox.Destroy();
ImGui_ImplOpenGL3_Shutdown(); ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown(); ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext(); ImGui::DestroyContext();
@ -111,13 +109,13 @@ void processInput( GLFWwindow* window)
const float CameraSpeed = 0.5f * deltaTime; const float CameraSpeed = 0.5f * deltaTime;
if(glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) if(glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
camera.ProcessKeyboard(FORWARD, CameraSpeed); scene.MainCamera.ProcessKeyboard(FORWARD, CameraSpeed);
if(glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) if(glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
camera.ProcessKeyboard(BACKWARD,CameraSpeed); scene.MainCamera.ProcessKeyboard(BACKWARD,CameraSpeed);
if(glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) if(glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
camera.ProcessKeyboard(LEFT, CameraSpeed); scene.MainCamera.ProcessKeyboard(LEFT, CameraSpeed);
if(glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) if(glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
camera.ProcessKeyboard(RIGHT,CameraSpeed); scene.MainCamera.ProcessKeyboard(RIGHT,CameraSpeed);
} }
void mouse_callback(GLFWwindow* window, double xpos, double ypos) void mouse_callback(GLFWwindow* window, double xpos, double ypos)
@ -134,17 +132,19 @@ void mouse_callback(GLFWwindow* window, double xpos, double ypos)
lastx = xpos; lastx = xpos;
lasty - ypos; lasty - ypos;
camera.ProcessMouseMovement(xoffset, yoffset); scene.MainCamera.ProcessMouseMovement(xoffset, yoffset);
} }
void framebuffer_size_callback(GLFWwindow* window, int width, int height) void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{ {
glViewport(0,0, width,height); glViewport(0,0, width,height);
} }
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset) void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{ {
camera.ProcessMouseScroll(yoffset); scene.MainCamera.ProcessMouseScroll(yoffset);
} }
int main() int main()