Skip to content

Commit

Permalink
Created sk_map, intro scene, menu music, floor texture & much more
Browse files Browse the repository at this point in the history
  • Loading branch information
iWas-Coder committed Apr 15, 2024
1 parent 7226d2f commit 6744fac
Show file tree
Hide file tree
Showing 13 changed files with 396 additions and 52 deletions.
Binary file added assets/cover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/music/menu.mp3
Binary file not shown.
140 changes: 140 additions & 0 deletions assets/shaders/pbr.frag.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* GNU Sparky --- A 5v5 character-based libre tactical shooter
* Copyright (C) 2024 Wasym A. Alonso
*
* This file is part of Sparky.
*
* Sparky is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sparky is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Sparky. If not, see <http://www.gnu.org/licenses/>.
*/


#version 330

#define PI 3.14159265358979323846
#define MAX_LIGHTS 4
#define LIGHT_POINT 1
#define LIGHT_DIRECTIONAL 0

struct Light {
int enabled;
int type;
vec3 position;
vec3 target;
vec4 color;
float intensity;
};

in vec3 fragPosition;
in vec2 fragTexCoord;
in vec4 fragColor;
in vec3 fragNormal;
in vec4 shadowPos;
in mat3 TBN;

uniform int numOfLights;
uniform sampler2D albedoMap;
uniform sampler2D mraMap;
uniform sampler2D normalMap;
uniform sampler2D emissiveMap;
uniform vec2 tiling;
uniform vec2 offset;
uniform int useTexAlbedo;
uniform int useTexNormal;
uniform int useTexMRA;
uniform int useTexEmissive;
uniform vec4 albedoColor;
uniform vec4 emissiveColor;
uniform float normalValue;
uniform float metallicValue;
uniform float roughnessValue;
uniform float aoValue;
uniform float emissivePower;
uniform Light lights[MAX_LIGHTS];
uniform vec3 viewPos;
uniform vec3 ambientColor;
uniform float ambient;

out vec4 finalColor;

vec3 SchlickFresnel(float hDotV, vec3 refl) {
return refl + ((1.0 - refl) * pow(1.0 - hDotV, 5.0));
}

float GgxDistribution(float nDotH, float roughness) {
float a = roughness * roughness * roughness * roughness;
float d = nDotH * nDotH * (a - 1.0) + 1.0;
d = PI * d * d;
return a / max(d, 0.0000001);
}

float GeomSmith(float nDotV,float nDotL,float roughness) {
float r = roughness + 1.0;
float k = (r * r) / 8.0;
float ik = 1.0 - k;
float ggx1 = nDotV / ((nDotV * ik) + k);
float ggx2 = nDotL / ((nDotL * ik) + k);
return ggx1 * ggx2;
}

vec3 ComputePBR(void) {
vec3 albedo = texture(albedoMap, vec2((fragTexCoord.x * tiling.x) + offset.x, (fragTexCoord.y * tiling.y) + offset.y)).rgb;
albedo = vec3(albedoColor.x * albedo.x, albedoColor.y * albedo.y, albedoColor.z * albedo.z);
float metallic = clamp(metallicValue, 0.0, 1.0);
float roughness = clamp(roughnessValue, 0.0, 1.0);
float ao = clamp(aoValue, 0.0, 1.0);
if (useTexMRA == 1) {
vec4 mra = texture(mraMap, vec2((fragTexCoord.x * tiling.x) + offset.x, (fragTexCoord.y * tiling.y) + offset.y)) * useTexMRA;
metallic = clamp(mra.r + metallicValue, 0.04, 1.0);
roughness = clamp(mra.g + roughnessValue, 0.04, 1.0);
ao = (mra.b + aoValue) * 0.5;
}
vec3 N = normalize(fragNormal);
if (useTexNormal == 1) {
N = texture(normalMap, vec2((fragTexCoord.x * tiling.x) + offset.y, (fragTexCoord.y * tiling.y) + offset.y)).rgb;
N = normalize((N * 2.0) - 1.0);
N = normalize(N * TBN);
}
vec3 V = normalize(viewPos - fragPosition);
vec3 emissive = vec3(0);
emissive = (texture(emissiveMap, vec2((fragTexCoord.x * tiling.x) + offset.x, (fragTexCoord.y * tiling.y) + offset.y)).rgb).g * emissiveColor.rgb * emissivePower * useTexEmissive;
vec3 baseRefl = mix(vec3(0.04), albedo.rgb, metallic);
vec3 lightAccum = vec3(0.0);
for (int i = 0; i < numOfLights; ++i) {
vec3 L = normalize(lights[i].position - fragPosition);
vec3 H = normalize(V + L);
float dist = length(lights[i].position - fragPosition);
float attenuation = 1.0 / (dist * dist * 0.23);
vec3 radiance = lights[i].color.rgb * lights[i].intensity * attenuation;
float nDotV = max(dot(N, V), 0.0000001);
float nDotL = max(dot(N, L), 0.0000001);
float hDotV = max(dot(H, V), 0.0);
float nDotH = max(dot(N, H), 0.0);
float D = GgxDistribution(nDotH, roughness);
float G = GeomSmith(nDotV, nDotL, roughness);
vec3 F = SchlickFresnel(hDotV, baseRefl);
vec3 spec = (D * G * F) / (4.0 * nDotV * nDotL);
vec3 kD = vec3(1.0) - F;
kD *= 1.0 - metallic;
lightAccum += (((kD * (albedo.rgb / PI)) + spec) * radiance * nDotL) * lights[i].enabled;
}
vec3 ambientFinal = (ambientColor + albedo) * ambient * 0.5;
return ambientFinal + (lightAccum * ao) + emissive;
}

