| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | #include <shader/shader.hh> |
| 38 | #include <stdexcept> |
| 39 | #include <utility> |
| 40 | |
| 41 | Shader::Shader(const string &src, GLenum type) { |
| 42 | id = glCreateShader(type); |
| 43 | const char *s = src.c_str(); |
| 44 | glShaderSource(id, 1, &s, nullptr); |
| 45 | glCompileShader(id); |
| 46 | |
| 47 | |
| 48 | GLint success; |
| 49 | glGetShaderiv(id, GL_COMPILE_STATUS, &success); |
| 50 | if (!success) { |
| 51 | char infoLog[512]; |
| 52 | glGetShaderInfoLog(id, 512, NULL, infoLog); |
| 53 | throw std::runtime_error("シェーダーのコンパイルに失敗しました:\n" + string(infoLog)); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | Shader::~Shader() { |
| 58 | glDeleteShader(id); |
| 59 | } |
| 60 | |
| 61 | Program::Program(Shader vertexShader, Shader fragmentShader) { |
| 62 | id = glCreateProgram(); |
| 63 | glAttachShader(id, vertexShader.GetShader()); |
| 64 | glAttachShader(id, fragmentShader.GetShader()); |
| 65 | glLinkProgram(id); |
| 66 | |
| 67 | |
| 68 | GLint success; |
| 69 | glGetProgramiv(id, GL_LINK_STATUS, &success); |
| 70 | if (!success) { |
| 71 | char infoLog[512]; |
| 72 | glGetProgramInfoLog(id, 512, NULL, infoLog); |
| 73 | throw std::runtime_error("プログラムのリンクに失敗しました:\n" + string(infoLog)); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | Program::~Program() { |
| 78 | glDeleteProgram(id); |
| 79 | } |
| 80 | |
| 81 | void Program::Reload(const string &vert, const string &frag) { |
| 82 | Program newProg( |
| 83 | Shader(vert, GL_VERTEX_SHADER), |
| 84 | Shader(frag, GL_FRAGMENT_SHADER) |
| 85 | ); |
| 86 | |
| 87 | std::swap(id, newProg.id); |
| 88 | } |
| 89 | |
| 90 | void Program::Use() { |
| 91 | glUseProgram(id); |
| 92 | } |
| 93 | |
| 94 | void Program::ClearColor(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha) { |
| 95 | glClearColor(red, green, blue, alpha); |
| 96 | } |
| 97 | |
| 98 | void Program::Clear(GLbitfield mask) { |
| 99 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 100 | } |
| 101 | |
| 102 | GLint Program::GetUniformLocation(string var) { |
| 103 | return glGetUniformLocation(id, var.c_str()); |
| 104 | } |
| 105 | |
| 106 | void Program::SetUniformVec3(const string &name, Vector3 v) { |
| 107 | glUniform3f(GetUniformLocation(name), v.pos.x, v.pos.y, v.pos.z); |
| 108 | } |
| 109 | |
| 110 | void Program::SetUniformMat4(const string &name, Matrix4 v) { |
| 111 | glUniformMatrix4fv(GetUniformLocation(name), 1, GL_FALSE, v.m); |
| 112 | } |
| 113 | |
| 114 | void Program::SetUniformInt(const string &name, i32 v) { |
| 115 | glUniform1i(GetUniformLocation(name), v); |
| 116 | } |
| 117 | |
| 118 | |
| 119 | |
| 120 | string GetDefaultVertexShader() { |
| 121 | return R"(#version 460 core |
| 122 | |
| 123 | layout(location = 0) in vec2 aPosition; |
| 124 | layout(location = 1) in vec2 aUv; |
| 125 | out vec2 vUv; |
| 126 | |
| 127 | void main() { |
| 128 | vUv = aUv; |
| 129 | gl_Position = vec4(aPosition, 0.0, 1.0); |
| 130 | })"; |
| 131 | } |
| 132 | |
| 133 | string GetDefaultFragmentShader() { |
| 134 | return R"(#version 460 core |
| 135 | |
| 136 | in vec2 vUv; |
| 137 | out vec4 out_color; |
| 138 | |
| 139 | uniform vec3 uColorA; |
| 140 | uniform vec3 uColorB; |
| 141 | uniform vec3 uColorC; |
| 142 | |
| 143 | void main() { |
| 144 | vec3 temp = mix(uColorA, uColorB, vUv.x); |
| 145 | vec3 blendedColor = mix(temp, uColorC, vUv.y * 0.95); |
| 146 | out_color = vec4(blendedColor, 1.0); |
| 147 | })"; |
| 148 | } |
| 149 | |
| 150 | string GetDefaultLuaCode() { |
| 151 | return R"(local mesh = { |
| 152 | vertices = { |
| 153 | -1, -1, 0, 0, |
| 154 | 1, -1, 1, 0, |
| 155 | 1, 1, 1, 1, |
| 156 | -1, 1, 0, 1 |
| 157 | }, |
| 158 | indices = { |
| 159 | 0, 1, 2, |
| 160 | 2, 3, 0 |
| 161 | }, |
| 162 | |
| 163 | attributes = { |
| 164 | { location = 0, size = 2, stride = 16, offset = 0 }, -- position |
| 165 | { location = 1, size = 2, stride = 16, offset = 0 } -- uv |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | le.gfx.add_mesh(mesh) |
| 170 | le.gfx.set_position(0, 0, 0, 0) |
| 171 | |
| 172 | function init() |
| 173 | le.log("screen res: ", le.sys.get_resolution()) |
| 174 | le.log("random: ", le.sys.random(2, 200)) |
| 175 | end |
| 176 | |
| 177 | function update() |
| 178 | local t = le.sys.get_time() -- glfwGetTime() |
| 179 | local speed = 1.0 -- animation speed |
| 180 | local phase = math.pi * 2.0 / 3.0 -- 120 deg phase diff (3 color) |
| 181 | |
| 182 | le.gfx.set_uniform3( |
| 183 | "uColorA", |
| 184 | math.sin(t * speed + 0.0) * 0.5 + 0.5, |
| 185 | math.sin(t * speed + phase) * 0.5 + 0.5, |
| 186 | math.sin(t * speed + phase * 2.0) * 0.5 + 0.5 |
| 187 | ) |
| 188 | |
| 189 | le.gfx.set_uniform3( |
| 190 | "uColorB", |
| 191 | math.sin(t * speed + phase * 2.0) * 0.5 + 0.5, |
| 192 | math.sin(t * speed + 0.0) * 0.5 + 0.5, |
| 193 | math.sin(t * speed + phase) * 0.5 + 0.5 |
| 194 | ) |
| 195 | |
| 196 | le.gfx.set_uniform3( |
| 197 | "uColorC", |
| 198 | math.sin(t * speed + phase) * 0.5 + 0.5, |
| 199 | math.sin(t * speed + phase * 2.0) * 0.5 + 0.5, |
| 200 | math.sin(t * speed + phase + 0.0) * 0.5 + 0.5 |
| 201 | ) |
| 202 | end)"; |
| 203 | } |