collada_scene: render scene textures

This commit is contained in:
Zack Buhman 2026-01-29 20:05:22 -06:00
parent af165a3843
commit 21f3de998c
11 changed files with 112 additions and 30 deletions

2
.gitignore vendored
View File

@ -12,4 +12,4 @@ __pycache__
.\#* .\#*
\#* \#*
*.gch *.gch
image/*.DDS image/*.DDS

View File

@ -74,9 +74,10 @@ src/scenes/%.cpp: scenes/%.DAE include/scenes/%.hpp
@mkdir -p $(@D) @mkdir -p $(@D)
PYTHONPATH=. python -m collada.main $< $@ $(<:.DAE=.vtx) $(<:.DAE=.idx) $(notdir $(<:.DAE=.rc)) $(notdir $(<:.DAE=.mk)) PYTHONPATH=. python -m collada.main $< $@ $(<:.DAE=.vtx) $(<:.DAE=.idx) $(notdir $(<:.DAE=.rc)) $(notdir $(<:.DAE=.mk))
# $(BUILD_TYPE)/curve_interpolation.res \
OBJS = \ OBJS = \
$(BUILD_TYPE)/main.res \ $(BUILD_TYPE)/main.res \
$(BUILD_TYPE)/curve_interpolation.res \
$(BUILD_TYPE)/robot_player.obj \ $(BUILD_TYPE)/robot_player.obj \
$(BUILD_TYPE)/cube.obj \ $(BUILD_TYPE)/cube.obj \
$(BUILD_TYPE)/main.obj \ $(BUILD_TYPE)/main.obj \

View File

@ -41,7 +41,7 @@ def render_makefile(state, f):
f.write(f"IMAGES += image/{filename}.DDS\r\n".encode('ascii')) f.write(f"IMAGES += image/{filename}.DDS\r\n".encode('ascii'))
f.write(f"image/{filename}.DDS: {escaped}\r\n".encode('ascii')) f.write(f"image/{filename}.DDS: {escaped}\r\n".encode('ascii'))
f.write('\ttexconv10.exe -nologo "$<"\r\n'.encode('ascii')) f.write('\ttexconv10.exe -f BC1_UNORM -nologo "$<"\r\n'.encode('ascii'))
f.write(f'\tmv "$(<:.{ext[1:]}=.DDS)" "$@"\r\n\r\n'.encode('ascii')) f.write(f'\tmv "$(<:.{ext[1:]}=.DDS)" "$@"\r\n\r\n'.encode('ascii'))
def main(): def main():

View File

@ -1,20 +1,20 @@
IMAGES += image/american_cherry.DDS IMAGES += image/american_cherry.DDS
image/american_cherry.DDS: C:/Program\ Files/Common\ Files/Autodesk\ Shared/Materials/Textures/1/Mats/american_cherry.png image/american_cherry.DDS: C:/Program\ Files/Common\ Files/Autodesk\ Shared/Materials/Textures/1/Mats/american_cherry.png
texconv10.exe -nologo "$<" texconv10.exe -f BC1_UNORM -nologo "$<"
mv "$(<:.png=.DDS)" "$@" mv "$(<:.png=.DDS)" "$@"
IMAGES += image/102.DDS IMAGES += image/102.DDS
image/102.DDS: C:/Program\ Files/Common\ Files/Autodesk\ Shared/Materials/Textures/3/Mats/102.png image/102.DDS: C:/Program\ Files/Common\ Files/Autodesk\ Shared/Materials/Textures/3/Mats/102.png
texconv10.exe -nologo "$<" texconv10.exe -f BC1_UNORM -nologo "$<"
mv "$(<:.png=.DDS)" "$@" mv "$(<:.png=.DDS)" "$@"
IMAGES += image/Finishes.Flooring.Tile.Square.Medium\ Blue.DDS IMAGES += image/Finishes.Flooring.Tile.Square.Medium\ Blue.DDS
image/Finishes.Flooring.Tile.Square.Medium\ Blue.DDS: C:/Program\ Files/Common\ Files/Autodesk\ Shared/Materials/Textures/3/Mats/Finishes.Flooring.Tile.Square.Medium\ Blue.png image/Finishes.Flooring.Tile.Square.Medium\ Blue.DDS: C:/Program\ Files/Common\ Files/Autodesk\ Shared/Materials/Textures/3/Mats/Finishes.Flooring.Tile.Square.Medium\ Blue.png
texconv10.exe -nologo "$<" texconv10.exe -f BC1_UNORM -nologo "$<"
mv "$(<:.png=.DDS)" "$@" mv "$(<:.png=.DDS)" "$@"
IMAGES += image/SiteWork.Planting.Grass.Bermuda1.DDS IMAGES += image/SiteWork.Planting.Grass.Bermuda1.DDS
image/SiteWork.Planting.Grass.Bermuda1.DDS: C:/Program\ Files/Common\ Files/Autodesk\ Shared/Materials/Textures/3/Mats/SiteWork.Planting.Grass.Bermuda1.jpg image/SiteWork.Planting.Grass.Bermuda1.DDS: C:/Program\ Files/Common\ Files/Autodesk\ Shared/Materials/Textures/3/Mats/SiteWork.Planting.Grass.Bermuda1.jpg
texconv10.exe -nologo "$<" texconv10.exe -f BC1_UNORM -nologo "$<"
mv "$(<:.jpg=.DDS)" "$@" mv "$(<:.jpg=.DDS)" "$@"

View File

@ -37,6 +37,8 @@ namespace collada_scene {
node_instance * m_nodeInstances = NULL; node_instance * m_nodeInstances = NULL;
ID3D10ShaderResourceView ** m_pShaderResourceViews = NULL;
XMFLOAT4 * m_lightPositions = NULL; XMFLOAT4 * m_lightPositions = NULL;
XMFLOAT4 * m_lightDirections = NULL; XMFLOAT4 * m_lightDirections = NULL;
XMFLOAT4 * m_lightColors = NULL; XMFLOAT4 * m_lightColors = NULL;
@ -51,6 +53,7 @@ namespace collada_scene {
private: private:
HRESULT load_layouts(); HRESULT load_layouts();
HRESULT allocate_images();
void allocate_node_instances(); void allocate_node_instances();
void allocate_node_instance(collada::node const * const node, void allocate_node_instance(collada::node const * const node,
node_instance * node_instance); node_instance * node_instance);
@ -64,6 +67,10 @@ namespace collada_scene {
void render_geometries(collada::instance_geometry const * const instance_geometries, void render_geometries(collada::instance_geometry const * const instance_geometries,
int const instance_geometries_count); int const instance_geometries_count);
void set_material(collada::effect const& effect);
void set_color_or_texture(collada::color_or_texture const& color_or_texture,
ID3D10EffectVectorVariable * pVectorVariable,
ID3D10EffectShaderResourceVariable * pShaderResourceVariable);
}; };
HRESULT LoadEffect(); HRESULT LoadEffect();

View File

@ -3,7 +3,7 @@ RES_FONT_FXO RCDATA "font.fxo"
RES_VOLUME_FXO RCDATA "volume.fxo" RES_VOLUME_FXO RCDATA "volume.fxo"
RES_BLOOM_FXO RCDATA "bloom.fxo" RES_BLOOM_FXO RCDATA "bloom.fxo"
RES_STATIC_FXO RCDATA "static.fxo" RES_STATIC_FXO RCDATA "static.fxo"
RES_PERLIN RCDATA "texture/perlin.data" //RES_PERLIN RCDATA "texture/perlin.data"
RES_ROBOT_PLAYER RCDATA "models/robot_player/robot_player.DDS" RES_ROBOT_PLAYER RCDATA "models/robot_player/robot_player.DDS"
RES_FONT_TERMINUS_6X12 RCDATA "font/terminus_128x64_6x12.data" RES_FONT_TERMINUS_6X12 RCDATA "font/terminus_128x64_6x12.data"
@ -12,3 +12,9 @@ RES_COLLADA_FXO RCDATA "collada.fxo"
RES_COLLADA_SCENE_FXO RCDATA "collada_scene.fxo" RES_COLLADA_SCENE_FXO RCDATA "collada_scene.fxo"
RES_MODELS_CURVE_INTERPOLATION_VTX RCDATA "scenes/curve_interpolation/curve_interpolation.vtx" RES_MODELS_CURVE_INTERPOLATION_VTX RCDATA "scenes/curve_interpolation/curve_interpolation.vtx"
RES_MODELS_CURVE_INTERPOLATION_IDX RCDATA "scenes/curve_interpolation/curve_interpolation.idx" RES_MODELS_CURVE_INTERPOLATION_IDX RCDATA "scenes/curve_interpolation/curve_interpolation.idx"
_AMERICAN_CHERRY_PNG RCDATA "image/american_cherry.DDS"
_102_PNG RCDATA "image/102.DDS"
_FINISHES_FLOORING_TILE_SQUARE_MEDIUM_BLUE_PNG RCDATA "image/Finishes.Flooring.Tile.Square.Medium Blue.DDS"
_SITEWORK_PLANTING_GRASS_BERMUDA1_JPG RCDATA "image/SiteWork.Planting.Grass.Bermuda1.DDS"

View File

@ -7,6 +7,7 @@
#include "print.hpp" #include "print.hpp"
#include "collada_scene.hpp" #include "collada_scene.hpp"
#include "new.hpp" #include "new.hpp"
#include "render_state.hpp"
extern ID3D10Device * g_pd3dDevice; extern ID3D10Device * g_pd3dDevice;
extern XMVECTOR g_Eye; extern XMVECTOR g_Eye;
@ -34,6 +35,13 @@ namespace collada_scene {
ID3D10EffectVectorVariable * g_pSpecularVariable = NULL; ID3D10EffectVectorVariable * g_pSpecularVariable = NULL;
ID3D10EffectScalarVariable * g_pShininessVariable = NULL; ID3D10EffectScalarVariable * g_pShininessVariable = NULL;
ID3D10EffectVectorVariable * g_pTextureChannelVariable = NULL;
ID3D10EffectShaderResourceVariable * g_pTexEmissionVariable = NULL;
ID3D10EffectShaderResourceVariable * g_pTexAmbientVariable = NULL;
ID3D10EffectShaderResourceVariable * g_pTexDiffuseVariable = NULL;
ID3D10EffectShaderResourceVariable * g_pTexSpecularVariable = NULL;
static inline DXGI_FORMAT dxgi_format(input_format format) static inline DXGI_FORMAT dxgi_format(input_format format)
{ {
switch (format) { switch (format) {
@ -163,6 +171,23 @@ namespace collada_scene {
} }
} }
HRESULT scene_state::allocate_images()
{
HRESULT hr;
m_pShaderResourceViews = New<ID3D10ShaderResourceView *>(m_descriptor->images_count);
for (int i = 0; i < m_descriptor->images_count; i++) {
image const * const image = m_descriptor->images[i];
hr = LoadDDSTexture2D(image->resource_name,
&m_pShaderResourceViews[i]);
if (FAILED(hr)) {
return hr;
}
}
return S_OK;
}
HRESULT scene_state::load_scene(collada::descriptor const * const descriptor) HRESULT scene_state::load_scene(collada::descriptor const * const descriptor)
{ {
HRESULT hr; HRESULT hr;
@ -182,6 +207,10 @@ namespace collada_scene {
if (FAILED(hr)) if (FAILED(hr))
return E_FAIL; return E_FAIL;
hr = allocate_images();
if (FAILED(hr))
return E_FAIL;
allocate_node_instances(); allocate_node_instances();
allocate_light_instances(); allocate_light_instances();
@ -263,6 +292,13 @@ namespace collada_scene {
g_pSpecularVariable = g_pEffect->GetVariableByName("Specular")->AsVector(); g_pSpecularVariable = g_pEffect->GetVariableByName("Specular")->AsVector();
g_pShininessVariable = g_pEffect->GetVariableByName("Shininess")->AsScalar(); g_pShininessVariable = g_pEffect->GetVariableByName("Shininess")->AsScalar();
g_pTextureChannelVariable = g_pEffect->GetVariableByName("TextureChannel")->AsVector();
g_pTexEmissionVariable = g_pEffect->GetVariableByName("TexEmission")->AsShaderResource();
g_pTexAmbientVariable = g_pEffect->GetVariableByName("TexAmbient")->AsShaderResource();
g_pTexDiffuseVariable = g_pEffect->GetVariableByName("TexDiffuse")->AsShaderResource();
g_pTexSpecularVariable = g_pEffect->GetVariableByName("TexSpecular")->AsShaderResource();
return S_OK; return S_OK;
} }
@ -340,26 +376,42 @@ namespace collada_scene {
} }
} }
static inline void SetMaterial(effect const& effect) void scene_state::set_color_or_texture(color_or_texture const& color_or_texture,
ID3D10EffectVectorVariable * pVectorVariable,
ID3D10EffectShaderResourceVariable * pShaderResourceVariable)
{
switch (color_or_texture.type) {
case color_or_texture_type::COLOR:
pVectorVariable->SetFloatVector((float *)&color_or_texture.color);
break;
case color_or_texture_type::TEXTURE:
pShaderResourceVariable->SetResource(m_pShaderResourceViews[color_or_texture.texture.image_index]);
break;
default:
assert(false);
}
}
void scene_state::set_material(effect const& effect)
{ {
switch (effect.type) { switch (effect.type) {
case effect_type::BLINN: case effect_type::BLINN:
g_pEmissionVariable->SetFloatVector((float *)&effect.blinn.emission.color); set_color_or_texture(effect.blinn.emission, g_pEmissionVariable, g_pTexEmissionVariable);
g_pAmbientVariable->SetFloatVector((float *)&effect.blinn.ambient.color); set_color_or_texture(effect.blinn.ambient, g_pAmbientVariable, g_pTexAmbientVariable);
g_pDiffuseVariable->SetFloatVector((float *)&effect.blinn.diffuse.color); set_color_or_texture(effect.blinn.diffuse, g_pDiffuseVariable, g_pTexDiffuseVariable);
g_pSpecularVariable->SetFloatVector((float *)&effect.blinn.specular.color); set_color_or_texture(effect.blinn.specular, g_pSpecularVariable, g_pTexSpecularVariable);
g_pShininessVariable->SetFloat(effect.blinn.shininess); g_pShininessVariable->SetFloat(effect.blinn.shininess);
break; break;
case effect_type::LAMBERT: case effect_type::LAMBERT:
g_pEmissionVariable->SetFloatVector((float *)&effect.lambert.emission.color); set_color_or_texture(effect.lambert.emission, g_pEmissionVariable, g_pTexEmissionVariable);
g_pAmbientVariable->SetFloatVector((float *)&effect.lambert.ambient.color); set_color_or_texture(effect.lambert.ambient, g_pAmbientVariable, g_pTexAmbientVariable);
g_pDiffuseVariable->SetFloatVector((float *)&effect.lambert.diffuse.color); set_color_or_texture(effect.lambert.diffuse, g_pDiffuseVariable, g_pTexDiffuseVariable);
break; break;
case effect_type::PHONG: case effect_type::PHONG:
g_pEmissionVariable->SetFloatVector((float *)&effect.phong.emission.color); set_color_or_texture(effect.phong.emission, g_pEmissionVariable, g_pTexEmissionVariable);
g_pAmbientVariable->SetFloatVector((float *)&effect.phong.ambient.color); set_color_or_texture(effect.phong.ambient, g_pAmbientVariable, g_pTexAmbientVariable);
g_pDiffuseVariable->SetFloatVector((float *)&effect.phong.diffuse.color); set_color_or_texture(effect.phong.diffuse, g_pDiffuseVariable, g_pTexDiffuseVariable);
g_pSpecularVariable->SetFloatVector((float *)&effect.phong.specular.color); set_color_or_texture(effect.phong.specular, g_pSpecularVariable, g_pTexSpecularVariable);
g_pShininessVariable->SetFloat(effect.phong.shininess); g_pShininessVariable->SetFloat(effect.phong.shininess);
break; break;
case effect_type::CONSTANT: case effect_type::CONSTANT:
@ -388,7 +440,14 @@ namespace collada_scene {
for (int j = 0; j < instance_geometry.instance_materials_count; j++) { for (int j = 0; j < instance_geometry.instance_materials_count; j++) {
instance_material const& instance_material = instance_geometry.instance_materials[j]; instance_material const& instance_material = instance_geometry.instance_materials[j];
triangles const& triangles = mesh.triangles[instance_material.element_index]; triangles const& triangles = mesh.triangles[instance_material.element_index];
SetMaterial(*instance_material.material->effect); set_material(*instance_material.material->effect);
int texture_channels[4] = {
instance_material.emission.input_set,
instance_material.ambient.input_set,
instance_material.diffuse.input_set,
instance_material.specular.input_set,
};
g_pTextureChannelVariable->SetIntVector(texture_channels);
g_pTechniqueBlinn->GetPassByIndex(0)->Apply(0); g_pTechniqueBlinn->GetPassByIndex(0)->Apply(0);
g_pd3dDevice->IASetInputLayout(m_pVertexLayouts[triangles.inputs_index]); g_pd3dDevice->IASetInputLayout(m_pVertexLayouts[triangles.inputs_index]);

View File

@ -75,6 +75,13 @@ float4 PS(PS_INPUT input) : SV_Target
float3 color = Emission.xyz; float3 color = Emission.xyz;
float3 diffuseColor;
if (TextureChannel.z >= 0) {
diffuseColor = TexDiffuse.Sample(samLinear, input.Tex).xyz;
} else {
diffuseColor = Diffuse.xyz;
}
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
float3 light_dir = normalize(-LightDir[i].xyz); float3 light_dir = normalize(-LightDir[i].xyz);
float diffuse_intensity = max(dot(normal, light_dir), 0.0); float diffuse_intensity = max(dot(normal, light_dir), 0.0);
@ -82,7 +89,7 @@ float4 PS(PS_INPUT input) : SV_Target
float distance = length(LightPos[i].xyz - input.WPos.xyz); float distance = length(LightPos[i].xyz - input.WPos.xyz);
float attenuation = 1.0 / (0.02 * distance * distance); float attenuation = 1.0 / (0.02 * distance * distance);
color += Diffuse.xyz * diffuse_intensity * LightColor[i].xyz * attenuation; color += diffuseColor * diffuse_intensity * LightColor[i].xyz * attenuation;
} }
return float4(color.xyz, 1); return float4(color.xyz, 1);

View File

@ -521,6 +521,7 @@ HRESULT LoadMesh()
return hr; return hr;
} }
/*
hr = LoadTexture3D(L"RES_PERLIN", hr = LoadTexture3D(L"RES_PERLIN",
256, // width 256, // width
256, // height 256, // height
@ -533,6 +534,7 @@ HRESULT LoadMesh()
print("LoadTexture2D\n"); print("LoadTexture2D\n");
return hr; return hr;
} }
*/
return S_OK; return S_OK;
} }
@ -716,7 +718,7 @@ HRESULT InitBloomBuffers()
HRSRC hRes = FindResource(NULL, L"RES_BLOOM_FXO", RT_RCDATA); HRSRC hRes = FindResource(NULL, L"RES_BLOOM_FXO", RT_RCDATA);
if (hRes == NULL) { if (hRes == NULL) {
print("FindResource RES_BLOOM_FXO\n"); print("FindResource RES_BLOOM_FXO %d\n", hRes);
return E_FAIL; return E_FAIL;
} }
DWORD dwResSize = SizeofResource(NULL, hRes); DWORD dwResSize = SizeofResource(NULL, hRes);
@ -1749,7 +1751,7 @@ void RenderVolume(float t)
g_pd3dDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); g_pd3dDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
g_pLayerVariableVolume->SetFloat(t * 0.1f); g_pLayerVariableVolume->SetFloat(t * 0.1f);
g_pDiffuseVariableVolume->SetResource(g_pTextureShaderResourceViewPerlin); //g_pDiffuseVariableVolume->SetResource(g_pTextureShaderResourceViewPerlin);
D3D10_TECHNIQUE_DESC techDescVolume; D3D10_TECHNIQUE_DESC techDescVolume;
g_pTechniqueVolume->GetDesc(&techDescVolume); g_pTechniqueVolume->GetDesc(&techDescVolume);
@ -1769,7 +1771,7 @@ void RenderVolumeMesh()
g_pViewVariableStatic->SetMatrix((float *)&g_View); g_pViewVariableStatic->SetMatrix((float *)&g_View);
g_pProjectionVariableStatic->SetMatrix((float *)&g_Projection); g_pProjectionVariableStatic->SetMatrix((float *)&g_Projection);
g_pDiffuseVariableStatic->SetResource(g_pTextureShaderResourceViewPerlin); //g_pDiffuseVariableStatic->SetResource(g_pTextureShaderResourceViewPerlin);
ID3D10Buffer * pVB[4] = { ID3D10Buffer * pVB[4] = {
g_pVertexBufferCube[0], g_pVertexBufferCube[0],
@ -1822,7 +1824,7 @@ void Render(float t, float dt)
g_pd3dDevice->ClearDepthStencilView(g_pDepthStencilView, D3D10_CLEAR_DEPTH, 1.0f, 0); g_pd3dDevice->ClearDepthStencilView(g_pDepthStencilView, D3D10_CLEAR_DEPTH, 1.0f, 0);
// render // render
RenderModel(t); //RenderModel(t);
const float ClearColorZero[4] = { 0.0f, 0.0f, 0.0f, 1.0f }; const float ClearColorZero[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
ID3D10RenderTargetView * RenderTargets[] = { ID3D10RenderTargetView * RenderTargets[] = {

View File

@ -20,7 +20,7 @@ HRESULT LoadTexture2D(const wchar_t * resourceName,
// texture // texture
HRSRC hRes = FindResource(NULL, resourceName, RT_RCDATA); HRSRC hRes = FindResource(NULL, resourceName, RT_RCDATA);
if (hRes == NULL) { if (hRes == NULL) {
print("FindResource %s\n", resourceName); printW(L"FindResource %s\n", resourceName);
return -1; return -1;
} }
DWORD dwResourceSize = SizeofResource(NULL, hRes); DWORD dwResourceSize = SizeofResource(NULL, hRes);

View File

@ -1398,8 +1398,8 @@ instance_light const instance_lights_node_cube[] = {
}; };
channel const * const node_channels_node_cube[] = { channel const * const node_channels_node_cube[] = {
&node_channel_node_cube_translation_x,
&node_channel_node_cube_translation_y, &node_channel_node_cube_translation_y,
&node_channel_node_cube_translation_x,
}; };
node const node_node_cube = { node const node_node_cube = {
@ -1629,9 +1629,9 @@ instance_light const instance_lights_node_geosphere[] = {
}; };
channel const * const node_channels_node_geosphere[] = { channel const * const node_channels_node_geosphere[] = {
&node_channel_node_geosphere_scale,
&node_channel_node_geosphere_inversescaleaxisrotation, &node_channel_node_geosphere_inversescaleaxisrotation,
&node_channel_node_geosphere_scaleaxisrotation, &node_channel_node_geosphere_scaleaxisrotation,
&node_channel_node_geosphere_scale,
}; };
node const node_node_geosphere = { node const node_node_geosphere = {
@ -1669,9 +1669,9 @@ instance_light const instance_lights_node_light[] = {
}; };
channel const * const node_channels_node_light[] = { channel const * const node_channels_node_light[] = {
&node_channel_node_light_translation_z,
&node_channel_node_light_translation_y, &node_channel_node_light_translation_y,
&node_channel_node_light_translation_x, &node_channel_node_light_translation_x,
&node_channel_node_light_translation_z,
}; };
node const node_node_light = { node const node_node_light = {