diff options
author | Brecht Van Lommel <brechtvanlommel@gmail.com> | 2015-11-27 23:32:14 +0300 |
---|---|---|
committer | Brecht Van Lommel <brechtvanlommel@gmail.com> | 2015-12-06 21:35:15 +0300 |
commit | 147f7a1e860587a928f465b0b4d0ecf816c997be (patch) | |
tree | f391edb612d8a53a5b31ecfb3fd2a91c3d9683b2 | |
parent | 42bff8bb1ef759caaf0c6c419349cbf4cc866e9a (diff) |
OpenGL: isolate fixed function lighting in simple shader code.
Differential Revision: https://developer.blender.org/D1645
-rw-r--r-- | source/blender/blenkernel/intern/DerivedMesh.c | 4 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/pbvh.c | 3 | ||||
-rw-r--r-- | source/blender/editors/interface/interface_draw.c | 17 | ||||
-rw-r--r-- | source/blender/editors/space_view3d/drawmesh.c | 132 | ||||
-rw-r--r-- | source/blender/editors/space_view3d/drawobject.c | 129 | ||||
-rw-r--r-- | source/blender/editors/space_view3d/view3d_draw.c | 3 | ||||
-rw-r--r-- | source/blender/gpu/GPU_draw.h | 1 | ||||
-rw-r--r-- | source/blender/gpu/intern/gpu_buffers.c | 26 | ||||
-rw-r--r-- | source/blender/gpu/intern/gpu_draw.c | 86 |
9 files changed, 166 insertions, 235 deletions
diff --git a/source/blender/blenkernel/intern/DerivedMesh.c b/source/blender/blenkernel/intern/DerivedMesh.c index c705c7adedb..07a1d3bea7f 100644 --- a/source/blender/blenkernel/intern/DerivedMesh.c +++ b/source/blender/blenkernel/intern/DerivedMesh.c @@ -3529,14 +3529,11 @@ static void navmesh_drawColored(DerivedMesh *dm) #if 0 //UI_ThemeColor(TH_WIRE); - glDisable(GL_LIGHTING); glLineWidth(2.0); dm->drawEdges(dm, 0, 1); glLineWidth(1.0); - glEnable(GL_LIGHTING); #endif - glDisable(GL_LIGHTING); /* if (GPU_buffer_legacy(dm) ) */ /* TODO - VBO draw code, not high priority - campbell */ { DEBUG_VBO("Using legacy code. drawNavMeshColored\n"); @@ -3566,7 +3563,6 @@ static void navmesh_drawColored(DerivedMesh *dm) } glEnd(); } - glEnable(GL_LIGHTING); } static void navmesh_DM_drawFacesTex( diff --git a/source/blender/blenkernel/intern/pbvh.c b/source/blender/blenkernel/intern/pbvh.c index b3cb8fbf3f6..d4c62fa57dc 100644 --- a/source/blender/blenkernel/intern/pbvh.c +++ b/source/blender/blenkernel/intern/pbvh.c @@ -1682,6 +1682,7 @@ void BKE_pbvh_node_draw(PBVHNode *node, void *data_v) #if 0 /* XXX: Just some quick code to show leaf nodes in different colors */ float col[3]; + float spec[3] = {0.0f, 0.0f, 0.0f}; if (0) { //is_partial) { col[0] = (rand() / (float)RAND_MAX); col[1] = col[2] = 0.6; @@ -1691,8 +1692,8 @@ void BKE_pbvh_node_draw(PBVHNode *node, void *data_v) for (int i = 0; i < 3; ++i) col[i] = (rand() / (float)RAND_MAX) * 0.3 + 0.7; } - glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, col); + GPU_simple_shader_colors(col, spec, 0, 1.0f); glColor3f(1, 0, 0); #endif diff --git a/source/blender/editors/interface/interface_draw.c b/source/blender/editors/interface/interface_draw.c index 97af45f6370..244aef633c1 100644 --- a/source/blender/editors/interface/interface_draw.c +++ b/source/blender/editors/interface/interface_draw.c @@ -1219,26 +1219,22 @@ void ui_draw_but_COLORBAND(uiBut *but, uiWidgetColors *UNUSED(wcol), const rcti void ui_draw_but_UNITVEC(uiBut *but, uiWidgetColors *wcol, const rcti *rect) { static GLuint displist = 0; - float diff[4], diffn[4] = {1.0f, 1.0f, 1.0f, 1.0f}; + float diffuse[3] = {1.0f, 1.0f, 1.0f}; float size; - /* store stuff */ - glGetMaterialfv(GL_FRONT, GL_DIFFUSE, diff); - /* backdrop */ glColor3ubv((unsigned char *)wcol->inner); UI_draw_roundbox_corner_set(UI_CNR_ALL); UI_draw_roundbox_gl_mode(GL_POLYGON, rect->xmin, rect->ymin, rect->xmax, rect->ymax, 5.0f); /* sphere color */ - glMaterialfv(GL_FRONT, GL_DIFFUSE, diffn); glCullFace(GL_BACK); glEnable(GL_CULL_FACE); /* setup lights */ GPULightData light = {0}; light.type = GPU_LIGHT_SUN; - copy_v3_v3(light.diffuse, diffn); + copy_v3_v3(light.diffuse, diffuse); zero_v3(light.specular); ui_but_v3_get(but, light.direction); @@ -1246,8 +1242,10 @@ void ui_draw_but_UNITVEC(uiBut *but, uiWidgetColors *wcol, const rcti *rect) for (int a = 1; a < 8; a++) GPU_simple_shader_light_set(a, NULL); - glEnable(GL_LIGHTING); - + /* setup shader */ + GPU_simple_shader_colors(diffuse, NULL, 0, 1.0f); + GPU_simple_shader_bind(GPU_SHADER_LIGHTING); + /* transform to button */ glPushMatrix(); glTranslatef(rect->xmin + 0.5f * BLI_rcti_size_x(rect), rect->ymin + 0.5f * BLI_rcti_size_y(rect), 0.0f); @@ -1278,10 +1276,9 @@ void ui_draw_but_UNITVEC(uiBut *but, uiWidgetColors *wcol, const rcti *rect) glCallList(displist); /* restore */ + GPU_simple_shader_bind(GPU_SHADER_USE_COLOR); GPU_default_lights(); - glDisable(GL_LIGHTING); glDisable(GL_CULL_FACE); - glMaterialfv(GL_FRONT, GL_DIFFUSE, diff); /* AA circle */ glEnable(GL_BLEND); diff --git a/source/blender/editors/space_view3d/drawmesh.c b/source/blender/editors/space_view3d/drawmesh.c index cfffeb01b06..452e36e6eb5 100644 --- a/source/blender/editors/space_view3d/drawmesh.c +++ b/source/blender/editors/space_view3d/drawmesh.c @@ -63,6 +63,7 @@ #include "GPU_extensions.h" #include "GPU_draw.h" #include "GPU_material.h" +#include "GPU_simple_shader.h" #include "RE_engine.h" @@ -179,7 +180,6 @@ void draw_mesh_face_select(RegionView3D *rv3d, Mesh *me, DerivedMesh *dm, bool d data.edge_flags = get_tface_mesh_marked_edge_info(me, draw_select_edges); glEnable(GL_DEPTH_TEST); - glDisable(GL_LIGHTING); ED_view3d_polygon_offset(rv3d, 1.0); /* Draw (Hidden) Edges */ @@ -231,10 +231,11 @@ static struct TextureDrawState { int is_lit, is_tex; int color_profile; bool use_backface_culling; + bool two_sided_lighting; unsigned char obcol[4]; bool is_texpaint; bool texpaint_material; /* use material slots for texture painting */ -} Gtexdraw = {NULL, NULL, NULL, false, 0, 0, 0, false, {0, 0, 0, 0}, false, false}; +} Gtexdraw = {NULL, NULL, NULL, false, 0, 0, 0, false, false, {0, 0, 0, 0}, false, false}; static bool set_draw_settings_cached(int clearcache, MTexPoly *texface, Material *ma, struct TextureDrawState gtexdraw) { @@ -370,24 +371,26 @@ static bool set_draw_settings_cached(int clearcache, MTexPoly *texface, Material if (c_badtex) lit = 0; if (lit != c_lit || ma != c_ma) { if (lit) { - float spec[4]; - if (!ma) ma = give_current_material_or_def(NULL, 0); /* default material */ - - spec[0] = ma->spec * ma->specr; - spec[1] = ma->spec * ma->specg; - spec[2] = ma->spec * ma->specb; - spec[3] = 1.0; - - glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, spec); - glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE); - glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, CLAMPIS(ma->har, 0, 128)); - glEnable(GL_LIGHTING); - glEnable(GL_COLOR_MATERIAL); + int options = GPU_SHADER_LIGHTING | GPU_SHADER_USE_COLOR; + + if (gtexdraw.two_sided_lighting) + options |= GPU_SHADER_TWO_SIDED; + if (c_textured && !c_badtex) + options |= GPU_SHADER_TEXTURE_2D; + + if (!ma) + ma = give_current_material_or_def(NULL, 0); /* default material */ + + float specular[3]; + mul_v3_v3fl(specular, &ma->specr, ma->spec); + + GPU_simple_shader_colors(NULL, specular, ma->har, 0.0f); + GPU_simple_shader_bind(options); } else { - glDisable(GL_LIGHTING); - glDisable(GL_COLOR_MATERIAL); + GPU_simple_shader_bind(GPU_SHADER_USE_COLOR); } + c_lit = lit; c_ma = ma; } @@ -488,12 +491,12 @@ static void draw_textured_begin(Scene *scene, View3D *v3d, RegionView3D *rv3d, O Gtexdraw.color_profile = BKE_scene_check_color_management_enabled(scene); Gtexdraw.use_game_mat = (RE_engines_find(scene->r.engine)->flag & RE_GAME) != 0; Gtexdraw.use_backface_culling = (v3d->flag2 & V3D_BACKFACE_CULLING) != 0; + Gtexdraw.two_sided_lighting = (me->flag & ME_TWOSIDED); memcpy(Gtexdraw.obcol, obcol, sizeof(obcol)); set_draw_settings_cached(1, NULL, NULL, Gtexdraw); glShadeModel(GL_SMOOTH); glCullFace(GL_BACK); - glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, (me->flag & ME_TWOSIDED) ? GL_TRUE : GL_FALSE); } static void draw_textured_end(void) @@ -526,7 +529,7 @@ static void draw_textured_end(void) glShadeModel(GL_FLAT); glDisable(GL_CULL_FACE); - glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE); + GPU_simple_shader_bind(GPU_SHADER_USE_COLOR); /* XXX, bad patch - GPU_default_lights() calls * glLightfv(GL_POSITION, ...) which @@ -1016,6 +1019,8 @@ typedef struct TexMatCallback { Object *ob; Mesh *me; DerivedMesh *dm; + bool shadeless; + bool two_sided_lighting; } TexMatCallback; static void tex_mat_set_material_cb(void *UNUSED(userData), int mat_nr, void *attribs) @@ -1035,31 +1040,21 @@ static void tex_mat_set_texture_cb(void *userData, int mat_nr, void *attribs) Image *ima; ImageUser *iuser; bNode *node; - int texture_set = 0; /* draw image texture if we find one */ if (ED_object_get_active_image(data->ob, mat_nr, &ima, &iuser, &node, NULL)) { /* get openl texture */ int mipmap = 1; int bindcode = (ima) ? GPU_verify_image(ima, iuser, 0, 0, mipmap, false) : 0; - float zero[4] = {0.0f, 0.0f, 0.0f, 0.0f}; if (bindcode) { NodeTexBase *texbase = node->storage; /* disable existing material */ GPU_object_material_unbind(); - glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, zero); - glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, zero); - glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, 0); /* bind texture */ - glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE); - glEnable(GL_COLOR_MATERIAL); - glEnable(GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D, ima->bindcode); - glColor3f(1.0f, 1.0f, 1.0f); glMatrixMode(GL_TEXTURE); glLoadMatrixf(texbase->tex_mapping.mat); @@ -1073,20 +1068,35 @@ static void tex_mat_set_texture_cb(void *userData, int mat_nr, void *attribs) gattribs->layer[0].gltexco = 1; gattribs->totlayer = 1; - texture_set = 1; + /* bind material */ + float diffuse[3] = {1.0f, 1.0f, 1.0f}; + + int options = GPU_SHADER_TEXTURE_2D; + if (!data->shadeless) + options |= GPU_SHADER_LIGHTING; + if (data->two_sided_lighting) + options |= GPU_SHADER_TWO_SIDED; + + GPU_simple_shader_colors(diffuse, NULL, 0, 0.0f); + GPU_simple_shader_bind(options); + + return; } } - if (!texture_set) { + /* disable texture material */ + GPU_simple_shader_bind(GPU_SHADER_USE_COLOR); + + if (data->shadeless) { + glColor3f(1.0f, 1.0f, 1.0f); + memset(gattribs, 0, sizeof(*gattribs)); + } + else { glMatrixMode(GL_TEXTURE); glLoadIdentity(); glMatrixMode(GL_MODELVIEW); - /* disable texture */ - glDisable(GL_TEXTURE_2D); - glDisable(GL_COLOR_MATERIAL); - - /* draw single color */ + /* enable solid material */ GPU_object_material_bind(mat_nr, attribs); } } @@ -1140,17 +1150,11 @@ void draw_mesh_textured(Scene *scene, View3D *v3d, RegionView3D *rv3d, Mesh *me = ob->data; - if ((v3d->flag2 & V3D_SHADELESS_TEX) && - ((v3d->drawtype == OB_TEXTURE) || (ob->mode & OB_MODE_TEXTURE_PAINT))) - { - glColor3f(1.0f, 1.0f, 1.0f); - } - else { - glEnable(GL_LIGHTING); - glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, (me->flag & ME_TWOSIDED) ? GL_TRUE : GL_FALSE); - } + bool shadeless = ((v3d->flag2 & V3D_SHADELESS_TEX) && + ((v3d->drawtype == OB_TEXTURE) || (ob->mode & OB_MODE_TEXTURE_PAINT))); + bool two_sided_lighting = (me->flag & ME_TWOSIDED) != 0; - TexMatCallback data = {scene, ob, me, dm}; + TexMatCallback data = {scene, ob, me, dm, shadeless, two_sided_lighting}; bool (*set_face_cb)(void *, int); bool picking = (G.f & G_PICKSEL) != 0; @@ -1174,13 +1178,7 @@ void draw_mesh_textured(Scene *scene, View3D *v3d, RegionView3D *rv3d, set_face_cb, &data); } else { - float zero[4] = {0.0f, 0.0f, 0.0f, 0.0f}; - /* draw textured */ - glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, zero); - glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, zero); - glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, 0); - dm->drawMappedFacesMat(dm, tex_mat_set_texture_cb, set_face_cb, &data); @@ -1189,11 +1187,11 @@ void draw_mesh_textured(Scene *scene, View3D *v3d, RegionView3D *rv3d, GPU_end_object_materials(); /* reset opengl state */ - glDisable(GL_COLOR_MATERIAL); - glDisable(GL_TEXTURE_2D); - glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE); - glDisable(GL_LIGHTING); + GPU_end_object_materials(); + GPU_simple_shader_bind(GPU_SHADER_USE_COLOR); + glBindTexture(GL_TEXTURE_2D, 0); + glFrontFace(GL_CCW); glMatrixMode(GL_TEXTURE); @@ -1210,25 +1208,15 @@ void draw_mesh_textured(Scene *scene, View3D *v3d, RegionView3D *rv3d, /* Vertex Paint and Weight Paint */ static void draw_mesh_paint_light_begin(void) { - const float spec[4] = {0.47f, 0.47f, 0.47f, 0.47f}; - - GPU_object_material_bind(0, NULL); - - /* but set default spec */ - glColorMaterial(GL_FRONT_AND_BACK, GL_SPECULAR); - glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, spec); - - /* diffuse */ - glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE); - glEnable(GL_LIGHTING); - glEnable(GL_COLOR_MATERIAL); + /* get material diffuse color from vertex colors but set default spec */ + const float specular[3] = {0.47f, 0.47f, 0.47f}; + GPU_simple_shader_colors(NULL, specular, 35, 1.0f); + GPU_simple_shader_bind(GPU_SHADER_LIGHTING | GPU_SHADER_USE_COLOR); } + static void draw_mesh_paint_light_end(void) { - glDisable(GL_COLOR_MATERIAL); - glDisable(GL_LIGHTING); - - GPU_object_material_unbind(); + GPU_simple_shader_bind(GPU_SHADER_USE_COLOR); } void draw_mesh_paint_weight_faces(DerivedMesh *dm, const bool use_light, diff --git a/source/blender/editors/space_view3d/drawobject.c b/source/blender/editors/space_view3d/drawobject.c index 53b14eb75b2..af44c8ca998 100644 --- a/source/blender/editors/space_view3d/drawobject.c +++ b/source/blender/editors/space_view3d/drawobject.c @@ -89,6 +89,7 @@ #include "GPU_draw.h" #include "GPU_extensions.h" #include "GPU_select.h" +#include "GPU_simple_shader.h" #include "ED_mesh.h" #include "ED_particle.h" @@ -1692,8 +1693,6 @@ static void draw_viewport_object_reconstruction(Scene *scene, Base *base, View3D v3d->bundle_size / 0.05f / camera_size[2]); if (v3d->drawtype == OB_WIRE) { - glDisable(GL_LIGHTING); - if ((dflag & DRAW_CONSTCOLOR) == 0) { if (selected && (track->flag & TRACK_CUSTOMCOLOR) == 0) { glColor3ubv(ob_wire_col); @@ -1704,8 +1703,6 @@ static void draw_viewport_object_reconstruction(Scene *scene, Base *base, View3D } drawaxes(0.05f, v3d->bundle_drawtype); - - glEnable(GL_LIGHTING); } else if (v3d->drawtype > OB_WIRE) { if (v3d->bundle_drawtype == OB_EMPTY_SPHERE) { @@ -1716,13 +1713,11 @@ static void draw_viewport_object_reconstruction(Scene *scene, Base *base, View3D } glLineWidth(2.0f); - glDisable(GL_LIGHTING); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); draw_bundle_sphere(); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); - glEnable(GL_LIGHTING); glLineWidth(1.0f); } @@ -1734,8 +1729,6 @@ static void draw_viewport_object_reconstruction(Scene *scene, Base *base, View3D draw_bundle_sphere(); } else { - glDisable(GL_LIGHTING); - if ((dflag & DRAW_CONSTCOLOR) == 0) { if (selected) { glColor3ubv(ob_wire_col); @@ -1747,8 +1740,6 @@ static void draw_viewport_object_reconstruction(Scene *scene, Base *base, View3D } drawaxes(0.05f, v3d->bundle_drawtype); - - glEnable(GL_LIGHTING); } } @@ -1776,7 +1767,6 @@ static void draw_viewport_object_reconstruction(Scene *scene, Base *base, View3D MovieReconstructedCamera *camera = reconstruction->cameras; int a = 0; - glDisable(GL_LIGHTING); UI_ThemeColor(TH_CAMERA_PATH); glLineWidth(2.0f); @@ -1787,7 +1777,6 @@ static void draw_viewport_object_reconstruction(Scene *scene, Base *base, View3D glEnd(); glLineWidth(1.0f); - glEnable(GL_LIGHTING); } } } @@ -1811,9 +1800,9 @@ static void draw_viewport_reconstruction(Scene *scene, Base *base, View3D *v3d, if (v3d->flag2 & V3D_RENDER_OVERRIDE) return; - glEnable(GL_LIGHTING); - glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE); - glEnable(GL_COLOR_MATERIAL); + GPU_simple_shader_colors(NULL, NULL, 0, 1.0f); + GPU_simple_shader_bind(GPU_SHADER_LIGHTING | GPU_SHADER_USE_COLOR); + glShadeModel(GL_SMOOTH); tracking_object = tracking->objects.first; @@ -1826,8 +1815,7 @@ static void draw_viewport_reconstruction(Scene *scene, Base *base, View3D *v3d, /* restore */ glShadeModel(GL_FLAT); - glDisable(GL_COLOR_MATERIAL); - glDisable(GL_LIGHTING); + GPU_simple_shader_bind(GPU_SHADER_USE_COLOR); if ((dflag & DRAW_CONSTCOLOR) == 0) { glColor3ubv(ob_wire_col); @@ -2135,7 +2123,6 @@ static void drawcamera(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base BKE_camera_view_frame_ex(scene, cam, cam->drawsize, is_view, scale, asp, shift, &drawsize, vec); - glDisable(GL_LIGHTING); glDisable(GL_CULL_FACE); /* camera frame */ @@ -3815,17 +3802,12 @@ static void draw_em_fancy(Scene *scene, ARegion *ar, View3D *v3d, } } else { - /* 3 floats for position, - * 3 for normal and times two because the faces may actually be quads instead of triangles */ - glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, (me->flag & ME_TWOSIDED) ? GL_TRUE : GL_FALSE); - - glEnable(GL_LIGHTING); glFrontFace((ob->transflag & OB_NEG_SCALE) ? GL_CW : GL_CCW); finalDM->drawMappedFaces(finalDM, draw_em_fancy__setFaceOpts, GPU_object_material_bind, NULL, me->edit_btmesh, DM_DRAW_SKIP_HIDDEN | DM_DRAW_NEED_NORMALS); glFrontFace(GL_CCW); - glDisable(GL_LIGHTING); - glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE); + + GPU_object_material_unbind(); } /* Setup for drawing wire over, disable zbuffer @@ -4004,13 +3986,13 @@ static void draw_mesh_object_outline(View3D *v3d, Object *ob, DerivedMesh *dm) glLineWidth(UI_GetThemeValuef(TH_OUTLINE_WIDTH) * 2.0f); glDepthMask(0); - /* if transparent, we cannot draw the edges for solid select... edges have no material info. - * drawFacesSolid() doesn't draw the transparent faces */ + /* if transparent, we cannot draw the edges for solid select... edges + * have no material info. GPU_object_material_visible will skip the + * transparent faces */ if (ob->dtx & OB_DRAWTRANSP) { glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); - dm->drawFacesSolid(dm, NULL, 0, GPU_object_material_bind); + dm->drawFacesSolid(dm, NULL, 0, GPU_object_material_visible); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); - GPU_object_material_unbind(); } else { dm->drawEdges(dm, 0, 1); @@ -4156,9 +4138,10 @@ static void draw_mesh_fancy(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D /* for object selection draws no shade */ if (dflag & (DRAW_PICKING | DRAW_CONSTCOLOR)) { dm->drawFacesSolid(dm, NULL, 0, GPU_object_material_bind); + GPU_object_material_unbind(); } else { - const float spec[4] = {0.47f, 0.47f, 0.47f, 0.47f}; + const float specular[3] = {0.47f, 0.47f, 0.47f}; /* draw outline */ if ((v3d->flag & V3D_SELECT_OUTLINE) && @@ -4173,21 +4156,13 @@ static void draw_mesh_fancy(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D /* materials arent compatible with vertex colors */ GPU_end_object_materials(); - GPU_object_material_bind(0, NULL); - - /* set default spec */ - glColorMaterial(GL_FRONT_AND_BACK, GL_SPECULAR); - glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, spec); - /* diffuse */ - glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE); - glEnable(GL_LIGHTING); - glEnable(GL_COLOR_MATERIAL); + /* set default specular */ + GPU_simple_shader_colors(NULL, specular, 35, 1.0f); + GPU_simple_shader_bind(GPU_SHADER_LIGHTING | GPU_SHADER_USE_COLOR); dm->drawMappedFaces(dm, NULL, NULL, NULL, NULL, DM_DRAW_USE_COLORS | DM_DRAW_NEED_NORMALS); - glDisable(GL_COLOR_MATERIAL); - glDisable(GL_LIGHTING); - GPU_object_material_unbind(); + GPU_simple_shader_bind(GPU_SHADER_USE_COLOR); } } else { @@ -4202,9 +4177,6 @@ static void draw_mesh_fancy(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D draw_mesh_object_outline(v3d, ob, dm); } - glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, (me->flag & ME_TWOSIDED) ? GL_TRUE : GL_FALSE); - - glEnable(GL_LIGHTING); glFrontFace((ob->transflag & OB_NEG_SCALE) ? GL_CW : GL_CCW); if (ob->sculpt && (p = BKE_paint_get_active(scene))) { @@ -4225,12 +4197,9 @@ static void draw_mesh_fancy(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D else dm->drawFacesSolid(dm, NULL, 0, GPU_object_material_bind); - GPU_object_material_unbind(); - glFrontFace(GL_CCW); - glDisable(GL_LIGHTING); - glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE); + GPU_object_material_unbind(); if (!ob->sculpt && (v3d->flag2 & V3D_RENDER_OVERRIDE) == 0) { if ((dflag & DRAW_CONSTCOLOR) == 0) { @@ -4534,7 +4503,6 @@ static void drawDispListsolid(ListBase *lb, Object *ob, const short dflag, if (lb == NULL) return; - glEnable(GL_LIGHTING); glEnableClientState(GL_VERTEX_ARRAY); if (ob->type == OB_MBALL) { /* mball always smooth shaded */ @@ -4551,8 +4519,6 @@ static void drawDispListsolid(ListBase *lb, Object *ob, const short dflag, if (ob->type == OB_SURF) { int nr; - glDisable(GL_LIGHTING); - if ((dflag & DRAW_CONSTCOLOR) == 0) glColor3ubv(ob_wire_col); @@ -4563,16 +4529,12 @@ static void drawDispListsolid(ListBase *lb, Object *ob, const short dflag, for (nr = dl->nr; nr; nr--, data += 3) glVertex3fv(data); glEnd(); - - glEnable(GL_LIGHTING); } break; case DL_POLY: if (ob->type == OB_SURF) { int nr; - glDisable(GL_LIGHTING); - /* for some reason glDrawArrays crashes here in half of the platforms (not osx) */ //glVertexPointer(3, GL_FLOAT, 0, dl->verts); //glDrawArrays(GL_LINE_LOOP, 0, dl->nr); @@ -4581,8 +4543,6 @@ static void drawDispListsolid(ListBase *lb, Object *ob, const short dflag, for (nr = dl->nr; nr; nr--, data += 3) glVertex3fv(data); glEnd(); - - glEnable(GL_LIGHTING); } break; case DL_SURF: @@ -4637,8 +4597,9 @@ static void drawDispListsolid(ListBase *lb, Object *ob, const short dflag, glDisableClientState(GL_VERTEX_ARRAY); glShadeModel(GL_FLAT); - glDisable(GL_LIGHTING); glFrontFace(GL_CCW); + + GPU_object_material_unbind(); } static void drawCurveDMWired(Object *ob) @@ -4665,11 +4626,8 @@ static bool drawCurveDerivedMesh(Scene *scene, View3D *v3d, RegionView3D *rv3d, int glsl = draw_glsl_material(scene, ob, v3d, dt); GPU_begin_object_materials(v3d, rv3d, scene, ob, glsl, NULL); - if (!glsl) { - glEnable(GL_LIGHTING); + if (!glsl) dm->drawFacesSolid(dm, NULL, 0, GPU_object_material_bind); - glDisable(GL_LIGHTING); - } else dm->drawFacesGLSL(dm, GPU_object_material_bind); @@ -5503,19 +5461,10 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv glEnableClientState(GL_COLOR_ARRAY); } - glEnable(GL_LIGHTING); - glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE); - glEnable(GL_COLOR_MATERIAL); - } -#if 0 - else { - glDisableClientState(GL_NORMAL_ARRAY); - - glDisable(GL_COLOR_MATERIAL); - glDisable(GL_LIGHTING); - UI_ThemeColor(TH_WIRE); + // XXX test + GPU_simple_shader_colors(NULL, NULL, 0.0f, 1.0f); + GPU_simple_shader_bind(GPU_SHADER_LIGHTING | GPU_SHADER_USE_COLOR); } -#endif if (totchild && (part->draw & PART_DRAW_PARENT) == 0) totpart = 0; @@ -5546,8 +5495,6 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv if (part->draw & PART_DRAW_GUIDE_HAIRS) { DerivedMesh *hair_dm = psys->hair_out_dm; - glDisable(GL_LIGHTING); - glDisable(GL_COLOR_MATERIAL); glDisableClientState(GL_NORMAL_ARRAY); glDisableClientState(GL_COLOR_ARRAY); @@ -5591,8 +5538,6 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv glEnd(); } - glEnable(GL_LIGHTING); - glEnable(GL_COLOR_MATERIAL); glEnableClientState(GL_NORMAL_ARRAY); if ((dflag & DRAW_CONSTCOLOR) == 0) if (part->draw_col == PART_DRAW_COL_MAT) @@ -5607,8 +5552,6 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv int *res = clmd->hair_grid_res; int i; - glDisable(GL_LIGHTING); - glDisable(GL_COLOR_MATERIAL); glDisableClientState(GL_NORMAL_ARRAY); glDisableClientState(GL_COLOR_ARRAY); @@ -5663,8 +5606,6 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv glEnd(); glDisable(GL_BLEND); - glEnable(GL_LIGHTING); - glEnable(GL_COLOR_MATERIAL); glEnableClientState(GL_NORMAL_ARRAY); if ((dflag & DRAW_CONSTCOLOR) == 0) if (part->draw_col == PART_DRAW_COL_MAT) @@ -5695,7 +5636,7 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv if (1) { //ob_dt > OB_WIRE) { if (part->draw_col == PART_DRAW_COL_MAT) glDisableClientState(GL_COLOR_ARRAY); - glDisable(GL_COLOR_MATERIAL); + GPU_simple_shader_bind(GPU_SHADER_USE_COLOR); } if (cdata2) { @@ -5756,11 +5697,8 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv if (pdd->ndata && ob_dt > OB_WIRE) { glEnableClientState(GL_NORMAL_ARRAY); glNormalPointer(GL_FLOAT, 0, pdd->ndata); - glEnable(GL_LIGHTING); - } - else { - glDisableClientState(GL_NORMAL_ARRAY); - glDisable(GL_LIGHTING); + GPU_simple_shader_colors(NULL, NULL, 0.0f, 1.0f); + GPU_simple_shader_bind(GPU_SHADER_LIGHTING | GPU_SHADER_USE_COLOR); } if ((dflag & DRAW_CONSTCOLOR) == 0) { @@ -5792,7 +5730,7 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv /* 7. */ - glDisable(GL_LIGHTING); + GPU_simple_shader_bind(GPU_SHADER_USE_COLOR); glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_NORMAL_ARRAY); @@ -5867,14 +5805,10 @@ static void draw_ptcache_edit(Scene *scene, View3D *v3d, PTCacheEdit *edit) glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); - glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE); - glEnable(GL_COLOR_MATERIAL); glShadeModel(GL_SMOOTH); - if (pset->brushtype == PE_BRUSH_WEIGHT) { + if (pset->brushtype == PE_BRUSH_WEIGHT) glLineWidth(2.0f); - glDisable(GL_LIGHTING); - } cache = edit->pathcache; for (i = 0, point = edit->points; i < totpoint; i++, point++) { @@ -5987,8 +5921,6 @@ static void draw_ptcache_edit(Scene *scene, View3D *v3d, PTCacheEdit *edit) } glDisable(GL_BLEND); - glDisable(GL_LIGHTING); - glDisable(GL_COLOR_MATERIAL); glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_NORMAL_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); @@ -8664,7 +8596,6 @@ static void draw_object_mesh_instance(Scene *scene, View3D *v3d, RegionView3D *r } glFrontFace((ob->transflag & OB_NEG_SCALE) ? GL_CW : GL_CCW); - glEnable(GL_LIGHTING); if (dm) { dm->drawFacesSolid(dm, NULL, 0, GPU_object_material_bind); @@ -8673,7 +8604,7 @@ static void draw_object_mesh_instance(Scene *scene, View3D *v3d, RegionView3D *r else if (edm) edm->drawMappedFaces(edm, NULL, GPU_object_material_bind, NULL, NULL, DM_DRAW_NEED_NORMALS); - glDisable(GL_LIGHTING); + GPU_object_material_unbind(); } if (edm) edm->release(edm); diff --git a/source/blender/editors/space_view3d/view3d_draw.c b/source/blender/editors/space_view3d/view3d_draw.c index 9393092fffa..781c848285a 100644 --- a/source/blender/editors/space_view3d/view3d_draw.c +++ b/source/blender/editors/space_view3d/view3d_draw.c @@ -3309,9 +3309,6 @@ ImBuf *ED_view3d_draw_offscreen_imbuf( float winmat[4][4]; if (own_ofs) { - /* state changes make normal drawing go weird otherwise */ - glPushAttrib(GL_LIGHTING_BIT); - /* bind */ ofs = GPU_offscreen_create(sizex, sizey, full_samples ? 0 : samples, err_out); if (ofs == NULL) { diff --git a/source/blender/gpu/GPU_draw.h b/source/blender/gpu/GPU_draw.h index ce30f7460a1..cb288e9f448 100644 --- a/source/blender/gpu/GPU_draw.h +++ b/source/blender/gpu/GPU_draw.h @@ -73,6 +73,7 @@ bool GPU_object_materials_check(void); int GPU_object_material_bind(int nr, void *attribs); void GPU_object_material_unbind(void); +int GPU_object_material_visible(int nr, void *attribs); void GPU_begin_dupli_object(struct DupliObject *dob); void GPU_end_dupli_object(void); diff --git a/source/blender/gpu/intern/gpu_buffers.c b/source/blender/gpu/intern/gpu_buffers.c index 6a60b4dfd37..efbd4c1afda 100644 --- a/source/blender/gpu/intern/gpu_buffers.c +++ b/source/blender/gpu/intern/gpu_buffers.c @@ -57,6 +57,7 @@ #include "GPU_buffers.h" #include "GPU_draw.h" +#include "GPU_simple_shader.h" #include "bmesh.h" @@ -1016,19 +1017,6 @@ struct GPU_PBVH_Buffers { float diffuse_color[4]; }; -static void gpu_colors_enable(void) -{ - glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE); - glEnable(GL_COLOR_MATERIAL); - glEnableClientState(GL_COLOR_ARRAY); -} - -static void gpu_colors_disable(void) -{ - glDisable(GL_COLOR_MATERIAL); - glDisableClientState(GL_COLOR_ARRAY); -} - static float gpu_color_from_mask(float mask) { return 1.0f - mask * 0.75f; @@ -1849,10 +1837,15 @@ void GPU_draw_pbvh_buffers(GPU_PBVH_Buffers *buffers, DMSetMaterial setMaterial, if (buffers->vert_buf) { char *base = NULL; char *index_base = NULL; + int bound_options = 0; glEnableClientState(GL_VERTEX_ARRAY); if (!wireframe) { glEnableClientState(GL_NORMAL_ARRAY); - gpu_colors_enable(); + glEnableClientState(GL_COLOR_ARRAY); + + /* weak inspection of bound options, should not be necessary ideally */ + bound_options = GPU_simple_shader_bound_options(); + GPU_simple_shader_bind(bound_options | GPU_SHADER_USE_COLOR); } GPU_buffer_bind(buffers->vert_buf, GPU_BINDING_ARRAY); @@ -1931,7 +1924,8 @@ void GPU_draw_pbvh_buffers(GPU_PBVH_Buffers *buffers, DMSetMaterial setMaterial, glDisableClientState(GL_VERTEX_ARRAY); if (!wireframe) { glDisableClientState(GL_NORMAL_ARRAY); - gpu_colors_disable(); + glDisableClientState(GL_COLOR_ARRAY); + GPU_simple_shader_bind(bound_options); } } } @@ -2047,8 +2041,6 @@ void GPU_init_draw_pbvh_BB(void) glEnableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_COLOR_ARRAY); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); - glDisable(GL_LIGHTING); - glDisable(GL_COLOR_MATERIAL); glEnable(GL_BLEND); } diff --git a/source/blender/gpu/intern/gpu_draw.c b/source/blender/gpu/intern/gpu_draw.c index 78a420f02f4..cb4d0a75cd3 100644 --- a/source/blender/gpu/intern/gpu_draw.c +++ b/source/blender/gpu/intern/gpu_draw.c @@ -1401,9 +1401,10 @@ void GPU_free_images_old(void) /* OpenGL state caching for materials */ typedef struct GPUMaterialFixed { - float diff[4]; - float spec[4]; + float diff[3]; + float spec[3]; int hard; + float alpha; } GPUMaterialFixed; static struct GPUMaterialState { @@ -1446,20 +1447,18 @@ static void gpu_material_to_fixed(GPUMaterialFixed *smat, const Material *bmat, { if (bmat->mode & MA_SHLESS) { copy_v3_v3(smat->diff, &bmat->r); - smat->diff[3] = 1.0; if (gamma) linearrgb_to_srgb_v3_v3(smat->diff, smat->diff); - zero_v4(smat->spec); + zero_v3(smat->spec); + smat->alpha = 1.0f; smat->hard = 0; } else if (new_shading_nodes) { copy_v3_v3(smat->diff, &bmat->r); - smat->diff[3] = 1.0; - copy_v3_v3(smat->spec, &bmat->specr); - smat->spec[3] = 1.0; + smat->alpha = 1.0f; smat->hard = CLAMPIS(bmat->har, 0, 128); if (dimdown) { @@ -1474,14 +1473,13 @@ static void gpu_material_to_fixed(GPUMaterialFixed *smat, const Material *bmat, } else { mul_v3_v3fl(smat->diff, &bmat->r, bmat->ref + bmat->emit); - smat->diff[3] = 1.0; /* caller may set this to bmat->alpha */ if (bmat->shade_flag & MA_OBCOLOR) mul_v3_v3(smat->diff, ob->col); mul_v3_v3fl(smat->spec, &bmat->specr, bmat->spec); - smat->spec[3] = 1.0; /* always 1 */ - smat->hard= CLAMPIS(bmat->har, 0, 128); + smat->hard = CLAMPIS(bmat->har, 1, 128); + smat->alpha = 1.0f; if (gamma) { linearrgb_to_srgb_v3_v3(smat->diff, smat->diff); @@ -1651,11 +1649,11 @@ void GPU_begin_object_materials(View3D *v3d, RegionView3D *rv3d, Scene *scene, O gpu_material_to_fixed(&GMS.matbuf[a], ma, gamma, ob, new_shading_nodes, false); if (GMS.use_alpha_pass && ((ma->mode & MA_TRANSP) || (new_shading_nodes && ma->alpha != 1.0f))) { - GMS.matbuf[a].diff[3] = ma->alpha; + GMS.matbuf[a].alpha = ma->alpha; alphablend = (ma->alpha == 1.0f)? GPU_BLEND_SOLID: GPU_BLEND_ALPHA; } else { - GMS.matbuf[a].diff[3] = 1.0f; + GMS.matbuf[a].alpha = 1.0f; alphablend = GPU_BLEND_SOLID; } } @@ -1712,19 +1710,17 @@ int GPU_object_material_bind(int nr, void *attribs) /* no GPU_begin_object_materials, use default material */ if (!GMS.matbuf) { - float diff[4], spec[4]; - memset(&GMS, 0, sizeof(GMS)); - mul_v3_v3fl(diff, &defmaterial.r, defmaterial.ref + defmaterial.emit); - diff[3] = 1.0; - - mul_v3_v3fl(spec, &defmaterial.specr, defmaterial.spec); - spec[3] = 1.0; + float diffuse[3], specular[3]; + mul_v3_v3fl(diffuse, &defmaterial.r, defmaterial.ref + defmaterial.emit); + mul_v3_v3fl(specular, &defmaterial.specr, defmaterial.spec); + GPU_simple_shader_colors(diffuse, specular, 35, 1.0f); - glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diff); - glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, spec); - glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, 35); /* blender default */ + if (GMS.two_sided_lighting) + GPU_simple_shader_bind(GPU_SHADER_LIGHTING | GPU_SHADER_TWO_SIDED); + else + GPU_simple_shader_bind(GPU_SHADER_LIGHTING); return 0; } @@ -1801,9 +1797,13 @@ int GPU_object_material_bind(int nr, void *attribs) } else { /* or do fixed function opengl material */ - glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, GMS.matbuf[nr].diff); - glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, GMS.matbuf[nr].spec); - glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, GMS.matbuf[nr].hard); + GPU_simple_shader_colors(GMS.matbuf[nr].diff, + GMS.matbuf[nr].spec, GMS.matbuf[nr].hard, GMS.matbuf[nr].alpha); + + if (GMS.two_sided_lighting) + GPU_simple_shader_bind(GPU_SHADER_LIGHTING | GPU_SHADER_TWO_SIDED); + else + GPU_simple_shader_bind(GPU_SHADER_LIGHTING); } /* set (alpha) blending mode */ @@ -1813,6 +1813,31 @@ int GPU_object_material_bind(int nr, void *attribs) return GMS.lastretval; } +int GPU_object_material_visible(int nr, void *attribs) +{ + GPUVertexAttribs *gattribs = attribs; + int visible; + + if (!GMS.matbuf) + return 0; + + if (gattribs) + memset(gattribs, 0, sizeof(*gattribs)); + + if (nr>=GMS.totmat) + nr = 0; + + if (GMS.use_alpha_pass) { + visible = ELEM(GMS.alphablend[nr], GPU_BLEND_SOLID, GPU_BLEND_CLIP); + if (GMS.is_alpha_pass) + visible = !visible; + } + else + visible = !GMS.is_alpha_pass; + + return visible; +} + void GPU_set_material_alpha_blend(int alphablend) { if (GMS.lastalphablend == alphablend) @@ -1840,6 +1865,8 @@ void GPU_object_material_unbind(void) GPU_material_unbind(GPU_material_from_blender(GMS.gscene, GMS.gboundmat, GMS.is_opensubdiv)); GMS.gboundmat = NULL; } + else + GPU_simple_shader_bind(GPU_SHADER_USE_COLOR); GPU_set_material_alpha_blend(GPU_BLEND_SOLID); } @@ -1855,7 +1882,8 @@ void GPU_material_diffuse_get(int nr, float diff[4]) mul_v3_v3fl(diff, &defmaterial.r, defmaterial.ref + defmaterial.emit); } else { - copy_v4_v4(diff, GMS.matbuf[nr].diff); + copy_v3_v3(diff, GMS.matbuf[nr].diff); + diff[3] = GMS.matbuf[nr].alpha; } } @@ -1941,9 +1969,6 @@ int GPU_default_lights(void) GPU_simple_shader_light_set(a, NULL); } - glDisable(GL_LIGHTING); - glDisable(GL_COLOR_MATERIAL); - return count; } @@ -2066,6 +2091,7 @@ void GPU_state_init(void) glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_specular); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular); glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, 35); + glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE); GPU_default_lights(); @@ -2125,6 +2151,8 @@ void GPU_state_init(void) glDisable(GL_CULL_FACE); gpu_multisample(false); + + GPU_simple_shader_bind(GPU_SHADER_USE_COLOR); } #ifdef WITH_OPENSUBDIV |