Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Commit

Permalink
add shader args
Browse files Browse the repository at this point in the history
  • Loading branch information
NeilKleistGao committed Jul 8, 2021
1 parent 569a843 commit bd9b2da
Show file tree
Hide file tree
Showing 27 changed files with 246 additions and 10 deletions.
10 changes: 10 additions & 0 deletions kernel/components/state_machine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,14 @@ void StateMachine::initArgument(luabridge::LuaRef& ref, const typename resources
}
}

luabridge::LuaRef StateMachine::getComponent(Object* object, const std::string& name) {
auto machine = object->getComponent<StateMachine>(name);
if (machine == nullptr) {
return luabridge::LuaRef(script::LuaState::getInstance()->getState());
}
else {
return machine->_instance;
}
}

} // namespace ngind::components
3 changes: 3 additions & 0 deletions kernel/components/state_machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ class StateMachine : public Component {
inline luabridge::LuaRef getInstance() {
return _instance;
}

static luabridge::LuaRef getComponent(Object* object, const std::string& name);
private:
/**
* The instance of this component in lua environment.
Expand Down Expand Up @@ -154,6 +156,7 @@ NGIND_LUA_BRIDGE_REGISTRATION(StateMachine) {
.addFunction("halt", &StateMachine::halt)
.addFunction("notify", &StateMachine::notify)
.addFunction("notifyAll", &StateMachine::notifyAll)
.addStaticFunction("getComponent", &StateMachine::getComponent)
.endClass()
.endNamespace();

Expand Down
2 changes: 2 additions & 0 deletions kernel/objects/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ class Object : public memory::AutoCollectionObject, public UpdatableObject {
_components[name] = component;
component->addReference();
component->setParent(this);
} else {
// TODO:
}
}

Expand Down
2 changes: 1 addition & 1 deletion kernel/objects/world.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
namespace ngind::objects {

World::World(std::string name) : Object(), _name(std::move(name)), _config(nullptr), _background_color() {
_config = resources::ResourcesManager::getInstance()->load<resources::ConfigResource>("worlds/world-" + _name + ".json");
_config = resources::ResourcesManager::getInstance()->load<resources::ConfigResource>("worlds/" + _name + ".json");
_background_color = rendering::Color(_config->getDocument()["background-color"].GetString());

ui::EventSystem::getInstance()->init();
Expand Down
96 changes: 94 additions & 2 deletions kernel/rendering/program.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@
namespace ngind::rendering {

Program::Program(const std::string& program_name) {
_vs = resources::ResourcesManager::getInstance()->load<resources::ShaderResource>(program_name + ".vs");
_fs = resources::ResourcesManager::getInstance()->load<resources::ShaderResource>(program_name + ".frag");
auto manager = resources::ResourcesManager::getInstance();
_program_config = manager->load<resources::ConfigResource>("programs/" + program_name + ".json");

_vs = manager->load<resources::ShaderResource>(std::string{_program_config->getDocument()["vertex"].GetString()} + ".vs");
_fs = manager->load<resources::ShaderResource>(std::string{_program_config->getDocument()["fragment"].GetString()} + ".frag");

this->_program = glCreateProgram();
glAttachShader(this->_program, _vs->getShader());
Expand All @@ -47,6 +50,11 @@ Program::Program(const std::string& program_name) {

Program::~Program() {
glDeleteProgram(this->_program);

auto manager = resources::ResourcesManager::getInstance();
manager->release(_vs);
manager->release(_fs);
manager->release(_program_config);
}

GLint Program::getUniform(const std::string& name) const {
Expand All @@ -58,4 +66,88 @@ GLint Program::getUniform(const std::string& name) const {
return res;
}

void Program::prepare() {
auto args = _program_config->getDocument()["args"].GetArray();
for (const auto& arg : args) {
std::string name = arg["name"].GetString();
std::string type = arg["type"].GetString();

if (type == "float") {
this->setFloat(name, arg["value"].GetFloat());
}
else if (type == "float2") {
// TODO:
}
else if (type == "float3") {

}
else if (type == "float4") {

}
else if (type == "int") {

}
else if (type == "int2") {

}
else if (type == "int3") {

}
else if (type == "int4") {

}
else if (type == "int") {

}
else if (type == "int2") {

}
else if (type == "int3") {

}
else if (type == "int4") {

}
else if (type == "unsigned") {

}
else if (type == "unsigned2") {

}
else if (type == "unsigned3") {

}
else if (type == "unsigned4") {

}
else if (type == "matrix2") {

}
else if (type == "matrix3") {

}
else if (type == "matrix4") {

}
else if (type == "matrix23") {

}
else if (type == "matrix24") {

}
else if (type == "matrix32") {

}
else if (type == "matrix34") {

}
else if (type == "matrix42") {

}
else if (type == "matrix43") {

}
}
}

} // namespace ngind::rendering
7 changes: 6 additions & 1 deletion kernel/rendering/program.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <string>

#include "resources/shader_resource.h"
#include "resources/config_resource.h"
#include "glm/glm.hpp"
#include "glm/gtc/type_ptr.hpp"

Expand All @@ -43,7 +44,7 @@ class Program {
/**
* @param name: program's name
*/
explicit Program(const std::string& name);
explicit Program(const std::string& program_name);

~Program();

Expand Down Expand Up @@ -168,6 +169,8 @@ class Program {
glUniformMatrix4x3fv(getUniform(name), 1, GL_FALSE, glm::value_ptr(m));
}

void prepare();

private:
/**
* The index of program
Expand All @@ -183,6 +186,8 @@ class Program {
* Reference of segment shader
*/
resources::ShaderResource* _fs;

resources::ConfigResource* _program_config;
};

} // namespace ngind::rendering
Expand Down
2 changes: 2 additions & 0 deletions kernel/rendering/rendering_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ class RenderingCommand : public memory::AutoCollectionObject {
color.b / 255.0f, color.a / 255.0f);
this->getProgram()->setMatrix4("projection", Camera::getInstance()->getProjection());
this->getProgram()->setMatrix4("model", this->getModel());

