Render own model with depth offset

master
Elias Fleckenstein 2022-04-20 12:41:18 +02:00
parent dde805203b
commit 52b661f165
No known key found for this signature in database
GPG Key ID: 06927A5199D6C9B2
9 changed files with 50 additions and 18 deletions

View File

@ -10,11 +10,14 @@ uniform mat4 VP;
uniform float daylight;
uniform float ambientLight;
uniform vec3 lightDir;
uniform float depthOffset;
void main()
{
vec4 worldSpace = model * vec4(vertexPosition, 1.0);
gl_Position = VP * worldSpace;
if (gl_Position.z > -1.0)
gl_Position.z = max(-1.0, gl_Position.z - depthOffset);
fragmentPosition = worldSpace.xyz;
fragmentTextureCoordinates = vertexPosition;

View File

@ -10,11 +10,14 @@ uniform mat4 VP;
uniform float daylight;
uniform float ambientLight;
uniform vec3 lightDir;
uniform float depthOffset;
void main()
{
vec4 worldSpace = model * vec4(vertexPosition, 1.0);
gl_Position = VP * worldSpace;
if (gl_Position.z > -1.0)
gl_Position.z = max(-1.0, gl_Position.z - depthOffset);
fragmentPosition = worldSpace.xyz;
fragmentColor = vertexColor;

View File

@ -37,6 +37,7 @@ Mesh client_entity_cube = {
static GLuint shader_prog;
static GLint loc_VP;
static GLint loc_depthOffset;
static LightShader light_shader;
static Map entities;
static List nametagged;
@ -177,6 +178,7 @@ bool client_entity_gfx_init()
free(shader_defs);
loc_VP = glGetUniformLocation(shader_prog, "VP"); GL_DEBUG
loc_depthOffset = glGetUniformLocation(shader_prog, "depthOffset"); GL_DEBUG
EntityVertex vertices[6][6];
for (int f = 0; f < 6; f++) {
@ -195,6 +197,8 @@ bool client_entity_gfx_init()
light_shader.prog = shader_prog;
light_shader_locate(&light_shader);
client_entity_depth_offset(0.0f);
return true;
}
@ -214,6 +218,11 @@ void client_entity_gfx_update()
pthread_mutex_unlock(&mtx_nametagged);
}
void client_entity_depth_offset(f32 offset)
{
glProgramUniform1f(shader_prog, loc_depthOffset, offset);
}
ClientEntity *client_entity_grab(u64 id)
{
return id ? map_get(&entities, &id, &cmp_entity, &refcount_grb) : NULL;

View File

@ -50,6 +50,7 @@ void client_entity_deinit();
bool client_entity_gfx_init();
void client_entity_gfx_deinit();
void client_entity_gfx_update();
void client_entity_depth_offset(f32 offset);
ClientEntity *client_entity_grab(u64 id);
void client_entity_drop(ClientEntity *entity);

View File

@ -11,6 +11,7 @@
static GLuint _3d_shader_prog;
static GLint _3d_loc_VP;
static GLint _3d_loc_depthOffset;
static ModelShader _3d_model_shader;
static LightShader _3d_light_shader;
@ -27,6 +28,7 @@ bool client_inventory_init()
free(_3d_shader_defs);
_3d_loc_VP = glGetUniformLocation(_3d_shader_prog, "VP");
_3d_loc_depthOffset = glGetUniformLocation(_3d_shader_prog, "depthOffset");
_3d_model_shader.prog = _3d_shader_prog;
_3d_model_shader.loc_transform = glGetUniformLocation(_3d_shader_prog, "model"); GL_DEBUG
@ -34,6 +36,8 @@ bool client_inventory_init()
_3d_light_shader.prog = _3d_shader_prog;
light_shader_locate(&_3d_light_shader);
client_inventory_depth_offset(0.0f);
return true;
}
@ -48,6 +52,11 @@ void client_inventory_update()
light_shader_update(&_3d_light_shader);
}
void client_inventory_depth_offset(f32 offset)
{
glProgramUniform1f(_3d_shader_prog, _3d_loc_depthOffset, offset);
}
static void wield_init(ModelNode *hand)
{
if (hand)

View File

@ -9,6 +9,7 @@ bool client_inventory_init();
void client_inventory_deinit();
void client_inventory_update();
void client_inventory_depth_offset(f32 offset);
void client_inventory_init_player(ClientEntity *entity);
void client_inventory_deinit_player(ClientEntity *entity);
void client_inventory_update_player(void *peer, ToClientPlayerInventory *pkt);

View File

@ -74,6 +74,17 @@ static void recv_pos_rot()
// entity callbacks
static void local_on_model_before_render(__attribute__((unused)) Model *model)
{
client_entity_depth_offset(0.2f);
client_inventory_depth_offset(0.2f);
}
static void local_on_model_after_render(__attribute__((unused)) Model *model)
{
client_entity_depth_offset(0.0f);
client_inventory_depth_offset(0.0f);}
static void on_add(ClientEntity *entity)
{
entity->model = model_clone(player_model);
@ -137,6 +148,8 @@ static void local_on_add(ClientEntity *entity)
}
on_add(entity);
entity->model->callbacks.before_render = &local_on_model_before_render;
entity->model->callbacks.after_render = &local_on_model_after_render;
player_entity = refcount_grb(&entity->rc);
recv_pos_rot();
@ -169,12 +182,6 @@ static void local_on_update_nametag(ClientEntity *entity)
}
}
static void __attribute__((unused)) on_model_step(Model *model, __attribute__((unused)) f64 dtime)
{
ClientPlayerData *data = ((ClientEntity *) model->extra)->extra;
(void) data; // ToDo: animations
}
static void on_model_delete(Model *model)
{
if (model->extra)
@ -229,7 +236,6 @@ void client_player_gfx_init()
RESSOURCE_PATH "models/player.txt", RESSOURCE_PATH "textures/models/player",
&client_entity_cube, &client_entity_shader);
player_model->callbacks.step = &on_model_step;
player_model->callbacks.delete = &on_model_delete;
}

