Skip to content

bugfix(ww3d2): Fix mesh material color processing - #3246

Open
CryoTheRenegade wants to merge 6 commits into
TheSuperHackers:mainfrom
CryoTheRenegade:bugfix/clang-tidy-material-initialization
Open

bugfix(ww3d2): Fix mesh material color processing#3246
CryoTheRenegade wants to merge 6 commits into
TheSuperHackers:mainfrom
CryoTheRenegade:bugfix/clang-tidy-material-initialization

Conversation

@CryoTheRenegade

@CryoTheRenegade CryoTheRenegade commented Sep 1, 2026

Copy link
Copy Markdown

Prevents crashes on null mesh materials and incorrect vertex colors caused by reusing another material's ambient or emissive color. Skips null entries, applies each vertex's own material colors, and removes unused first-material baseline calculations in Generals and Zero Hour.

For example, with white input vertex colors and two ambient-only materials, green followed by red, the old code applies the last material's red color to both vertices:

Vertex material Before After
Green 🟥 Red 🟩 Green
Red 🟥 Red 🟥 Red

This RGB example was reproduced in the CPU regression test. Emissive-only materials have the same problem.

@CryoTheRenegade
CryoTheRenegade marked this pull request as ready for review September 9, 2026 15:36
@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Summary by Qodo

Initialize WW3D2 mesh material colors safely

🐞 Bug fix 🕐 Less than 10 minutes

Grey Divider

AI Description

• Zero-initializes material color vectors before mesh color analysis.
• Skips null per-vertex materials during analysis and color application.
• Mirrors the safety fix across Generals and Zero Hour codebases.
Diagram

graph TD
  A["Mesh post-load"] --> B["Read vertex material"] --> C{"Material exists?"}
  C -->|"No"| D["Skip vertex"]
  C -->|"Yes"| E["Load initialized colors"] --> F["Analyze and apply colors"]
Loading
High-Level Assessment

The localized initialization and null guards are the appropriate approach because null entries are valid material-array contents. Normalizing arrays upstream would broaden the change and could alter established sparse-array semantics without improving safety at the dereference sites.

Files changed (2) +26 / -6

Bug fix (2) +26 / -6
meshmatdesc.cppGuard Generals mesh material color processing +13/-3

Guard Generals mesh material color processing

• Initializes diffuse, ambient, and emissive material vectors to zero. Skips null material-array entries during property analysis and vertex color application, preventing invalid dereferences and stale colors.

Generals/Code/Libraries/Source/WWVegas/WW3D2/meshmatdesc.cpp

meshmatdesc.cppGuard Zero Hour mesh material color processing +13/-3

Guard Zero Hour mesh material color processing

• Applies the same zero-initialization and null-entry checks to the GeneralsMD implementation. Valid material entries continue through the existing color analysis and application logic.

GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/meshmatdesc.cpp

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Sep 9, 2026

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0) 🎨 UX issues (0) 🔗 Cross-repo conflicts (0) 📜 Skill insights (0)

Grey Divider


Action required

1. A later null material still crashes ✓ Resolved 🐞 Bug ≡ Correctness
Description
Post_Load_Process skips null entries in the modified analysis loop but its later GeneralsMD
lighting scan calls Get_Diffuse, Get_Ambient, and Get_Emissive whenever the material changes
without checking the new value. When a material array contains a valid entry followed by a null
entry, the new guard allows processing to reach that scan, where the transition to null is
dereferenced.
Code

GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/meshmatdesc.cpp[R720-722]

+			if (mtl == nullptr) {
+				continue;
+			}
Evidence
The added guard establishes that null array entries are valid and lets processing continue past
them. The later scan reads each entry at lines 920-926 and dereferences every pointer that differs
from the previous one, so a non-null-to-null transition calls three methods through null; the
following lighting-update scan at lines 937-944 has the same missing guard before Set_Lighting.

GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/meshmatdesc.cpp[717-729]
GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/meshmatdesc.cpp[920-927]
GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/meshmatdesc.cpp[934-947]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
`MeshMatDescClass::Post_Load_Process` now skips null materials in its color-analysis loops, but the later GeneralsMD lighting scans still dereference null entries when the material pointer changes.
## Issue Context
A material array may contain a valid material followed by null. Apply equivalent null handling to both later loops while preserving processing for subsequent valid entries and avoiding reads from uninitialized material-color values.
## Fix Focus Areas
- GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/meshmatdesc.cpp[920-947]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

Tip of the day
💡 Did you know, you can hide the parts of a finding you never read, like the evidence or the agent prompt

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

Comment thread GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/meshmatdesc.cpp Outdated
@greptile-apps

greptile-apps Bot commented Sep 9, 2026

Copy link
Copy Markdown

Greptile Summary

Fixes mesh material processing in Generals and Zero Hour by skipping null material entries and applying each vertex material’s own ambient and emissive colors. The follow-up also safely short-circuits material scans once all relevant color channels have been detected.

  • Prevents null material dereferences.
  • Corrects ambient and emissive vertex-color processing.
  • Applies equivalent handling across both game implementations.

Confidence Score: 5/5

The PR appears safe to merge, with no outstanding correctness or repository-rule violations identified.

The new early exits preserve behavior because they occur only after all three pass-wide usage flags are true, and the Zero Hour lighting scan now skips null materials before dereferencing them. The previous thread was manually resolved without explanation, and the current code also addresses its reported null-dereference path.

Important Files Changed

Filename Overview
Generals/Code/Libraries/Source/WWVegas/WW3D2/meshmatdesc.cpp Adds null-safe per-material color processing and a valid early exit once all color channels are detected.
GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/meshmatdesc.cpp Mirrors the material-color fix and makes the later lighting scan null-safe; the previous thread was manually resolved without explanation.

Reviews (6): Last reviewed commit: "refactor(ww3d2): Stop material analysis ..." | Re-trigger Greptile

Comment thread GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/meshmatdesc.cpp Outdated
@CryoTheRenegade

Copy link
Copy Markdown
Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: d72a2ebcc2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/meshmatdesc.cpp Outdated
@CryoTheRenegade CryoTheRenegade changed the title bugfix(ww3d2): Initialize mesh material colors safely bugfix(ww3d2): Fix mesh material color processing Sep 10, 2026
Comment thread GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/meshmatdesc.cpp Outdated
Comment thread GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/meshmatdesc.cpp Outdated
Comment thread GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/meshmatdesc.cpp Outdated
Comment thread GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/meshmatdesc.cpp Outdated

@xezon xezon left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks logical. I don't know how this will translate to the game.

Comment thread GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/meshmatdesc.cpp

@xezon xezon left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you test in game? Does it look normal?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants