随着OpenGL的发展,其提供的绘图函数也变得多种多样。对于同一个效果来说,常常有多种不同的实现方法,因此想要在此对OpenGL的绘图函数进行全方位的介绍是不可能的,这里我们只简单介绍Cocos2d-x中常用的绘图函数。
例子代码:
void draw() {
static CCSize winSize = CCDirector::sharedDirector()->getWinSize();
static int _x = winSize.width / 2.0f;
static int _y = winSize.height / 2.0f;
static GLfloat vertex[] = {
_x, _y , 0.0f,
_x + 57.0f, _y , 0.0f,
_y, _y + 57.0f , 0.0f,
_x + 57.0f, _y + 57.0f , 0.0f,
};
static GLfloat coord[] = {
0.0f, 1.0f,
1.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f,
};
static GLfloat color[] = {
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
};
static CCTexture2D *texture2d = NULL;
if(texture2d == NULL) {
texture2d = CCTextureCache::sharedTextureCache()->addImage("Hello.png"); // 添加要绘制的纹理
coord[2] = coord[6] = texture2d->getMaxS();
coord[1] = coord[3] = texture2d->getMaxT();
}
//kCCVertexAttribFlag_PosColorTex = ( kCCVertexAttribFlag_Position | kCCVertexAttribFlag_Color | kCCVertexAttribFlag_TexCoords ),
ccGLEnableVertexAttribs(kCCVertexAttribFlag_PosColorTex);
texture2d->getShaderProgram()->use();
texture2d->getShaderProgram()->setUniformForModelViewProjectionMatrix();
//
int iname = texture2d->getName();
glBindTexture(GL_TEXTURE_2D, texture2d->getName());
//
glVertexAttribPointer(kCCVertexAttribFlag_Position, 3, GL_FLOAT, GL_FALSE, 0, vertex);
glVertexAttribPointer(kCCVertexAttribFlag_TexCoords, 2, GL_FLOAT, GL_FALSE, 0, coord);
glVertexAttribPointer(kCCVertexAttribFlag_Color, 4, GL_FLOAT, GL_FALSE, 0, color);
//
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
};
需要重写CCNode的draw方法
版权声明:本文为ganpengjin1原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。