Uses MV in Quad Command

This commit is contained in:
Ricardo Quesada 2013-12-05 19:04:01 -08:00
parent 5f902d95b3
commit e699a3b765
14 changed files with 240 additions and 52 deletions

View File

@ -1 +1 @@
b266b35a737f7500465360bf16cb73ce109762e2
9bfb5237a1dfb299a770a4f5a9b81d5b63e5a3a4

View File

@ -98,7 +98,7 @@ protected:
bool initWithTileFile(const std::string& tile, long tileWidth, long tileHeight, long itemsToRender);
/** initializes an AtlasNode with a texture the width and height of each item measured in points and the quantity of items to render*/
bool initWithTexture(Texture2D* texture, long tileWidth, long tileHeight, long itemsToRender);
virtual bool initWithTexture(Texture2D* texture, long tileWidth, long tileHeight, long itemsToRender);
void calculateMaxItems();
void updateBlendFunc();

View File

@ -45,7 +45,7 @@ THE SOFTWARE.
#include "platform/CCFileUtils.h"
#include "CCApplication.h"
#include "CCLabelBMFont.h"
#include "CCLabelAtlas.h"
#include "CCNewLabelAtlas.h"
#include "CCActionManager.h"
#include "CCAnimationCache.h"
#include "CCTouch.h"
@ -926,17 +926,17 @@ void Director::createStatsLabel()
*/
float factor = EGLView::getInstance()->getDesignResolutionSize().height / 320.0f;
_FPSLabel = new LabelAtlas();
_FPSLabel = new NewLabelAtlas;
_FPSLabel->setIgnoreContentScaleFactor(true);
_FPSLabel->initWithString("00.0", texture, 12, 32 , '.');
_FPSLabel->setScale(factor);
_SPFLabel = new LabelAtlas();
_SPFLabel = new NewLabelAtlas;
_SPFLabel->setIgnoreContentScaleFactor(true);
_SPFLabel->initWithString("0.000", texture, 12, 32, '.');
_SPFLabel->setScale(factor);
_drawsLabel = new LabelAtlas();
_drawsLabel = new NewLabelAtlas;
_drawsLabel->setIgnoreContentScaleFactor(true);
_drawsLabel->initWithString("000", texture, 12, 32, '.');
_drawsLabel->setScale(factor);

View File

