Using stencils when DModelPanel paints results in graphical glitches on the rendered model
3 replies, posted
[img]http://i.imgur.com/31rVHn3.png[/img] [img]http://i.imgur.com/bpPze1I.png[/img]
[lua]
function self.ModelPanel:Paint(w, h)
local ent = self.Entity;
if (!IsValid(ent)) then
return;
end;
if (!self.CircleStencil) then
self.CircleStencil = util.GenerateCirclePoly(w, h, w / 2, 0, 360);
end;
render.ClearStencil();
render.SetStencilEnable(true);
--------------------------------------------------------
--- Setup the stencil & draw the circle mask onto it ---
--------------------------------------------------------
render.SetStencilWriteMask(1);
render.SetStencilTestMask(1);
render.SetStencilCompareFunction(STENCILCOMPARISONFUNCTION_NEVER);
render.SetStencilFailOperation(STENCILOPERATION_REPLACE);
render.SetStencilZFailOperation(STENCILOPERATION_KEEP);
render.SetStencilPassOperation(STENCILOPERATION_KEEP);
render.SetStencilReferenceValue(1);
surface.SetDrawColor(1, 0, 0);
draw.NoTexture();
surface.DrawPoly(self.CircleStencil);
--------------------------------------------------------
--- Now, only draw whats allowed by the stencil ---
--------------------------------------------------------
render.SetStencilCompareFunction(STENCILCOMPARISONFUNCTION_EQUAL);
render.SetStencilReferenceValue(1);
render.SetStencilFailOperation(STENCILOPERATION_ZERO);
render.SetStencilZFailOperation(STENCILOPERATION_ZERO);
render.SetStencilPassOperation(STENCILOPERATION_KEEP);
-- Background
surface.SetDrawColor(Color(20, 20, 20));
surface.DrawRect(0, 0, w, h);
surface.SetMaterial(hud.GradientDownMaterial);
surface.SetDrawColor(Color(50, 50, 50));
surface.DrawTexturedRect(0, 0, w, h);
-- Copypasted from DModelPanel.lua
local ang = self.aLookAngle;
if (!ang) then
ang = (self.vLookatPos-self.vCamPos):Angle();
end;
cam.Start3D(self.vCamPos, ang, self.fFOV, hud.x + self.x, hud.y + self.y, w, h, 5, self.FarZ);
cam.IgnoreZ(true);
render.SuppressEngineLighting(true);
render.SetLightingOrigin(self.Entity:GetPos());
render.ResetModelLighting(self.colAmbientLight.r/255, self.colAmbientLight.g/255, self.colAmbientLight.b/255);
render.SetColorModulation(self.colColor.r/255, self.colColor.g/255, self.colColor.b/255);
render.SetBlend(self.colColor.a/255);
for i =0, 6 do
local col = self.DirectionalLight[i];
if (col) then
render.SetModelLighting(i, col.r/255, col.g/255, col.b/255);
end;
end;
self.Entity:DrawModel();
render.SuppressEngineLighting(false);
cam.IgnoreZ(false);
cam.End3D();
render.SetStencilEnable(false);
render.ClearStencil();
self.LastPaint = RealTime();
return true;
end;
[/lua]
Any ideas?
The glitch disappears when I comment out the stencil calls. It's also not visible when the DModelPanel is really small (like, 100x100 px). I tried to use a simple rectangle for the stencil mask - nothing changed.
I suspect it's an issue with Z ordering - the glitch that's showing up might be the ponytail thing on the female model that shouldn't be drawn:
[img]http://i.imgur.com/PlpRD3G.png[/img]
EDIT: it's hungry
[img]http://i.imgur.com/sOQPynG.png[/img]
I'll give a few bucks to whoever helps me resolve this problem.
I'm quite desperate about it.
Could this per chance be an issue connected with combining 3D rendering in a 2D scope?
As in the DModelPanel.Paint (where the stencil is defined) is 2D, yet it's being used for a 3D thing.
Also the cam.IgnoreZ calls, I'd lie if I said I know what they actually do (I'm too dumb to get it from the gwiki description), but then again - removing them has no effect on the glitch.
I had the same issue, try adjusting the camera a tiny bit with specific models, from what I gather it's just clipping.
Try setting NearZ in cam.Start3D to something that is lower than 5. 0.05 or something.
Sorry, you need to Log In to post a reply to this thread.