重复在cocos2d 3.2中绑定到山丘的OpenGL-es纹理

时间:2015-03-12 09:25:47

标签: c++ cocos2d-x-3.0

原始文章

我正在尝试使用cocos2d实现raywenderlich关于使用重复条带坐标生成山丘的教程,本文是为Cocos2D编写的,当我尝试将其移植到Cocos2Dx 3.2时这意味着将其更新为openGl-es 2.到目前为止,一切都运行得很好但是我遇到的问题是让山的纹理能够正确重复,并且在重复一遍后纹理开始降低......  请帮我正确地获得质感....... 这是我的代码:

#define Point_FROM_B2VEC(v) Point((v.x*PTM_RATIO),(v.y*PTM_RATIO))
        #define B2VEC_FROM_Point(v) b2Vec2(v.x/PTM_RATIO,v.y/PTM_RATIO)
    #define PTM_RATIO       32
    #define MAX_HILL_POINTS 15
    #define MAX_SEGMENTS    MAX_HILL_POINTS*10
    #define MAX_COORDINATES 600

        Point hillTopVertices[MAX_COORDINATES],hillBottomVertices[MAX_COORDINATES];
            Point hillTopTexCoords[MAX_COORDINATES];
     Point hillBottomTexCoords[MAX_COORDINATES];

            GLuint         terraintopTexId, terrainBottomTexId;
     float         terrainTopTexSize,  terrainBottomTexSize;

向山丘发送纹理:

         texture = Director::getInstance()->getTextureCache()->addImage("surface.png");

            terraintopTexId = texture->getName();
            terrainTopTexSize = texture->getPixelsWide()/2;

    //terrain top texture
        texture1 = Director::getInstance()->getTextureCache()->addImage("old_stone_wall_textures_v3_3000x2000_1.jpg");


        terrainBottomTexId = texture1->getName();
        terrainBottomTexSize = texture1->getPixelsWide()/2;



void Terrain::generateCoordinates()
{
    int nTopVertCount = 0;
     int nTopVertCount1 = 0;

    //get the hill vertex and texcoordinates
    for(short i = 0; i < MAX_SEGMENTS; ++i)
    {
        Point point1 = Point_FROM_B2VEC(vertices[i]);
        Point point2 = Point_FROM_B2VEC(vertices[i+1]);






        CCLOG("%f",terrainBottomTexSize);

        hillTopVertices[nTopVertCount1]      = Point(point1.x, point1.y+16);
        hillTopTexCoords[nTopVertCount1++]   = Point(point1.x/terrainTopTexSize, 1);
        hillTopVertices[nTopVertCount1]      = Point(point2.x, point2.y+16);
        hillTopTexCoords[nTopVertCount1++]   = Point(point2.x/terrainTopTexSize, 1);

        hillTopVertices[nTopVertCount1]      = Point(point1.x, point1.y-16);
        hillTopTexCoords[nTopVertCount1++]   = Point(point1.x/terrainTopTexSize, 0);
        hillTopVertices[nTopVertCount1]      = Point(point2.x, point2.y-16);
        hillTopTexCoords[nTopVertCount1++]   = Point(point2.x/terrainTopTexSize, 0);

    }

        for(short i = 0; i < MAX_SEGMENTS; ++i)
        {
            Point point1 = Point_FROM_B2VEC(vertices[i]);
            Point point2 = Point_FROM_B2VEC(vertices[i+1]);

        hillBottomVertices[nTopVertCount]      = Point(point1.x, point1.y-terrainBottomTexSize+1);
        hillBottomTexCoords[nTopVertCount++]   = Point(point1.x/terrainBottomTexSize, 1);
        hillBottomVertices[nTopVertCount]      = Point(point2.x, point2.y-terrainBottomTexSize+1);
        hillBottomTexCoords[nTopVertCount++]   = Point(point2.x/terrainBottomTexSize, 1);

        hillBottomVertices[nTopVertCount]      = Point(point1.x, point1.y+2);
        hillBottomTexCoords[nTopVertCount++]   = Point(point1.x/terrainBottomTexSize, 0);
        hillBottomVertices[nTopVertCount]      = Point(point2.x, point2.y+2);
        hillBottomTexCoords[nTopVertCount++]   = Point(point2.x/terrainBottomTexSize, 0);
        }
        //energy adding

        //CCLOG("hillsegment---%d,pointx---%f,pointy---%f,offset--%f",i,point1.x,point1.y,offsetX);









}
void Terrain::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
{


    Node::draw(renderer, transform, flags);

    _renderCmds[0].init(0.0f);
    _renderCmds[0].func = CC_CALLBACK_0(Terrain::onDraw, this, transform);
    renderer->addCommand(&_renderCmds[0]);



}
void Terrain::onDraw(const Mat4 &transform) {



    auto glProgram = getGLProgram();
    glProgram->use();
    glProgram->setUniformsForBuiltins(transform);

    GL::bindTexture2D( terrainBottomTexId );
    GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX );

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
    glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, hillBottomVertices);
    glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, hillBottomTexCoords);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)MAX_COORDINATES);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    GL::bindTexture2D(terraintopTexId );
    GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX );

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
    glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, hillTopVertices);
    glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, hillTopTexCoords);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)MAX_COORDINATES);


    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

}

0 个答案:

没有答案