@ -234,8 +234,9 @@ void GLProgram::updateUniforms()
_uniforms[UNIFORM_SAMPLER] = glGetUniformLocation(_program, UNIFORM_NAME_SAMPLER);
_flags.usesP = _uniforms[UNIFORM_P_MATRIX] != -1;
_flags.usesMV = _uniforms[UNIFORM_MV_MATRIX] != -1;
_flags.usesMVP = _uniforms[UNIFORM_MVP_MATRIX] != -1;
_flags.usesMV = (_uniforms[UNIFORM_MV_MATRIX] != -1 && _uniforms[UNIFORM_P_MATRIX] != -1 );
_flags.usesTime = (
_uniforms[UNIFORM_TIME] != -1 ||
_uniforms[UNIFORM_SIN_TIME] != -1 ||
@ -553,16 +554,18 @@ void GLProgram::setUniformsForBuiltins()
kmGLGetMatrix(KM_GL_MODELVIEW, &matrixMV);
if(_flags.usesP)
setUniformLocationWithMatrix4fv(_uniforms[UNIFORM_MVP_MATRIX], matrixP.mat, 1);
if(_flags.usesMV)
setUniformLocationWithMatrix4fv(_uniforms[UNIFORM_MV_MATRIX], matrixMV.mat, 1);
if(_flags.usesMVP) {
kmMat4 matrixMVP;
kmMat4Multiply(&matrixMVP, &matrixP, &matrixMV);
setUniformLocationWithMatrix4fv(_uniforms[UNIFORM_MVP_MATRIX], matrixMVP.mat, 1);
}
if(_flags.usesMV) {
setUniformLocationWithMatrix4fv(_uniforms[UNIFORM_P_MATRIX], matrixP.mat, 1);
setUniformLocationWithMatrix4fv(_uniforms[UNIFORM_MV_MATRIX], matrixMV.mat, 1);
}
if(_flags.usesTime) {
Director *director = Director::getInstance();

View File

@ -255,6 +255,7 @@ private:
unsigned int usesTime:1;
unsigned int usesMVP:1;
unsigned int usesMV:1;
unsigned int usesP:1;
unsigned int usesRandom:1;
// handy way to initialize the bitfield

View File

@ -62,7 +62,7 @@ bool LabelAtlas::initWithString(const std::string& string, const std::string& ch
bool LabelAtlas::initWithString(const std::string& string, Texture2D* texture, long itemWidth, long itemHeight, long startCharMap)
{
if (AtlasNode::initWithTexture(texture, itemWidth, itemHeight, string.size()))
if (initWithTexture(texture, itemWidth, itemHeight, string.size()))
{
_mapStartChar = startCharMap;
this->setString(string);
@ -169,7 +169,8 @@ void LabelAtlas::updateAtlasValues()
quads[i].tr.vertices.x = (float)(i * _itemWidth + _itemWidth);
quads[i].tr.vertices.y = (float)(_itemHeight);
quads[i].tr.vertices.z = 0.0f;
Color4B c(_displayedColor.r, _displayedColor.g, _displayedColor.b, _displayedOpacity);
// Color4B c(_displayedColor.r, _displayedColor.g, _displayedColor.b, _displayedOpacity);
Color4B c(255,255,255,255);
quads[i].tl.colors = c;
quads[i].tr.colors = c;
quads[i].bl.colors = c;

View File

@ -415,35 +415,38 @@ void ParticleSystemQuad::draw()
//quad command
if(_particleIdx > 0)
{
//transform vertices
std::vector<V3F_C4B_T2F_Quad> drawQuads(_particleIdx);
memcpy(&drawQuads[0], _quads, sizeof(V3F_C4B_T2F_Quad) * _particleIdx);
AffineTransform worldTM = getNodeToWorldTransform();
for(int index = 0; index <_particleIdx; ++index)
{
V3F_C4B_T2F_Quad* quad = _quads + index;
Point pt(0,0);
pt = PointApplyAffineTransform( Point(quad->bl.vertices.x, quad->bl.vertices.y), worldTM);
drawQuads[index].bl.vertices.x = pt.x;
drawQuads[index].bl.vertices.y = pt.y;
pt = PointApplyAffineTransform( Point(quad->br.vertices.x, quad->br.vertices.y), worldTM);
drawQuads[index].br.vertices.x = pt.x;
drawQuads[index].br.vertices.y = pt.y;
pt = PointApplyAffineTransform( Point(quad->tl.vertices.x, quad->tl.vertices.y), worldTM);
drawQuads[index].tl.vertices.x = pt.x;
drawQuads[index].tl.vertices.y = pt.y;
pt = PointApplyAffineTransform( Point(quad->tr.vertices.x, quad->tr.vertices.y), worldTM);
drawQuads[index].tr.vertices.x = pt.x;
drawQuads[index].tr.vertices.y = pt.y;
}
// //transform vertices
// std::vector<V3F_C4B_T2F_Quad> drawQuads(_particleIdx);
// memcpy(&drawQuads[0], _quads, sizeof(V3F_C4B_T2F_Quad) * _particleIdx);
// AffineTransform worldTM = getNodeToWorldTransform();
// for(int index = 0; index <_particleIdx; ++index)
// {
// V3F_C4B_T2F_Quad* quad = _quads + index;
//
// Point pt(0,0);
// pt = PointApplyAffineTransform( Point(quad->bl.vertices.x, quad->bl.vertices.y), worldTM);
// drawQuads[index].bl.vertices.x = pt.x;
// drawQuads[index].bl.vertices.y = pt.y;
//
// pt = PointApplyAffineTransform( Point(quad->br.vertices.x, quad->br.vertices.y), worldTM);
// drawQuads[index].br.vertices.x = pt.x;
// drawQuads[index].br.vertices.y = pt.y;
//
// pt = PointApplyAffineTransform( Point(quad->tl.vertices.x, quad->tl.vertices.y), worldTM);
// drawQuads[index].tl.vertices.x = pt.x;
// drawQuads[index].tl.vertices.y = pt.y;
//
// pt = PointApplyAffineTransform( Point(quad->tr.vertices.x, quad->tr.vertices.y), worldTM);
// drawQuads[index].tr.vertices.x = pt.x;
// drawQuads[index].tr.vertices.y = pt.y;
//
// }
kmMat4 mv;
kmGLGetMatrix(KM_GL_MODELVIEW, &mv);
QuadCommand* cmd = QuadCommand::getCommandPool().generateCommand();
cmd->init(0, _vertexZ, _texture->getName(), _shaderProgram, _blendFunc, &drawQuads[0], _particleIdx);
cmd->init(0, _vertexZ, _texture->getName(), _shaderProgram, _blendFunc, _quads, _particleIdx, mv);
Renderer::getInstance()->addCommand(cmd);
}

View File

@ -663,10 +663,14 @@ void Sprite::updateTransform(void)
void Sprite::draw(void)
{
updateQuadVertices();
// updateQuadVertices();
kmMat4 mv;
kmGLGetMatrix(KM_GL_MODELVIEW, &mv);
//TODO implement z order
QuadCommand* renderCommand = QuadCommand::getCommandPool().generateCommand();
renderCommand->init(0, _vertexZ, _texture->getName(), _shaderProgram, _blendFunc, &_quad, 1);
renderCommand->init(0, _vertexZ, _texture->getName(), _shaderProgram, _blendFunc, &_quad, 1, mv);
Renderer::getInstance()->addCommand(renderCommand);
}

View File

@ -0,0 +1,68 @@
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "CCNewLabelAtlas.h"
#include "RenderCommand.h"
#include "Renderer.h"
#include "QuadCommand.h"
#include "CCMenuItem.h"
#include "Frustum.h"
#include "CCDirector.h"
#include "CCTextureAtlas.h"
#include "CCShaderCache.h"
NS_CC_BEGIN
bool NewLabelAtlas::initWithTexture(Texture2D* texture, long tileWidth, long tileHeight, long itemsToRender)
{
LabelAtlas::initWithTexture(texture, tileWidth, tileHeight, itemsToRender);
// shader stuff
setShaderProgram(ShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
return true;
}
void NewLabelAtlas::draw()
{
// LabelAtlas::draw();
// _renderCommand.init(0, _vertexZ, _textureAtlas->getTexture()->getName(), _shaderProgram, _blendFunc,
// _textureAtlas->getQuads(), _textureAtlas->getTotalQuads() );
//
// Renderer::getInstance()->addCommand(&_renderCommand);
kmMat4 mv;
kmGLGetMatrix(KM_GL_MODELVIEW, &mv);
QuadCommand* cmd = QuadCommand::getCommandPool().generateCommand();
cmd->init(0, _vertexZ, _textureAtlas->getTexture()->getName(), _shaderProgram, _blendFunc, _textureAtlas->getQuads(), _textureAtlas->getTotalQuads(), mv);
Renderer::getInstance()->addCommand(cmd);
}
NS_CC_END

View File

@ -0,0 +1,52 @@
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __CCNEWLABELATLAS_H_
#define __CCNEWLABELATLAS_H_
#include "CCLabelAtlas.h"
#include "CCPlatformMacros.h"
#include "QuadCommand.h"
NS_CC_BEGIN
class NewLabelAtlas : public LabelAtlas
{
public:
NewLabelAtlas() {}
virtual ~NewLabelAtlas() {}
virtual bool initWithTexture(Texture2D* texture, long tileWidth, long tileHeight, long itemsToRender);
virtual void draw(void) override;
protected:
QuadCommand _renderCommand;
};
NS_CC_END
#endif /* defined(__CCNEWLABELATLAS_H_) */

View File

@ -108,15 +108,17 @@ void NewSprite::updateQuadVertices()
void NewSprite::draw(void)
{
updateQuadVertices();
// updateQuadVertices();
if(!culling())
{
return;
}
kmMat4 mv;
kmGLGetMatrix(KM_GL_MODELVIEW, &mv);
//TODO implement z order
QuadCommand* renderCommand = QuadCommand::getCommandPool().generateCommand();
renderCommand->init(0, _vertexZ, _texture->getName(), _shaderProgram, _blendFunc, &_quad, 1);
renderCommand->init(0, _vertexZ, _texture->getName(), _shaderProgram, _blendFunc, &_quad, 1, mv);
Renderer::getInstance()->addCommand(renderCommand);
}

View File

@ -60,11 +60,14 @@ void NewSpriteBatchNode::draw()
for(const auto &child: _children)
child->updateTransform();
// arrayMakeObjectsPerformSelector(_children, updateTransform, NewSprite*);
kmMat4 mv;
kmGLGetMatrix(KM_GL_MODELVIEW, &mv);
QuadCommand* cmd = QuadCommand::getCommandPool().generateCommand();
cmd->init(0, _vertexZ, _textureAtlas->getTexture()->getName(), _shaderProgram, _blendFunc, _textureAtlas->getQuads(), _textureAtlas->getTotalQuads());
cmd->init(0, _vertexZ, _textureAtlas->getTexture()->getName(), _shaderProgram, _blendFunc, _textureAtlas->getQuads(), _textureAtlas->getTotalQuads(), mv);
Renderer::getInstance()->addCommand(cmd);
}

View File

@ -16,13 +16,14 @@ QuadCommand::QuadCommand()
,_textureID(0)
,_blendType(BlendFunc::DISABLE)
,_quadCount(0)
,_capacity(0)
{
_type = QUAD_COMMAND;
_shader = nullptr;
_quad = nullptr;
}
void QuadCommand::init(int viewport, int32_t depth, GLuint textureID, GLProgram* shader, BlendFunc blendType, V3F_C4B_T2F_Quad* quad, int quadCount)
void QuadCommand::init(int viewport, int32_t depth, GLuint textureID, GLProgram* shader, BlendFunc blendType, V3F_C4B_T2F_Quad* quad, int quadCount, const kmMat4 &mv)
{
_viewport = viewport;
_depth = depth;
@ -30,9 +31,55 @@ void QuadCommand::init(int viewport, int32_t depth, GLuint textureID, GLProgram*
_blendType = blendType;
_quadCount = quadCount;
_shader = shader;
free(_quad);
_quad = (V3F_C4B_T2F_Quad*)malloc(sizeof(V3F_C4B_T2F_Quad) * quadCount);
if(quadCount > _capacity ) {
// _quad = (V3F_C4B_T2F_Quad*)malloc(sizeof(V3F_C4B_T2F_Quad) * quadCount);
_quad = (V3F_C4B_T2F_Quad*) realloc(_quad, sizeof(*quad) * quadCount );
_capacity = quadCount;
}
_quadCount = quadCount;
memcpy(_quad, quad, sizeof(V3F_C4B_T2F_Quad) * quadCount);
for(int i=0; i<quadCount; ++i) {
V3F_C4B_T2F_Quad *q = &_quad[i];
kmVec3 vec1, out1;
vec1.x = q->bl.vertices.x;
vec1.y = q->bl.vertices.y;
vec1.z = q->bl.vertices.z;
kmVec3Transform(&out1, &vec1, &mv);
q->bl.vertices.x = out1.x;
q->bl.vertices.y = out1.y;
q->bl.vertices.z = out1.z;
kmVec3 vec2, out2;
vec2.x = q->br.vertices.x;
vec2.y = q->br.vertices.y;
vec2.z = q->br.vertices.z;
kmVec3Transform(&out2, &vec2, &mv);
q->br.vertices.x = out2.x;
q->br.vertices.y = out2.y;
q->br.vertices.z = out2.z;
kmVec3 vec3, out3;
vec3.x = q->tr.vertices.x;
vec3.y = q->tr.vertices.y;
vec3.z = q->tr.vertices.z;
kmVec3Transform(&out3, &vec3, &mv);
q->tr.vertices.x = out3.x;
q->tr.vertices.y = out3.y;
q->tr.vertices.z = out3.z;
kmVec3 vec4, out4;
vec4.x = q->tl.vertices.x;
vec4.y = q->tl.vertices.y;
vec4.z = q->tl.vertices.z;
kmVec3Transform(&out4, &vec4, &mv);
q->tl.vertices.x = out4.x;
q->tl.vertices.y = out4.y;
q->tl.vertices.z = out4.z;
}
}
QuadCommand::~QuadCommand()

View File

@ -10,6 +10,7 @@
#include "RenderCommand.h"
#include "CCGLProgram.h"
#include "RenderCommandPool.h"
#include "kazmath.h"
NS_CC_BEGIN
@ -17,12 +18,13 @@ NS_CC_BEGIN
class QuadCommand : public RenderCommand
{
protected:
public:
QuadCommand();
~QuadCommand();
public:
void init(int viewport, int32_t depth, GLuint texutreID, GLProgram* shader, BlendFunc blendType, V3F_C4B_T2F_Quad* quad, int quadCount);
void init(int viewport, int32_t depth, GLuint texutreID, GLProgram* shader, BlendFunc blendType, V3F_C4B_T2F_Quad* quad, int quadCount,
const kmMat4& mv);
// +----------+----------+-----+-----------------------------------+
// | | | | | |
@ -69,6 +71,8 @@ protected:
V3F_C4B_T2F_Quad* _quad;
int _quadCount;
int _capacity;
public:
friend class RenderCommandPool<QuadCommand>;
static RenderCommandPool<QuadCommand>& getCommandPool() { return _commandPool; }