Skip to content

Commit 9c0033e

Browse files
Mee-guminggo
authored andcommitted
Feature/draw in rect (cocos2d#19664)
* Texture2D draw in point and draw in rectangle tests work * optimize code * fix compile error * make initProgam() private
1 parent 65fda3d commit 9c0033e

File tree

4 files changed

+100
-84
lines changed

4 files changed

+100
-84
lines changed

cocos/renderer/CCTexture2D.cpp

+82-60
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ THE SOFTWARE.
4545
#include "base/CCNinePatchImageParser.h"
4646
#include "renderer/backend/Device.h"
4747
#include "renderer/backend/StringUtils.h"
48+
#include "renderer/backend/ProgramState.h"
49+
#include "renderer/ccShaders.h"
4850
#include "renderer/CCTextureUtils.h"
51+
#include "renderer/CCRenderer.h"
4952

5053
#if CC_ENABLE_CACHE_TEXTURE_DATA
5154
#include "renderer/CCTextureCache.h"
@@ -223,6 +226,7 @@ Texture2D::~Texture2D()
223226
CC_SAFE_DELETE(_ninePatchInfo);
224227

225228
CC_SAFE_RELEASE(_texture);
229+
CC_SAFE_RELEASE(_programState);
226230
}
227231

228232
Texture2D::PixelFormat Texture2D::getPixelFormat() const
@@ -637,66 +641,6 @@ bool Texture2D::initWithBackendTexture(backend::Texture *texture)
637641
return true;
638642
}
639643

640-
641-
//// implementation Texture2D (Drawing)
642-
//
643-
//void Texture2D::drawAtPoint(const Vec2& point)
644-
//{
645-
// GLfloat coordinates[] = {
646-
// 0.0f, _maxT,
647-
// _maxS,_maxT,
648-
// 0.0f, 0.0f,
649-
// _maxS,0.0f };
650-
//
651-
// GLfloat width = (GLfloat)_pixelsWide * _maxS,
652-
// height = (GLfloat)_pixelsHigh * _maxT;
653-
//
654-
// GLfloat vertices[] = {
655-
// point.x, point.y,
656-
// width + point.x, point.y,
657-
// point.x, height + point.y,
658-
// width + point.x, height + point.y };
659-
//
660-
// glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
661-
// glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD);
662-
// _shaderProgram->use();
663-
// _shaderProgram->setUniformsForBuiltins();
664-
//
665-
// glActiveTexture(GL_TEXTURE0);
666-
// glBindTexture(GL_TEXTURE_2D, _name);
667-
//
668-
// glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
669-
// glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, coordinates);
670-
//
671-
// glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
672-
//}
673-
//
674-
//void Texture2D::drawInRect(const Rect& rect)
675-
//{
676-
// GLfloat coordinates[] = {
677-
// 0.0f, _maxT,
678-
// _maxS,_maxT,
679-
// 0.0f, 0.0f,
680-
// _maxS,0.0f };
681-
//
682-
// GLfloat vertices[] = { rect.origin.x, rect.origin.y, /*0.0f,*/
683-
// rect.origin.x + rect.size.width, rect.origin.y, /*0.0f,*/
684-
// rect.origin.x, rect.origin.y + rect.size.height, /*0.0f,*/
685-
// rect.origin.x + rect.size.width, rect.origin.y + rect.size.height, /*0.0f*/ };
686-
//
687-
// glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
688-
// glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD);
689-
// _shaderProgram->use();
690-
// _shaderProgram->setUniformsForBuiltins();
691-
//
692-
// glActiveTexture(GL_TEXTURE0);
693-
// glBindTexture(GL_TEXTURE_2D, _name);
694-
//
695-
// glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
696-
// glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, coordinates);
697-
// glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
698-
//}
699-
700644
bool Texture2D::hasMipmaps() const
701645
{
702646
return _hasMipmaps;
@@ -927,6 +871,84 @@ void Texture2D::generateMipmap()
927871
_hasMipmaps = true;
928872
}
929873

