TINYGL: Eliminate passing context where possible

This commit is contained in:
Paweł Kołodziejski 2021-12-07 00:58:14 +01:00
parent 88dd98204d
commit c1512a5c40
No known key found for this signature in database
GPG key ID: 0BDADC9E74440FF7
16 changed files with 542 additions and 550 deletions

View file

@ -32,39 +32,39 @@
namespace TinyGL {
void GLContext::glopNormal(GLContext *c, GLParam *p) {
c->current_normal.X = p[1].f;
c->current_normal.Y = p[2].f;
c->current_normal.Z = p[3].f;
c->current_normal.W = 0.0f;
current_normal.X = p[1].f;
current_normal.Y = p[2].f;
current_normal.Z = p[3].f;
current_normal.W = 0.0f;
}
void GLContext::glopTexCoord(GLContext *c, GLParam *p) {
c->current_tex_coord.X = p[1].f;
c->current_tex_coord.Y = p[2].f;
c->current_tex_coord.Z = p[3].f;
c->current_tex_coord.W = p[4].f;
current_tex_coord.X = p[1].f;
current_tex_coord.Y = p[2].f;
current_tex_coord.Z = p[3].f;
current_tex_coord.W = p[4].f;
}
void GLContext::glopEdgeFlag(GLContext *c, GLParam *p) {
c->current_edge_flag = p[1].i;
current_edge_flag = p[1].i;
}
void GLContext::glopColor(GLContext *c, GLParam *p) {
c->current_color.X = p[1].f;
c->current_color.Y = p[2].f;
c->current_color.Z = p[3].f;
c->current_color.W = p[4].f;
current_color.X = p[1].f;
current_color.Y = p[2].f;
current_color.Z = p[3].f;
current_color.W = p[4].f;
if (c->color_material_enabled) {
if (color_material_enabled) {
GLParam q[7];
q[0].op = OP_Material;
q[1].i = c->current_color_material_mode;
q[2].i = c->current_color_material_type;
q[1].i = current_color_material_mode;
q[2].i = current_color_material_type;
q[3].f = p[1].f;
q[4].f = p[2].f;
q[5].f = p[3].f;
q[6].f = p[4].f;
c->glopMaterial(c, q);
glopMaterial(c, q);
}
}
@ -92,62 +92,62 @@ void GLContext::glopBegin(GLContext *c, GLParam *p) {
assert(c->in_begin == 0);
type = p[1].i;
c->begin_type = type;
c->in_begin = 1;
c->vertex_n = 0;
c->vertex_cnt = 0;
begin_type = type;
in_begin = 1;
vertex_n = 0;
vertex_cnt = 0;
if (c->matrix_model_projection_updated) {
if (c->lighting_enabled) {
if (matrix_model_projection_updated) {
if (lighting_enabled) {
// precompute inverse modelview
c->matrix_model_view_inv = *c->matrix_stack_ptr[0];
c->matrix_model_view_inv.invert();
c->matrix_model_view_inv.transpose();
matrix_model_view_inv = *matrix_stack_ptr[0];
matrix_model_view_inv.invert();
matrix_model_view_inv.transpose();
} else {
// precompute projection matrix
c->matrix_model_projection = (*c->matrix_stack_ptr[1]) * (*c->matrix_stack_ptr[0]);
matrix_model_projection = (*matrix_stack_ptr[1]) * (*matrix_stack_ptr[0]);
// test to accelerate computation
c->matrix_model_projection_no_w_transform = 0;
if (c->matrix_model_projection._m[3][0] == 0.0 && c->matrix_model_projection._m[3][1] == 0.0 && c->matrix_model_projection._m[3][2] == 0.0)
c->matrix_model_projection_no_w_transform = 1;
matrix_model_projection_no_w_transform = 0;
if (matrix_model_projection._m[3][0] == 0.0 && matrix_model_projection._m[3][1] == 0.0 && matrix_model_projection._m[3][2] == 0.0)
matrix_model_projection_no_w_transform = 1;
}
c->matrix_model_projection_updated = 0;
matrix_model_projection_updated = 0;
}
// test if the texture matrix is not Identity
c->apply_texture_matrix = !c->matrix_stack_ptr[2]->isIdentity();
apply_texture_matrix = !matrix_stack_ptr[2]->isIdentity();
// viewport
if (c->viewport.updated) {
if (viewport.updated) {
gl_eval_viewport(c);
c->viewport.updated = 0;
viewport.updated = 0;
}
// triangle drawing functions
if (c->render_mode == TGL_SELECT) {
c->draw_triangle_front = gl_draw_triangle_select;
c->draw_triangle_back = gl_draw_triangle_select;
if (render_mode == TGL_SELECT) {
draw_triangle_front = gl_draw_triangle_select;
draw_triangle_back = gl_draw_triangle_select;
} else {
switch (c->polygon_mode_front) {
switch (polygon_mode_front) {
case TGL_POINT:
c->draw_triangle_front = gl_draw_triangle_point;
draw_triangle_front = gl_draw_triangle_point;
break;
case TGL_LINE:
c->draw_triangle_front = gl_draw_triangle_line;
draw_triangle_front = gl_draw_triangle_line;
break;
default:
c->draw_triangle_front = gl_draw_triangle_fill;
draw_triangle_front = gl_draw_triangle_fill;
break;
}
switch (c->polygon_mode_back) {
case TGL_POINT:
c->draw_triangle_back = gl_draw_triangle_point;
draw_triangle_back = gl_draw_triangle_point;
break;
case TGL_LINE:
c->draw_triangle_back = gl_draw_triangle_line;
draw_triangle_back = gl_draw_triangle_line;
break;
default:
c->draw_triangle_back = gl_draw_triangle_fill;
draw_triangle_back = gl_draw_triangle_fill;
break;
}
}
@ -195,27 +195,27 @@ void GLContext::glopVertex(GLContext *c, GLParam *p) {
GLVertex *v;
int n, cnt;
assert(c->in_begin != 0);
assert(in_begin != 0);
n = c->vertex_n;
cnt = c->vertex_cnt;
n = vertex_n;
cnt = vertex_cnt;
cnt++;
c->vertex_cnt = cnt;
vertex_cnt = cnt;
// quick fix to avoid crashes on large polygons
if (n >= c->vertex_max) {
if (n >= vertex_max) {
GLVertex *newarray;
c->vertex_max <<= 1; // just double size
newarray = (GLVertex *)gl_malloc(sizeof(GLVertex) * c->vertex_max);
vertex_max <<= 1; // just double size
newarray = (GLVertex *)gl_malloc(sizeof(GLVertex) * vertex_max);
if (!newarray) {
error("unable to allocate GLVertex array.");
}
memcpy(newarray, c->vertex, n * sizeof(GLVertex));
gl_free(c->vertex);
c->vertex = newarray;
memcpy(newarray, vertex, n * sizeof(GLVertex));
gl_free(vertex);
vertex = newarray;
}
// new vertex entry
v = &c->vertex[n];
v = &vertex[n];
n++;
v->coord.X = p[1].f;
@ -227,40 +227,40 @@ void GLContext::glopVertex(GLContext *c, GLParam *p) {
// color
if (c->lighting_enabled) {
c->gl_shade_vertex(c, v);
if (lighting_enabled) {
gl_shade_vertex(v);
} else {
v->color = c->current_color;
v->color = current_color;
}
// tex coords
if (c->texture_2d_enabled) {
if (c->apply_texture_matrix) {
c->matrix_stack_ptr[2]->transform(c->current_tex_coord, v->tex_coord);
if (texture_2d_enabled) {
if (apply_texture_matrix) {
matrix_stack_ptr[2]->transform(current_tex_coord, v->tex_coord);
} else {
v->tex_coord = c->current_tex_coord;
v->tex_coord = current_tex_coord;
}
}
// precompute the mapping to the viewport
if (v->clip_code == 0)
c->gl_transform_to_viewport(c, v);
gl_transform_to_viewport(v);
// edge flag
v->edge_flag = c->current_edge_flag;
v->edge_flag = current_edge_flag;
c->vertex_n = n;
vertex_n = n;
}
void GLContext::glopEnd(GLContext *c, GLParam *) {
assert(c->in_begin == 1);
assert(in_begin == 1);
if (c->vertex_cnt > 0) {
c->issueDrawCall(new RasterizationDrawCall());
if (vertex_cnt > 0) {
issueDrawCall(new RasterizationDrawCall());
}
c->in_begin = 0;
in_begin = 0;
}
} // end of namespace TinyGL