Skybox.cpp 3.99 KB
Newer Older
's avatar
committed
1
2
#include "Skybox.h" 
#include "engine/EngineState.h"
unknown's avatar
unknown committed
3
4
5
6

#include <util/log.h>
#include <util/files.h>
#include <util/images.h>
's avatar
committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

namespace glare
{
	namespace core
	{

		Skybox::Skybox(std::vector<fs::path> faces)
			: m_bindless_texture_id(0)
		{
			gl::genTextures(1, &m_texture_id);
			gl::activeTexture(0);

			gl::bindTexture(gl::TextureTarget::eCubeMap, m_texture_id);

			int width; int height;
			gl::textureParameter(gl::TextureTarget::eCubeMap, gl::TextureParameter::eBaseLevel, 0);
			gl::textureParameter(gl::TextureTarget::eCubeMap, gl::TextureParameter::eMaxLevel, 8);

			for (GLuint face = 0; face < faces.size(); face++)
			{
				Log_Debug << "Face No " << face;
unknown's avatar
unknown committed
28
29
				auto vec = images::load<unsigned char>(faces[face], width, height, 3);
				images::flip(width, height, 3, vec.data());
's avatar
committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
				gl::textureImage2D(gl::TextureImageTarget2D(int(gl::TextureImageTarget2D::eCubeMapPosX) + face), 0, gl::TextureInternalFormat::eRGB, width, height, 0, gl::TextureFormat::eRGB, gl::Type::eUByte, vec.data());
			}

			gl::textureParameter(gl::TextureTarget::eCubeMap, gl::TextureParameter::eMagFilter, gl::TextureFilterMag::eLinear);
			gl::textureParameter(gl::TextureTarget::eCubeMap, gl::TextureParameter::eMinFilter, gl::TextureFilterMin::eLinearMipmapLinear);
			gl::textureParameter(gl::TextureTarget::eCubeMap, gl::TextureParameter::eWrapS, gl::TextureWrapMode::eClampToEdge);
			gl::textureParameter(gl::TextureTarget::eCubeMap, gl::TextureParameter::eWrapT, gl::TextureWrapMode::eClampToEdge);
			gl::textureParameter(gl::TextureTarget::eCubeMap, gl::TextureParameter::eWrapR, gl::TextureWrapMode::eClampToEdge);
			gl::generateMipmap(gl::TextureTarget::eCubeMap);
			gl::bindTexture(gl::TextureTarget::eCubeMap, 0);

			std::vector<math::Vertex> triangle_vertices
			{
				math::Vertex{ glm::vec4(-1, -1, 0, 1), glm::vec4(0, 0, -1, 0), glm::vec2(0, 0) },
				math::Vertex{ glm::vec4(3, -1, 0, 1), glm::vec4(0, 0, -1, 0), glm::vec2(2, 0) },
				math::Vertex{ glm::vec4(-1, 3, 0, 1), glm::vec4(0, 0, -1, 0), glm::vec2(0, 2) },
			};

			std::vector<unsigned> indices { 0, 2, 1 };

			m_triangle_mesh = std::make_shared<Mesh>(triangle_vertices, indices);

52
53
			const auto vert = std::make_shared<Shader>(gl::ShaderType::eVertex, files::shader("/cubemap/triangle_skybox.vert"));
			const auto frag = std::make_shared<Shader>(gl::ShaderType::eFragment, files::shader("/cubemap/triangle_skybox.frag"));
's avatar
committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
			m_cubemap_shader = std::make_shared<ShaderProgram>(std::vector<std::shared_ptr<Shader>>{ frag, vert });

		}

		std::vector<fs::path> Skybox::collectFilesFrom(const fs::path &directory)
		{
			return { directory / "/posx.jpg",
				directory / "/negx.jpg",
				directory / "/posy.jpg",
				directory / "/negy.jpg",
				directory / "/posz.jpg",
				directory / "/negz.jpg" };
		}

		void Skybox::draw() const
		{
			gl::depthMask(false);
			gl::cullFace(gl::Face::eFront);
			m_cubemap_shader->use();
			gl::activeTexture(0);
			gl::bindTexture(gl::TextureTarget::eCubeMap, m_texture_id);

			m_cubemap_shader->updateUniformInt("u_cube_map", 0);
			m_cubemap_shader->updateUniformMatrix("u_inv_view_projection", glm::inverse(EngineState::main_camera->getViewProjection()));
			m_cubemap_shader->updateUniformVec3("cam_position", EngineState::main_camera->getOwner()->c_worldTransform().position);
			m_triangle_mesh->draw();

			gl::bindTexture(gl::TextureTarget::eCubeMap, 0);
			gl::cullFace(gl::Face::eBack);
			gl::depthMask(true);
		}

		unsigned Skybox::getID() const
		{
			return m_texture_id;
		}

		uint64_t Skybox::getBindlessID()
		{
			m_bindless_texture_id = gl::getTextureHandle(m_texture_id);

			if (!gl::isTextureHandleResident(m_bindless_texture_id))
				gl::makeTextureHandleResident(m_bindless_texture_id);

			return m_bindless_texture_id;
		}

		void Skybox::unbindBindless() const
		{
			if (gl::isTextureHandleResident(m_bindless_texture_id))
				gl::makeTextureHandleNonResident(m_bindless_texture_id);
		}


		Skybox::~Skybox()
		{

		}
	}
}