874+
void Texture2D::initProgram()
875+
{
876+
if(_programState != nullptr)
877+
return;
878+
879+
auto& pipelineDescriptor = _customCommand.getPipelineDescriptor();
880+
//create program state
881+
_programState = new (std::nothrow) cocos2d::backend::ProgramState(
882+
positionTexture_vert, positionTexture_frag);
883+
_mvpMatrixLocation = _programState->getUniformLocation("u_MVPMatrix");
884+
_textureLocation = _programState->getUniformLocation("u_texture");
885+
886+
pipelineDescriptor.programState = _programState;
887+
888+
//setup vertex layout
889+
auto& vertexLayout = pipelineDescriptor.vertexLayout;
890+
auto& attributes = _programState->getProgram()->getActiveAttributes();
891+
auto iter = attributes.find("a_position");
892+
if(iter != attributes.end())
893+
vertexLayout.setAttribute("a_position", iter->second.location, backend::VertexFormat::FLOAT2, 0, false);
894+
895+
iter = attributes.find("a_texCoord");
896+
if(iter != attributes.end())
897+
vertexLayout.setAttribute("a_texCoord", iter->second.location, backend::VertexFormat::FLOAT2, 2 * sizeof(float), false);
898+
899+
vertexLayout.setLayout(4 * sizeof(float), backend::VertexStepMode::VERTEX);
900+
901+
//create vertex buffer
902+
_customCommand.setDrawType(CustomCommand::DrawType::ARRAY);
903+
_customCommand.setPrimitiveType(CustomCommand::PrimitiveType::TRIANGLE_STRIP);
904+
_customCommand.createVertexBuffer(4 * sizeof(float), 4, CustomCommand::BufferUsage::DYNAMIC);
905+
906+
//setup blend state
907+
BlendFunc blendFunc;
908+
if(hasPremultipliedAlpha())
909+
{
910+
blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
911+
}
912+
else
913+
{
914+
blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
915+
}
916+
917+
auto& blendDescriptor = pipelineDescriptor.blendDescriptor;
918+
blendDescriptor.blendEnabled = true;
919+
blendDescriptor.sourceRGBBlendFactor = blendDescriptor.sourceAlphaBlendFactor = blendFunc.src;
920+
blendDescriptor.destinationRGBBlendFactor = blendDescriptor.destinationAlphaBlendFactor = blendFunc.dst;
921+
922+
_programState->setTexture(_textureLocation, 0, _texture);
923+
}
924+
925+
void Texture2D::drawAtPoint(const Vec2 &point, float globalZOrder)
926+
{
927+
float width = (float)_pixelsWide * _maxS;
928+
float height = (float)_pixelsHigh * _maxT;
929+
Rect rect = { point.x, point.y, width, height };
930+
drawInRect(rect, globalZOrder);
931+
}
930932

933+
void Texture2D::drawInRect(const Rect& rect, float globalZOrder)
934+
{
935+
initProgram();
936+
_customCommand.init(globalZOrder);
937+
auto director = Director::getInstance();
938+
const auto& modelView = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
939+
const auto& projection = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
940+
941+
Mat4 matrixMVP = projection * modelView;
942+
943+
float vertexData[] = {
944+
rect.origin.x, rect.origin.y, 0.0f, _maxT,
945+
rect.size.width + rect.origin.x, rect.origin.y, _maxS, _maxT,
946+
rect.origin.x, rect.size.height + rect.origin.y, 0.0f, 0.0f,
947+
rect.size.width + rect.origin.x, rect.size.height + rect.origin.y, _maxS, 0.0f };
948+
949+
_programState->setUniform(_mvpMatrixLocation, matrixMVP.m, sizeof(matrixMVP.m));
950+
_customCommand.updateVertexBuffer(vertexData, sizeof(vertexData));
951+
Director::getInstance()->getRenderer()->addCommand(&_customCommand);
952+
}
931953

932954
NS_CC_END

cocos/renderer/CCTexture2D.h

+11-5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ THE SOFTWARE.
3434
#include "base/CCRef.h"
3535
#include "math/CCGeometry.h"
3636
#include "base/ccTypes.h"
37+
#include "renderer/CCCustomCommand.h"
3738

3839
NS_CC_BEGIN
3940

@@ -50,6 +51,7 @@ namespace ui
5051
namespace backend {
5152
class Texture2D;
5253
class Texture;
54+
class ProgramState;
5355
}
5456

5557
/**
@@ -245,9 +247,9 @@ class CC_DLL Texture2D : public Ref
245247
These functions require GL_TEXTURE_2D and both GL_VERTEX_ARRAY and GL_TEXTURE_COORD_ARRAY client states to be enabled.
246248
*/
247249
/** Draws a texture at a given point. */
248-
// void drawAtPoint(const Vec2& point);
250+
void drawAtPoint(const Vec2& point, float globalZOrder);
249251
/** Draws a texture inside a rect.*/
250-
// void drawInRect(const Rect& rect);
252+
void drawInRect(const Rect& rect, float globalZOrder);
251253

252254
/**
253255
Extensions to make it easy to create a Texture2D object from an image file.
@@ -380,6 +382,7 @@ class CC_DLL Texture2D : public Ref
380382
Texture2D* getAlphaTexture() const;
381383

382384
bool getAlphaTextureName() const;
385+
383386
public:
384387
/** Get pixel info map, the key-value pairs is PixelFormat and PixelFormatInfo.*/
385388
static const PixelFormatInfoMap& getPixelFormatInfoMap();
@@ -427,10 +430,9 @@ class CC_DLL Texture2D : public Ref
427430
* @param capInsets The parsed capInset from a .9 patch image.
428431
*/
429432
void addSpriteFrameCapInset(SpriteFrame* spritframe, const Rect& capInsets);
430-
431-
432-
433433

