Apple OSX OpenGL offline rendering with CoreGL

落爺英雄遲暮 提交于 2019-12-08 06:19:22

问题


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 <stdio.h>
#include <OpenGL/OpenGL.h>
#include <Opengl/glext.h>
#include <stdlib.h>
#include <assert.h>
#include <opengl/gl3.h>

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;
}

回答1:


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.



来源:https://stackoverflow.com/questions/17932151/apple-osx-opengl-offline-rendering-with-coregl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!