openg离线包,使用CoreGL的Apple OSX OpenGL离线渲染

I'm trying to render on OSX using CoreGL and a Framebuffer: it seems that the triangle rendering is not working at all, while for example a glClear with a color will be shown in the resulting PPM image.

I've tried several variation of the code below, but right now it seems to me that this is an impossible task on OSX.

#include

#include

#include

#include

#include

#include

int main(int argc, const char * argv[])

{

CGLContextObj context;

CGLPixelFormatAttribute attributes[13] = {

kCGLPFAOpenGLProfile,

(CGLPixelFormatAttribute) kCGLOGLPVersion_3_2_Core,

kCGLPFAAccelerated,

kCGLPFAColorSize, (CGLPixelFormatAttribute)24,

kCGLPFAAlphaSize, (CGLPixelFormatAttribute)8,

kCGLPFADoubleBuffer,

kCGLPFASampleBuffers, (CGLPixelFormatAttribute)1,

kCGLPFASamples, (CGLPixelFormatAttribute)4,

(CGLPixelFormatAttribute) 0

};

CGLPixelFormatObj pix; CGLError errorCode; GLint num;

errorCode = CGLChoosePixelFormat( attributes, &pix, &num );

errorCode = CGLCreateContext(pix, NULL, &context);

CGLDestroyPixelFormat( pix );

errorCode = CGLSetCurrentContext( context );

// -------

GLuint VertexArrayID;

glGenVertexArrays(1, &VertexArrayID);

glBindVertexArray(VertexArrayID);

//--------

GLuint fboid, rboid, dboid;

glGenFramebuffers( 1, &fboid );

glBindFramebuffer( GL_FRAMEBUFFER, fboid );

glGenRenderbuffers( 1, &rboid );

glBindRenderbuffer( GL_RENDERBUFFER, rboid );

glRenderbufferStorage( GL_RENDERBUFFER, GL_RGBA, 1440, 900);

glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rboid );

glBindRenderbuffer( GL_RENDERBUFFER, 0 );

glGenRenderbuffers( 1, &dboid );

glBindRenderbuffer( GL_RENDERBUFFER, dboid );

glRenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH_COMPONENT, 1440, 900);

glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, dboid );

glBindRenderbuffer( GL_RENDERBUFFER, 0 );

glBindFramebuffer(GL_FRAMEBUFFER, 0);

// --------

glBindFramebuffer( GL_FRAMEBUFFER, fboid );

static const GLfloat g_vertex_buffer_data[] = {-1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f};

GLuint vertexbuffer;

glGenBuffers(1, &vertexbuffer);

glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);

glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);

assert(glGetError()==0);

// -------

glEnableVertexAttribArray(0);

glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0 );

glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle

glDisableVertexAttribArray(0);

// -------

errorCode = CGLFlushDrawable(context);

char data[1440*900*3];

glReadPixels(0, 0, 1440, 900, GL_BGR, GL_UNSIGNED_BYTE, &data[0]);

FILE* ff = fopen("/Users/invernizzi/pippo.ppm", "w");

char* header = "P6 1440 900 255 ";

size_t i = fwrite(header, sizeof(char), 16, ff);

i = fwrite(&data[0], sizeof(char), 1440*900*3, ff);

fclose(ff);

return 0;

}

解决方案

If you have a 3.2 context, you have to create and bind a program with vertex and fragment programs. The fact that you can see a clear color shows the context is created correctly.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值