<1>myshader.vert
attribute vec4 a_position;attribute vec4 a_color;attribute vec2 TextureCoord;varying vec4 Destinationcolor;varying vec2 v_texCoord;voID main(){ Destinationcolor = a_color; v_texCoord = TextureCoord; gl_position = CC_MVPMatrix * a_position;}<2>myshader.frag
varying vec4 Destinationcolor;varying vec2 v_texCoord;voID main(){ gl_Fragcolor = Destinationcolor * texture2D(CC_Texture0,v_texCoord);}
<3>CubeTexture.h
#ifndef __JNTest__CubeTexture__#define __JNTest__CubeTexture__#include "cocos2d.h"USING_NS_CC;class CubeTexture : public Layer{public: static Scene* createScene(); virtual bool init(); virtual voID visit(Renderer *renderer,const Mat4& parenttransform,uint32_t parentFlags); voID onDraw(); CREATE_FUNC(CubeTexture);private: Mat4 _modelVIEwMV; CustomCommand _customCommand; GLProgram* mShaderProgram; Glint _colorLocation; Glint _positionLocation; Glint _textureLocation; gluint _textureUniform; gluint _textureID; gluint _textureID2; gluint vertexBuffer; gluint indexBuffer; gluint _vertexBuffer2; gluint _indexBuffer2;};#endif /* defined(__JNTest__CubeTexture__) */<4>CubeTexture.cpp
#include "CubeTexture.h"using namespace GL;Scene* CubeTexture::createScene(){ auto scene = Scene::create(); auto layer = CubeTexture::create(); scene->addChild(layer); return scene;}bool CubeTexture::init(){ if(!Layer::init()){ return false; } mShaderProgram = new GLProgram; mShaderProgram->initWithfilenames("myshader.vert","myshader.frag"); mShaderProgram->link(); mShaderProgram->updateUniforms(); _textureID = Director::getInstance()->getTextureCache()->addImage("HelloWorld.png")->getname(); _textureID2 = Director::getInstance()->getTextureCache()->addImage("heart.png")->getname(); glGenBuffers(1,&vertexBuffer); glBindBuffer(GL_ARRAY_BUFFER,vertexBuffer); glGenBuffers(1,&indexBuffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,indexBuffer); return true;}voID CubeTexture::visit(Renderer *renderer,uint32_t parentFlags){ Node::visit(renderer,parenttransform,parentFlags); _customCommand.init(_globalZOrder); _customCommand.func = CC_CALLBACK_0(CubeTexture::onDraw,this); renderer->addCommand(&_customCommand);}voID CubeTexture::onDraw(){ Director::getInstance()->pushmatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); Director::getInstance()->loadIDentityMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); Director::getInstance()->pushmatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION); Director::getInstance()->loadIDentityMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION); Mat4 modelVIEwMatrix; Mat4::createLookAt(Vec3(0,5),Vec3(0,0),-1,&modelVIEwMatrix); modelVIEwMatrix.translate(0,0 ); static float rotation = 20; modelVIEwMatrix.rotate(Vec3(0,1,CC_degrees_TO_radians(rotation)); Mat4 projectionMatrix; Mat4::createPerspective(60,480/320,1.0,42,&projectionMatrix); Director::getInstance()->multiplyMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION,projectionMatrix); Director::getInstance()->multiplyMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW,modelVIEwMatrix); typedef struct { float position[3]; float color[4]; float TexCoord[2]; } Vertex; #define TEX_COORD_MAX 1 Vertex Vertices[] = { // Front {{1,0},{1,1},{TEX_COORD_MAX,0}},{{1,{0,TEX_COORD_MAX}},{{-1,// Back {{1,-2},// left {{-1,// Right {{1,// top {{1,// Bottom {{1,0}} }; int vertexCount = sizeof(Vertices) / sizeof(Vertices[0]); glubyte Indices[] = { // Front 0,2,3,// Back 4,5,6,4,7,// left 8,9,10,11,8,// Right 12,13,14,15,12,// top 16,17,18,19,16,// Bottom 20,21,22,23,20 }; // 1) Add to top of file const Vertex Vertices2[] = { {{0.5,-0.5,0.01},1}},{{0.5,0.5,{{-0.5,}; const glubyte Indices2[] = { 1,3 }; glBindBuffer(GL_ARRAY_BUFFER,vertexBuffer); glBufferData(GL_ARRAY_BUFFER,sizeof(Vertices),Vertices,GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,indexBuffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(Indices),Indices,GL_STATIC_DRAW); _positionLocation = glGetAttribLocation(mShaderProgram->getProgram(),"a_position"); _colorLocation = glGetAttribLocation(mShaderProgram->getProgram(),"a_color"); _textureLocation = glGetAttribLocation(mShaderProgram->getProgram(),"TextureCoord"); _textureUniform = glGetUniformlocation(mShaderProgram->getProgram(),"CC_Texture0"); mShaderProgram->use(); mShaderProgram->setUniformsForBuiltins(); glEnabLevertexAttribarray(_positionLocation); glEnabLevertexAttribarray(_colorLocation); glEnabLevertexAttribarray(_textureLocation); glVertexAttribPointer(_positionLocation,GL_float,GL_FALSE,sizeof(Vertex),(GLvoID*)offsetof(Vertex,position)); glVertexAttribPointer(_colorLocation,color)); glVertexAttribPointer(_textureLocation,TexCoord)); // ////set sampler GL::bindTexture2DN(0,_textureID); //glActiveTexture( GL_TEXTURE0 ); //glBindTexture(GL_TEXTURE_2D,_textureID); glBlendFunc(GL_ONE,GL_ONE_MINUS_SRC_Alpha); glEnable(GL_BLEND); glEnable(GL_DEPTH_TEST); glDrawElements(GL_TRIANGLES,36,GL_UNSIGNED_BYTE,0); gluniform1i(_textureUniform,0); // unnecc in practice glGenBuffers(1,&_vertexBuffer2); glBindBuffer(GL_ARRAY_BUFFER,_vertexBuffer2); glBufferData(GL_ARRAY_BUFFER,sizeof(Vertices2),Vertices2,GL_STATIC_DRAW); glGenBuffers(1,&_indexBuffer2); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,_indexBuffer2); glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(Indices2),Indices2,GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER,_vertexBuffer2); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,_indexBuffer2); GL::bindTexture2DN(0,_textureID2); gluniform1i(_textureUniform,0); // unnecc in practice glVertexAttribPointer(_positionLocation,0); glVertexAttribPointer(_colorLocation,(GLvoID*) (sizeof(float) * 3)); glVertexAttribPointer(_textureLocation,(GLvoID*) (sizeof(float) * 7)); glDrawElements(GL_TRIANGLE_STRIP,sizeof(Indices2)/sizeof(Indices2[0]),0); CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,vertexCount); CHECK_GL_ERROR_DEBUG(); gldisable(GL_DEPTH_TEST); Director::getInstance()->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION); Director::getInstance()->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);}总结
以上是内存溢出为你收集整理的cocos2dx 多重纹理贴图全部内容,希望文章能够帮你解决cocos2dx 多重纹理贴图所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)