Fix node editor drawing when built with core profile

There are two major things in this commit.

First one is to have proper stack for projection matrices. This is
something what OpenGL specification grants to have at least 2 elements
for and what is required to have for proper editor drawing without
refactoring the way how we restore projection matrix.

Supporting this stack have following advantages:

- Our GPU stack is closer to OpenGL specs, making it easier to follow
  by other developers who are always familiar with OpenGL.

- Makes it easier to port all editors to a new API.

- Should help us getting rid of extra matrix push/pop added in
  various commits to 2.8 branch.

The new API follows the following convention:

- gpuPushMatrix/gpuPopMatrix ALWAYS deals with model view matrix
  and nothing more.

  While this name does not fully indicate that it's only model view
  matrix operator, it matches behavior of other matrix operations
  such as transform which also doesn't indicate what matrix type
  they are operating on.

- Projection matrix has dedicated calls for push/pop which are
  gpuPushProjectionMatrix/gpuPopProjectionMatrix.
This commit is contained in:
Sergey Sharybin 2017-04-19 10:13:10 +02:00
parent 781108cdc6
commit ba4d23fe37
6 changed files with 66 additions and 26 deletions

View File

@ -3176,7 +3176,7 @@ void draw_nodespace_back_pix(const bContext *C, ARegion *ar, SpaceNode *snode, b
float x, y;
glMatrixMode(GL_PROJECTION);
gpuPushMatrix();
gpuPushProjectionMatrix();
glMatrixMode(GL_MODELVIEW);
gpuPushMatrix();
@ -3264,7 +3264,7 @@ void draw_nodespace_back_pix(const bContext *C, ARegion *ar, SpaceNode *snode, b
}
glMatrixMode(GL_PROJECTION);
gpuPopMatrix();
gpuPopProjectionMatrix();
glMatrixMode(GL_MODELVIEW);
gpuPopMatrix();
}

View File

