Added Cubemap

This commit is contained in:
Grayson Riffe (Desktop) 2021-09-06 01:06:47 -05:00
parent f749b29c31
commit 9158de2f39
22 changed files with 325 additions and 16 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 99 KiB

View File

@ -21,5 +21,4 @@ void MainState::render(nf::Renderer& renderer) {
void MainState::onExit() { void MainState::onExit() {
Log("MainState onExit!"); Log("MainState onExit!");
} }

View File

@ -0,0 +1,16 @@
#version 330 core
in vec3 texCoord;
uniform samplerCube cm;
out vec4 color;
void main() {
vec3 tc;
tc = vec3(-texCoord.x, texCoord.y, texCoord.z);
if (texCoord.y > 0.99 || texCoord.y < -0.99) {
tc = vec3(texCoord.x, texCoord.y, texCoord.z);
}
color = texture(cm, tc);
}

View File

@ -0,0 +1,15 @@
#version 330 core
layout(location = 0) in vec3 pos;
uniform mat4 view;
uniform mat4 proj;
out vec3 texCoord;
void main() {
texCoord = pos;
vec4 vertexPos = proj * view * vec4(pos, 1.0);
gl_Position = vertexPos.xyww;
}

View File

@ -200,6 +200,7 @@
<ClCompile Include="src\Gamestate.cpp" /> <ClCompile Include="src\Gamestate.cpp" />
<ClCompile Include="src\IntroGamestate.cpp" /> <ClCompile Include="src\IntroGamestate.cpp" />
<ClCompile Include="src\Renderer\Camera.cpp" /> <ClCompile Include="src\Renderer\Camera.cpp" />
<ClCompile Include="src\Renderer\Drawable\Cubemap.cpp" />
<ClCompile Include="src\Renderer\Drawable\Drawable.cpp" /> <ClCompile Include="src\Renderer\Drawable\Drawable.cpp" />
<ClCompile Include="src\Renderer\Drawable\Entity.cpp" /> <ClCompile Include="src\Renderer\Drawable\Entity.cpp" />
<ClCompile Include="src\Renderer\Drawable\Light.cpp" /> <ClCompile Include="src\Renderer\Drawable\Light.cpp" />
@ -218,6 +219,7 @@
<ItemGroup> <ItemGroup>
<ClInclude Include="src\include\Assets.h" /> <ClInclude Include="src\include\Assets.h" />
<ClInclude Include="src\include\Camera.h" /> <ClInclude Include="src\include\Camera.h" />
<ClInclude Include="src\include\Cubemap.h" />
<ClInclude Include="src\include\Entity.h" /> <ClInclude Include="src\include\Entity.h" />
<ClInclude Include="src\include\Application.h" /> <ClInclude Include="src\include\Application.h" />
<ClInclude Include="src\include\Config.h" /> <ClInclude Include="src\include\Config.h" />

View File

@ -72,6 +72,9 @@
<ClCompile Include="src\Renderer\Drawable\Light.cpp"> <ClCompile Include="src\Renderer\Drawable\Light.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="src\Renderer\Drawable\Cubemap.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="src\include\Config.h"> <ClInclude Include="src\include\Config.h">
@ -140,6 +143,9 @@
<ClInclude Include="src\include\Light.h"> <ClInclude Include="src\include\Light.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="src\include\Cubemap.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Natvis Include="NatvisFile.natvis" /> <Natvis Include="NatvisFile.natvis" />

View File

@ -16,6 +16,15 @@ namespace nf {
} }
ACubemap::~ACubemap() {
delete[] frontData;
delete[] backData;
delete[] topData;
delete[] bottomData;
delete[] leftData;
delete[] rightData;
}
AShader::~AShader() { AShader::~AShader() {
} }
@ -31,6 +40,7 @@ namespace nf {
void AssetPack::load(const char* packName) { void AssetPack::load(const char* packName) {
std::string path = "assets/" + (std::string)packName; std::string path = "assets/" + (std::string)packName;
std::string packContents = readFile(path); std::string packContents = readFile(path);
unsigned int cubemapCount = 0;
while (packContents.size()) { while (packContents.size()) {
unsigned int startingPos = packContents.find_first_of("#NFASSET ") + 9; unsigned int startingPos = packContents.find_first_of("#NFASSET ") + 9;
packContents = packContents.substr(9); packContents = packContents.substr(9);
@ -62,6 +72,56 @@ namespace nf {
continue; continue;
} }
if (extension == "png") { if (extension == "png") {
if (assetName.find("_cmfront") != std::string::npos || assetName.find("_cmback") != std::string::npos || assetName.find("_cmtop") != std::string::npos || assetName.find("_cmbottom") != std::string::npos || assetName.find("_cmleft") != std::string::npos || assetName.find("_cmright") != std::string::npos) {
static std::unordered_map<std::string, ACubemap*> cubemaps;
std::string cmName = assetName.substr(0, assetName.find('_'));
ACubemap* curr;
if (cubemaps.find(cmName) == cubemaps.end()) {
cubemaps[cmName] = new ACubemap;
cubemaps[cmName]->numImages = 0;
}
curr = cubemaps[cmName];
if (curr->numImages == 6)
Error("Duplicate cubemap images in pack \"" + (std::string)packName + (std::string)"\"!");
if (assetName.find("_cmfront") != std::string::npos) {
curr->frontData = new char[assetSize];
std::memcpy(curr->frontData, &assetContents[0], assetSize);
curr->frontSize = assetSize;
}
if (assetName.find("_cmback") != std::string::npos) {
curr->backData = new char[assetSize];
std::memcpy(curr->backData, &assetContents[0], assetSize);
curr->backSize = assetSize;
}
if (assetName.find("_cmtop") != std::string::npos) {
curr->topData = new char[assetSize];
std::memcpy(curr->topData, &assetContents[0], assetSize);
curr->topSize = assetSize;
}
if (assetName.find("_cmbottom") != std::string::npos) {
curr->bottomData = new char[assetSize];
std::memcpy(curr->bottomData, &assetContents[0], assetSize);
curr->bottomSize = assetSize;
}
if (assetName.find("_cmleft") != std::string::npos) {
curr->leftData = new char[assetSize];
std::memcpy(curr->leftData, &assetContents[0], assetSize);
curr->leftSize = assetSize;
}
if (assetName.find("_cmright") != std::string::npos) {
curr->rightData = new char[assetSize];
std::memcpy(curr->rightData, &assetContents[0], assetSize);
curr->rightSize = assetSize;
}
curr->numImages++;
if (curr->numImages == 6) {
m_assets[cmName] = curr;
}
cubemapCount++;
continue;
}
ATexture* texture = new ATexture; ATexture* texture = new ATexture;
texture->data = new char[assetSize]; texture->data = new char[assetSize];
std::memcpy(texture->data, &assetContents[0], assetSize); std::memcpy(texture->data, &assetContents[0], assetSize);
@ -87,6 +147,8 @@ namespace nf {
} }
Error("Invalid asset extention in pack \"" + (std::string)packName + (std::string)"\"!"); Error("Invalid asset extention in pack \"" + (std::string)packName + (std::string)"\"!");
} }
if (cubemapCount % 6 != 0)
Error("Could not find full cubemap in pack \"" + (std::string)packName + (std::string)"\"!")
} }
Asset* AssetPack::operator[](const char* in) { Asset* AssetPack::operator[](const char* in) {

View File

@ -71,7 +71,7 @@ namespace nf {
return m_front; return m_front;
} }
void Camera::bind(Shader* shader) { void Camera::bind(Shader* entityShader, Shader* cubemapShader) {
glm::mat4 view; glm::mat4 view;
switch (m_type) { switch (m_type) {
@ -112,8 +112,11 @@ namespace nf {
} }
} }
glm::vec3 pos(m_position.x, m_position.y, m_position.z); glm::vec3 pos(m_position.x, m_position.y, m_position.z);
shader->setUniform("camera.pos", pos); entityShader->setUniform("camera.pos", pos);
shader->setUniform("view", view); entityShader->setUniform("view", view);
glm::mat4 cubemapView = glm::mat4(glm::mat3(view));
cubemapShader->setUniform("view", cubemapView);
} }
Camera::~Camera() { Camera::~Camera() {

View File

@ -0,0 +1,129 @@
#include "Cubemap.h"
#include <vector>
#include "GL/glew.h"
#include "stb_image.h"
#include "Assets.h"
namespace nf {
Cubemap::Cubemap() :
m_constructed(false),
m_id(0)
{
}
void Cubemap::create(Asset* cubemapAsset) {
m_constructed = true;
ACubemap& cm = *(ACubemap*)cubemapAsset;
glGenTextures(1, &m_id);
glBindTexture(GL_TEXTURE_CUBE_MAP, m_id);
struct CMFace {
CMFace(unsigned char* d, unsigned int w, unsigned int h, unsigned int nc) :
data(d),
width(w),
height(h),
nChannels(nc)
{
}
unsigned char* data;
unsigned int width;
unsigned int height;
unsigned int nChannels;
};
std::vector<CMFace> faces;
int tempWidth, tempHeight, tempNChannels;
unsigned char* tempData;
stbi_set_flip_vertically_on_load(false);
tempData = stbi_load_from_memory((unsigned char*)cm.leftData, cm.leftSize, &tempWidth, &tempHeight, &tempNChannels, 0);
faces.emplace_back(tempData, tempWidth, tempHeight, tempNChannels);
tempData = stbi_load_from_memory((unsigned char*)cm.rightData, cm.rightSize, &tempWidth, &tempHeight, &tempNChannels, 0);
faces.emplace_back(tempData, tempWidth, tempHeight, tempNChannels);
stbi_set_flip_vertically_on_load(true);
tempData = stbi_load_from_memory((unsigned char*)cm.topData, cm.topSize, &tempWidth, &tempHeight, &tempNChannels, 0);
faces.emplace_back(tempData, tempWidth, tempHeight, tempNChannels);
tempData = stbi_load_from_memory((unsigned char*)cm.bottomData, cm.bottomSize, &tempWidth, &tempHeight, &tempNChannels, 0);
faces.emplace_back(tempData, tempWidth, tempHeight, tempNChannels);
stbi_set_flip_vertically_on_load(false);
tempData = stbi_load_from_memory((unsigned char*)cm.backData, cm.backSize, &tempWidth, &tempHeight, &tempNChannels, 0);
faces.emplace_back(tempData, tempWidth, tempHeight, tempNChannels);
tempData = stbi_load_from_memory((unsigned char*)cm.frontData, cm.frontSize, &tempWidth, &tempHeight, &tempNChannels, 0);
faces.emplace_back(tempData, tempWidth, tempHeight, tempNChannels);
for (unsigned int i = 0; i < faces.size(); i++) {
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGBA, faces[i].width, faces[i].height, 0, faces[i].nChannels == 3 ? GL_RGB : GL_RGBA, GL_UNSIGNED_BYTE, faces[i].data);
stbi_image_free(faces[i].data);
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_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);
float vb[] = {
-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
};
m_vao = new VertexArray;
m_vao->addBuffer(vb, sizeof(vb));
m_vao->push<float>(3);
m_vao->finishBufferLayout();
}
bool Cubemap::isConstructed() {
return m_constructed;
}
void Cubemap::render() {
m_vao->bind();
glBindTexture(GL_TEXTURE_CUBE_MAP, m_id);
glDepthFunc(GL_LEQUAL);
glDrawArrays(GL_TRIANGLES, 0, 36);
glDepthFunc(GL_LESS);
}
Cubemap::~Cubemap() {
if(m_constructed)
glDeleteTextures(1, &m_id);
}
}

View File

@ -6,6 +6,7 @@
namespace nf { namespace nf {
Entity::Entity() : Entity::Entity() :
m_constructed(false),
m_model(nullptr), m_model(nullptr),
m_position(0.0), m_position(0.0),
m_rotation(0.0), m_rotation(0.0),
@ -15,6 +16,7 @@ namespace nf {
} }
void Entity::create(Asset* modelAsset, Asset* textureAsset) { void Entity::create(Asset* modelAsset, Asset* textureAsset) {
m_constructed = true;
AModel& model = *(AModel*)modelAsset; AModel& model = *(AModel*)modelAsset;
if (model.alreadyLoaded && textureAsset == nullptr) { if (model.alreadyLoaded && textureAsset == nullptr) {
m_model = model.loadedModel; m_model = model.loadedModel;
@ -36,6 +38,10 @@ namespace nf {
model.loadedModel = m_model; model.loadedModel = m_model;
} }
bool Entity::isConstructed() {
return m_constructed;
}
void Entity::setPosition(double x, double y, double z) { void Entity::setPosition(double x, double y, double z) {
m_position = { x, y, z }; m_position = { x, y, z };
} }

View File

@ -2,6 +2,7 @@
namespace nf { namespace nf {
UIElement::UIElement() : UIElement::UIElement() :
m_constructed(false),
m_centeredX(false), m_centeredX(false),
m_centeredY(false) m_centeredY(false)
{ {

View File

@ -7,14 +7,15 @@
namespace nf { namespace nf {
UITexture::UITexture() : UITexture::UITexture() :
m_texture(0) m_texture(nullptr),
m_scale(1.0f)
{ {
} }
void UITexture::create(Asset* texture, const Vec2& position, double scale) { void UITexture::create(Asset* textureAsset, const Vec2& position, double scale) {
m_constructed = true; m_constructed = true;
ATexture* tex = (ATexture*)texture; ATexture* tex = (ATexture*)textureAsset;
m_position = position; m_position = position;
m_scale = (float)scale; m_scale = (float)scale;
if (tex->alreadyLoaded) { if (tex->alreadyLoaded) {

View File

@ -8,12 +8,15 @@
#include "Shader.h" #include "Shader.h"
#include "Light.h" #include "Light.h"
#include "Entity.h" #include "Entity.h"
#include "Cubemap.h"
#include "UIElement.h" #include "UIElement.h"
#include "Camera.h" #include "Camera.h"
#include "Utility.h" #include "Utility.h"
namespace nf { namespace nf {
Renderer::Renderer(Application* app) { Renderer::Renderer(Application* app) :
m_cubemap(nullptr)
{
m_app = app; m_app = app;
m_hdc = GetDC(m_app->getWindow()); m_hdc = GetDC(m_app->getWindow());
PIXELFORMATDESCRIPTOR pfd = { PIXELFORMATDESCRIPTOR pfd = {
@ -69,6 +72,9 @@ namespace nf {
const char* uiTextureVertex = m_baseAP["uiTextureVertex.shader"]->data; const char* uiTextureVertex = m_baseAP["uiTextureVertex.shader"]->data;
const char* uiTextureFragment = m_baseAP["uiTextureFragment.shader"]->data; const char* uiTextureFragment = m_baseAP["uiTextureFragment.shader"]->data;
m_uiTextureShader = new Shader(uiTextureVertex, uiTextureFragment); m_uiTextureShader = new Shader(uiTextureVertex, uiTextureFragment);
const char* cubemapVertex = m_baseAP["cubemapVertex.shader"]->data;
const char* cubemapFragment = m_baseAP["cubemapFragment.shader"]->data;
m_cubemapShader = new Shader(cubemapVertex, cubemapFragment);
BaseAssets::cube = (AModel*)m_baseAP["cube.obj"]; BaseAssets::cube = (AModel*)m_baseAP["cube.obj"];
BaseAssets::plane = (AModel*)m_baseAP["plane.obj"]; BaseAssets::plane = (AModel*)m_baseAP["plane.obj"];
@ -80,7 +86,7 @@ namespace nf {
} }
void Renderer::render(Entity& in) { void Renderer::render(Entity& in) {
if (in.getModel() == nullptr) if (in.isConstructed() == false)
Error("Tried to render Entity before being created!"); Error("Tried to render Entity before being created!");
m_lGame.push_back(&in); m_lGame.push_back(&in);
//TODO: Sort transparent objects by distance; Farthest first //TODO: Sort transparent objects by distance; Farthest first
@ -95,10 +101,17 @@ namespace nf {
Error("Tried to render a light before being created!"); Error("Tried to render a light before being created!");
m_lights.push_back(&in); m_lights.push_back(&in);
} }
void Renderer::render(Cubemap& in) {
if (in.isConstructed() == false)
Error("Tried to render a cubemap before being created!");
m_cubemap = &in;
}
void Renderer::doFrame(Camera* camera) { void Renderer::doFrame(Camera* camera) {
//Begin frame
glViewport(0, 0, m_app->getConfig().width, m_app->getConfig().height); glViewport(0, 0, m_app->getConfig().width, m_app->getConfig().height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
camera->bind(m_entityShader, m_cubemapShader);
//Draw Entities (3D models) //Draw Entities (3D models)
glm::mat4 proj = glm::perspective(glm::radians(45.0f), (float)m_app->getConfig().width / (float)m_app->getConfig().height, 0.1f, 100000.0f); glm::mat4 proj = glm::perspective(glm::radians(45.0f), (float)m_app->getConfig().width / (float)m_app->getConfig().height, 0.1f, 100000.0f);
@ -107,7 +120,6 @@ namespace nf {
for (Entity* draw : m_lGame) { for (Entity* draw : m_lGame) {
Entity& curr = *draw; Entity& curr = *draw;
curr.bind(m_entityShader); curr.bind(m_entityShader);
camera->bind(m_entityShader);
//TODO: Clean this up a bit //TODO: Clean this up a bit
m_entityShader->setUniform("numberOfLights", (int)m_lights.size() + 1); m_entityShader->setUniform("numberOfLights", (int)m_lights.size() + 1);
for (unsigned int i = 0; i < m_lights.size(); i++) { for (unsigned int i = 0; i < m_lights.size(); i++) {
@ -120,19 +132,24 @@ namespace nf {
m_lGame.clear(); m_lGame.clear();
m_lights.clear(); m_lights.clear();
//Draw cubemap where there isn't anything else
if (m_cubemap != nullptr) {
m_cubemapShader->setUniform("proj", proj);
m_cubemap->render();
}
m_cubemap = nullptr;
//Draw UI elements //Draw UI elements
glDisable(GL_DEPTH_TEST); glDisable(GL_DEPTH_TEST);
proj = glm::ortho(0.0f, (float)m_app->getConfig().width, 0.0f, (float)m_app->getConfig().height); proj = glm::ortho(0.0f, (float)m_app->getConfig().width, 0.0f, (float)m_app->getConfig().height);
for (UIElement* draw : m_lUI) { for (UIElement* draw : m_lUI) {
UIElement& curr = *draw; UIElement& curr = *draw;
if (curr.identity() == "text") { if (curr.identity() == "text") {
m_textShader->bind();
m_textShader->setUniform("proj", proj); m_textShader->setUniform("proj", proj);
curr.render(m_textShader, m_app->getConfig().width, m_app->getConfig().height); curr.render(m_textShader, m_app->getConfig().width, m_app->getConfig().height);
continue; continue;
} }
if (curr.identity() == "texture") { if (curr.identity() == "texture") {
m_uiTextureShader->bind();
m_uiTextureShader->setUniform("proj", proj); m_uiTextureShader->setUniform("proj", proj);
curr.render(m_uiTextureShader, m_app->getConfig().width, m_app->getConfig().height); curr.render(m_uiTextureShader, m_app->getConfig().width, m_app->getConfig().height);
} }

View File

@ -48,21 +48,25 @@ namespace nf {
} }
void Shader::setUniform(const std::string& name, glm::mat4& data) { void Shader::setUniform(const std::string& name, glm::mat4& data) {
bind();
if (m_uniformLocations.find(name) == m_uniformLocations.end()) if (m_uniformLocations.find(name) == m_uniformLocations.end())
getUniformLocation(name); getUniformLocation(name);
glUniformMatrix4fv(m_uniformLocations[name], 1, GL_FALSE, glm::value_ptr(data)); glUniformMatrix4fv(m_uniformLocations[name], 1, GL_FALSE, glm::value_ptr(data));
} }
void Shader::setUniform(const std::string& name, glm::vec3& data) { void Shader::setUniform(const std::string& name, glm::vec3& data) {
bind();
if (m_uniformLocations.find(name) == m_uniformLocations.end()) if (m_uniformLocations.find(name) == m_uniformLocations.end())
getUniformLocation(name); getUniformLocation(name);
glUniform3fv(m_uniformLocations[name], 1, glm::value_ptr(data)); glUniform3fv(m_uniformLocations[name], 1, glm::value_ptr(data));
} }
void Shader::setUniform(const std::string& name, int data) { void Shader::setUniform(const std::string& name, int data) {
bind();
if (m_uniformLocations.find(name) == m_uniformLocations.end()) if (m_uniformLocations.find(name) == m_uniformLocations.end())
getUniformLocation(name); getUniformLocation(name);
glUniform1i(m_uniformLocations[name], data); glUniform1i(m_uniformLocations[name], data);
} }
void Shader::setUniform(const std::string& name, float data) { void Shader::setUniform(const std::string& name, float data) {
bind();
if (m_uniformLocations.find(name) == m_uniformLocations.end()) if (m_uniformLocations.find(name) == m_uniformLocations.end())
getUniformLocation(name); getUniformLocation(name);
glUniform1f(m_uniformLocations[name], data); glUniform1f(m_uniformLocations[name], data);

View File

@ -20,7 +20,7 @@ namespace nf {
glBindTexture(GL_TEXTURE_2D, m_id); glBindTexture(GL_TEXTURE_2D, m_id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, nChannels == 3 ? GL_RGB : GL_RGBA, m_x, m_y, 0, nChannels == 3 ? GL_RGB : GL_RGBA, GL_UNSIGNED_BYTE, texture); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_x, m_y, 0, nChannels == 3 ? GL_RGB : GL_RGBA, GL_UNSIGNED_BYTE, texture);
glGenerateMipmap(GL_TEXTURE_2D); glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(texture); stbi_image_free(texture);
} }

View File

@ -9,7 +9,6 @@ namespace nf {
struct Asset { struct Asset {
char* data; char* data;
bool alreadyLoaded = false; bool alreadyLoaded = false;
virtual ~Asset(); virtual ~Asset();
}; };
@ -26,6 +25,25 @@ namespace nf {
~ATexture() override; ~ATexture() override;
}; };
struct ACubemap : Asset {
char* frontData;
size_t frontSize;
char* backData;
size_t backSize;
char* topData;
size_t topSize;
char* bottomData;
size_t bottomSize;
char* leftData;
size_t leftSize;
char* rightData;
size_t rightSize;
unsigned int numImages;
~ACubemap();
};
struct AShader : Asset { struct AShader : Asset {
~AShader() override; ~AShader() override;
}; };

View File

@ -27,7 +27,7 @@ namespace nf {
const Vec3& getPosition(); const Vec3& getPosition();
const Vec3& getRotation(); const Vec3& getRotation();
void bind(Shader* shader); void bind(Shader* entityShader, Shader* cubemapShader);
~Camera(); ~Camera();
private: private:

View File

@ -0,0 +1,20 @@
#pragma once
#include "Drawable.h"
namespace nf {
struct Asset;
class Cubemap : public Drawable {
public:
Cubemap();
void create(Asset* cubemapAsset);
bool isConstructed();
void render();
~Cubemap();
private:
bool m_constructed;
unsigned int m_id;
};
}

View File

@ -11,6 +11,7 @@ namespace nf {
Entity(); Entity();
void create(Asset* modelAsset, Asset* textureAsset = nullptr); void create(Asset* modelAsset, Asset* textureAsset = nullptr);
bool isConstructed();
void setPosition(double x, double y, double z); void setPosition(double x, double y, double z);
void setPosition(const Vec3& position); void setPosition(const Vec3& position);
@ -27,6 +28,7 @@ namespace nf {
private: private:
void setModelMatrix(Shader* shader); void setModelMatrix(Shader* shader);
bool m_constructed;
Model* m_model; Model* m_model;
Vec3 m_position; Vec3 m_position;

View File

@ -10,6 +10,7 @@
#include "IntroGamestate.h" #include "IntroGamestate.h"
#include "Assets.h" #include "Assets.h"
#include "Light.h" #include "Light.h"
#include "Cubemap.h"
#include "Text.h" #include "Text.h"
#include "UITexture.h" #include "UITexture.h"
#include "Input.h" #include "Input.h"
@ -54,6 +55,7 @@ namespace nf {
void render(Entity& in); void render(Entity& in);
void render(UIElement& in); void render(UIElement& in);
void render(Light& in); void render(Light& in);
void render(Cubemap& in);
void doFrame(Camera* camera); void doFrame(Camera* camera);
@ -68,10 +70,12 @@ namespace nf {
std::vector<Light*> m_lights; std::vector<Light*> m_lights;
std::vector<Entity*> m_lGame; std::vector<Entity*> m_lGame;
Cubemap* m_cubemap;
std::vector<UIElement*> m_lUI; std::vector<UIElement*> m_lUI;
Shader* m_entityShader; Shader* m_entityShader;
Shader* m_textShader; Shader* m_textShader;
Shader* m_uiTextureShader; Shader* m_uiTextureShader;
Shader* m_cubemapShader;
}; };
class Application { class Application {

View File

@ -10,6 +10,7 @@ namespace nf {
class Entity; class Entity;
class UIElement; class UIElement;
class Light; class Light;
class Cubemap;
class Camera; class Camera;
class Renderer { class Renderer {
@ -19,6 +20,7 @@ namespace nf {
void render(Entity& in); void render(Entity& in);
void render(UIElement& in); void render(UIElement& in);
void render(Light& in); void render(Light& in);
void render(Cubemap& in);
void doFrame(Camera* camera); void doFrame(Camera* camera);
@ -33,9 +35,11 @@ namespace nf {
std::vector<Light*> m_lights; std::vector<Light*> m_lights;
std::vector<Entity*> m_lGame; std::vector<Entity*> m_lGame;
Cubemap* m_cubemap;
std::vector<UIElement*> m_lUI; std::vector<UIElement*> m_lUI;
Shader* m_entityShader; Shader* m_entityShader;
Shader* m_textShader; Shader* m_textShader;
Shader* m_uiTextureShader; Shader* m_uiTextureShader;
Shader* m_cubemapShader;
}; };
} }

View File

@ -10,7 +10,7 @@ namespace nf {
public: public:
UITexture(); UITexture();
void create(Asset* texture, const Vec2& position, double scale = 1.0); void create(Asset* textureAsset, const Vec2& position, double scale = 1.0);
const char* identity() override; const char* identity() override;
void render(Shader* shader, unsigned int windowWidth, unsigned int windowHeight) override; void render(Shader* shader, unsigned int windowWidth, unsigned int windowHeight) override;