在OpenGL ES for iPhone中逐像素绘制:它是否可行,效率低下?

时间:2014-03-15 19:29:18

标签: ios iphone opengl-es pixel

我没有时间在这里详细描述我的问题,但这是我尝试逐个像素地绘制一个对象。我这样做是因为我想提出自己的3d到2D投影方法:<​​/ p>

#import "PixelatedObject.h"

typedef struct
{
    GLfloat position[2];
    GLubyte color[3];
} Pixel;

@implementation PixelatedObject

static GLKBaseEffect *baseEffect;
static GLKView *baseView;
static GLuint _vertexArrayObject;
static GLuint _pixelBufferObject;
static Pixel *pixels;
static GLuint numPixels;
static GLuint pixelWidth;
static GLuint pixelHeight;

+ (void)setupWithEffect: (GLKBaseEffect *)effect view: (GLKView *)view;
{
    baseEffect = effect;
    baseView = view;

    glGenVertexArraysOES(1, &_vertexArrayObject);
    glBindVertexArrayOES(_vertexArrayObject);

    pixelWidth = (GLuint) baseView.bounds.size.width;
    pixelHeight = (GLuint) baseView.bounds.size.height;
    numPixels = pixelWidth * pixelHeight;

    pixels = malloc(sizeof(Pixel) * numPixels);
    for (int i = 0; i < pixelWidth; i++)
    {
        for (int j = 0; j < pixelHeight; j++)
        {
            int index = i * pixelHeight + j;
            pixels[index].position[0] = i;
            pixels[index].position[1] = j;
            pixels[index].color[0] = (GLubyte) arc4random() % 256;
            pixels[index].color[1] = (GLubyte) arc4random() % 256;
            pixels[index].color[2] = (GLubyte) arc4random() % 256;
        }
    }

    glGenBuffers(1, &_pixelBufferObject);
    glBindBuffer(GL_ARRAY_BUFFER, _pixelBufferObject);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Pixel) * numPixels, pixels, GL_DYNAMIC_DRAW);

    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, sizeof(Pixel), (const GLvoid *) offsetof(Pixel, position));
    glEnableVertexAttribArray(GLKVertexAttribColor);
    glVertexAttribPointer(GLKVertexAttribColor, 3, GL_UNSIGNED_BYTE, GL_FALSE, sizeof(Pixel), (const GLvoid *) offsetof(Pixel, color));


    glBindVertexArrayOES(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
}

- (void)debug
{
    for (int i = 0; i < numPixels; i++)
    {
        Pixel pixel = pixels[i];
        NSLog(@"Pixel %i: {%6.1f,%6.1f} {%-3i,%-3i,%-3i}",
              i,
              pixel.position[0], pixel.position[1],
              pixel.color[0], pixel.color[1], pixel.color[2]);
    }
}

- (void)render
{
    glBindVertexArrayOES(_vertexArrayObject);
    glBindBuffer(GL_ARRAY_BUFFER, _pixelBufferObject);
    [baseEffect prepareToDraw];
    glDrawArrays(GL_POINTS, 0, numPixels);
    glBindVertexArrayOES(0);
}

@end

然后我只是在我的ViewController中渲染它,但没有任何东西弹出。有关如何做到这一点的任何想法?

0 个答案:

没有答案