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

# SpriteRenderer.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 "Graphics2D/Renderer/SpriteRenderer.h"
    00027 #include "Graphics2D/Renderer/SpriteGraphicsBuffer.h"
    00028 #include "Graphics2D/Renderer/SpriteRenderState.h"
    00029 #include "Graphics2D/Renderer/SpriteRequest.h"
    00030 #include "Graphics/Renderer/RenderingDevice.h"
    00031
    00032 #include "Graphics2D/SpriteState/ColorSpriteState.h"
    00033 #include "Graphics2D/SpriteState/AddressModeSpriteState.h"
    00034 #include "Graphics2D/SpriteState/BlendSpriteState.h"
    00035 #include "Graphics2D/SpriteState/ViewportSpriteState.h"
    00036
    00037 namespace Lamp{
    00038
    00039 //------------------------------------------------------------------------------
    00040 // 生成、破棄
    00041 //------------------------------------------------------------------------------
    00042 // コンストラクタ
    00043 SpriteRenderer::SpriteRenderer(int maxPriority, int defaultPriority) :
    00044     maxPriority_(maxPriority){
    00045     Assert(maxPriority_ > 0);
    00046     setDefaultPriority(defaultPriority);
    00047     requests_ = new ArrayList<SpriteRequest*>[maxPriority_];
    00048     graphicsBuffer_ = new SpriteGraphicsBuffer();
    00049 }
    00050 //------------------------------------------------------------------------------
    00051 // デストラクタ
    00052 SpriteRenderer::~SpriteRenderer(){
    00053     SafeDelete(graphicsBuffer_);
    00054     SafeArrayDelete(requests_);
    00055 }
    00056 //------------------------------------------------------------------------------
    00057 // リクエスト
    00058 //------------------------------------------------------------------------------
    00059 // リクエスト
    00060 void SpriteRenderer::request(SpriteRequest* spriteRequest, int priority){
    00061     Assert((priority >= 0) && (priority < maxPriority_));
    00062     requests_[priority].add(spriteRequest);
    00063 }
    00064 //------------------------------------------------------------------------------
    00065 // デフォルトステートのリクエスト
    00066 void SpriteRenderer::requestDefaultState(int priority){
    00067     request(&ColorSpriteState::defaultState, priority);
    00068     request(&AddressModeSpriteState::defaultState, priority);
    00069     request(&BlendSpriteState::defaultState, priority);
    00070     request(&ViewportSpriteState::defaultState, priority);
    00071 }
    00072 //------------------------------------------------------------------------------
    00073 // デフォルトステートの適用
    00074 void SpriteRenderer::applyDefaultState(SpriteRenderState* renderState){
    00075     ((ColorSpriteState*)(&ColorSpriteState::defaultState))->
    00076         render(renderState);
    00077     ((AddressModeSpriteState*)(&AddressModeSpriteState::defaultState))->
    00078         render(renderState);
    00079     ((BlendSpriteState*)(&BlendSpriteState::defaultState))->
    00080         render(renderState);
    00081     ((ViewportSpriteState*)(&ViewportSpriteState::defaultState))->
    00082         render(renderState);
    00083 }
    00084 //------------------------------------------------------------------------------
    00085 // レンダリング
    00086 //------------------------------------------------------------------------------
    00087 // レンダリング
    00088 void SpriteRenderer::render(int startPriority, int endPriority){
    00089     Assert(startPriority <= endPriority);
    00090     Assert((startPriority >= 0) && (endPriority < maxPriority_));
    00091     RenderingDevice* device = RenderingDevice::getInstance();
    00092     SpriteRenderState renderState(graphicsBuffer_);
    00093
    00094     // デフォルトステートブロックの適用
    00095     device->applyDefaultStateBlock();
    00096
    00097     // グラフィックスバッファのセットアップ
    00098     graphicsBuffer_->setup();
    00099
    00100     // レンダラステート適用
    00101     applyRendererState();
    00102
    00103     // スプライトデフォルトステートの適用
    00104     applyDefaultState(&renderState);
    00105
    00106     // 描画開始
    00107     if(device->beginScene()){
    00108         // endPriorityを含むループ
    00109         for(int i = startPriority; i <= endPriority; i++){
    00110             // レンダリング
    00111             int count = requests_[i].getCount();
    00112             for(int j = 0; j < count; j++){
    00113                 SpriteRequest* request = requests_[i].get(j);
    00114                 request->render(&renderState);
    00115             }
    00116             // リクエストをクリアする
    00117             requests_[i].clear();
    00118         }
    00119         // 描画後始末
    00120         graphicsBuffer_->render();
    00121         device->endScene();
    00122     }
    00123
    00124     // デフォルトステートブロックの適用
    00125     device->applyDefaultStateBlock();
    00126 }
    00127 //------------------------------------------------------------------------------
    00128 // レンダラステートの適用
    00129 void SpriteRenderer::applyRendererState(){
    00130     // レンダリング中に不変のステートを設定する
    00131     RenderingDevice* device = RenderingDevice::getInstance();
    00132
    00133     //--------------------------------------------------------------------------
    00134     // 静的設定
    00135     //--------------------------------------------------------------------------
    00136     // ライティング無し
    00137     device->setRenderState(D3DRS_LIGHTING, false);
    00138     // カリング無し
    00139     device->setRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
    00140     // Z書き込み無し
    00141     device->setRenderState(D3DRS_ZWRITEENABLE, false);
    00142     // Zテスト無し
    00143     device->setZTest(false);
    00144
    00145     // カラーステージの設定
    00146     device->setTextureState(0, D3DTSS_COLOROP, D3DTOP_MODULATE4X);
    00147     device->setTextureState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
    00148     device->setTextureState(0, D3DTSS_COLORARG2, D3DTA_TEXTURE);
    00149     // アルファステージの設定
    00150     device->setTextureState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
    00151     device->setTextureState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
    00152     device->setTextureState(0, D3DTSS_ALPHAARG2, D3DTA_TEXTURE);
    00153
    00154     // テクスチャサンプラにミップマップと線形補間を設定する
    00155     device->setSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
    00156     device->setSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
    00157     device->setSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
    00158
    00159     // アルファテストの設定
    00160     device->setRenderState(D3DRS_ALPHATESTENABLE, true);
    00161     device->setRenderState(D3DRS_ALPHAREF, 1);
    00162     device->setRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL);
    00163
    00164     //--------------------------------------------------------------------------
    00165     // 動的設定
    00166     //--------------------------------------------------------------------------
    00167     // レンダーターゲットサイズを取得する
    00168     DimensionF renderTargetSize;
    00169     renderTargetSize.set(device->getRenderTargetSize());
    00170
    00171     // 射影行列
    00172     Matrix44 projectionMatrix;
    00173     float nearZ = 1.f;
    00174     float farZ = 10001.f;
    00175     projectionMatrix.set(
    00176         2.f / renderTargetSize.width, 0.f, 0.f, 0.f,
    00177         0.f, 2.f / renderTargetSize.height, 0.f, 0.f,
    00178         0.f, 0.f, 1.f / (nearZ - farZ), nearZ / (nearZ - farZ),
    00179         0.f, 0.f, 0.f, 1.f);
    00180     device->setProjectionMatrix(projectionMatrix);
    00181
    00182     // ビュー行列
    00183     Matrix44 viewMatrix;
    00184     Vector3 viewPosition(renderTargetSize.width * 0.5f,
    00185         renderTargetSize.height * 0.5f, (farZ - nearZ) * 0.5f + nearZ);
    00186     viewMatrix.setTranslation(-viewPosition);
    00187     viewMatrix.addScale(Vector3(1.f, -1.f, 1.f));
    00188     device->setViewMatrix(viewMatrix);
    00189
    00190 }
    00191 //------------------------------------------------------------------------------
    00192 } // End of namespace Lamp
    00193 //------------------------------------------------------------------------------