twn_gl_15_rendering.c: use display lists to cache common series of calls

This commit is contained in:
veclav talica 2024-10-01 14:58:00 +03:00
parent 91ea5356fc
commit aa5ff1edf1

View File

@ -59,11 +59,34 @@ static Pipeline pipeline_last_used = PIPELINE_NO;
void use_space_pipeline(void) { void use_space_pipeline(void) {
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); static GLuint list = 0;
glShadeModel(GL_SMOOTH); if (!list) {
list = glGenLists(1);
if (GLAD_GL_ARB_depth_clamp) glNewList(list, GL_COMPILE); {
glDisable(GL_DEPTH_CLAMP); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glShadeModel(GL_SMOOTH);
if (GLAD_GL_ARB_depth_clamp)
glDisable(GL_DEPTH_CLAMP);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glDepthFunc(GL_LESS);
glDepthRange(0, 1);
glDisable(GL_BLEND);
glEnable(GL_ALPHA_TEST); /* TODO: infer its usage? */
glAlphaFunc(GL_EQUAL, 1.0f);
glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
/* solid white, no modulation */
glColor4ub(255, 255, 255, 255);
} glEndList();
}
glCallList(list);
glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION);
glLoadMatrixf(&camera_projection_matrix.row[0].x); glLoadMatrixf(&camera_projection_matrix.row[0].x);
@ -71,20 +94,6 @@ void use_space_pipeline(void) {
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(&camera_look_at_matrix.row[0].x); glLoadMatrixf(&camera_look_at_matrix.row[0].x);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glDepthFunc(GL_LESS);
glDepthRange(0, 1);
glDisable(GL_BLEND);
glEnable(GL_ALPHA_TEST); /* TODO: infer its usage? */
glAlphaFunc(GL_EQUAL, 1.0f);
glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
/* solid white, no modulation */
glColor4ub(255, 255, 255, 255);
pipeline_last_used = PIPELINE_SPACE; pipeline_last_used = PIPELINE_SPACE;
} }
@ -95,12 +104,26 @@ void use_2d_pipeline(void) {
glFlush(); glFlush();
} }
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST); static GLuint list = 0;
glShadeModel(GL_FLAT); if (!list) {
list = glGenLists(1);
/* removes near/far plane comparison and discard */ glNewList(list, GL_COMPILE); {
if (GLAD_GL_ARB_depth_clamp) glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
glDisable(GL_DEPTH_CLAMP); glShadeModel(GL_FLAT);
/* removes near/far plane comparison and discard */
if (GLAD_GL_ARB_depth_clamp)
glDisable(GL_DEPTH_CLAMP);
glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glDisable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
} glEndList();
}
glCallList(list);
glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION);
glLoadIdentity(); glLoadIdentity();
@ -109,11 +132,6 @@ void use_2d_pipeline(void) {
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glDisable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
pipeline_last_used = PIPELINE_2D; pipeline_last_used = PIPELINE_2D;
} }
@ -183,23 +201,40 @@ void render_circle(const CirclePrimitive *circle) {
void use_texture_mode(TextureMode mode) { void use_texture_mode(TextureMode mode) {
GLuint lists = 0;
if (!lists) {
lists = glGenLists(3);
glNewList(lists + 0, GL_COMPILE); {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDepthFunc(GL_ALWAYS);
glDepthMask(GL_FALSE);
glDisable(GL_ALPHA_TEST);
} glEndList();
glNewList(lists + 1, GL_COMPILE); {
glDisable(GL_BLEND);
glDepthFunc(GL_LEQUAL);
glDepthMask(GL_TRUE);
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_EQUAL, 1.0f);
} glEndList();
glNewList(lists + 2, GL_COMPILE); {
glDisable(GL_BLEND);
glDepthFunc(GL_LESS);
glDepthMask(GL_TRUE);
glDisable(GL_ALPHA_TEST);
} glEndList();
}
if (mode == TEXTURE_MODE_GHOSTLY) { if (mode == TEXTURE_MODE_GHOSTLY) {
glEnable(GL_BLEND); glCallList(lists + 0);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDepthFunc(GL_ALWAYS);
glDepthMask(GL_FALSE);
glDisable(GL_ALPHA_TEST);
} else if (mode == TEXTURE_MODE_SEETHROUGH) { } else if (mode == TEXTURE_MODE_SEETHROUGH) {
glDisable(GL_BLEND); glCallList(lists + 1);
glDepthFunc(GL_LEQUAL);
glDepthMask(GL_TRUE);
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_EQUAL, 1.0f);
} else { } else {
glDisable(GL_BLEND); glCallList(lists + 2);
glDepthFunc(GL_LESS);
glDepthMask(GL_TRUE);
glDisable(GL_ALPHA_TEST);
} }
} }
@ -517,10 +552,6 @@ void finally_render_skybox(char *paths) {
glEnable(GL_TEXTURE_CUBE_MAP); glEnable(GL_TEXTURE_CUBE_MAP);
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemap); glBindTexture(GL_TEXTURE_CUBE_MAP, cubemap);
/* note: assumes that space pipeline is applied already */
glDisable(GL_ALPHA_TEST);
glDepthMask(GL_TRUE);
if (loading_needed) { if (loading_needed) {
/* load all the sides */ /* load all the sides */
char *expanded = expand_asterisk(paths, "up"); char *expanded = expand_asterisk(paths, "up");
@ -548,70 +579,84 @@ void finally_render_skybox(char *paths) {
SDL_free(expanded); SDL_free(expanded);
} }
/* TODO: use lists at the very least */
/* TODO: figure out which coordinates to use to not have issues with far z */ /* TODO: figure out which coordinates to use to not have issues with far z */
/* TODO: recalculate the list if far z requirement changes */
glBegin(GL_QUADS); { static GLuint list = 0;
/* up */ if (!list) {
glTexCoord3f(50.f, 50.f, 50.f); list = glGenLists(1);
glVertex3f(50.f, 50.f, 50.f);
glTexCoord3f(-50.f, 50.f, 50.f);
glVertex3f(-50.f, 50.f, 50.f);
glTexCoord3f(-50.f, 50.f, -50.f);
glVertex3f(-50.f, 50.f, -50.f);
glTexCoord3f(50.f, 50.f, -50.f);
glVertex3f(50.f, 50.f, -50.f);
/* down */ glNewList(list, GL_COMPILE); {
glTexCoord3f(50.f, -50.f, 50.f); /* note: assumes that space pipeline is applied already */
glVertex3f(50.f, -50.f, 50.f); glDisable(GL_ALPHA_TEST);
glTexCoord3f(50.f, -50.f, -50.f); glDepthMask(GL_TRUE);
glVertex3f(50.f, -50.f, -50.f);
glTexCoord3f(-50.f, -50.f, -50.f);
glVertex3f(-50.f, -50.f, -50.f);
glTexCoord3f(-50.f, -50.f, 50.f);
glVertex3f(-50.f, -50.f, 50.f);
/* east */ glBegin(GL_QUADS); {
glTexCoord3f(50.f, -50.f, 50.f); /* up */
glVertex3f(50.f, -50.f, 50.f); glTexCoord3f(50.f, 50.f, 50.f);
glTexCoord3f(50.f, 50.f, 50.f); glVertex3f(50.f, 50.f, 50.f);
glVertex3f(50.f, 50.f, 50.f); glTexCoord3f(-50.f, 50.f, 50.f);
glTexCoord3f(50.f, 50.f, -50.f); glVertex3f(-50.f, 50.f, 50.f);
glVertex3f(50.f, 50.f, -50.f); glTexCoord3f(-50.f, 50.f, -50.f);
glTexCoord3f(50.f, -50.f, -50.f); glVertex3f(-50.f, 50.f, -50.f);
glVertex3f(50.f, -50.f, -50.f); glTexCoord3f(50.f, 50.f, -50.f);
glVertex3f(50.f, 50.f, -50.f);
/* west */ /* down */
glTexCoord3f(-50.f, -50.f, 50.f); glTexCoord3f(50.f, -50.f, 50.f);
glVertex3f(-50.f, -50.f, 50.f); glVertex3f(50.f, -50.f, 50.f);
glTexCoord3f(-50.f, -50.f, -50.f); glTexCoord3f(50.f, -50.f, -50.f);
glVertex3f(-50.f, -50.f, -50.f); glVertex3f(50.f, -50.f, -50.f);
glTexCoord3f(-50.f, 50.f, -50.f); glTexCoord3f(-50.f, -50.f, -50.f);
glVertex3f(-50.f, 50.f, -50.f); glVertex3f(-50.f, -50.f, -50.f);
glTexCoord3f(-50.f, 50.f, 50.f); glTexCoord3f(-50.f, -50.f, 50.f);
glVertex3f(-50.f, 50.f, 50.f); glVertex3f(-50.f, -50.f, 50.f);
/* north */ /* east */
glTexCoord3f(-50.f, -50.f, 50.f); glTexCoord3f(50.f, -50.f, 50.f);
glVertex3f(-50.f, -50.f, 50.f); glVertex3f(50.f, -50.f, 50.f);
glTexCoord3f(-50.f, 50.f, 50.f); glTexCoord3f(50.f, 50.f, 50.f);
glVertex3f(-50.f, 50.f, 50.f); glVertex3f(50.f, 50.f, 50.f);
glTexCoord3f(50.f, 50.f, 50.f); glTexCoord3f(50.f, 50.f, -50.f);
glVertex3f(50.f, 50.f, 50.f); glVertex3f(50.f, 50.f, -50.f);
glTexCoord3f(50.f, -50.f, 50.f); glTexCoord3f(50.f, -50.f, -50.f);
glVertex3f(50.f, -50.f, 50.f); glVertex3f(50.f, -50.f, -50.f);
/* south */ /* west */
glTexCoord3f(-50.f, -50.f, -50.f); glTexCoord3f(-50.f, -50.f, 50.f);
glVertex3f(-50.f, -50.f, -50.f); glVertex3f(-50.f, -50.f, 50.f);
glTexCoord3f(50.f, -50.f, -50.f); glTexCoord3f(-50.f, -50.f, -50.f);
glVertex3f(50.f, -50.f, -50.f); glVertex3f(-50.f, -50.f, -50.f);
glTexCoord3f(50.f, 50.f, -50.f); glTexCoord3f(-50.f, 50.f, -50.f);
glVertex3f(50.f, 50.f, -50.f); glVertex3f(-50.f, 50.f, -50.f);
glTexCoord3f(-50.f, 50.f, -50.f); glTexCoord3f(-50.f, 50.f, 50.f);
glVertex3f(-50.f, 50.f, -50.f); glVertex3f(-50.f, 50.f, 50.f);
} glEnd();
glDisable(GL_TEXTURE_CUBE_MAP); /* north */
glTexCoord3f(-50.f, -50.f, 50.f);
glVertex3f(-50.f, -50.f, 50.f);
glTexCoord3f(-50.f, 50.f, 50.f);
glVertex3f(-50.f, 50.f, 50.f);
glTexCoord3f(50.f, 50.f, 50.f);
glVertex3f(50.f, 50.f, 50.f);
glTexCoord3f(50.f, -50.f, 50.f);
glVertex3f(50.f, -50.f, 50.f);
/* south */
glTexCoord3f(-50.f, -50.f, -50.f);
glVertex3f(-50.f, -50.f, -50.f);
glTexCoord3f(50.f, -50.f, -50.f);
glVertex3f(50.f, -50.f, -50.f);
glTexCoord3f(50.f, 50.f, -50.f);
glVertex3f(50.f, 50.f, -50.f);
glTexCoord3f(-50.f, 50.f, -50.f);
glVertex3f(-50.f, 50.f, -50.f);
} glEnd();
glDisable(GL_TEXTURE_CUBE_MAP);
} glEndList();
}
glCallList(list);
} }