@ -1189,7 +1189,7 @@ void node_set_cursor(wmWindow *win, SpaceNode *snode, float cursor[2])
void node_draw_default(const bContext *C, ARegion *ar, SpaceNode *snode, bNodeTree *ntree, bNode *node, bNodeInstanceKey key)
{
glMatrixMode(GL_PROJECTION);
gpuPushMatrix();
gpuPushProjectionMatrix();
glMatrixMode(GL_MODELVIEW);
gpuPushMatrix();
@ -1199,7 +1199,7 @@ void node_draw_default(const bContext *C, ARegion *ar, SpaceNode *snode, bNodeTr
node_draw_basis(C, ar, snode, ntree, node, key);
glMatrixMode(GL_PROJECTION);
gpuPopMatrix();
gpuPopProjectionMatrix();
glMatrixMode(GL_MODELVIEW);
gpuPopMatrix();
}

View File

@ -755,7 +755,7 @@ static void view3d_draw_bgpic(Scene *scene, ARegion *ar, View3D *v3d,
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glMatrixMode(GL_PROJECTION);
gpuPushMatrix();
gpuPushProjectionMatrix();
glMatrixMode(GL_MODELVIEW);
gpuPushMatrix();
ED_region_pixelspace(ar);
@ -778,7 +778,7 @@ static void view3d_draw_bgpic(Scene *scene, ARegion *ar, View3D *v3d,
zoomx, zoomy, col);
glMatrixMode(GL_PROJECTION);
gpuPopMatrix();
gpuPopProjectionMatrix();
glMatrixMode(GL_MODELVIEW);
gpuPopMatrix();
@ -1823,7 +1823,7 @@ void ED_view3d_draw_offscreen(
}
glMatrixMode(GL_PROJECTION);
gpuPushMatrix();
gpuPushProjectionMatrix();
gpuLoadIdentity();
glMatrixMode(GL_MODELVIEW);
gpuPushMatrix();
@ -2533,7 +2533,7 @@ void view3d_main_region_draw_legacy(const bContext *C, ARegion *ar)
bool clip_border = (render_border && !BLI_rcti_compare(&ar->drawrct, &border_rect));
glMatrixMode(GL_PROJECTION);
gpuPushMatrix();
gpuPushProjectionMatrix();
gpuLoadIdentity();
glMatrixMode(GL_MODELVIEW);
gpuPushMatrix();
@ -2565,7 +2565,7 @@ void view3d_main_region_draw_legacy(const bContext *C, ARegion *ar)
view3d_main_region_draw_info(C, scene, ar, v3d, grid_unit, render_border);
glMatrixMode(GL_PROJECTION);
gpuPopMatrix();
gpuPopProjectionMatrix();
glMatrixMode(GL_MODELVIEW);
gpuPopMatrix();

View File

@ -87,6 +87,10 @@ void gpuScale2f(float x, float y);
void gpuScale2fv(const float vec[2]);
void gpuRotate2D(float deg);
/* Projection Matrix (2D or 3D) */
void gpuPushProjectionMatrix(void);
void gpuPopProjectionMatrix(void);
/* 3D Projection Matrix */

View File

@ -44,11 +44,14 @@
typedef float Mat4[4][4];
typedef float Mat3[3][3];
typedef struct {
Mat4 ModelViewStack[MATRIX_STACK_DEPTH];
Mat4 ProjectionMatrix;
typedef struct MatrixStack {
Mat4 stack[MATRIX_STACK_DEPTH];
unsigned int top;
} MatrixStack;
unsigned top; /* of ModelView stack */
typedef struct {
MatrixStack model_view_stack;
MatrixStack projection_stack;
bool dirty;
@ -66,20 +69,23 @@ typedef struct {
{0.0f, 0.0f, 0.0f, 1.0f}}
static MatrixState state = {
.ModelViewStack = {MATRIX_4X4_IDENTITY},
.ProjectionMatrix = MATRIX_4X4_IDENTITY,
.top = 0,
.model_view_stack = {{MATRIX_4X4_IDENTITY}, 0},
.projection_stack = {{MATRIX_4X4_IDENTITY}, 0},
.dirty = true
};
#undef MATRIX_4X4_IDENTITY
#define ModelView state.ModelViewStack[state.top]
#define Projection state.ProjectionMatrix
#define ModelViewStack state.model_view_stack
#define ModelView ModelViewStack.stack[ModelViewStack.top]
#define ProjectionStack state.projection_stack
#define Projection ProjectionStack.stack[ProjectionStack.top]
void gpuMatrixReset(void)
{
state.top = 0;
state.model_view_stack.top = 0;
state.projection_stack.top = 0;
unit_m4(ModelView);
unit_m4(Projection);
state.dirty = true;
@ -119,9 +125,9 @@ void gpuPushMatrix(void)
}
#endif
BLI_assert(state.top < MATRIX_STACK_DEPTH);
state.top++;
copy_m4_m4(ModelView, state.ModelViewStack[state.top - 1]);
BLI_assert(ModelViewStack.top < MATRIX_STACK_DEPTH);
ModelViewStack.top++;
copy_m4_m4(ModelView, ModelViewStack.stack[ModelViewStack.top - 1]);
}
void gpuPopMatrix(void)
@ -134,8 +140,38 @@ void gpuPopMatrix(void)
}
#endif
BLI_assert(state.top > 0);
state.top--;
BLI_assert(ModelViewStack.top > 0);
ModelViewStack.top--;
state.dirty = true;
}
void gpuPushProjectionMatrix(void)
{
#if SUPPORT_LEGACY_MATRIX
{
glPushMatrix();
state.dirty = true;
return;
}
#endif
BLI_assert(ProjectionStack.top < MATRIX_STACK_DEPTH);
ProjectionStack.top++;
copy_m4_m4(Projection, ProjectionStack.stack[ProjectionStack.top - 1]);
}
void gpuPopProjectionMatrix(void)
{
#if SUPPORT_LEGACY_MATRIX
{
glPopMatrix();
state.dirty = true;
return;
}
#endif
BLI_assert(ProjectionStack.top > 0);
ProjectionStack.top--;
state.dirty = true;
}

View File

@ -361,7 +361,7 @@ static void playanim_toscreen(PlayState *ps, PlayAnimPict *picture, struct ImBuf
fac = 2.0f * fac - 1.0f;
glMatrixMode(GL_PROJECTION); /* TODO: convert this nasty code */
gpuPushMatrix();
gpuPushProjectionMatrix();
gpuLoadIdentity();
glMatrixMode(GL_MODELVIEW);
gpuPushMatrix();
@ -381,7 +381,7 @@ static void playanim_toscreen(PlayState *ps, PlayAnimPict *picture, struct ImBuf
gpuPopMatrix();
glMatrixMode(GL_PROJECTION);
gpuPopMatrix();
gpuPopProjectionMatrix();
glMatrixMode(GL_MODELVIEW);
}