duDebugDrawTorque.cpp
Engine/source/navigation/duDebugDrawTorque.cpp
Detailed Description
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2013 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#include "torqueRecast.h" 25#include "duDebugDrawTorque.h" 26 27#include "gfx/gfxDevice.h" 28#include "gfx/primBuilder.h" 29#include "gfx/gfxStateBlock.h" 30 31/// @class duDebugDrawTorque 32/// This class uses the primitive builder (gfx/primBuild.h) to render navmeshes 33/// and other Recast data. To facilitate the primbuilder's requirement to know 34/// the number of vertices to render beforehand, this class stores all vertices 35/// in a buffer of its own, then passes on that known-size buffer. 36/// This means that you only need to call the duDebugDraw functions when your 37/// data changes. At other times, you can cache the duDebugDrawTorque object 38/// and call its render() method, which actually renders its buffered data. 39 40duDebugDrawTorque::duDebugDrawTorque() 41{ 42 mPrimType = 0; 43 mQuadsMode = false; 44 mVertCount = 0; 45 mGroup = 0; 46 mCurrColor = 0; 47 mOverrideColor = 0; 48 mOverride = false; 49 dMemset(&mStore, 0, sizeof(mStore)); 50} 51 52duDebugDrawTorque::~duDebugDrawTorque() 53{ 54 clear(); 55} 56 57void duDebugDrawTorque::depthMask(bool state) 58{ 59 mDesc.setZReadWrite(state, state); 60} 61 62void duDebugDrawTorque::texture(bool state) 63{ 64} 65 66/// Begin drawing primitives. 67/// @param prim [in] primitive type to draw, one of rcDebugDrawPrimitives. 68/// @param size [in] size of a primitive, applies to point size and line width only. 69void duDebugDrawTorque::begin(duDebugDrawPrimitives prim, float size) 70{ 71 mCurrColor = -1; 72 mQuadsMode = false; 73 mVertCount = 0; 74 mPrimType = 0; 75 switch(prim) 76 { 77 case DU_DRAW_POINTS: mPrimType = GFXPointList; break; 78 case DU_DRAW_LINES: mPrimType = GFXLineList; break; 79 case DU_DRAW_TRIS: mPrimType = GFXTriangleList; break; 80 case DU_DRAW_QUADS: mPrimType = GFXTriangleList; 81 mQuadsMode = true; break; 82 } 83 mBuffers.push_back(Buffer(mPrimType)); 84 mBuffers.last().group = mGroup; 85 mDesc.setCullMode(GFXCullNone); 86 mDesc.setBlend(true); 87} 88 89void duDebugDrawTorque::beginGroup(U32 group) 90{ 91 mGroup = group; 92} 93 94/// Submit a vertex 95/// @param pos [in] position of the verts. 96/// @param color [in] color of the verts. 97void duDebugDrawTorque::vertex(const float* pos, unsigned int color) 98{ 99 vertex(pos[0], pos[1], pos[2], color); 100} 101 102/// Submit a vertex 103/// @param x,y,z [in] position of the verts. 104/// @param color [in] color of the verts. 105void duDebugDrawTorque::vertex(const float x, const float y, const float z, unsigned int color) 106{ 107 if(mQuadsMode) 108 { 109 if(mVertCount == 3) 110 { 111 _vertex(x, -z, y, color); 112 _vertex(mStore[0][0], mStore[0][1], mStore[0][2], color); 113 _vertex(mStore[1][0], mStore[1][1], mStore[1][2], color); 114 _vertex(mStore[1][0], mStore[1][1], mStore[1][2], color); 115 _vertex(mStore[2][0], mStore[2][1], mStore[2][2], color); 116 _vertex(x, -z, y, color); 117 mVertCount = 0; 118 } 119 else 120 { 121 mStore[mVertCount][0] = x; 122 mStore[mVertCount][1] = -z; 123 mStore[mVertCount][2] = y; 124 mVertCount++; 125 } 126 } 127 else 128 { 129 _vertex(x, -z, y, color); 130 } 131} 132 133/// Submit a vertex 134/// @param pos [in] position of the verts. 135/// @param color [in] color of the verts. 136void duDebugDrawTorque::vertex(const float* pos, unsigned int color, const float* uv) 137{ 138 vertex(pos[0], pos[1], pos[2], color); 139} 140 141/// Submit a vertex 142/// @param x,y,z [in] position of the verts. 143/// @param color [in] color of the verts. 144void duDebugDrawTorque::vertex(const float x, const float y, const float z, unsigned int color, const float u, const float v) 145{ 146 vertex(x, y, z, color); 147} 148 149/// Push a vertex onto the buffer. 150void duDebugDrawTorque::_vertex(const float x, const float y, const float z, unsigned int color) 151{ 152 // Use override color if we must. 153 //if(mOverride) 154 //color = mOverrideColor; 155 if(mCurrColor != color || !mBuffers.last().buffer.size()) 156 { 157 U8 r, g, b, a; 158 // Convert color integer to components. 159 rcCol(color, r, g, b, a); 160 mBuffers.last().buffer.push_back(Instruction(r, g, b, a)); 161 mCurrColor = color; 162 } 163 // Construct vertex data. 164 mBuffers.last().buffer.push_back(Instruction(x, y, z)); 165} 166 167/// End drawing primitives. 168void duDebugDrawTorque::end() 169{ 170} 171 172void duDebugDrawTorque::overrideColor(unsigned int col) 173{ 174 mOverride = true; 175 mOverrideColor = col; 176} 177 178void duDebugDrawTorque::cancelOverride() 179{ 180 mOverride = false; 181} 182 183void duDebugDrawTorque::renderBuffer(Buffer &b) 184{ 185 PrimBuild::begin(b.primType, b.buffer.size()); 186 Vector<Instruction> &buf = b.buffer; 187 for(U32 i = 0; i < buf.size(); i++) 188 { 189 switch(buf[i].type) 190 { 191 case Instruction::POINT: 192 PrimBuild::vertex3f(buf[i].data.point.x, 193 buf[i].data.point.y, 194 buf[i].data.point.z); 195 break; 196 197 case Instruction::COLOR: 198 if(mOverride) 199 break; 200 PrimBuild::color4i(buf[i].data.color.r, 201 buf[i].data.color.g, 202 buf[i].data.color.b, 203 buf[i].data.color.a); 204 break; 205 } 206 } 207 PrimBuild::end(); 208} 209 210void duDebugDrawTorque::render() 211{ 212 GFXStateBlockRef sb = GFX->createStateBlock(mDesc); 213 GFX->setStateBlock(sb); 214 // Use override color for all rendering. 215 if(mOverride) 216 { 217 U8 r, g, b, a; 218 rcCol(mOverrideColor, r, g, b, a); 219 PrimBuild::color4i(r, g, b, a); 220 } 221 for(U32 b = 0; b < mBuffers.size(); b++) 222 { 223 renderBuffer(mBuffers[b]); 224 } 225} 226 227void duDebugDrawTorque::renderGroup(U32 group) 228{ 229 GFXStateBlockRef sb = GFX->createStateBlock(mDesc); 230 GFX->setStateBlock(sb); 231 // Use override color for all rendering. 232 if(mOverride) 233 { 234 U8 r, g, b, a; 235 rcCol(mOverrideColor, r, g, b, a); 236 PrimBuild::color4i(r, g, b, a); 237 } 238 for(U32 b = 0; b < mBuffers.size(); b++) 239 { 240 if(mBuffers[b].group == group) 241 renderBuffer(mBuffers[b]); 242 } 243} 244 245void duDebugDrawTorque::clear() 246{ 247 for(U32 b = 0; b < mBuffers.size(); b++) 248 mBuffers[b].buffer.clear(); 249 mBuffers.clear(); 250} 251