65.9K
CodeProject 正在变化。 阅读更多。
Home

cocos2d-x V3 中的 2D 纹理

2014年2月16日

CPOL
viewsIcon

18328

事情并没有我想象的那么容易,但我成功地在 cocos2d-x V3 Beta 2 下运行了我的滚动纹理程序的一个版本。我按照我之前的设置 … 继续阅读 →

事情并没有我想象的那么容易,但我成功地在 cocos2d-x V3 Beta 2 下运行了我的滚动纹理程序的一个版本。

我按照我之前文章中的方法设置了 cocos2d-x v3。然后我编辑了 HelloWorldScene.cpp,几乎重写了它,以复制我在使用优秀的 PRKit 之后,在 cocos2d 中 之前所做的事情

这个想法完全相同——创建一个点的数组,代表要绘制的三角形条,以创建我们景观的表面和亚地表。所以,第一个点是表面,第二个点是在屏幕底部(y = 0)的相同 X 坐标。第三个点是右侧的下一个表面点,第四个点是在屏幕底部(y = 0)的相同 X 坐标。依此类推。

每次绘制 cocos 层时,我们都会减少所有 X 坐标——因此实现了我们的“景观”向左滚动的效果。

Scrolling textures with cocos2d-x 3

使用 cocos2d-x 3 的滚动纹理

它不太大,所以我将把 HelloWorldScene.cpp 中的全部代码粘贴在这里

#import <CoreGraphics/CoreGraphics.h>
#include "HelloWorldScene.h"

USING_NS_CC;

Texture2D* _texture;
// The points used to draw the openGl triangle strip
cocos2d::Point* _areaTrianglePoints;
// The points used by openGl to apply the testure to the triabgle strip
cocos2d::Point* _texturePoints;
// The points on the surface. Not actually used in this demo, but would be used to set the surface sprites for example
std::vector<cocos2d::Point> _points;
// An offset, used to allow our textrue to scrol with the triangle list
cocos2d::Point _textureOffset;
// The number of points in our triangle array (so 2* the number of surface points)
int _numberOfPoints;
// The dy and dx that we use to scroll
cocos2d::Point _scrollSpeed;

// The openGl program object used
GLProgram* _glProgram;

Scene* HelloWorld::createScene()
{
    auto scene = Scene::create();
    auto layer = HelloWorld::create();
    scene->addChild(layer);
    return scene;
}

bool HelloWorld::init()
{
    if ( !Layer::init() )
    {
        return false;
    }

	_textureOffset = cocos2d::Point(0,0);
	_numberOfPoints = 200;
	_texture = Director::getInstance()->getTextureCache()->addImage("somerock.png");

	_areaTrianglePoints = (cocos2d::Point*) malloc(sizeof(cocos2d::Point) * _numberOfPoints);
	_texturePoints = (cocos2d::Point*) malloc(sizeof(cocos2d::Point) * _numberOfPoints);

	float x=0;
	float y=400;
	float maxy=800;
	float maxDx = 50;
	float maxDy = 240;
	for (int i=0;i<_numberOfPoints;i+=2)
	{
		cocos2d::Point p = cocos2d::Point(x,y);

		_points.push_back(p);
		// Add a point
		_areaTrianglePoints[i] = p;
		// Add another point, vertically below the previous point
		_areaTrianglePoints[i+1] = cocos2d::Point(x, 0);

		x+= rand() % (int)maxDx;
		float dy = (rand() % (int)(maxDy * 2)) – maxDy;
		y+=dy;

		if (y>maxy) y=maxy;
		if (y<0) y=20;
	}

	_glProgram = cocos2d::ShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE)  ;
    return true;
}

void HelloWorld::draw()
{
	_scrollSpeed = cocos2d::Point(-0.5,0);
	_textureOffset -= _scrollSpeed;

	for (int i = 0; i < _numberOfPoints; i++)
	{
		_areaTrianglePoints[i] += _scrollSpeed;
	}

	HelloWorld::calculateTexturePoints();

	// Tell OpenGL this is the texture we're using
	GL::bindTexture2D(_texture->getName());
	// wrap in both the S and T directions (that's X and Y for the rest of us!)
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	// Enable the arrays
	GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_TEX_COORDS);

	_glProgram->use();
	// Tell Cocos2D to pass the CCNode's position/scale/rotation matrix to the shader (well, that's what Ray says!)
	_glProgram->setUniformsForBuiltins();
	// Give OpenGl our array of points that we want to fill
	glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(cocos2d::Point), _areaTrianglePoints);
	// Give OpenGl the array of points in the texture we want to use to fill the above array of points
	glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, sizeof(cocos2d::Point), _texturePoints);
	// Tell OpenGl to draw the arrays we gave it
	glDrawArrays(GL_TRIANGLE_STRIP, 0, _numberOfPoints);
}

void HelloWorld::calculateTexturePoints()
{
	for (int i = 0; i < _numberOfPoints; i++)
	{
		// Calculate a point based on the areaTriangle point plus the current offset
		cocos2d::Point p = cocos2d::Point(_areaTrianglePoints[i].x + _textureOffset.x, _areaTrianglePoints[i].y + _textureOffset.y);
		// mod it to be within the bounds of the texture
		_texturePoints[i] = p * (1.0f / _texture->getPixelsWide());
		// reverse the y coordinate
		_texturePoints[i].y = 1 – _texturePoints[i].y;
	}
}

我现在要去把这个构建到 PooperPig for cocos2d-x 中了!

© . All rights reserved.