cocos2dx 多重纹理贴图

前端之家收集整理的这篇文章主要介绍了cocos2dx 多重纹理贴图前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

<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);
}
原文链接:https://www.f2er.com/cocos2dx/342538.html

猜你在找的Cocos2d-x相关文章