调用glTexSubImage2d时如何确定我收到的错误

时间:2011-07-25 19:19:48

标签: opengl core-animation

我正在尝试使用CAOpenGLLayer绘制一些纹理,但在尝试调用glTexSubImage2d时接收GL_INVALID_OPERATION。根据这份文件:http://www.opengl.org/sdk/docs/man/xhtml/glTexSubImage2D.xml这是其中描述的错误之一。但似乎我没有打破那里描述的任何规则,我不明白我做错了什么。这是我正在尝试运行的代码:

- (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat
{
    uint32_t plugin_width = 32, plugin_height = 32;

    texture_data = new uint8_t[plugin_width * plugin_height * 4];


    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity();

    glScalef(1.0f, -1.0f, 1.0f);
    glOrtho(0, plugin_width , 0, plugin_height , -1.0, 1.0);

    glActiveTexture(GL_TEXTURE0);
    glEnable(GL_TEXTURE_RECTANGLE_EXT);
    glGenTextures(1, &textureName); 
    glBindTexture(GL_TEXTURE_RECTANGLE_EXT, textureName);
    glTextureRangeAPPLE(GL_TEXTURE_RECTANGLE_EXT, plugin_width * plugin_height * 4, texture_data);
    glTexParameteri(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_STORAGE_HINT_APPLE , GL_STORAGE_SHARED_APPLE);

    glTexParameteri(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

    glTexImage2D(GL_TEXTURE_RECTANGLE_EXT, 0, GL_RGBA, plugin_width, plugin_height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8, texture_data);      
    glBindTexture(GL_TEXTURE_RECTANGLE_EXT, 0);

    return [super copyCGLContextForPixelFormat:pixelFormat];
}

- (void)drawInCGLContext:(CGLContextObj)ctx pixelFormat:(CGLPixelFormatObj)pf forLayerTime:(CFTimeInterval)t displayTime:(const CVTimeStamp *)ts 
{
    uint32_t plugin_width = 32, plugin_height = 32;
    uint32_t width  = plugin_width;
    uint32_t height = plugin_height;


    srand(time(NULL));
    for (int i = 0; i < 32*32*4; i ++)
        texture_data[i] = rand() % 255;

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_RECTANGLE_EXT, textureName);


    glTexSubImage2D (GL_TEXTURE_RECTANGLE_EXT, 0, 0, 0, width, height,GL_BGRA, GL_UNSIGNED_INT_8_8_8_8 ,texture_data);  

    assert(glGetError() == GL_NO_ERROR); // here i'm getting GL_INVALID_OPERATION

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glBegin(GL_QUADS);

    glTexCoord2f(0.0f,0.0f);
    glVertex2f(0.0f, 0.0f);

    glTexCoord2f(1.0f,0.0f);
    glVertex2f(width, 0);

    glTexCoord2f(0.0f,1.0f);
    glVertex2f(0, height);

    glTexCoord2f(1.0f,1.0f);
    glVertex2f(width, height);      
    glEnd();


    [super drawInCGLContext:ctx pixelFormat:pf forLayerTime:t displayTime:ts];
}

1 个答案:

答案 0 :(得分:0)

您是否尝试在两次调用(glTexImage2D和glTexSubImage2D)中使用GL_UNSIGNED_BYTE替换外部格式GL_UNSIGNED_INT_8_8_8_8? 这可能有所帮助。