Einzelnen Beitrag anzeigen

Benutzerbild von Jan
Jan

Registriert seit: 24. Sep 2002
Ort: Magdeburg
491 Beiträge
 
Delphi 7 Enterprise
 
#3

Re: (OpenGL) Texturen in PixelBuffer rendern

  Alt 30. Okt 2008, 19:04
draw:
Code:
void drawpBuffer()
{
   int flag = 0;
   wglQueryPbufferARB( g_pbuffer.hPBuffer, WGL_PBUFFER_LOST_ARB, &flag );

   if( flag != 0 )
   {
      exit(-1);
   }



   if( !wglReleaseTexImageARB( g_pbuffer.hPBuffer, WGL_FRONT_LEFT_ARB ) )
   {
      exit(-1);
   }

   
   if( !wglMakeCurrent( g_pbuffer.hDC, g_pbuffer.hRC ) )
   {
      exit(-1);
   }


   glMatrixMode(GL_MODELVIEW);
   glPushMatrix();
   glLoadIdentity();

   glMatrixMode(GL_PROJECTION);
   glPushMatrix();
   glLoadIdentity();
   xMatrix4D mOrtho = xMatrix4D_ortho(0,streetRes,0,streetRes, -1, 1);
   glMultMatrixf((float*)&mOrtho);
   glViewport(0, 0,streetRes, streetRes);


   glDisable(GL_DEPTH_TEST);

   //RENDER TO PBUFFER


   glClearColor(0.3, 0.7, 0.3, 0);
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

   

   glEnable(GL_TEXTURE_2D);
   glMatrixMode(GL_MODELVIEW);

    glBindTexture( GL_TEXTURE_2D, TexIDpng[10] );
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );

   glBegin(GL_QUADS);
   //quad with texture
   glColor4f(1,0,0,0.5);
   glNormal3f(0,0,1);
   glTexCoord2f(1,1);
   glVertex2f(0,0);
   glTexCoord2f(0,1);
   glVertex2f(3 ,0);
   glTexCoord2f(0,0);
   glVertex2f(3 ,-3);
   glTexCoord2f(1,0);
   glVertex2f(0,-3);

   glEnd();

   glDisable(GL_TEXTURE_2D);


   glMatrixMode(GL_PROJECTION);
   glPopMatrix();

   glMatrixMode(GL_MODELVIEW);
   glPopMatrix();

//QT window reactivate
   mainWindow->openGL->makeCurrent();



   glBindTexture( GL_TEXTURE_2D, pbufferTex );
   if( !wglBindTexImageARB( g_pbuffer.hPBuffer, WGL_FRONT_LEFT_ARB ) )
   {
      exit(-1);
   }

//cg shader update
   cgGLSetTextureParameter(myCgFragmentParam_TextureStreets,pbufferTex);
   cgGLEnableTextureParameter(myCgFragmentParam_TextureStreets);
}
und so sieht die Init aus:

Code:
void initpBuffer()
{
   initpb = true;
   GLuint PixelFormat;

   PIXELFORMATDESCRIPTOR pfd;
   memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));


   pfd.nSize     = sizeof(PIXELFORMATDESCRIPTOR);
   pfd.nVersion  = 1;
   pfd.dwFlags   = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
   pfd.iPixelType = PFD_TYPE_RGBA;
   pfd.cColorBits = 16;
   pfd.cDepthBits = 16;

   
   //PixelFormat = ChoosePixelFormat( g_hDC, &pfd );


   g_hDC = wglGetCurrentDC();
   PixelFormat = ChoosePixelFormat( g_hDC, &pfd );
   SetPixelFormat( g_hDC, PixelFormat, &pfd);
   g_hRC = wglCreateContext( g_hDC );
   wglMakeCurrent( g_hDC, g_hRC );



   wglGetExtensionsStringARB = (PFNWGLGETEXTENSIONSSTRINGARBPROC)wglGetProcAddress("wglGetExtensionsStringARB");
   char *ext = NULL;

   if( wglGetExtensionsStringARB )
      ext = (char*)wglGetExtensionsStringARB( wglGetCurrentDC() );
   else
   {
//       MessageBox(NULL,"Unable to get address for wglGetExtensionsStringARB!",
//          "ERROR",MB_OK|MB_ICONEXCLAMATION);
      exit(-1);
   }

   //
   // WGL_ARB_pbuffer
   //

   if( strstr( ext, "WGL_ARB_pbuffer" ) == NULL )
   {
      exit(-1);
   }
   else
   {
      wglCreatePbufferARB   = (PFNWGLCREATEPBUFFERARBPROC)wglGetProcAddress("wglCreatePbufferARB");
      wglGetPbufferDCARB    = (PFNWGLGETPBUFFERDCARBPROC)wglGetProcAddress("wglGetPbufferDCARB");
      wglReleasePbufferDCARB = (PFNWGLRELEASEPBUFFERDCARBPROC)wglGetProcAddress("wglReleasePbufferDCARB");
      wglDestroyPbufferARB  = (PFNWGLDESTROYPBUFFERARBPROC)wglGetProcAddress("wglDestroyPbufferARB");
      wglQueryPbufferARB    = (PFNWGLQUERYPBUFFERARBPROC)wglGetProcAddress("wglQueryPbufferARB");

      if( !wglCreatePbufferARB || !wglGetPbufferDCARB || !wglReleasePbufferDCARB ||
         !wglDestroyPbufferARB || !wglQueryPbufferARB )
      {
         exit(-1);
      }
   }

   //
   // WGL_ARB_pixel_format
   //

   if( strstr( ext, "WGL_ARB_pixel_format" ) == NULL )
   {
      return;
   }
   else
   {
      wglChoosePixelFormatARB = (PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB");

      if( !wglChoosePixelFormatARB )
      {
         exit(-1);
      }
   }

   //
   // WGL_ARB_render_texture
   //

   if( strstr( ext, "WGL_ARB_render_texture" ) == NULL )
   {
      exit(-1);
   }
   else
   {
      wglBindTexImageARB    = (PFNWGLBINDTEXIMAGEARBPROC)wglGetProcAddress("wglBindTexImageARB");
      wglReleaseTexImageARB = (PFNWGLRELEASETEXIMAGEARBPROC)wglGetProcAddress("wglReleaseTexImageARB");
      wglSetPbufferAttribARB = (PFNWGLSETPBUFFERATTRIBARBPROC)wglGetProcAddress("wglSetPbufferAttribARB");

      if( !wglBindTexImageARB || !wglReleaseTexImageARB || !wglSetPbufferAttribARB )
      {
         exit(-1);
      }
   }

   //-------------------------------------------------------------------------
   // Create a p-buffer for off-screen rendering.
   //-------------------------------------------------------------------------

   g_pbuffer.hPBuffer = NULL;
   g_pbuffer.nWidth  = streetRes;
   g_pbuffer.nHeight = streetRes;

   //
   // Define the minimum pixel format requirements we will need for our
   // p-buffer. A p-buffer is just like a frame buffer, it can have a depth
   // buffer associated with it and it can be double buffered.
   //

   int pf_attr[] =
   {
      WGL_SUPPORT_OPENGL_ARB, TRUE,      // P-buffer will be used with OpenGL
      WGL_DRAW_TO_PBUFFER_ARB, TRUE,     // Enable render to p-buffer
      WGL_BIND_TO_TEXTURE_RGBA_ARB, TRUE, // P-buffer will be used as a texture
      WGL_RED_BITS_ARB, 8,               // At least 8 bits for RED channel
      WGL_GREEN_BITS_ARB, 8,             // At least 8 bits for GREEN channel
      WGL_BLUE_BITS_ARB, 8,              // At least 8 bits for BLUE channel
      WGL_ALPHA_BITS_ARB, 8,             // At least 8 bits for ALPHA channel
      WGL_DEPTH_BITS_ARB, 16,            // At least 16 bits for depth buffer
      WGL_DOUBLE_BUFFER_ARB, FALSE,      // We don't require double buffering
      0                                   // Zero terminates the list
   };

   unsigned int count = 0;
   int pixelFormat;

   wglChoosePixelFormatARB( g_hDC,(const int*)pf_attr, NULL, 1, &pixelFormat, &count);

   if( count == 0 )
   {
      exit(-1);
   }

   //
   // Set some p-buffer attributes so that we can use this p-buffer as a
   // 2D RGBA texture target.
   //

   int pb_attr[] =
   {
      WGL_TEXTURE_FORMAT_ARB, WGL_TEXTURE_RGBA_ARB, // Our p-buffer will have a texture format of RGBA
      WGL_TEXTURE_TARGET_ARB, WGL_TEXTURE_2D_ARB,  // Of texture target will be GL_TEXTURE_2D
      0                                             // Zero terminates the list
   };

   //
   // Create the p-buffer...
   //

   g_pbuffer.hPBuffer = wglCreatePbufferARB( g_hDC, pixelFormat, g_pbuffer.nWidth, g_pbuffer.nHeight, pb_attr );
   g_pbuffer.hDC     = wglGetPbufferDCARB( g_pbuffer.hPBuffer );
   g_pbuffer.hRC     = wglCreateContext( g_pbuffer.hDC );

   if( !g_pbuffer.hPBuffer )
   {
      exit(-1);
   }

   int h;
   int w;
   wglQueryPbufferARB( g_pbuffer.hPBuffer, WGL_PBUFFER_WIDTH_ARB, &h );
   wglQueryPbufferARB( g_pbuffer.hPBuffer, WGL_PBUFFER_WIDTH_ARB, &w );

   if( h != g_pbuffer.nHeight || w != g_pbuffer.nWidth )
   {
      exit(-1);
   }

   //
   // We were successful in creating a p-buffer. We can now make its context
   // current and set it up just like we would a regular context
   // attached to a window.
   //

   if( !wglMakeCurrent( g_pbuffer.hDC, g_pbuffer.hRC ) )
   {
      exit(-1);
   }

   drawpBuffer();

}
Das ist der code der den pbuffer betrifft. Hoffe das hilft weiter.

mfg
Jan
Jan
Wenn die Sonne der Kultur tief steht, werfen auch kleine Gestalten lange Schatten.
  Mit Zitat antworten Zitat