Skip to content

Commit

Permalink
Added support for wireframe-only and wireframe-overlay drawing modes …
Browse files Browse the repository at this point in the history
…to both SpinningLights and Viewer.
  • Loading branch information
dnadlinger committed Feb 12, 2010
1 parent 653d270 commit 474777f
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 16 deletions.
43 changes: 42 additions & 1 deletion src/SpinningLights.d
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ import d4.scene.Node;
import d4.scene.Primitives;
import d4.scene.FixedMaterialRenderVisitor;
import d4.scene.Vertex;
import d4.scene.WireframeMaterial;
import d4.shader.SingleColorShader;
import d4.util.ArrayUtils;
import d4.util.FreeCameraApplication;
import d4.util.Key;
import util.EntryPoint;
import util.EnumUtils;

/**
* A shader which renders the objects all white illuminated with two colored
Expand Down Expand Up @@ -148,6 +152,14 @@ private:
Rasterizer m_rasterizer;
}

/**
* The available wireframe drawing modes.
*/
enum WireframeMode {
OFF, /// Use the normal solid materials.
ONLY, /// Only render the wireframes.
OVERLAY /// Render the wireframes above the solid image.
}

class SpinningLights : FreeCameraApplication {
public:
Expand Down Expand Up @@ -184,6 +196,9 @@ protected:

cameraPosition = Vector3( 0, 3, 5 );
m_material = new Material();

m_wireframeMode = WireframeMode.OFF;
m_wireframeMaterial = new WireframeMaterial();
}

override void render( float deltaTime ) {
Expand All @@ -192,14 +207,37 @@ protected:
m_material.updatePositions( deltaTime );

renderer().beginScene();
m_rootNode.accept( new FixedMaterialRenderVisitor( renderer(), m_material ) );

if ( m_wireframeMode != WireframeMode.ONLY ) {
m_rootNode.accept(
new FixedMaterialRenderVisitor( renderer(), m_material ) );
}

if ( m_wireframeMode != WireframeMode.OFF ) {
m_rootNode.accept(
new FixedMaterialRenderVisitor( renderer(), m_wireframeMaterial ) );
}

renderer().endScene();
}

override void shutdown() {
super.shutdown();
}

override void handleKeyUp( Key key ) {
super.handleKeyUp( key );

switch ( key ) {
case Key.x:
m_wireframeMode = step( m_wireframeMode, 1 );
break;
default:
// Do nothing.
break;
}
}

