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

FSA

Community-Fossil

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

1

02.02.2012, 14:19

Per Pixel Shader mit Phong Model

Hallo. Nun ich habe es jetzt hinbekommen auf einem Würfel mit Textur ein per Pixel Spotlight zu benutzen:)
Dem Shader muss man einige Dinge übergeben. Vorallem Matrizen und Werte des Lichtes. Mann muss auch noch eine Textur übergeben das ist alles kein Problem, aber was ist wenn ich ein riesiges Level ausleuchten will? Was soll ich für eine Textur übergeben die zu beleuchten ist? Das ist ein wenig unverständlich für mich aber wenigstens habe ich schon einen funktionierenden Phong-Shader. Code kann ich erst heute Abend zeigen, da ich im moment keinen PC zur Hand habe ;)
Danke im Voraus.

Zitat

Der RCCSWU (RandomCamelCaseSomtimesWithUndersquare) Stil bricht auch mal mit den veraltet strukturierten Denkmustern und erlaubt dem Entwickler seine Kreativität zu entfalten.

David_pb

Community-Fossil

Beiträge: 3 886

Beruf: 3D Graphics Programmer

  • Private Nachricht senden

2

02.02.2012, 14:25

Was soll ich für eine Textur übergeben die zu beleuchten ist?


Du übergibst immer die Textur des aktuell gerenderten Objekts an den Shader.
@D13_Dreinig

FSA

Community-Fossil

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

3

02.02.2012, 15:35

Danke. Nur was wenn ich ein Objekt habe was mehrer Texturen hat? Und gibt es bei directX eine Möglichkeit die aktuelle Textur herauszufinden?

Zitat

Der RCCSWU (RandomCamelCaseSomtimesWithUndersquare) Stil bricht auch mal mit den veraltet strukturierten Denkmustern und erlaubt dem Entwickler seine Kreativität zu entfalten.

David_pb

Community-Fossil

Beiträge: 3 886

Beruf: 3D Graphics Programmer

  • Private Nachricht senden

4

02.02.2012, 16:34

Was meinst du mit "aktuell"? Du weißt doch welche Texturen du für das Modell gebunden hast.
@D13_Dreinig

dot

Supermoderator

Beiträge: 9 757

Wohnort: Graz

  • Private Nachricht senden

5

02.02.2012, 17:08

Danke. Nur was wenn ich ein Objekt habe was mehrer Texturen hat?

Dann musst du üblicherweise die Teile mit verschiedenen Texturen einzeln rendern...

FSA

Community-Fossil

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

6

03.02.2012, 12:05

Ok das werde ich noch auf die Reihe bekommen;)
Eine Frage hätte ich aber noch: Wie weiß der Shader denn wie hell die Textur ist? Bzw Wie stark er sie ausleuchten muss. Wenn man die Modelle verschieden positioniert. Also kurz gesagt wie weiß der Shader die Entfernung zur Lichtquelle.
Ich muss den Code nochmal richtig durchlesen und dann kann ich ihn auch mal Posten;)
Danke

Zitat

Der RCCSWU (RandomCamelCaseSomtimesWithUndersquare) Stil bricht auch mal mit den veraltet strukturierten Denkmustern und erlaubt dem Entwickler seine Kreativität zu entfalten.

dot

Supermoderator

Beiträge: 9 757

Wohnort: Graz

  • Private Nachricht senden

7

03.02.2012, 12:12

Also kurz gesagt wie weiß der Shader die Entfernung zur Lichtquelle.

Indem er sie ausrechnet? Du wirst die Position der Lichtquelle eben an den Shader übergeben müssen...

David_pb

Community-Fossil

Beiträge: 3 886

Beruf: 3D Graphics Programmer

  • Private Nachricht senden

8

03.02.2012, 12:24

Hallo. Nun ich habe es jetzt hinbekommen auf einem Würfel mit Textur ein per Pixel Spotlight zu benutzen


Ich versteh deine Probleme alle nicht. Im ersten Post hast du doch gemeint du hättest das alles bereits hinbekommen?
@D13_Dreinig

FSA

Community-Fossil

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

9

03.02.2012, 13:07

Ja, das stimmt, allerdings ist da ein Unterschied wenn ich mehrere Objekte habe?
Hier der Code des Shaders

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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
struct Light
{
    float3 dir;                     // world space direction
    float3 pos;                     // world space position
    float4 ambient;
    float4 diffuse;
    float4 specular;
    float spotInnerCone;            // spot light inner cone (theta) angle
    float spotOuterCone;            // spot light outer cone (phi) angle
    float radius;                   // applies to point and spot lights only
};

struct Material
{
    float4 ambient;
    float4 diffuse;
    float4 emissive;
    float4 specular;
    float shininess;
};

//-----------------------------------------------------------------------------
// Globals.
//-----------------------------------------------------------------------------

float4x4 worldMatrix;
float4x4 worldInverseTransposeMatrix;
float4x4 worldViewProjectionMatrix;

float3 cameraPos;
float4 globalAmbient;

Light light;
Material material;

//-----------------------------------------------------------------------------
// Textures.
//-----------------------------------------------------------------------------

texture colorMapTexture;

sampler2D colorMap = sampler_state
{
    Texture = <colorMapTexture>;
    MagFilter = Linear;
    MinFilter = Anisotropic;
    MipFilter = Linear;
    MaxAnisotropy = 16;
};

//-----------------------------------------------------------------------------
// Vertex Shaders.
//-----------------------------------------------------------------------------

struct VS_INPUT
{
    float3 position : POSITION;
    float2 texCoord : TEXCOORD0;
    float3 normal : NORMAL;
};

struct VS_OUTPUT_DIR
{
    float4 position : POSITION;
    float2 texCoord : TEXCOORD0;
    float3 halfVector : TEXCOORD1;
    float3 lightDir : TEXCOORD2;
    float3 normal : TEXCOORD3;
    float4 diffuse : COLOR0;
    float4 specular : COLOR1;
};

struct VS_OUTPUT_POINT
{
    float4 position : POSITION;
    float2 texCoord : TEXCOORD0;
    float3 viewDir : TEXCOORD1;
    float3 lightDir : TEXCOORD2;
    float3 normal : TEXCOORD3;
    float4 diffuse : COLOR0;
    float4 specular : COLOR1;
};

struct VS_OUTPUT_SPOT
{
    float4 position : POSITION;
    float2 texCoord : TEXCOORD0;
    float3 viewDir : TEXCOORD1;
    float3 lightDir : TEXCOORD2;
    float3 spotDir : TEXCOORD3;
    float3 normal : TEXCOORD4;
    float4 diffuse : COLOR0;
    float4 specular : COLOR1;
};

VS_OUTPUT_DIR VS_DirLighting(VS_INPUT IN)
{
    VS_OUTPUT_DIR OUT;

    float3 worldPos = mul(float4(IN.position, 1.0f), worldMatrix).xyz;
    float3 viewDir = cameraPos - worldPos;
        
    OUT.position = mul(float4(IN.position, 1.0f), worldViewProjectionMatrix);
    OUT.texCoord = IN.texCoord;
    OUT.lightDir = -light.dir;
    OUT.halfVector = normalize(normalize(OUT.lightDir) + normalize(viewDir));
    OUT.normal = mul(IN.normal, (float3x3)worldInverseTransposeMatrix);
    OUT.diffuse = material.diffuse * light.diffuse;
    OUT.specular = material.specular * light.specular;
        
    return OUT;
}

VS_OUTPUT_POINT VS_PointLighting(VS_INPUT IN)
{
    VS_OUTPUT_POINT OUT;

    float3 worldPos = mul(float4(IN.position, 1.0f), worldMatrix).xyz;
    
    OUT.position = mul(float4(IN.position, 1.0f), worldViewProjectionMatrix);
    OUT.texCoord = IN.texCoord;
    OUT.viewDir = cameraPos - worldPos;
    OUT.lightDir = (light.pos - worldPos) / light.radius;
    OUT.normal = mul(IN.normal, (float3x3)worldInverseTransposeMatrix);
    OUT.diffuse = material.diffuse * light.diffuse;
    OUT.specular = material.specular * light.specular;
    
    return OUT;
}

VS_OUTPUT_SPOT VS_SpotLighting(VS_INPUT IN)
{
    VS_OUTPUT_SPOT OUT;
    
    float3 worldPos = mul(float4(IN.position, 1.0f), worldMatrix).xyz;
           
    OUT.position = mul(float4(IN.position, 1.0f), worldViewProjectionMatrix);
    OUT.texCoord = IN.texCoord;
    OUT.viewDir = cameraPos - worldPos;
    OUT.lightDir = (light.pos - worldPos) / light.radius;
    OUT.spotDir = light.dir;
    OUT.normal = mul(IN.normal, (float3x3)worldInverseTransposeMatrix);
    OUT.diffuse = material.diffuse * light.diffuse;
    OUT.specular = material.specular * light.specular;
       
    return OUT;
}

//-----------------------------------------------------------------------------
// Pixel Shaders.
//-----------------------------------------------------------------------------

float4 PS_DirLighting(VS_OUTPUT_DIR IN) : COLOR
{
    float3 n = normalize(IN.normal);
    float3 h = normalize(IN.halfVector);
    float3 l = normalize(IN.lightDir);
    
    float nDotL = saturate(dot(n, l));
    float nDotH = saturate(dot(n, h));
    float power = (nDotL == 0.0f) ? 0.0f : pow(nDotH, material.shininess);   

    float4 color = (material.ambient * (globalAmbient + light.ambient)) +
                   (IN.diffuse * nDotL) + (IN.specular * power);

    return color * tex2D(colorMap, IN.texCoord);
}

float4 PS_PointLighting(VS_OUTPUT_POINT IN) : COLOR
{
    float atten = saturate(1.0f - dot(IN.lightDir, IN.lightDir));

    float3 n = normalize(IN.normal);
    float3 l = normalize(IN.lightDir);
    float3 v = normalize(IN.viewDir);
    float3 h = normalize(l + v);
    
    float nDotL = saturate(dot(n, l));
    float nDotH = saturate(dot(n, h));
    float power = (nDotL == 0.0f) ? 0.0f : pow(nDotH, material.shininess);
    
    float4 color = (material.ambient * (globalAmbient + (atten * light.ambient))) +
                   (IN.diffuse * nDotL * atten) + (IN.specular * power * atten);
                   
    return color * tex2D(colorMap, IN.texCoord);
}

float4 PS_SpotLighting(VS_OUTPUT_SPOT IN) : COLOR
{
    float atten = saturate(1.0f - dot(IN.lightDir, IN.lightDir));
    
    float3 l = normalize(IN.lightDir);
    float2 cosAngles = cos(float2(light.spotOuterCone, light.spotInnerCone) * 0.5f);
    float spotDot = dot(-l, normalize(IN.spotDir));
    float spotEffect = smoothstep(cosAngles[0], cosAngles[1], spotDot);
    
    atten *= spotEffect;
                                
    float3 n = normalize(IN.normal);
    float3 v = normalize(IN.viewDir);
    float3 h = normalize(l + v);
    
    float nDotL = saturate(dot(n, l));
    float nDotH = saturate(dot(n, h));
    float power = (nDotL == 0.0f) ? 0.0f : pow(nDotH, material.shininess);
    
    float4 color = (material.ambient * (globalAmbient + (atten * light.ambient))) +
                   (IN.diffuse * nDotL * atten) + (IN.specular * power * atten);
    
    return color * tex2D(colorMap, IN.texCoord);
}

//-----------------------------------------------------------------------------
// Techniques.
//-----------------------------------------------------------------------------

technique PerPixelDirectionalLighting
{
    pass
    {
        VertexShader = compile vs_2_0 VS_DirLighting();
        PixelShader = compile ps_2_0 PS_DirLighting();
    }
}

technique PerPixelPointLighting
{
    pass
    {
        VertexShader = compile vs_2_0 VS_PointLighting();
        PixelShader = compile ps_2_0 PS_PointLighting();
    }
}

technique PerPixelSpotLighting
{
    pass
    {
        VertexShader = compile vs_2_0 VS_SpotLighting();
        PixelShader = compile ps_2_0 PS_SpotLighting();
    }
}

Habe noch 2 weitere Lichtypen dazugenommen. Der Code ist nicht ganz von mir. Ein klein wenig hat mir ein Tutorial geholfen ;)
Danke

Zitat

Der RCCSWU (RandomCamelCaseSomtimesWithUndersquare) Stil bricht auch mal mit den veraltet strukturierten Denkmustern und erlaubt dem Entwickler seine Kreativität zu entfalten.

David_pb

Community-Fossil

Beiträge: 3 886

Beruf: 3D Graphics Programmer

  • Private Nachricht senden

10

03.02.2012, 13:19

Ja, das stimmt, allerdings ist da ein Unterschied wenn ich mehrere Objekte habe?


Nein ist es nicht. Du renderst jedes Objekt einzeln mit dem Shader.
@D13_Dreinig

Werbeanzeige

Ähnliche Themen