aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/Graphics/JSL.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/libjin/Graphics/JSL.cpp')
-rw-r--r--src/libjin/Graphics/JSL.cpp142
1 files changed, 0 insertions, 142 deletions
diff --git a/src/libjin/Graphics/JSL.cpp b/src/libjin/Graphics/JSL.cpp
deleted file mode 100644
index 1eb1357..0000000
--- a/src/libjin/Graphics/JSL.cpp
+++ /dev/null
@@ -1,142 +0,0 @@
-#include "../modules.h"
-#if JIN_MODULES_RENDER
-
-#include "../utils/macros.h"
-#include "jsl.h"
-namespace jin
-{
-namespace graphics
-{
-
- static const char* base_f = R"base_f(
-#define number float
-#define Texture sampler2D
-#define Canvas sampler2D
-#define Color vec4
-#define Texel texture2D
-#define extern uniform
-uniform Texture _tex0_;
-%s
-void main()
-{
- gl_FragColor = effect(gl_Color, _tex0_, gl_TexCoord[0].xy, gl_FragCoord.xy);
-}
- )base_f";
-
- /*static*/ JSLProgram* JSLProgram::currentJSLProgram = nullptr;
-
- JSLProgram* JSLProgram::createJSLProgram(const char* program)
- {
- return new JSLProgram(program);
- }
-
- JSLProgram::JSLProgram(const char* program)
- : currentTextureUnit(0)
- {
- char* fs = (char*)alloca(strlen(program) + strlen(base_f));
- sprintf(fs, base_f, program);
- GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
- glShaderSource(fragmentShader, 1, (const GLchar**)&fs, NULL);
- glCompileShader(fragmentShader);
- pid = glCreateProgram();
- glAttachShader(pid, fragmentShader);
- glLinkProgram(pid);
- }
-
- JSLProgram::~JSLProgram()
- {
- if (currentJSLProgram == this)
- unuse();
- }
-
- static inline GLint getMaxTextureUnits()
- {
- GLint maxTextureUnits = 0;
- glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
- return maxTextureUnits;
- }
-
- GLint JSLProgram::getTextureUnit(const std::string& name)
- {
- std::map<std::string, GLint>::iterator texture_unit = texturePool.find(name);
- if (texture_unit != texturePool.end())
- return texture_unit->second;
- static GLint maxTextureUnits = getMaxTextureUnits();
- if (++currentTextureUnit >= maxTextureUnits)
- return 0;
- texturePool[name] = currentTextureUnit;
- return currentTextureUnit;
- }
-
-#define checkJSL() if (currentJSLProgram != this) return
-
- void JSLProgram::sendFloat(const char* variable, float number)
- {
- checkJSL();
- int loc = glGetUniformLocation(pid, variable);
- glUniform1f(loc, number);
- }
-
- void JSLProgram::sendTexture(const char* variable, const Texture* tex)
- {
- checkJSL();
- GLint location = glGetUniformLocation(pid, variable);
- if (location == -1)
- return;
- GLint texture_unit = getTextureUnit(variable);
- glUniform1i(location, texture_unit);
- glActiveTexture(GL_TEXTURE0 + texture_unit);
- glBindTexture(GL_TEXTURE_2D, tex->getTexture());
- glActiveTexture(GL_TEXTURE0);
- }
-
- void JSLProgram::sendCanvas(const char* variable, const Canvas* canvas)
- {
- checkJSL();
- GLint location = glGetUniformLocation(pid, variable);
- if (location == -1)
- return;
- GLint texture_unit = getTextureUnit(variable);
- glUniform1i(location, texture_unit);
- glActiveTexture(GL_TEXTURE0 + texture_unit);
- glBindTexture(GL_TEXTURE_2D, canvas->getTexture());
- glActiveTexture(GL_TEXTURE0);
- }
-
- void JSLProgram::sendVec2(const char* name, float x, float y)
- {
- checkJSL();
- int loc = glGetUniformLocation(pid, name);
- glUniform2f(loc, x, y);
- }
-
- void JSLProgram::sendVec3(const char* name, float x, float y, float z)
- {
- checkJSL();
- int loc = glGetUniformLocation(pid, name);
- glUniform3f(loc, x, y, z);
- }
-
- void JSLProgram::sendVec4(const char* name, float x, float y, float z, float w)
- {
- checkJSL();
- int loc = glGetUniformLocation(pid, name);
- glUniform4f(loc, x, y, z, w);
- }
-
- void JSLProgram::sendColor(const char* name, const color* col)
- {
- checkJSL();
- int loc = glGetUniformLocation(pid, name);
- glUniform4f(loc,
- col->rgba.r / 255.f,
- col->rgba.g / 255.f,
- col->rgba.b / 255.f,
- col->rgba.a / 255.f
- );
- }
-
-} // graphics
-} // jin
-
-#endif // JIN_MODULES_RENDER \ No newline at end of file