Game Development Community

Cel Shading / Outlining Revisited

by Drew Robson · in Torque Game Engine · 07/09/2007 (7:15 am) · 1 replies

Hi all,

I've implemented object outlining for DTS objects in TGE as per this resource: Cel Shading Code

This is a screenshot of D3D rendering of the test level.

This is a screenshot of OpenGL rendering of the test level.

As you can see some lines/polys are being outlined when they shouldn't. Has anyone successfully used this resource, or have any idea of how to fix this? Code below.

Is anyone still interested in doing cel shading with TGE?

/*cell shading*/
   bool doOutline=Con::getBoolVariable("$Pref::renderOutline",true);
   
   if (doOutline) 
   {
		// new addition
		glEnable (GL_POLYGON_OFFSET_LINE);
		glPolygonOffset (2.5, 0.9); // arguments are factor and units - tweek theese
		glEnable (GL_LINE_SMOOTH);


      bool oldlighting=glIsEnabled(GL_LIGHTING);
      glDisable(GL_LIGHTING);
      F32 oldLineWidth;
      glGetFloatv(GL_LINE_WIDTH,&oldLineWidth);
      F32 lineWidth=Con::getFloatVariable("$Pref::OutlineWidth",2);
      glLineWidth(lineWidth);
      glCullFace(GL_FRONT);
      glPolygonMode (GL_BACK, GL_LINE);
      glDepthFunc(GL_LEQUAL);
      bool old2dtex=glIsEnabled(GL_TEXTURE_2D);
      glDisable(GL_TEXTURE_2D);
      F32 oldcolor[4];
      glGetFloatv(GL_CURRENT_COLOR,oldcolor);
      glColor4f(0.0f,0.0f,0.0f,1.0f); // Outline Color
      for (S32 i=0; i<primitives.size(); i++)
      {
         TSDrawPrimitive & draw = primitives[i];
         AssertFatal(draw.matIndex & TSDrawPrimitive::Indexed,
                  "TSMesh::render: rendering of non-indexed meshes no longer supported");

         S32 drawType = getDrawType(draw.matIndex>>30);
         glDrawElements(drawType,draw.numElements,GL_UNSIGNED_SHORT,&indices[draw.start]);
     }

      glColor4fv(oldcolor);
      if (old2dtex) glEnable(GL_TEXTURE_2D);
      glDepthFunc(GL_LEQUAL);
      glLineWidth(oldLineWidth);
      glColor3f(1.0f,1.0f,1.0f);
      glCullFace(GL_BACK);
      glPolygonMode (GL_BACK, GL_FILL);
      if (oldlighting) glEnable(GL_LIGHTING);

	  // End Addition
	  glDisable (GL_POLYGON_OFFSET_FILL);
	  glDisable (GL_LINE_SMOOTH);
   }

   /* end cell shading*/

#1
07/09/2007 (7:39 am)
I'm very interested in cel shading. Cel Shading is a serious performance hit on TGE according to the TDN article and anyway, you can control the effect more easily with a proper shader. I wonder if anyone has tried a proper hlsl cel shader on TGEA and seen what kind of a performance impact it has. Or using the modernization kit on TGE, using a proper shader for brushes and terrains + this for DTS shapes. If it is being used instead of a boatload of other shaders (bloom, frensel, normal maps, environment maps, etc.), then cel shading might actually be a performance gain over the photorealistic option.