mirror of
https://github.com/cyberarm/cyberarm_engine.git
synced 2026-03-21 19:56:11 +00:00
Added support for rendering multiple lights, standardized shaders to use snake case for variables and camel case for functions, stubbed PBR material shader include.
This commit is contained in:
@@ -5,25 +5,25 @@ layout (location = 1) out vec4 fragColor;
|
|||||||
layout (location = 2) out vec3 fragNormal;
|
layout (location = 2) out vec3 fragNormal;
|
||||||
layout (location = 3) out vec3 fragUV;
|
layout (location = 3) out vec3 fragUV;
|
||||||
|
|
||||||
in vec3 outPosition, outColor, outNormal, outUV, outFragPos, outCameraPos;
|
in vec3 out_position, out_color, out_normal, out_uv, out_frag_pos, out_camera_pos;
|
||||||
out vec4 outputFragColor;
|
out vec4 outputFragColor;
|
||||||
flat in int outHasTexture;
|
flat in int out_has_texture;
|
||||||
|
|
||||||
uniform sampler2D diffuse_texture;
|
uniform sampler2D diffuse_texture;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
vec3 result;
|
vec3 result;
|
||||||
|
|
||||||
if (outHasTexture == 0) {
|
if (out_has_texture == 0) {
|
||||||
result = outColor;
|
result = out_color;
|
||||||
} else {
|
} else {
|
||||||
result = texture(diffuse_texture, outUV.xy).xyz + 0.25;
|
result = texture(diffuse_texture, out_uv.xy).xyz + 0.25;
|
||||||
}
|
}
|
||||||
|
|
||||||
fragPosition = outPosition;
|
fragPosition = out_position;
|
||||||
fragColor = vec4(result, 1.0);
|
fragColor = vec4(result, 1.0);
|
||||||
fragNormal = outNormal;
|
fragNormal = out_normal;
|
||||||
fragUV = outUV;
|
fragUV = out_uv;
|
||||||
|
|
||||||
float gamma = 2.2;
|
float gamma = 2.2;
|
||||||
outputFragColor.rgb = pow(fragColor.rgb, vec3(1.0 / gamma));
|
outputFragColor.rgb = pow(fragColor.rgb, vec3(1.0 / gamma));
|
||||||
|
|||||||
@@ -1,22 +1,23 @@
|
|||||||
#version 330 core
|
#version 330 core
|
||||||
out vec4 FragColor;
|
out vec4 frag_color;
|
||||||
|
|
||||||
@include "light_struct"
|
@include "light_struct"
|
||||||
const int DIRECTIONAL = 0;
|
const int DIRECTIONAL = 0;
|
||||||
const int POINT = 1;
|
const int POINT = 1;
|
||||||
const int SPOT = 2;
|
const int SPOT = 2;
|
||||||
|
|
||||||
in vec2 outTexCoords;
|
flat in Light out_lights[7];
|
||||||
flat in Light outLight[1];
|
in vec2 out_tex_coords;
|
||||||
|
flat in int out_light_count;
|
||||||
|
|
||||||
uniform sampler2D diffuse, position, texcoord, normal, depth;
|
uniform sampler2D diffuse, position, texcoord, normal, depth;
|
||||||
|
|
||||||
vec4 directionalLight(Light light) {
|
vec4 directionalLight(Light light) {
|
||||||
vec3 norm = normalize(texture(normal, outTexCoords).rgb);
|
vec3 norm = normalize(texture(normal, out_tex_coords).rgb);
|
||||||
vec3 diffuse_color = texture(diffuse, outTexCoords).rgb;
|
vec3 diffuse_color = texture(diffuse, out_tex_coords).rgb;
|
||||||
vec3 fragPos = texture(position, outTexCoords).rgb;
|
vec3 frag_pos = texture(position, out_tex_coords).rgb;
|
||||||
|
|
||||||
vec3 lightDir = normalize(light.position - fragPos);
|
vec3 lightDir = normalize(light.position - frag_pos);
|
||||||
float diff = max(dot(norm, lightDir), 0);
|
float diff = max(dot(norm, lightDir), 0);
|
||||||
|
|
||||||
vec3 _ambient = light.ambient;
|
vec3 _ambient = light.ambient;
|
||||||
@@ -59,5 +60,10 @@ vec4 calculateLighting(Light light) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
FragColor = texture(diffuse, outTexCoords) * calculateLighting(outLight[0]);
|
frag_color = vec4(0.0);
|
||||||
|
|
||||||
|
for(int i = 0; i < out_light_count; i++)
|
||||||
|
{
|
||||||
|
frag_color += texture(diffuse, out_tex_coords) * calculateLighting(out_lights[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
16
assets/shaders/include/material_struct.glsl
Normal file
16
assets/shaders/include/material_struct.glsl
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
struct Material {
|
||||||
|
vec3 color;
|
||||||
|
vec3 roughness;
|
||||||
|
vec3 metalic;
|
||||||
|
vec3 specular;
|
||||||
|
|
||||||
|
bool use_color_texture;
|
||||||
|
bool use_roughness_texture;
|
||||||
|
bool use_metalic_texture;
|
||||||
|
bool use_specular_texture;
|
||||||
|
|
||||||
|
sampler2D color_tex;
|
||||||
|
sampler2D roughness_tex;
|
||||||
|
sampler2D metalic_tex;
|
||||||
|
sampler2D specular_tex;
|
||||||
|
};
|
||||||
@@ -1,28 +1,28 @@
|
|||||||
# version 330 core
|
# version 330 core
|
||||||
|
|
||||||
layout(location = 0) in vec3 inPosition;
|
layout(location = 0) in vec3 in_position;
|
||||||
layout(location = 1) in vec3 inColor;
|
layout(location = 1) in vec3 in_color;
|
||||||
layout(location = 2) in vec3 inNormal;
|
layout(location = 2) in vec3 in_normal;
|
||||||
layout(location = 3) in vec3 inUV;
|
layout(location = 3) in vec3 in_uv;
|
||||||
|
|
||||||
uniform mat4 projection, view, model;
|
uniform mat4 projection, view, model;
|
||||||
uniform int hasTexture;
|
uniform int has_texture;
|
||||||
uniform vec3 cameraPos;
|
uniform vec3 camera_pos;
|
||||||
|
|
||||||
out vec3 outPosition, outColor, outNormal, outUV;
|
out vec3 out_position, out_color, out_normal, out_uv;
|
||||||
out vec3 outFragPos, outViewPos, outCameraPos;
|
out vec3 out_frag_pos, out_view_pos, out_camera_pos;
|
||||||
flat out int outHasTexture;
|
flat out int out_has_texture;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
// projection * view * model * position
|
// projection * view * model * position
|
||||||
outPosition = inPosition;
|
out_position = in_position;
|
||||||
outColor = inColor;
|
out_color = in_color;
|
||||||
outNormal= normalize(transpose(inverse(mat3(model))) * inNormal);
|
out_normal= normalize(transpose(inverse(mat3(model))) * in_normal);
|
||||||
outUV = inUV;
|
out_uv = in_uv;
|
||||||
outHasTexture = hasTexture;
|
out_has_texture = has_texture;
|
||||||
outCameraPos = cameraPos;
|
out_camera_pos = camera_pos;
|
||||||
|
|
||||||
outFragPos = vec3(model * vec4(inPosition, 1.0));
|
out_frag_pos = vec3(model * vec4(in_position, 1.0));
|
||||||
|
|
||||||
gl_Position = projection * view * model * vec4(inPosition, 1.0);
|
gl_Position = projection * view * model * vec4(in_position, 1.0);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,24 @@
|
|||||||
#version 330 core
|
#version 330 core
|
||||||
@include "light_struct"
|
@include "light_struct"
|
||||||
|
|
||||||
layout (location = 0) in vec3 inPosition;
|
layout (location = 0) in vec3 in_position;
|
||||||
layout (location = 1) in vec2 inTexCoords;
|
layout (location = 1) in vec2 in_tex_coords;
|
||||||
|
|
||||||
uniform sampler2D diffuse, position, texcoord, normal, depth;
|
uniform sampler2D diffuse, position, texcoord, normal, depth;
|
||||||
uniform Light light[1];
|
uniform int light_count;
|
||||||
|
uniform Light lights[7];
|
||||||
|
|
||||||
out vec2 outTexCoords;
|
out vec2 out_tex_coords;
|
||||||
flat out Light outLight[1];
|
flat out int out_light_count;
|
||||||
|
flat out Light out_lights[7];
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
gl_Position = vec4(inPosition.x, inPosition.y, inPosition.z, 1.0);
|
gl_Position = vec4(in_position.x, in_position.y, in_position.z, 1.0);
|
||||||
outTexCoords = inTexCoords;
|
out_tex_coords = in_tex_coords;
|
||||||
outLight = light;
|
out_light_count = light_count;
|
||||||
|
|
||||||
|
for(int i = 0; i < light_count; i++)
|
||||||
|
{
|
||||||
|
out_lights[i] = lights[i];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -49,9 +49,9 @@ module CyberarmEngine
|
|||||||
|
|
||||||
@objects.each { |o| @vertex_count += o.vertices.size }
|
@objects.each { |o| @vertex_count += o.vertices.size }
|
||||||
|
|
||||||
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
|
# start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
|
||||||
build_collision_tree
|
# build_collision_tree
|
||||||
puts " Building mesh collision tree took #{((Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond) - start_time) / 1000.0).round(2)} seconds"
|
# puts " Building mesh collision tree took #{((Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond) - start_time) / 1000.0).round(2)} seconds"
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse(parser)
|
def parse(parser)
|
||||||
@@ -151,24 +151,24 @@ module CyberarmEngine
|
|||||||
glBindBuffer(GL_ARRAY_BUFFER, @positions_buffer_id)
|
glBindBuffer(GL_ARRAY_BUFFER, @positions_buffer_id)
|
||||||
gl_error?
|
gl_error?
|
||||||
|
|
||||||
# inPosition
|
# in_position
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nil)
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nil)
|
||||||
gl_error?
|
gl_error?
|
||||||
# colors
|
# colors
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, @colors_buffer_id)
|
glBindBuffer(GL_ARRAY_BUFFER, @colors_buffer_id)
|
||||||
# inColor
|
# in_color
|
||||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, nil)
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, nil)
|
||||||
gl_error?
|
gl_error?
|
||||||
# normals
|
# normals
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, @normals_buffer_id)
|
glBindBuffer(GL_ARRAY_BUFFER, @normals_buffer_id)
|
||||||
# inNormal
|
# in_normal
|
||||||
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 0, nil)
|
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 0, nil)
|
||||||
gl_error?
|
gl_error?
|
||||||
|
|
||||||
if has_texture?
|
if has_texture?
|
||||||
# uvs
|
# uvs
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, @uvs_buffer_id)
|
glBindBuffer(GL_ARRAY_BUFFER, @uvs_buffer_id)
|
||||||
# inUV
|
# in_uv
|
||||||
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 0, nil)
|
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 0, nil)
|
||||||
gl_error?
|
gl_error?
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ module CyberarmEngine
|
|||||||
shader.uniform_transform("projection", camera.projection_matrix)
|
shader.uniform_transform("projection", camera.projection_matrix)
|
||||||
shader.uniform_transform("view", camera.view_matrix)
|
shader.uniform_transform("view", camera.view_matrix)
|
||||||
shader.uniform_transform("model", entity.model_matrix)
|
shader.uniform_transform("model", entity.model_matrix)
|
||||||
shader.uniform_vec3("cameraPosition", camera.position)
|
shader.uniform_vector3("camera_position", camera.position)
|
||||||
|
|
||||||
gl_error?
|
gl_error?
|
||||||
draw_model(entity.model, shader)
|
draw_model(entity.model, shader)
|
||||||
@@ -154,16 +154,22 @@ module CyberarmEngine
|
|||||||
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:depth))
|
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:depth))
|
||||||
shader.uniform_integer("depth", 4)
|
shader.uniform_integer("depth", 4)
|
||||||
|
|
||||||
lights.each_with_index do |light, _i|
|
# FIXME: Try to figure out how to up this to 32 and/or beyond
|
||||||
shader.uniform_integer("light[0].type", light.type)
|
# (currently fails with more then 7 lights passed in to shader)
|
||||||
shader.uniform_vec3("light[0].direction", light.direction)
|
lights.each_slice(7).each do |light_group|
|
||||||
shader.uniform_vec3("light[0].position", light.position)
|
light_group.each_with_index do |light, _i|
|
||||||
shader.uniform_vec3("light[0].diffuse", light.diffuse)
|
shader.uniform_integer("light_count", light_group.size)
|
||||||
shader.uniform_vec3("light[0].ambient", light.ambient)
|
|
||||||
shader.uniform_vec3("light[0].specular", light.specular)
|
shader.uniform_integer("lights[#{_i}].type", light.type)
|
||||||
|
shader.uniform_vector3("lights[#{_i}].direction", light.direction)
|
||||||
|
shader.uniform_vector3("lights[#{_i}].position", light.position)
|
||||||
|
shader.uniform_vector3("lights[#{_i}].diffuse", light.diffuse)
|
||||||
|
shader.uniform_vector3("lights[#{_i}].ambient", light.ambient)
|
||||||
|
shader.uniform_vector3("lights[#{_i}].specular", light.specular)
|
||||||
|
|
||||||
glDrawArrays(GL_TRIANGLES, 0, @g_buffer.vertices.size)
|
glDrawArrays(GL_TRIANGLES, 0, @g_buffer.vertices.size)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
glBindVertexArray(0)
|
glBindVertexArray(0)
|
||||||
end
|
end
|
||||||
@@ -215,7 +221,7 @@ module CyberarmEngine
|
|||||||
|
|
||||||
offset = 0
|
offset = 0
|
||||||
model.objects.each do |object|
|
model.objects.each do |object|
|
||||||
shader.uniform_boolean("hasTexture", object.has_texture?)
|
shader.uniform_boolean("has_texture", object.has_texture?)
|
||||||
|
|
||||||
if object.has_texture?
|
if object.has_texture?
|
||||||
glBindTexture(GL_TEXTURE_2D, object.materials.find { |mat| mat.texture_id }.texture_id)
|
glBindTexture(GL_TEXTURE_2D, object.materials.find { |mat| mat.texture_id }.texture_id)
|
||||||
|
|||||||
@@ -385,7 +385,7 @@ module CyberarmEngine
|
|||||||
# @param value [Vector]
|
# @param value [Vector]
|
||||||
# @param location [Integer]
|
# @param location [Integer]
|
||||||
# @return [void]
|
# @return [void]
|
||||||
def uniform_vec3(variable, value, location = nil)
|
def uniform_vector3(variable, value, location = nil)
|
||||||
attr_loc = location || attribute_location(variable)
|
attr_loc = location || attribute_location(variable)
|
||||||
|
|
||||||
glUniform3f(attr_loc, *value.to_a[0..2])
|
glUniform3f(attr_loc, *value.to_a[0..2])
|
||||||
@@ -397,7 +397,7 @@ module CyberarmEngine
|
|||||||
# @param value [Vector]
|
# @param value [Vector]
|
||||||
# @param location [Integer]
|
# @param location [Integer]
|
||||||
# @return [void]
|
# @return [void]
|
||||||
def uniform_vec4(variable, value, location = nil)
|
def uniform_vector4(variable, value, location = nil)
|
||||||
attr_loc = location || attribute_location(variable)
|
attr_loc = location || attribute_location(variable)
|
||||||
|
|
||||||
glUniform4f(attr_loc, *value.to_a)
|
glUniform4f(attr_loc, *value.to_a)
|
||||||
|
|||||||
Reference in New Issue
Block a user