1 Commits

Author SHA1 Message Date
07d2d8ea05 Work In Progress 2020-03-25 16:16:47 -05:00
22 changed files with 93 additions and 320 deletions

View File

@@ -2,7 +2,6 @@ source "https://rubygems.org"
gem "opengl-bindings", require: "opengl"
gem "cyberarm_engine", git: "https://github.com/cyberarm/cyberarm_engine"
gem "nokogiri", ">= 1.11.0.rc1"
gem "async-websocket"
group(:packaging) do
gem "ocra"

View File

@@ -1,16 +0,0 @@
require "async/websocket"
require_relative "lib/networking/director"
require_relative "lib/networking/packet_handler"
require_relative "lib/networking/server"
require_relative "lib/networking/client"
require_relative "lib/networking/backends/memory_server"
require_relative "lib/networking/backends/memory_connection"
director = IMICFPS::Networking::Director.new(mode: :server, hostname: "0.0.0.0", port: 56789, interface: IMICFPS::Networking::MemoryServer)
director.define_singleton_method(:tick) do |dt|
puts "Ticked: #{dt}"
end
director.run.join

View File

@@ -106,15 +106,6 @@ require_relative "lib/scenes/turn_table"
require_relative "lib/crosshair"
require_relative "lib/demo"
require_relative "lib/networking/director"
require_relative "lib/networking/packet_handler"
require_relative "lib/networking/client"
require_relative "lib/networking/server"
require_relative "lib/networking/connection"
require_relative "lib/networking/backends/memory_server"
require_relative "lib/networking/backends/memory_connection"
require_relative "lib/window"
require_relative "lib/tools/asset_viewer"

View File

@@ -32,6 +32,9 @@ class IMICFPS
end
add_entity(Player.new(spawnpoint: @map_parser.spawnpoints.sample, manifest: Manifest.new(package: "base", name: "character")))
# add_light(Light.new(id: available_light, position: Vector.new(30, 10.0, 30)))
# add_light(Light.new(id: available_light, position: Vector.new(0, 100, 0), diffuse: Color.new(1.0, 0.5, 0.1)))
end
def data
@@ -56,6 +59,7 @@ class IMICFPS
@collision_manager.update
@entities.each(&:update)
# @lights.each(&:update)
end
end
end

View File

@@ -1,6 +0,0 @@
class IMICFPS
module Networking
class MemoryConnection < Connection
end
end
end

View File

@@ -1,6 +0,0 @@
class IMICFPS
module Networking
class MemoryServer < Server
end
end
end

View File

@@ -1,18 +0,0 @@
class IMICFPS
module Networking
class Client
def initialize(socket:)
@socket = socket
end
def read
end
def write
end
def close
end
end
end
end

View File

@@ -1,14 +0,0 @@
class IMICFPS
module Networking
class Connection
def initialize(hostname:, port:)
end
def connect
end
def close
end
end
end
end

View File

@@ -1,61 +0,0 @@
class IMICFPS
module Networking
class Director
attr_reader :mode, :hostname, :port, :tick_rate, :storage
def initialize(mode:, hostname:, port:, interface:, state: nil, tick_rate: 2)
@mode = mode
@hostname = hostname
@port = port
@state = state
@tick_rate = (1000.0 / tick_rate) / 1000.0
case @mode
when :server
@server = interface.new(hostname: @hostname, port: @port)
when :connection
@connection = interface.new(hostname: @hostname, port: @port)
when :memory
@server = interface[:server].new(hostname: @hostname, port: @port)
@connection = interface[:connection].new(hostname: @hostname, port: @port)
else
raise ArgumentError, "Expected mode to be :server, :connection, or :memory, not #{mode.inspect}"
end
@last_tick_time = milliseconds
@directing = true
@storage = {}
end
def run
Thread.start do |thread|
while(@directing)
dt = milliseconds - @last_tick_time
tick(dt)
@server.update if @server
@connection.update if @connection
@last_tick_time = milliseconds
sleep(@tick_rate)
end
end
end
def tick(dt)
end
def shutdown
@directing = false
@clients.each(&:close)
@server.update if @server
@connection.update if @connection
end
def milliseconds
Process.clock_gettime(Process::CLOCK_MONOTONIC)
end
end
end
end

