From ea88790b539f80e1d41bba268136d78357aa87d1 Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 5 Sep 2018 22:40:20 +0800 Subject: *update --- bin/Jin.exe | Bin 1407488 -> 548864 bytes bin/img.png | Bin 0 -> 442 bytes bin/img2.bmp | Bin 0 -> 12342 bytes bin/jin.exe | Bin 1407488 -> 548864 bytes bin/main.lua | 33 +++++++ build/vc++/libjin/libjin.vcxproj | 5 +- build/vc++/libjin/libjin.vcxproj.filters | 15 ++-- src/libjin/Graphics/Graphics.h | 2 +- src/libjin/Graphics/JSL.cpp | 142 ------------------------------- src/libjin/Graphics/JSL.h | 59 ------------- src/libjin/Graphics/Texture.cpp | 1 - src/lua/modules/embed/boot.lua.h | 1 + 12 files changed, 47 insertions(+), 211 deletions(-) create mode 100644 bin/img.png create mode 100644 bin/img2.bmp create mode 100644 bin/main.lua delete mode 100644 src/libjin/Graphics/JSL.cpp delete mode 100644 src/libjin/Graphics/JSL.h diff --git a/bin/Jin.exe b/bin/Jin.exe index fe5bf33..54e55b7 100644 Binary files a/bin/Jin.exe and b/bin/Jin.exe differ diff --git a/bin/img.png b/bin/img.png new file mode 100644 index 0000000..0d11f85 Binary files /dev/null and b/bin/img.png differ diff --git a/bin/img2.bmp b/bin/img2.bmp new file mode 100644 index 0000000..dc9cf95 Binary files /dev/null and b/bin/img2.bmp differ diff --git a/bin/jin.exe b/bin/jin.exe index fe5bf33..54e55b7 100644 Binary files a/bin/jin.exe and b/bin/jin.exe differ diff --git a/bin/main.lua b/bin/main.lua new file mode 100644 index 0000000..89a8c01 --- /dev/null +++ b/bin/main.lua @@ -0,0 +1,33 @@ +local shader; +local program = [[ +extern Texture img; +extern number dt; +Color effect(Color col, Texture tex, vec2 uv, vec2 screen) +{ + //return vec4(1, 0, 1, 1); + return sin(dt)*Texel(tex, uv) * Texel(img, uv); +} +]] +local img +local img2 +function jin.core.onLoad() + shader = jin.graphics.newShader(program) + img = jin.graphics.newTexture("img.png") + img2 = jin.graphics.newTexture("img2.bmp") +end + +function jin.core.onEvent(e) + if e.type == "quit" then + jin.core.stop() + end +end + +local dt = 0 +function jin.core.onDraw() + dt = dt + 0.1 + jin.graphics.useShader(shader) + shader:sendTexture("img", img2) + shader:sendNumber("dt", dt) + jin.graphics.draw(img, 0, 0) + jin.graphics.unuseShader() +end \ No newline at end of file diff --git a/build/vc++/libjin/libjin.vcxproj b/build/vc++/libjin/libjin.vcxproj index 98fd116..5c3c91c 100644 --- a/build/vc++/libjin/libjin.vcxproj +++ b/build/vc++/libjin/libjin.vcxproj @@ -152,11 +152,12 @@ + - + @@ -201,7 +202,7 @@ - + diff --git a/build/vc++/libjin/libjin.vcxproj.filters b/build/vc++/libjin/libjin.vcxproj.filters index 63f78ed..769b79a 100644 --- a/build/vc++/libjin/libjin.vcxproj.filters +++ b/build/vc++/libjin/libjin.vcxproj.filters @@ -192,9 +192,6 @@ Source\Graphics - - Source\Graphics - Source\Graphics @@ -207,6 +204,12 @@ Source\Time + + Source\Graphics + + + Source\Graphics + @@ -295,9 +298,6 @@ Source\Graphics - - Source\Graphics - Source\Graphics @@ -310,5 +310,8 @@ Source\Time + + Source\Graphics + \ No newline at end of file diff --git a/src/libjin/Graphics/Graphics.h b/src/libjin/Graphics/Graphics.h index 210dadf..bca3812 100644 --- a/src/libjin/Graphics/Graphics.h +++ b/src/libjin/Graphics/Graphics.h @@ -8,7 +8,7 @@ #include "font.h" #include "Shapes.h" #include "texture.h" -#include "jsl.h" +#include "Shader.h" #include "window.h" #endif // JIN_MODULES_RENDER 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::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 diff --git a/src/libjin/Graphics/JSL.h b/src/libjin/Graphics/JSL.h deleted file mode 100644 index a99a4c7..0000000 --- a/src/libjin/Graphics/JSL.h +++ /dev/null @@ -1,59 +0,0 @@ -#ifndef __JIN_JSL_H -#define __JIN_JSL_H -#include "../modules.h" -#if JIN_MODULES_RENDER - -#include -#include -#include "color.h" -#include "texture.h" -#include "canvas.h" -#include "../3rdparty/GLee/GLee.h" - -namespace jin -{ -namespace graphics -{ - - class JSLProgram - { - public: - static JSLProgram* createJSLProgram(const char* program); - static inline JSLProgram* getCurrentJSL() { return currentJSLProgram; } - virtual ~JSLProgram(); - - inline void use() - { - glUseProgram(pid); - currentJSLProgram = this; - } - static inline void unuse() - { - glUseProgram(0); - currentJSLProgram = nullptr; - } - - void sendFloat(const char* name, float number); - void sendTexture(const char* name, const Texture* image); - void sendVec2(const char* name, float x, float y); - void sendVec3(const char* name, float x, float y, float z); - void sendVec4(const char* name, float x, float y, float z, float w); - void sendCanvas(const char* name, const Canvas* canvas); - void sendColor(const char* name, const color* col); - - protected: - static JSLProgram* currentJSLProgram; - JSLProgram(const char* program); - - GLuint pid; - std::map texturePool; - GLint currentTextureUnit; - GLint getTextureUnit(const std::string& name); - - }; - -} // graphics -} // jin - -#endif // JIN_MODULES_RENDER -#endif // __JIN_JSL_H \ No newline at end of file diff --git a/src/libjin/Graphics/Texture.cpp b/src/libjin/Graphics/Texture.cpp index 633eeac..708042e 100644 --- a/src/libjin/Graphics/Texture.cpp +++ b/src/libjin/Graphics/Texture.cpp @@ -68,7 +68,6 @@ namespace graphics bool Texture::loadb(const void* b, size_t s) { - // 使用stbi_load_from_memory替代 int w; int h; pixels = (color*)stbi_load_from_memory((unsigned char *)b, s, &w, &h, NULL, STBI_rgb_alpha); diff --git a/src/lua/modules/embed/boot.lua.h b/src/lua/modules/embed/boot.lua.h index 1638cbd..8ff89ee 100644 --- a/src/lua/modules/embed/boot.lua.h +++ b/src/lua/modules/embed/boot.lua.h @@ -26,6 +26,7 @@ jin.config.fps = jin.config.fps or 60 jin.graphics.init(jin.config) jin.audio.init() +-- TODO: 禁用系统模块 ------------------------------------------------------------------------- -- Default game loop -- cgit v1.1-26-g67d0