Du bist nicht angemeldet.

Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!

Werbeanzeige

C0dR

Frischling

  • »C0dR« ist der Autor dieses Themas
  • Private Nachricht senden

1

24.06.2012, 20:33

HLSL 5 (DirectX 11) mehrere lichter problem

Hallo,

ich bin zur Zeit am Erlernen von DirectX11 und stehe im moment vor einem Problem bezüglich mehrerer Lichter.
Mein Shader sieht im moment so aus:

HLSL-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
///////////// 
// DEFINES // 
///////////// 
#define NUM_MAX_LIGHTS 20 


///////////// 
// GLOBALS // 
///////////// 
cbuffer MatrixBuffer 
{ 
matrix worldMatrix; 
matrix viewMatrix; 
matrix projectionMatrix; 
}; 
Texture2D shaderTexture; 
SamplerState SampleType; 

cbuffer LightPositionBuffer 
{ 
float4 lightPosition[NUM_MAX_LIGHTS]; 
float4 numLights; 
}; 
cbuffer LightColorBuffer 
{ 
float4 diffuseColor[NUM_MAX_LIGHTS]; 
}; 

////////////// 
// TYPEDEFS // 
////////////// 
struct VertexInputType 
{ 
float4 position : POSITION; 
float2 tex : TEXCOORD0; 
float3 normal : NORMAL; 
}; 

struct PixelInputType 
{ 
float4 position : SV_POSITION; 
float2 tex : TEXCOORD0; 
float3 normal : NORMAL; 
float3 lightPos[NUM_MAX_LIGHTS] : TEXCOORD1; 
}; 

//////////////////////////////////////////////////////////////////////////////// 
// Vertex Shader 
//////////////////////////////////////////////////////////////////////////////// 
PixelInputType LightVertexShader(VertexInputType input) 
{ 
PixelInputType output; 
float4 worldPosition; 

input.position.w = 1.0f; 

output.position = mul(input.position, worldMatrix); 
output.position = mul(output.position, viewMatrix); 
output.position = mul(output.position, projectionMatrix); 
output.tex = input.tex; 

output.normal = mul(input.normal, (float3x3)worldMatrix); 

output.normal = normalize(output.normal); 

worldPosition = mul(input.position, worldMatrix); 

uint nl = asuint( numLights.x ); 
for(int i=0;i< 
nl 
;i++) 
{ 

output.lightPos[i].xyz = lightPosition[i].xyz - worldPosition.xyz; 

output.lightPos[i] = normalize(output.lightPos[i]); 
} 

return output; 
} 