void main(void) {
vec3 color = ComputePBR();
color = pow(color, color + vec3(1.0));
color = pow(color, vec3(1.0 / 2.2));
finalColor = vec4(color, 1.0);
}
56 changes: 56 additions & 0 deletions assets/shaders/pbr.vert.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* GNU Sparky --- A 5v5 character-based libre tactical shooter
* Copyright (C) 2024 Wasym A. Alonso
*
* This file is part of Sparky.
*
* Sparky is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sparky is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Sparky. If not, see <http://www.gnu.org/licenses/>.
*/


#version 330

const float normalOffset = 0.1;

in vec3 vertexPosition;
in vec2 vertexTexCoord;
in vec3 vertexNormal;
in vec3 vertexTangent;
in vec4 vertexColor;

uniform mat4 mvp;
uniform mat4 matModel;
uniform mat4 matNormal;
uniform vec3 lightPos;
uniform vec4 difColor;

out vec3 fragPosition;
out vec2 fragTexCoord;
out vec4 fragColor;
out vec3 fragNormal;
out mat3 TBN;

void main(void) {
vec3 vertexBinormal = cross(vertexNormal, vertexTangent);
mat3 normalMatrix = transpose(inverse(mat3(matModel)));
fragPosition = vec3(matModel * vec4(vertexPosition, 1.0f));
fragTexCoord = vertexTexCoord * 2.0;
fragNormal = normalize(normalMatrix * vertexNormal);
vec3 fragTangent = normalize(normalMatrix * vertexTangent);
fragTangent = normalize(fragTangent - (dot(fragTangent, fragNormal) * fragNormal));
vec3 fragBinormal = normalize(normalMatrix * vertexBinormal);
fragBinormal = cross(fragNormal, fragTangent);
TBN = transpose(mat3(fragTangent, fragBinormal, fragNormal));
gl_Position = mvp * vec4(vertexPosition, 1.0);
}
49 changes: 21 additions & 28 deletions src/sk_renderer_update.c → include/sk_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,24 @@
*/


#include <assert.h>
#include <sk_config.h>
#include <sk_defines.h>
#include <sk_renderer.h>

static void __update_main_menu(sk_state *s) {
if (IsKeyPressed(KEY_ENTER)) {
s->curr_scene.kind = SK_SCENE_KIND_GAMEPLAY;
sk_player_load(&s->player, SK_WEAPON_KIND_7MM);
DisableCursor();
}
}

void sk_renderer_update(sk_state *s) {
switch (s->curr_scene.kind) {
case SK_SCENE_KIND_MAIN_MENU:
__update_main_menu(s);
break;
case SK_SCENE_KIND_GAMEPLAY:
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) sk_weapon_shoot(&s->player.weapon);
if (IsKeyPressed(KEY_R)) sk_weapon_reload(&s->player.weapon);
sk_player_jump(&s->player);
sk_player_move(&s->player, sk_player_peek(&s->player));
break;
default:
assert(0 && "Unreachable");
}
}
#pragma once

#include <raylib.h>

typedef enum {
SK_MAP_CAMPING
} sk_map_kind;

typedef struct {
sk_map_kind kind;
Shader pbr_shader;
Model floor;
} sk_map;

sk_map sk_map_create(sk_map_kind kind);

void sk_map_destroy(sk_map *m);

void sk_map_load(sk_map *m);

void sk_map_draw(sk_map *m);
1 change: 1 addition & 0 deletions include/sk_scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#pragma once

typedef enum {
SK_SCENE_KIND_INTRO,
SK_SCENE_KIND_MAIN_MENU,
SK_SCENE_KIND_GAMEPLAY
} sk_scene_kind;
Expand Down
9 changes: 8 additions & 1 deletion include/sk_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#pragma once

#include <sk_map.h>
#include <sk_scene.h>
#include <sk_lobby.h>

