⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rendertexture.cpp

📁 PDE simulator on GPU.
💻 CPP
📖 第 1 页 / 共 3 页
字号:
            iBindTarget    = WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGBA_NV;  
            iTextureFormat = WGL_TEXTURE_FLOAT_RGBA_NV; 
          }
          else
          {
            iBindTarget    = WGL_BIND_TO_TEXTURE_RGBA_ARB;  
            iTextureFormat = WGL_TEXTURE_RGBA_ARB; 
          }
        } 
        else if (iBBits > 0)
        { 
          if (bNVIDIA)
          {
            iBindTarget    = WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RGB_NV;  
            iTextureFormat = WGL_TEXTURE_FLOAT_RGB_NV; 
          }
          else
          {
            iBindTarget    = WGL_BIND_TO_TEXTURE_RGB_ARB;  
            iTextureFormat = WGL_TEXTURE_RGB_ARB; 
          }
        } 
        else if (iGBits > 0)
        { 
          if (bNVIDIA)
          {
            iBindTarget    = WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_RG_NV;  
            iTextureFormat = WGL_TEXTURE_FLOAT_RG_NV; 
          }
          else
          {
            iBindTarget    = WGL_BIND_TO_TEXTURE_RGB_ARB;  
            iTextureFormat = WGL_TEXTURE_RGB_ARB; 
          }
        } 
        else 
        { 
          if (bNVIDIA)
          {
            iBindTarget    = WGL_BIND_TO_TEXTURE_RECTANGLE_FLOAT_R_NV;  
            iTextureFormat = WGL_TEXTURE_FLOAT_R_NV; 
          }
          else
          {
            iBindTarget    = WGL_BIND_TO_TEXTURE_RGB_ARB;  
            iTextureFormat = WGL_TEXTURE_RGB_ARB; 
          }
        }
#else if defined(DEBUG) || defined(_DEBUG)
        printf("RenderTexture Error: Render-to-Texture not supported in Linux\n");
