2

draw我的CCLayer. 我正在画一些线条并CGPoints存储在std::vector. 我已经使用 ccDrawLine 成功绘制了线条。但是,使用时glDrawArray,什么都没有显示。我已经包含了结果的屏幕截图。正如您在使用 ccDrawnLine 时所看到的,它正确地绘制了线条。有任何想法吗?

[更新] 我使用的是 Cocos2d v2.0,默认情况下它使用 OpenGLES 2.0。

//openGLES
#include <OpenGLES/EAGL.h>
#include <OpenGLES/ES2/gl.h>
#include <OpenGLES/ES2/glext.h>

- (void) draw {
    glLineWidth( 3.0f );

    //begin and clear
    [renderTexture beginWithClear:waveform4F.r g:waveform4F.g b:waveform4F.b a:waveform4F.a];
    vector<CGPoint> vertices = bufferQueue.front();

    if (WaveformStyleLined) {
        for (int i = 1; i < vertices.size(); i++)
            ccDrawLine(vertices[i - 1], vertices[i]);
    } else {
        ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );
        kmGLPushMatrix();

        glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, sizeof(CGPoint), &vertices[0]);
        glDrawArrays(GL_LINES, sizeof(CGPoint), vertices.size());

        kmGLPopMatrix();
    }

    bufferQueue.pop();
    [renderTexture end];
}

使用 ccDrawLine

使用 glDrawArrays

以下是 my 中的一些CGPoints vertices

2012-08-18 08:57:49.750 vertices[1]:NSPoint: {1, 48.09375}
2012-08-18 08:57:49.751 vertices[2]:NSPoint: {2, 47.996094}
2012-08-18 08:57:49.752 vertices[3]:NSPoint: {3, 48.046875}
2012-08-18 08:57:49.753 vertices[4]:NSPoint: {4, 48.214844}
2012-08-18 08:57:49.754 vertices[5]:NSPoint: {5, 48.152344}
2012-08-18 08:57:49.755 vertices[6]:NSPoint: {6, 48.035156}
2012-08-18 08:57:49.755 vertices[7]:NSPoint: {7, 48.078125}
2012-08-18 08:57:49.756 vertices[8]:NSPoint: {8, 48.144531}
2012-08-18 08:57:49.757 vertices[9]:NSPoint: {9, 48.0625}
2012-08-18 08:57:49.757 vertices[10]:NSPoint: {10, 48.242188}
4

2 回答 2

0

我最终使用了ccDrawPoly,其中内部使用glDrawArray并实现了与 ccDrawLine 完全相同的输出,但速度要快得多,因为我不是逐点检索。但是,我仍然很好奇如何使用纯 OpenGL 代码来做到这一点。任何帮助或指导将不胜感激。

if (WaveformStyleLined) {
    for (int i = 1; i < vertices.size(); i++)
        ccDrawLine(vertices[i - 1], vertices[i]);

} else {
    ccDrawPoly(&vertices[0], vertices.size(), false);
}
于 2012-08-20T08:20:57.780 回答
0

通过添加像 DrummerB 这样的着色器,线条开始出现。我从 Cocos2d 复制了相同的着色器代码,它就像一个魅力。

//openGLES
#include <OpenGLES/EAGL.h>
#include <OpenGLES/ES2/gl.h>
#include <OpenGLES/ES2/glext.h>

static BOOL initialized = NO;
static CCGLProgram *shader_ = nil;
static int colorLocation_ = -1;
static ccColor4F color_ = {1,1,1,1};
static int pointSizeLocation_ = -1;
static void lazy_init( void )
{
    if( ! initialized ) {

    //
    // Position and 1 color passed as a uniform (to similate glColor4ub )
    //
        shader_ = [[CCShaderCache sharedShaderCache] programForKey:kCCShader_Position_uColor];

        colorLocation_ = glGetUniformLocation( shader_->program_, "u_color");
        pointSizeLocation_ = glGetUniformLocation( shader_->program_, "u_pointSize");

        initialized = YES;
    }
}


- (void) draw {
    glLineWidth( 3.0f );

    //begin and clear
    [renderTexture beginWithClear:waveform4F.r g:waveform4F.g b:waveform4F.b a:waveform4F.a];
    vector<CGPoint> vertices = bufferQueue.front();

    if (WaveformStyleLined) {

        ccDrawPoly(&vertices[0], vertices.size(), false);

    } else {

        lazy_init();

        [shader_ use];
        [shader_ setUniformForModelViewProjectionMatrix];
        [shader_ setUniformLocation:colorLocation_ with4fv:(GLfloat*) &color_.r count:1];

        ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );

        glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, &vertices[0]);
        glDrawArrays(GL_LINE_STRIP, 0, vertices.size());
    }

    bufferQueue.pop();
    [renderTexture end];
}
于 2012-08-22T09:24:10.283 回答