Skip to content

Commit

Permalink
some changes and fixes xd
Browse files Browse the repository at this point in the history
  • Loading branch information
dummren committed Nov 24, 2023
1 parent f430d3e commit 9f5c777
Show file tree
Hide file tree
Showing 18 changed files with 1,122 additions and 679 deletions.
446 changes: 223 additions & 223 deletions data/mdl/test_ground.obj

Large diffs are not rendered by default.

859 changes: 429 additions & 430 deletions data/mdl/test_tree.obj

Large diffs are not rendered by default.

16 changes: 10 additions & 6 deletions data/shdr/canvas.frag
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#version 330 core

#define BLUR_STEPS 16
#define BLUR_INTENSITY 0.025f
#define BLUR_STEPS 8
#define BLUR_STEP 2
#define BLUR_INTENSITY 0.05f

in vec3 f_pos;
in vec2 f_uv;
Expand All @@ -19,11 +20,14 @@ vec4 bloomTex() {
vec2 texelSize = 1.0f / textureSize(u_brightTex, 0);

for (int i = 0; i < BLUR_STEPS; i++) {
tex += texture(u_brightTex, f_uv + vec2(texelSize.x * i, 0.0f)).rgb;
tex += texture(u_brightTex, f_uv - vec2(texelSize.x * i, 0.0f)).rgb;
vec2 uvH = vec2(texelSize.x * i * BLUR_STEP, 0.0f);
vec2 uvV = vec2(0.0f, texelSize.y * i * BLUR_STEP);

tex += texture(u_brightTex, f_uv + vec2(0.0f, texelSize.y * i)).rgb;
tex += texture(u_brightTex, f_uv - vec2(0.0f, texelSize.y * i)).rgb;
tex += texture(u_brightTex, f_uv + uvH).rgb;
tex += texture(u_brightTex, f_uv - uvH).rgb;

tex += texture(u_brightTex, f_uv + uvV).rgb;
tex += texture(u_brightTex, f_uv - uvV).rgb;
}

return vec4(tex * BLUR_INTENSITY, 1.0f);
Expand Down
50 changes: 50 additions & 0 deletions data/shdr/object.frag
Original file line number Diff line number Diff line change
@@ -1,23 +1,67 @@
#version 330 core

#define MAX_MATERIALS 8
#define MAX_POINT_LIGHTS 128
#define LIGHT_BRIGHTNESS_INTENSITY 1.5f

struct Material {
vec4 albedoCol, brightCol;
int albedoTexEnabled, brightTexEnabled;
sampler2D albedoTex, brightTex;
};

struct PointLight {
vec3 pos;
vec3 col;
float radius;
};

flat in uint f_index, f_materialIndex;
in vec3 f_pos, f_norm;
in vec2 f_uv;

uniform float u_time;
uniform mat4 u_projMat, u_viewMat, u_transMat;
uniform vec4 u_ambientCol;
uniform Material u_materials[MAX_MATERIALS];
uniform PointLight u_pointLights[MAX_POINT_LIGHTS];

layout (location = 0) out vec4 o_fragCol;
layout (location = 1) out vec4 o_brightCol;

vec3 diffuseLight() {
vec3 col;

for (int i = 0; i < MAX_POINT_LIGHTS; i++) {
if (u_pointLights[i].col == vec3(0.0f))
continue;

vec3 lightPos = u_pointLights[i].pos;
vec3 fragPos = vec3(u_transMat * vec4(f_pos, 1.0f));

vec3 lightCol = u_pointLights[i].col;
float lightRadius = u_pointLights[i].radius;

vec3 norm = mat3(u_transMat) * normalize(f_norm);
vec3 direction = lightPos - fragPos;

float amount = dot(normalize(norm), normalize(direction));
amount = max(amount, 0.0f);

float dist = distance(fragPos, lightPos);
float distanceFactor = clamp(1.0f - dist * dist
/ (lightRadius * lightRadius),
0.0f, 1.0f);
amount *= distanceFactor;

col += vec3(lightCol.r * amount,
lightCol.g * amount,
lightCol.b * amount);
}

return col;
}

void main() {
o_fragCol = u_materials[f_materialIndex].albedoCol;
o_brightCol = u_materials[f_materialIndex].brightCol;
Expand All @@ -27,4 +71,10 @@ void main() {

if (u_materials[f_materialIndex].brightTexEnabled == 1)
o_brightCol *= texture(u_materials[f_materialIndex].brightTex, f_uv);

o_fragCol *= u_ambientCol + vec4(diffuseLight(), 1.0f);
o_brightCol += u_ambientCol;
o_brightCol.rgb += diffuseLight();
o_brightCol.rgb -= vec3(1.0f);
o_brightCol.rgb *= LIGHT_BRIGHTNESS_INTENSITY;
}
3 changes: 2 additions & 1 deletion src/consts.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#ifndef __CG__CONSTS_H__
#define __CG__CONSTS_H__

#define TEX_FMT GL_RGBA4
#define TEX_FMT GL_RGBA8
#define TEX_FMT_DISTORT GL_RGBA16F

#define FONTSIZE_TITLE 50
#define FONTSIZE_TITLE_ALT 40
Expand Down
46 changes: 42 additions & 4 deletions src/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@
#include "res.h"
#include "shaders.h"
#include "obj.h"
#include "physics.h"
#include "lights.h"

vec4s cgGameAmbientColor = { 0.0f, 0.0f, 0.0f, 1.0f };
cg_physics_world_t *cgGamePhysicsWorld = NULL;
cg_player_t *cgGamePlayer = NULL;

cg_object_t *trees[4];
cg_object_t *ground;

void makePon() {
void makeObjects() {
for (size_t i = 0; i < 4; i++)
trees[i] = cgObjCreateObject("mdl/test_tree.obj\0");

Expand All @@ -28,21 +32,49 @@ void makePon() {
trees[2]->pos = (vec3s) { -1.0f, 0.0f, 1.0f };
trees[3]->pos = (vec3s) { 1.0f, 0.0f, 1.0f };

trees[2]->rot = (vec3s) { 1.54f, 0.44f, 145.4f };
trees[2]->rot = (vec3s) { 0.0f, 3.14f, 0.0f };

ground = cgObjCreateObject("mdl/test_ground.obj\0");

cgPhysicsWorldAdd(&cgGamePhysicsWorld,
cgPhysicsCollider(true, false,
(vec3s) { 0.25f, 0.25f, 0.25f },
(vec3s) { -1.0f, 0.25f, -1.0f }));

cgPhysicsWorldAdd(&cgGamePhysicsWorld,
cgPhysicsCollider(true, false,
(vec3s) { 0.25f, 0.25f, 0.25f },
(vec3s) { 1.0f, 0.25f, -1.0f }));

cgPhysicsWorldAdd(&cgGamePhysicsWorld,
cgPhysicsCollider(true, false,
(vec3s) { 0.25f, 0.25f, 0.25f },
(vec3s) { -1.0f, 0.25f, 1.0f }));

cgPhysicsWorldAdd(&cgGamePhysicsWorld,
cgPhysicsCollider(true, false,
(vec3s) { 0.25f, 0.25f, 0.25f },
(vec3s) { 1.0f, 0.25f, 1.0f }));
}

cg_start_func_t cgGameStart() {
cgGamePhysicsWorld = cgPhysicsWorld();
cgGamePlayer = cgPlayer();
makePon();
makeObjects();
}

cg_update_func_t cgGameUpdate() {
cgPlayerUpdate(cgGamePlayer);
}

cg_draw_func_t cgGameDraw() {
cgLightsPoint(0.0f, 3.5f, 0.0f,
1.5f, 0.0f, 0.0f,
10.0f);
cgLightsPoint(10.0f, 5.0f, 5.0f,
1.2f, 1.2f, 1.2f,
10.0f);

for (size_t i = 0; i < 4; i++)
cgObjectDraw(trees[i],
cgCameraProj(cgGamePlayer->cam),
Expand All @@ -53,5 +85,11 @@ cg_draw_func_t cgGameDraw() {
}

cg_end_func_t cgGameEnd() {
free(cgGamePlayer);
cgPhysicsWorldDelete(&cgGamePhysicsWorld);
cgPlayerDelete(&cgGamePlayer);

for (size_t i = 0; i < 4; i++)
cgObjectDelete(&trees[i]);

cgObjectDelete(&ground);
}
5 changes: 5 additions & 0 deletions src/game.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
#ifndef __CG__GAME_H__
#define __CG__GAME_H__

#include <cglm/struct.h>

#include "main.h"
#include "player.h"
#include "physics.h"

extern vec4s cgGameAmbientColor;
extern cg_physics_world_t *cgGamePhysicsWorld;
extern cg_player_t *cgGamePlayer;

cg_start_func_t cgGameStart();
Expand Down
50 changes: 50 additions & 0 deletions src/lights.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "lights.h"

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

#include "cglm/struct.h"

cg_lights_point_t cgLightsPoints[CG_LIGHTS_POINTS_MAX];

void cgLightsInit() {
cgLightsPointsClear();
}

bool cgLightsIsPointSet(cg_lights_point_t point) {
cg_lights_point_t defaultPoint = {
(vec3s) { 0.0f, 0.0f, 0.0f },
(vec3s) { 0.0f, 0.0f, 0.0f },
0.0f,
};

return memcmp(point.col.raw, defaultPoint.col.raw, sizeof(vec3s)) == 0;
}

void cgLightsPointsClear() {
for (size_t i = 0; i < CG_LIGHTS_POINTS_MAX; i++)
cgLightsPoints[i] = (cg_lights_point_t) {
(vec3s) { 0.0f, 0.0f, 0.0f },
(vec3s) { 0.0f, 0.0f, 0.0f },
0.0f,
};
}

void cgLightsPoint(const float x, const float y, const float z,
const float r, const float g, const float b,
const float radius) {
for (size_t i = 0; i < CG_LIGHTS_POINTS_MAX; i++)
if (cgLightsIsPointSet(cgLightsPoints[i])) {
cg_lights_point_t light;

light.pos = (vec3s) { x, y, z };
light.col = (vec3s) { r, g, b };
light.radius = radius;

cgLightsPoints[i] = light;

break;
}
}
25 changes: 25 additions & 0 deletions src/lights.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef __CG__LIGHTS_H__
#define __CG__LIGHTS_H__

#include <stdbool.h>

#include "cglm/struct.h"

#define CG_LIGHTS_POINTS_MAX 128

typedef struct {
vec3s pos;
vec3s col;
float radius;
} cg_lights_point_t;

extern cg_lights_point_t cgLightsPoints[CG_LIGHTS_POINTS_MAX];

void cgLightsInit();
bool cgLightsIsPointSet(cg_lights_point_t);
void cgLightsPointsClear();
void cgLightsPoint(const float, const float, const float,
const float, const float, const float,
const float);

#endif // __CG__LIGHTS_H__
11 changes: 9 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "fonts.h"
#include "sfx.h"
#include "consts.h"
#include "lights.h"

const cg_start_func_t (*SCENES_START_FUNCS[SCENES_COUNT])() = {
cgMenuStart,
Expand Down Expand Up @@ -129,6 +130,7 @@ int main(int argc, char *argv[]) {
cgTextInit();
cgFontsInit();
cgSfxInit();
cgLightsInit();

cg_shader_t prog = cgShadersCanvasProg;
cg_mesh_t *mesh = cgMesh();
Expand All @@ -155,17 +157,20 @@ int main(int argc, char *argv[]) {
glGenRenderbuffers(1, &renderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);

unsigned int fbTexFmt =
rand() > RAND_MAX / 512 ? TEX_FMT : TEX_FMT_DISTORT;

cg_texture_t fbFragTex =
cgTextureEmpty(GL_FLOAT,
WND_WIDTH, WND_HEIGHT,
TEX_FMT, GL_RGBA,
fbTexFmt, GL_RGBA,
GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE,
CGS_TEX_FLTR, CGS_TEX_FLTR);

cg_texture_t fbBrightTex =
cgTextureEmpty(GL_FLOAT,
WND_WIDTH, WND_HEIGHT,
TEX_FMT, GL_RGBA,
fbTexFmt, GL_RGBA,
GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE,
CGS_TEX_FLTR, CGS_TEX_FLTR);

Expand Down Expand Up @@ -243,6 +248,8 @@ int main(int argc, char *argv[]) {
cgWindowSwapBuffers(window);
cgWindowResetPressedKeysB(window);
cgWindowResetRelCursorPos(window);

cgLightsPointsClear();
}

SCENES_END_FUNCS[scene]();
Expand Down
8 changes: 5 additions & 3 deletions src/mesh.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ void cgMeshDraw(const cg_mesh_t *mesh) {
cgMeshUnbind(mesh);
}

void cgMeshDelete(cg_mesh_t *mesh) {
glDeleteVertexArrays(1, &mesh->vao);
glDeleteBuffers(1, &mesh->ebo);
void cgMeshDelete(cg_mesh_t **mesh) {
glDeleteVertexArrays(1, &(*mesh)->vao);
glDeleteBuffers(1, &(*mesh)->ebo);
free(*mesh);
*mesh = NULL;
}
2 changes: 1 addition & 1 deletion src/mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ void cgMeshStoreIndices(cg_mesh_t*,

void cgMeshDraw(const cg_mesh_t*);

void cgMeshDelete(cg_mesh_t*);
void cgMeshDelete(cg_mesh_t**);

#endif // __CG__MESH_H__
Loading

0 comments on commit 9f5c777

Please sign in to comment.