this->getProgram()->prepare();
}
private:
/**
Expand Down
11 changes: 11 additions & 0 deletions resources/config/programs/opaque.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"vertex": "sprite",
"fragment": "opaque",
"args": [
{
"name": "opaque",
"type": "float",
"value": 0.6
}
]
}
5 changes: 5 additions & 0 deletions resources/config/programs/sprite.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"vertex": "sprite",
"fragment": "sprite",
"args": []
}
5 changes: 5 additions & 0 deletions resources/config/programs/text.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"vertex": "text",
"fragment": "text",
"args": []
}
File renamed without changes.
82 changes: 82 additions & 0 deletions resources/config/worlds/shader.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
"world-name": "shader",
"background-color": "#000000FF",
"size": {
"width": 1024,
"height": 768
},
"camera": {
"x": 512,
"y": 384
},
"children": [
{
"id": 1,
"name": "title",
"position": {
"x": 512,
"y": 700
},
"scale": {
"x": 1,
"y": 1
},
"rotate": 0,
"z-order": 0,
"components": [
{
"type": "Label",
"name": "Label",
"font": "manaspc.ttf",
"size": 36,
"text": "Shader Parameters Test",
"color": "#FFFFFFFF",
"alignment": 2
}
]
},
{
"id": 2,
"name": "icon",
"position": {
"x": 512,
"y": 384
},
"scale": {
"x": 2,
"y": 2
},
"rotate": 0,
"z-order": 0,
"components": [
{
"type": "Sprite",
"name": "Sprite",
"filename": "dice.png",
"shader": "opaque",
"boundary": {
"left-bottom": {
"x": 0,
"y": 0
},
"right-up": {
"x": 80,
"y": 64
}
},
"color": "#FFFFFFFF"
}
]
}
],
"components": [
{
"type": "StateMachine",
"name": "WorldSwitch",
"driver-script": "world_switch.lua",
"classname": "WorldSwitch",
"subscription": [
]
}
]
}
5 changes: 5 additions & 0 deletions resources/script/kernel/components.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ end
Label = {}
Label.getComponent = function(object)
return engine.Label.getComponent(object)
end

StateMachine = {}
StateMachine.getComponent = function(object, name)
return engine.StateMachine.getComponent(object, name)
end
5 changes: 2 additions & 3 deletions resources/script/prefab_loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ end

function PrefabLoader:updateCreate(delta)
local spin1 = PrefabFactory.loadPrefab("spin")
--local spin2 = PrefabFactory.loadPrefab("spin")
spin1:setPosition(engine.vec2(200, 384))
--spin2:setPosition(engine.vec2(824, 384))
local script = StateMachine.getComponent(spin1, "Spin")
script.direction = 1
self.game_object:addChild("spin1", spin1)
--self.game_object:addChild("spin2", spin2)
self.move("Idle")
end

Expand Down
9 changes: 6 additions & 3 deletions resources/script/world_switch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ end
WORLD_SWITCH_TABLE = {}
WORLD_SWITCH_TABLE["welcome"] = {}
WORLD_SWITCH_TABLE["welcome"]["next"] = "colorful-labels"
WORLD_SWITCH_TABLE["welcome"]["prev"] = "prefab"
WORLD_SWITCH_TABLE["welcome"]["prev"] = "shader"
WORLD_SWITCH_TABLE["colorful-labels"] = {}
WORLD_SWITCH_TABLE["colorful-labels"]["next"] = "audio"
WORLD_SWITCH_TABLE["colorful-labels"]["prev"] = "welcome"
Expand Down Expand Up @@ -81,5 +81,8 @@ WORLD_SWITCH_TABLE["archive"] = {}
WORLD_SWITCH_TABLE["archive"]["next"] = "prefab"
WORLD_SWITCH_TABLE["archive"]["prev"] = "camera"
WORLD_SWITCH_TABLE["prefab"] = {}
WORLD_SWITCH_TABLE["prefab"]["next"] = "welcome"
WORLD_SWITCH_TABLE["prefab"]["prev"] = "archive"
WORLD_SWITCH_TABLE["prefab"]["next"] = "shader"
WORLD_SWITCH_TABLE["prefab"]["prev"] = "archive"
WORLD_SWITCH_TABLE["shader"] = {}
WORLD_SWITCH_TABLE["shader"]["next"] = "welcome"
WORLD_SWITCH_TABLE["shader"]["prev"] = "prefab"
12 changes: 12 additions & 0 deletions resources/shaders/opaque.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#version 330 core
in vec2 TexCoord;

out vec4 color;

uniform sampler2D image;
uniform vec4 my_color;
uniform float opaque;

void main() {
color = opaque * my_color * texture(image, TexCoord);
}

0 comments on commit bd9b2da

Please sign in to comment.