cocos2dx之OpenGl绘图

随着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方法

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值