Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

# PrimitiveRenderer.cpp

Go to the documentation of this file.

    00001 //------------------------------------------------------------------------------
    00002 // Lamp : Open source game middleware
    00003 // Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
    00004 //
    00005 // This library is free software; you can redistribute it and/or
    00006 // modify it under the terms of the GNU Lesser General Public
    00007 // License as published by the Free Software Foundation; either
    00008 // version 2.1 of the License, or (at your option) any later version.
    00009 //
    00010 // This library is distributed in the hope that it will be useful,
    00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013 // Lesser General Public License for more details.
    00014 //
    00015 // You should have received a copy of the GNU Lesser General Public
    00016 // License along with this library; if not, write to the Free Software
    00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    00018 //------------------------------------------------------------------------------
    00019
    00020 /** @file
    00021  * プリミティブレンダラ実装
    00022  * @author Junpee
    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     // カラーステージの設定（Diffuse * TextureFactor）
    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     // アルファステージの設定（Diffuse * TextureFactor）
    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     // Zテストの設定
    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 } // End of namespace Lamp
    00235 //------------------------------------------------------------------------------