mirror of
https://github.com/cyberarm/i-mic-fps.git
synced 2025-12-16 08:02:36 +00:00
Initital shader lighting support
This commit is contained in:
@@ -1,35 +1,19 @@
|
|||||||
class IMICFPS
|
class IMICFPS
|
||||||
class Light
|
class Light
|
||||||
attr_reader :ambient, :diffuse, :specular, :position, :light_id
|
attr_reader :light_id
|
||||||
attr_accessor :x, :y, :z, :intensity
|
attr_accessor :ambient, :diffuse, :specular, :position, :intensity
|
||||||
def initialize(id:, x:,y:,z:,
|
def initialize(id:,
|
||||||
ambient: Vector.new(0.5, 0.5, 0.5, 1),
|
ambient: Vector.new(0.5, 0.5, 0.5, 1),
|
||||||
diffuse: Vector.new(1, 0.5, 0, 1), specular: Vector.new(0.2, 0.2, 0.2, 1),
|
diffuse: Vector.new(1, 0.5, 0, 1), specular: Vector.new(0.2, 0.2, 0.2, 1),
|
||||||
position: Vector.new(x, y, z, 0), intensity: 1)
|
position: Vector.new(0, 0, 0, 0), intensity: 1
|
||||||
|
)
|
||||||
@light_id = id
|
@light_id = id
|
||||||
@x,@y,@z = x,y,z
|
|
||||||
@intensity = intensity
|
@intensity = intensity
|
||||||
|
|
||||||
self.ambient = ambient
|
@ambient = ambient
|
||||||
self.diffuse = diffuse
|
@diffuse = diffuse
|
||||||
self.specular = specular
|
@specular = specular
|
||||||
self.position = position
|
@position = position
|
||||||
end
|
|
||||||
|
|
||||||
def ambient=(color)
|
|
||||||
@ambient = convert(color).pack("f*")
|
|
||||||
end
|
|
||||||
|
|
||||||
def diffuse=(color)
|
|
||||||
@diffuse = convert(color, true).pack("f*")
|
|
||||||
end
|
|
||||||
|
|
||||||
def specular=(color)
|
|
||||||
@specular = convert(color, true).pack("f*")
|
|
||||||
end
|
|
||||||
|
|
||||||
def position=(vertex)
|
|
||||||
@position = convert(vertex).pack("f*")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def draw
|
def draw
|
||||||
|
|||||||
@@ -29,8 +29,8 @@ class IMICFPS
|
|||||||
add_entity(Player.new(spawnpoint: @map_loader.spawnpoints.sample, manifest: Manifest.new(package: "base", name: "character")))
|
add_entity(Player.new(spawnpoint: @map_loader.spawnpoints.sample, manifest: Manifest.new(package: "base", name: "character")))
|
||||||
|
|
||||||
# TODO: Load lights from MapLoader
|
# TODO: Load lights from MapLoader
|
||||||
add_light(Light.new(id: available_light, x: 3, y: -6, z: 6))
|
add_light(Light.new(id: available_light, position: Vector.new(30, 10.0, 30)))
|
||||||
add_light(Light.new(id: available_light, x: 0, y: 100, z: 0, diffuse: Color.new(1.0, 0.5, 0.1)))
|
# add_light(Light.new(id: available_light, x: 0, y: 100, z: 0, diffuse: Color.new(1.0, 0.5, 0.1)))
|
||||||
end
|
end
|
||||||
|
|
||||||
def data
|
def data
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ class IMICFPS
|
|||||||
shader.set_uniform("hasTexture", object.model.has_texture?)
|
shader.set_uniform("hasTexture", object.model.has_texture?)
|
||||||
|
|
||||||
# TODO: Upload and use lights
|
# TODO: Upload and use lights
|
||||||
|
shader.set_uniform("lightPos", lights.first.position)
|
||||||
|
|
||||||
handleGlError
|
handleGlError
|
||||||
draw_model(object.model)
|
draw_model(object.model)
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ in vec3 outColor;
|
|||||||
in vec4 outNormal;
|
in vec4 outNormal;
|
||||||
in vec3 outUV;
|
in vec3 outUV;
|
||||||
in float outTextureID;
|
in float outTextureID;
|
||||||
|
in vec3 outLightPos;
|
||||||
|
in vec3 outFragPos;
|
||||||
|
|
||||||
// optimizing compilers are annoying at this stage of my understanding of GLSL
|
// optimizing compilers are annoying at this stage of my understanding of GLSL
|
||||||
vec4 lokiVar;
|
vec4 lokiVar;
|
||||||
@@ -12,5 +14,13 @@ vec4 lokiVar;
|
|||||||
void main() {
|
void main() {
|
||||||
lokiVar = vec4(outColor, 1.0) + outNormal + vec4(outUV, 1.0) + vec4(outTextureID, 1.0, 1.0, 1.0);
|
lokiVar = vec4(outColor, 1.0) + outNormal + vec4(outUV, 1.0) + vec4(outTextureID, 1.0, 1.0, 1.0);
|
||||||
lokiVar = normalize(lokiVar);
|
lokiVar = normalize(lokiVar);
|
||||||
gl_FragColor = vec4(outColor, 1.0);
|
|
||||||
|
vec3 lightDir = normalize(outLightPos - outFragPos);
|
||||||
|
vec3 ambient = vec3(0.5, 0.5, 0.35);
|
||||||
|
float diffuse = max(dot(vec3(outNormal), lightDir), -0.2);
|
||||||
|
vec3 specular = vec3(0, 0, 0);
|
||||||
|
|
||||||
|
vec3 result =(ambient + diffuse + specular) * outColor;
|
||||||
|
|
||||||
|
gl_FragColor = vec4(result, 1.0);
|
||||||
}
|
}
|
||||||
@@ -12,11 +12,15 @@ out vec4 outNormal;
|
|||||||
out vec3 outUV;
|
out vec3 outUV;
|
||||||
out float outTextureID;
|
out float outTextureID;
|
||||||
out float outHasTexture;
|
out float outHasTexture;
|
||||||
|
out vec3 outLightPos;
|
||||||
|
out vec3 outFragPos;
|
||||||
|
|
||||||
uniform mat4 projection;
|
uniform mat4 projection;
|
||||||
uniform mat4 view;
|
uniform mat4 view;
|
||||||
uniform mat4 model;
|
uniform mat4 model;
|
||||||
uniform int hasTexture;
|
uniform int hasTexture;
|
||||||
|
uniform vec3 lightPos;
|
||||||
|
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
// projection * view * model * position
|
// projection * view * model * position
|
||||||
@@ -26,6 +30,9 @@ void main() {
|
|||||||
outUV = inUV;
|
outUV = inUV;
|
||||||
outTextureID = inTextureID;
|
outTextureID = inTextureID;
|
||||||
outHasTexture = hasTexture;
|
outHasTexture = hasTexture;
|
||||||
|
outLightPos = lightPos;
|
||||||
|
|
||||||
|
outFragPos = vec3(model * vec4(inPosition, 1.0));
|
||||||
|
|
||||||
gl_Position = projection * view * model * vec4(inPosition, 1.0);
|
gl_Position = projection * view * model * vec4(inPosition, 1.0);
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user