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 /source/blender/gpu/intern/gpu_draw.c | |
parent | 42bff8bb1ef759caaf0c6c419349cbf4cc866e9a (diff) |
OpenGL: isolate fixed function lighting in simple shader code.
Differential Revision: https://developer.blender.org/D1645
Diffstat (limited to 'source/blender/gpu/intern/gpu_draw.c')
-rw-r--r-- | source/blender/gpu/intern/gpu_draw.c | 86 |
1 files changed, 57 insertions, 29 deletions
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 |