434+
void initProgram();
435+
434436
protected:
435437
/** pixel format of the texture */
436438
Texture2D::PixelFormat _pixelFormat;
@@ -472,6 +474,10 @@ class CC_DLL Texture2D : public Ref
472474
std::string _filePath;
473475

474476
Texture2D* _alphaTexture;
477+
backend::ProgramState* _programState = nullptr;
478+
backend::UniformLocation _mvpMatrixLocation;
479+
backend::UniformLocation _textureLocation;
480+
CustomCommand _customCommand;
475481
};
476482

477483

tests/cpp-tests/Classes/Texture2dTest/Texture2dTest.cpp

+7-17
Original file line numberDiff line numberDiff line change
@@ -1769,7 +1769,7 @@ void TextureDrawAtPoint::onEnter()
17691769

17701770
_tex1 = Director::getInstance()->getTextureCache()->addImage("Images/grossinis_sister1.png");
17711771
_Tex2F = Director::getInstance()->getTextureCache()->addImage("Images/grossinis_sister2.png");
1772-
1772+
17731773
_tex1->retain();
17741774
_Tex2F->retain();
17751775
}
@@ -1794,11 +1794,7 @@ void TextureDrawAtPoint::draw(Renderer *renderer, const Mat4 &transform, uint32_
17941794
{
17951795
TextureDemo::draw(renderer, transform, flags);
17961796

1797-
_renderCmd.init(_globalZOrder, transform, flags);
1798-
//TODO: impl new CustomRenderer
1799-
//_renderCmd.func = CC_CALLBACK_0(TextureDrawAtPoint::onDraw, this, transform, flags);
1800-
//renderer->addCommand(&_renderCmd);
1801-
1797+
onDraw(transform, flags);
18021798
}
18031799

18041800
void TextureDrawAtPoint::onDraw(const Mat4 &transform, uint32_t flags)
@@ -1810,9 +1806,8 @@ void TextureDrawAtPoint::onDraw(const Mat4 &transform, uint32_t flags)
18101806

18111807
auto s = Director::getInstance()->getWinSize();
18121808

1813-
//TODO: minggo
1814-
// _tex1->drawAtPoint(Vec2(s.width/2-50, s.height/2 - 50));
1815-
// _Tex2F->drawAtPoint(Vec2(s.width/2+50, s.height/2 - 50));
1809+
_tex1->drawAtPoint(Vec2(s.width/2-50, s.height/2 - 50), _globalZOrder);
1810+
_Tex2F->drawAtPoint(Vec2(s.width/2+50, s.height/2 - 50), _globalZOrder);
18161811

18171812
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
18181813
}
@@ -1838,11 +1833,7 @@ TextureDrawInRect::~TextureDrawInRect()
18381833
void TextureDrawInRect::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
18391834
{
18401835
TextureDemo::draw(renderer, transform, flags);
1841-
1842-
_renderCmd.init(_globalZOrder, transform, flags);
1843-
_renderCmd.func = CC_CALLBACK_0(TextureDrawInRect::onDraw, this, transform, flags);
1844-
//TODO: impl new CustomRenderer
1845-
//renderer->addCommand(&_renderCmd);
1836+
onDraw(transform, flags);
18461837
}
18471838

18481839
void TextureDrawInRect::onDraw(const Mat4 &transform, uint32_t flags)
@@ -1857,9 +1848,8 @@ void TextureDrawInRect::onDraw(const Mat4 &transform, uint32_t flags)
18571848
auto rect1 = Rect( s.width/2 - 80, 20, _tex1->getContentSize().width * 0.5f, _tex1->getContentSize().height *2 );
18581849
auto rect2 = Rect( s.width/2 + 80, s.height/2, _tex1->getContentSize().width * 2, _tex1->getContentSize().height * 0.5f );
18591850

1860-
//TODO: minggo
1861-
// _tex1->drawInRect(rect1);
1862-
// _Tex2F->drawInRect(rect2);
1851+
_tex1->drawInRect(rect1, _globalZOrder);
1852+
_Tex2F->drawInRect(rect2, _globalZOrder);
18631853

18641854
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
18651855
}

tests/cpp-tests/Classes/Texture2dTest/Texture2dTest.h

-2
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,6 @@ class TextureDrawAtPoint : public TextureDemo
475475
protected:
476476
void onDraw(const cocos2d::Mat4& transform, uint32_t flags);
477477

478-
cocos2d::CustomCommand _renderCmd;
479478
cocos2d::Texture2D* _tex1, *_Tex2F;
480479
};
481480

@@ -491,7 +490,6 @@ class TextureDrawInRect : public TextureDemo
491490
protected:
492491
void onDraw(const cocos2d::Mat4& transform, uint32_t flags);
493492

494-
cocos2d::CustomCommand _renderCmd;
495493
cocos2d::Texture2D* _tex1, *_Tex2F;
496494
};
497495

0 commit comments

Comments
 (0)