Skip to content

Commit

Permalink
skybox
Browse files Browse the repository at this point in the history
  • Loading branch information
computermouth committed Nov 14, 2023
1 parent 9a7e318 commit 210adea
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 12 deletions.
2 changes: 1 addition & 1 deletion c1k3-assets
5 changes: 5 additions & 0 deletions entity_player.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ void entity_player_init(entity_t * e) {
hud.health = e->_health;
hud.ammo = -1;

// spawn skybox
entity_params_t skybox_ep = map_entt_params_from_eid(ENTITY_ID_SKYBOX);
skybox_ep.position = e->p;
game_spawn(&skybox_ep);

entity_player_hud_update_health(hud.health);
entity_player_hud_update_ammo(hud.ammo);
}
Expand Down
49 changes: 49 additions & 0 deletions entity_skybox.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

#include "entity_skybox.h"
#include "game.h"
#include "math.h"
#include "render.h"

void entity_skybox_init(entity_t * e);
void entity_skybox_draw_model(entity_t * e);
void entity_skybox_update(entity_t * e);

void entity_skybox_constructor(entity_t * e) {
entity_constructor(e);
e->_draw_model = entity_skybox_draw_model;
e->_update = entity_skybox_update;
entity_skybox_init(e);
}

void entity_skybox_init(entity_t * e) {
// todo
entity_set_model(e);
e->s = (vec3_t){0};
}

void entity_skybox_update(entity_t * e) {
// track player
if (game_entity_player != NULL) {
e->p = game_entity_player->p;
}

e->_yaw += 0.001 * game_tick;
e->_pitch += 0.001 * game_tick;

e->_draw_model(e);
}

void entity_skybox_draw_model(entity_t * e) {
draw_call_t call = {
.pos = e->p,
.yaw = e->_yaw,
.pitch = e->_pitch,
.texture = e->_texture,
.f1 = e->_model.frames[0],
.f2 = e->_model.frames[0],
.mix = 0.0f,
.unlit = 1,
.num_verts = e->_model.nv
};
r_draw(call);
}
9 changes: 9 additions & 0 deletions entity_skybox.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

#ifndef ENTITY_SKYBOX_H
#define ENTITY_SKYBOX_H

#include "entity.h"

void entity_skybox_constructor(entity_t *e);

#endif
2 changes: 1 addition & 1 deletion game.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ void game_run(float time_now) {
game_tick = 0.001f;
game_time = time_now;

r_prepare_frame(0.1, 0.2, 0.5);
r_prepare_frame(0.0, 0.0, 0.0);

vector_clear(game_entities_list_all);
vector_clear(game_entities_list_enemies);
Expand Down
2 changes: 1 addition & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ int main(int argc, char* argv[]) {
return 1;
}
SDL_GL_CreateContext(window);
SDL_GL_SetSwapInterval(0);
// SDL_GL_SetSwapInterval(0);
// todo, vsync?

game_load();
Expand Down
4 changes: 4 additions & 0 deletions map.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "render.h"
#include "entity.h"
#include "entity_player.h"
#include "entity_skybox.h"
#include "entity_enemy_grunt.h"
#include "entity_enemy_mutant.h"
#include "entity_enemy_enforcer.h"
Expand Down Expand Up @@ -57,6 +58,9 @@ void map_init() {
map_entity_table[ENTITY_ID_PLAYER] = (map_entity_table_t) {
"player", entity_player_constructor
};
map_entity_table[ENTITY_ID_SKYBOX] = (map_entity_table_t) {
"skybox", entity_skybox_constructor
};
map_entity_table[ENTITY_ID_ENEMY_GRUNT] = (map_entity_table_t) {
"grunt", entity_enemy_grunt_constructor
};
Expand Down
1 change: 1 addition & 0 deletions map.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ typedef struct {
// todo, move entity table and init to another file?
typedef enum {
ENTITY_ID_PLAYER,
ENTITY_ID_SKYBOX,
ENTITY_ID_ENEMY_GRUNT,
ENTITY_ID_ENEMY_MUTANT,
ENTITY_ID_ENEMY_ENFORCER,
Expand Down
11 changes: 3 additions & 8 deletions render.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ GLint r_u_mouse;
GLint r_u_pos;
GLint r_u_rotation;
GLint r_u_frame_mix;
GLint r_u_unlit;

// Vertex attribute location for mixing
GLint r_va_p2;
Expand Down Expand Up @@ -150,6 +151,7 @@ void r_init() {
r_u_pos = glGetUniformLocation(shader_program, "mp");
r_u_rotation = glGetUniformLocation(shader_program, "mr");
r_u_frame_mix = glGetUniformLocation(shader_program, "f");
r_u_unlit = glGetUniformLocation(shader_program, "unlit");

glGenBuffers(1, &vertex_buffer);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
Expand Down Expand Up @@ -280,6 +282,7 @@ void r_end_frame() {
glUniform3f(r_u_pos, c.pos.x, c.pos.y, c.pos.z);
glUniform2f(r_u_rotation, c.yaw, c.pitch);
glUniform1f(r_u_frame_mix, c.mix);
glUniform1i(r_u_unlit, c.unlit);

if (vo != (c.f2 - c.f1)) {
vo = (c.f2 - c.f1);
Expand All @@ -295,16 +298,8 @@ void r_end_frame() {

glBindFramebuffer(GL_READ_FRAMEBUFFER, offscreen_fbo);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, default_fbo);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBlitFramebuffer(0, 0, INTERNAL_W, INTERNAL_H, r_padx, r_pady, r_current_window_width - r_padx, r_current_window_height - r_pady, GL_COLOR_BUFFER_BIT, GL_NEAREST);

glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
r_vertex_attrib(shader_program, "p", 3, 8, 0);
r_vertex_attrib(shader_program, "t", 2, 8, 3);
r_vertex_attrib(shader_program, "n", 3, 8, 5);
r_va_p2 = r_vertex_attrib(shader_program, "p2", 3, 8, 0);
r_va_n2 = r_vertex_attrib(shader_program, "n2", 3, 8, 5);

vector_clear(r_draw_calls);
}

Expand Down
1 change: 1 addition & 0 deletions render.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ typedef struct {
GLint f1; // todo, first frame of interpolation
GLint f2; // second frame of interpolation
float mix;
int unlit;
int num_verts;
} draw_call_t;

Expand Down
8 changes: 7 additions & 1 deletion shader.frag
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
// Lights [(x,y,z), [r,g,b], ...]
"uniform vec3 l["STR(R_MAX_LIGHT_V3)"];"

// flag to turn off lighting
"varying float f_unlit;"

"void main(void){"
"gl_FragColor=texture2D(s,vt);"

Expand All @@ -33,8 +36,11 @@
// Debug: full bright lights
// "vl = vec3(2,2,2);"

"vec3 p = pow(vl,vec3(0.75));"
"if (f_unlit != 0.) { p = vec3(0.75); }"

"gl_FragColor.rgb=floor("
"gl_FragColor.rgb*pow(vl,vec3(0.75))" // Light, Gamma
"gl_FragColor.rgb*p" // Light, Gamma
"*16.0+0.5"
")/16.0;" // Reduce final output color for some extra dirty looks
"}"
6 changes: 6 additions & 0 deletions shader.vert
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
// Blend factor between the two vertex positions
"uniform float f;"

// Flag to turn off lighting in the frag shadeer
"uniform int unlit;"
"varying float f_unlit;"

// Generate a rotation Matrix around the x,y,z axis;
// Used for model rotation and camera yaw
"mat4 rx(float r){"
Expand Down Expand Up @@ -55,6 +59,8 @@
"}"

"void main(void){"
"f_unlit = float(unlit);"

// Rotation Matrixes for model rotation
"mat4 "
"mry=ry(mr.x),"
Expand Down

0 comments on commit 210adea

Please sign in to comment.