Make 3D fragment shader portable

master
Elias Fleckenstein 2021-08-24 19:07:46 +02:00
parent c5833cc313
commit b398f3286b
17 changed files with 102 additions and 40 deletions

1
.gitignore vendored
View File

@ -13,4 +13,3 @@ DragonblocksServer
Dragonblocks
world.sqlite
DragonblocksAlpha-*.zip

View File

@ -1,12 +1,10 @@
#version 460 core
in float fragmentTextureIndex;
in vec2 fragmentTextureCoords;
in vec3 fragmentColor;
out vec4 outColor;
uniform sampler2D textures[8]; // ToDo: Replace 8 by max_texture_units
uniform sampler2D textures[MAX_TEXTURE_UNITS];
vec3 hsv2rgb(vec3 c)
{

View File

@ -1,5 +1,3 @@
#version 460 core
layout(location = 0) in vec3 vertexPosition;
layout(location = 1) in float vertexTextureIndex;
layout(location = 2) in vec2 vertexTextureCoords;

View File

@ -1,5 +1,3 @@
#version 460 core
in vec2 fragmentTextureCoords;
out vec4 outColor;

View File

@ -1,5 +1,3 @@
#version 460 core
layout(location = 0) in vec2 vertexPosition;
layout(location = 1) in vec2 vertexTextureCoords;

View File

@ -1,5 +1,3 @@
#version 460 core
in vec2 fragmentTextureCoords;
out vec4 outColor;

View File

@ -1,5 +1,3 @@
#version 460 core
layout(location = 0) in vec2 vertexPosition;
layout(location = 1) in vec2 vertexTextureCoords;

View File

@ -89,7 +89,7 @@ static bool client_authenticate()
return false;
}
static void client_start(int fd)
static bool client_start(int fd)
{
client.fd = fd;
pthread_mutex_init(&client.mtx, NULL);
@ -102,8 +102,7 @@ static void client_start(int fd)
pthread_t recv_thread;
pthread_create(&recv_thread, NULL, &reciever_thread, NULL);
if (client_authenticate())
game(&client);
bool return_value = client_authenticate() && game(&client);
if (client.state != CS_DISCONNECTED)
client_disconnect(true, NULL);
@ -117,6 +116,8 @@ static void client_start(int fd)
client_map_deinit();
pthread_mutex_destroy(&client.mtx);
return return_value;
}
int main(int argc, char **argv)
@ -155,7 +156,6 @@ int main(int argc, char **argv)
freeaddrinfo(info);
signal_handlers_init();
client_start(fd);
return EXIT_SUCCESS;
return client_start(fd) ? EXIT_SUCCESS : EXIT_FAILURE;
}

View File

@ -69,20 +69,20 @@ static void game_loop(Client *client)
}
}
void game(Client *client)
bool game(Client *client)
{
int width, height;
width = 1250;
height = 750;
if (! window_init(width, height))
return;
return false;
if (! font_init())
return;
return false;
if (! scene_init())
return;
return false;
scene_on_resize(width, height);
@ -92,7 +92,9 @@ void game(Client *client)
camera_set_position((v3f32) {0.0f, 0.0f, 0.0f});
camera_set_angle(0.0f, 0.0f);
hud_init();
if (! hud_init())
return false;
hud_on_resize(width, height);
hud_add((HUDElementDefinition) {
@ -131,4 +133,6 @@ void game(Client *client)
font_deinit();
hud_deinit();
scene_deinit();
return true;
}

View File

@ -3,6 +3,6 @@
#include "client/client.h"
void game(Client *client);
bool game(Client *client);
#endif

View File

@ -77,7 +77,7 @@ static VertexImage image_vertices[6] = {
bool hud_init()
{
if (! shader_program_create(RESSOURCEPATH "shaders/hud/image", &hud.image_prog)) {
if (! shader_program_create(RESSOURCEPATH "shaders/hud/image", &hud.image_prog, NULL)) {
fprintf(stderr, "Failed to create HUD image shader program\n");
return false;
}
@ -85,7 +85,7 @@ bool hud_init()
hud.image_loc_model = glGetUniformLocation(hud.image_prog, "model");
hud.image_loc_projection = glGetUniformLocation(hud.image_prog, "projection");
if (! shader_program_create(RESSOURCEPATH "shaders/hud/font", &hud.font_prog)) {
if (! shader_program_create(RESSOURCEPATH "shaders/hud/font", &hud.font_prog, NULL)) {
fprintf(stderr, "Failed to create HUD font shader program\n");
return false;
}

View File

@ -26,15 +26,18 @@ bool scene_init()
scene.objects = list_create(NULL),
pthread_mutex_init(&scene.mtx, NULL);
if (! shader_program_create(RESSOURCEPATH "shaders/3d", &scene.prog)) {
glGetIntegerv(GL_MAX_TEXTURE_UNITS, &scene.max_texture_units);
char max_texture_units_def[BUFSIZ];
sprintf(max_texture_units_def, "#define MAX_TEXTURE_UNITS %d\n", scene.max_texture_units);
if (! shader_program_create(RESSOURCEPATH "shaders/3d", &scene.prog, max_texture_units_def)) {
fprintf(stderr, "Failed to create 3D shader program\n");
return false;
}
scene.loc_MVP = glGetUniformLocation(scene.prog, "MVP");
glGetIntegerv(GL_MAX_TEXTURE_UNITS, &scene.max_texture_units);
GLint texture_indices[scene.max_texture_units];
for (GLint i = 0; i < scene.max_texture_units; i++)
texture_indices[i] = i;

View File

@ -4,7 +4,7 @@
#include <stdlib.h>
#include "client/shader.h"
static GLuint compile_and_attach_shader(GLenum type, const char *path, const char *name, GLuint program)
static GLuint compile_and_attach_shader(GLenum type, const char *path, const char *name, GLuint program, const char *definitions)
{
char full_path[strlen(path) + 1 + strlen(name) + 1 + 4 + 1];
sprintf(full_path, "%s/%s.glsl", path, name);
@ -47,9 +47,21 @@ static GLuint compile_and_attach_shader(GLenum type, const char *path, const cha
GLuint id = glCreateShader(type);
char const *codeptr = code;
const int isize = (int) size;
glShaderSource(id, 1, &codeptr, &isize);
const char *version = "#version 460 core\n";
const char *code_list[3] = {
version,
definitions,
code,
};
int size_list[3] = {
18,
strlen(definitions),
size,
};
glShaderSource(id, 3, code_list, size_list);
glCompileShader(id);
@ -58,7 +70,7 @@ static GLuint compile_and_attach_shader(GLenum type, const char *path, const cha
if (! success) {
char errbuf[BUFSIZ];
glGetShaderInfoLog(id, BUFSIZ, NULL, errbuf);
fprintf(stderr, "Failed to compile %s shader: %s\n", name, errbuf);
fprintf(stderr, "Failed to compile %s shader: %s", name, errbuf);
glDeleteShader(id);
return 0;
}
@ -68,19 +80,21 @@ static GLuint compile_and_attach_shader(GLenum type, const char *path, const cha
return id;
}
bool shader_program_create(const char *path, GLuint *idptr)
bool shader_program_create(const char *path, GLuint *idptr, const char *definitions)
{
GLuint id = glCreateProgram();
if (! definitions)
definitions = "";
GLuint vert, frag;
if (! (vert = compile_and_attach_shader(GL_VERTEX_SHADER, path, "vertex", id))) {
if (! (vert = compile_and_attach_shader(GL_VERTEX_SHADER, path, "vertex", id, definitions))) {
glDeleteProgram(id);
return false;
}
if (! (frag = compile_and_attach_shader(GL_FRAGMENT_SHADER, path, "fragment", id))) {
if (! (frag = compile_and_attach_shader(GL_FRAGMENT_SHADER, path, "fragment", id, definitions))) {
glDeleteShader(vert);
glDeleteProgram(id);
return false;

View File

@ -5,6 +5,6 @@
#include <GL/glew.h>
#include <GL/gl.h>
bool shader_program_create(const char *path, GLuint *idptr);
bool shader_program_create(const char *path, GLuint *idptr, const char *definitions);
#endif

39
src/debug.sh Executable file
View File

@ -0,0 +1,39 @@
#! /bin/bash
if ! make -j$(nproc); then
exit 1
fi
DEBUG_DIR=/tmp/dragonblocks_alpha_debug_$$/
mkdir $DEBUG_DIR
echo "singleplayer" > $DEBUG_DIR/name
COMMON="set confirm off
handle SIGTERM nostop print pass
handle SIGPIPE nostop noprint pass
set height 0
define hook-stop
if \$_exitcode == 0
quit
end
end
"
echo "$COMMON
run ::1 4000 < $DEBUG_DIR/name
" > $DEBUG_DIR/client_script
echo "$COMMON
run 4000
" > $DEBUG_DIR/server_script
konsole -e bash -c "
echo \$\$ > $DEBUG_DIR/server_pid
exec gdb --command $DEBUG_DIR/server_script ./DragonblocksServer
" & sleep 0.5
gdb --command $DEBUG_DIR/client_script ./Dragonblocks
kill `cat $DEBUG_DIR/server_pid`
rm -rf $DEBUG_DIR

12
src/debug_loop.sh Executable file
View File

@ -0,0 +1,12 @@
#! /bin/bash
COMMAND="./debug.sh"
if [[ $1 == "mapgen" ]]; then
COMMAND="./debug_mapgen.sh"
fi
while true; do
if ! $COMMAND; then
read
fi
done

3
src/debug_mapgen.sh Executable file
View File

@ -0,0 +1,3 @@
#! /bin/bash
rm -f world.sqlite
./debug.sh