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 "../model.h"
#include "camera.h"
#include "Skybox.h"
class Scene {
public:
Camera cameras;
Camera MainCamera;
Skybox skybox;
std::vector <Model> entities;
// std::vector<Material> materials;

View File

@ -1,38 +1,5 @@
#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()
{
glBindVertexArray(m_skyboxVAO);
@ -45,8 +12,33 @@ void Skybox::Unbind()
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)
{
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();
int width, height, nrChannels;
unsigned char* data;

View File

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

View File

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

View File

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