Material update, before changing prefabs
This commit is contained in:
@ -5,14 +5,12 @@
|
|||||||
|
|
||||||
#include "RenderPipeline.hpp"
|
#include "RenderPipeline.hpp"
|
||||||
#include "game/DawnGame.hpp"
|
#include "game/DawnGame.hpp"
|
||||||
#include "display/mesh/QuadMesh.hpp"
|
#include "display/RenderManager.hpp"
|
||||||
#include "scene/SceneItem.hpp"
|
|
||||||
|
|
||||||
using namespace Dawn;
|
using namespace Dawn;
|
||||||
|
|
||||||
RenderPipeline::RenderPipeline(RenderManager *renderManager) {
|
RenderPipeline::RenderPipeline(RenderManager *renderManager) {
|
||||||
assertNotNull(renderManager);
|
assertNotNull(renderManager);
|
||||||
|
|
||||||
this->renderManager = renderManager;
|
this->renderManager = renderManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,12 +75,84 @@ void RenderPipeline::renderScene(Scene *scene) {
|
|||||||
void RenderPipeline::renderSceneCamera(Scene *scene, Camera *camera) {
|
void RenderPipeline::renderSceneCamera(Scene *scene, Camera *camera) {
|
||||||
assertNotNull(scene);
|
assertNotNull(scene);
|
||||||
assertNotNull(camera);
|
assertNotNull(camera);
|
||||||
|
|
||||||
|
// Create a new render ID. Long story short this is a really dirty way of
|
||||||
|
// not sending parameters to shaders more than we need.
|
||||||
|
this->renderId++;
|
||||||
|
|
||||||
|
// Get the list of things to render first.
|
||||||
|
std::vector<struct RenderPipelineItem> pipelineItems;
|
||||||
|
|
||||||
|
// Meshes
|
||||||
auto meshes = scene->findComponents<MeshRenderer>();
|
auto meshes = scene->findComponents<MeshRenderer>();
|
||||||
auto uiCanvasList = scene->findComponents<UICanvas>();
|
auto itMesh = meshes.begin();
|
||||||
|
while(itMesh != meshes.end()) {
|
||||||
|
// Get Mesh
|
||||||
|
auto mesh = *itMesh;
|
||||||
|
assertNotNull(mesh->mesh);
|
||||||
|
|
||||||
|
// Make sure this mesh has a material
|
||||||
|
auto mat = mesh->item->getComponent<Material>();
|
||||||
|
assertNotNull(mat);
|
||||||
|
|
||||||
|
auto shader = mat->getShader();
|
||||||
|
assertNotNull(shader);
|
||||||
|
|
||||||
|
// Now do each pass.
|
||||||
|
auto passes = shader->getItemPasses(mesh, mat);
|
||||||
|
auto itPass = passes.begin();
|
||||||
|
while(itPass != passes.end()) {
|
||||||
|
struct RenderPipelineItem item;
|
||||||
|
item.mesh = mesh;
|
||||||
|
item.pass = *itPass;
|
||||||
|
|
||||||
|
// Do we need to get the W Vector?
|
||||||
|
if(item.pass.needsW) {
|
||||||
|
assertUnreachable();// TODO: Add distance from camera for W vector.
|
||||||
|
} else {
|
||||||
|
item.w = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Queue
|
||||||
|
pipelineItems.push_back(item);
|
||||||
|
++itPass;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now, for optimization, we bind the global parameters here, once for each
|
||||||
|
// shader.
|
||||||
|
if(shader->renderId != this->renderId) {
|
||||||
|
shader->setGlobalParameters(
|
||||||
|
camera->projection,
|
||||||
|
camera->transform->getWorldTransform()
|
||||||
|
);
|
||||||
|
shader->renderId = this->renderId;
|
||||||
|
}
|
||||||
|
|
||||||
|
++itMesh;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Get UI stuff here.
|
||||||
|
|
||||||
|
// Now we've queued everything, let's sort the rendering queue by the priority
|
||||||
|
std::sort(
|
||||||
|
pipelineItems.begin(),
|
||||||
|
pipelineItems.end(),
|
||||||
|
[](struct RenderPipelineItem &a, struct RenderPipelineItem &b){
|
||||||
|
if(a.pass.orderShader == b.pass.orderShader) {
|
||||||
|
return a.w < b.w;
|
||||||
|
}
|
||||||
|
return a.pass.orderShader < b.pass.orderShader;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// Now we've sorted everything! Let's actually start rendering.
|
||||||
|
ShaderProgram *boundProgram = nullptr;
|
||||||
|
std::map<textureslot_t, Texture*> boundTextures;
|
||||||
|
|
||||||
auto renderTarget = camera->getRenderTarget();
|
auto renderTarget = camera->getRenderTarget();
|
||||||
|
|
||||||
assertNotNull(renderTarget);
|
assertNotNull(renderTarget);
|
||||||
|
|
||||||
|
// TODO: This will be editable!
|
||||||
renderTarget->bind();
|
renderTarget->bind();
|
||||||
renderTarget->clear(
|
renderTarget->clear(
|
||||||
RENDER_TARGET_CLEAR_FLAG_DEPTH |
|
RENDER_TARGET_CLEAR_FLAG_DEPTH |
|
||||||
@ -92,92 +162,67 @@ void RenderPipeline::renderSceneCamera(Scene *scene, Camera *camera) {
|
|||||||
RENDER_MANAGER_RENDER_FLAG_DEPTH_TEST |
|
RENDER_MANAGER_RENDER_FLAG_DEPTH_TEST |
|
||||||
RENDER_MANAGER_RENDER_FLAG_BLEND
|
RENDER_MANAGER_RENDER_FLAG_BLEND
|
||||||
);
|
);
|
||||||
|
|
||||||
// Render all Meshes on the scene
|
|
||||||
auto it = meshes.begin();
|
|
||||||
while(it != meshes.end()) {
|
|
||||||
auto mesh = *it;
|
|
||||||
auto material = mesh->item->getComponent<Material>();
|
|
||||||
|
|
||||||
// TODO: fallback material?
|
auto itItems = pipelineItems.begin();
|
||||||
if(material == nullptr) {
|
while(itItems != pipelineItems.end()) {
|
||||||
++it;
|
auto item = *itItems;
|
||||||
continue;
|
|
||||||
|
// Bind the program.
|
||||||
|
if(boundProgram != item.pass.shaderProgram) {
|
||||||
|
boundProgram->bind();
|
||||||
|
boundProgram = item.pass.shaderProgram;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto shader = material->getShader();
|
// Bind the textures to the slots
|
||||||
assertNotNull(shader);
|
auto itTextureSlot = item.pass.textureSlots.begin();
|
||||||
shader->bind();
|
while(itTextureSlot != item.pass.textureSlots.end()) {
|
||||||
shader->setGlobalParameters(camera->projection, camera->transform->getWorldTransform());
|
if(boundTextures[itTextureSlot->first] != itTextureSlot->second) {
|
||||||
shader->setMeshParameters(mesh->item->transform.getWorldTransform());
|
itTextureSlot->second->bind(itTextureSlot->first);
|
||||||
material->setShaderParameters();
|
boundTextures[itTextureSlot->first] = itTextureSlot->second;
|
||||||
|
}
|
||||||
mesh->mesh->draw(MESH_DRAW_MODE_TRIANGLES, 0, -1);
|
++itTextureSlot;
|
||||||
++it;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Now we only render world-absolute UI canvas'
|
|
||||||
auto uiShader = this->renderManager->getUIShader();
|
|
||||||
assertNotNull(uiShader);
|
|
||||||
uiShader->bind();
|
|
||||||
uiShader->setUICamera(camera->projection, camera->transform->getWorldTransform());
|
|
||||||
this->renderManager->setRenderFlags(RENDER_MANAGER_RENDER_FLAG_DEPTH_TEST);
|
|
||||||
|
|
||||||
auto it2 = uiCanvasList.begin();
|
|
||||||
while(it2 != uiCanvasList.end()) {
|
|
||||||
auto canvas = *it2;
|
|
||||||
|
|
||||||
switch(canvas->drawType) {
|
|
||||||
case UI_DRAW_TYPE_WORLD_ABSOLUTE:
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
++it2;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto it3 = canvas->children.begin();
|
|
||||||
auto rootMatrix = canvas->transform->getWorldTransform();
|
|
||||||
while(it3 != canvas->children.end()) {
|
|
||||||
(*it3)->draw(uiShader,
|
|
||||||
glm::translate(glm::scale(rootMatrix, glm::vec3(1.0f, -1.0f, 1.0f)), glm::vec3(0, -renderTarget->getHeight(), 0))
|
|
||||||
);
|
|
||||||
++it3;
|
|
||||||
}
|
|
||||||
++it2;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Now render camera-relative UI
|
|
||||||
this->renderManager->setRenderFlags(
|
|
||||||
RENDER_MANAGER_RENDER_FLAG_BLEND
|
|
||||||
);
|
|
||||||
uiShader->setUICamera(
|
|
||||||
glm::ortho(0.0f, renderTarget->getWidth(), renderTarget->getHeight(), 0.0f),
|
|
||||||
glm::mat4(1.0f)
|
|
||||||
);
|
|
||||||
|
|
||||||
it2 = uiCanvasList.begin();
|
|
||||||
while(it2 != uiCanvasList.end()) {
|
|
||||||
auto canvas = *it2;
|
|
||||||
|
|
||||||
switch(canvas->drawType) {
|
|
||||||
case UI_DRAW_TYPE_WORLD_CAMERA_RELATIVE:
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
++it2;
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto it3 = canvas->children.begin();
|
// Now set each of the parameters. Nothing exciting here.
|
||||||
auto rootMatrix = canvas->transform->getWorldTransform();
|
auto itColors = item.pass.colorValues.begin();
|
||||||
while(it3 != canvas->children.end()) {
|
while(itColors != item.pass.colorValues.end()) {
|
||||||
(*it3)->draw(uiShader, rootMatrix);
|
item.pass.shaderProgram->setColor(itColors->first, itColors->second);
|
||||||
++it3;
|
++itColors;
|
||||||
}
|
}
|
||||||
++it2;
|
|
||||||
|
auto itBool = item.pass.boolValues.begin();
|
||||||
|
while(itBool != item.pass.boolValues.end()) {
|
||||||
|
item.pass.shaderProgram->setBoolean(itBool->first, itBool->second);
|
||||||
|
++itBool;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto itMat = item.pass.matrixValues.begin();
|
||||||
|
while(itMat != item.pass.matrixValues.end()) {
|
||||||
|
item.pass.shaderProgram->setMatrix(itMat->first, itMat->second);
|
||||||
|
++itMat;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto itVec3 = item.pass.vec3Values.begin();
|
||||||
|
while(itVec3 != item.pass.vec3Values.end()) {
|
||||||
|
item.pass.shaderProgram->setVector3(itVec3->first, itVec3->second);
|
||||||
|
++itVec3;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto itText = item.pass.textureValues.begin();
|
||||||
|
while(itText != item.pass.textureValues.end()) {
|
||||||
|
item.pass.shaderProgram->setTexture(itText->first, itText->second);
|
||||||
|
++itText;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto itFloat = item.pass.floatValues.begin();
|
||||||
|
while(itFloat != item.pass.floatValues.end()) {
|
||||||
|
item.pass.shaderProgram->setFloat(itFloat->first, itFloat->second);
|
||||||
|
++itFloat;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Thank god that's done, now just draw the damn mesh.
|
||||||
|
item.mesh->mesh->draw(MESH_DRAW_MODE_TRIANGLES, 0, -1);
|
||||||
|
++itItems;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,14 +5,25 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "dawnlibs.hpp"
|
#include "dawnlibs.hpp"
|
||||||
#include "display/RenderManager.hpp"
|
#include "scene/SceneItem.hpp"
|
||||||
#include "scene/Scene.hpp"
|
#include "scene/Scene.hpp"
|
||||||
#include "scene/components/Components.hpp"
|
#include "scene/components/display/Material.hpp"
|
||||||
#include "scene/components/ui/UICanvas.hpp"
|
#include "scene/components/display/MeshRenderer.hpp"
|
||||||
#include "ui/UIComponent.hpp"
|
#include "scene/components/display/Camera.hpp"
|
||||||
|
|
||||||
namespace Dawn {
|
namespace Dawn {
|
||||||
|
class RenderManager;
|
||||||
|
|
||||||
|
struct RenderPipelineItem {
|
||||||
|
MeshRenderer *mesh;
|
||||||
|
struct ShaderPass pass;
|
||||||
|
float_t w;
|
||||||
|
};
|
||||||
|
|
||||||
class RenderPipeline {
|
class RenderPipeline {
|
||||||
|
private:
|
||||||
|
int_fast16_t renderId;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
RenderManager *renderManager;
|
RenderManager *renderManager;
|
||||||
|
|
||||||
|
65
src/dawn/display/shader/Shader.hpp
Normal file
65
src/dawn/display/shader/Shader.hpp
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
// Copyright (c) 2023 Dominic Masters
|
||||||
|
//
|
||||||
|
// This software is released under the MIT License.
|
||||||
|
// https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "display/shader/ShaderProgram.hpp"
|
||||||
|
#include "scene/components/display/MeshRenderer.hpp"
|
||||||
|
|
||||||
|
namespace Dawn {
|
||||||
|
class Material;
|
||||||
|
|
||||||
|
struct ShaderPass {
|
||||||
|
ShaderProgram *shaderProgram;
|
||||||
|
int32_t orderShader;
|
||||||
|
bool_t needsW;
|
||||||
|
|
||||||
|
// Parameters
|
||||||
|
std::map<shaderparameter_t, struct Color> colorValues;
|
||||||
|
std::map<shaderparameter_t, bool_t> boolValues;
|
||||||
|
std::map<shaderparameter_t, glm::mat4> matrixValues;
|
||||||
|
std::map<shaderparameter_t, glm::vec3> vec3Values;
|
||||||
|
std::map<shaderparameter_t, textureslot_t> textureValues;
|
||||||
|
std::map<shaderparameter_t, float_t> floatValues;
|
||||||
|
|
||||||
|
// Textures
|
||||||
|
std::map<textureslot_t, Texture*> textureSlots;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Shader {
|
||||||
|
public:
|
||||||
|
int_fast16_t renderId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compile all programs for this shader. This amy not remain forever as I
|
||||||
|
* may decide to allow shaders to share programs somehow. For now though
|
||||||
|
* this is clean enough.
|
||||||
|
*/
|
||||||
|
virtual void compile() = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the list of passes to render for the given scene item.
|
||||||
|
*
|
||||||
|
* @param mesh Mesh Renderer for the scene item.
|
||||||
|
* @param material Material for the scene item.
|
||||||
|
* @return List of passes to render.
|
||||||
|
*/
|
||||||
|
virtual std::vector<struct ShaderPass> getItemPasses(
|
||||||
|
MeshRenderer *mesh,
|
||||||
|
Material *material
|
||||||
|
) = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called once per frame, set the global shader parameters that is used by
|
||||||
|
* every item in the scene.
|
||||||
|
*
|
||||||
|
* @param cameraProjection Projection matrix of the camera.
|
||||||
|
* @param cameraView View matrix of the camera.
|
||||||
|
*/
|
||||||
|
virtual void setGlobalParameters(
|
||||||
|
glm::mat4 cameraProjection,
|
||||||
|
glm::mat4 cameraView
|
||||||
|
) = 0;
|
||||||
|
};
|
||||||
|
}
|
@ -1,115 +0,0 @@
|
|||||||
// Copyright (c) 2022 Dominic Masters
|
|
||||||
//
|
|
||||||
// This software is released under the MIT License.
|
|
||||||
// https://opensource.org/licenses/MIT
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
#include "display/Texture.hpp"
|
|
||||||
|
|
||||||
namespace Dawn {
|
|
||||||
class Material;
|
|
||||||
|
|
||||||
enum ShaderParameterType {
|
|
||||||
SHADER_PARAMETER_TYPE_MATRIX,
|
|
||||||
SHADER_PARAMETER_TYPE_BOOLEAN,
|
|
||||||
SHADER_PARAMETER_TYPE_COLOR,
|
|
||||||
SHADER_PARAMETER_TYPE_VECTOR3,
|
|
||||||
SHADER_PARAMETER_TYPE_TEXTURE,
|
|
||||||
SHADER_PARAMETER_TYPE_FLOAT
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
class IShader {
|
|
||||||
public:
|
|
||||||
/**
|
|
||||||
* Attaches the supplied shader as the current shader.
|
|
||||||
*/
|
|
||||||
virtual void bind() = 0;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Requested by the Material to set the default parameters of the shader.
|
|
||||||
* Each parameter really should have a default value set so that there is
|
|
||||||
* no nullptr's or other issues.
|
|
||||||
*
|
|
||||||
* @param material Material to set the default parameters on to.
|
|
||||||
*/
|
|
||||||
virtual void setDefaultParameters(Material *material) = 0;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Requested by the render pipeline (typically) to set global level (once
|
|
||||||
* per frame) parameters.
|
|
||||||
*
|
|
||||||
* @param projection Projection matrix of the current viewport.
|
|
||||||
* @param view View matrix of the current viewport.
|
|
||||||
*/
|
|
||||||
virtual void setGlobalParameters(
|
|
||||||
glm::mat4 projection,
|
|
||||||
glm::mat4 view
|
|
||||||
) = 0;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Requested by the render pipeline (typically) to set mesh-level params.
|
|
||||||
* This may be performed multiple times per frame.
|
|
||||||
*
|
|
||||||
* @param position Matrix of the position of the mesh.
|
|
||||||
*/
|
|
||||||
virtual void setMeshParameters(glm::mat4 position) = 0;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retreives the list of all parameters that the shader supports. This
|
|
||||||
* should not include the GLOBAL shader parameters (listed above) since
|
|
||||||
* those will be modified by the engine directly.
|
|
||||||
*
|
|
||||||
* @return Key-Value-Pair of Shader parameters and their type.
|
|
||||||
*/
|
|
||||||
virtual std::map<T, enum ShaderParameterType> getParameters() = 0;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set's a specific shader parameter to a matrix.
|
|
||||||
*
|
|
||||||
* @param parameter parameter on the shader to set.
|
|
||||||
* @param matrix Matrix to apply.
|
|
||||||
*/
|
|
||||||
virtual void setMatrix(T parameter, glm::mat4 matrix) = 0;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Attaches a boolean to a shader.
|
|
||||||
*
|
|
||||||
* @param parameter parameter to set.
|
|
||||||
* @param value Value to set.
|
|
||||||
*/
|
|
||||||
virtual void setBoolean(T parameter, bool_t value) = 0;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set a color on to the shader.
|
|
||||||
*
|
|
||||||
* @param parameter parameter to set the color to.
|
|
||||||
* @param color Color to set.
|
|
||||||
*/
|
|
||||||
virtual void setColor(T parameter, struct Color color) = 0;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set a 3D vector on to the shader.
|
|
||||||
*
|
|
||||||
* @param parameter parameter to set the vector to.
|
|
||||||
* @param vector Vector to set.
|
|
||||||
*/
|
|
||||||
virtual void setVector3(T parameter, glm::vec3 vector) = 0;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Attaches a texture to the currently bound shader.
|
|
||||||
*
|
|
||||||
* @param parameter parameter to set the texture on to.
|
|
||||||
* @param texture Texture to bind to the parameter.
|
|
||||||
*/
|
|
||||||
virtual void setTexture(T parameter, Texture *texture) = 0;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets a floating point value to the shader.
|
|
||||||
*
|
|
||||||
* @param parameter Paramater to set the float ont o.
|
|
||||||
* @param Float to bind.
|
|
||||||
*/
|
|
||||||
virtual void setFloat(T parameter, float_t value) = 0;
|
|
||||||
};
|
|
||||||
}
|
|
76
src/dawn/display/shader/_ShaderProgram.hpp
Normal file
76
src/dawn/display/shader/_ShaderProgram.hpp
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
// Copyright (c) 2022 Dominic Masters
|
||||||
|
//
|
||||||
|
// This software is released under the MIT License.
|
||||||
|
// https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "display/Texture.hpp"
|
||||||
|
|
||||||
|
namespace Dawn {
|
||||||
|
// class Material;
|
||||||
|
// enum ShaderParameterType {
|
||||||
|
// SHADER_PARAMETER_TYPE_MATRIX,
|
||||||
|
// SHADER_PARAMETER_TYPE_BOOLEAN,
|
||||||
|
// SHADER_PARAMETER_TYPE_COLOR,
|
||||||
|
// SHADER_PARAMETER_TYPE_VECTOR3,
|
||||||
|
// SHADER_PARAMETER_TYPE_TEXTURE,
|
||||||
|
// SHADER_PARAMETER_TYPE_FLOAT
|
||||||
|
// };
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
class IShaderProgram {
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* Attaches the supplied shader as the current shader.
|
||||||
|
*/
|
||||||
|
virtual void bind() = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set's a specific shader parameter to a matrix.
|
||||||
|
*
|
||||||
|
* @param parameter parameter on the shader to set.
|
||||||
|
* @param matrix Matrix to apply.
|
||||||
|
*/
|
||||||
|
virtual void setMatrix(T parameter, glm::mat4 matrix) = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attaches a boolean to a shader.
|
||||||
|
*
|
||||||
|
* @param parameter parameter to set.
|
||||||
|
* @param value Value to set.
|
||||||
|
*/
|
||||||
|
virtual void setBoolean(T parameter, bool_t value) = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set a color on to the shader.
|
||||||
|
*
|
||||||
|
* @param parameter parameter to set the color to.
|
||||||
|
* @param color Color to set.
|
||||||
|
*/
|
||||||
|
virtual void setColor(T parameter, struct Color color) = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set a 3D vector on to the shader.
|
||||||
|
*
|
||||||
|
* @param parameter parameter to set the vector to.
|
||||||
|
* @param vector Vector to set.
|
||||||
|
*/
|
||||||
|
virtual void setVector3(T parameter, glm::vec3 vector) = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attaches a texture to the currently bound shader.
|
||||||
|
*
|
||||||
|
* @param parameter parameter to set the texture on to.
|
||||||
|
* @param texture Texture slot to bind to the parameter.
|
||||||
|
*/
|
||||||
|
virtual void setTexture(T parameter, textureslot_t texture) = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a floating point value to the shader.
|
||||||
|
*
|
||||||
|
* @param parameter Paramater to set the float ont o.
|
||||||
|
* @param Float to bind.
|
||||||
|
*/
|
||||||
|
virtual void setFloat(T parameter, float_t value) = 0;
|
||||||
|
};
|
||||||
|
}
|
@ -10,61 +10,4 @@
|
|||||||
using namespace Dawn;
|
using namespace Dawn;
|
||||||
|
|
||||||
Material::Material(SceneItem *item) : SceneItemComponent(item) {
|
Material::Material(SceneItem *item) : SceneItemComponent(item) {
|
||||||
this->shader = item->scene->game->renderManager.getDefaultShader();
|
|
||||||
this->updateShaderParameters();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Material::updateShaderParameters() {
|
|
||||||
this->colorValues.clear();
|
|
||||||
this->boolValues.clear();
|
|
||||||
|
|
||||||
this->parameters = this->shader->getParameters();
|
|
||||||
this->shader->setDefaultParameters(this);
|
|
||||||
|
|
||||||
// We do need to validate these params at some point to make sure that the
|
|
||||||
// shader has actually bound them.
|
|
||||||
}
|
|
||||||
|
|
||||||
void Material::setShaderParameters() {
|
|
||||||
auto it = this->parameters.begin();
|
|
||||||
while(it != this->parameters.end()) {
|
|
||||||
switch(it->second) {
|
|
||||||
case SHADER_PARAMETER_TYPE_COLOR:
|
|
||||||
this->shader->setColor(it->first, this->colorValues[it->first]);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case SHADER_PARAMETER_TYPE_MATRIX:
|
|
||||||
this->shader->setMatrix(it->first, this->matrixValues[it->first]);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case SHADER_PARAMETER_TYPE_BOOLEAN:
|
|
||||||
this->shader->setBoolean(it->first, this->boolValues[it->first]);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case SHADER_PARAMETER_TYPE_VECTOR3:
|
|
||||||
this->shader->setVector3(it->first, this->vec3Values[it->first]);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case SHADER_PARAMETER_TYPE_TEXTURE:
|
|
||||||
this->shader->setTexture(it->first, this->textureValues[it->first]);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case SHADER_PARAMETER_TYPE_FLOAT:
|
|
||||||
this->shader->setFloat(it->first, this->floatValues[it->first]);
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
throw "An unsupported or invalid shader parameter type was supplied.";
|
|
||||||
}
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Shader * Material::getShader() {
|
|
||||||
return this->shader;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Material::setShader(Shader * shader) {
|
|
||||||
this->shader = shader;
|
|
||||||
this->updateShaderParameters();
|
|
||||||
}
|
}
|
@ -8,56 +8,20 @@
|
|||||||
#include "display/shader/Shader.hpp"
|
#include "display/shader/Shader.hpp"
|
||||||
|
|
||||||
namespace Dawn {
|
namespace Dawn {
|
||||||
class Shader;
|
|
||||||
|
|
||||||
class Material : public SceneItemComponent {
|
class Material : public SceneItemComponent {
|
||||||
private:
|
|
||||||
Shader *shader = nullptr;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Internal method that will be invoked to go through and update all of
|
|
||||||
* the shader parameters whenever the shader is swapped out.
|
|
||||||
*/
|
|
||||||
void updateShaderParameters();
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
std::map<shaderparameter_t, enum ShaderParameterType> parameters;
|
|
||||||
std::map<shaderparameter_t, struct Color> colorValues;
|
|
||||||
std::map<shaderparameter_t, bool_t> boolValues;
|
|
||||||
std::map<shaderparameter_t, glm::mat4> matrixValues;
|
|
||||||
std::map<shaderparameter_t, glm::vec3> vec3Values;
|
|
||||||
std::map<shaderparameter_t, Texture*> textureValues;
|
|
||||||
std::map<shaderparameter_t, float_t> floatValues;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Material component constructor.
|
* Material component constructor.
|
||||||
*
|
*
|
||||||
* @param item Scene Item this component belongs to.
|
* @param item Scene Item this component belongs to.
|
||||||
*/
|
*/
|
||||||
Material(SceneItem *item);
|
Material(SceneItem *item);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the shader this material is currently using.
|
* Returns the shader that this material uses.
|
||||||
*
|
*
|
||||||
* @return Shader pointer to the currently bound shader.
|
* @return Shader that belongs to this material.
|
||||||
*/
|
*/
|
||||||
Shader * getShader();
|
virtual Shader * getShader() = 0;
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the shader for the material to use. This will also clear and
|
|
||||||
* update all of the parameters from the shaders' default parameter list.
|
|
||||||
*
|
|
||||||
* @param shader Shader to set.
|
|
||||||
*/
|
|
||||||
void setShader(Shader * shader);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Protected method that can be called, likely by the render pipeline, to
|
|
||||||
* set and update all of the shader parameters from this material on to
|
|
||||||
* the shader.
|
|
||||||
*
|
|
||||||
* This method assumes that the shader has already been bound.
|
|
||||||
*/
|
|
||||||
void setShaderParameters();
|
|
||||||
};
|
};
|
||||||
}
|
}
|
@ -1,60 +0,0 @@
|
|||||||
// Copyright (c) 2023 Dominic Masters
|
|
||||||
//
|
|
||||||
// This software is released under the MIT License.
|
|
||||||
// https://opensource.org/licenses/MIT
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
#include "scene/components/display/Material.hpp"
|
|
||||||
#include "scene/SceneItem.hpp"
|
|
||||||
|
|
||||||
namespace Dawn {
|
|
||||||
template<class T>
|
|
||||||
class ShaderInterface : public SceneItemComponent {
|
|
||||||
public:
|
|
||||||
/**
|
|
||||||
* ShaderInterface to provide a common interface language for all shaders
|
|
||||||
* that can be referenced by scene components.
|
|
||||||
*
|
|
||||||
* @param i SceneItem this Shader interface belongs to.
|
|
||||||
*/
|
|
||||||
ShaderInterface(SceneItem *i) : SceneItemComponent(i) {}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the shader for this shader interface to use. Will also update the
|
|
||||||
* underlying material for you.
|
|
||||||
*
|
|
||||||
* @param shader Shader to use for this interface.
|
|
||||||
*/
|
|
||||||
void setShader(T *shader) {
|
|
||||||
assertNotNull(shader);
|
|
||||||
this->getMaterial()->setShader(shader);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the shader, assuming that the material has the correct shader.
|
|
||||||
*
|
|
||||||
* @return Pointer to the shader.
|
|
||||||
*/
|
|
||||||
T * getShader() {
|
|
||||||
auto material = dynamic_cast<T*>(this->getMaterial()->getShader());
|
|
||||||
return material;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the material attached to this scene item.
|
|
||||||
*
|
|
||||||
* @return The attached material.
|
|
||||||
*/
|
|
||||||
Material * getMaterial() {
|
|
||||||
auto mat = this->item->getComponent<Material>();
|
|
||||||
assertNotNull(mat);
|
|
||||||
return mat;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<SceneItemComponent*> getDependencies() override {
|
|
||||||
return std::vector<SceneItemComponent*>{
|
|
||||||
this->item->getComponent<Material>()
|
|
||||||
};
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
@ -8,7 +8,6 @@ target_sources(${DAWN_TARGET_NAME}
|
|||||||
PRIVATE
|
PRIVATE
|
||||||
RenderManager.cpp
|
RenderManager.cpp
|
||||||
BackBufferRenderTarget.cpp
|
BackBufferRenderTarget.cpp
|
||||||
StandardRenderPipeline.cpp
|
|
||||||
Texture.cpp
|
Texture.cpp
|
||||||
TextureRenderTarget.cpp
|
TextureRenderTarget.cpp
|
||||||
)
|
)
|
||||||
|
@ -6,21 +6,20 @@
|
|||||||
#include "dawnopengl.hpp"
|
#include "dawnopengl.hpp"
|
||||||
#include "game/DawnGame.hpp"
|
#include "game/DawnGame.hpp"
|
||||||
#include "display/RenderManager.hpp"
|
#include "display/RenderManager.hpp"
|
||||||
#include "display/StandardRenderPipeline.hpp"
|
|
||||||
|
|
||||||
using namespace Dawn;
|
using namespace Dawn;
|
||||||
|
|
||||||
RenderManager::RenderManager(DawnGame *game) :
|
RenderManager::RenderManager(DawnGame *game) :
|
||||||
IRenderManager(game),
|
IRenderManager(game),
|
||||||
backBuffer(*this)
|
backBuffer(*this),
|
||||||
|
renderPipeline(this)
|
||||||
{
|
{
|
||||||
this->standardRenderPipeline = new StandardRenderPipeline(this);
|
|
||||||
this->simpleShader = new SimpleTexturedShader();
|
this->simpleShader = new SimpleTexturedShader();
|
||||||
this->uiShader = new UIShader();
|
this->uiShader = new UIShader();
|
||||||
}
|
}
|
||||||
|
|
||||||
void RenderManager::init() {
|
void RenderManager::init() {
|
||||||
this->standardRenderPipeline->init();
|
this->renderPipeline.init();
|
||||||
this->simpleShader->compile();
|
this->simpleShader->compile();
|
||||||
this->uiShader->compile();
|
this->uiShader->compile();
|
||||||
|
|
||||||
@ -36,7 +35,7 @@ RenderTarget * RenderManager::getBackBuffer() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
RenderPipeline * RenderManager::getRenderPipeline() {
|
RenderPipeline * RenderManager::getRenderPipeline() {
|
||||||
return this->standardRenderPipeline;
|
return &this->renderPipeline;
|
||||||
}
|
}
|
||||||
|
|
||||||
Shader * RenderManager::getDefaultShader() {
|
Shader * RenderManager::getDefaultShader() {
|
||||||
@ -68,7 +67,6 @@ void RenderManager::update() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
RenderManager::~RenderManager() {
|
RenderManager::~RenderManager() {
|
||||||
delete this->standardRenderPipeline;
|
|
||||||
delete this->simpleShader;
|
delete this->simpleShader;
|
||||||
delete this->uiShader;
|
delete this->uiShader;
|
||||||
}
|
}
|
@ -7,13 +7,12 @@
|
|||||||
#include "display/_RenderManager.hpp"
|
#include "display/_RenderManager.hpp"
|
||||||
#include "display/BackBufferRenderTarget.hpp"
|
#include "display/BackBufferRenderTarget.hpp"
|
||||||
#include "display/shader/SimpleTexturedShader.hpp"
|
#include "display/shader/SimpleTexturedShader.hpp"
|
||||||
|
#include "display/RenderPipeline.hpp"
|
||||||
|
|
||||||
namespace Dawn {
|
namespace Dawn {
|
||||||
class StandardRenderPipeline;
|
|
||||||
|
|
||||||
class RenderManager : public IRenderManager {
|
class RenderManager : public IRenderManager {
|
||||||
private:
|
private:
|
||||||
StandardRenderPipeline *standardRenderPipeline;
|
RenderPipeline renderPipeline;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
BackBufferRenderTarget backBuffer;
|
BackBufferRenderTarget backBuffer;
|
||||||
|
@ -1,14 +0,0 @@
|
|||||||
// Copyright (c) 2022 Dominic Masters
|
|
||||||
//
|
|
||||||
// This software is released under the MIT License.
|
|
||||||
// https://opensource.org/licenses/MIT
|
|
||||||
|
|
||||||
#include "StandardRenderPipeline.hpp"
|
|
||||||
#include "scene/components/Components.hpp"
|
|
||||||
|
|
||||||
using namespace Dawn;
|
|
||||||
|
|
||||||
StandardRenderPipeline::StandardRenderPipeline(RenderManager *renderManager) :
|
|
||||||
RenderPipeline(renderManager)
|
|
||||||
{
|
|
||||||
}
|
|
@ -1,15 +0,0 @@
|
|||||||
// Copyright (c) 2022 Dominic Masters
|
|
||||||
//
|
|
||||||
// This software is released under the MIT License.
|
|
||||||
// https://opensource.org/licenses/MIT
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
#include "dawnopengl.hpp"
|
|
||||||
#include "display/RenderPipeline.hpp"
|
|
||||||
|
|
||||||
namespace Dawn {
|
|
||||||
class StandardRenderPipeline : public RenderPipeline {
|
|
||||||
public:
|
|
||||||
StandardRenderPipeline(RenderManager *renderManager);
|
|
||||||
};
|
|
||||||
}
|
|
@ -6,5 +6,5 @@
|
|||||||
# Sources
|
# Sources
|
||||||
target_sources(${DAWN_TARGET_NAME}
|
target_sources(${DAWN_TARGET_NAME}
|
||||||
PRIVATE
|
PRIVATE
|
||||||
Shader.cpp
|
ShaderProgram.cpp
|
||||||
)
|
)
|
@ -3,11 +3,11 @@
|
|||||||
// This software is released under the MIT License.
|
// This software is released under the MIT License.
|
||||||
// https://opensource.org/licenses/MIT
|
// https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
#include "Shader.hpp"
|
#include "ShaderProgram.hpp"
|
||||||
|
|
||||||
using namespace Dawn;
|
using namespace Dawn;
|
||||||
|
|
||||||
void Shader::compileShader(
|
void ShaderProgram::compileShader(
|
||||||
std::string vertexShader,
|
std::string vertexShader,
|
||||||
std::string fragmentShader
|
std::string fragmentShader
|
||||||
) {
|
) {
|
||||||
@ -61,51 +61,40 @@ void Shader::compileShader(
|
|||||||
// Now parse out the variables.
|
// Now parse out the variables.
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::setTextureSlot(shaderparameter_t param, textureslot_t slot) {
|
void ShaderProgram::setTexture(shaderparameter_t param, textureslot_t slot) {
|
||||||
glUniform1i(param, slot);
|
glUniform1i(param, slot);
|
||||||
}
|
}
|
||||||
|
|
||||||
shaderparameter_t Shader::getParameterByName(std::string name) {
|
shaderparameter_t ShaderProgram::getParameterByName(std::string name) {
|
||||||
return glGetUniformLocation(this->shaderProgram, name.c_str());
|
return glGetUniformLocation(this->shaderProgram, name.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::setMatrix(shaderparameter_t uniform, glm::mat4 matrix) {
|
void ShaderProgram::setMatrix(shaderparameter_t uniform, glm::mat4 matrix) {
|
||||||
glUniformMatrix4fv(uniform, 1, GL_FALSE, glm::value_ptr(matrix));
|
glUniformMatrix4fv(uniform, 1, GL_FALSE, glm::value_ptr(matrix));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::setBoolean(shaderparameter_t uni, bool value) {
|
void ShaderProgram::setBoolean(shaderparameter_t uni, bool value) {
|
||||||
glUniform1i(uni, value);
|
glUniform1i(uni, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::setColor(shaderparameter_t uniform, struct Color color) {
|
void ShaderProgram::setColor(shaderparameter_t uniform, struct Color color) {
|
||||||
glUniform4f(uniform, color.r, color.g, color.b, color.a);
|
glUniform4f(uniform, color.r, color.g, color.b, color.a);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::setVector3(shaderparameter_t uniform, glm::vec3 vector) {
|
void ShaderProgram::setVector3(shaderparameter_t uniform, glm::vec3 vector) {
|
||||||
glUniform3f(uniform, vector.x, vector.y, vector.z);
|
glUniform3f(uniform, vector.x, vector.y, vector.z);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::setTexture(
|
void ShaderProgram::setFloat(shaderparameter_t param, float_t value) {
|
||||||
shaderparameter_t param,
|
|
||||||
Texture *texture
|
|
||||||
) {
|
|
||||||
if(texture == nullptr || !texture->isReady()) {
|
|
||||||
this->bindTexture(param, nullptr);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this->bindTexture(param, texture);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Shader::setFloat(shaderparameter_t param, float_t value) {
|
|
||||||
glUniform1f(param, value);
|
glUniform1f(param, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::bind() {
|
void ShaderProgram::bind() {
|
||||||
if(this->shaderProgram == -1) throw "Shader has not yet been compiled";
|
if(this->shaderProgram == -1) throw "Shader has not yet been compiled";
|
||||||
glUseProgram(this->shaderProgram);
|
glUseProgram(this->shaderProgram);
|
||||||
}
|
}
|
||||||
|
|
||||||
Shader::~Shader() {
|
ShaderProgram::~ShaderProgram() {
|
||||||
if(this->shaderProgram != -1) glDeleteProgram(this->shaderProgram);
|
if(this->shaderProgram != -1) glDeleteProgram(this->shaderProgram);
|
||||||
if(this->shaderVertex != -1) glDeleteShader(this->shaderVertex);
|
if(this->shaderVertex != -1) glDeleteShader(this->shaderVertex);
|
||||||
if(this->shaderFrag != -1) glDeleteShader(this->shaderFrag);
|
if(this->shaderFrag != -1) glDeleteShader(this->shaderFrag);
|
@ -4,14 +4,14 @@
|
|||||||
// https://opensource.org/licenses/MIT
|
// https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "display/shader/_Shader.hpp"
|
#include "display/shader/_ShaderProgram.hpp"
|
||||||
#include "dawnopengl.hpp"
|
#include "dawnopengl.hpp"
|
||||||
#include "display/Color.hpp"
|
#include "display/Color.hpp"
|
||||||
|
|
||||||
typedef GLuint shaderparameter_t;
|
typedef GLuint shaderparameter_t;
|
||||||
|
|
||||||
namespace Dawn {
|
namespace Dawn {
|
||||||
class Shader : public IShader<shaderparameter_t> {
|
class ShaderProgram : public IShaderProgram<shaderparameter_t> {
|
||||||
private:
|
private:
|
||||||
/** Pointer to an uploaded vertex shader program */
|
/** Pointer to an uploaded vertex shader program */
|
||||||
GLuint shaderVertex = -1;
|
GLuint shaderVertex = -1;
|
||||||
@ -32,28 +32,6 @@ namespace Dawn {
|
|||||||
*/
|
*/
|
||||||
void compileShader(std::string vertexShader, std::string fragmentShader);
|
void compileShader(std::string vertexShader, std::string fragmentShader);
|
||||||
|
|
||||||
/**
|
|
||||||
* Bind a specific texture slot (of an already bound texture) to a shader
|
|
||||||
* parameter.
|
|
||||||
*
|
|
||||||
* @param param Parameter to bind the texture slot for.
|
|
||||||
* @param slot Slot to bind.
|
|
||||||
*/
|
|
||||||
void setTextureSlot(shaderparameter_t param, textureslot_t slot);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Method designed to be overwritten by child shaders on how to handle a
|
|
||||||
* bind texture request. This is left to the discretion of the child
|
|
||||||
* shader for which slot(s) to use, how to handle nullptr textures, etc.
|
|
||||||
*
|
|
||||||
* @param param Parameter to bind the requested texture to.
|
|
||||||
* @param texture Texture trying to be bound, may be nullptr.
|
|
||||||
*/
|
|
||||||
virtual void bindTexture(
|
|
||||||
shaderparameter_t param,
|
|
||||||
Texture *texture
|
|
||||||
) = 0;
|
|
||||||
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
@ -75,12 +53,12 @@ namespace Dawn {
|
|||||||
void setBoolean(shaderparameter_t parameter, bool_t value) override;
|
void setBoolean(shaderparameter_t parameter, bool_t value) override;
|
||||||
void setColor(shaderparameter_t parameter, struct Color color) override;
|
void setColor(shaderparameter_t parameter, struct Color color) override;
|
||||||
void setVector3(shaderparameter_t parameter, glm::vec3 vector) override;
|
void setVector3(shaderparameter_t parameter, glm::vec3 vector) override;
|
||||||
void setTexture(shaderparameter_t parameter, Texture *texture) override;
|
void setTexture(shaderparameter_t parameter, textureslot_t texture) override;
|
||||||
void setFloat(shaderparameter_t parameter, float_t value) override;
|
void setFloat(shaderparameter_t parameter, float_t value) override;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Destroys and deletes the shader from the GPU.
|
* Destroys and deletes the shader from the GPU.
|
||||||
*/
|
*/
|
||||||
virtual ~Shader();
|
virtual ~ShaderProgram();
|
||||||
};
|
};
|
||||||
}
|
}
|
@ -1,102 +1,33 @@
|
|||||||
// Copyright (c) 2022 Dominic Masters
|
// Copyright (c) 2023 Dominic Masters
|
||||||
//
|
//
|
||||||
// This software is released under the MIT License.
|
// This software is released under the MIT License.
|
||||||
// https://opensource.org/licenses/MIT
|
// https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "display/shader/Shader.hpp"
|
#include "display/shader/Shader.hpp"
|
||||||
#include "scene/components/Components.hpp"
|
#include "SimpleTexturedShaderProgram.hpp"
|
||||||
|
|
||||||
namespace Dawn {
|
namespace Dawn {
|
||||||
class SimpleTexturedShader : public Shader {
|
class SimpleTexturedShader : public Shader {
|
||||||
|
protected:
|
||||||
|
SimpleTexturedShaderProgram program;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
shaderparameter_t paramProjection;
|
|
||||||
shaderparameter_t paramView;
|
|
||||||
shaderparameter_t paramModel;
|
|
||||||
shaderparameter_t paramColor;
|
|
||||||
shaderparameter_t paramTexture;
|
|
||||||
shaderparameter_t paramHasTexture;
|
|
||||||
|
|
||||||
std::map<shaderparameter_t, enum ShaderParameterType>
|
|
||||||
getParameters() override {
|
|
||||||
std::map<shaderparameter_t, enum ShaderParameterType> ps;
|
|
||||||
|
|
||||||
ps[this->paramColor] = SHADER_PARAMETER_TYPE_COLOR;
|
|
||||||
ps[this->paramHasTexture] = SHADER_PARAMETER_TYPE_BOOLEAN;
|
|
||||||
ps[this->paramTexture] = SHADER_PARAMETER_TYPE_TEXTURE;
|
|
||||||
|
|
||||||
return ps;
|
|
||||||
}
|
|
||||||
|
|
||||||
void setDefaultParameters(Material *material) override {
|
|
||||||
material->colorValues[this->paramColor] = COLOR_WHITE;
|
|
||||||
material->textureValues[this->paramTexture] = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void setGlobalParameters(glm::mat4 proj, glm::mat4 view) override {
|
|
||||||
this->setMatrix(this->paramProjection, proj);
|
|
||||||
this->setMatrix(this->paramView, view);
|
|
||||||
}
|
|
||||||
|
|
||||||
void setMeshParameters(glm::mat4 transform) override {
|
|
||||||
this->setMatrix(this->paramModel, transform);
|
|
||||||
}
|
|
||||||
|
|
||||||
void bindTexture(
|
|
||||||
shaderparameter_t param,
|
|
||||||
Texture *texture
|
|
||||||
) override {
|
|
||||||
if(texture == nullptr || !texture->isReady()) {
|
|
||||||
this->setBoolean(this->paramHasTexture, false);
|
|
||||||
} else {
|
|
||||||
this->setBoolean(this->paramHasTexture, true);
|
|
||||||
this->setTextureSlot(param, 0x01);
|
|
||||||
texture->bind(0x01);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void compile() override {
|
void compile() override {
|
||||||
this->compileShader(
|
this->program.compile();
|
||||||
// Vertex Shader
|
}
|
||||||
"#version 330 core\n"
|
|
||||||
"layout (location = 0) in vec3 aPos;\n"
|
|
||||||
"layout (location = 1) in vec2 aTexCoord;\n"
|
|
||||||
|
|
||||||
"uniform mat4 u_Proj;\n"
|
std::vector<struct ShaderPass> getItemPasses(
|
||||||
"uniform mat4 u_View;\n"
|
MeshRenderer *mesh,
|
||||||
"uniform mat4 u_Model;\n"
|
Material *material
|
||||||
|
) override {
|
||||||
|
return std::vector<struct ShaderPass>();
|
||||||
|
}
|
||||||
|
|
||||||
"out vec2 o_TextCoord;\n"
|
void setGlobalParameters(
|
||||||
"void main() {\n"
|
glm::mat4 cameraProjection,
|
||||||
"gl_Position = u_Proj * u_View * u_Model * vec4(aPos, 1.0);\n"
|
glm::mat4 cameraView
|
||||||
"o_TextCoord = vec2(aTexCoord.x, aTexCoord.y);\n"
|
) override {
|
||||||
"}",
|
|
||||||
|
|
||||||
// Fragment Shader
|
|
||||||
"#version 330 core\n"
|
|
||||||
"in vec2 o_TextCoord;\n"
|
|
||||||
"out vec4 o_Color;\n"
|
|
||||||
"uniform vec4 u_Color;\n"
|
|
||||||
"uniform bool u_HasTexture;\n"
|
|
||||||
"uniform sampler2D u_Text;\n"
|
|
||||||
|
|
||||||
"void main() {\n"
|
|
||||||
"if(u_HasTexture) {\n"
|
|
||||||
"o_Color = texture(u_Text, o_TextCoord) * u_Color;\n"
|
|
||||||
"} else {\n"
|
|
||||||
"o_Color = u_Color;"
|
|
||||||
"}\n"
|
|
||||||
"}\n"
|
|
||||||
);
|
|
||||||
|
|
||||||
this->paramProjection = this->getParameterByName("u_Proj");
|
|
||||||
this->paramView = this->getParameterByName("u_View");
|
|
||||||
this->paramModel = this->getParameterByName("u_Model");
|
|
||||||
this->paramColor = this->getParameterByName("u_Color");
|
|
||||||
this->paramTexture = this->getParameterByName("u_Text");
|
|
||||||
this->paramHasTexture = this->getParameterByName("u_HasTexture");
|
|
||||||
|
|
||||||
this->setBoolean(this->paramHasTexture, false);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
@ -0,0 +1,62 @@
|
|||||||
|
// Copyright (c) 2022 Dominic Masters
|
||||||
|
//
|
||||||
|
// This software is released under the MIT License.
|
||||||
|
// https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "display/shader/Shader.hpp"
|
||||||
|
#include "scene/components/Components.hpp"
|
||||||
|
|
||||||
|
namespace Dawn {
|
||||||
|
class SimpleTexturedShaderProgram : public ShaderProgram {
|
||||||
|
public:
|
||||||
|
shaderparameter_t paramProjection;
|
||||||
|
shaderparameter_t paramView;
|
||||||
|
shaderparameter_t paramModel;
|
||||||
|
shaderparameter_t paramColor;
|
||||||
|
shaderparameter_t paramTexture;
|
||||||
|
shaderparameter_t paramHasTexture;
|
||||||
|
|
||||||
|
void compile() override {
|
||||||
|
this->compileShader(
|
||||||
|
// Vertex Shader
|
||||||
|
"#version 330 core\n"
|
||||||
|
"layout (location = 0) in vec3 aPos;\n"
|
||||||
|
"layout (location = 1) in vec2 aTexCoord;\n"
|
||||||
|
|
||||||
|
"uniform mat4 u_Proj;\n"
|
||||||
|
"uniform mat4 u_View;\n"
|
||||||
|
"uniform mat4 u_Model;\n"
|
||||||
|
|
||||||
|
"out vec2 o_TextCoord;\n"
|
||||||
|
"void main() {\n"
|
||||||
|
"gl_Position = u_Proj * u_View * u_Model * vec4(aPos, 1.0);\n"
|
||||||
|
"o_TextCoord = vec2(aTexCoord.x, aTexCoord.y);\n"
|
||||||
|
"}",
|
||||||
|
|
||||||
|
// Fragment Shader
|
||||||
|
"#version 330 core\n"
|
||||||
|
"in vec2 o_TextCoord;\n"
|
||||||
|
"out vec4 o_Color;\n"
|
||||||
|
"uniform vec4 u_Color;\n"
|
||||||
|
"uniform bool u_HasTexture;\n"
|
||||||
|
"uniform sampler2D u_Text;\n"
|
||||||
|
|
||||||
|
"void main() {\n"
|
||||||
|
"if(u_HasTexture) {\n"
|
||||||
|
"o_Color = texture(u_Text, o_TextCoord) * u_Color;\n"
|
||||||
|
"} else {\n"
|
||||||
|
"o_Color = u_Color;"
|
||||||
|
"}\n"
|
||||||
|
"}\n"
|
||||||
|
);
|
||||||
|
|
||||||
|
this->paramProjection = this->getParameterByName("u_Proj");
|
||||||
|
this->paramView = this->getParameterByName("u_View");
|
||||||
|
this->paramModel = this->getParameterByName("u_Model");
|
||||||
|
this->paramColor = this->getParameterByName("u_Color");
|
||||||
|
this->paramTexture = this->getParameterByName("u_Text");
|
||||||
|
this->paramHasTexture = this->getParameterByName("u_HasTexture");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
@ -4,116 +4,9 @@
|
|||||||
// https://opensource.org/licenses/MIT
|
// https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "display/shader/Shader.hpp"
|
#include "SimpleTexturedShader.hpp"
|
||||||
#include "scene/components/Components.hpp"
|
|
||||||
|
|
||||||
namespace Dawn {
|
namespace Dawn {
|
||||||
class UIShader : public Shader {
|
class UIShader : public SimpleTexturedShader {
|
||||||
public:
|
|
||||||
shaderparameter_t paramProjection;
|
|
||||||
shaderparameter_t paramView;
|
|
||||||
shaderparameter_t paramModel;
|
|
||||||
shaderparameter_t paramColor;
|
|
||||||
shaderparameter_t paramTexture;
|
|
||||||
shaderparameter_t paramHasTexture;
|
|
||||||
|
|
||||||
std::map<shaderparameter_t, enum ShaderParameterType>
|
|
||||||
getParameters() override {
|
|
||||||
std::map<shaderparameter_t, enum ShaderParameterType> ps;
|
|
||||||
|
|
||||||
ps[this->paramColor] = SHADER_PARAMETER_TYPE_COLOR;
|
|
||||||
ps[this->paramHasTexture] = SHADER_PARAMETER_TYPE_BOOLEAN;
|
|
||||||
ps[this->paramTexture] = SHADER_PARAMETER_TYPE_TEXTURE;
|
|
||||||
|
|
||||||
return ps;
|
|
||||||
}
|
|
||||||
|
|
||||||
void setDefaultParameters(Material *material) override {
|
|
||||||
material->colorValues[this->paramColor] = COLOR_WHITE;
|
|
||||||
material->textureValues[this->paramTexture] = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void setGlobalParameters(glm::mat4 proj, glm::mat4 view) override {
|
|
||||||
this->setMatrix(this->paramProjection, proj);
|
|
||||||
this->setMatrix(this->paramView, view);
|
|
||||||
}
|
|
||||||
|
|
||||||
void setMeshParameters(glm::mat4 transform) override {
|
|
||||||
this->setMatrix(this->paramModel, transform);
|
|
||||||
}
|
|
||||||
|
|
||||||
void bindTexture(
|
|
||||||
shaderparameter_t param,
|
|
||||||
Texture *texture
|
|
||||||
) override {
|
|
||||||
if(texture == nullptr || !texture->isReady()) {
|
|
||||||
this->setBoolean(this->paramHasTexture, false);
|
|
||||||
} else {
|
|
||||||
this->setBoolean(this->paramHasTexture, true);
|
|
||||||
this->setTextureSlot(param, 0x00);
|
|
||||||
texture->bind(0x00);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void compile() override {
|
|
||||||
this->compileShader(
|
|
||||||
// Vertex Shader
|
|
||||||
"#version 330 core\n"
|
|
||||||
"layout (location = 0) in vec3 aPos;\n"
|
|
||||||
"layout (location = 1) in vec2 aTexCoord;\n"
|
|
||||||
|
|
||||||
"uniform mat4 u_Proj;\n"
|
|
||||||
"uniform mat4 u_View;\n"
|
|
||||||
"uniform mat4 u_Model;\n"
|
|
||||||
|
|
||||||
"out vec2 o_TextCoord;\n"
|
|
||||||
"void main() {\n"
|
|
||||||
"gl_Position = u_Proj * u_View * u_Model * vec4(aPos, 1.0);\n"
|
|
||||||
"o_TextCoord = vec2(aTexCoord.x, aTexCoord.y);\n"
|
|
||||||
"}",
|
|
||||||
|
|
||||||
// Fragment Shader
|
|
||||||
"#version 330 core\n"
|
|
||||||
"in vec2 o_TextCoord;\n"
|
|
||||||
"out vec4 o_Color;\n"
|
|
||||||
"uniform vec4 u_Color;\n"
|
|
||||||
"uniform bool u_HasTexture;\n"
|
|
||||||
"uniform sampler2D u_Text;\n"
|
|
||||||
|
|
||||||
"void main() {\n"
|
|
||||||
"if(u_HasTexture) {\n"
|
|
||||||
"o_Color = texture(u_Text, o_TextCoord) * u_Color;\n"
|
|
||||||
"} else {\n"
|
|
||||||
"o_Color = u_Color;"
|
|
||||||
"}\n"
|
|
||||||
"}\n"
|
|
||||||
);
|
|
||||||
|
|
||||||
this->paramProjection = this->getParameterByName("u_Proj");
|
|
||||||
this->paramView = this->getParameterByName("u_View");
|
|
||||||
this->paramModel = this->getParameterByName("u_Model");
|
|
||||||
this->paramColor = this->getParameterByName("u_Color");
|
|
||||||
this->paramTexture = this->getParameterByName("u_Text");
|
|
||||||
this->paramHasTexture = this->getParameterByName("u_HasTexture");
|
|
||||||
|
|
||||||
this->setBoolean(this->paramHasTexture, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
void setUICamera(glm::mat4 projection, glm::mat4 view) {
|
|
||||||
this->setMatrix(this->paramProjection, projection);
|
|
||||||
this->setMatrix(this->paramView, view);
|
|
||||||
}
|
|
||||||
|
|
||||||
void setUIModel(glm::mat4 model) {
|
|
||||||
this->setMatrix(this->paramModel, model);
|
|
||||||
}
|
|
||||||
|
|
||||||
void setUITexture(Texture *texture) {
|
|
||||||
this->bindTexture(this->paramTexture, texture);
|
|
||||||
}
|
|
||||||
|
|
||||||
void setUIColor(struct Color color) {
|
|
||||||
this->setColor(this->paramColor, color);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
@ -4,4 +4,4 @@
|
|||||||
# https://opensource.org/licenses/MIT
|
# https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
# Subdirs
|
# Subdirs
|
||||||
add_subdirectory(shader)
|
add_subdirectory(material)
|
@ -6,5 +6,5 @@
|
|||||||
# Sources
|
# Sources
|
||||||
target_sources(${DAWN_TARGET_NAME}
|
target_sources(${DAWN_TARGET_NAME}
|
||||||
PRIVATE
|
PRIVATE
|
||||||
SimpleTexturedShaderInterface.cpp
|
SimpleTexturedMaterial.cpp
|
||||||
)
|
)
|
@ -0,0 +1,17 @@
|
|||||||
|
// Copyright (c) 2023 Dominic Masters
|
||||||
|
//
|
||||||
|
// This software is released under the MIT License.
|
||||||
|
// https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
#include "SimpleTexturedMaterial.hpp"
|
||||||
|
|
||||||
|
using namespace Dawn;
|
||||||
|
|
||||||
|
SimpleTexturedMaterial::SimpleTexturedMaterial(SceneItem *i) :
|
||||||
|
Material(i)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Shader * SimpleTexturedMaterial::getShader() {
|
||||||
|
return this->shader;
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
// Copyright (c) 2023 Dominic Masters
|
||||||
|
//
|
||||||
|
// This software is released under the MIT License.
|
||||||
|
// https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "display/shader/SimpleTexturedShader.hpp"
|
||||||
|
#include "scene/components/display/Material.hpp"
|
||||||
|
|
||||||
|
namespace Dawn {
|
||||||
|
class SimpleTexturedMaterial : public Material {
|
||||||
|
public:
|
||||||
|
SimpleTexturedShader *shader = nullptr;
|
||||||
|
Texture *texture = nullptr;
|
||||||
|
struct Color color = COLOR_WHITE;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SimpleTexturedShader scene item component interface.
|
||||||
|
*
|
||||||
|
* @param i Scene Item this interface belongs to.
|
||||||
|
*/
|
||||||
|
SimpleTexturedMaterial(SceneItem *i);
|
||||||
|
|
||||||
|
Shader * getShader() override;
|
||||||
|
};
|
||||||
|
}
|
@ -1,26 +0,0 @@
|
|||||||
// Copyright (c) 2023 Dominic Masters
|
|
||||||
//
|
|
||||||
// This software is released under the MIT License.
|
|
||||||
// https://opensource.org/licenses/MIT
|
|
||||||
|
|
||||||
#include "SimpleTexturedShaderInterface.hpp"
|
|
||||||
|
|
||||||
using namespace Dawn;
|
|
||||||
|
|
||||||
SimpleTexturedShaderInterface::SimpleTexturedShaderInterface(SceneItem *i) :
|
|
||||||
ShaderInterface<SimpleTexturedShader>(i)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void SimpleTexturedShaderInterface::setTexture(Texture *texture) {
|
|
||||||
this->getMaterial()->textureValues[this->getShader()->paramTexture] = texture;
|
|
||||||
}
|
|
||||||
|
|
||||||
void SimpleTexturedShaderInterface::setColor(struct Color color) {
|
|
||||||
this->getMaterial()->colorValues[this->getShader()->paramColor] = color;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Color SimpleTexturedShaderInterface::getColor() {
|
|
||||||
return this->getMaterial()->colorValues[this->getShader()->paramColor];
|
|
||||||
}
|
|
@ -1,43 +0,0 @@
|
|||||||
// Copyright (c) 2023 Dominic Masters
|
|
||||||
//
|
|
||||||
// This software is released under the MIT License.
|
|
||||||
// https://opensource.org/licenses/MIT
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
#include "scene/components/display/ShaderInterface.hpp"
|
|
||||||
#include "display/shader/SimpleTexturedShader.hpp"
|
|
||||||
|
|
||||||
namespace Dawn {
|
|
||||||
class SimpleTexturedShaderInterface :
|
|
||||||
public ShaderInterface<SimpleTexturedShader>
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
/**
|
|
||||||
* SimpleTexturedShader scene item component interface.
|
|
||||||
*
|
|
||||||
* @param i Scene Item this interface belongs to.
|
|
||||||
*/
|
|
||||||
SimpleTexturedShaderInterface(SceneItem *i);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the primary colour texture to be used by the shader.
|
|
||||||
*
|
|
||||||
* @param texture Texture to use.
|
|
||||||
*/
|
|
||||||
void setTexture(Texture *texture);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the multiplicitive color to be used by the shader.
|
|
||||||
*
|
|
||||||
* @param color Color to be used.
|
|
||||||
*/
|
|
||||||
void setColor(struct Color color);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the current color from the shader.
|
|
||||||
*
|
|
||||||
* @return Current color.
|
|
||||||
*/
|
|
||||||
struct Color getColor();
|
|
||||||
};
|
|
||||||
}
|
|
Reference in New Issue
Block a user