Add Instanced Rendering / 2 more scenes

This commit is contained in:
2024-12-28 17:24:11 +01:00
parent 033909656a
commit 5e28e5fff4
30 changed files with 13730 additions and 100 deletions

View File

@@ -5,6 +5,7 @@ set(SOURCES
"src/Renderer.cpp" "src/Renderer.cpp"
"src/Timer.cpp" "src/Timer.cpp"
"src/Mesh.cpp" "src/Mesh.cpp"
"src/InstancedMesh.cpp"
"src/Camera.cpp" "src/Camera.cpp"
"src/Texture.cpp" "src/Texture.cpp"
"src/GamePadController.cpp" "src/GamePadController.cpp"
@@ -22,6 +23,8 @@ set(SOURCES
"src/Scenes/BaseScene.cpp" "src/Scenes/BaseScene.cpp"
"src/Scenes/MainScene.cpp" "src/Scenes/MainScene.cpp"
"src/Scenes/DioramaScene.cpp" "src/Scenes/DioramaScene.cpp"
"src/Scenes/InstancedScene.cpp"
"src/Scenes/PlanetScene.cpp"
) )
SET(INCLUDE_DIRS SET(INCLUDE_DIRS

View File

@@ -0,0 +1,194 @@
SamplerState gSampleState : SampleState;
RasterizerState gRasterizerState : RastState;
float4x4 gWorldViewProj : WorldViewProjection;
float4x4 gWorldMatrix : WorldMatrix;
texture2D gDiffuseMap : DiffuseMap;
texture2D gNormalMap : Normal;
texture2D gSpecularMap : Specular;
texture2D gGlossMap : Gloss;
float3 gLightDirection : LightDirection;
float3 gLightColor : LightColor;
float3 gCameraPosition : CameraPosition;
bool gUseNormal : UseNormal;
static const float3 gAmbient = float3(.03f, .03f, .03f);
static const float gLightIntensity = 7.f;
static const float PI = 3.14159f;
static const float gSpecularReflectance = 1.f;
static const float gShininess = 25.f;
//Input output
struct VS_INPUT {
float3 Position : POSITION;
float2 TexCoord : TEXCOORD;
float3 Normal : NORMAL;
float3 Tangent : TANGENT;
float4 InstanceWorld0 : INSTANCEWORLD0;
float4 InstanceWorld1 : INSTANCEWORLD1;
float4 InstanceWorld2 : INSTANCEWORLD2;
float4 InstanceWorld3 : INSTANCEWORLD3;
float4 InstanceColor : INSTANCECOLOR;
};
struct VS_OUTPUT {
float4 Position : SV_POSITION;
float4 WorldPosition : WORLDPOSITION;
float2 TexCoord : TEXCOORD;
float3 Normal : NORMAL;
float3 Tangent : TANGENT;
float4 Color : COLOR; // Pass instance color to the pixel shader
};
//----------------------
// Rasterizer state
//----------------------
// RasterizerState gRasterizerState
// {
// CullMode = none;
// FrontCounterClockwise = false; //default
// };
//Vertex shader
VS_OUTPUT VS(VS_INPUT input) {
VS_OUTPUT output;
// Reconstruct the world matrix from instance data
float4x4 instanceWorld = float4x4(
input.InstanceWorld0,
input.InstanceWorld1,
input.InstanceWorld2,
input.InstanceWorld3
);
// Transform the position to world space and then to clip space
float4 worldPosition = mul(float4(input.Position, 1.0f), instanceWorld);
output.Position = mul(worldPosition, gWorldViewProj); // Transform to clip space using gViewProjMatrix
output.WorldPosition = worldPosition; // Pass through the
//
// Extract the 3x3 upper portion of the world matrix for normal and tangent transformations
float3x3 instanceWorld3x3 = (float3x3)instanceWorld; // Extract upper 3x3 matrix
output.Normal = normalize(mul(input.Normal, instanceWorld3x3)); // Transform and normalize the normal
output.Tangent = normalize(mul(input.Tangent, instanceWorld3x3)); // Transform and normalize the tangent
// Pass through texture coordinates
output.TexCoord = input.TexCoord;
// Pass through the instance color
output.Color = input.InstanceColor;
return output;
}
float3 Phong(float ks, float exp, float3 l, float3 v, float3 n)
{
float3 reflected = reflect(l, n);
float cosAngle = dot(reflected, v);
return ks * pow(max(0.f, cosAngle), exp) * gLightColor;
}
float3 Lambert(float kd, float3 cd)
{
return (kd * cd) / PI;
}
float3 Shade(VS_OUTPUT input)
{
// Sample diffuse, specular, and gloss maps
float3 diffuseSample = gDiffuseMap.Sample(gSampleState, input.TexCoord).rgb;
float3 specularSample = gSpecularMap.Sample(gSampleState, input.TexCoord).rgb;
float glossSample = gGlossMap.Sample(gSampleState, input.TexCoord).x;
float3 normalSample = gNormalMap.Sample(gSampleState, input.TexCoord).rgb;
// Compute inversed view and light directions
float3 invViewDirection = normalize(gCameraPosition - input.WorldPosition.xyz);
float3 invLightDirection = -gLightDirection;
// Compute tangent space axes if normal mapping is used
float3 normal = input.Normal;
if (gUseNormal) {
float3 binormal = cross(input.Normal, input.Tangent);
float3x3 tangentSpaceAxis = float3x3(input.Tangent, binormal, input.Normal);
// Sample and transform normal map
normal = float3(2.f * normalSample.x - 1.f,
2.f * normalSample.y - 1.f,
2.f * normalSample.z - 1.f);
normal = mul(normal, tangentSpaceAxis);
}
// Compute Lambert diffuse lighting
float3 diffuse = Lambert(gLightIntensity, diffuseSample);
// Compute Phong specular lighting
float ks = (specularSample.x + specularSample.y + specularSample.z) / 3.f;
float3 specular = Phong(ks, glossSample * gShininess, invLightDirection, invViewDirection, normal);
// Compute observed area based on the cosine of the light angle
float cosAngle = dot(invLightDirection, normal);
float3 observedArea = float3(cosAngle, cosAngle, cosAngle);
// Combine lighting components
float3 color = saturate(diffuse * observedArea + specular + gAmbient * cosAngle);
return color;
}
float4 PS(VS_OUTPUT input) : SV_TARGET {
// Normalize vectors
float3 norm = normalize(input.Normal);
float3 lightDir = normalize(gLightDirection);
float3 viewDir = normalize(gCameraPosition - input.WorldPosition.xyz);
// Compute ambient lighting
float3 ambient = gAmbient * gLightColor;
// Compute diffuse lighting
float diff = max(dot(norm, lightDir), 0.0f); // Ensure clamping to non-negative values
float3 diffuse = diff * gLightColor;
// Compute specular lighting
float3 reflectDir = reflect(-lightDir, norm); // Reflect light direction around normal
float spec = pow(max(dot(reflectDir, viewDir), 0.0f), gShininess); // Apply shininess exponent
float3 specular = gSpecularReflectance * spec * gLightColor;
// Combine the lighting components with input color
float3 finalColor = ambient + diffuse + specular;
// return float4(finalColor * input.Color.rgb, input.Color.a); // Multiply by vertex color
return gDiffuseMap.Sample(gSampleState, input.TexCoord);
}
float4 PSNormal(VS_OUTPUT input) : SV_TARGET {
// Normalize the input normal
float3 norm = normalize(input.Normal);
// Map the normal from [-1, 1] to [0, 1]
float3 normalColor = 0.5f * (norm + 1.0f);
// Output the normal as RGB color
return float4(normalColor, 1.0f); // Alpha is set to 1 for full opacity
}
DepthStencilState gDepthStencilState
{
//enable
DepthEnable = true;
DepthWriteMask = ALL;
DepthFunc = LESS;
//stencil
StencilEnable = true;
};
technique11 DefaultTechnique {
pass P0 {
SetDepthStencilState(gDepthStencilState, 0);
SetRasterizerState(gRasterizerState);
SetVertexShader(CompileShader(vs_5_0, VS()));
SetGeometryShader(NULL);
SetPixelShader(CompileShader(ps_5_0, PS()));
}
}

View File

@@ -0,0 +1,12 @@
# Blender 4.3.2 MTL File: 'None'
# www.blender.org
newmtl Grass
Ns 360.000000
Ka 1.000000 1.000000 1.000000
Kd 0.800000 0.800000 0.800000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2

View File

@@ -0,0 +1,34 @@
# Blender 4.3.2
# www.blender.org
mtllib cube.mtl
o Cube
v -1.000000 -1.000000 1.000000
v -1.000000 1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v -1.000000 1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v 1.000000 1.000000 1.000000
v 1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -1.000000
vn -1.0000 -0.0000 -0.0000
vn -0.0000 -0.0000 -1.0000
vn 1.0000 -0.0000 -0.0000
vn -0.0000 -0.0000 1.0000
vn -0.0000 -1.0000 -0.0000
vn -0.0000 1.0000 -0.0000
vt 0.500000 0.500000
vt 0.500000 1.000000
vt 1.000000 1.000000
vt 1.000000 0.500000
vt 0.000000 1.000000
vt 0.000000 0.500000
vt 0.500000 0.000000
vt 1.000000 0.000000
s 1
usemtl Grass
f 1/1/1 2/2/1 4/3/1 3/4/1
f 3/1/2 4/2/2 8/3/2 7/4/2
f 7/4/3 8/3/3 6/2/3 5/1/3
f 5/4/4 6/3/4 2/2/4 1/1/4
f 3/5/5 7/2/5 5/1/5 1/6/5
f 8/4/6 4/1/6 2/7/6 6/8/6

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 MiB

View File

@@ -0,0 +1,13 @@
# Blender MTL File: 'None'
# Material Count: 1
newmtl Mars
Ns 96.078443
Ka 1.000000 1.000000 1.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ke 0.0 0.0 0.0
Ni 1.000000
d 1.000000
illum 2
map_Kd mars.png

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,12 @@
# Blender MTL File: 'Rock1.blend'
# Material Count: 1
newmtl Material
Ns 13.725490
Ka 0.000000 0.000000 0.000000
Kd 0.640000 0.640000 0.640000
Ks 0.007937 0.007937 0.007937
Ni 1.000000
d 1.000000
illum 2
map_Bump rock.png

View File

@@ -0,0 +1,559 @@
# Blender v2.71 (sub 0) OBJ File: 'Rock1.blend'
# www.blender.org
mtllib rock.mtl
o Cube
v 0.896930 -0.116701 -1.078061
v 0.736314 -0.076033 1.066762
v -1.052088 -0.064600 0.954513
v -0.692441 -0.114299 -0.835835
v 1.303753 1.083753 -1.023542
v 0.444005 1.032712 1.513821
v -1.019775 1.174950 1.105961
v -0.681862 1.180458 -1.481573
v 1.045407 -0.157597 -0.147996
v 0.067265 -0.072713 -0.783522
v 1.200361 0.538245 -1.221685
v -0.154563 -0.114500 1.228987
v 1.155268 0.478039 1.381585
v -1.094633 -0.254018 -0.118459
v -1.648060 0.452009 0.853586
v -1.185727 0.481559 -1.133555
v 0.975431 1.246558 0.047895
v 0.105258 1.126820 -1.585067
v -0.344848 1.152380 1.730000
v -1.177093 1.303243 0.009798
v -0.015789 -0.312706 -0.060242
v 0.046107 1.351565 -0.253730
v 1.175182 0.692469 0.169032
v 0.260077 0.452640 1.365451
v -1.648881 0.429819 -0.064031
v -0.199824 0.336820 -1.536424
v 1.008866 -0.126459 0.470257
v -0.321458 -0.060305 -0.702046
v 1.385825 0.807576 -1.181814
v -0.537873 -0.057997 1.154378
v 0.936321 0.677546 1.426494
v -0.862364 -0.219203 -0.537108
v -1.281588 0.883991 1.218077
v -0.974527 0.926074 -1.431442
v 0.808457 1.346412 0.902078
v -0.174992 1.147173 -1.801163
v -0.610285 1.147948 1.327881
v -1.356023 1.245036 -0.716849
v 1.042022 -0.190494 -0.637866
v 0.468949 -0.142859 -1.026806
v 1.081272 0.175115 -1.287556
v 0.243586 -0.141476 1.191072
v 1.014587 0.183491 1.312948
v -1.155731 -0.217847 0.459469
v -1.571341 0.110279 0.770709
v -1.063276 0.074521 -1.015607
v 1.231954 1.216866 -0.594471
v 0.770454 1.122192 -1.299703
v -0.065279 1.102928 1.796044
v -1.176430 1.281231 0.646380
v -0.476225 -0.274043 -0.037390
v 0.480423 -0.312115 -0.111060
v 0.172793 -0.188473 -0.532460
v -0.106274 -0.274351 0.595963
v -0.198010 1.360380 0.630818
v 0.062998 1.276304 -0.994314
v 0.611501 1.352344 -0.290780
v -0.603343 1.343584 -0.011276
v 1.267461 0.630730 1.008356
v 1.267107 0.600208 -0.689727
v 1.268899 0.241091 -0.063884
v 1.050912 1.068502 0.197512
v -1.043570 0.402928 1.289593
v 0.667029 0.439932 1.448684
v -0.135120 0.156677 1.505020
v -0.066870 0.652060 1.602193
v -1.759897 0.319614 -0.724614
v -1.646483 0.462612 0.459024
v -1.548443 -0.153801 -0.255587
v -1.483055 1.068517 0.045330
v -0.775458 0.497559 -1.402503
v 0.556317 0.449266 -1.496503
v 0.153226 0.839249 -1.832230
v -0.180515 0.060946 -1.233899
v -0.630920 -0.226328 0.592326
v -0.677921 1.329703 0.698183
v 1.115930 1.067893 0.980967
v -0.944509 0.743625 1.392342
v -1.719440 0.916506 -0.699776
v -0.751048 0.149059 -1.123006
v -0.359688 -0.219676 -0.596748
v 0.548058 -0.284113 -0.657622
v 0.383454 -0.298149 0.533170
v 0.342781 1.370886 0.863692
v 0.801564 1.333314 -0.716665
v -0.544178 1.290048 -0.889409
v 1.347667 0.189763 0.746821
v 1.323792 0.127379 -0.856060
v 1.423543 0.897206 -0.707035
v -0.706694 0.190636 1.346937
v 0.494274 0.153436 1.399456
v 0.524189 0.691410 1.410453
v -1.346437 -0.078697 -0.808084
v -1.550380 0.033513 0.487642
v -1.434319 1.017100 0.746778
v -0.263198 0.916780 -1.716774
v 0.802037 0.850318 -1.528586
v 0.512712 0.114694 -1.198383
vt 0.058285 0.569867
vt 0.082613 0.506661
vt 0.152906 0.498683
vt 0.206316 0.378186
vt 0.155630 0.402329
vt 0.099783 0.387655
vt 0.854473 0.349296
vt 0.794976 0.248970
vt 0.870663 0.174892
vt 0.944083 0.694636
vt 0.905653 0.682602
vt 0.916733 0.604517
vt 0.336809 0.082005
vt 0.369246 0.000842
vt 0.489191 0.000000
vt 0.512673 0.310590
vt 0.481603 0.323059
vt 0.447322 0.276247
vt 0.276846 0.583529
vt 0.215363 0.578539
vt 0.223692 0.502410
vt 0.288525 0.695621
vt 0.226456 0.694739
vt 0.220811 0.635843
vt 0.149628 0.688747
vt 0.060049 0.668982
vt 0.053708 0.620010
vt 0.115533 0.299946
vt 0.042023 0.402498
vt 0.011393 0.383384
vt 0.133867 0.094672
vt 0.226136 0.127633
vt 0.165692 0.199396
vt 0.273683 0.187557
vt 0.335575 0.267841
vt 0.258354 0.321530
vt 0.422948 0.654063
vt 0.520455 0.684734
vt 0.477461 0.726331
vt 0.422729 0.459207
vt 0.529375 0.479105
vt 0.539575 0.580737
vt 0.744126 0.814485
vt 0.672331 0.787452
vt 0.683844 0.711964
vt 0.016271 0.536299
vt 0.006598 0.482318
vt 0.054401 0.402498
vt 0.855242 0.125920
vt 0.818683 0.137473
vt 0.786060 0.070157
vt 0.829103 0.182611
vt 0.720806 0.251159
vt 0.693981 0.268226
vt 0.348488 0.225548
vt 0.431026 0.176751
vt 0.821728 0.494375
vt 0.758928 0.488146
vt 0.747834 0.361119
vt 0.853441 0.628842
vt 0.844101 0.700647
vt 0.766741 0.707183
vt 0.554998 0.057463
vt 0.491069 0.166304
vt 0.661203 0.071636
vt 0.642437 0.169965
vt 0.565426 0.199969
vt 0.619708 0.321108
vt 0.560004 0.310321
vt 0.556413 0.271543
vt 0.137636 0.563411
vt 0.140662 0.629249
vt 0.155028 0.344089
vt 0.206193 0.274302
vt 0.896175 0.282885
vt 0.907061 0.133559
vt 0.622627 0.581780
vt 0.617335 0.678426
vt 0.662917 0.562066
vt 0.672331 0.661064
vt 0.949240 0.587705
vt 0.869574 0.490922
vt 0.791582 0.866218
vt 0.744126 0.821215
vt 0.752056 0.707183
vt 0.999879 0.706758
vt 0.460672 0.063535
vt 0.766456 0.640000
vt 0.672331 0.596657
vt 0.677773 0.459778
vt 0.674914 0.687648
vt 0.485748 0.254952
vt 0.336809 0.833560
vt 0.336920 0.785916
vt 0.384519 0.726331
vt 0.506205 0.350977
vt 0.448079 0.354288
vt 0.559475 0.342029
vt 0.266533 0.646047
vt 0.609960 0.361119
vt 0.278588 0.749240
vt 0.320949 0.673747
vt 0.218381 0.752553
vt 0.143231 0.752483
vt 0.069266 0.722735
vt 0.808347 0.361119
vt 0.116225 0.149680
vt 0.046020 0.277128
vt 0.000000 0.241327
vt 0.056221 0.145884
vt 0.187902 0.035191
vt 0.085645 0.065345
vt 0.273048 0.064123
vt 0.316518 0.068429
vt 0.336809 0.140426
vt 0.340137 0.610197
vt 0.404599 0.713241
vt 0.339988 0.536299
vt 0.439840 0.553878
vt 0.429528 0.400325
vt 0.336833 0.476818
vt 0.517650 0.412671
vt 0.604166 0.482239
vt 0.586459 0.423461
vt 0.646477 0.441109
vt 0.670954 0.491413
vt 0.090851 0.431859
vt 0.000000 0.608449
vt 0.694133 0.143704
vt 0.012308 0.682984
vt 0.019832 0.742597
vt 0.857058 0.070133
vt 0.758736 0.168010
vt 0.408067 0.318081
vt 0.232244 0.440506
vt 0.291247 0.454530
vt 0.270084 0.523882
vt 0.685316 0.388716
vt 0.130148 0.436240
vt 0.834672 0.401576
vt 0.563922 0.000091
vt 0.582099 0.007382
vt 0.608937 0.072162
vt 0.207942 0.000000
vt 0.293309 0.025246
vt 0.579472 0.365264
vt 0.477201 0.361119
vt 0.628288 0.255903
vt 0.693981 0.150197
vt 0.693822 0.237100
vt 0.668575 0.311882
vt 0.731678 0.707183
vt 0.934356 0.187628
vt 0.939564 0.506329
vt 0.791426 0.711743
vt 1.000000 0.606532
vt 0.387737 0.801509
vt 0.324683 0.722893
vt 0.116277 0.014773
vt 0.336809 0.674909
vt 0.353162 0.419686
vt 0.736508 0.092307
vt 0.913859 0.076969
vt 0.867108 0.417288
vt 0.639569 0.392860
vn -0.233500 -0.819800 0.522800
vn -0.341800 -0.625700 0.701200
vn -0.276700 -0.937700 0.209800
vn -0.424000 0.890700 0.163900
vn -0.339200 0.818400 0.463900
vn -0.460600 0.772900 0.436500
vn 0.378500 0.884900 0.271500
vn 0.471200 0.579600 0.664800
vn 0.320600 0.158900 0.933800
vn -0.646800 0.288800 0.705800
vn -0.900700 0.371900 -0.224300
vn -0.458400 0.862700 -0.213600
vn -0.301000 0.819900 -0.486800
vn 0.010700 -0.806900 -0.590500
vn 0.230000 -0.694400 -0.681800
vn -0.231200 -0.595000 -0.769700
vn 0.016800 -0.846500 -0.532100
vn -0.040300 -0.998600 -0.032900
vn -0.122100 -0.991600 -0.041200
vn -0.012200 -0.971900 -0.235000
vn 0.122300 -0.992400 0.015000
vn -0.071000 -0.993000 -0.094000
vn 0.122200 -0.981400 0.148100
vn 0.080500 -0.849300 0.521600
vn -0.086200 -0.849400 0.520600
vn -0.046700 0.996400 0.070500
vn -0.217700 0.494400 0.841500
vn -0.086400 0.146500 0.985400
vn 0.178300 0.970700 -0.161300
vn 0.007300 0.987200 -0.159100
vn -0.027300 0.998100 -0.054800
vn -0.044300 0.991500 -0.121900
vn -0.344700 0.938400 0.022500
vn 0.921600 -0.360400 0.143800
vn 0.959300 0.222600 0.173800
vn 0.648500 -0.032100 0.760500
vn 0.919900 -0.291200 -0.262500
vn 0.985400 -0.162400 0.050100
vn 0.963400 0.267200 -0.018400
vn 0.993300 0.035300 0.109500
vn 0.638100 0.769000 0.038600
vn 0.596700 0.799100 0.073100
vn -0.326300 -0.473800 0.818000
vn -0.434100 -0.329200 0.838600
vn -0.913600 0.089800 0.396600
vn 0.126700 -0.032600 0.991400
vn -0.189100 -0.186900 0.964000
vn -0.073600 -0.184200 0.980100
vn 0.114700 -0.242200 0.963400
vn -0.899000 -0.340400 -0.275600
vn -0.479500 -0.331900 -0.812300
vn -0.993700 0.107100 0.034300
vn -0.999200 0.000100 0.039900
vn -0.739400 -0.654700 -0.156500
vn -0.824700 0.505000 0.254600
vn -0.218100 -0.181700 -0.958800
vn -0.432200 -0.296800 -0.851500
vn 0.426900 0.149400 -0.891800
vn 0.225900 -0.430300 -0.873900
vn -0.117000 -0.587100 -0.801000
vn -0.076900 -0.842600 -0.532900
vn -0.211900 -0.938300 -0.273300
vn -0.131600 -0.849400 -0.511000
vn -0.085500 -0.979800 0.180600
vn -0.055900 -0.987800 0.145500
vn -0.093600 0.986700 0.133100
vn -0.043400 0.998800 -0.021100
vn 0.752400 0.571000 0.328300
vn 0.940500 0.338300 0.030400
vn -0.364600 -0.020200 0.930900
vn -0.006300 -0.522900 0.852400
vn -0.549100 0.086400 -0.831300
vn -0.818900 0.564900 0.101300
vn -0.109100 -0.775000 -0.622500
vn -0.043500 -0.970400 -0.237300
vn -0.216400 -0.916000 -0.337700
vn 0.483000 -0.867200 -0.120600
vn 0.586200 -0.808400 0.052800
vn 0.453300 -0.878500 0.150900
vn 0.302100 -0.888100 0.346500
vn -0.011700 0.990200 0.139300
vn 0.104700 0.994500 -0.006000
vn 0.287400 0.799500 -0.527400
vn 0.226500 0.797300 -0.559400
vn 0.166900 0.398200 -0.901900
vn 0.449900 -0.591100 0.669400
vn 0.991200 -0.131900 -0.006400
vn 0.421700 -0.247600 -0.872200
vn 0.719900 -0.329000 -0.611100
vn 0.873300 -0.027000 -0.486400
vn 0.695900 0.658800 -0.285700
vn -0.735800 -0.552500 0.391500
vn 0.149300 -0.476900 0.866200
vn -0.467200 -0.714600 -0.520600
vn -0.791300 -0.591900 0.152900
vn 0.150800 0.022200 -0.988300
vn 0.072000 -0.564800 -0.822100
vn 0.246700 -0.812300 -0.528500
usemtl Material
s 1
f 30/1/1 3/2/2 44/3/3
f 50/4/4 7/5/5 37/6/6
f 35/7/7 6/8/8 31/9/9
f 37/10/6 7/11/5 33/12/10
f 79/13/11 38/14/12 8/15/13
f 28/16/14 4/17/15 46/18/16
f 81/19/17 51/20/18 14/21/19
f 82/22/20 52/23/21 21/24/22
f 83/25/23 42/26/24 12/27/25
f 55/28/26 19/29/27 49/30/28
f 85/31/29 56/32/30 22/33/31
f 86/34/32 38/35/12 20/36/33
f 87/37/34 59/38/35 13/39/36
f 88/40/37 60/41/38 23/42/39
f 89/43/40 47/44/41 17/45/42
f 90/46/43 63/47/44 15/48/45
f 64/49/46 24/50/47 65/51/48
f 92/52/49 49/53/28 19/54/27
f 67/55/50 16/56/51 46/18/16
f 68/57/52 25/58/53 69/59/54
f 95/60/55 50/61/4 20/62/33
f 96/63/56 71/64/57 16/56/51
f 97/65/58 72/66/59 26/67/60
f 40/68/61 10/69/62 74/70/63
f 75/71/64 44/3/3 14/21/19
f 21/24/22 54/72/65 75/71/64
f 12/27/25 30/1/1 75/71/64
f 76/73/66 37/6/6 19/29/27
f 22/33/31 58/74/67 76/73/66
f 58/74/67 20/36/33 50/4/4
f 77/75/68 31/9/9 13/76/36
f 62/77/69 77/78/68 59/38/35
f 17/79/42 35/80/7 77/78/68
f 78/81/70 33/12/10 15/82/45
f 24/83/47 66/84/71 78/85/70
f 19/86/27 37/10/6 78/81/70
f 67/55/50 79/13/11 34/87/72
f 70/88/73 79/89/11 67/90/50
f 20/62/33 38/91/12 79/89/11
f 71/64/57 80/92/74 46/18/16
f 74/70/63 80/92/74 71/64/57
f 10/93/62 28/94/14 80/95/74
f 28/16/14 81/96/17 32/97/75
f 53/98/76 81/96/17 28/16/14
f 53/99/76 21/24/22 51/20/18
f 40/68/61 82/100/20 53/98/76
f 39/101/77 82/22/20 40/102/61
f 39/101/77 9/103/78 52/23/21
f 52/23/21 83/25/23 54/72/65
f 9/103/78 27/104/79 83/25/23
f 27/104/79 2/105/80 42/26/24
f 35/7/7 84/106/81 49/53/28
f 57/107/82 84/108/81 35/109/7
f 57/107/82 22/33/31 55/28/26
f 85/31/29 57/107/82 17/110/42
f 48/111/83 85/31/29 47/112/41
f 18/113/84 56/32/30 85/31/29
f 56/32/30 86/34/32 58/74/67
f 36/114/85 86/34/32 56/32/30
f 36/114/85 8/115/13 38/35/12
f 27/116/79 87/37/34 43/117/86
f 9/118/78 61/119/87 87/37/34
f 61/119/87 23/42/39 59/38/35
f 88/40/37 61/119/87 9/118/78
f 41/120/88 88/40/37 39/121/77
f 41/120/88 11/122/89 60/41/38
f 60/41/38 89/123/40 62/77/69
f 11/122/89 29/124/90 89/123/40
f 29/124/90 5/125/91 47/126/41
f 30/1/1 90/46/43 45/127/92
f 12/27/25 65/128/48 90/46/43
f 65/51/48 24/50/47 63/129/44
f 91/130/93 65/128/48 12/27/25
f 43/131/86 91/130/93 42/26/24
f 13/76/36 64/49/46 91/132/93
f 64/49/46 92/52/49 66/133/71
f 13/76/36 31/9/9 92/52/49
f 31/9/9 6/8/8 49/53/28
f 93/134/94 46/18/16 4/17/15
f 69/135/54 93/136/94 32/137/75
f 25/58/53 67/90/50 93/138/94
f 44/3/3 94/139/95 69/135/54
f 3/2/2 45/127/92 94/139/95
f 15/82/45 68/57/52 94/140/95
f 95/60/55 70/88/73 25/58/53
f 33/12/10 95/60/55 68/57/52
f 33/12/10 7/11/5 50/61/4
f 36/141/85 96/63/56 34/87/72
f 18/142/84 73/143/96 96/63/56
f 73/143/96 26/67/60 71/64/57
f 48/111/83 97/144/58 73/145/96
f 5/125/91 29/124/90 97/146/58
f 29/124/90 11/122/89 72/147/59
f 72/66/59 98/148/97 74/70/63
f 11/149/89 41/150/88 98/148/97
f 41/150/88 1/151/98 40/68/61
f 75/71/64 30/1/1 44/3/3
f 76/73/66 50/4/4 37/6/6
f 77/75/68 35/7/7 31/9/9
f 78/81/70 37/10/6 33/12/10
f 34/87/72 79/13/11 8/15/13
f 80/92/74 28/16/14 46/18/16
f 32/137/75 81/19/17 14/21/19
f 53/99/76 82/22/20 21/24/22
f 54/72/65 83/25/23 12/27/25
f 84/108/81 55/28/26 49/30/28
f 57/107/82 85/31/29 22/33/31
f 58/74/67 86/34/32 20/36/33
f 43/117/86 87/37/34 13/39/36
f 61/119/87 88/40/37 23/42/39
f 62/152/69 89/43/40 17/45/42
f 45/127/92 90/46/43 15/48/45
f 91/132/93 64/49/46 65/51/48
f 66/133/71 92/52/49 19/54/27
f 93/134/94 67/55/50 46/18/16
f 94/140/95 68/57/52 69/59/54
f 70/88/73 95/60/55 20/62/33
f 34/87/72 96/63/56 16/56/51
f 73/143/96 97/65/58 26/67/60
f 98/148/97 40/68/61 74/70/63
f 51/20/18 75/71/64 14/21/19
f 51/20/18 21/24/22 75/71/64
f 54/72/65 12/27/25 75/71/64
f 55/28/26 76/73/66 19/29/27
f 55/28/26 22/33/31 76/73/66
f 76/73/66 58/74/67 50/4/4
f 59/153/35 77/75/68 13/76/36
f 23/42/39 62/77/69 59/38/35
f 62/77/69 17/79/42 77/78/68
f 63/154/44 78/81/70 15/82/45
f 63/155/44 24/83/47 78/85/70
f 66/156/71 19/86/27 78/81/70
f 16/56/51 67/55/50 34/87/72
f 25/58/53 70/88/73 67/90/50
f 70/88/73 20/62/33 79/89/11
f 16/56/51 71/64/57 46/18/16
f 26/67/60 74/70/63 71/64/57
f 74/157/63 10/93/62 80/95/74
f 4/17/15 28/16/14 32/97/75
f 10/69/62 53/98/76 28/16/14
f 81/19/17 53/99/76 51/20/18
f 10/69/62 40/68/61 53/98/76
f 1/158/98 39/101/77 40/102/61
f 82/22/20 39/101/77 52/23/21
f 21/24/22 52/23/21 54/72/65
f 52/23/21 9/103/78 83/25/23
f 83/25/23 27/104/79 42/26/24
f 6/8/8 35/7/7 49/53/28
f 17/110/42 57/107/82 35/109/7
f 84/108/81 57/107/82 55/28/26
f 47/112/41 85/31/29 17/110/42
f 5/159/91 48/111/83 47/112/41
f 48/111/83 18/113/84 85/31/29
f 22/33/31 56/32/30 58/74/67
f 18/113/84 36/114/85 56/32/30
f 86/34/32 36/114/85 38/35/12
f 2/160/80 27/116/79 43/117/86
f 27/116/79 9/118/78 87/37/34
f 87/37/34 61/119/87 59/38/35
f 39/121/77 88/40/37 9/118/78
f 1/161/98 41/120/88 39/121/77
f 88/40/37 41/120/88 60/41/38
f 23/42/39 60/41/38 62/77/69
f 60/41/38 11/122/89 89/123/40
f 89/123/40 29/124/90 47/126/41
f 3/2/2 30/1/1 45/127/92
f 30/1/1 12/27/25 90/46/43
f 90/162/43 65/51/48 63/129/44
f 42/26/24 91/130/93 12/27/25
f 2/105/80 43/131/86 42/26/24
f 43/163/86 13/76/36 91/132/93
f 24/50/47 64/49/46 66/133/71
f 64/49/46 13/76/36 92/52/49
f 92/52/49 31/9/9 49/53/28
f 32/97/75 93/134/94 4/17/15
f 14/21/19 69/135/54 32/137/75
f 69/59/54 25/58/53 93/138/94
f 14/21/19 44/3/3 69/135/54
f 44/3/3 3/2/2 94/139/95
f 45/164/92 15/82/45 94/140/95
f 68/57/52 95/60/55 25/58/53
f 15/82/45 33/12/10 68/57/52
f 95/60/55 33/12/10 50/61/4
f 8/15/13 36/141/85 34/87/72
f 36/141/85 18/142/84 96/63/56
f 96/63/56 73/143/96 71/64/57
f 18/113/84 48/111/83 73/145/96
f 48/165/83 5/125/91 97/146/58
f 97/146/58 29/124/90 72/147/59
f 26/67/60 72/66/59 74/70/63
f 72/66/59 11/149/89 98/148/97
f 98/148/97 41/150/88 40/68/61

Binary file not shown.

After

Width:  |  Height:  |  Size: 738 KiB

View File

@@ -0,0 +1,14 @@
# Blender 4.3.2 MTL File: 'None'
# www.blender.org
newmtl Sphere
Ns 360.000000
Ka 1.000000 1.000000 1.000000
Kd 0.800000 0.800000 0.800000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
map_Kd space_nebula_6k.png

File diff suppressed because it is too large Load Diff

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 MiB

View File

@@ -192,3 +192,29 @@ dae::Matrix dae::Camera::GetViewProjectionMatrix() const {
const dae::Vector3 &dae::Camera::GetPosition() { const dae::Vector3 &dae::Camera::GetPosition() {
return origin; return origin;
} }
const dae::Vector3 dae::Camera::GetRotation() const {
return {totalPitch, totalYaw, 0.f};
}
void dae::Camera::SetPosition(const dae::Vector3 &position) {
origin = position;
CalculateViewMatrix();
CalculateProjectionMatrix();
}
void dae::Camera::SetRotation(const dae::Vector3 &rotation) {
totalPitch = rotation.x;
totalYaw = rotation.y;
const Matrix yawMatrix{Matrix::CreateRotationY(totalYaw)};
const Matrix pitchMatrix{Matrix::CreateRotationX(totalPitch)};
const Matrix finalRotation{pitchMatrix * yawMatrix};
forward = finalRotation.TransformVector(Vector3::UnitZ);
forward.Normalize();
CalculateViewMatrix();
CalculateProjectionMatrix();
}

View File

@@ -30,6 +30,10 @@ namespace dae {
Matrix GetViewProjectionMatrix() const; Matrix GetViewProjectionMatrix() const;
const Vector3 &GetPosition(); const Vector3 &GetPosition();
void SetPosition(const Vector3& position);
const Vector3 GetRotation() const;
void SetRotation(const Vector3& rotation);
private: private:
Vector3 origin{}; Vector3 origin{};

View File

@@ -0,0 +1,228 @@
#include <cassert>
#include "InstancedMesh.h"
InstancedMesh::InstancedMesh(ID3D11Device *devicePtr, const std::vector<VertexIn> &verticesIn, const std::vector<Uint32> &indices,
std::shared_ptr<Material> material, BaseEffect *effectPtr, const std::vector<InstancedData> &instanceData)
: m_EffectPtr(effectPtr),
m_InputLayoutPtr(nullptr),
m_VertexBufferPtr(nullptr),
m_IndexBufferPtr(nullptr),
m_InstanceBufferPtr(nullptr),
m_VerticesIn(verticesIn),
m_Indices(indices),
m_InstancedData(instanceData),
m_InstanceCount(static_cast<UINT>(instanceData.size())),
m_IndicesCount(static_cast<UINT>(m_Indices.size())),
m_Material(std::move(material)) {
HRESULT result;
D3D11_BUFFER_DESC bufferDesc{};
D3D11_SUBRESOURCE_DATA subresourceData{};
m_EffectPtr->SetMaterial(m_Material.get());
//Create vertex layout
static constexpr uint32_t vertexElementCount{9}; // 4 for vertex data + 4 for world matrix + 1 for instance color
D3D11_INPUT_ELEMENT_DESC vertexDesc[vertexElementCount]{};
// Vertex data
vertexDesc[0].SemanticName = "POSITION";
vertexDesc[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
vertexDesc[0].AlignedByteOffset = 0;
vertexDesc[0].InputSlot = 0;
vertexDesc[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
vertexDesc[1].SemanticName = "TEXCOORD";
vertexDesc[1].Format = DXGI_FORMAT_R32G32_FLOAT;
vertexDesc[1].AlignedByteOffset = offsetof(VertexIn, uv);
vertexDesc[1].InputSlot = 0;
vertexDesc[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
vertexDesc[2].SemanticName = "NORMAL";
vertexDesc[2].Format = DXGI_FORMAT_R32G32B32_FLOAT;
vertexDesc[2].AlignedByteOffset = offsetof(VertexIn, normal);
vertexDesc[2].InputSlot = 0;
vertexDesc[2].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
vertexDesc[3].SemanticName = "TANGENT";
vertexDesc[3].Format = DXGI_FORMAT_R32G32B32_FLOAT;
vertexDesc[3].AlignedByteOffset = offsetof(VertexIn, tangent);
vertexDesc[3].InputSlot = 0;
vertexDesc[3].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
// Instance world matrix (split into four float4 components)
vertexDesc[4].SemanticName = "INSTANCEWORLD";
vertexDesc[4].SemanticIndex = 0; // First row of the matrix
vertexDesc[4].Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
vertexDesc[4].AlignedByteOffset = 0;
vertexDesc[4].InputSlot = 1;
vertexDesc[4].InputSlotClass = D3D11_INPUT_PER_INSTANCE_DATA;
vertexDesc[4].InstanceDataStepRate = 1;
vertexDesc[5].SemanticName = "INSTANCEWORLD";
vertexDesc[5].SemanticIndex = 1; // Second row of the matrix
vertexDesc[5].Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
vertexDesc[5].AlignedByteOffset = 16; // 4 floats * 4 bytes per float
vertexDesc[5].InputSlot = 1;
vertexDesc[5].InputSlotClass = D3D11_INPUT_PER_INSTANCE_DATA;
vertexDesc[5].InstanceDataStepRate = 1;
vertexDesc[6].SemanticName = "INSTANCEWORLD";
vertexDesc[6].SemanticIndex = 2; // Third row of the matrix
vertexDesc[6].Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
vertexDesc[6].AlignedByteOffset = 32;
vertexDesc[6].InputSlot = 1;
vertexDesc[6].InputSlotClass = D3D11_INPUT_PER_INSTANCE_DATA;
vertexDesc[6].InstanceDataStepRate = 1;
vertexDesc[7].SemanticName = "INSTANCEWORLD";
vertexDesc[7].SemanticIndex = 3; // Fourth row of the matrix
vertexDesc[7].Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
vertexDesc[7].AlignedByteOffset = 48;
vertexDesc[7].InputSlot = 1;
vertexDesc[7].InputSlotClass = D3D11_INPUT_PER_INSTANCE_DATA;
vertexDesc[7].InstanceDataStepRate = 1;
// Instance color
vertexDesc[8].SemanticName = "INSTANCECOLOR";
vertexDesc[8].Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
vertexDesc[8].AlignedByteOffset = 64; // After the matrix
vertexDesc[8].InputSlot = 1;
vertexDesc[8].InputSlotClass = D3D11_INPUT_PER_INSTANCE_DATA;
vertexDesc[8].InstanceDataStepRate = 1;
bufferDesc.Usage = D3D11_USAGE_DYNAMIC; // Allow updates
bufferDesc.ByteWidth = sizeof(InstancedData) * m_InstanceCount; // Correct buffer size calculation
bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
bufferDesc.MiscFlags = 0;
subresourceData.pSysMem = m_InstancedData.data();
result = devicePtr->CreateBuffer(&bufferDesc, &subresourceData, &m_InstanceBufferPtr);
assert(result == S_OK && "Creating instance buffer failed");
//Create input layout
D3DX11_PASS_DESC passDesc{};
ID3DX11EffectTechnique *techniquePtr = m_EffectPtr->GetTechniquePtr();
techniquePtr->GetPassByIndex(0)->GetDesc(&passDesc);
result = devicePtr->CreateInputLayout(
vertexDesc,
vertexElementCount,
passDesc.pIAInputSignature,
passDesc.IAInputSignatureSize,
&m_InputLayoutPtr);
assert(result == S_OK && "Creating input layout failed");
//Create vertex buffer
bufferDesc.Usage = D3D11_USAGE_IMMUTABLE;
bufferDesc.ByteWidth = sizeof(VertexIn) * static_cast<uint32_t>(verticesIn.size());
bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bufferDesc.CPUAccessFlags = 0;
bufferDesc.MiscFlags = 0;
subresourceData.pSysMem = verticesIn.data();
result = devicePtr->CreateBuffer(&bufferDesc, &subresourceData, &m_VertexBufferPtr);
assert(result == S_OK && "Creating vertex buffer failed");
//Create index buffer
bufferDesc.Usage = D3D11_USAGE_IMMUTABLE;
bufferDesc.ByteWidth = sizeof(uint32_t) * m_IndicesCount;
bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
bufferDesc.CPUAccessFlags = 0;
bufferDesc.MiscFlags = 0;
subresourceData.pSysMem = m_Indices.data();
result = devicePtr->CreateBuffer(&bufferDesc, &subresourceData, &m_IndexBufferPtr);
assert(result == S_OK && "Creating index buffer failed");
}
InstancedMesh::~InstancedMesh() {
m_InputLayoutPtr->Release();
m_InputLayoutPtr = nullptr;
m_VertexBufferPtr->Release();
m_VertexBufferPtr = nullptr;
m_IndexBufferPtr->Release();
m_IndexBufferPtr = nullptr;
if (m_InstanceBufferPtr) {
m_InstanceBufferPtr->Release();
m_InstanceBufferPtr = nullptr;
}
delete m_EffectPtr;
m_EffectPtr = nullptr;
m_Material.reset();
m_InstancedData.clear();
m_VerticesIn.clear();
}
void InstancedMesh::Render(ID3D11DeviceContext *deviceContextPtr, const Matrix &worldViewProj) const {
m_EffectPtr->SetWorldViewProjMatrix(worldViewProj);
deviceContextPtr->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
deviceContextPtr->IASetInputLayout(m_InputLayoutPtr);
constexpr UINT vertexStride = sizeof(VertexIn);
constexpr UINT vertexOffset = 0;
constexpr UINT instanceStride = sizeof(InstancedData);
constexpr UINT instanceOffset = 0;
ID3D11Buffer *buffers[] = {m_VertexBufferPtr, m_InstanceBufferPtr};
UINT strides[] = {vertexStride, instanceStride};
UINT offsets[] = {vertexOffset, instanceOffset};
deviceContextPtr->IASetVertexBuffers(0, 2, buffers, strides, offsets);
deviceContextPtr->IASetIndexBuffer(m_IndexBufferPtr, DXGI_FORMAT_R32_UINT, 0);
// Draw using instancing
D3DX11_TECHNIQUE_DESC techniqueDesc{};
m_EffectPtr->GetTechniquePtr()->GetDesc(&techniqueDesc);
for (UINT p{}; p < techniqueDesc.Passes; p++) {
m_EffectPtr->GetTechniquePtr()->GetPassByIndex(p)->Apply(0, deviceContextPtr);
deviceContextPtr->DrawIndexedInstanced(m_IndicesCount, m_InstanceCount, 0, 0, 0);
}
}
void InstancedMesh::UpdateInstanceData(ID3D11DeviceContext *deviceContextPtr, const std::vector<InstancedData> &instanceData) {
assert(instanceData.size() <= m_InstanceCount && "Instance data exceeds buffer size");
D3D11_MAPPED_SUBRESOURCE mappedResource{};
HRESULT result = deviceContextPtr->Map(m_InstanceBufferPtr, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
assert(result == S_OK && "Mapping instance buffer failed");
assert(mappedResource.pData != nullptr && "Mapped resource data is null");
if (!instanceData.empty()) {
// Ensure that the data fits in the buffer
std::memcpy(mappedResource.pData, instanceData.data(), sizeof(InstancedData) * instanceData.size());
}
deviceContextPtr->Unmap(m_InstanceBufferPtr, 0);
}
void InstancedMesh::SetCameraPos(const Vector3 &pos) const {
m_EffectPtr->SetCameraPos(pos);
}
void InstancedMesh::SetWorldMatrix(const Matrix &matrix) {
m_WorldMatrix = matrix;
}
Material *InstancedMesh::GetMaterial() const { return m_Material.get(); }
Matrix InstancedMesh::GetWorldMatrix() const { return m_WorldMatrix; }
void InstancedMesh::SetMaterial(Material *pMaterial) {
m_Material.reset(pMaterial);
m_EffectPtr->SetMaterial(pMaterial);
}

View File

@@ -0,0 +1,66 @@
#ifndef GP1_DIRECTX_INSTANCEDMESH_H
#define GP1_DIRECTX_INSTANCEDMESH_H
#include "Texture.h"
#include "Effects/BaseEffect.h"
#include "Mesh.h"
#include <d3d11.h>
#include <vector>
struct alignas(16) InstancedData {
Matrix worldMatrix;
Vector4 color;
};
class InstancedMesh {
public:
InstancedMesh(ID3D11Device *devicePtr, const std::vector<VertexIn> &verticesIn, const std::vector<Uint32> &indices,
std::shared_ptr<Material> material, BaseEffect* effectPtr, const std::vector<InstancedData>& instanceData);
~InstancedMesh();
void Render(ID3D11DeviceContext *deviceContextPtr, const Matrix &worldViewProj) const;
void SetCameraPos(const Vector3 &pos) const;
Matrix GetWorldMatrix() const;
Material* GetMaterial() const;
void SetWorldMatrix(const Matrix &matrix);
std::vector<VertexIn>& GetVertices() { return m_VerticesIn; }
std::vector<Uint32>& GetIndices() { return m_Indices; }
PrimitiveTopology GetPrimitiveTopology() const { return m_PrimitiveTopology; }
void SetMaterial(Material *pMaterial);
void UpdateInstanceData(ID3D11DeviceContext* deviceContextPtr, const std::vector<InstancedData>& instanceData);
private:
BaseEffect *m_EffectPtr;
Matrix m_WorldMatrix{};
ID3D11InputLayout *m_InputLayoutPtr;
ID3D11Buffer *m_VertexBufferPtr;
ID3D11Buffer *m_IndexBufferPtr;
ID3D11Buffer* m_InstanceBufferPtr;
std::vector<InstancedData> m_InstancedData;
UINT m_InstanceCount;
std::vector<VertexIn> m_VerticesIn;
std::vector<uint32_t> m_Indices;
UINT m_IndicesCount;
std::shared_ptr<Material> m_Material{};
PrimitiveTopology m_PrimitiveTopology{PrimitiveTopology::TriangleList};
};
#endif //GP1_DIRECTX_INSTANCEDMESH_H

View File

@@ -8,7 +8,8 @@
#include "HitTest.h" #include "HitTest.h"
#include "Scenes/MainScene.h" #include "Scenes/MainScene.h"
#include "Scenes/DioramaScene.h" #include "Scenes/DioramaScene.h"
#include "Scenes/InstancedScene.h"
#include "Scenes/PlanetScene.h"
namespace dae { namespace dae {
@@ -27,12 +28,20 @@ namespace dae {
std::cout << RED << "DirectX initialization failed!" << RESET << std::endl; std::cout << RED << "DirectX initialization failed!" << RESET << std::endl;
} }
//Best camera positions (Sorry for the magic numbers)
m_SceneCameraPositions[SceneNames::Main] = {Vector3{0.f, 0.f, 0.f}, Vector3{0.f, 0.f, 0.f}};
m_SceneCameraPositions[SceneNames::Diorama] = {Vector3{48.88f, 23.0f, -3.3f}, Vector3{-0.23f, 4.79f, 0}};
m_SceneCameraPositions[SceneNames::Instanced] = {Vector3{11.8f, 12.4f, 12.9f}, Vector3{-0.30f, 0.76f, 0.f}};
m_SceneCameraPositions[SceneNames::Planet] = {Vector3{-18.5f, 10.5f, -15.7f}, Vector3{-0.46f, 0.86f, 0}};
InitializeSDLRasterizer(); InitializeSDLRasterizer();
m_pScene = new MainScene(); m_pScene = new MainScene();
m_pScene->Initialize(m_DevicePtr); m_pScene->Initialize(m_DevicePtr, m_DeviceContextPtr);
if (!m_pScene->GetMeshes().empty()) {
m_pFireMesh = m_pScene->GetMeshes().back(); m_pFireMesh = m_pScene->GetMeshes().back();
}
float aspectRatio = static_cast<float>(m_Width) / static_cast<float>(m_Height); float aspectRatio = static_cast<float>(m_Width) / static_cast<float>(m_Height);
m_Camera = Camera({.0f, .0f, .0f}, 45.f); m_Camera = Camera({.0f, .0f, .0f}, 45.f);
@@ -88,6 +97,7 @@ namespace dae {
} }
} }
m_pScene->Update();
} }
void Renderer::Render() { void Renderer::Render() {
@@ -104,7 +114,8 @@ namespace dae {
void Renderer::RenderDirectX() const { void Renderer::RenderDirectX() const {
//Clear back buffer //Clear back buffer
ColorRGB ChosenClearColor = m_UseUniformClearColor ? m_UniformClearColor : DirectXClearColor; ColorRGB ChosenClearColor = m_UseUniformClearColor ? m_UniformClearColor : DirectXClearColor;
const float clearColor[] = {static_cast<float>(ChosenClearColor.r) / 255.f, static_cast<float>(ChosenClearColor.g) / 255.f, static_cast<float>(ChosenClearColor.b) / 255.f, 1.f}; const float clearColor[] = {static_cast<float>(ChosenClearColor.r) / 255.f, static_cast<float>(ChosenClearColor.g) / 255.f,
static_cast<float>(ChosenClearColor.b) / 255.f, 1.f};
m_DeviceContextPtr->ClearRenderTargetView(m_RenderTargetViewPtr, clearColor); m_DeviceContextPtr->ClearRenderTargetView(m_RenderTargetViewPtr, clearColor);
m_DeviceContextPtr->ClearDepthStencilView(m_DepthStencilViewPtr, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0); m_DeviceContextPtr->ClearDepthStencilView(m_DepthStencilViewPtr, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
@@ -119,6 +130,8 @@ namespace dae {
} }
} }
m_pScene->Render(m_DeviceContextPtr, m_RenderTargetViewPtr, m_DepthStencilViewPtr, m_Camera);
//Present //Present
m_SwapChainPtr->Present(0, 0); m_SwapChainPtr->Present(0, 0);
} }
@@ -260,7 +273,6 @@ namespace dae {
} }
} }
} }
} }
@@ -392,8 +404,7 @@ namespace dae {
void Renderer::VertexTransformationFunction(const Matrix &WorldViewProjectionMatrix, Mesh *mesh, void Renderer::VertexTransformationFunction(const Matrix &WorldViewProjectionMatrix, Mesh *mesh,
std::vector<VertexIn> &vertices_in, std::vector<VertexOut> &vertices_out) const { std::vector<VertexIn> &vertices_in, std::vector<VertexOut> &vertices_out) const {
for (const VertexIn& vert : vertices_in) for (const VertexIn &vert: vertices_in) {
{
VertexOut vertex_out{}; VertexOut vertex_out{};
Vector4 vertPos{WorldViewProjectionMatrix.TransformPoint({vert.position, 1})}; Vector4 vertPos{WorldViewProjectionMatrix.TransformPoint({vert.position, 1})};
@@ -402,7 +413,6 @@ namespace dae {
const Vector3 tangent{mesh->GetWorldMatrix().TransformVector(vert.tangent)}; const Vector3 tangent{mesh->GetWorldMatrix().TransformVector(vert.tangent)};
vertPos.x /= vertPos.w; vertPos.x /= vertPos.w;
vertPos.y /= vertPos.w; vertPos.y /= vertPos.w;
vertPos.z /= vertPos.w; vertPos.z /= vertPos.w;
@@ -442,7 +452,8 @@ namespace dae {
Material *currentMaterial = sample.mesh->GetMaterial(); Material *currentMaterial = sample.mesh->GetMaterial();
if(currentMaterial->normalTexturePtr == nullptr && currentMaterial->specularTexturePtr == nullptr && currentMaterial->glossTexturePtr == nullptr){ if (currentMaterial->normalTexturePtr == nullptr && currentMaterial->specularTexturePtr == nullptr &&
currentMaterial->glossTexturePtr == nullptr) {
return currentMaterial->diffuseTexturePtr->Sample(sample.uv); return currentMaterial->diffuseTexturePtr->Sample(sample.uv);
} }
@@ -542,6 +553,11 @@ namespace dae {
std::string mode = m_isHitbox ? "ON" : "OFF"; std::string mode = m_isHitbox ? "ON" : "OFF";
std::cout << MAGENTA << "[SOFTWARE]" << BLUE << " Hitbox " << mode << RESET << std::endl; std::cout << MAGENTA << "[SOFTWARE]" << BLUE << " Hitbox " << mode << RESET << std::endl;
std::cout << "Camera pos: " << m_Camera.GetPosition().x << " " << m_Camera.GetPosition().y << " " << m_Camera.GetPosition().z << std::endl;
std::cout << "Camera rot: " << m_Camera.GetRotation().x << " " << m_Camera.GetRotation().y << " " << m_Camera.GetRotation().z << std::endl;
} }
void Renderer::CycleRenderingMode() { void Renderer::CycleRenderingMode() {
@@ -637,22 +653,48 @@ namespace dae {
void Renderer::NextScene() { void Renderer::NextScene() {
m_pScene->Cleanup(); m_pScene->Cleanup();
delete m_pScene; delete m_pScene;
if(m_CurrentScene == SceneNames::Main){
m_pScene = new DioramaScene();
m_pFireMesh = nullptr;
m_CurrentScene = SceneNames::Diorama;
std::cout << MAGENTA << "[SHARED]" << BLUE << " Scene = Diorama" << RESET << std::endl; //Calculate the next scene
std::cout << MAGENTA << "This could take a second" << RESET << std::endl; int index = static_cast<int>(m_CurrentScene);
index = (index + 1) % static_cast<int>(SceneNames::Count);
m_CurrentScene = static_cast<SceneNames>(index);
} else { switch (m_CurrentScene) {
case SceneNames::Main:
m_pScene = new MainScene(); m_pScene = new MainScene();
m_CurrentScene = SceneNames::Main; m_pFireMesh = nullptr;
std::cout << MAGENTA << "[SHARED]" << BLUE << " Scene = Main" << RESET << std::endl; std::cout << MAGENTA << "[SHARED]" << BLUE << " Scene = Main" << RESET << std::endl;
std::cout << MAGENTA << "This could take a second" << RESET << std::endl; std::cout << MAGENTA << "This could take a second" << RESET << std::endl;
break;
case SceneNames::Diorama:
m_pScene = new DioramaScene();
std::cout << MAGENTA << "[SHARED]" << BLUE << " Scene = Diorama" << RESET << std::endl;
std::cout << MAGENTA << "This could take a second" << RESET << std::endl;
break;
case SceneNames::Instanced:
m_pScene = new InstancedScene();
std::cout << MAGENTA << "[SHARED]" << BLUE << " Scene = Instanced" << RESET << std::endl;
std::cout << MAGENTA << "This could take a second" << RESET << std::endl;
break;
case SceneNames::Planet:
m_pScene = new PlanetScene();
std::cout << MAGENTA << "[SHARED]" << BLUE << " Scene = Planet" << RESET << std::endl;
std::cout << MAGENTA << "This could take a second" << RESET << std::endl;
break;
case SceneNames::Count:
break;
} }
m_pScene->Initialize(m_DevicePtr);
m_Camera.SetPosition(m_SceneCameraPositions[m_CurrentScene].first);
m_Camera.SetRotation(m_SceneCameraPositions[m_CurrentScene].second);
m_pScene->Initialize(m_DevicePtr, m_DeviceContextPtr);
if (m_CurrentScene == SceneNames::Main) { if (m_CurrentScene == SceneNames::Main) {

View File

@@ -6,6 +6,8 @@
#include "Scenes/BaseScene.h" #include "Scenes/BaseScene.h"
#include "Effects/Effect.h" #include "Effects/Effect.h"
#include <unordered_map>
struct SDL_Window; struct SDL_Window;
struct SDL_Surface; struct SDL_Surface;
@@ -23,7 +25,11 @@ enum class ShadeMode{
enum class SceneNames{ enum class SceneNames{
Main, Main,
Diorama Diorama,
Instanced,
Planet,
Count
}; };
namespace dae namespace dae
@@ -75,6 +81,8 @@ namespace dae
BaseScene* m_pScene{}; BaseScene* m_pScene{};
SceneNames m_CurrentScene{ SceneNames::Main }; SceneNames m_CurrentScene{ SceneNames::Main };
//Pos rot
std::unordered_map<SceneNames, std::pair<Vector3, Vector3>> m_SceneCameraPositions{};
Camera m_Camera{}; Camera m_Camera{};

View File

@@ -8,6 +8,7 @@
#include "../Mesh.h" #include "../Mesh.h"
#include "../Material.h" #include "../Material.h"
#include "../Camera.h"
class BaseScene { class BaseScene {
@@ -16,11 +17,11 @@ public:
virtual ~BaseScene() = default; virtual ~BaseScene() = default;
virtual void Initialize(ID3D11Device *DevicePtr) = 0; virtual void Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *DeviceContextPtr) = 0;
// virtual void Update() = 0; virtual void Update() = 0;
// virtual void Render() = 0; virtual void Render(ID3D11DeviceContext* devicePtr, ID3D11RenderTargetView *renderTargetViewPtr, ID3D11DepthStencilView *depthStencilViewPtr, const Camera& camera) = 0;
virtual void Cleanup() = 0; virtual void Cleanup() = 0;

View File

@@ -10,8 +10,7 @@
#include <iostream> #include <iostream>
void DioramaScene::Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *DeviceContextPtr) {
void DioramaScene::Initialize(ID3D11Device *DevicePtr) {
std::vector<std::unique_ptr<Utils::MaterialMesh>> materialMeshes; std::vector<std::unique_ptr<Utils::MaterialMesh>> materialMeshes;
Utils::LoadObjWithMaterials("resources/scene.obj", materialMeshes, true, DevicePtr); Utils::LoadObjWithMaterials("resources/scene.obj", materialMeshes, true, DevicePtr);
@@ -21,37 +20,35 @@ void DioramaScene::Initialize(ID3D11Device *DevicePtr) {
BaseEffect *effect{nullptr}; BaseEffect *effect{nullptr};
if (mesh->opacity_map != "") { if (mesh->opacity_map != "") {
// std::cout << "Opacity map found" << mesh->opacity_map << std::endl;
effect = new FireEffect(DevicePtr, L"resources/Fire.fx"); effect = new FireEffect(DevicePtr, L"resources/Fire.fx");
material->diffuseTexturePtr = Texture::LoadFromFile("./resources/diorama/" + mesh->diffuse_texture, DevicePtr); material->diffuseTexturePtr = Texture::LoadFromFile("./resources/diorama/" + mesh->diffuse_texture, DevicePtr);
// material-> = Texture::LoadFromFile("./resources/" + mesh->opacity_map, DevicePtr);
} else { } else {
material->diffuseTexturePtr = Texture::LoadFromFile("./resources/diorama/" + mesh->diffuse_texture, DevicePtr); material->diffuseTexturePtr = Texture::LoadFromFile("./resources/diorama/" + mesh->diffuse_texture, DevicePtr);
effect = new Effect(DevicePtr, L"resources/SimpleDiffuse.fx"); effect = new Effect(DevicePtr, L"resources/SimpleDiffuse.fx");
} }
m_meshes.push_back(new Mesh(DevicePtr, mesh->vertices, mesh->indices, material, effect)); m_meshes.push_back(new Mesh(DevicePtr, mesh->vertices, mesh->indices, material, effect));
Matrix worldMatrix = m_meshes.back()->GetWorldMatrix(); Matrix worldMatrix = m_meshes.back()->GetWorldMatrix();
worldMatrix *= Matrix::CreateScale(2.f, 2.f, 2.f); worldMatrix *= Matrix::CreateScale(2.f, 2.f, 2.f);
worldMatrix *= Matrix::CreateScale(-1.f, 1.f, 1.f); worldMatrix *= Matrix::CreateScale(-1.f, 1.f, 1.f); // Mirror the model (Possible loading fault)
m_meshes.back()->SetWorldMatrix(worldMatrix); m_meshes.back()->SetWorldMatrix(worldMatrix);
} }
} }
//Load brok
materialMeshes.clear(); materialMeshes.clear();
Utils::LoadObjWithMaterials("resources/brok/brok.obj", materialMeshes, true, DevicePtr); Utils::LoadObjWithMaterials("resources/brok/brok.obj", materialMeshes, true, DevicePtr);
for (const auto &mesh: materialMeshes) { for (const auto &mesh: materialMeshes) {
if(mesh->vertices.size() > 0) { if (!mesh->vertices.empty()) {
std::shared_ptr<Material> material = std::make_shared<Material>(); std::shared_ptr<Material> material = std::make_shared<Material>();
BaseEffect *effect{nullptr}; BaseEffect *effect{nullptr};
effect = new Effect(DevicePtr, L"resources/SimpleDiffuse.fx"); effect = new Effect(DevicePtr, L"resources/SimpleDiffuse.fx");
material->diffuseTexturePtr = Texture::LoadFromFile("./resources/brok/" + mesh->diffuse_texture, DevicePtr); material->diffuseTexturePtr = Texture::LoadFromFile("./resources/brok/" + mesh->diffuse_texture, DevicePtr);
m_meshes.push_back(new Mesh(DevicePtr, mesh->vertices, mesh->indices, material, effect)); m_meshes.push_back(new Mesh(DevicePtr, mesh->vertices, mesh->indices, material, effect));
m_brokMeshses.push_back(m_meshes.back());
Matrix worldMatrix = m_meshes.back()->GetWorldMatrix(); Matrix worldMatrix = m_meshes.back()->GetWorldMatrix();
worldMatrix *= Matrix::CreateRotationY(3.14f / 2.f); worldMatrix *= Matrix::CreateRotationY(3.14f / 2.f);
@@ -63,6 +60,27 @@ void DioramaScene::Initialize(ID3D11Device *DevicePtr) {
} }
} }
void DioramaScene::Update() {
m_brokTimer += 0.01f;
for(auto* mesh : m_brokMeshses){
if(mesh == nullptr)
continue;
Matrix worldMatrix = mesh->GetWorldMatrix();
float YOffset = sin(m_brokTimer) / 2000.f;
worldMatrix *= Matrix::CreateTranslation(0.f, YOffset, 0.f);
mesh->SetWorldMatrix(worldMatrix);
}
}
void DioramaScene::Render(ID3D11DeviceContext *devicePtr, ID3D11RenderTargetView *renderTargetViewPtr,
ID3D11DepthStencilView *depthStencilViewPtr, const Camera &camera) {
}
std::vector<Mesh *> &DioramaScene::GetMeshes() { std::vector<Mesh *> &DioramaScene::GetMeshes() {
return m_meshes; return m_meshes;
} }

View File

@@ -7,15 +7,23 @@ class DioramaScene : public BaseScene {
public: public:
void Cleanup() override; void Cleanup() override;
void Initialize(ID3D11Device *DevicePtr) override; void Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *DeviceContextPtr) override;
std::vector<Mesh *> &GetMeshes() override; std::vector<Mesh *> &GetMeshes() override;
std::vector<std::shared_ptr<Material>> &GetMaterials() override; std::vector<std::shared_ptr<Material>> &GetMaterials() override;
void Update() override;
void Render(ID3D11DeviceContext* devicePtr, ID3D11RenderTargetView *renderTargetViewPtr, ID3D11DepthStencilView *depthStencilViewPtr, const Camera& camera) override;
private: private:
std::vector<Mesh *> m_meshes{}; std::vector<Mesh *> m_meshes{};
std::vector<std::shared_ptr<Material>> m_materials{}; std::vector<std::shared_ptr<Material>> m_materials{};
std::vector<Mesh*> m_brokMeshses{ nullptr };
float m_brokTimer{ 0.f };
}; };
#endif //GP1_DIRECTX_DIORAMASCENE_H #endif //GP1_DIRECTX_DIORAMASCENE_H

View File

@@ -0,0 +1,95 @@
//
// Created by Bram on 28/12/2024.
//
#include "InstancedScene.h"
#include "../Utils.h"
#include "../Effects/Effect.h"
void InstancedScene::Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *DeviceContextPtr) {
m_DeviceContextPtr = DeviceContextPtr;
std::vector<VertexIn> vertices{};
std::vector<uint32_t> indices{};
if (!Utils::ParseOBJNew("resources/cube.obj", vertices, indices, false)) {
std::cout << "Model failed to load" << std::endl;
assert(true && "Model failed to load");
}
std::shared_ptr<Material> cubeMaterial = std::make_shared<Material>();
cubeMaterial->diffuseTexturePtr = Texture::LoadFromFile("resources/grass_block.png", DevicePtr);
std::vector<InstancedData> instanceData;
for (int x = 0; x < 100; ++x) {
for (int y = 0; y < 100; ++y) {
InstancedData data;
float scale = 2;
//Generate sine wave based on x and y
float YOffset = sin(x * 0.5f + SDL_GetTicks() * 0.001f) + cos(y * 0.5f + SDL_GetTicks() * 0.001f);
//Add random but predictable randomness
YOffset += (x * 0.1f + y * 0.1f) * 0.1f;
data.worldMatrix = Matrix::CreateTranslation(x * scale, YOffset, y * scale);
data.worldMatrix *= Matrix::CreateScale(0.5f, 0.5f, 0.5f);
data.color = Vector4(float(x) / 255.f, float(y) / 255.f, 1.f, 1.0f);
instanceData.push_back(data);
}
}
auto *effect = new Effect(DevicePtr, L"resources/InstancedSimpleDiffuse.fx");
effect->NextSamplingState();
effect->NextSamplingState(); //Dirty hack
m_instancedMeshes.push_back(new InstancedMesh(DevicePtr, vertices, indices, cubeMaterial, effect, instanceData));
}
void InstancedScene::Cleanup() {
for (auto mesh: m_instancedMeshes) {
delete mesh;
}
m_instancedMeshes.clear();
for (auto mesh: m_meshes) {
delete mesh;
}
m_meshes.clear();
}
std::vector<Mesh *> &InstancedScene::GetMeshes() {
return m_meshes;
}
std::vector<std::shared_ptr<Material>> &InstancedScene::GetMaterials() {
return m_materials;
}
void InstancedScene::Render(ID3D11DeviceContext *devicePtr, ID3D11RenderTargetView *renderTargetViewPtr,
ID3D11DepthStencilView *depthStencilViewPtr, const Camera &camera) {
Matrix viewProjMatrix = camera.GetViewProjectionMatrix();
for (auto mesh: m_instancedMeshes) {
Matrix modelMatrix = mesh->GetWorldMatrix();
Matrix worldViewProjMatrix = modelMatrix * viewProjMatrix;
mesh->Render(devicePtr, worldViewProjMatrix);
}
}
void InstancedScene::Update() {
if (SDL_GetTicks() % 3 == 0) {
std::vector<InstancedData> instanceData;
for (int x = 0; x < 100; ++x) {
for (int y = 0; y < 100; ++y) {
InstancedData data;
float scale = 2;
//Generate sine wave based on x and y and the SDL_GetTicks
float YOffset = sin(x * 0.5f + SDL_GetTicks() * 0.001f) + cos(y * 0.5f + SDL_GetTicks() * 0.001f);
YOffset += (x * 0.1f + y * 0.1f) * 0.1f;
data.worldMatrix = Matrix::CreateTranslation(x * scale, YOffset, y * scale);
data.worldMatrix *= Matrix::CreateScale(0.5f, 0.5f, 0.5f);
data.color = Vector4(float(x) / 255.f, float(y) / 255.f, 1.f, 1.0f);
instanceData.push_back(data);
}
}
m_instancedMeshes[0]->UpdateInstanceData(m_DeviceContextPtr, instanceData);
}
}

View File

@@ -0,0 +1,32 @@
#ifndef GP1_DIRECTX_INSTANCEDSCENE_H
#define GP1_DIRECTX_INSTANCEDSCENE_H
#include "BaseScene.h"
#include "../Camera.h"
#include "../InstancedMesh.h"
class InstancedScene : public BaseScene {
public:
void Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *DeviceContextPtr) override;
void Render(ID3D11DeviceContext* devicePtr, ID3D11RenderTargetView *renderTargetViewPtr, ID3D11DepthStencilView *depthStencilViewPtr, const Camera& camera) override;
void Update() override;
void Cleanup() override;
std::vector<Mesh *> &GetMeshes() override;
std::vector<std::shared_ptr<Material>> &GetMaterials() override;
private:
ID3D11DeviceContext* m_DeviceContextPtr{};
std::vector<Mesh*> m_meshes;
//Kind of hack since InstancedMesh doesnt extend mesh
std::vector<InstancedMesh*> m_instancedMeshes;
std::vector<std::shared_ptr<Material>> m_materials;
};
#endif //GP1_DIRECTX_INSTANCEDSCENE_H

View File

@@ -11,7 +11,7 @@
#include <cassert> #include <cassert>
void MainScene::Initialize(ID3D11Device *DevicePtr) { void MainScene::Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *DeviceContextPtr) {
std::vector<VertexIn> vertices{}; std::vector<VertexIn> vertices{};
std::vector<uint32_t> indices{}; std::vector<uint32_t> indices{};
@@ -69,3 +69,11 @@ std::vector<std::shared_ptr<Material>> &MainScene::GetMaterials() {
return m_materials; return m_materials;
} }
void MainScene::Update() {
}
void MainScene::Render(ID3D11DeviceContext* devicePtr, ID3D11RenderTargetView *renderTargetViewPtr, ID3D11DepthStencilView *depthStencilViewPtr, const Camera& camera) {
}

View File

@@ -12,7 +12,7 @@
class MainScene : public BaseScene { class MainScene : public BaseScene {
public: public:
void Initialize(ID3D11Device* DevicePtr) override; void Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *DeviceContextPtr) override;
void Cleanup() override; void Cleanup() override;
@@ -20,6 +20,10 @@ public:
std::vector<std::shared_ptr<Material>> &GetMaterials() override; std::vector<std::shared_ptr<Material>> &GetMaterials() override;
void Update() override;
void Render(ID3D11DeviceContext* devicePtr, ID3D11RenderTargetView *renderTargetViewPtr, ID3D11DepthStencilView *depthStencilViewPtr, const Camera& camera) override;
private: private:

View File

@@ -0,0 +1,136 @@
#include "PlanetScene.h"
#include "../Utils.h"
#include "../Effects/Effect.h"
void PlanetScene::Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *DeviceContextPtr) {
m_DeviceContextPtr = DeviceContextPtr;
std::vector<VertexIn> vertices{};
std::vector<uint32_t> indices{};
if (!Utils::ParseOBJNew("resources/planet/rock/rock.obj", vertices, indices, false)) {
std::cout << "Model failed to load" << std::endl;
assert(true && "Model failed to load");
}
std::shared_ptr<Material> rockMaterial = std::make_shared<Material>();
rockMaterial->diffuseTexturePtr = Texture::LoadFromFile("resources/planet/rock/rock.png", DevicePtr);
unsigned int amount = 1000; // Adjusted for a different number of instances
float radius = 50.0f;
float offset = 2.5f;
float scale = 0.25f; // Random scale factor
for (unsigned int i = 0; i < amount; ++i) {
InstancedData data;
// Generate translation: displace along circle with 'radius' and apply random offset
float angle = static_cast<float>(i) / static_cast<float>(amount) * 360.0f;
float displacement = static_cast<float>(rand() % static_cast<int>(2 * offset * 100)) / 100.0f - offset;
float x = sin(angle) * radius + displacement;
displacement = static_cast<float>(rand() % static_cast<int>(2 * offset * 100)) / 100.0f - offset;
float y = displacement * 0.4f; // Keep height smaller compared to width of x and z
displacement = static_cast<float>(rand() % static_cast<int>(2 * offset * 100)) / 100.0f - offset;
float z = cos(angle) * radius + displacement;
data.worldMatrix = Matrix();
//Rotate the model randomly
data.worldMatrix *= Matrix::CreateRotationY(static_cast<float>(rand() % 360));
data.worldMatrix *= Matrix::CreateRotationX(static_cast<float>(rand() % 360));
data.worldMatrix *= Matrix::CreateRotationZ(static_cast<float>(rand() % 360));
// Create translation matrix
data.worldMatrix *= Matrix::CreateTranslation(x, y, z);
// Apply scaling (randomized)
float randScale = (rand() % 20) / 100.0f + 0.05f; // Random scale between 0.05 and 0.25
data.worldMatrix *= Matrix::CreateScale(randScale, randScale, randScale);
// Apply random rotation
// float rotAngle = static_cast<float>(rand() % 360); // Random rotation angle
// data.worldMatrix *= Matrix::CreateRotation(0.4f, 0.6f, 0.8f); // Apply rotation around random axis
// Set color (randomized based on position)
data.color = Vector4(remap(rand(), 0, RAND_MAX, 0, 1.f),remap(rand(), 0, RAND_MAX, 0, 1.f), remap(rand(), 0, RAND_MAX, 0, 1.f), 1.0f);
m_InstancedData.push_back(data);
}
auto *rockEffect = new Effect(DevicePtr, L"resources/InstancedSimpleDiffuse.fx");
m_instancedMeshes.push_back(new InstancedMesh(DevicePtr, vertices, indices, rockMaterial, rockEffect, m_InstancedData));
indices.clear();
vertices.clear();
if (!Utils::ParseOBJNew("resources/planet/planet.obj", vertices, indices, false)) {
std::cout << "Model failed to load" << std::endl;
assert(true && "Model failed to load");
}
std::shared_ptr<Material> planetMaterial = std::make_shared<Material>();
planetMaterial->diffuseTexturePtr = Texture::LoadFromFile("resources/planet/mars.png", DevicePtr);
auto* planetEffect = new Effect(DevicePtr, L"resources/SimpleDiffuse.fx");
m_meshes.push_back(new Mesh(DevicePtr, vertices, indices, planetMaterial, planetEffect));
indices.clear();
vertices.clear();
if (!Utils::ParseOBJNew("resources/planet/skybox/skybox.obj", vertices, indices, false)) {
std::cout << "Model failed to load" << std::endl;
assert(true && "Model failed to load");
}
std::shared_ptr<Material> skyboxMaterial = std::make_shared<Material>();
skyboxMaterial->diffuseTexturePtr = Texture::LoadFromFile("resources/planet/skybox/space_nebula_6k.png", DevicePtr);
auto* skyboxEffect = new Effect(DevicePtr, L"resources/SimpleDiffuse.fx");
m_meshes.push_back(new Mesh(DevicePtr, vertices, indices, skyboxMaterial, skyboxEffect));
Matrix worldMatrix = m_meshes.back()->GetWorldMatrix();
worldMatrix *= Matrix::CreateScale(0.1f, 0.1f, 0.1f);
m_meshes.back()->SetWorldMatrix(worldMatrix);
}
void PlanetScene::Cleanup() {
for (auto mesh: m_instancedMeshes) {
delete mesh;
}
m_instancedMeshes.clear();
for (auto mesh: m_meshes) {
delete mesh;
}
m_meshes.clear();
}
std::vector<Mesh *> &PlanetScene::GetMeshes() {
return m_meshes;
}
std::vector<std::shared_ptr<Material>> &PlanetScene::GetMaterials() {
return m_materials;
}
void PlanetScene::Render(ID3D11DeviceContext *devicePtr, ID3D11RenderTargetView *renderTargetViewPtr,
ID3D11DepthStencilView *depthStencilViewPtr, const Camera &camera) {
Matrix viewProjMatrix = camera.GetViewProjectionMatrix();
for (auto mesh: m_instancedMeshes) {
Matrix modelMatrix = mesh->GetWorldMatrix();
Matrix worldViewProjMatrix = modelMatrix * viewProjMatrix;
mesh->Render(devicePtr, worldViewProjMatrix);
}
}
void PlanetScene::Update() {
for (auto& data : m_InstancedData) {
data.worldMatrix *= Matrix::CreateRotationY(remap(rand(), 0, RAND_MAX, 0.00001f, 0.0002f));
}
m_instancedMeshes[0]->UpdateInstanceData(m_DeviceContextPtr, m_InstancedData);
}

View File

@@ -0,0 +1,35 @@
#ifndef GP1_DIRECTX_PLANETSCENE_H
#define GP1_DIRECTX_PLANETSCENE_H
//https://learnopengl.com/Advanced-OpenGL/Instancing
#include "BaseScene.h"
#include "../InstancedMesh.h"
class PlanetScene : public BaseScene {
public:
void Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *DeviceContextPtr) override;
void Render(ID3D11DeviceContext* devicePtr, ID3D11RenderTargetView *renderTargetViewPtr, ID3D11DepthStencilView *depthStencilViewPtr, const Camera& camera) override;
void Update() override;
void Cleanup() override;
std::vector<Mesh *> &GetMeshes() override;
std::vector<std::shared_ptr<Material>> &GetMaterials() override;
private:
ID3D11DeviceContext* m_DeviceContextPtr{};
std::vector<Mesh*> m_meshes;
//Kind of hack since InstancedMesh doesnt extend mesh
std::vector<InstancedMesh*> m_instancedMeshes;
std::vector<InstancedData> m_InstancedData;
std::vector<std::shared_ptr<Material>> m_materials;
};
#endif //GP1_DIRECTX_PLANETSCENE_H

View File

@@ -124,6 +124,8 @@ int main(int argc, char *args[]) {
(void) argc; (void) argc;
(void) args; (void) args;
srand(static_cast<unsigned int>(time(nullptr)));
//Create window + surfaces //Create window + surfaces
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK); SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);