Added Cubemap
This commit is contained in:
1 parent
f749b29c31
commit
9158de2f39
22 files changed
+325
-16
No files matched your search
@@ -71,7 +71,7 @@ namespace nf {
|
||||
return m_front;
|
||||
}
|
||||
|
||||
void Camera::bind(Shader* shader) {
|
||||
void Camera::bind(Shader* entityShader, Shader* cubemapShader) {
|
||||
glm::mat4 view;
|
||||
|
||||
switch (m_type) {
|
||||
@@ -112,8 +112,11 @@ namespace nf {
|
||||
}
|
||||
}
|
||||
glm::vec3 pos(m_position.x, m_position.y, m_position.z);
|
||||
shader->setUniform("camera.pos", pos);
|
||||
shader->setUniform("view", view);
|
||||
entityShader->setUniform("camera.pos", pos);
|
||||
entityShader->setUniform("view", view);
|
||||
|
||||
glm::mat4 cubemapView = glm::mat4(glm::mat3(view));
|
||||
cubemapShader->setUniform("view", cubemapView);
|
||||
}
|
||||
|
||||
Camera::~Camera() {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
namespace nf {
|
||||
Entity::Entity() :
|
||||
m_constructed(false),
|
||||
m_model(nullptr),
|
||||
m_position(0.0),
|
||||
m_rotation(0.0),
|
||||
@@ -15,6 +16,7 @@ namespace nf {
|
||||
}
|
||||
|
||||
void Entity::create(Asset* modelAsset, Asset* textureAsset) {
|
||||
m_constructed = true;
|
||||
AModel& model = *(AModel*)modelAsset;
|
||||
if (model.alreadyLoaded && textureAsset == nullptr) {
|
||||
m_model = model.loadedModel;
|
||||
@@ -36,6 +38,10 @@ namespace nf {
|
||||
model.loadedModel = m_model;
|
||||
}
|
||||
|
||||
bool Entity::isConstructed() {
|
||||
return m_constructed;
|
||||
}
|
||||
|
||||
void Entity::setPosition(double x, double y, double z) {
|
||||
m_position = { x, y, z };
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace nf {
|
||||
UIElement::UIElement() :
|
||||
m_constructed(false),
|
||||
m_centeredX(false),
|
||||
m_centeredY(false)
|
||||
{
|
||||
|
||||
@@ -7,14 +7,15 @@
|
||||
|
||||
namespace nf {
|
||||
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;
|
||||
ATexture* tex = (ATexture*)texture;
|
||||
ATexture* tex = (ATexture*)textureAsset;
|
||||
m_position = position;
|
||||
m_scale = (float)scale;
|
||||
if (tex->alreadyLoaded) {
|
||||
|
||||
@@ -8,12 +8,15 @@
|
||||
#include "Shader.h"
|
||||
#include "Light.h"
|
||||
#include "Entity.h"
|
||||
#include "Cubemap.h"
|
||||
#include "UIElement.h"
|
||||
#include "Camera.h"
|
||||
#include "Utility.h"
|
||||
|
||||
namespace nf {
|
||||
Renderer::Renderer(Application* app) {
|
||||
Renderer::Renderer(Application* app) :
|
||||
m_cubemap(nullptr)
|
||||
{
|
||||
m_app = app;
|
||||
m_hdc = GetDC(m_app->getWindow());
|
||||
PIXELFORMATDESCRIPTOR pfd = {
|
||||
@@ -69,6 +72,9 @@ namespace nf {
|
||||
const char* uiTextureVertex = m_baseAP["uiTextureVertex.shader"]->data;
|
||||
const char* uiTextureFragment = m_baseAP["uiTextureFragment.shader"]->data;
|
||||
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::plane = (AModel*)m_baseAP["plane.obj"];
|
||||
@@ -80,7 +86,7 @@ namespace nf {
|
||||
}
|
||||
|
||||
void Renderer::render(Entity& in) {
|
||||
if (in.getModel() == nullptr)
|
||||
if (in.isConstructed() == false)
|
||||
Error("Tried to render Entity before being created!");
|
||||
m_lGame.push_back(&in);
|
||||
//TODO: Sort transparent objects by distance; Farthest first
|
||||
@@ -95,10 +101,17 @@ namespace nf {
|
||||
Error("Tried to render a light before being created!");
|
||||
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 = ∈
|
||||
}
|
||||
|
||||
void Renderer::doFrame(Camera* camera) {
|
||||
//Begin frame
|
||||
glViewport(0, 0, m_app->getConfig().width, m_app->getConfig().height);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
camera->bind(m_entityShader, m_cubemapShader);
|
||||
|
||||
//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);
|
||||
@@ -107,7 +120,6 @@ namespace nf {
|
||||
for (Entity* draw : m_lGame) {
|
||||
Entity& curr = *draw;
|
||||
curr.bind(m_entityShader);
|
||||
camera->bind(m_entityShader);
|
||||
//TODO: Clean this up a bit
|
||||
m_entityShader->setUniform("numberOfLights", (int)m_lights.size() + 1);
|
||||
for (unsigned int i = 0; i < m_lights.size(); i++) {
|
||||
@@ -120,19 +132,24 @@ namespace nf {
|
||||
m_lGame.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
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
proj = glm::ortho(0.0f, (float)m_app->getConfig().width, 0.0f, (float)m_app->getConfig().height);
|
||||
for (UIElement* draw : m_lUI) {
|
||||
UIElement& curr = *draw;
|
||||
if (curr.identity() == "text") {
|
||||
m_textShader->bind();
|
||||
m_textShader->setUniform("proj", proj);
|
||||
curr.render(m_textShader, m_app->getConfig().width, m_app->getConfig().height);
|
||||
continue;
|
||||
}
|
||||
if (curr.identity() == "texture") {
|
||||
m_uiTextureShader->bind();
|
||||
m_uiTextureShader->setUniform("proj", proj);
|
||||
curr.render(m_uiTextureShader, m_app->getConfig().width, m_app->getConfig().height);
|
||||
}
|
||||
|
||||
@@ -48,21 +48,25 @@ namespace nf {
|
||||
}
|
||||
|
||||
void Shader::setUniform(const std::string& name, glm::mat4& data) {
|
||||
bind();
|
||||
if (m_uniformLocations.find(name) == m_uniformLocations.end())
|
||||
getUniformLocation(name);
|
||||
glUniformMatrix4fv(m_uniformLocations[name], 1, GL_FALSE, glm::value_ptr(data));
|
||||
}
|
||||
void Shader::setUniform(const std::string& name, glm::vec3& data) {
|
||||
bind();
|
||||
if (m_uniformLocations.find(name) == m_uniformLocations.end())
|
||||
getUniformLocation(name);
|
||||
glUniform3fv(m_uniformLocations[name], 1, glm::value_ptr(data));
|
||||
}
|
||||
void Shader::setUniform(const std::string& name, int data) {
|
||||
bind();
|
||||
if (m_uniformLocations.find(name) == m_uniformLocations.end())
|
||||
getUniformLocation(name);
|
||||
glUniform1i(m_uniformLocations[name], data);
|
||||
}
|
||||
void Shader::setUniform(const std::string& name, float data) {
|
||||
bind();
|
||||
if (m_uniformLocations.find(name) == m_uniformLocations.end())
|
||||
getUniformLocation(name);
|
||||
glUniform1f(m_uniformLocations[name], data);
|
||||
|
||||
@@ -20,7 +20,7 @@ namespace nf {
|
||||
glBindTexture(GL_TEXTURE_2D, m_id);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_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);
|
||||
stbi_image_free(texture);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user