#endif    
      }
    }
    else
    {
      if (!_bRectangle)
      {
        _iTextureTarget = GL_TEXTURE_2D;
        glBindTexture(_iTextureTarget, _iTextureID);
            
        // We'll use clamp to edge as the default texture wrap mode for all tex types
        glTexParameteri( _iTextureTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri( _iTextureTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
        glTexParameteri( _iTextureTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
      
        if (bMipmap)
        {
          _bMipmap = true;
          glTexParameteri( _iTextureTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
        
          // Generate mipmap automatically if supported
          if (GLEW_SGIS_generate_mipmap)
          {
            glTexParameteri( _iTextureTarget, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
          }
          else
          {
            PrintExtensionError("GL_SGIS_generate_mipmap");
          }
        }
        else
        {
          glTexParameteri( _iTextureTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        }
      
        // Set anisotropic filter to the max ratio
        if (bAnisoFilter)
        {
          if (GLEW_EXT_texture_filter_anisotropic)
          {
            _bAnisoFilter = true;
            float rMaxAniso;
            glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &rMaxAniso);
            glTexParameterf( _iTextureTarget, GL_TEXTURE_MAX_ANISOTROPY_EXT, rMaxAniso);
          }
        }

        if (RT_COPY_TO_TEXTURE == _eUpdateMode)
        {
          GLuint iInternalFormat;
          GLuint iFormat;
          if (iABits > 0)
          {
            iInternalFormat = GL_RGBA8;
            iFormat = GL_RGBA;
          }
          else 
          {
            iInternalFormat = GL_RGB8;
            iFormat = GL_RGB;
          }
          // Allocate the texture image (but pass it no data for now).
          glTexImage2D(_iTextureTarget, 0, iInternalFormat, _iWidth, _iHeight, 
                       0, iFormat, GL_FLOAT, NULL);
        }
        else
        {
#ifdef _WIN32    
          iWGLTextureTarget = WGL_TEXTURE_2D_ARB; 
          if (iABits > 0) 
          { 
            iBindTarget    = WGL_BIND_TO_TEXTURE_RGBA_ARB;
            iTextureFormat = WGL_TEXTURE_RGBA_ARB;
          } 
          else 
          {
            iBindTarget    = WGL_BIND_TO_TEXTURE_RGB_ARB;  
            iTextureFormat = WGL_TEXTURE_RGB_ARB; 
          } 
#endif
        }
      } 
      else
      {
        if (!bNVIDIA)
        {
          fprintf(stderr, 
                  "RenderTexture Error: ATI textures must be power-of-two-dimensioned.\n");
          return false;
        }
        
        _iTextureTarget = GL_TEXTURE_RECTANGLE_NV;
        
        glBindTexture(_iTextureTarget, _iTextureID);
      
        glTexParameteri( _iTextureTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri( _iTextureTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
        glTexParameteri( _iTextureTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri( _iTextureTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        
        if (bMipmap) 
        { 

          fprintf(stderr, 
                  "RenderTexture Error: rectangle textures do not support mipmaps\n"); 
          return false; 
        } 

        if (RT_COPY_TO_TEXTURE == _eUpdateMode)
        {
          GLuint iInternalFormat;
          GLuint iFormat;
          if (iABits > 0)
          {
            iInternalFormat = GL_RGBA8;
            iFormat = GL_RGBA;
          }
          else 
          {
            iInternalFormat = GL_RGB8;
            iFormat = GL_RGB;
          }
          // Allocate the texture image (but pass it no data for now).
          glTexImage2D(_iTextureTarget, 0, iInternalFormat, _iWidth, _iHeight, 
                       0, iFormat, GL_FLOAT, NULL);
        }
        else
        {
#ifdef _WIN32
          iWGLTextureTarget = WGL_TEXTURE_RECTANGLE_NV; 
          if (iABits > 0) 
          { 
            iBindTarget    = WGL_BIND_TO_TEXTURE_RECTANGLE_RGBA_NV; 
            iTextureFormat = WGL_TEXTURE_RGBA_ARB; 
          } 
          else 
          { 
            iBindTarget   = WGL_BIND_TO_TEXTURE_RECTANGLE_RGB_NV; 
            iTextureFormat= WGL_TEXTURE_RGB_ARB; 
          }
#endif 
        }
      } 
    }
  }

  if (_bIsDepthTexture)
  {
    if (!bNVIDIA && _bRectangle)
    {
      fprintf(stderr, 
        "RenderTexture Error: ATI textures must be power-of-two-dimensioned.\n");
      return false;
    }

    if (!_iTextureTarget)
      _iTextureTarget = _bRectangle ? GL_TEXTURE_RECTANGLE_NV : GL_TEXTURE_2D;
    
    glBindTexture(_iTextureTarget, _iDepthTextureID);  
    
    // We'll use clamp to edge as the default texture wrap mode for all tex types
    glTexParameteri( _iTextureTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri( _iTextureTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
    glTexParameteri( _iTextureTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri( _iTextureTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    
    // user will need to set up hardware shadow mapping himself (using ARB_shadow)
        
    if (RT_COPY_TO_TEXTURE == _eUpdateMode)
    {
      // [Redge]
      if (_bHasArbDepthTexture) 
      {
        // Allocate the texture image (but pass it no data for now).
        glTexImage2D(_iTextureTarget, 0, GL_DEPTH_COMPONENT, _iWidth, _iHeight, 
                     0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
      } 
      else 
      {
        // allocate memory for depth texture
        // Since this is slow, we warn the user in debug mode. (above)
        _pPoorDepthTexture = new unsigned short[_iWidth * _iHeight];
        glTexImage2D(_iTextureTarget, 0, GL_LUMINANCE16, _iWidth, _iHeight, 
                   0, GL_LUMINANCE, GL_UNSIGNED_SHORT, _pPoorDepthTexture);
      }
      // [/Redge]
    }
    else  // RENDER_TO_TEXTURE
    {
#ifdef _WIN32
      if(_bRectangle)
      {
        if (!iWGLTextureTarget)  iWGLTextureTarget = WGL_TEXTURE_RECTANGLE_NV; 
        iDepthBindTarget    = WGL_BIND_TO_TEXTURE_RECTANGLE_DEPTH_NV;
        iDepthTextureFormat = WGL_TEXTURE_DEPTH_COMPONENT_NV;
      }
      else
      {
        if (!iWGLTextureTarget)  iWGLTextureTarget = WGL_TEXTURE_2D_ARB; 
        iDepthBindTarget    = WGL_BIND_TO_TEXTURE_DEPTH_NV;
        iDepthTextureFormat = WGL_TEXTURE_DEPTH_COMPONENT_NV;
      }
#endif
    }
  }

#if _WIN32
  // Get the current context.
  HDC hdc = wglGetCurrentDC();
  if (NULL == hdc)
    _wglGetLastError();
  HGLRC hglrc = wglGetCurrentContext();
  if (NULL == hglrc)
    _wglGetLastError();
  
  int iFormat = 0;
  unsigned int iNumFormats;
  int attribChooseList[50]; 
  int attribCreateList[50];
  int attribChoose = 0;
  int attribCreate = 0;
  
  // Setup the attrib list for wglChoosePixelFormat()
  
  attribChooseList[attribChoose++] = WGL_RED_BITS_ARB;
  attribChooseList[attribChoose++] = iRBits;
  attribChooseList[attribChoose++] = WGL_GREEN_BITS_ARB;
  attribChooseList[attribChoose++] = iGBits;
  attribChooseList[attribChoose++] = WGL_BLUE_BITS_ARB;
  attribChooseList[attribChoose++] = iBBits;
  attribChooseList[attribChoose++] = WGL_ALPHA_BITS_ARB;
  attribChooseList[attribChoose++] = iABits;
  if (_bFloat)
  {
    if (bNVIDIA)
    {
      attribChooseList[attribChoose++] = WGL_FLOAT_COMPONENTS_NV;
      attribChooseList[attribChoose++] = GL_TRUE;
    }
    else
    {
      attribChooseList[attribChoose++] = WGL_PIXEL_TYPE_ARB;
      attribChooseList[attribChoose++] = WGL_TYPE_RGBA_FLOAT_ATI;
    }
  }
    
  attribChooseList[attribChoose++] = WGL_STENCIL_BITS_ARB;
  attribChooseList[attribChoose++] = (bStencil) ? 8 : 0;
  attribChooseList[attribChoose++] = WGL_DEPTH_BITS_ARB;
  attribChooseList[attribChoose++] = (bDepth) ? 24 : 0;
  attribChooseList[attribChoose++] = WGL_DRAW_TO_PBUFFER_ARB;
  attribChooseList[attribChoose++] = GL_TRUE;

  if (_bIsTexture && RT_RENDER_TO_TEXTURE == _eUpdateMode)
  {
    attribChooseList[attribChoose++] = iBindTarget;
    attribChooseList[attribChoose++] = GL_TRUE;
  }
  if (_bIsDepthTexture && RT_RENDER_TO_TEXTURE == _eUpdateMode)
  {
    attribChooseList[attribChoose++] = iDepthBindTarget;
    attribChooseList[attribChoose++] = GL_TRUE;
  }

  attribChooseList[attribChoose++] = 0;

  // Setup the attrib list for wglCreatePbuffer()
  if ((_bIsTexture || _bIsDepthTexture) && RT_RENDER_TO_TEXTURE == _eUpdateMode)
  {
    attribCreateList[attribCreate++] = WGL_TEXTURE_TARGET_ARB;
    attribCreateList[attribCreate++] = iWGLTextureTarget;
  }
  if (_bIsTexture && RT_RENDER_TO_TEXTURE == _eUpdateMode)
  {
    attribCreateList[attribCreate++] = WGL_TEXTURE_FORMAT_ARB;
    attribCreateList[attribCreate++] = iTextureFormat;
    /*attribCreateList[attribCreate++] = WGL_TEXTURE_TARGET_ARB;
    attribCreateList[attribCreate++] = iWGLTextureTarget;*/
    attribCreateList[attribCreate++] = WGL_MIPMAP_TEXTURE_ARB;
    attribCreateList[attribCreate++] = (bMipmap) ? GL_TRUE : GL_FALSE;  
  }
  if (_bIsDepthTexture && RT_RENDER_TO_TEXTURE == _eUpdateMode)
  {
    attribCreateList[attribCreate++] = WGL_DEPTH_TEXTURE_FORMAT_NV;
    attribCreateList[attribCreate++] = iDepthTextureFormat;
  }
  attribCreateList[attribCreate++] = WGL_PBUFFER_LARGEST_ARB;
  attribCreateList[attribCreate++] = GL_FALSE;
  attribCreateList[attribCreate++] = 0;

  if (!wglChoosePixelFormatARB( hdc, attribChooseList, NULL, 1, &iFormat, &iNumFormats))
  {
    fprintf(stderr, 
            "RenderTexture::Initialize() creation error: wglChoosePixelFormatARB() failed.\n");
    _wglGetLastError();
    return false;
  }
  if ( iNumFormats <= 0 )
  {
    fprintf(stderr, 
            "RenderTexture::Initialize() creation error: Couldn't find a suitable pixel format.\n");
    _wglGetLastError();
    return false;
  }

  // Create the p-buffer.    
  _hPBuffer = wglCreatePbufferARB( hdc, iFormat, _iWidth, _iHeight, attribCreateList );
  if (!_hPBuffer)
  {
    fprintf(stderr, "RenderTexture::Initialize() pbuffer creation error: wglCreatePbufferARB() failed\n");
    _wglGetLastError();
    return false;
  }
  
   // Get the device context.
  _hDC = wglGetPbufferDCARB( _hPBuffer);
  if ( !_hDC )
  {
    fprintf(stderr, 
            "RenderTexture::Initialize() creation error: wglGetGetPbufferDCARB() failed\n");
    _wglGetLastError();
    return false;
  }
  
  // Create a gl context for the p-buffer.

  
  _hGLContext = wglCreateContext( _hDC );
  if ( !_hGLContext )
  {
    fprintf(stderr, "RenderTexture::Initialize() creation error:  wglCreateContext() failed\n");
    _wglGetLastError();
    return false;
  }
  
  // Share lists, texture objects, and program objects.
  if( bShare )
  {
    if( !wglShareLists(hglrc, _hGLContext) )
    {
      fprintf(stderr, "RenderTexture::Initialize() creation error: wglShareLists() failed\n" );
      _wglGetLastError();
      return false;
    }
  }

  // bind the pbuffer to the render texture object
  if (_bIsTexture && RT_RENDER_TO_TEXTURE == _eUpdateMode)
  {
    glBindTexture(_iTextureTarget, _iTextureID);
    if (wglBindTexImageARB(_hPBuffer, WGL_FRONT_LEFT_ARB) == FALSE)
    {
      _wglGetLastError();
      return false;
    }
  }
  if (_bIsDepthTexture && RT_RENDER_TO_TEXTURE == _eUpdateMode)
  {
    glBindTexture(_iTextureTarget, _iDepthTextureID);
    if (wglBindTexImageARB(_hPBuffer, WGL_DEPTH_COMPONENT_NV) == FALSE)
    {
      _wglGetLastError();
      return false;
    }
  }

   // Determine the actual width and height we were able to create.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -