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

View File

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

View File

@ -1,20 +1,20 @@
IMAGES += image/american_cherry.DDS
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)" "$@"
IMAGES += image/102.DDS
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)" "$@"
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
texconv10.exe -nologo "$<"
texconv10.exe -f BC1_UNORM -nologo "$<"
mv "$(<:.png=.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
texconv10.exe -nologo "$<"
texconv10.exe -f BC1_UNORM -nologo "$<"
mv "$(<:.jpg=.DDS)" "$@"

View File

@ -37,6 +37,8 @@ namespace collada_scene {
node_instance * m_nodeInstances = NULL;
ID3D10ShaderResourceView ** m_pShaderResourceViews = NULL;
XMFLOAT4 * m_lightPositions = NULL;
XMFLOAT4 * m_lightDirections = NULL;
XMFLOAT4 * m_lightColors = NULL;
@ -51,6 +53,7 @@ namespace collada_scene {
private:
HRESULT load_layouts();
HRESULT allocate_images();
void allocate_node_instances();
void allocate_node_instance(collada::node const * const node,
node_instance * node_instance);
@ -64,6 +67,10 @@ namespace collada_scene {
void render_geometries(collada::instance_geometry const * const instance_geometries,
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();

View File

@ -3,7 +3,7 @@ RES_FONT_FXO RCDATA "font.fxo"
RES_VOLUME_FXO RCDATA "volume.fxo"
RES_BLOOM_FXO RCDATA "bloom.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_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_MODELS_CURVE_INTERPOLATION_VTX RCDATA "scenes/curve_interpolation/curve_interpolation.vtx"
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 "collada_scene.hpp"
#include "new.hpp"
#include "render_state.hpp"
extern ID3D10Device * g_pd3dDevice;
extern XMVECTOR g_Eye;
@ -34,6 +35,13 @@ namespace collada_scene {
ID3D10EffectVectorVariable * g_pSpecularVariable = 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)
{
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 hr;
@ -182,6 +207,10 @@ namespace collada_scene {
if (FAILED(hr))
return E_FAIL;
hr = allocate_images();
if (FAILED(hr))
return E_FAIL;
allocate_node_instances();
allocate_light_instances();
@ -263,6 +292,13 @@ namespace collada_scene {
g_pSpecularVariable = g_pEffect->GetVariableByName("Specular")->AsVector();
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;
}
@ -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) {
case effect_type::BLINN:
g_pEmissionVariable->SetFloatVector((float *)&effect.blinn.emission.color);
g_pAmbientVariable->SetFloatVector((float *)&effect.blinn.ambient.color);
g_pDiffuseVariable->SetFloatVector((float *)&effect.blinn.diffuse.color);
g_pSpecularVariable->SetFloatVector((float *)&effect.blinn.specular.color);
set_color_or_texture(effect.blinn.emission, g_pEmissionVariable, g_pTexEmissionVariable);
set_color_or_texture(effect.blinn.ambient, g_pAmbientVariable, g_pTexAmbientVariable);
set_color_or_texture(effect.blinn.diffuse, g_pDiffuseVariable, g_pTexDiffuseVariable);
set_color_or_texture(effect.blinn.specular, g_pSpecularVariable, g_pTexSpecularVariable);
g_pShininessVariable->SetFloat(effect.blinn.shininess);
break;
case effect_type::LAMBERT:
g_pEmissionVariable->SetFloatVector((float *)&effect.lambert.emission.color);
g_pAmbientVariable->SetFloatVector((float *)&effect.lambert.ambient.color);
g_pDiffuseVariable->SetFloatVector((float *)&effect.lambert.diffuse.color);
set_color_or_texture(effect.lambert.emission, g_pEmissionVariable, g_pTexEmissionVariable);
set_color_or_texture(effect.lambert.ambient, g_pAmbientVariable, g_pTexAmbientVariable);
set_color_or_texture(effect.lambert.diffuse, g_pDiffuseVariable, g_pTexDiffuseVariable);
break;
case effect_type::PHONG:
g_pEmissionVariable->SetFloatVector((float *)&effect.phong.emission.color);
g_pAmbientVariable->SetFloatVector((float *)&effect.phong.ambient.color);
g_pDiffuseVariable->SetFloatVector((float *)&effect.phong.diffuse.color);
g_pSpecularVariable->SetFloatVector((float *)&effect.phong.specular.color);
set_color_or_texture(effect.phong.emission, g_pEmissionVariable, g_pTexEmissionVariable);
set_color_or_texture(effect.phong.ambient, g_pAmbientVariable, g_pTexAmbientVariable);
set_color_or_texture(effect.phong.diffuse, g_pDiffuseVariable, g_pTexDiffuseVariable);
set_color_or_texture(effect.phong.specular, g_pSpecularVariable, g_pTexSpecularVariable);
g_pShininessVariable->SetFloat(effect.phong.shininess);
break;
case effect_type::CONSTANT:
@ -388,7 +440,14 @@ namespace collada_scene {
for (int j = 0; j < instance_geometry.instance_materials_count; j++) {
instance_material const& instance_material = instance_geometry.instance_materials[j];
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_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 diffuseColor;
if (TextureChannel.z >= 0) {
diffuseColor = TexDiffuse.Sample(samLinear, input.Tex).xyz;
} else {
diffuseColor = Diffuse.xyz;
}
for (int i = 0; i < 2; i++) {
float3 light_dir = normalize(-LightDir[i].xyz);
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 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);

View File

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

View File

@ -20,7 +20,7 @@ HRESULT LoadTexture2D(const wchar_t * resourceName,
// texture
HRSRC hRes = FindResource(NULL, resourceName, RT_RCDATA);
if (hRes == NULL) {
print("FindResource %s\n", resourceName);
printW(L"FindResource %s\n", resourceName);
return -1;
}
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[] = {
&node_channel_node_cube_translation_x,
&node_channel_node_cube_translation_y,
&node_channel_node_cube_translation_x,
};
node const node_node_cube = {
@ -1629,9 +1629,9 @@ instance_light const instance_lights_node_geosphere[] = {
};
channel const * const node_channels_node_geosphere[] = {
&node_channel_node_geosphere_scale,
&node_channel_node_geosphere_inversescaleaxisrotation,
&node_channel_node_geosphere_scaleaxisrotation,
&node_channel_node_geosphere_scale,
};
node const node_node_geosphere = {
@ -1669,9 +1669,9 @@ instance_light const instance_lights_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_x,
&node_channel_node_light_translation_z,
};
node const node_node_light = {