// // LLWindowMesaHeadless // LLWindowMesaHeadless::LLWindowMesaHeadless(LLWindowCallbacks* callbacks, const std::string& title, const std::string& name, S32 x, S32 y, S32 width, S32 height, U32 flags, BOOL fullscreen, BOOL clearBg, BOOL disable_vsync, BOOL use_gl, BOOL ignore_pixel_depth) : LLWindow(callbacks, fullscreen, flags) { if (use_gl) { llinfos << "MESA Init" << llendl; mMesaContext = OSMesaCreateContextExt( GL_RGBA, 32, 0, 0, NULL ); /* Allocate the image buffer */ mMesaBuffer = new unsigned char [width * height * 4 * MESA_CHANNEL_SIZE]; llassert(mMesaBuffer); gMesaBuffer = (U16*)mMesaBuffer; /* Bind the buffer to the context and make it current */ if (!OSMesaMakeCurrent( mMesaContext, mMesaBuffer, MESA_CHANNEL_TYPE, width, height )) { llerrs << "MESA: OSMesaMakeCurrent failed!" << llendl; } llverify(gGLManager.initGL()); } }
int init_context(int w, int h) { Width = w; Height = h; ctx = OSMesaCreateContextExt( OSMESA_RGBA, 16, 0, 0, NULL ); if (!ctx) { printf("OSMesaCreateContext failed!\n"); return -1; } buffer = malloc( Width * Height * 4 * sizeof(GLubyte) ); if (!buffer) { printf("Alloc image buffer failed!\n"); return -1; } if (!OSMesaMakeCurrent( ctx, buffer, GL_UNSIGNED_BYTE, Width, Height )) { printf("OSMesaMakeCurrent failed!\n"); return -1; } { int z, s, a; glGetIntegerv(GL_DEPTH_BITS, &z); glGetIntegerv(GL_STENCIL_BITS, &s); glGetIntegerv(GL_ACCUM_RED_BITS, &a); printf("Depth=%d Stencil=%d Accum=%d\n", z, s, a); } return 0; }
/* * Create an Off-Screen Mesa rendering context. The only attribute needed is * an RGBA vs Color-Index mode flag. * * Input: format - either GL_RGBA or GL_COLOR_INDEX * sharelist - specifies another OSMesaContext with which to share * display lists. NULL indicates no sharing. * Return: an OSMesaContext or 0 if error */ GLAPI OSMesaContext GLAPIENTRY OSMesaCreateContext( GLenum format, OSMesaContext sharelist ) { const GLint accumBits = (format == OSMESA_COLOR_INDEX) ? 0 : 16; return OSMesaCreateContextExt(format, DEFAULT_SOFTWARE_DEPTH_BITS, 8, accumBits, sharelist); }
void Renderer3dImpl::set_parameters_low_level() { ctx_ = OSMesaCreateContextExt(OSMESA_RGB, 16, 0, 0, NULL); ctx_buffer_ = malloc(width_ * height_ * 3 * sizeof(GLubyte)); OSMesaMakeCurrent(ctx_, ctx_buffer_, GL_UNSIGNED_BYTE, width_, height_); }
OSMesaContext init_osmesa(int width, int height, void *buffer) { OSMesaContext ctx = OSMesaCreateContextExt(OSMESA_RGBA, 16, 0, 0, NULL); if (!ctx) { fprintf(stderr, "ERROR: OSMesaCreateContext failed"); } else if (!OSMesaMakeCurrent(ctx, buffer, GL_UNSIGNED_BYTE, width, height)) { fprintf(stderr, "ERROR: OSMesaMakeCurrent failed"); OSMesaDestroyContext(ctx); ctx = NULL; } return ctx; }
SkMesaGLContext::SkMesaGLContext() : fContext(static_cast<Context>(NULL)) , fImage(NULL) { GR_STATIC_ASSERT(sizeof(Context) == sizeof(OSMesaContext)); /* Create an RGBA-mode context */ #if OSMESA_MAJOR_VERSION * 100 + OSMESA_MINOR_VERSION >= 305 /* specify Z, stencil, accum sizes */ fContext = (Context)OSMesaCreateContextExt(OSMESA_BGRA, 0, 0, 0, NULL); #else fContext = (Context)OSMesaCreateContext(OSMESA_BGRA, NULL); #endif if (!fContext) { SkDebugf("OSMesaCreateContext failed!\n"); this->destroyGLContext(); return; } // Allocate the image buffer fImage = (GrGLubyte *) sk_malloc_throw(gBOGUS_SIZE * gBOGUS_SIZE * 4 * sizeof(GrGLubyte)); if (!fImage) { SkDebugf("Alloc image buffer failed!\n"); this->destroyGLContext(); return; } // Bind the buffer to the context and make it current if (!OSMesaMakeCurrent((OSMesaContext)fContext, fImage, GR_GL_UNSIGNED_BYTE, gBOGUS_SIZE, gBOGUS_SIZE)) { SkDebugf("OSMesaMakeCurrent failed!\n"); this->destroyGLContext(); return; } SkAutoTUnref<const GrGLInterface> gl(GrGLCreateMesaInterface()); if (NULL == gl.get()) { SkDebugf("Could not create GL interface!\n"); this->destroyGLContext(); return; } if (!gl->validate()) { SkDebugf("Could not validate GL interface!\n"); this->destroyGLContext(); return; } this->init(gl.detach()); }
static void *osmesa_ctx_init(video_frame_info_t *video_info, void *video_driver) { #ifdef HAVE_OSMESA_CREATE_CONTEXT_ATTRIBS const int attribs[] = { OSMESA_FORMAT, g_osmesa_format, OSMESA_DEPTH_BITS, 0, OSMESA_STENCIL_BITS, 0, OSMESA_ACCUM_BITS, 0, OSMESA_PROFILE, g_osmesa_profile, OSMESA_CONTEXT_MAJOR_VERSION, g_osmesa_major, OSMESA_CONTEXT_MINOR_VERSION, g_osmesa_minor, 0, 0 }; #endif gfx_ctx_osmesa_data_t *osmesa = (gfx_ctx_osmesa_data_t*) calloc(1, sizeof(gfx_ctx_osmesa_data_t)); if (!osmesa) goto error; #ifdef HAVE_OSMESA_CREATE_CONTEXT_ATTRIBS osmesa->ctx = OSMesaCreateContextAttribs(attribs, NULL); #endif #ifdef HAVE_OSMESA_CREATE_CONTEXT_EXT if (!osmesa->ctx) osmesa->ctx = OSMesaCreateContextExt(g_osmesa_format, 0, 0, 0, NULL); #endif if (!osmesa->ctx) { #if defined(HAVE_OSMESA_CREATE_CONTEXT_ATTRIBS) || defined(HAVE_OSMESA_CREATE_CONTEXT_EXT) RARCH_WARN("[osmesa]: Falling back to standard context creation.\n"); #endif osmesa->ctx = OSMesaCreateContext(g_osmesa_format, NULL); } if (!osmesa->ctx) goto error; osmesa->pixsize = g_osmesa_bpp; return osmesa; error: if (osmesa) free(osmesa); RARCH_WARN("[omesa]: Failed to initialize the context driver.\n"); return NULL; }
OSGLContext_osmesa::OSGLContext_osmesa(const FramebufferConfig& pixelFormatAttrs, int /*major*/, int /*minor*/, bool /*coreProfile*/, const GLRendererID& rendererID, const OSGLContext_osmesa* shareContex) : _imp(new OSGLContext_osmesaPrivate()) { GLenum format = GL_RGBA; int depthBits = pixelFormatAttrs.depthBits; int stencilBits = pixelFormatAttrs.stencilBits; int accumBits = pixelFormatAttrs.accumRedBits + pixelFormatAttrs.accumGreenBits + pixelFormatAttrs.accumBlueBits + pixelFormatAttrs.accumAlphaBits; /* Create an RGBA-mode context */ #if defined(OSMESA_GALLIUM_DRIVER) && (OSMESA_MAJOR_VERSION * 100 + OSMESA_MINOR_VERSION >= 1102) /* specify Z, stencil, accum sizes */ { int cpuDriver = rendererID.renderID; int attribs[100], n = 0; attribs[n++] = OSMESA_FORMAT; attribs[n++] = format; attribs[n++] = OSMESA_DEPTH_BITS; attribs[n++] = depthBits; attribs[n++] = OSMESA_STENCIL_BITS; attribs[n++] = stencilBits; attribs[n++] = OSMESA_ACCUM_BITS; attribs[n++] = accumBits; attribs[n++] = OSMESA_GALLIUM_DRIVER; attribs[n++] = (int)cpuDriver; attribs[n++] = 0; _imp->ctx = OSMesaCreateContextAttribs( attribs, shareContex ? shareContex->_imp->ctx : 0); } #else Q_UNUSED(rendererID); #if OSMESA_MAJOR_VERSION * 100 + OSMESA_MINOR_VERSION >= 305 /* specify Z, stencil, accum sizes */ _imp->ctx = OSMesaCreateContextExt( format, depthBits, stencilBits, accumBits, shareContex ? shareContex->_imp->ctx : 0); #else _imp->ctx = OSMesaCreateContext( format, shareContex ? shareContex->_imp->ctx : 0); #endif #endif if (!_imp->ctx) { throw std::runtime_error("OSMesaCreateContext failed"); } }
DMesaContext DMesaCreateContext (DMesaVisual visual, DMesaContext share) { DMesaContext dmesa; if ((dmesa = (DMesaContext)CALLOC_STRUCT(dmesa_context)) != NULL) { dmesa->osmesa = OSMesaCreateContextExt( visual->format, visual->depthBits, visual->stencilBits, visual->accumBits, (share != NULL) ? share->osmesa : NULL); if (dmesa->osmesa == NULL) { FREE(dmesa); dmesa = NULL; } } return dmesa; }
const GrGLInterface* SkMesaGLContext::createGLContext() { /* Create an RGBA-mode context */ #if OSMESA_MAJOR_VERSION * 100 + OSMESA_MINOR_VERSION >= 305 /* specify Z, stencil, accum sizes */ fContext = (Context)OSMesaCreateContextExt(OSMESA_BGRA, 0, 0, 0, NULL); #else fContext = (Context)OSMesaCreateContext(OSMESA_BGRA, NULL); #endif if (!fContext) { SkDebugf("OSMesaCreateContext failed!\n"); this->destroyGLContext(); return NULL; } // Allocate the image buffer fImage = (GrGLubyte *) sk_malloc_throw(gBOGUS_SIZE * gBOGUS_SIZE * 4 * sizeof(GrGLubyte)); if (!fImage) { SkDebugf("Alloc image buffer failed!\n"); this->destroyGLContext(); return NULL; } // Bind the buffer to the context and make it current if (!OSMesaMakeCurrent((OSMesaContext)fContext, fImage, GR_GL_UNSIGNED_BYTE, gBOGUS_SIZE, gBOGUS_SIZE)) { SkDebugf("OSMesaMakeCurrent failed!\n"); this->destroyGLContext(); return NULL; } const GrGLInterface* interface = GrGLCreateMesaInterface(); if (!interface) { SkDebugf("Could not create GL interface!\n"); this->destroyGLContext(); return NULL; } return interface; }
static sk_sp<GrContext> create_grcontext() { // We just leak the OSMesaContext... the process will die soon anyway. if (OSMesaContext osMesaContext = OSMesaCreateContextExt(OSMESA_BGRA, 0, 0, 0, nullptr)) { static uint32_t buffer[16 * 16]; OSMesaMakeCurrent(osMesaContext, &buffer, GL_UNSIGNED_BYTE, 16, 16); } auto osmesa_get = [](void* ctx, const char name[]) { SkASSERT(nullptr == ctx); SkASSERT(OSMesaGetCurrentContext()); return OSMesaGetProcAddress(name); }; sk_sp<const GrGLInterface> mesa(GrGLAssembleInterface(nullptr, osmesa_get)); if (!mesa) { return nullptr; } return sk_sp<GrContext>(GrContext::Create( kOpenGL_GrBackend, reinterpret_cast<intptr_t>(mesa.get()))); }
CAMLprim value ml_osmesacreatecontextext( value _format, value depthBits, value stencilBits, value accumBits, value ml_sharelist ) { OSMesaContext ctx; OSMesaContext sharelist; GLenum format; if (Is_long(ml_sharelist)) sharelist = NULL; else sharelist = (OSMesaContext) Field(ml_sharelist,0); switch (Int_val(_format)) { case 0: format = OSMESA_COLOR_INDEX; break; case 1: format = OSMESA_RGBA; break; case 2: format = OSMESA_BGRA; break; case 3: format = OSMESA_ARGB; break; case 4: format = OSMESA_RGB; break; case 5: format = OSMESA_BGR; break; } #if OSMESA_MAJOR_VERSION * 100 + OSMESA_MINOR_VERSION >= 305 ctx = OSMesaCreateContextExt( format, Int_val(depthBits), Int_val(stencilBits), Int_val(accumBits), sharelist ); #else caml_failwith("function OSMesaCreateContextExt not available"); #endif if (!ctx) { caml_failwith("osMesaCreateContextExt"); } return (value) ctx; }
/** * Create an Off-Screen Mesa rendering context. The only attribute needed is * an RGBA vs Color-Index mode flag. * * Input: format - Must be GL_RGBA * sharelist - specifies another OSMesaContext with which to share * display lists. NULL indicates no sharing. * Return: an OSMesaContext or 0 if error */ GLAPI OSMesaContext GLAPIENTRY OSMesaCreateContext( GLenum format, OSMesaContext sharelist ) { return OSMesaCreateContextExt(format, DEFAULT_SOFTWARE_DEPTH_BITS, 8, 0, sharelist); }
float* renderDepth(Mesh3D* model, float* projection, int render_width, int render_height){ unsigned char * pbufferRGB = new unsigned char [3 * render_width * render_height]; float m_near = 0.1; // 0.3; float m_far = 1e8; //float m_far = 10; //1e8; //int m_level = 0; // Step 1: setup off-screen mesa's binding OSMesaContext ctx = OSMesaCreateContextExt(OSMESA_RGB, 32, 0, 0, NULL ); // Bind the buffer to the context and make it current if (!OSMesaMakeCurrent(ctx, (void*)pbufferRGB, GL_UNSIGNED_BYTE, render_width, render_height)) { std::cerr << "OSMesaMakeCurrent failed!: " << render_width << ' ' << render_height << std::endl; return NULL; } OSMesaPixelStore(OSMESA_Y_UP, 1); /* GLint vup; OSMesaGetIntegerv(OSMESA_Y_UP, &vup); std::cout<<"OSMESA_Y_UP="<<vup<<std::endl; */ // Step 2: Setup basic OpenGL setting glEnable(GL_DEPTH_TEST); glDisable(GL_LIGHTING); glDisable(GL_CULL_FACE); //glEnable(GL_CULL_FACE); //glCullFace(GL_BACK); glPolygonMode(GL_FRONT, GL_FILL); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //glClearColor(m_clearColor[0], m_clearColor[1], m_clearColor[2], 1.0f); // this line seems useless glViewport(0, 0, render_width, render_height); // Step 3: Set projection matrices //double scale = (0x0001) << m_level; double final_matrix[16]; // new way: faster way by reuse computation and symbolic derive. See sym_derive.m to check the math. //double inv_width_scale = 1.0/(m_width*scale); // double inv_height_scale = 1.0/(m_height*scale); //double inv_width_scale_1 =inv_width_scale - 1.0; //double inv_height_scale_1_s = -(inv_height_scale - 1.0); //double inv_width_scale_2 = inv_width_scale*2.0; //double inv_height_scale_2_s = -inv_height_scale*2.0; double cx_rgb = render_width/2; double cy_rgb = render_height/2; double inv_width_scale_1 = 1.0 - 4 * cx_rgb / render_width; double inv_height_scale_1_s = -1.0 + 4 * cy_rgb / render_height; double inv_width_scale_2 = 2.0 / render_width; double inv_height_scale_2_s = -2.0 / render_height; double m_far_a_m_near = m_far + m_near; double m_far_s_m_near = m_far - m_near; double m_far_d_m_near = m_far_a_m_near/m_far_s_m_near; final_matrix[ 0]= projection[2+0*3]*inv_width_scale_1 + projection[0+0*3]*inv_width_scale_2; final_matrix[ 1]= projection[2+0*3]*inv_height_scale_1_s + projection[1+0*3]*inv_height_scale_2_s; final_matrix[ 2]= projection[2+0*3]*m_far_d_m_near; final_matrix[ 3]= projection[2+0*3]; final_matrix[ 4]= projection[2+1*3]*inv_width_scale_1 + projection[0+1*3]*inv_width_scale_2; final_matrix[ 5]= projection[2+1*3]*inv_height_scale_1_s + projection[1+1*3]*inv_height_scale_2_s; final_matrix[ 6]= projection[2+1*3]*m_far_d_m_near; final_matrix[ 7]= projection[2+1*3]; final_matrix[ 8]= projection[2+2*3]*inv_width_scale_1 + projection[0+2*3]*inv_width_scale_2; final_matrix[ 9]= projection[2+2*3]*inv_height_scale_1_s + projection[1+2*3]*inv_height_scale_2_s; final_matrix[10]= projection[2+2*3]*m_far_d_m_near; final_matrix[11]= projection[2+2*3]; final_matrix[12]= projection[2+3*3]*inv_width_scale_1 + projection[0+3*3]*inv_width_scale_2; final_matrix[13]= projection[2+3*3]*inv_height_scale_1_s + projection[1+3*3]*inv_height_scale_2_s; final_matrix[14]= projection[2+3*3]*m_far_d_m_near - (2*m_far*m_near)/m_far_s_m_near; final_matrix[15]= projection[2+3*3]; /* // new way: faster way by reuse computation and symbolic derive. See sym_derive.m to check the math. double inv_width_scale = 1.0/(render_width*scale); double inv_height_scale = 1.0/(render_height*scale); double inv_width_scale_1 =inv_width_scale - 1.0; double inv_height_scale_1_s = -(inv_height_scale - 1.0); double inv_width_scale_2 = inv_width_scale*2.0; double inv_height_scale_2_s = -inv_height_scale*2.0; double m_far_a_m_near = m_far + m_near; double m_far_s_m_near = m_far - m_near; double m_far_d_m_near = m_far_a_m_near/m_far_s_m_near; final_matrix[ 0]= projection[2+0*3]*inv_width_scale_1 + projection[0+0*3]*inv_width_scale_2; final_matrix[ 1]= projection[2+0*3]*inv_height_scale_1_s + projection[1+0*3]*inv_height_scale_2_s; final_matrix[ 2]= projection[2+0*3]*m_far_d_m_near; final_matrix[ 3]= projection[2+0*3]; final_matrix[ 4]= projection[2+1*3]*inv_width_scale_1 + projection[0+1*3]*inv_width_scale_2; final_matrix[ 5]= projection[2+1*3]*inv_height_scale_1_s + projection[1+1*3]*inv_height_scale_2_s; final_matrix[ 6]= projection[2+1*3]*m_far_d_m_near; final_matrix[ 7]= projection[2+1*3]; final_matrix[ 8]= projection[2+2*3]*inv_width_scale_1 + projection[0+2*3]*inv_width_scale_2; final_matrix[ 9]= projection[2+2*3]*inv_height_scale_1_s + projection[1+2*3]*inv_height_scale_2_s; final_matrix[10]= projection[2+2*3]*m_far_d_m_near; final_matrix[11]= projection[2+2*3]; final_matrix[12]= projection[2+3*3]*inv_width_scale_1 + projection[0+3*3]*inv_width_scale_2; final_matrix[13]= projection[2+3*3]*inv_height_scale_1_s + projection[1+3*3]*inv_height_scale_2_s; final_matrix[14]= projection[2+3*3]*m_far_d_m_near - (2*m_far*m_near)/m_far_s_m_near; final_matrix[15]= projection[2+3*3]; */ // matrix is ready. use it glMatrixMode(GL_PROJECTION); glLoadMatrixd(final_matrix); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // Step 3: render the mesh for (unsigned int i = 0; i < model->face.size() ; ++i) { glBegin(GL_POLYGON); for (unsigned int j=0; j < model->face[i].size(); ++j){ int vi = model->face[i][j]; glVertex3f(model->vertex[vi].x, model->vertex[vi].y, model->vertex[vi].z); } glEnd(); } glFinish(); // done rendering //////////////////////////////////////////////////////////////////////////////// unsigned int* pDepthBuffer; GLint outWidth, outHeight, bitPerDepth; OSMesaGetDepthBuffer(ctx, &outWidth, &outHeight, &bitPerDepth, (void**)&pDepthBuffer); float* pbufferD = new float[outWidth*outHeight]; for(int i=0; i<outWidth*outHeight; i++){ pbufferD[i] = float( m_near / (1.0 - double(pDepthBuffer[i])/(4294967296.0)) ); //double d = double(pDepthBuffer[i])/4294967296.0; //pbufferD[i] = float( 1.0 / ( ( m_far / (m_far - m_near) ) - d * ( - (m_near * m_far) / (m_far - m_near) ) ) ); } OSMesaDestroyContext(ctx); delete [] pbufferRGB; return pbufferD; }
GLFWbool _glfwCreateContextOSMesa(_GLFWwindow* window, const _GLFWctxconfig* ctxconfig, const _GLFWfbconfig* fbconfig) { OSMesaContext share = NULL; const int accumBits = fbconfig->accumRedBits + fbconfig->accumGreenBits + fbconfig->accumBlueBits + fbconfig->accumAlphaBits; if (ctxconfig->client == GLFW_OPENGL_ES_API) { _glfwInputError(GLFW_API_UNAVAILABLE, "OSMesa: OpenGL ES is not available on OSMesa"); return GLFW_FALSE; } if (ctxconfig->share) share = ctxconfig->share->context.osmesa.handle; if (OSMesaCreateContextAttribs) { int index = 0, attribs[40]; setAttrib(OSMESA_FORMAT, OSMESA_RGBA); setAttrib(OSMESA_DEPTH_BITS, fbconfig->depthBits); setAttrib(OSMESA_STENCIL_BITS, fbconfig->stencilBits); setAttrib(OSMESA_ACCUM_BITS, accumBits); if (ctxconfig->profile == GLFW_OPENGL_CORE_PROFILE) { setAttrib(OSMESA_PROFILE, OSMESA_CORE_PROFILE); } else if (ctxconfig->profile == GLFW_OPENGL_COMPAT_PROFILE) { setAttrib(OSMESA_PROFILE, OSMESA_COMPAT_PROFILE); } if (ctxconfig->major != 1 || ctxconfig->minor != 0) { setAttrib(OSMESA_CONTEXT_MAJOR_VERSION, ctxconfig->major); setAttrib(OSMESA_CONTEXT_MINOR_VERSION, ctxconfig->minor); } if (ctxconfig->forward) { _glfwInputError(GLFW_VERSION_UNAVAILABLE, "OSMesa: Foward-compatible contexts not supported"); return GLFW_FALSE; } setAttrib(0, 0); window->context.osmesa.handle = OSMesaCreateContextAttribs(attribs, share); } else { if (ctxconfig->profile) { _glfwInputError(GLFW_VERSION_UNAVAILABLE, "OSMesa: OpenGL profiles unavailable"); return GLFW_FALSE; } window->context.osmesa.handle = OSMesaCreateContextExt(OSMESA_RGBA, fbconfig->depthBits, fbconfig->stencilBits, accumBits, share); } if (window->context.osmesa.handle == NULL) { _glfwInputError(GLFW_VERSION_UNAVAILABLE, "OSMesa: Failed to create context"); return GLFW_FALSE; } window->context.makeCurrent = makeContextCurrentOSMesa; window->context.swapBuffers = swapBuffersOSMesa; window->context.swapInterval = swapIntervalOSMesa; window->context.extensionSupported = extensionSupportedOSMesa; window->context.getProcAddress = getProcAddressOSMesa; window->context.destroy = destroyContextOSMesa; return GLFW_TRUE; }
int main(int argc, char *argv[]) { int Width = 384; int Height = 384; OSMesaContext ctx; void *buffer; char *filename = NULL; int ev; int repeat=1; /* Create an RGBA-mode context */ /* specify Z, stencil, accum sizes */ ctx = OSMesaCreateContextExt( OSMESA_BGRA, 16, 0, 0, NULL ); if (!ctx) { printf("OSMesaCreateContext failed!\n"); return 0; } /* Allocate the image buffer */ buffer = malloc( Width * Height * 4 * sizeof(GLubyte) ); if (!buffer) { printf("Alloc image buffer failed!\n"); return 0; } /* Bind the buffer to the context and make it current */ if (!OSMesaMakeCurrent( ctx, buffer, GL_UNSIGNED_BYTE, Width, Height )) { printf("OSMesaMakeCurrent failed!\n"); return 0; } { int z, s, a; glGetIntegerv(GL_DEPTH_BITS, &z); glGetIntegerv(GL_STENCIL_BITS, &s); glGetIntegerv(GL_ACCUM_RED_BITS, &a); printf("Depth=%d Stencil=%d Accum=%d\n", z, s, a); } reshape(Width, Height); glClearColor( 0, 0, 0, 1); init(); draw(); DrawWindow(10, 10, Width+TYPE_3_BORDER_WIDTH*2, Height+TYPE_3_BORDER_WIDTH+get_skin_height(), "OpenGL Engine Demo", 0x000000, 0x74); Blit(buffer, TYPE_3_BORDER_WIDTH, get_skin_height(), 0, 0, Width, Height, Width,Height,Width*4); int start = time_now(); int frames = 0; while(repeat) { int now = time_now(); oskey_t key; ev = check_os_event(); switch(ev) { case 1: DrawWindow(0,0,0,0, NULL, 0x000000,0x74); break; case 2: key = get_key(); keyboard(key.code); break; case 3: if(get_os_button()==1) repeat=0; continue; }; if (now - start >= 5000) { double elapsed = (double) (now - start) / 1000.0; printf("%d frames in %3.1f seconds = %6.3f FPS\n", frames, elapsed, frames / elapsed); fflush(stdout); start = now; frames = 0; } idle(); draw(); glFlush(); Blit(buffer, TYPE_3_BORDER_WIDTH, get_skin_height(), 0, 0, Width, Height, Width,Height,Width*4); frames++; }; /* free the image buffer */ free( buffer ); /* destroy the context */ OSMesaDestroyContext( ctx ); return 0; }
/** * Create an Off-Screen Mesa rendering context. The only attribute needed is * an RGBA vs Color-Index mode flag. * * Input: format - Must be GL_RGBA * sharelist - specifies another OSMesaContext with which to share * display lists. NULL indicates no sharing. * Return: an OSMesaContext or 0 if error */ GLAPI OSMesaContext GLAPIENTRY OSMesaCreateContext(GLenum format, OSMesaContext sharelist) { return OSMesaCreateContextExt(format, 24, 8, 0, sharelist); }
int main(int argc, char *argv[]) { OSMesaContext ctx; void *buffer; if (argc < 3) { fprintf(stderr, "Usage:\n"); fprintf(stderr, " render modelname pngname [width height] [camx camy camz] [centerx centerz centerz] [upx upy upz] [fovy]\n"); fprintf(stderr, "Default: width=%d height=%d cam=[%0.4f %0.4f %0.4f] center=[%0.4f %0.4f %0.4f] up=[%0.4f %0.4f %0.4f] fovy=%0.4f\n", Width, Height, camx, camy, camz, centerx, centery, centerz, upx, upy, upz, fovy); return 0; } modelname = argv[1]; pngname = argv[2]; if (argc >= 5) { Width = atoi(argv[3]); Height = atoi(argv[4]); } if (argc >= 8) { camx = atoi(argv[5]); camy = atoi(argv[6]); camz = atoi(argv[7]); } if (argc >= 11) { centerx = atoi(argv[8]); centery = atoi(argv[9]); centerz = atoi(argv[10]); } if (argc >= 14) { upx = atoi(argv[11]); upy = atoi(argv[12]); upz = atoi(argv[13]); } if (argc >= 15) { fovy = atoi(argv[14]); } if (!Import3DFromFile(modelname)) { fprintf(stderr, "model cannot be loaded!\n"); return 0; } /* Create an RGBA-mode context */ #if OSMESA_MAJOR_VERSION * 100 + OSMESA_MINOR_VERSION >= 305 /* specify Z, stencil, accum sizes */ ctx = OSMesaCreateContextExt( OSMESA_RGBA, 16, 0, 0, NULL ); #else ctx = OSMesaCreateContext( OSMESA_RGBA, NULL ); #endif if (!ctx) { printf("OSMesaCreateContext failed!\n"); return 0; } /* Allocate the image buffer */ buffer = malloc( Width * Height * 4 * sizeof(GLubyte) ); if (!buffer) { printf("Alloc image buffer failed!\n"); return 0; } /* Bind the buffer to the context and make it current */ if (!OSMesaMakeCurrent( ctx, buffer, GL_UNSIGNED_BYTE, Width, Height )) { printf("OSMesaMakeCurrent failed!\n"); return 0; } { int z, s, a; glGetIntegerv(GL_DEPTH_BITS, &z); glGetIntegerv(GL_STENCIL_BITS, &s); glGetIntegerv(GL_ACCUM_RED_BITS, &a); printf("Depth=%d Stencil=%d Accum=%d\n", z, s, a); } InitGL(Width, Height); render_image(); if (pngname != NULL) { png::image< png::rgba_pixel > image(Width, Height); GLubyte * p_buffer = (GLubyte*)buffer; for (png::uint_32 y = 0; y < Height; ++y) { for (png::uint_32 x = 0; x < Width; ++x) { png::uint_32 r, g, b, a; r = *(p_buffer++); g = *(p_buffer++); b = *(p_buffer++); a = *(p_buffer++); image[Height-1-y][x] = png::rgba_pixel(r, g, b, a); } } image.write(pngname); } else { printf("Specify a filename if you want to make an image file\n"); } printf("all done\n"); /* free the image buffer */ free( buffer ); // *** cleanup *** textureIdMap.clear(); //no need to delete pointers in it manually here. (Pointers point to textureIds deleted in next step) textureName.clear(); //no need to delete pointers in it manually here. (Pointers point to textureIds deleted in next step) if (textureIds) { delete[] textureIds; textureIds = NULL; } /* destroy the context */ OSMesaDestroyContext( ctx ); return 0; }
int main(int argc, char* argv[]) { if (argc < 3) { printf("Usage: ./zbuffer input.ply [output.pcd,outputFolder]\n"); return 1; } std::vector<Point> vertices; std::vector<Triangle> faces; std::vector<Point> pointcloud; char buffer[128]; bool merge = opendir(argv[2]) == NULL; readPLY(argv[1],&vertices,&faces); //get bounding box double minX=vertices[0].x,maxX=vertices[0].x; double minY=vertices[0].y,maxY=vertices[0].y; double minZ=vertices[0].z,maxZ=vertices[0].z; for (size_t i=1; i<vertices.size(); i++) { if (vertices[i].x < minX) minX = vertices[i].x; else if (vertices[i].x > maxX) maxX = vertices[i].x; if (vertices[i].y < minY) minY = vertices[i].y; else if (vertices[i].y > maxY) maxY = vertices[i].y; if (vertices[i].z < minZ) minZ = vertices[i].z; else if (vertices[i].z > maxZ) maxZ = vertices[i].z; } printf("Bounding box: x:(%.2f %.2f) y:(%.2f %.2f) z:(%.2f %.2f)\n",minX,maxX,minY,maxY,minZ,maxZ); Point centroid = { (minX + maxX) / 2, (minY + maxY) / 2, (minZ + maxZ) / 2 }; for (size_t i = 0; i < vertices.size(); i++) { vertices[i].x -= centroid.x; vertices[i].y -= centroid.y; vertices[i].z -= centroid.z; } int width = RESOLUTION; int height = RESOLUTION; OSMesaContext ctx; ctx = OSMesaCreateContextExt(OSMESA_RGB, 32, 0, 0, NULL ); unsigned char * pbuffer = new unsigned char [3 * width * height]; // Bind the buffer to the context and make it current if (!OSMesaMakeCurrent(ctx, (void*)pbuffer, GL_UNSIGNED_BYTE, width, height)) printf("fail to create MESA context\n"); OSMesaPixelStore(OSMESA_Y_UP, 0); glEnable(GL_DEPTH_TEST); glDisable(GL_LIGHTING); glDisable(GL_CULL_FACE); glPolygonMode(GL_FRONT, GL_FILL); glMatrixMode(GL_PROJECTION); glLoadIdentity(); float fov = 70; float fov_scale = 2 * tan(fov / 2 / 180 * M_PI); float zfar = 100000; gluPerspective(fov,1,1,zfar); glViewport(0, 0, width, height); float cameraX = maxX - minX; float cameraY = maxY - minY; float cameraZ = maxZ - minZ; float cx = 0.5 * (width + 1); float cy = 0.5 * (height + 1); unsigned int* depth = new unsigned int[width * height]; float rho = sqrt(cameraX*cameraX + cameraY*cameraY); float theta = atan2(cameraY, cameraX); int depthBits=0; glGetIntegerv(GL_DEPTH_BITS, &depthBits); printf("depth buffer bits %d\n",depthBits); int numViews = INCLUDE_TOP ? NUM_CAMERAS + 2 : NUM_CAMERAS; for (int k = 0; k < numViews; k++) { // if (!merge) // pointcloud.clear(); float rx = rho * cos(theta); float ry = rho * sin(theta); theta += 2 * 3.14159265 / NUM_CAMERAS; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); if (k < NUM_CAMERAS) gluLookAt(rx,ry, cameraZ, 0,0,0, 0,0,1); else if (k == NUM_CAMERAS) gluLookAt(0,0, cameraZ*4, 0,0,0, 1,0,0); else if (k == NUM_CAMERAS + 1) gluLookAt(0,0, -cameraZ*4, 0,0,0, 1,0,0); GLfloat R[16] = {}; glGetFloatv(GL_MODELVIEW_MATRIX, R); // printf("%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n",R[0],R[1],R[2],R[3],R[4],R[5],R[6],R[7],R[8],R[9],R[10],R[11],R[12],R[13],R[14],R[15]); // printf("camera: %f %f %f\n",rx,ry,cameraZ); glBegin(GL_TRIANGLES); glColor3ub(150, 150, 150); for (size_t i = 0; i < faces.size(); i++) { Point p1 = vertices[faces[i].id1]; Point p2 = vertices[faces[i].id2]; Point p3 = vertices[faces[i].id3]; glVertex3f(p1.x, p1.y, p1.z); glVertex3f(p2.x, p2.y, p2.z); glVertex3f(p3.x, p3.y, p3.z); } glEnd(); glFinish(); // done rendering GLint outWidth, outHeight, bitPerDepth; GLboolean ret = OSMesaGetDepthBuffer(ctx, &outWidth, &outHeight, &bitPerDepth, (void**)&depth); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { float buffer_z = (float) depth[j * width + i] / 0xFFFFFFFF; if (buffer_z > 0 && buffer_z < 1) { // float z = -1 / (1 - buffer_z); float z = 1 / (buffer_z - 1 - buffer_z / zfar); float x = (i - cx) * -z / width * fov_scale; float y = (j - cy) * -z / height * fov_scale; x -= R[12]; y -= R[13]; z -= R[14]; Point p = { R[0] * x + R[1] * y + R[2] * z, R[4] * x + R[5] * y + R[6] * z, R[8] * x + R[9] * y + R[10] * z }; pointcloud.push_back(p); } } } // printf("pointcloud: %lu\n",pointcloud.size()); if (!merge && pointcloud.size() > 0) { int n=0; while (true) { sprintf(buffer,"%s/%d-cloud.pcd",argv[2],n); FILE* f = fopen(buffer,"r"); if (!f) { writeToPCD(buffer,&pointcloud); break; } fclose(f); n++; } } } if (merge && pointcloud.size() > 0) { sprintf(buffer,"%s",argv[2]); writeToPCD(buffer,&pointcloud); } // sprintf(buffer,"%s/vertex.pcd",argv[2]); // writeToPCD(buffer,&vertices); OSMesaDestroyContext(ctx); return 0; }
static int setup(int width, int height) { #if USE_OSMESA const size_t size = width * height * 4; pbuffer = malloc(size); memset(pbuffer, 0x21, size); context = OSMesaCreateContextExt(GL_RGBA, 24, 0, 0, 0); OSMesaMakeCurrent(context, (void *)pbuffer, GL_UNSIGNED_BYTE, width, height); return 0; #elif USE_CGL /* OpenGL PBuffer initialization: OSX specific */ CGLPixelFormatAttribute pfattr[] = { kCGLPFAPBuffer, (CGLPixelFormatAttribute)0 }; CGLPixelFormatObj pixformat; GLint npixels; int e; e = CGLChoosePixelFormat(pfattr, &pixformat, &npixels); if (e != kCGLNoError) { fprintf(stderr, "CGLChoosePixelFormat failed, err %d\n", e); return e; } e = CGLCreateContext(pixformat, 0, &context); if (e != kCGLNoError) { fprintf(stderr, "CGLChoosePixelFormat failed, err %d\n", e); return e; } e = CGLDestroyPixelFormat(pixformat); if (e != kCGLNoError) { fprintf(stderr, "CGLDestroyPixelFormat failed, err %d\n", e); return e; } e = CGLSetCurrentContext(context); if (e != kCGLNoError) { fprintf(stderr, "CGLSetCurrentContext failed, err %d\n", e); return e; } e = CGLCreatePBuffer(width, height, GL_TEXTURE_2D, GL_RGB, 0, &pbuffer); if (e != kCGLNoError) { fprintf(stderr, "CGLCreatePBuffer failed, err %d\n", e); return e; } e = CGLSetPBuffer(context, pbuffer, 0, 0, 0); if (e != kCGLNoError) { fprintf(stderr, "CGLSetPBuffer failed, err %d\n", e); return e; } return (int)kCGLNoError; #elif USE_GLX int result = (-1); char *glxversion; int screen; GLXFBConfig *fbConfigs = NULL; GLXFBConfig chosenFBConfig; GLXFBConfig fbconfig = 0; GLXPbuffer pbuffer = None; int nConfigs; int fbconfigid; int fbAttribs[] = { GLX_RENDER_TYPE, GLX_RGBA_BIT, GLX_DEPTH_SIZE, 1, GLX_DRAWABLE_TYPE, GLX_PIXMAP_BIT | GLX_PBUFFER_BIT, None }; int pbAttribs[] = { GLX_PBUFFER_WIDTH, 0, GLX_PBUFFER_HEIGHT, 0, GLX_LARGEST_PBUFFER, False, GLX_PRESERVED_CONTENTS, False, None }; /* Open the X display */ display = XOpenDisplay(NULL); if (!display) { fprintf(stderr, "Error: couldn't open default X display.\n"); goto end; } /* Get default screen */ screen = DefaultScreen(display); glxversion = (char *) glXGetClientString(display, GLX_VERSION); if (!(strstr(glxversion, "1.3") || strstr(glxversion, "1.4"))) { goto end; } glxversion = (char *) glXQueryServerString(display, screen, GLX_VERSION); if (!(strstr(glxversion, "1.3") || strstr(glxversion, "1.4"))) { goto end; } /* Create Pbuffer */ pbAttribs[1] = width; pbAttribs[3] = height; fbConfigs = glXChooseFBConfig(display, screen, fbAttribs, &nConfigs); if (0 == nConfigs || !fbConfigs) { fprintf(stderr, "Error: glxChooseFBConfig failed\n"); XFree(fbConfigs); goto end; } chosenFBConfig = fbConfigs[0]; glXGetFBConfigAttrib(display, chosenFBConfig, GLX_FBCONFIG_ID, &fbconfigid); /* Create the pbuffer using first fbConfig in the list that works. */ pbuffer = glXCreatePbuffer(display, chosenFBConfig, pbAttribs); if (pbuffer) { fbconfig = chosenFBConfig; } XFree(fbConfigs); if (pbuffer == None) { fprintf(stderr, "Error: couldn't create pbuffer\n"); goto end; } /* Create GLX context */ context = glXCreateNewContext(display, fbconfig, GLX_RGBA_TYPE, NULL, True); if (!context) { fprintf(stderr, "Error: Couldn't create GLXContext\n"); goto end; } /* Bind context to pbuffer */ if (!glXMakeCurrent(display, pbuffer, context)) { fprintf(stderr, "Error: glXMakeCurrent failed\n"); goto end; } result = 0; end: if (fbConfigs) XFree(fbConfigs); if (display) XCloseDisplay(display); return result; #else /* TODO: pbuffer initialization */ return 0; #endif }
int main( int argc, char *argv[] ) { void *buffer; int i; char *filename = NULL; /* Create an RGBA-mode context */ #if OSMESA_MAJOR_VERSION * 100 + OSMESA_MINOR_VERSION >= 305 /* specify Z, stencil, accum sizes */ OSMesaContext ctx = OSMesaCreateContextExt( OSMESA_RGBA, 16, 0, 0, NULL ); #else OSMesaContext ctx = OSMesaCreateContext( OSMESA_RGBA, NULL ); #endif if (!ctx) { printf("OSMesaCreateContext failed!\n"); return 0; } for ( i=1; i<argc; i++ ) { if (argv[i][0] != '-') filename = argv[i]; } /* Allocate the image buffer */ buffer = malloc( WIDTH * HEIGHT * 4 * sizeof(GLubyte) ); if (!buffer) { printf("Alloc image buffer failed!\n"); return 0; } /* Bind the buffer to the context and make it current */ if (!OSMesaMakeCurrent( ctx, buffer, GL_UNSIGNED_BYTE, WIDTH, HEIGHT )) { printf("OSMesaMakeCurrent failed!\n"); return 0; } { int z, s, a; glGetIntegerv(GL_DEPTH_BITS, &z); glGetIntegerv(GL_STENCIL_BITS, &s); glGetIntegerv(GL_ACCUM_RED_BITS, &a); printf("Depth=%d Stencil=%d Accum=%d\n", z, s, a); } InitGL(); RenderImage(); if (filename != NULL) { write_ppm(filename, (GLubyte*)buffer, WIDTH, HEIGHT); } else { printf("Specify a filename (with ppm extension) if you want to make an image file\n"); } printf("all done\n"); /* free the image buffer */ free( buffer ); /* destroy the context */ OSMesaDestroyContext( ctx ); return 0; }