1 #include
CubeTexture.h"
2 namespace GL;
3
4 cocos2d::Scene*
CubeTexture::createScene()
5 {
6 auto scene =
Scene::create();
7 auto layer =
CubeTexture::create();
8 scene->
addChild(layer);
9 return scene;
10 }
11
12 bool CubeTexture::init()
13 {
14 if ( Layer::init() )
15 {
16 mShaderProgram =
new GLProgram;
17 mShaderProgram->initWithFilenames(
myshader.vert",
myshader.frag");
18 mShaderProgram->
link();
19 mShaderProgram->
updateUniforms();
20
21 _textureID = Director::getInstance()->getTextureCache()->addImage(
HelloWorld.png" )->
getName();
22 _textureID2 = Director::getInstance()->getTextureCache()->addImage(
item_powerup_fish.png")->
getName();
23 glGenBuffers(
1,&
vertexBuffer );
24 glBindBuffer(GL_ARRAY_BUFFER,vertexBuffer );
25
26 glGenBuffers(
indexBuffer );
27 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,indexBuffer );
28
29 return true;
30 }
31 false;
32 }
33
34 void CubeTexture::draw( Renderer *renderer,255);line-height:1.5;">const Mat4 &
transform,uint32_t transformUpdated )
35 {
36 Layer::draw(renderer,transform,transformUpdated);
37
38 _customCommand.init(_globalZOrder);
39 _customCommand.func = CC_CALLBACK_0(CubeTexture::onDraw,255);line-height:1.5;">this
);
40 renderer->addCommand(&
_customCommand);
41 }
42
43 void CubeTexture::onDraw()
44 {
45 Director::getInstance()->
pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
46 Director::getInstance()->
loadIdentityMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
47 Director::getInstance()->
pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
48 Director::getInstance()->
loadIdentityMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
49
50 Mat4 modelViewMatrix;
51 Mat4::createLookAt(Vec3(
0,128);line-height:1.5;">5),Vec3(
0),-
modelViewMatrix);
52 modelViewMatrix.translate(
0 );
53
54 static float rotation =
20;
55 modelViewMatrix.rotate(Vec3(
0),CC_DEGREES_TO_RADIANS(rotation));
56
57 Mat4 projectionMatrix;
58 Mat4::createPerspective(
60,128);line-height:1.5;">480/
320,128);line-height:1.5;">1.0,128);line-height:1.5;">42,&
projectionMatrix);
59 Director::getInstance()->
multiplyMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION,projectionMatrix);
60 Director::getInstance()->
multiplyMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW,modelViewMatrix);
61
62 typedef
struct {
63 float Position[
3];
64 float Color[
4];
65 float TexCoord[
2];
66 } Vertex;
67 #define TEX_COORD_MAX 1
68
69 Vertex Vertices[] =
{
70 Front
71 {{
0},{
1},{TEX_COORD_MAX,128);line-height:1.5;">0
}}, 72 {{
1},TEX_COORD_MAX}},128);line-height:1.5;"> 73 {{-
0,128);line-height:1.5;"> 74 {{-
75 Back
76 {{
2},128);line-height:1.5;"> 77 {{-
78 {{
79 {{-
80 Left
81 {{-
82 {{-
83 {{-
84 {{-
85 Right
86 {{
87 {{
88 {{
89 {{
90 Top
91 {{
92 {{
93 {{-
94 {{-
95 Bottom
96 {{
97 {{
98 {{-
99 {{-
0}}
100 };
101 int vertexCount =
sizeof(Vertices) /
sizeof(Vertices[
0]);
102
103 GLubyte Indices[] =
{
104 105 2,128);line-height:1.5;">106 2,128);line-height:1.5;">3,128);line-height:1.5;">107
108 4,128);line-height:1.5;">5,128);line-height:1.5;">6
,128);line-height:1.5;">109 7,128);line-height:1.5;">110 111 8,128);line-height:1.5;">9,128);line-height:1.5;">10
,128);line-height:1.5;">112 10,128);line-height:1.5;">11,128);line-height:1.5;">8
,128);line-height:1.5;">113 114 12,128);line-height:1.5;">13,128);line-height:1.5;">14
,128);line-height:1.5;">115 14,128);line-height:1.5;">15,128);line-height:1.5;">12
,128);line-height:1.5;">116 117 16,128);line-height:1.5;">17,128);line-height:1.5;">18
,128);line-height:1.5;">118 18,128);line-height:1.5;">19,128);line-height:1.5;">16
,128);line-height:1.5;">119 120 20,128);line-height:1.5;">21,128);line-height:1.5;">22
,128);line-height:1.5;">121 22,128);line-height:1.5;">23,128);line-height:1.5;">20
122 };
123
124 1) Add to top of file
125 const Vertex Vertices2[] =
{
126 {{
0.5,128);line-height:1.5;">0.01},128);line-height:1.5;">1
}},128);line-height:1.5;">127 {{
128 {{-
129 {{-
130 };
131
132 const GLubyte Indices2[] =
{
133 3
134 };
135
136 glBindBuffer(GL_ARRAY_BUFFER,vertexBuffer);
137 glBufferData(GL_ARRAY_BUFFER,255);line-height:1.5;">sizeof
(Vertices),Vertices,GL_STATIC_DRAW);
138
139 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,indexBuffer);
140 glBufferData(GL_ELEMENT_ARRAY_BUFFER,255);line-height:1.5;">sizeof
(Indices),Indices,128);line-height:1.5;">141
142
143 _positionLocation = glGetAttribLocation(mShaderProgram->getProgram(),0);line-height:1.5;">a_position
144 _colorLocation = glGetAttribLocation(mShaderProgram->getProgram(),0);line-height:1.5;">a_color
145
146 _textureLocation = glGetAttribLocation(mShaderProgram->getProgram(),0);line-height:1.5;">TextureCoord
147 _textureUniform = glGetUniformLocation(mShaderProgram->getProgram(),0);line-height:1.5;">CC_Texture0
148
149 mShaderProgram->
use();
150 mShaderProgram->
setUniformsForBuiltins();
151
152 glEnableVertexAttribArray(_positionLocation);
153 glEnableVertexAttribArray(_colorLocation);
154 glEnableVertexAttribArray(_textureLocation);
155
156 glVertexAttribPointer(_positionLocation,GL_FLOAT,GL_FALSE,255);line-height:1.5;">sizeof(Vertex),(GLvoid*
)offsetof(Vertex,Position));
157
158 glVertexAttribPointer(_colorLocation,Color));
159
160 glVertexAttribPointer(_textureLocation,255);line-height:1.5;">sizeof
(Vertex),128);line-height:1.5;">161 (GLvoid*
)offsetof(Vertex,TexCoord));
162 //
163 ////set sampler
164 GL::bindTexture2DN(
165 glActiveTexture( GL_TEXTURE0 );
166 glBindTexture(GL_TEXTURE_2D,_textureID);
167 glBlendFunc(GL_ONE,GL_ONE_MINUS_SRC_ALPHA);
168 glEnable(GL_BLEND);
169 glEnable(GL_DEPTH_TEST);
170 glDrawElements(GL_TRIANGLES,128);line-height:1.5;">36,GL_UNSIGNED_BYTE,128);line-height:1.5;">0
);
171 glUniform1i(_textureUniform,128);line-height:1.5;">0);
unnecc in practice
172
173 glGenBuffers(
_vertexBuffer2);
174 glBindBuffer(GL_ARRAY_BUFFER,_vertexBuffer2);
175 glBufferData(GL_ARRAY_BUFFER,255);line-height:1.5;">sizeof
(Vertices2),Vertices2,128);line-height:1.5;">176
177 glGenBuffers(
_indexBuffer2);
178 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,_indexBuffer2);
179 glBufferData(GL_ELEMENT_ARRAY_BUFFER,255);line-height:1.5;">sizeof
(Indices2),Indices2,128);line-height:1.5;">180
181 glBindBuffer(GL_ARRAY_BUFFER,128);line-height:1.5;">182 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,128);line-height:1.5;">183
184 GL::bindTexture2DN(
185 glUniform1i(_textureUniform,128);line-height:1.5;">186
187 glVertexAttribPointer(_positionLocation,128);line-height:1.5;">188 glVertexAttribPointer(_colorLocation,(GLvoid*) (
sizeof(
float) *
3));
189 glVertexAttribPointer(_textureLocation,128);line-height:1.5;">7
));
190
191 glDrawElements(GL_TRIANGLE_STRIP,255);line-height:1.5;">sizeof(Indices2)/
sizeof(Indices2[
0]),128);line-height:1.5;">192
193 CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(
1,vertexCount);
194
195 CHECK_GL_ERROR_DEBUG();
196 glDisable(GL_DEPTH_TEST);
197
198 Director::getInstance()->
popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
199 Director::getInstance()->
popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
200 }