OpenGL offscreen render

穿精又带淫゛_ 提交于 2019-12-01 14:39:00

I just had a look into the source code I did for Windows. As it was a study for productive code (and hence uses other stuff of our productive code) I cannot provide it as is. What I present here is a stripped version which should show how it works:

// standard C/C++ header:
#include <iostream>

// Windows header:
#include <Windows.h>

using namespace std;

int main(int argc, char **argv)
{
  if (argc < 3) {
    cerr << "USAGE: " << argv[0]
      << " FILE [FILES...] IMG_FILE" << endl;
    return -1;
  }
  // Import Scene Graph
  // excluded: initialize importers
  // excluded: import 3d files
#ifdef _WIN32
  // Window Setup
  // set window properties
  enum { Width = 1024, Height = 768 };
  WNDCLASSEX wndClass; memset(&wndClass, 0, sizeof wndClass);
  wndClass.cbSize = sizeof(WNDCLASSEX);
  wndClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;
  wndClass.lpfnWndProc = &DefWindowProc;
  wndClass.cbClsExtra = 0;
  wndClass.cbWndExtra = 0;
  wndClass.hInstance = 0;
  wndClass.hIcon = 0;
  wndClass.hCursor = LoadCursor(0, IDC_ARROW);
  wndClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  wndClass.lpszMenuName = 0;
  wndClass.lpszClassName = "WndClass";
  wndClass.hIconSm = 0;
  RegisterClassEx(&wndClass);
  // style the window and remove the caption bar (WS_POPUP)
  DWORD style = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_POPUP;
  // Create the window. Position and size it.
  HWND hwnd = CreateWindowEx(0,
    "WndClass",
    "",
    style,
    CW_USEDEFAULT, CW_USEDEFAULT, Width, Height,
    0, 0, 0, 0);
  HDC hdc = GetDC(hwnd);
  // Windows OpenGL Setup
  PIXELFORMATDESCRIPTOR pfd; memset(&pfd, 0, sizeof pfd);
  pfd.nSize = sizeof(pfd);
  pfd.nVersion = 1;
  pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  pfd.iPixelType = PFD_TYPE_RGBA;
  pfd.cColorBits = 32;
  pfd.cDepthBits = 16;
  pfd.cStencilBits = 8;
  pfd.iLayerType = PFD_MAIN_PLANE;
  // get the best available match of pixel format for the device context
  int iPixelFormat = ChoosePixelFormat(hdc, &pfd);
  // make that the pixel format of the device context 
  SetPixelFormat(hdc, iPixelFormat, &pfd);
  // create the context
  HGLRC hGLRC = wglCreateContext(hdc);
  wglMakeCurrent(hdc, hGLRC);
#endif // _WIN32
  // OpenGL Rendering Setup
  /* excluded: init our private OpenGL binding as
   * the Microsoft API for OpenGL is stuck <= OpenGL 2.0
   */
  // create Render Buffer Object (RBO) for colors
  GLuint rboColor = 0;
  glGenRenderbuffers(1, &rboColor);
  glBindRenderbuffer(GL_RENDERBUFFER, rboColor);
  glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, Width, Height);
  glBindRenderbuffer(GL_RENDERBUFFER, 0);
  // create Render Buffer Object (RBO) for depth
  GLuint rboDepth = 0;
  glGenRenderbuffers(1, &rboDepth);
  glBindRenderbuffer(GL_RENDERBUFFER, rboDepth);
  glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, Width, Height);
  glBindRenderbuffer(GL_RENDERBUFFER, 0);
  // create Frame Buffer Object (FBO)
  GLuint fbo = 0;
  glGenFramebuffers(1, &fbo);
  glBindFramebuffer(GL_FRAMEBUFFER, fbo);
  // attach RBO to FBO
  glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
    GL_RENDERBUFFER, rboColor);
  glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
    GL_RENDERBUFFER, rboDepth);
  // GL Rendering Setup
  // excluded: prepare our GL renderer
  glViewport(0, 0, Width, Height);
  glClearColor(0.525f, 0.733f, 0.851f, 1.0f);
  glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  /* compute projection matrix from
   * - field of view (property fov)
   * - aspect ratio of view
   * - near/far clip distance (properties dNear and dFar).
   */
  const DegreeD fov(30.0);
  const double dNear = 0.1, dFar = 100.0;
  const double ar = (float)Width / Height;
  const double d = ::tan(fov / 2.0) * 2.0 * dNear;
  // excluded: construct a projection matrix for perspective view
  // excluded: determine bounding sphere of 3D scene
  // excluded: compute camera and view matrix from the bounding sphere of scene
  // excluded: OpenGL rendering of 3d scene
  // read image from render buffer
  // excluded: prepare image object to store read-back
  //Image::Object img(4, Image::BottomToTop);
  //img.set(Width, Height, Image::RGB24);
  //const size_t bytesPerLine = (3 * Width * 4 + 3) / 4;
  //glReadPixels(0, 0, Width, Height, GL_RGB, GL_UNSIGNED_BYTE, img.getData());
  // store image
  const string filePath = argv[argc - 1];
  // excluded: export image in a supported image file format
  // clean-up
  // excluded: clean-up of 3D scene (incl. OpenGL rendering add-ons)
  glDeleteFramebuffers(1, &fbo);
  glDeleteRenderbuffers(1, &rboColor);
  glDeleteRenderbuffers(1, &rboDepth);
#ifdef _WIN32
  wglMakeCurrent(NULL, NULL);
  wglDeleteContext(hGLRC);
#endif // _WIN32
  // done
  return 0;
}

I didn't check whether this even compiles (as is above). It is stripped out of code which compiles and run on Windows 10 on my side.


A Note about OpenGL and Windows:

I did the GL binding by myself because the Microsoft Windows OpenGL API does not support OpenGL 3.0 or higher. (I could've used a library like glfw instead.) This means I have to assign function addresses to function pointers (to correct function prototypes) so that I can call OpenGL functions properly using C function calls.

The availability of the functions is granted if I have appropriate H/W and the appropriate drivers installed. (There are possibilities to check whether the driver provides certain functions.)

If such bound function call fails (e.g. with a segmentation fault) possible reasons could be:

  1. The signature of called function is wrong. (I used headers downloaded from khronos.org to grant correct prototypes. Hopefully, the driver provider did as well.)

  2. The function does not exist in the driver. (I use functions which are part of the OpenGL standard which is supported by the installed driver. The driver supports OpenGL 4.x but I need only OpenGL 3.x (at least until now).)

  3. The function pointers have to be initialized before I use them. (I have written an initialization which is not exposed in the code. This is where I placed the comment /* excluded: init our private OpenGL binding as the Microsoft API for OpenGL is stuck <= OpenGL 2.0 */.

To illustrate this, some code examples:

In my OpenGL init function, I do:

  glGenFramebuffers
    = (PFNGLGENFRAMEBUFFERSPROC)wglGetProcAddress(
      "glGenFramebuffers");

and the header provides:

extern PFNGLGENFRAMEBUFFERSPROC glGenFramebuffers;

PFNGLGENFRAMEBUFFERSPROC is provided by glext.h I downloaded from kronos.org:

typedef void (APIENTRYP PFNGLGENFRAMEBUFFERSPROC) (GLsizei n, GLuint *framebuffers);

wglGetProcAddress() is provided by the Microsoft Windows API.

A Note about OpenGL and Linux:

If H/W and the installed driver supports the desired OpenGL standard, functions can be used as usual by

  1. including the necessary headers (e.g. #include <GL/gl.h>)

  2. linking the necessary libraries (e.g. -lGL -lGLU).


derhass commented:

There is absolutely no guarantee that GL 3.x functions are exported by whatever libGL.so one is using, and even if they are exported, there is no guarantee that the function is supported (i.e. mesa uses the same frontend lib for all driver backends, but each driver may only support a subset of the functions). You have to use the extension mechanism on both platforms.

I'm not able to provide a simple recommendation how to handle this nor I've valuable practical experience about this. So, I want to provide at least these links (from khronos.org) I've found by google search:

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