00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "LampBasic.h"
00026 #include "Graphics/PrimitiveRenderer/PrimitiveRenderer.h"
00027 #include "Graphics/PrimitiveRenderer/PrimitiveDrawRequestBuilder.h"
00028 #include "Graphics/System/LampGraphics.h"
00029 #include "Graphics/Renderer/RenderingDevice.h"
00030 #include "Graphics/Camera/Camera.h"
00031 #include "Graphics/Material/Material.h"
00032
00033 namespace Lamp{
00034
00035
00036
00037
00038
00039 PrimitiveRenderer::PrimitiveRenderer() :
00040 positionDeclaration_(NULL), positionColorDeclaration_(NULL){
00041 LampGraphics::addDeviceObjectHolder(this);
00042
00043 PrimitiveDrawRequestBuilder::buildPoint(&point_);
00044 PrimitiveDrawRequestBuilder::buildAxisPoint(&axisAxisPoint_);
00045 PrimitiveDrawRequestBuilder::buildAxis(&axis_);
00046 PrimitiveDrawRequestBuilder::buildArrow(&arrow_);
00047 PrimitiveDrawRequestBuilder::buildGrid(&grid_, 10, 10);
00048 PrimitiveDrawRequestBuilder::buildPlane(&plane_);
00049 PrimitiveDrawRequestBuilder::buildSphere(&sphere_, 1.f, 12, 8);
00050 PrimitiveDrawRequestBuilder::buildBox(&box_, 1.f, 1.f, 1.f);
00051 PrimitiveDrawRequestBuilder::buildCylinder(&cylinder_, 1.f, 1.f, 12);
00052 PrimitiveDrawRequestBuilder::buildCone(&cone_, 1.f, 1.f, 12);
00053 }
00054
00055
00056 PrimitiveRenderer::~PrimitiveRenderer(){
00057 LampGraphics::removeDeviceObjectHolder(this);
00058
00059 invalidateGraphicsDeviceObjects();
00060 }
00061
00062
00063
00064
00065 void PrimitiveRenderer::render(const Matrix44& viewMatrix,
00066 const Matrix44& projectionMatrix){
00067 if(requests_.getCount() == 0){ return; }
00068
00069
00070 RenderingDevice* device = RenderingDevice::getInstance();
00071
00072 device->applyDefaultStateBlock();
00073
00074 device->setViewMatrix(viewMatrix);
00075
00076 device->setProjectionMatrix(projectionMatrix);
00077
00078 device->setRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
00079
00080
00081 device->setRenderState(D3DRS_LIGHTING, false);
00082
00083 device->setMaterial(Color3f::white, Color3f::black, Color3f::black,
00084 Color3f::black, 0.f, 1.f);
00085
00086 device->setTextureState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
00087 device->setTextureState(0, D3DTSS_COLORARG1, D3DTA_DIFFUSE);
00088 device->setTextureState(0, D3DTSS_COLORARG2, D3DTA_TFACTOR);
00089
00090 device->setTextureState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
00091 device->setTextureState(0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE);
00092 device->setTextureState(0, D3DTSS_ALPHAARG2, D3DTA_TFACTOR);
00093
00094 device->setBlending(true);
00095 device->setBlendMode(Material::blendModeAdd,
00096 Material::blendStateSourceAlpha,
00097 Material::blendStateInverseSourceAlpha);
00098
00099
00100
00101 if(device->beginScene()){
00102 Request request;
00103 int requestCount = requests_.getCount();
00104 for(int i = 0; i < requestCount; i++){
00105 renderRequest(requests_[i]);
00106
00107 requests_.set(i, request);
00108 }
00109 device->endScene();
00110 }
00111
00112
00113
00114
00115 device->applyDefaultStateBlock();
00116
00117 requests_.clear();
00118 }
00119
00120
00121 void PrimitiveRenderer::render(Camera* camera){
00122 render(camera->getViewMatrix(), camera->getProjectionMatrix());
00123 }
00124
00125
00126 void PrimitiveRenderer::renderRequest(Request& request){
00127 RenderingDevice* device = RenderingDevice::getInstance();
00128
00129 device->setWorldMatrix(request.matrix_);
00130
00131 device->setRenderState(D3DRS_TEXTUREFACTOR, request.color_.getARGB());
00132
00133 device->setZTest(request.zTest_);
00134
00135
00136 PrimitiveDrawRequest& primitive = request.primitive_;
00137
00138 int vertexSize = primitive.getVertexSize();
00139 if(!primitive.hasColor()){
00140 setPositionVertexDeclaration();
00141 }else{
00142 setPositionColorVertexDeclaration();
00143 }
00144
00145 device->setVertexBuffer(primitive.getVertexBuffer(), vertexSize);
00146
00147
00148 bool hasIndices = primitive.hasVertexIndices();
00149 int vertexCount = primitive.getVertexCount();
00150 if(hasIndices){
00151 device->setIndexBuffer(primitive.getIndexBuffer());
00152 int indexCount = primitive.getVertexIndexCount();
00153 Assert((indexCount % 2) == 0);
00154 device->drawIndexedLineList(vertexCount, indexCount / 2);
00155 }else{
00156 Assert((vertexCount % 2) == 0);
00157 device->drawLineList(vertexCount / 2);
00158 }
00159 }
00160
00161
00162
00163
00164 void PrimitiveRenderer::request(const PrimitiveDrawRequest& primitive,
00165 const Matrix34& matrix, Color4c color, bool zTest){
00166 Request request;
00167 request.primitive_ = primitive;
00168 request.matrix_ = matrix;
00169 request.color_ = color;
00170 request.zTest_ = zTest;
00171 requests_.add(request);
00172 }
00173
00174
00175 void PrimitiveRenderer::requestLine(int vertexCount, Vector3* positions,
00176 const Matrix34& matrix, Color4c color, bool zTest){
00177 Assert((vertexCount > 1) && ((vertexCount % 2) == 0));
00178 Assert(positions != NULL);
00179 PrimitiveDrawRequest primitive;
00180 primitive.setVertexCount(vertexCount);
00181 for(int i = 0; i < vertexCount; i++){
00182 primitive.setPosition(i, positions[i]);
00183 }
00184 request(primitive, matrix, color, zTest);
00185 }
00186
00187
00188 void PrimitiveRenderer::requestLine(int vertexCount, Vector3* positions,
00189 Color4c* colors, const Matrix34& matrix, Color4c color, bool zTest){
00190 Assert((vertexCount > 1) && ((vertexCount % 2) == 0));
00191 Assert(positions != NULL);
00192 Assert(colors != NULL);
00193 PrimitiveDrawRequest primitive;
00194 primitive.setVertexCount(vertexCount);
00195 primitive.enableColor(true);
00196 for(int i = 0; i < vertexCount; i++){
00197 primitive.setPosition(i, positions[i]);
00198 primitive.setColor(i, colors[i]);
00199 }
00200 request(primitive, matrix, color, zTest);
00201 }
00202
00203
00204
00205
00206 void PrimitiveRenderer::invalidateGraphicsDeviceObjects(){
00207
00208 SafeRelease(positionDeclaration_);
00209 SafeRelease(positionColorDeclaration_);
00210 }
00211
00212
00213
00214
00215 void PrimitiveRenderer::setPositionVertexDeclaration(){
00216 RenderingDevice* device = RenderingDevice::getInstance();
00217 if(positionDeclaration_ == NULL){
00218 device->createVertexDeclaration(&positionDeclaration_,
00219 true, 0, 0, false, false, 0, NULL);
00220 }
00221 device->setVertexDeclaration(positionDeclaration_);
00222 }
00223
00224
00225 void PrimitiveRenderer::setPositionColorVertexDeclaration(){
00226 RenderingDevice* device = RenderingDevice::getInstance();
00227 if(positionColorDeclaration_ == NULL){
00228 device->createVertexDeclaration(&positionColorDeclaration_,
00229 true, 0, 0, false, true, 0, NULL);
00230 }
00231 device->setVertexDeclaration(positionColorDeclaration_);
00232 }
00233
00234 }
00235