ORIGINAL ARTICLE
I am trying to implement a Raywenderlich tutorial on creating hills with repeating striped coordinates using cocos2d. This article was written for Cocos2D, and as I try to port it to Cocos2Dx 3.2, it means updating it for openGl-es 2. Until now, everything worked fine, however, I am having problems so that the hill texture repeats correctly , and also after some repetition, the texture began to deteriorate ... please help me get the texture correctly ....... Here is my code:
#define Point_FROM_B2VEC(v) Point((vx*PTM_RATIO),(vy*PTM_RATIO)) #define B2VEC_FROM_Point(v) b2Vec2(vx/PTM_RATIO,vy/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;
Sending hills texture:
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); }