View File

@ -145,29 +145,27 @@ static int cmp_model(const Model *model, const f32 *distance)
static void render_model(Model *model)
{
if (model->flags.wireframe) {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); GL_DEBUG
}
if (model->callbacks.before_render)
model->callbacks.before_render(model);
render_node(model->root);
if (model->flags.wireframe) {
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); GL_DEBUG
}
if (model->callbacks.after_render)
model->callbacks.after_render(model);
}
// step model help im stuck
static void model_step(Model *model, Tree *transparent, f64 dtime)
{
if (model->callbacks.step)
model->callbacks.step(model, dtime);
if (client_config.view_distance < (model->distance = sqrt(
pow(model->root->pos.x - camera.eye[0], 2) +
pow(model->root->pos.y - camera.eye[1], 2) +
pow(model->root->pos.z - camera.eye[2], 2))))
return;
if (model->callbacks.step)
model->callbacks.step(model, dtime);
if (!model->root->visible)
return;
@ -212,10 +210,11 @@ Model *model_create()
model->replace = NULL;
model->callbacks.step = NULL;
model->callbacks.before_render = NULL;
model->callbacks.after_render = NULL;
model->callbacks.delete = NULL;
model->flags.delete =
model->flags.wireframe =
model->flags.frustum_culling =
model->flags.transparent = 0;

View File

@ -55,11 +55,12 @@ typedef struct Model {
struct Model *replace;
struct {
void (*step)(struct Model *model, f64 dtime);
void (*before_render)(struct Model *model);
void (*after_render)(struct Model *model);
void (*delete)(struct Model *model);
} callbacks;
struct {
unsigned int delete: 1;
unsigned int wireframe: 1;
unsigned int frustum_culling: 1;
unsigned int transparent: 1;
} flags;