View File

@@ -1,11 +0,0 @@
class IMICFPS
module Networking
module Events
def on_connect(client)
end
def on_disconnect(client)
end
end
end
end

View File

@@ -1,18 +0,0 @@
class IMICFPS
module Networking
class Packet
def initialize(type:, payload:)
end
def self.encode(packet)
"#{packet.type}|#{packet.payload}"
end
def self.decode(string)
split = string.split("|")
Packet.new(split.first, split.last)
end
end
end
end

View File

@@ -1,6 +0,0 @@
class IMICFPS
module Networking
class PacketHandler
end
end
end

View File

@@ -1,26 +0,0 @@
class IMICFPS
module Networking
MAX_CLIENTS = 32
class Server
attr_reader :hostname, :port, :max_clients, :clients
def initialize(hostname:, port:, max_clients: MAX_CLIENTS)
@hostname = hostname
@port = port
@max_clients = max_clients
@clients = []
@socket = nil
end
def bind
end
def broadcast(packet)
end
def close
end
end
end
end

View File

@@ -5,7 +5,7 @@ class IMICFPS
attr_reader :screen_vbo, :vertices, :uvs
def initialize
@framebuffer = nil
@buffers = [:position, :diffuse, :normal, :texcoord]
@buffers = [:position, :diffuse, :normal, :texcoord, :scene]
@textures = {}
@screen_vbo = nil
@ready = false
@@ -86,9 +86,9 @@ class IMICFPS
@textures[@buffers[i]] = texture_id
glBindTexture(GL_TEXTURE_2D, texture_id)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, width, height, 0, GL_RGBA, GL_FLOAT, nil)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0, GL_RGBA, GL_FLOAT, nil)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, texture_id, 0)
end
@@ -101,7 +101,7 @@ class IMICFPS
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, nil)
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, texture_id, 0)
draw_buffers = [ GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 ]
draw_buffers = @buffers.each_with_index.map { |b ,i| Object.const_get("GL_COLOR_ATTACHMENT#{i}") }
glDrawBuffers(draw_buffers.size, draw_buffers.pack("I*"))
end

View File

@@ -16,7 +16,7 @@ class IMICFPS
end
def render(camera, lights, entities)
if Shader.available?("default") && Shader.available?("render_screen")
if Shader.available?("default") && Shader.available?("render_screen") && Shader.available?("lighting")
@g_buffer.bind_for_writing
gl_error?
@@ -24,19 +24,6 @@ class IMICFPS
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
Shader.use("default") do |shader|
lights.each_with_index do |light, i|
shader.uniform_float("lights[#{i}.end", -1.0);
shader.uniform_float("lights[#{i}.type", light.type);
shader.uniform_vec3("lights[#{i}].position", light.position)
shader.uniform_vec3("lights[#{i}].ambient", light.ambient)
shader.uniform_vec3("lights[#{i}].diffuse", light.diffuse)
shader.uniform_vec3("lights[#{i}].specular", light.specular)
end
gl_error?
shader.uniform_integer("totalLights", lights.size)
entities.each do |entity|
next unless entity.visible && entity.renderable
@@ -52,6 +39,9 @@ class IMICFPS
end
end
lighting(lights)
post_processing
@g_buffer.unbind_framebuffer
gl_error?
@@ -59,8 +49,6 @@ class IMICFPS
@g_buffer.bind_for_reading
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0)
# lighting(lights)
post_processing
render_framebuffer
@g_buffer.unbind_framebuffer
@@ -96,25 +84,32 @@ class IMICFPS
end
def lighting(lights)
@g_buffer.set_read_buffer(:position)
glBlitFramebuffer(0, 0, @g_buffer.width, @g_buffer.height,
0, 0, @g_buffer.width / 2, @g_buffer.height / 2,
GL_COLOR_BUFFER_BIT, GL_LINEAR)
if Shader.available?("lighting")
Shader.use("lighting") do |shader|
lights.each do |light|
shader.uniform_integer("inLightType", light.type);
shader.uniform_vec3("inLightPosition", light.position)
shader.uniform_vec3("inLightAmbient", light.ambient)
shader.uniform_vec3("inLightDiffuse", light.diffuse)
shader.uniform_vec3("inLightSpecular", light.specular)
@g_buffer.set_read_buffer(:diffuse)
glBlitFramebuffer(0, 0, @g_buffer.width, @g_buffer.height,
0, @g_buffer.height / 2, @g_buffer.width / 2, @g_buffer.height,
GL_COLOR_BUFFER_BIT, GL_LINEAR)
glBindVertexArray(@g_buffer.screen_vbo)
glEnableVertexAttribArray(0)
glEnableVertexAttribArray(1)
@g_buffer.set_read_buffer(:normal)
glBlitFramebuffer(0, 0, @g_buffer.width, @g_buffer.height,
@g_buffer.width / 2, @g_buffer.height / 2, @g_buffer.width, @g_buffer.height,
GL_COLOR_BUFFER_BIT, GL_LINEAR)
glDisable(GL_DEPTH_TEST)
glEnable(GL_BLEND)
@g_buffer.set_read_buffer(:texcoord)
glBlitFramebuffer(0, 0, @g_buffer.width, @g_buffer.height,
@g_buffer.width / 2, 0, @g_buffer.width, @g_buffer.height / 2,
GL_COLOR_BUFFER_BIT, GL_LINEAR)
glDrawArrays(GL_TRIANGLES, 0, @g_buffer.vertices.size)
glDisableVertexAttribArray(1)
glDisableVertexAttribArray(0)
glBindVertexArray(0)
end
gl_error?
end
end
end
def post_processing
@@ -131,7 +126,7 @@ class IMICFPS
glEnable(GL_BLEND)
glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:diffuse))
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:scene))
glDrawArrays(GL_TRIANGLES, 0, @g_buffer.vertices.size)

