mirror of
https://github.com/cyberarm/cyberarm_engine.git
synced 2025-12-15 04:32:35 +00:00
Added shaders from i-mic-fps, preload shaders if cyberarm_engine/opengl has been required.
This commit is contained in:
30
assets/shaders/fragment/g_buffer.glsl
Normal file
30
assets/shaders/fragment/g_buffer.glsl
Normal file
@@ -0,0 +1,30 @@
|
||||
# version 330 core
|
||||
|
||||
layout(location = 0) out vec3 fragPosition;
|
||||
layout (location = 1) out vec4 fragColor;
|
||||
layout (location = 2) out vec3 fragNormal;
|
||||
layout (location = 3) out vec3 fragUV;
|
||||
|
||||
in vec3 outPosition, outColor, outNormal, outUV, outFragPos, outCameraPos;
|
||||
out vec4 outputFragColor;
|
||||
flat in int outHasTexture;
|
||||
|
||||
uniform sampler2D diffuse_texture;
|
||||
|
||||
void main() {
|
||||
vec3 result;
|
||||
|
||||
if (outHasTexture == 0) {
|
||||
result = outColor;
|
||||
} else {
|
||||
result = texture(diffuse_texture, outUV.xy).xyz + 0.25;
|
||||
}
|
||||
|
||||
fragPosition = outPosition;
|
||||
fragColor = vec4(result, 1.0);
|
||||
fragNormal = outNormal;
|
||||
fragUV = outUV;
|
||||
|
||||
float gamma = 2.2;
|
||||
outputFragColor.rgb = pow(fragColor.rgb, vec3(1.0 / gamma));
|
||||
}
|
||||
63
assets/shaders/fragment/lighting.glsl
Normal file
63
assets/shaders/fragment/lighting.glsl
Normal file
@@ -0,0 +1,63 @@
|
||||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
|
||||
@include "light_struct"
|
||||
const int DIRECTIONAL = 0;
|
||||
const int POINT = 1;
|
||||
const int SPOT = 2;
|
||||
|
||||
in vec2 outTexCoords;
|
||||
flat in Light outLight[1];
|
||||
|
||||
uniform sampler2D diffuse, position, texcoord, normal, depth;
|
||||
|
||||
vec4 directionalLight(Light light) {
|
||||
vec3 norm = normalize(texture(normal, outTexCoords).rgb);
|
||||
vec3 diffuse_color = texture(diffuse, outTexCoords).rgb;
|
||||
vec3 fragPos = texture(position, outTexCoords).rgb;
|
||||
|
||||
vec3 lightDir = normalize(light.position - fragPos);
|
||||
float diff = max(dot(norm, lightDir), 0);
|
||||
|
||||
vec3 _ambient = light.ambient;
|
||||
vec3 _diffuse = light.diffuse * diff;
|
||||
vec3 _specular = light.specular;
|
||||
|
||||
return vec4(_diffuse + _ambient + _specular, 1.0);
|
||||
}
|
||||
|
||||
vec4 pointLight(Light light) {
|
||||
return vec4(0.25, 0.25, 0.25, 1);
|
||||
}
|
||||
|
||||
vec4 spotLight(Light light) {
|
||||
return vec4(0.5, 0.5, 0.5, 1);
|
||||
}
|
||||
|
||||
vec4 calculateLighting(Light light) {
|
||||
vec4 result;
|
||||
|
||||
// switch(light.type) {
|
||||
// case DIRECTIONAL: {
|
||||
// result = directionalLight(light);
|
||||
// }
|
||||
// case SPOT: {
|
||||
// result = spotLight(light);
|
||||
// }
|
||||
// default: {
|
||||
// result = pointLight(light);
|
||||
// }
|
||||
// }
|
||||
|
||||
if (light.type == DIRECTIONAL) {
|
||||
result = directionalLight(light);
|
||||
} else {
|
||||
result = pointLight(light);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void main() {
|
||||
FragColor = texture(diffuse, outTexCoords) * calculateLighting(outLight[0]);
|
||||
}
|
||||
11
assets/shaders/include/light_struct.glsl
Normal file
11
assets/shaders/include/light_struct.glsl
Normal file
@@ -0,0 +1,11 @@
|
||||
struct Light {
|
||||
int type;
|
||||
vec3 direction;
|
||||
vec3 position;
|
||||
|
||||
vec3 diffuse;
|
||||
vec3 ambient;
|
||||
vec3 specular;
|
||||
|
||||
float intensity;
|
||||
};
|
||||
28
assets/shaders/vertex/g_buffer.glsl
Normal file
28
assets/shaders/vertex/g_buffer.glsl
Normal file
@@ -0,0 +1,28 @@
|
||||
# version 330 core
|
||||
|
||||
layout(location = 0) in vec3 inPosition;
|
||||
layout(location = 1) in vec3 inColor;
|
||||
layout(location = 2) in vec3 inNormal;
|
||||
layout(location = 3) in vec3 inUV;
|
||||
|
||||
uniform mat4 projection, view, model;
|
||||
uniform int hasTexture;
|
||||
uniform vec3 cameraPos;
|
||||
|
||||
out vec3 outPosition, outColor, outNormal, outUV;
|
||||
out vec3 outFragPos, outViewPos, outCameraPos;
|
||||
flat out int outHasTexture;
|
||||
|
||||
void main() {
|
||||
// projection * view * model * position
|
||||
outPosition = inPosition;
|
||||
outColor = inColor;
|
||||
outNormal= normalize(transpose(inverse(mat3(model))) * inNormal);
|
||||
outUV = inUV;
|
||||
outHasTexture = hasTexture;
|
||||
outCameraPos = cameraPos;
|
||||
|
||||
outFragPos = vec3(model * vec4(inPosition, 1.0));
|
||||
|
||||
gl_Position = projection * view * model * vec4(inPosition, 1.0);
|
||||
}
|
||||
17
assets/shaders/vertex/lighting.glsl
Normal file
17
assets/shaders/vertex/lighting.glsl
Normal file
@@ -0,0 +1,17 @@
|
||||
#version 330 core
|
||||
@include "light_struct"
|
||||
|
||||
layout (location = 0) in vec3 inPosition;
|
||||
layout (location = 1) in vec2 inTexCoords;
|
||||
|
||||
uniform sampler2D diffuse, position, texcoord, normal, depth;
|
||||
uniform Light light[1];
|
||||
|
||||
out vec2 outTexCoords;
|
||||
flat out Light outLight[1];
|
||||
|
||||
void main() {
|
||||
gl_Position = vec4(inPosition.x, inPosition.y, inPosition.z, 1.0);
|
||||
outTexCoords = inTexCoords;
|
||||
outLight = light;
|
||||
}
|
||||
@@ -7,8 +7,8 @@ module CyberarmEngine
|
||||
@title_size = 56
|
||||
@caption_size = 24
|
||||
|
||||
@title = CyberarmEngine::Text.new("", size: @title_size, shadow_color: 0xaa_222222)
|
||||
@caption = CyberarmEngine::Text.new("", size: @caption_size, shadow_color: 0xaa_222222)
|
||||
@title = CyberarmEngine::Text.new("", size: @title_size, shadow_color: 0xaa_222222, static: true)
|
||||
@caption = CyberarmEngine::Text.new("", size: @caption_size, shadow_color: 0xaa_222222, static: true)
|
||||
|
||||
@spacer_width = 256
|
||||
@spacer_height = 6
|
||||
|
||||
@@ -11,7 +11,19 @@ module CyberarmEngine
|
||||
if e != GL_NO_ERROR
|
||||
warn "OpenGL error detected by handler at: #{caller[0]}"
|
||||
warn " #{gluErrorString(e)} (#{e})\n"
|
||||
exit if window.exit_on_opengl_error?
|
||||
exit if Window.instance&.exit_on_opengl_error?
|
||||
end
|
||||
end
|
||||
|
||||
def preload_default_shaders
|
||||
shaders = %w[g_buffer lighting]
|
||||
shaders.each do |shader|
|
||||
Shader.new(
|
||||
name: shader,
|
||||
includes_dir: "#{CYBERARM_ENGINE_ROOT_PATH}/assets/shaders/include",
|
||||
vertex: "#{CYBERARM_ENGINE_ROOT_PATH}/assets/shaders/vertex/#{shader}.glsl",
|
||||
fragment: "#{CYBERARM_ENGINE_ROOT_PATH}/assets/shaders/fragment/#{shader}.glsl"
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
module CyberarmEngine
|
||||
class GBuffer
|
||||
attr_reader :screen_vbo, :vertices, :uvs
|
||||
attr_reader :width, :height
|
||||
|
||||
def initialize(width:, height:)
|
||||
@width = width
|
||||
|
||||
@@ -41,6 +41,7 @@ module CyberarmEngine
|
||||
|
||||
@states = []
|
||||
@exit_on_opengl_error = false
|
||||
preload_default_shaders if respond_to?(:preload_default_shaders)
|
||||
|
||||
setup if defined?(setup)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user