attempt to build web version out of emscripten legacy gl wrapper

This commit is contained in:
veclavtalica
2025-02-21 18:07:04 +03:00
parent 85e47dd677
commit dd4fc45be3
10 changed files with 123 additions and 68 deletions

View File

@ -5,7 +5,13 @@
#include "twn_types.h"
#include "twn_deferred_commands.h"
#ifdef EMSCRIPTEN
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glext.h>
#else
#include <glad/glad.h>
#endif
#include <stb_ds.h>
@ -35,13 +41,16 @@ static void APIENTRY opengl_log(GLenum source,
bool render_init(void) {
#ifndef EMSCRIPTEN
if (gladLoadGLLoader(&SDL_GL_GetProcAddress) == 0) {
CRY("Init", "GLAD failed");
return false;
}
#endif
log_info("OpenGL context: %s\n", glGetString(GL_VERSION));
#ifndef EMSCRIPTEN
glHint(GL_TEXTURE_COMPRESSION_HINT, GL_NICEST);
glHint(GL_GENERATE_MIPMAP_HINT, GL_NICEST);
glHint(GL_FOG_HINT, GL_FASTEST);
@ -51,6 +60,7 @@ bool render_init(void) {
glEnable(GL_DEBUG_OUTPUT);
glDebugMessageCallback(opengl_log, NULL);
}
#endif
return true;
}
@ -88,16 +98,21 @@ static void finally_use_space_pipeline(void) {
depth_range_high = 1.0;
depth_range_low = 0.0;
#ifndef EMSCRIPTEN
static GLuint list = 0;
if (!list) {
list = glGenLists(1);
glNewList(list, GL_COMPILE); {
glNewList(list, GL_COMPILE);
#endif
{
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glShadeModel(GL_FLAT);
#ifndef EMSCRIPTEN
if (GLAD_GL_ARB_depth_clamp)
glDisable(GL_DEPTH_CLAMP);
#endif
if (ctx.cull_faces)
glEnable(GL_CULL_FACE);
@ -110,10 +125,12 @@ static void finally_use_space_pipeline(void) {
/* solid white, no modulation */
glColor4ub(255, 255, 255, 255);
#ifndef EMSCRIPTEN
} glEndList();
}
glCallList(list);
#endif
}
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(&camera_projection_matrix.row[0].x);
@ -135,26 +152,30 @@ static void finally_use_2d_pipeline(void) {
if (pipeline_last_used == PIPELINE_2D)
return;
#ifndef EMSCRIPTEN
static GLuint list = 0;
if (!list) {
list = glGenLists(1);
glNewList(list, GL_COMPILE); {
#endif
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
glShadeModel(GL_FLAT);
#ifndef EMSCRIPTEN
/* removes near/far plane comparison and discard */
if (GLAD_GL_ARB_depth_clamp)
glEnable(GL_DEPTH_CLAMP);
#endif
glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glDisable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
#ifndef EMSCRIPTEN
} glEndList();
}
glCallList(list);
} glCallList(list);
#endif
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
@ -180,34 +201,41 @@ static void finally_use_texture_mode(TextureMode mode) {
if (texture_mode_last_used == mode)
return;
#ifndef EMSCRIPTEN
static GLuint lists = 0;
if (!lists) {
lists = glGenLists(3);
/* ghostly */
glNewList(lists + 0, GL_COMPILE); {
#endif
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDepthFunc(GL_LESS);
glDepthMask(GL_FALSE);
glDisable(GL_ALPHA_TEST);
#ifndef EMSCRIPTEN
} glEndList();
/* seethrough */
glNewList(lists + 1, GL_COMPILE); {
#endif
glDisable(GL_BLEND);
glDepthFunc(GL_LESS);
glDepthMask(GL_TRUE);
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_EQUAL, 1.0f);
#ifndef EMSCRIPTEN
} glEndList();
/* opaque */
glNewList(lists + 2, GL_COMPILE); {
#endif
glDisable(GL_BLEND);
glDepthFunc(GL_LESS);
glDepthMask(GL_TRUE);
glDisable(GL_ALPHA_TEST);
#ifndef EMSCRIPTEN
} glEndList();
}
@ -219,6 +247,8 @@ static void finally_use_texture_mode(TextureMode mode) {
glCallList(lists + 2);
}
#endif
texture_mode_last_used = mode;
}
@ -227,7 +257,11 @@ VertexBufferBuilder build_vertex_buffer(VertexBuffer buffer, size_t bytes) {
SDL_assert(bytes != 0);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, bytes, NULL, GL_STREAM_DRAW);
#ifndef EMSCRIPTEN
void *mapping = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
#else
void *mapping = SDL_malloc(bytes);
#endif
if (!mapping)
CRY("build_vertex_buffer", "Error mapping a vertex array buffer");
@ -239,8 +273,13 @@ VertexBufferBuilder build_vertex_buffer(VertexBuffer buffer, size_t bytes) {
void finish_vertex_builder(VertexBufferBuilder *builder) {
#ifndef EMSCRIPTEN
if (!glUnmapBuffer(GL_ARRAY_BUFFER))
CRY("finish_vertex_builder", "Error unmapping a vertex array buffer");
#else
glBufferData(GL_ARRAY_BUFFER, builder->size, builder->base, GL_STREAM_DRAW);
SDL_free(builder->base);
#endif
glBindBuffer(GL_ARRAY_BUFFER, 0);
builder->base = 0;
@ -333,18 +372,23 @@ void finally_render_skybox(DeferredCommandDrawSkybox command) {
/* TODO: figure out which coordinates to use to not have issues with far z */
/* TODO: recalculate the list if far z requirement changes */
#ifndef EMSCRIPTEN
static GLuint list = 0;
if (!list) {
list = glGenLists(1);
glNewList(list, GL_COMPILE); {
glNewList(list, GL_COMPILE);
#endif
{
/* note: assumes that space pipeline is applied already */
glDisable(GL_ALPHA_TEST);
glDepthMask(GL_FALSE);
#ifndef EMSCRIPTEN
/* removes near/far plane comparison and discard */
if (GLAD_GL_ARB_depth_clamp)
glEnable(GL_DEPTH_CLAMP);
#endif
glBegin(GL_QUADS); {
/* up */
@ -408,16 +452,19 @@ void finally_render_skybox(DeferredCommandDrawSkybox command) {
glVertex3f(-50.f, 50.f, -50.f);
} glEnd();
#ifndef EMSCRIPTEN
if (GLAD_GL_ARB_depth_clamp)
glDisable(GL_DEPTH_CLAMP);
#endif
glDepthMask(GL_TRUE);
glDisable(GL_TEXTURE_CUBE_MAP);
#ifndef EMSCRIPTEN
} glEndList();
}
glCallList(list);
#endif
}
}

View File

@ -5,12 +5,13 @@
#include <stb_ds.h>
#ifdef EMSCRIPTEN
#include <GLES2/gl2.h>
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glext.h>
#else
#include <glad/glad.h>
#endif
void setup_viewport(int x, int y, int width, int height) {
glViewport(x, y, width, height);
}
@ -127,12 +128,7 @@ GPUTexture create_gpu_texture(TextureFilter filter, bool generate_mipmaps, int c
SDL_assert(width > 0 && height > 0);
SDL_assert(channels > 0 && channels <= 4);
#if !defined(EMSCRIPTEN)
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, (GLboolean)generate_mipmaps);
#else
if (generate_mipmaps)
glGenerateMipmap(GL_TEXTURE_2D);
#endif
if (filter == TEXTURE_FILTER_NEAREAST) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
@ -145,24 +141,14 @@ GPUTexture create_gpu_texture(TextureFilter filter, bool generate_mipmaps, int c
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
#if !defined(EMSCRIPTEN)
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
#endif
int format_internal, format;
if (channels == 4) {
#ifdef EMSCRIPTEN
format_internal = GL_RGBA;
#else
format_internal = GL_RGBA8;
#endif
format = GL_RGBA;
} else if (channels == 3) {
#ifdef EMSCRIPTEN
format_internal = GL_RGBA;
#else
format_internal = GL_RGBA8;
#endif
format = GL_RGB;
} else if (channels == 1) {
format_internal = GL_ALPHA;

View File

@ -398,7 +398,4 @@ void finally_draw_text(FontData const *font_data,
};
arrpush(deferred_commands, final_command);
/* TODO: why doesn't it get restored if not placed here? */
// glDepthMask(GL_TRUE);
}