Expand All @@ -34,9 +35,13 @@ typedef struct {
typedef struct {
u8 is_online;
sk_scene curr_scene;
Music menu_music;
union {
sk_lobby lobby;
sk_player player;
struct {
sk_map map;
sk_player player;
};
};
} sk_state;

Expand All @@ -46,4 +51,6 @@ void sk_state_global_assign_lobby(sk_state_global *sg, i8 *lobby_id, i8 *lobby_s

sk_state sk_state_create_offline(void);

void sk_state_destroy_offline(sk_state *s);

sk_state sk_state_create_online(u8 lobby_id);
4 changes: 1 addition & 3 deletions src/sk_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,11 @@
static void offline_mode(void) {
SK_LOG_WARN("Running in offline mode");
sk_state state = sk_state_create_offline();
sk_renderer_create();
sk_renderer_loop {
sk_renderer_update(&state);
sk_renderer_draw(&state);
}
sk_player_destroy(&state.player);
sk_renderer_destroy();
sk_state_destroy_offline(&state);
}

static i8 online_mode(const char *ip) {
Expand Down
58 changes: 58 additions & 0 deletions src/sk_map.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* GNU Sparky --- A 5v5 character-based libre tactical shooter
* Copyright (C) 2024 Wasym A. Alonso
*
* This file is part of Sparky.
*
* Sparky is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sparky is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Sparky. If not, see <http://www.gnu.org/licenses/>.
*/


#include <assert.h>
#include <sk_map.h>
#include <sk_defines.h>

sk_map sk_map_create(sk_map_kind kind) {
return (sk_map) {
.kind = kind,
.pbr_shader = LoadShader("assets/shaders/pbr.vert.glsl", "assets/shaders/pbr.frag.glsl")
};
}

void sk_map_destroy(sk_map *m) {
UnloadModel(m->floor);
UnloadShader(m->pbr_shader);
}

void sk_map_load(sk_map *m) {
m->floor = LoadModelFromMesh(GenMeshPlane(32, 32, 1, 1));
assert(m->floor.meshCount == 1);
assert(m->floor.materialCount == 1);
m->pbr_shader.locs[SHADER_LOC_MAP_ALBEDO] = GetShaderLocation(m->pbr_shader, "albedoMap");
m->pbr_shader.locs[SHADER_LOC_MAP_METALNESS] = GetShaderLocation(m->pbr_shader, "mraMap");
m->pbr_shader.locs[SHADER_LOC_MAP_NORMAL] = GetShaderLocation(m->pbr_shader, "normalMap");
m->pbr_shader.locs[SHADER_LOC_MAP_EMISSION] = GetShaderLocation(m->pbr_shader, "emissiveMap");
m->pbr_shader.locs[SHADER_LOC_COLOR_DIFFUSE] = GetShaderLocation(m->pbr_shader, "albedoColor");
m->pbr_shader.locs[SHADER_LOC_VECTOR_VIEW] = GetShaderLocation(m->pbr_shader, "viewPos");
// m->floor.materials[0].shader = m->pbr_shader;
m->floor.materials[0].maps[MATERIAL_MAP_ALBEDO].texture = LoadTexture("assets/textures/wood/albedo.png");
m->floor.materials[0].maps[MATERIAL_MAP_NORMAL].texture = LoadTexture("assets/textures/wood/normal.png");
m->floor.materials[0].maps[MATERIAL_MAP_ROUGHNESS].texture = LoadTexture("assets/textures/wood/roughness.png");
m->floor.materials[0].maps[MATERIAL_MAP_HEIGHT].texture = LoadTexture("assets/textures/wood/height.png");
m->floor.materials[0].maps[MATERIAL_MAP_OCCLUSION].texture = LoadTexture("assets/textures/wood/ao.png");
}

void sk_map_draw(sk_map *m) {
DrawModel(m->floor, (Vector3) { 0, 0, 0 }, 1, WHITE);
}
31 changes: 31 additions & 0 deletions src/sk_renderer.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,34 @@ void sk_renderer_destroy(void) {
CloseAudioDevice();
CloseWindow();
}

void sk_renderer_update(sk_state *s) {
switch (s->curr_scene.kind) {
case SK_SCENE_KIND_INTRO:
assert(IsMusicReady(s->menu_music));
if (!IsMusicStreamPlaying(s->menu_music)) {
SetMusicVolume(s->menu_music, 0.35f);
PlayMusicStream(s->menu_music);
}
else UpdateMusicStream(s->menu_music);
break;
case SK_SCENE_KIND_MAIN_MENU:
if (IsMusicStreamPlaying(s->menu_music)) UpdateMusicStream(s->menu_music);
if (IsKeyPressed(KEY_ENTER)) {
s->curr_scene.kind = SK_SCENE_KIND_GAMEPLAY;
StopMusicStream(s->menu_music);
sk_player_load(&s->player, SK_WEAPON_KIND_7MM);
sk_map_load(&s->map);
DisableCursor();
}
break;
case SK_SCENE_KIND_GAMEPLAY:
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) sk_weapon_shoot(&s->player.weapon);
if (IsKeyPressed(KEY_R)) sk_weapon_reload(&s->player.weapon);
sk_player_jump(&s->player);
sk_player_move(&s->player, sk_player_peek(&s->player));
break;
default:
assert(0 && "Unreachable");
}
}
Loading

0 comments on commit 6744fac

Please sign in to comment.