Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Updating textures in OpenGL with an external plugin

Discussion in 'Scripting' started by Panzerhandschuh, Nov 2, 2013.

  1. Panzerhandschuh

    Panzerhandschuh

    Joined:
    Dec 4, 2012
    Posts:
    17
    I am working on an external plugin that updates the in game textures by reading DDS files. So far I was able to get DDS files to load in DX11 and DX9, but I can't seem to get OpenGL to work.

    I am using the nvidia graphics sdk 10 for opengl to read the DDS files and GLEW to update the texture, but the texture won't update in game properly. I am trying to read a 2048x2048 DXT1 texture with 12 mipmaps.

    Here are the relevant parts of my code:
    Code (csharp):
    1.  
    2. extern "C" void EXPORT_API UnitySetGraphicsDevice(void* device, int deviceType, int eventType)
    3. {
    4.     #if SUPPORT_OPENGL
    5.     if (deviceType == kGfxRendererOpenGL)
    6.     {
    7.         DebugLog("Set OpenGL graphics device\n");
    8.         g_DeviceType = deviceType;
    9.  
    10.         // Initialize glew
    11.         GLenum err = glewInit();
    12.         if (GLEW_OK != err)
    13.         {
    14.             char message[64];
    15.             sprintf_s(message, "Error: %s", glewGetErrorString(err));
    16.             DebugLog(message);
    17.         }
    18.     }
    19.     #endif
    20. }
    21.  
    22. static void DoRendering()
    23. {
    24.     #if SUPPORT_OPENGL
    25.     if (g_DeviceType == kGfxRendererOpenGL)
    26.     {
    27.         // update native texture from code
    28.         if (g_TexturePointer)
    29.         {
    30.             nv::Image texture;
    31.             if (!texture.loadImageFromFile("myimage.dds"))
    32.                 DebugLog("Failed to load DDS texture");
    33.  
    34.             GLuint gltex = (GLuint)(size_t)(g_TexturePointer);
    35.             glBindTexture(GL_TEXTURE_2D, gltex);
    36.  
    37.             //glCompressedTexImage2DARB(GL_TEXTURE_2D, 0, texture.getInternalFormat(), texture.getWidth(), texture.getHeight(), 0, texture.getImageSize(0), texture.getLevel(0));
    38.             for (int i = 0; i < texture.getMipLevels(); i++)
    39.             {
    40.                 glCompressedTexImage2DARB(GL_TEXTURE_2D, i, texture.getInternalFormat(), texture.getWidth(), texture.getHeight(), 0, texture.getImageSize(i), texture.getLevel(i));
    41.             }
    42.         }
    43.     }
    44.     #endif
    45. }
    46.  
    I must initialize GLEW when the OpenGL device is created to use glCompressedTexImage2DARB.

    With this code I was able to get the in game texture to turn black, but not to how it should actually look. I'm sure the plugin is at least doing something since the texture turns black instead of using the default DXT1 image, I'm just not sure why it's not displaying the actual image from the DDS file.