32-bit color instead of palette

This commit is contained in:
Shylie 2024-05-16 13:48:24 -05:00
parent 4b7991a3a6
commit 325ef5167d
5 changed files with 28 additions and 76 deletions

View File

@ -6,19 +6,15 @@ namespace
{ {
void init() void init()
{ {
glerminal_update_palette(0, 0x00000000); glerminal_update_sprite(0xFF00007F, {
glerminal_update_palette(1, 0xFF00007F); 0x00FF007F, 0x00FF007F, 0x00FF007F, 0x00FF007F, 0x00FF007F, 0x00FF007F, 0x00FF007F, 0x00FF007F,
glerminal_update_palette(2, 0x00FF007F); 0x00FF007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0x00FF007F,
0x00FF007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0x00FF007F,
glerminal_update_sprite(1, { 0x00FF007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0x00FF007F,
2, 2, 2, 2, 2, 2, 2, 2, 0x00FF007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0x00FF007F,
2, 1, 1, 1, 1, 1, 1, 2, 0x00FF007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0x00FF007F,
2, 1, 1, 1, 1, 1, 1, 2, 0x00FF007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0xFF00007F, 0x00FF007F,
2, 1, 1, 1, 1, 1, 1, 2, 0x00FF007F, 0x00FF007F, 0x00FF007F, 0x00FF007F, 0x00FF007F, 0x00FF007F, 0x00FF007F, 0x00FF007F
2, 1, 1, 1, 1, 1, 1, 2,
2, 1, 1, 1, 1, 1, 1, 2,
2, 1, 1, 1, 1, 1, 1, 2,
2, 2, 2, 2, 2, 2, 2, 2
}); });
} }

View File

@ -9,24 +9,19 @@ namespace
{ {
for (int i = 1; i < 256; i++) for (int i = 1; i < 256; i++)
{ {
constexpr unsigned char c = 0; constexpr unsigned char c = 16;
const unsigned char v = (255 - c) * powf((i - 1) / 256.0f, 4.0f) + c; const unsigned char v = (255 - c) * powf((i - 1) / 256.0f, 4.0f) + c;
glerminal_update_palette(i, (v << 24) | (v << 16) | (v << 8) | 0xE0); const unsigned int j = (0xFF << 24) | (v << 16) | (v << 8) | v;
}
for (int i = 1; i < 256; i++)
{
const unsigned char j = i;
glerminal_update_sprite(i, glerminal_update_sprite(i,
{ {
0,j,0,0,0,0,j,0,
j,j,j,j,j,j,j,j, j,j,j,j,j,j,j,j,
0,j,0,0,0,0,j,0,
0,j,0,0,0,0,j,0,
0,j,0,0,0,0,j,0,
0,j,0,0,0,0,j,0,
j,j,j,j,j,j,j,j, j,j,j,j,j,j,j,j,
0,j,0,0,0,0,j,0, j,j,j,j,j,j,j,j,
j,j,j,j,j,j,j,j,
j,j,j,j,j,j,j,j,
j,j,j,j,j,j,j,j,
j,j,j,j,j,j,j,j,
j,j,j,j,j,j,j,j,
}); });
} }
@ -34,7 +29,8 @@ namespace
{ {
for (int j = 0; j < 25; j++) for (int j = 0; j < 25; j++)
{ {
for (int k = 0; k < 256; k++) const int c = rand() % 224 + 32;
for (int k = 0; k < c; k++)
{ {
glerminal_set(i, j, k, k); glerminal_set(i, j, k, k);
} }
@ -66,8 +62,8 @@ namespace
{ {
for (int k = 0; k < 256; k++) for (int k = 0; k < 256; k++)
{ {
const float ox = 0.05f * powf(k, 0.55f) * copysignf(sqrtf((i - cx) * (i - cx)), i - cx); const float ox = 0.025f * powf(k, 0.8f) * (i - cx);
const float oy = 0.05f * powf(k, 0.55f) * copysignf(sqrtf((j - cy) * (j - cy)), j - cy); const float oy = 0.025f * powf(k, 0.8f) * (j - cy);
glerminal_offset(i, j, k, ox, oy); glerminal_offset(i, j, k, ox, oy);
} }
} }

View File

@ -17,11 +17,12 @@ typedef void (*glerminal_main_cb)(float dt);
typedef struct glerminal_sprite typedef struct glerminal_sprite
{ {
unsigned char data[GLERMINAL_CELL_AREA]; unsigned int data[GLERMINAL_CELL_AREA];
} glerminal_sprite; } glerminal_sprite;
/** /**
* @brief Call init once, then run the application's mainloop * @brief Call init once, then run the application's mainloop
* @param init initialization callback
* @param main main calllback * @param main main calllback
*/ */
void glerminal_run(glerminal_init_cb init, glerminal_main_cb main); void glerminal_run(glerminal_init_cb init, glerminal_main_cb main);
@ -64,13 +65,6 @@ void glerminal_offset(unsigned char x, unsigned char y, unsigned char layer, flo
*/ */
void glerminal_update_sprite(unsigned char id, glerminal_sprite sprite); void glerminal_update_sprite(unsigned char id, glerminal_sprite sprite);
/**
* @brief Update palette color at index ID
* @param id Palette index to update
* @param color New color
*/
void glerminal_update_palette(unsigned char id, unsigned int color);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -43,7 +43,6 @@ namespace glerminal
void offset(unsigned char x, unsigned char y, unsigned char layer, float x_offset, float y_offset); void offset(unsigned char x, unsigned char y, unsigned char layer, float x_offset, float y_offset);
void update_sprite(unsigned char id, glerminal_sprite sprite); void update_sprite(unsigned char id, glerminal_sprite sprite);
void update_palette_color(unsigned char id, unsigned int color);
private: private:
GLFWwindow* m_window; GLFWwindow* m_window;
@ -64,8 +63,7 @@ namespace glerminal
unsigned char m_cells[GRID_AREA * LAYER_COUNT]; unsigned char m_cells[GRID_AREA * LAYER_COUNT];
float m_offsets[GRID_AREA * LAYER_COUNT * 2]; float m_offsets[GRID_AREA * LAYER_COUNT * 2];
unsigned char m_sprites[CELL_SIZE * CELL_SIZE * 256]; unsigned int m_sprites[CELL_SIZE * CELL_SIZE * 256];
float m_palette[256 * 4];
glerminal_main_cb m_main; glerminal_main_cb m_main;
@ -81,7 +79,6 @@ namespace glerminal
void deinit_gl(); void deinit_gl();
void update_sprites(); void update_sprites();
void update_palette();
}; };
} }

View File

@ -1,7 +1,6 @@
#include "glerminal-private.h" #include "glerminal-private.h"
#define GRID_SIZE_UNIFORM_NAME "grid_size" #define GRID_SIZE_UNIFORM_NAME "grid_size"
#define PALETTE_UNIFORM_NAME "palette"
#define SPRITES_UNIFORM_NAME "sprites" #define SPRITES_UNIFORM_NAME "sprites"
#define LAYERS_UNIFORM_NAME "layers" #define LAYERS_UNIFORM_NAME "layers"
@ -81,12 +80,11 @@ namespace
"#version 400 core\n" "#version 400 core\n"
"in vec2 texcoord;\n" "in vec2 texcoord;\n"
"flat in int sprite;\n" "flat in int sprite;\n"
"uniform usampler2DArray " SPRITES_UNIFORM_NAME ";\n" "uniform sampler2DArray " SPRITES_UNIFORM_NAME ";\n"
"uniform vec4 " PALETTE_UNIFORM_NAME "[256];\n"
"out vec4 FragColor;\n" "out vec4 FragColor;\n"
"void main()\n" "void main()\n"
"{\n" "{\n"
" FragColor = " PALETTE_UNIFORM_NAME "[int(texture(" SPRITES_UNIFORM_NAME ", vec3(texcoord, sprite)))];\n" " FragColor = texture(" SPRITES_UNIFORM_NAME ", vec3(texcoord, sprite));\n"
"}"; "}";
constexpr char* SCREEN_VERTEX_SHADER_SOURCE = constexpr char* SCREEN_VERTEX_SHADER_SOURCE =
@ -122,8 +120,7 @@ namespace glerminal
m_main(main), m_main(main),
m_cells{ }, m_cells{ },
m_offsets{ }, m_offsets{ },
m_sprites{ }, m_sprites{ }
m_palette{ }
#ifdef _DEBUG #ifdef _DEBUG
, m_log("log.txt") , m_log("log.txt")
#endif #endif
@ -181,7 +178,6 @@ namespace glerminal
glNamedBufferData(m_sprites_instance_vbo, sizeof(m_cells), m_cells, GL_STREAM_DRAW); glNamedBufferData(m_sprites_instance_vbo, sizeof(m_cells), m_cells, GL_STREAM_DRAW);
glNamedBufferData(m_offsets_instance_vbo, sizeof(m_offsets), m_offsets, GL_STREAM_DRAW); glNamedBufferData(m_offsets_instance_vbo, sizeof(m_offsets), m_offsets, GL_STREAM_DRAW);
update_sprites(); update_sprites();
update_palette();
glViewport(0, 0, GRID_WIDTH * CELL_SIZE, GRID_HEIGHT * CELL_SIZE); glViewport(0, 0, GRID_WIDTH * CELL_SIZE, GRID_HEIGHT * CELL_SIZE);
glUseProgram(m_program); glUseProgram(m_program);
@ -237,17 +233,6 @@ namespace glerminal
reinterpret_cast<glerminal_sprite*>(m_sprites)[id] = sprite; reinterpret_cast<glerminal_sprite*>(m_sprites)[id] = sprite;
} }
void glerminal::update_palette_color(unsigned char id, unsigned int color)
{
if (id < 256)
{
m_palette[4 * id + 0] = ((color >> 24) & 0xFF) / 255.0f;
m_palette[4 * id + 1] = ((color >> 16) & 0xFF) / 255.0f;
m_palette[4 * id + 2] = ((color >> 8) & 0xFF) / 255.0f;
m_palette[4 * id + 3] = ((color >> 0) & 0xFF) / 255.0f;
}
}
void glerminal::init_glfw() void glerminal::init_glfw()
{ {
glfwInit(); glfwInit();
@ -457,15 +442,12 @@ namespace glerminal
// setup uniforms // setup uniforms
m_screen_size_uniform_location = glGetUniformLocation(m_program, GRID_SIZE_UNIFORM_NAME); m_screen_size_uniform_location = glGetUniformLocation(m_program, GRID_SIZE_UNIFORM_NAME);
m_palette_uniform_location = glGetUniformLocation(m_program, PALETTE_UNIFORM_NAME);
glUseProgram(m_program); glUseProgram(m_program);
glUniform4f(m_screen_size_uniform_location, GRID_WIDTH, GRID_HEIGHT, 1.0f / GRID_WIDTH, 1.0f / GRID_HEIGHT); glUniform4f(m_screen_size_uniform_location, GRID_WIDTH, GRID_HEIGHT, 1.0f / GRID_WIDTH, 1.0f / GRID_HEIGHT);
glUniform1i(glGetUniformLocation(m_program, SPRITES_UNIFORM_NAME), 0); glUniform1i(glGetUniformLocation(m_program, SPRITES_UNIFORM_NAME), 0);
update_palette();
// compile // compile
const unsigned int screen_vertex_shader = glCreateShader(GL_VERTEX_SHADER); const unsigned int screen_vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(screen_vertex_shader, 1, &SCREEN_VERTEX_SHADER_SOURCE, nullptr); glShaderSource(screen_vertex_shader, 1, &SCREEN_VERTEX_SHADER_SOURCE, nullptr);
@ -574,13 +556,7 @@ namespace glerminal
void glerminal::update_sprites() void glerminal::update_sprites()
{ {
glBindTexture(GL_TEXTURE_2D_ARRAY, m_sprites_texture); glBindTexture(GL_TEXTURE_2D_ARRAY, m_sprites_texture);
glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_R8UI, CELL_SIZE, CELL_SIZE, 256, 0, GL_RED_INTEGER, GL_UNSIGNED_BYTE, m_sprites); glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA, CELL_SIZE, CELL_SIZE, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_sprites);
}
void glerminal::update_palette()
{
glUseProgram(m_program);
glUniform4fv(m_palette_uniform_location, 256, m_palette);
} }
} }
@ -631,11 +607,4 @@ void glerminal_update_sprite(unsigned char id, glerminal_sprite sprite)
if (!GLERMINAL_G) { return; } if (!GLERMINAL_G) { return; }
GLERMINAL_G->update_sprite(id, sprite); GLERMINAL_G->update_sprite(id, sprite);
}
void glerminal_update_palette(unsigned char id, unsigned int color)
{
if (!GLERMINAL_G) { return; }
GLERMINAL_G->update_palette_color(id, color);
} }