Cubemap added, Skybox pass implemented

This commit is contained in:
Nigel Barink 2022-10-16 20:14:06 +02:00
parent db448390ae
commit e7a119f0dd
13 changed files with 236 additions and 18 deletions

11
Cubemap.fs Normal file
View File

@ -0,0 +1,11 @@
#version 460 core
out vec4 FragColor;
in vec3 TexCoords;
uniform samplerCube cubemap;
void main()
{
FragColor = texture(cubemap, TexCoords);
}

View File

@ -21,9 +21,9 @@ void main(){
*/
// physically accurate grayscale
// FragColor = texture(screenTexure, TexCoords);
// float average = 0.2126 * FragColor.r + 0.7152 * FragColor.g + 0.0722 * FragColor.b;
// FragColor = vec4(average,average,average, 1.0);
/* FragColor = texture(screenTexure, TexCoords);
float average = 0.2126 * FragColor.r + 0.7152 * FragColor.g + 0.0722 * FragColor.b;
FragColor = vec4(average,average,average, 1.0);*/
vec2 offset[9] = vec2[] (
vec2(-offset, offset), // top-left

BIN
Textures/skybox/back.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Textures/skybox/bottom.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Textures/skybox/front.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Textures/skybox/left.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Textures/skybox/right.jpg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Textures/skybox/top.jpg (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -1,8 +1,10 @@
#version 460 core
out vec4 FragColor;
in vec2 TexCoords;
in vec2 TexCoords;
uniform sampler2D texture_diffuse1;
void main()

13
skybox.vs Normal file
View File

@ -0,0 +1,13 @@
#version 460 core
layout (location = 0) in vec3 aPos;
out vec3 TexCoords;
uniform mat4 projection;
uniform mat4 view;
void main()
{
TexCoords = aPos;
gl_Position = projection * view * vec4(aPos ,1.0);
}

44
src/Cubemap.cpp Normal file
View File

@ -0,0 +1,44 @@
#include "Cubemap.h"
Cubemap::Cubemap(){
glGenTextures(1,&id);
}
Cubemap::~Cubemap(){
// glDeleteTextures(id);
}
void Cubemap::Bind(){
glBindTexture(GL_TEXTURE_CUBE_MAP, id);
};
void Cubemap::Unbind(){
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
}
void Cubemap::loadCubeTextures(const std::vector<std::string>& texture_faces){
int width, height, nrChannels;
unsigned char* data;
stbi_set_flip_vertically_on_load(false);
for (unsigned int i = 0; i < texture_faces.size(); i++){
data = stbi_load(texture_faces[i].c_str(), &width, &height, &nrChannels,0);
if (data){
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, width, height, 0 ,GL_RGB, GL_UNSIGNED_BYTE,data);
} else {
std::cout << "Cubemap tex failed to load at path: " << texture_faces[i] << std::endl;
}
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_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);
stbi_set_flip_vertically_on_load(true);
}

25
src/Cubemap.h Normal file
View File

@ -0,0 +1,25 @@
#pragma once
#include <glad/glad.h>
#include "stb_image.h"
#include <iostream>
#include <vector>
#include <string>
class Cubemap {
public:
unsigned int id;
Cubemap();
~Cubemap();
void Bind();
void Unbind();
void loadCubeTextures(const std::vector<std::string>& texture_faces);
};

View File

@ -1,5 +1,6 @@
#include <stdio.h>
#include <cmath>
#include <vector>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
@ -13,6 +14,7 @@
#include "model.h"
#include "FrameBuffer.hpp"
#include "RenderBuffer.hpp"
#include "Cubemap.h"
// https://learnopengl.com/Advanced-OpenGL/Framebuffers
@ -40,6 +42,60 @@ std::vector<float> ScreenVertices = {
1.0f, 1.0f, 1.0f, 1.0f,
} ;
std::vector<std::string> faces = {
"Textures/skybox/right.jpg",
"Textures/skybox/left.jpg",
"Textures/skybox/top.jpg",
"Textures/skybox/bottom.jpg",
"Textures/skybox/front.jpg",
"Textures/skybox/back.jpg"
};
std::vector<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
};
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
glViewport(0,0, width, height);
}
@ -111,12 +167,31 @@ GLFWwindow* CreateWindowWithOpenGLContext()
}
void Skybox_pass (GLuint& SkyboxVAO, Shader& skyboxShader, Cubemap& skycubemap)
{
//glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
//glClear(GL_COLOR_BUFFER_BIT );
glDepthMask(GL_FALSE);
skyboxShader.use();
skyboxShader.setMat4("projection", projection);
glm::mat4 centeredView = glm::mat4(glm::mat3(camera.GetViewMatrix()));
skyboxShader.setMat4("view", centeredView);
glBindVertexArray(SkyboxVAO);
skycubemap.Bind();
glDrawArrays(GL_TRIANGLES, 0, 36);
glDepthMask(GL_TRUE);
}
void drawScene_pass(Shader& shader, Model& Object)
{
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glStencilFunc(GL_ALWAYS, 1, 0xff);
glStencilMask(0xFF);
//glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
shader.use();
shader.setMat4("model", model);
@ -128,6 +203,8 @@ void drawScene_pass(Shader& shader, Model& Object)
void outline_pass( Shader& shader, Model& Object)
{
glStencilFunc(GL_NOTEQUAL, 1, 0xFF);
glStencilMask(0x00);
glDisable(GL_DEPTH_TEST);
@ -142,6 +219,9 @@ void outline_pass( Shader& shader, Model& Object)
shader.setMat4("model", model);
Object.Draw(shader);
glStencilFunc(GL_ALWAYS, 1, 0xff);
glStencilMask(0xFF);
}
void DrawToScreen( GLuint& VAO, GLuint& ScreenTexture, Shader& shader)
@ -179,6 +259,7 @@ int main() {
Shader shader ("shader.vs", "shader.fs");
Shader outlineShader("shader.vs","outlineshader.fs");
Shader framebufferShader("Framebuffers.vs", "Framebuffers.fs" );
Shader skyboxShader("skybox.vs", "Cubemap.fs");
Model backpack("Models/backpack.obj");
@ -223,7 +304,12 @@ int main() {
framebuffer.Unbind();
Cubemap skybox= Cubemap ();
skybox.Bind();
skybox.loadCubeTextures(faces);
// Create ScreenVAO
#pragma region ScreenVAO
GLuint ScreenVAO, VBO;
glGenVertexArrays(1, &ScreenVAO);
glBindVertexArray(ScreenVAO);
@ -240,6 +326,24 @@ int main() {
glBindVertexArray(0);
#pragma endregion ScreenVAO
// Create SkyboxVAO
GLuint SkyboxVAO, SkyboxVBO;
glGenVertexArrays(1, &SkyboxVAO);
glBindVertexArray(SkyboxVAO);
glGenBuffers(1, &SkyboxVBO);
glBindBuffer(GL_ARRAY_BUFFER, SkyboxVBO);
glBufferData(GL_ARRAY_BUFFER, skyboxVertices.size() * sizeof(float), &skyboxVertices[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0,3, GL_FLOAT,GL_FALSE, 3 * sizeof(float),(void*) 0);
glBindVertexArray(0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_STENCIL_TEST);
@ -264,22 +368,23 @@ while(!glfwWindowShouldClose(window))
framebuffer.Bind();
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Skybox_pass(SkyboxVAO, skyboxShader, skybox);
drawScene_pass(shader, backpack);
//outline_pass(outlineShader, backpack);
outline_pass(outlineShader, backpack);
DrawToScreen(ScreenVAO, ColourBuffer->id, framebufferShader);
// Reset stencil
// Reset stencil
glStencilMask(0xFF);
glStencilFunc(GL_ALWAYS, 1, 0xFF);
glEnable(GL_DEPTH_TEST);
glfwSwapBuffers(window);
glfwPollEvents();
}