@@ -45,7 +45,10 @@ THE SOFTWARE.
45
45
#include " base/CCNinePatchImageParser.h"
46
46
#include " renderer/backend/Device.h"
47
47
#include " renderer/backend/StringUtils.h"
48
+ #include " renderer/backend/ProgramState.h"
49
+ #include " renderer/ccShaders.h"
48
50
#include " renderer/CCTextureUtils.h"
51
+ #include " renderer/CCRenderer.h"
49
52
50
53
#if CC_ENABLE_CACHE_TEXTURE_DATA
51
54
#include " renderer/CCTextureCache.h"
@@ -223,6 +226,7 @@ Texture2D::~Texture2D()
223
226
CC_SAFE_DELETE (_ninePatchInfo);
224
227
225
228
CC_SAFE_RELEASE (_texture);
229
+ CC_SAFE_RELEASE (_programState);
226
230
}
227
231
228
232
Texture2D::PixelFormat Texture2D::getPixelFormat () const
@@ -637,66 +641,6 @@ bool Texture2D::initWithBackendTexture(backend::Texture *texture)
637
641
return true ;
638
642
}
639
643
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
-
700
644
bool Texture2D::hasMipmaps () const
701
645
{
702
646
return _hasMipmaps;
@@ -927,6 +871,84 @@ void Texture2D::generateMipmap()
927
871
_hasMipmaps = true ;
928
872
}
929
873
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
+ }
930
932
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
+ }
931
953
932
954
NS_CC_END
0 commit comments