Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Register
    • Register
    • Login

    Baking normal in Object space: Bake Material Tag V.S. BakeTexture()

    Cinema 4D SDK
    windows c++ 2024
    2
    4
    702
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • justinleducJ
      justinleduc
      last edited by

      Hey guys,

      I'm trying to understand why the normal map baked (in object space) by the Bake Material tag is different from the one that is baked by the the SDK (using the InitBakeTexture() and BakeTexture() functions) when they are seemingly both given the same parameters.

      In both cases (the Bake Material tag and the SDK functions), these are my settings:

      • Width: 1024
      • Height: 1024
      • Channel: Normal (active)
      • Source (inside of the Normals sub-section): Object whose normals are being baked
      • Method: Object (default)

      The Base Material tag gives me this:
      head-object-normals_001.png

      While the InitBakeTexture() and BakeTexture() functions give me this:
      head-object-normals_002.png

      Unless I am wrong, the result yielded by the Bake Material tag seems to be the most accurate and favorable one (in terms of color spectrum). Am I wrong?

      Here is an excerpt of my C++ code:

      BaseContainer settings;
      settings.SetInt32(BAKE_TEX_WIDTH, 1024);
      settings.SetInt32(BAKE_TEX_HEIGHT, 1024);
      settings.SetInt32(BAKE_TEX_PIXELBORDER, 0);
      settings.SetBool(BAKE_TEX_CONTINUE_UV, true);
      settings.SetBool(BAKE_TEX_USE_PHONG_TAG, true);
      settings.SetVector(BAKE_TEX_FILL_COLOR, Vector(0.0));
      settings.SetBool(BAKE_TEX_NORMAL, true);
      settings.SetBool(BAKE_TEX_NORMAL_METHOD_OBJECT, true);
      
      BAKE_TEX_ERR err;
      
      BaseDocument* bakeDoc = InitBakeTexture(doc, (TextureTag*)textureTag, (UVWTag*)uvwTag, nullptr, settings, &err, nullptr);
      
      // Check if initialization was successful
      if (err != BAKE_TEX_ERR::NONE)
          return maxon::UnexpectedError(MAXON_SOURCE_LOCATION, "Failed to initialize baking of the normal map."_s);
      
      // Prepare bitmap for baking
      MultipassBitmap* normalMap = MultipassBitmap::Alloc(1024, 1024, COLORMODE::RGBw);
      if (!normalMap)
          return maxon::UnexpectedError(MAXON_SOURCE_LOCATION, "Could not allocate normal map."_s);
      
      // Perform the bake for the normal map
      BakeTexture(bakeDoc, settings, normalMap, nullptr, nullptr, nullptr);
      
      // File path of the normal map
      maxon::Url urlNormalMapResult{ "file:///C:/filepath/normal_map.png"_s };
      
      normalMap->Save(MaxonConvert(urlNormalMapResult), FILTER_PNG, nullptr, SAVEBIT::NONE);
      

      Note: I am currently assigning the color mode RGBw to normalMap. I have experimented a lot in that area and I have not been able to find any other COLORMODE or any other setting/enum for that matter that would get me closer to the normal map baked by the Base Material tag.

      Any insight/guidance would be massively appreciated as always.

      Thank you so much!

      i_mazlovI 1 Reply Last reply Reply Quote 0
      • justinleducJ
        justinleduc
        last edited by

        Thinking that the problem stemmed from the lack of color profile, I attempted to add the BAKE_TEX_COLORPROFILE enum to my settings, but I was unable to do so without it throwing an error upon the building of my solution.

        My updated settings:

        AutoAlloc<ColorProfile> colorProfile;
        if (!colorProfile || !colorProfile->GetDefaultSRGB())
        {
            return maxon::UnexpectedError(MAXON_SOURCE_LOCATION, "Failed to allocate or initialize the color profile."_s);
        }
        
        // Setup bake settings
        BaseContainer settings;
        
        // ...other settings removed for brevity...
        
        settings.SetData(BAKE_TEX_COLORPROFILE, GeData(colorProfile));
        

        Unfortunately, the additional line above (i.e. BAKE_TEX_COLORPROFILE) throws the following errors upon building my solution:

        • Error C2661: BaseContainer::SetData: no overloaded function takes 1 arguments
        • Error C2440: <function-style-cast>: cannot convert from AutoAlloc<ColorProfile> to GeData

        I've been "fighting" with the SDK for the past hour while extensively reading the documentation, but I have yet to find a solution.

        If my problem has nothing to do with the color profile, I guess that would also be good to know! 😆

        Thank you very much as always.

        1 Reply Last reply Reply Quote 0
        • justinleducJ
          justinleduc
          last edited by

          Whoopsies. I had forgotten this one setting:

          settings.SetBool(BAKE_TEX_NORMAL_USE_RAYCAST, true);

          It works as expected now.

          Apologies!

          This matter is now resolved.

          1 Reply Last reply Reply Quote 0
          • i_mazlovI
            i_mazlov @justinleduc
            last edited by

            Hi @justinleduc ,

            Great to hear your issue was resolved! Special thanks on sharing your solution with the community!

            Regarding compiling issues you had:

            Error C2440: <function-style-cast>: cannot convert from AutoAlloc<ColorProfile> to GeData

            The AutoAlloc wrapper is just a convenient wrapper for a raw pointer. Hence, to fix your issue you just had to dereference the pointer:

            CheckState(colorProfile);
            settings.SetData(BAKE_TEX_COLORPROFILE, GeData(*colorProfile));
            

            Cheers,
            Ilia

            MAXON SDK Specialist
            developers.maxon.net

            1 Reply Last reply Reply Quote 1
            • First post
              Last post