//////////////////////////////////////////////////////////////////////////////// 
// Pixel Shader 
//////////////////////////////////////////////////////////////////////////////// 
float4 LightPixelShader(PixelInputType input) : SV_TARGET 
{ 
float4 textureColor; 
float lightIntensity[NUM_MAX_LIGHTS]; 
float4 color[NUM_MAX_LIGHTS]; 
float4 colorSum = (float4)0; 

uint nl = asuint( numLights.x ); 

for(int i=0;i { 
lightIntensity[i] = saturate(dot(input.normal, input.lightPos[i])); 

// Determine the diffuse color amount of each of the four lights. 
color[i] = diffuseColor[i] * lightIntensity[i]; 
} 

// Sample the texture pixel at this location. 
textureColor = shaderTexture.Sample(SampleType, input.tex); 

// Multiply the texture pixel by the combination of all four light colors to get the final result. 
for(int i=0;i< 
nl 
;i++) 
{ 
colorSum += color[i]; 
} 
colorSum = saturate(colorSum) * textureColor; 
return colorSum; 
}



und der C++ part so:

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
bool cLightshader::SetShaderParameters(ID3D11DeviceContext* deviceContext, D3DXMATRIX worldMatrix, D3DXMATRIX viewMatrix, D3DXMATRIX projectionMatrix, ID3D11ShaderResourceView* texture) 
{ 
HRESULT result; 
D3D11_MAPPED_SUBRESOURCE mappedResource; 
MatrixBufferType* dataPtr; 
LightPositionBufferType* dataPtr2; 
LightColorBufferType* dataPtr3; 
unsigned int bufferNumber; 


// Transpose the matrices to prepare them for the shader. 
D3DXMatrixTranspose(&worldMatrix, &worldMatrix); 
D3DXMatrixTranspose(&viewMatrix, &viewMatrix); 
D3DXMatrixTranspose(&projectionMatrix, &projectionMatrix); 

// Lock the matrix constant buffer so it can be written to. 
result = deviceContext->Map(m_matrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); 
if(FAILED(result)) 
{ 
return false; 
} 

// Get a pointer to the data in the constant buffer. 
dataPtr = (MatrixBufferType*)mappedResource.pData; 

// Copy the matrices into the constant buffer. 
dataPtr->world = worldMatrix; 
dataPtr->view = viewMatrix; 
dataPtr->projection = projectionMatrix; 

// Unlock the matrix constant buffer. 
deviceContext->Unmap(m_matrixBuffer, 0); 

// Set the position of the matrix constant buffer in the vertex shader. 
bufferNumber = 0; 

// Now set the matrix constant buffer in the vertex shader with the updated values. 
deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_matrixBuffer); 

// Lock the light position constant buffer so it can be written to. 
result = deviceContext->Map(m_lightPositionBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); 
if(FAILED(result)) 
{ 
return false; 
} 

// Get a pointer to the data in the constant buffer. 
dataPtr2 = (LightPositionBufferType*)mappedResource.pData; 

// Copy the light position variables into the constant buffer. 
for(int i=0; i { 
dataPtr2->lightPosition = m_lights->GetPosition(); 
} 
dataPtr2->numLights = D3DXVECTOR4(m_lights.size(),m_lights.size(),m_lights.size(),m_lights.size()); 

// Unlock the constant buffer. 
deviceContext->Unmap(m_lightPositionBuffer, 0); 

// Finally set the constant buffer in the vertex shader with the updated values. 
deviceContext->VSSetConstantBuffers(1, 1, &m_lightPositionBuffer); 

// Set shader texture resource in the pixel shader. 
deviceContext->PSSetShaderResources(0, 1, &texture); 

// Lock the light color constant buffer so it can be written to. 
result = deviceContext->Map(m_lightColorBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); 
if(FAILED(result)) 
{ 
return false; 
} 

// Get a pointer to the data in the constant buffer. 
dataPtr3 = (LightColorBufferType*)mappedResource.pData; 

// Copy the light color variables into the constant buffer. 
for(int i=0;i { 
dataPtr3->diffuseColor = m_lights->GetColor(); 
} 

// Unlock the constant buffer. 
deviceContext->Unmap(m_lightColorBuffer, 0); 

// Set the position of the constant buffer in the pixel shader. 
bufferNumber = 0; 

// Finally set the constant buffer in the pixel shader with the updated values. 
deviceContext->PSSetConstantBuffers(bufferNumber, 1, &m_lightColorBuffer); 

return true; 

}


Das problem ist jetzt dass numLights einfach nicht gesetzt wird. Es bleibt 0. Ich hab schon alles mögliche versucht aber will einfach nicht funktionieren.
Wüsste einer wie ich das lösen kann oder mach ich einfach nur was falsch?
Ich nehm auch gerne Vorschläge an wie ich vielleicht das problem grundsätzlich besser lösen könnte.

gruß,
C0dR

Fireball

Alter Hase

Beiträge: 415

Wohnort: Werne

Beruf: Dipl. Inf.

  • Private Nachricht senden

2

09.07.2012, 14:22

Hallo,

hab nicht so viel Zeit, deswegen schnell ein paar Anmerkungen.

1. überprüfen ob deine eigenen Datentypen wie z.B. m_lights mit denen des constant buffer übereinstimmen.
2. shader in zwei Dateien aufteilen. Ich glaube da wird es dann das erste mal krachen ;-)
3. sich die Frage stellen, warum ein shader eine Member Variable m_Lights besitzt soll, wenn du in der Methode setShaderParameter alle anderen Parameter übergibst. Stell dir vor du willst mal deine Lichter verändern, dann musst du erst eine Set Methode für die Lights aufrufen und dann noch mal setShaderParameter(). Naja wobei das hier das kleinere Übel ist.


Gruß

fb

BlackSnake

Community-Fossil

Beiträge: 1 549

Beruf: Student

  • Private Nachricht senden

3

13.07.2012, 12:19

Wodrin besteht denn genau dein Problem?

Werbeanzeige