override void handleSwitchArgument( char[] name ) {
switch ( name ) {
case "display-room":
Expand All @@ -226,6 +264,9 @@ private:
bool m_displayRoom;
Node m_rootNode;
Material m_material;

WireframeMode m_wireframeMode;
IMaterial m_wireframeMaterial;
}

mixin EntryPoint!( SpinningLights );
46 changes: 31 additions & 15 deletions src/Viewer.d
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ import d4.scene.FixedMaterialRenderVisitor;
import d4.scene.GenericBasicRasterizerFactory;
import d4.scene.RenderVisitor;
import d4.scene.Vertex;
import d4.scene.WireframeMaterial;
import d4.shader.LitSingleColorShader;
import d4.shader.SingleColorShader;
import d4.util.FreeCameraApplication;
import d4.util.Key;
import util.EntryPoint;
import util.EnumUtils;

/**
* The available shading modes.
Expand All @@ -44,8 +46,18 @@ enum ShadingMode {
GOURAUD_TEXTURED
}

/**
* The available wireframe drawing modes.
*/
enum WireframeMode {
OFF, /// Use the normal solid materials.
ONLY, /// Only render the wireframes.
OVERLAY /// Render the wireframes above the solid image.
}

/**
* The main application class.
*
* Manages the scene, reacts to user input, etc.
*/
class Viewer : FreeCameraApplication {
Expand All @@ -70,12 +82,12 @@ protected:
m_backgroundTime = 0;
renderer().clearColor = Color( 0, 0, 0 );

// Enable everything by default.
// Use non-wireframe mode with both gouraud shading and texturing enabled
// by default.
m_shadingMode = ShadingMode.GOURAUD_TEXTURED;
m_forceWireframe = false;
m_wireframeMode = WireframeMode.OFF;

m_wireframeMaterial = new BasicMaterial( m_rasterizerFactory );
m_wireframeMaterial.wireframe = true;
m_wireframeMaterial = new WireframeMaterial();

m_flatMaterial = new BasicMaterial( m_rasterizerFactory );
m_flatMaterial.gouraudShading = false;
Expand All @@ -96,12 +108,10 @@ protected:
updateRotatingWorld( deltaTime );
}

ISceneVisitor renderVisitor;
renderer().beginScene();

if ( m_forceWireframe ) {
renderVisitor = new FixedMaterialRenderVisitor(
renderer(), m_wireframeMaterial );
} else {
if ( m_wireframeMode != WireframeMode.ONLY ) {
ISceneVisitor renderVisitor;
switch ( m_shadingMode ) {
case ShadingMode.FLAT:
renderVisitor = new FixedMaterialRenderVisitor(
Expand All @@ -114,11 +124,17 @@ protected:
case ShadingMode.GOURAUD_TEXTURED:
renderVisitor = new RenderVisitor( renderer() );
break;
default:
throw new Exception( "Invalid shading mode!" );
}
m_scene.rootNode.accept( renderVisitor );
}

if ( m_wireframeMode != WireframeMode.OFF ) {
m_scene.rootNode.accept(
new FixedMaterialRenderVisitor( renderer(), m_wireframeMaterial ) );
}

renderer().beginScene();
m_scene.rootNode.accept( renderVisitor );
renderer().endScene();
}

Expand All @@ -132,10 +148,10 @@ protected:
switch ( key ) {
case Key.y:
case Key.z:
m_shadingMode = cast( ShadingMode )( ( m_shadingMode + 1 ) % ( m_shadingMode.max + 1 ) );
m_shadingMode = step( m_shadingMode, 1 );
break;
case Key.x:
m_forceWireframe = !m_forceWireframe;
m_wireframeMode = step( m_wireframeMode, 1 );
break;
case Key.v:
m_rotateWorld = !m_rotateWorld;
Expand Down Expand Up @@ -198,10 +214,10 @@ private:
Scene m_scene;

ShadingMode m_shadingMode;
bool m_forceWireframe;
WireframeMode m_wireframeMode;

IBasicRasterizerFactory m_rasterizerFactory;
BasicMaterial m_wireframeMaterial;
IMaterial m_wireframeMaterial;
BasicMaterial m_flatMaterial;
BasicMaterial m_gouraudMaterial;

Expand Down
24 changes: 24 additions & 0 deletions src/d4/scene/WireframeMaterial.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module d4.scene.WireframeMaterial;

import d4.renderer.IMaterial;
import d4.renderer.IRasterizer;
import d4.renderer.Renderer;
import d4.renderer.WireframeRasterizer;
import d4.shader.SingleColorShader;

/**
* A simple material for rendering a white unlit wireframe model.
*/
class WireframeMaterial : IMaterial {
IRasterizer getRasterizer() {
return new WireframeRasterizer!( SingleColorShader )();
}

void prepareForRendering( Renderer renderer ) {
// Nothing to do – we just need our rasterizer activated.
}

bool usesTextures() {
return false;
}
}
18 changes: 18 additions & 0 deletions src/util/EnumUtils.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module util.EnumUtils;

/**
* Advances the passed enumeration value by the given number of steps, wrapping
* around if the limit has been reached.
*
* Of course, this requires the enumeration items to be numbered continuously.
*
* Params:
* value = The start value.
* offset = The number of steps to advance the value. Negative values also
* work like expected.
* Returns:
* The advanced value.
*/
T step( T )( T value, int offset ) {
return cast( T )( ( value + offset ) % ( value.max + 1 ) );
}

0 comments on commit 474777f

Please sign in to comment.