master
ブランチ master
ShaderPlayground / src / shader / shader.cc
5.7 KB、203行 生 Blame 履歴
1/*
2# 076 License
3
4Copyright (c) 076 Studio LLC
5
6Permission is hereby granted to any person obtaining a copy of the software
7Shader Playground (the "Software") to use, modify, merge, copy, publish, distribute,
8sublicense, and/or sell copies of the Software, subject to the following conditions:
9
10 1. **Origin Attribution**:
11 - You must not misrepresent the origin of the Software; you must not claim
12 you created the original Software.
13 - If the Software is used in a product, you must either:
14 a. Provide clear attribution in the product's documentation, user interface,
15 or other visible areas, **OR**
16 b. Pay the original developers a fee they specify in writing.
17 2. **Usage Restriction**:
18 - The Software, or any derivative works, dependencies, or libraries
19 incorporating it, must not be used for censorship or to suppress freedom of
20 speech, expression, or creativity. Prohibited uses include, but are not
21 limited to:
22 - Censorship of so-called "hate speech", visuals, non-mainstream opinions,
23 ideas, or objective reality.
24 - Tools or systems designed to restrict access to information or
25 artistic works.
26 3. **Notice Preservation**:
27 - This license and the above copyright notice must remain intact in all copies
28 of the source code.
29
30THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
31INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
32PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
33HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
34CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
35OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36*/
37#include <shader/shader.hh>
38#include <stdexcept>
39#include <utility>
40
41Shader::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
57Shader::~Shader() {
58 glDeleteShader(id);
59}
60
61Program::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
77Program::~Program() {
78 glDeleteProgram(id);
79}
80
81void 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
90void Program::Use() {
91 glUseProgram(id);
92}
93
94void Program::ClearColor(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha) {
95 glClearColor(red, green, blue, alpha);
96}
97
98void Program::Clear(GLbitfield mask) {
99 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
100}
101
102GLint Program::GetUniformLocation(string var) {
103 return glGetUniformLocation(id, var.c_str());
104}
105
106void Program::SetUniformVec3(const string &name, Vector3 v) {
107 glUniform3f(GetUniformLocation(name), v.pos.x, v.pos.y, v.pos.z);
108}
109
110void Program::SetUniformMat4(const string &name, Matrix4 v) {
111 glUniformMatrix4fv(GetUniformLocation(name), 1, GL_FALSE, v.m);
112}
113
114void Program::SetUniformInt(const string &name, i32 v) {
115 glUniform1i(GetUniformLocation(name), v);
116}
117
118/////////////////
119
120string GetDefaultVertexShader() {
121 return R"(#version 460 core
122
123layout(location = 0) in vec2 aPosition;
124layout(location = 1) in vec2 aUv;
125out vec2 vUv;
126
127void main() {
128 vUv = aUv;
129 gl_Position = vec4(aPosition, 0.0, 1.0);
130})";
131}
132
133string GetDefaultFragmentShader() {
134 return R"(#version 460 core
135
136in vec2 vUv;
137out vec4 out_color;
138
139uniform vec3 uColorA;
140uniform vec3 uColorB;
141uniform vec3 uColorC;
142
143void 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
150string 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
169le.gfx.add_mesh(mesh)
170le.gfx.set_position(0, 0, 0, 0)
171
172function init()
173 le.log("screen res: ", le.sys.get_resolution())
174 le.log("random: ", le.sys.random(2, 200))
175end
176
177function 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 )
202end)";
203}