View File

@@ -10,7 +10,7 @@ class IMICFPS
end
def preload_default_shaders
shaders = ["default", "render_screen"]
shaders = ["default", "render_screen", "lighting"]
shaders.each do |shader|
Shader.new(
name: shader,

View File

@@ -9,7 +9,6 @@ class IMICFPS
@player = @map.find_entity_by(name: "character")
@camera = Camera.new(position: @player.position.clone)
@camera.attach_to(@player)
@director = Networking::Director.new(mode: :memory, hostname: "i-mic.rubyclan.org", port: 56789, interface: { server: Networking::MemoryServer, connection: Networking::MemoryConnection }, state: self)
@crosshair = Crosshair.new
@@ -40,7 +39,6 @@ class IMICFPS
control_player
@camera.update
@director.tick(window.dt)
if window.config.get(:debug_options, :stats)
@text.text = update_text

View File

@@ -1,19 +1,15 @@
# version 330 core
@include "light_struct"
layout(location = 0) out vec3 fragPosition;
layout (location = 0) out vec4 fragPosition;
layout (location = 1) out vec4 fragColor;
layout (location = 2) out vec3 fragNormal;
layout (location = 3) out vec3 fragUV;
layout (location = 2) out vec4 fragNormal;
layout (location = 3) out vec4 fragUV;
in vec3 outPosition;
in vec3 outColor;
in vec3 outNormal;
in vec3 outUV;
in float outTextureID;
in Light outLights[MAX_LIGHTS];
flat in int outTotalLights;
in vec3 outFragPos;
in vec3 outCameraPos;
flat in int outHasTexture;
@@ -21,88 +17,17 @@ flat in int outDisableLighting;
uniform sampler2D diffuse_texture;
// optimizing compilers are annoying at this stage of my understanding of GLSL
vec4 lokiVar;
// https://learnopengl.com/Lighting/Multiple-lights
vec3 calculatePointLight(Light light) {
vec3 viewDir = normalize(outCameraPos - outFragPos);
vec3 lightDir = normalize(light.position - outFragPos);
float diff = max(dot(vec3(outNormal), lightDir), 0.0);
vec3 reflectDir = reflect(-lightDir, vec3(outNormal));
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 16.0);
float distance = length(light.position - outFragPos);
float attenuation = 1.0 / (1.0 + 0.09 * distance +
0.032 * (distance * distance));
vec3 ambient = light.ambient * outColor;
vec3 diffuse = light.diffuse * outColor;
vec3 specular = light.specular * spec * vec3(1.0, 1.0, 1.0);
ambient *= attenuation;
diffuse *= attenuation;
specular *= attenuation;
return (ambient + diffuse + specular);
}
// https://learnopengl.com/Lighting/Basic-Lighting
vec3 calculateBasicLight(Light light) {
vec3 lightDir = normalize(light.position - outFragPos);
float ambientStrength = 0.25;
vec3 ambient = ambientStrength * light.ambient;
float diff = max(dot(normalize(vec3(outNormal)), lightDir), 0.0);
vec3 diffuse = diff * light.diffuse;
float specularStrength = 0.5;
vec3 viewDir = normalize(outCameraPos - outFragPos);
vec3 reflectDir = reflect(-lightDir, outNormal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
vec3 specular = specularStrength * spec * light.specular;
return vec3(ambient + diffuse + specular);
}
vec3 calculateLighting() {
vec3 result = vec3(0.0, 0.0, 0.0);
for (int i = 0; i < min(outTotalLights, MAX_LIGHTS); i++) {
if (int(outLights[i].type) == 0) {
result += calculateBasicLight(outLights[i]);
} else if (int(outLights[i].type) == 1) {
result += calculateBasicLight(outLights[i]);
}
}
return result;
}
void main() {
lokiVar = vec4(outColor, 1.0) + vec4(outNormal, 1.0) + vec4(outUV, 1.0) + vec4(outTextureID, 1.0, 1.0, 1.0);
lokiVar = normalize(lokiVar);
vec3 result;
if (outHasTexture == 0) {
if (outDisableLighting == 1.0) {
result = outColor + 0.25;
} else {
result = calculateLighting() * outColor;
}
result = outColor;
} else {
if (outDisableLighting == 1.0) {
result = texture(diffuse_texture, outUV.xy).xyz + 0.25;
} else {
result = calculateLighting() * texture(diffuse_texture, outUV.xy).xyz;
}
result = texture(diffuse_texture, outUV.xy).xyz;
}
fragPosition = outPosition;
fragPosition = vec4(outPosition, 1.0);
fragColor = vec4(result, 1.0);
fragNormal = outNormal;
fragUV = outUV;
fragNormal = vec4(outNormal, 1.0);
fragUV = vec4(outUV, 1.0);
}

View File

@@ -0,0 +1,17 @@
# version 330 core
layout (location = 4) out vec4 sceneColor;
flat in int outLightType;
in vec3 outLightPosition;
in vec3 outLightAmbient;
in vec3 outLightDiffuse;
in vec3 outLightSpecular;
in vec2 outTexCoords;
@include "light_struct"
void main() {
sceneColor = vec4(1.0, 0.5, 0.25, 1.0);
}

View File

@@ -39,8 +39,6 @@ void main() {
outUV = inUV;
outTextureID = inTextureID;
outHasTexture = hasTexture;
outLights = lights;
outTotalLights = totalLights;
outCameraPos = cameraPos;
outDisableLighting = disableLighting;

View File

@@ -0,0 +1,28 @@
#version 330 core
layout (location = 0) in vec3 inPosition;
layout (location = 1) in vec2 inTexCoords;
in int inLightType;
in vec3 inLightPosition;
in vec3 inLightAmbient;
in vec3 inLightDiffuse;
in vec3 inLightSpecular;
flat out int outLightType;
out vec3 outLightPosition;
out vec3 outLightAmbient;
out vec3 outLightDiffuse;
out vec3 outLightSpecular;
out vec2 outTexCoords;
void main() {
gl_Position = vec4(inPosition.x, inPosition.y, inPosition.z, 1.0);
outLightType = inLightType;
outLightPosition = inLightPosition;
outLightAmbient = inLightAmbient;
outLightDiffuse = inLightDiffuse;
outLightSpecular = inLightSpecular;
outTexCoords = inTexCoords;
}