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

1

28.01.2007, 17:04

Specular Lighting in HLSL funkt nicht

Hallo,
es geht um eine effekt file. ich habe versucht die beleuchtung mit hlsl zu schreiben.
allerdings haut des net so ganz hin :(
der diffuse part funktioniert, aber beim specular part passt was nicht.
die grösse des glanzpunktes variiert, abhängig von der position des objects, hier eine halbkugel,
die lichtposition ist fix, (0, 1, 0)
die halbkugel dreht sich im kreis (sin(ftime), 0, cos(ftime))

achja ich verwende ne normal und gloss map, kann das mit dem zusammenhängen?

habt ihr eine ahnung was im shader falsch ist??

Quellcode

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
//--------------------------------------------------------------------------------------
// Globale Variablen
//--------------------------------------------------------------------------------------
float4 g_MaterialDiffuseColor;      // Materialstreufarbe
float4 g_MaterialAmbientColor;      // Materialhintergrundfarbe
float4 g_MaterialSpecularColor;     // Materialglanzfarbe

float4 g_LightDiffuse;              // Streufarbe des Lichts
float4 g_LightAmbient;              // Hintergrundfarbe des Lichts
float4 g_LightSpecular;             // Glanzfarbe des Lichts
float3 g_LightPos;                  // Lichtposition im "world space"

float g_fPhongExponent;             // Glanzstärke
float3 g_ViewPos;                   // Beobachter-Postition

texture g_MeshTexture;              // Modelltextur (Color Texture)
texture g_NormalTexture;            // (Normal Texture)
texture g_SpecularTexture;

float4x4 g_mWorld;                  // Weltmatrix für Objekt
float4x4 g_mWorldTrans;             // Transponierte Weltmatrix für Objekt
float4x4 g_mWorldViewProjection;    // Welt * Sicht * Projektion Matrix


//--------------------------------------------------------------------------------------
// Textur Sampler
//--------------------------------------------------------------------------------------
sampler MeshTextureSampler = 
sampler_state
{
    Texture = <g_MeshTexture>;
    MipFilter = Anisotropic;
    MinFilter = Anisotropic;
    MagFilter = Anisotropic;
    MaxAnisotropy = 16;
};

sampler MeshNTextureSampler = 
sampler_state
{
    Texture = <g_NormalTexture>;
    MipFilter = Anisotropic;
    MinFilter = Anisotropic;
    MagFilter = Anisotropic;
    MaxAnisotropy = 16;
};

sampler MeshSTextureSampler = 
sampler_state
{
    Texture = <g_SpecularTexture>;
    MipFilter = Anisotropic;
    MinFilter = Anisotropic;
    MagFilter = Anisotropic;
    MaxAnisotropy = 16;
};

//--------------------------------------------------------------------------------------
// Vertex Shader Ausgabe-Struktur
//--------------------------------------------------------------------------------------
struct VS_OUTPUT
{
    float4 Position   : POSITION;   // Vertex Position 
    float2 TextureUV  : TEXCOORD0;  // Vertex Textur Koord
    float3 Normal     : TEXCOORD1;  // Normale
    float3 Tangent    : TEXCOORD2;  // Tangente
    float3 Binormal   : TEXCOORD3;  // Binormale
    float3 Light      : TEXCOORD4;  // Lichtvektor
    float3 View       : TEXCOORD5;  // Viewvektor

};


//--------------------------------------------------------------------------------------
// Vertex Shader
//--------------------------------------------------------------------------------------
VS_OUTPUT RenderSceneVS( float4 vPos : POSITION, 
                         float3 vNormal : NORMAL,
                         float3 vTangent : TANGENT,
                         float2 vTexCoord0 : TEXCOORD0,
                         uniform bool bTexture)
{
    VS_OUTPUT Output;
    float3 vNormalWorldSpace;
    float3 vTangentWorldSpace;
    float3 vBinormalWorldSpace;
    float4 vVertexWorldSpace;

    // Transform the position from object space to homogeneous projection space
    Output.Position = mul(vPos, g_mWorldViewProjection);
    
    // Transform the normal from Texture/Tangent space to world space
    Output.Normal = mul(vNormal, (float3x3)g_mWorldTrans);
     
    // Transform the tangent from Texture/Tangent space to world space    
    Output.Tangent = mul(vTangent, (float3x3)g_mWorldTrans);
    
    // Set up the tangent frame in world space
    Output.Binormal = cross( Output.Normal, Output.Tangent ); 

    // Vertex to world Space
    vVertexWorldSpace = mul(vPos, g_mWorld);
    
    // Build the light vector from Light Source to Vertex
    Output.Light = g_LightPos - float3(vVertexWorldSpace.x, vVertexWorldSpace.y, vVertexWorldSpace.z);
    
    // Build view vector
    Output.View = g_ViewPos - float3(vVertexWorldSpace.x, vVertexWorldSpace.y, vVertexWorldSpace.z);
    
    // Textur Koordinaten setzen
    if( bTexture ) 
        Output.TextureUV = vTexCoord0; 
    else
        Output.TextureUV = 0; 
    
    return Output;    
}


//--------------------------------------------------------------------------------------
// Pixel Shader Ausgabe-Struktur
//--------------------------------------------------------------------------------------
struct PS_OUTPUT
{
    float4 RGBColor : COLOR0;  // Pixel color    
};


//--------------------------------------------------------------------------------------
// Pixel Shader
//--------------------------------------------------------------------------------------
PS_OUTPUT RenderScenePS( VS_OUTPUT In,
                         uniform bool bTexture,
                         uniform bool bSpecularOnly) 
{ 
    PS_OUTPUT Output;
    float3 vrgb = float3(0,0,0);;
    float3 vDiffuse = float3(0,0,0);

    // Load NormalMap
    float3 vTexNormal = tex2D(MeshNTextureSampler, In.TextureUV);
    
    // Load GlossMap and calc glossValue
    float3 vTexGloss = tex2D(MeshSTextureSampler, In.TextureUV);
    float fTexGloss = 0.299f*vTexGloss.x;
    fTexGloss = fTexGloss*g_fPhongExponent; //4
    
    // Normalize and Move the lightvector from tangent space to world space
    float3x3 mTangentFrame = { In.Binormal, In.Tangent, In.Normal };
    mTangentFrame = transpose(mTangentFrame);
    float3 vLight = normalize(In.Light);
    vLight = mul(vLight, (float3x3)mTangentFrame);
    vLight = normalize(vLight);
    
    // Normalize and Move the viewvector from tangent space to world space
    float3 vView = normalize(In.View);
    vView = mul(vView, (float3x3)mTangentFrame);
    vView = normalize(vView);
    
     // If using a signed texture, we must unbias the normal map data
    vTexNormal = (vTexNormal*2)-1;

    // Streukomponente berechnen
    float fNdotV = saturate(dot(vTexNormal, vLight));
    vDiffuse = g_MaterialDiffuseColor * g_LightDiffuse * fNdotV;

    // Glanzkomponente berechnen
    if (bSpecularOnly)
    {
        float3 vReflectionWorldSpace = float3(0,0,0);
        float3 vSpecular = float3(0,0,0);

        if ((dot(vTexNormal, vLight)) >= 0)
        {
            vReflectionWorldSpace = 2*vTexNormal*fNdotV - vLight;
            vSpecular = g_MaterialSpecularColor * g_LightSpecular * pow(max(0,dot(vReflectionWorldSpace, vView)), fTexGloss);
        }
        vrgb =  vDiffuse + vSpecular;
    }
    else
        vrgb =  vDiffuse;
    
    Output.RGBColor.a = 1.0f;
    
    
    // Textur laden und mit Diffus-Farbe multiplizieren
    if( bTexture )
        Output.RGBColor.rgb = tex2D(MeshTextureSampler, In.TextureUV) * vrgb;
    else
        Output.RGBColor.rgb = vrgb;

    return Output;
}


//--------------------------------------------------------------------------------------
// Rendern
//--------------------------------------------------------------------------------------
technique RenderSceneWithTexture1Light2
{
    pass P0
    {          
        VertexShader = compile vs_2_0 RenderSceneVS( true);
        PixelShader  = compile ps_2_0 RenderScenePS( true, true);
    }
}


hier n paar screenies:

http://img242.imageshack.us/img242/7562/screenshot8up6.jpg
http://img412.imageshack.us/img412/7374/screenshot9ux1.jpg
http://img242.imageshack.us/img242/9259/screenshot10ue6.jpg
http://img242.imageshack.us/img242/1604/screenshot11ae1.jpg
http://img252.imageshack.us/img252/7426/screenshot12fn5.jpg
http://img252.imageshack.us/img252/3927/screenshot13bk5.jpg

Beiträge: 774

Beruf: Student

  • Private Nachricht senden

2

28.01.2007, 18:26

Zum Transformieren der Normalen brauchst du die Invertierte Transponierte Weltmatrix.

Jedoch würde ich empfehlen, statt die Normalen zu transformieren, einfach die Position des Lichtes vor der übergabe an den Shader mit der invertierten Weltmatrix zu transformieren. Natrürlich musst du dann die Position des Betrachters genauso transformieren und sparst dir damit wiederum die Transformation der Positionen.
Beim Vektor zum Betrachter reicht es meines Wissens ebenfalls aus ihn PerVertex zu rechnen
Zudem solltest du alle Normalen aus dem Vertexshader im Pixelshader normalisieren, da sie durch das GoroudShading verändert werden.

Das mit den ifs in Vertex und Pixelshader macht zwar flexibel, halt ich aber für keine gute idee, da sie das ganze noch unötig langsamer machen.

Das mit der "Gloss" Textur ist mir neu. Was macht die?

BlackSnake

Community-Fossil

Beiträge: 1 549

Beruf: Student

  • Private Nachricht senden

3

28.01.2007, 19:34

was ich mir sonst immer anhören muss:
google ist dein freund ;)

4

28.01.2007, 20:34

So die ifs hab ich jetzt entfernt, waren vorher eh nur zum austesten da.

die normale, binormale und tangente übergeb ich jetzt nicht mehr an den pixel shader, nur mehr den view und light vector

Zitat

Jedoch würde ich empfehlen, statt die Normalen zu transformieren, einfach die Position des Lichtes vor der übergabe an den Shader mit der invertierten Weltmatrix zu transformieren. Natrürlich musst du dann die Position des Betrachters genauso transformieren und sparst dir damit wiederum die Transformation der Positionen.

Hab ich dann die Position des Lichtes und Betrachters in dem Objekt space angegeben?

Durch die gloss textur erreiche ich verschieden glanzstärken = helligkeit der texur,
so kann ich zb metall u holz auf EINER textur simulieren.


irgendwie will jetzt das licht mit dem object mit, obwohls nicht so vorgesehen ist, hmm

und die elliptische specular highlight ist immer noch da :(

hier der aktuelle hlsl code:

Quellcode

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
//--------------------------------------------------------------------------------------
// Globale Variablen
//--------------------------------------------------------------------------------------
float4 g_MaterialDiffuseColor;      // Materialstreufarbe
float4 g_MaterialAmbientColor;      // Materialhintergrundfarbe
float4 g_MaterialSpecularColor;     // Materialglanzfarbe

float4 g_LightDiffuse;              // Streufarbe des Lichts
float4 g_LightAmbient;              // Hintergrundfarbe des Lichts
float4 g_LightSpecular;            // Glanzfarbe des Lichts
float3 g_LightPos;                  // Lichtposition im "world space"

float g_fPhongExponent;            // Glanzstärke
float3 g_ViewPos;               // Beobachter-Postition

texture g_MeshTexture;              // Modelltextur (Color Texture)
texture g_NormalTexture;            // (Normal Texture)
texture g_SpecularTexture;

float4x4 g_mWorld;                  // Weltmatrix für Objekt
float4x4 g_mWorldTrans;             // Transponierte Weltmatrix für Objekt
float4x4 g_mWorldViewProjection;    // Welt * Sicht * Projektion Matrix


//--------------------------------------------------------------------------------------
// Textur Sampler
//--------------------------------------------------------------------------------------
sampler MeshTextureSampler =
sampler_state
{
    Texture = <g_MeshTexture>;
    MipFilter = Anisotropic;
    MinFilter = Anisotropic;
    MagFilter = Anisotropic;
    MaxAnisotropy = 16;
};

sampler MeshNTextureSampler =
sampler_state
{
    Texture = <g_NormalTexture>;
    MipFilter = Anisotropic;
    MinFilter = Anisotropic;
    MagFilter = Anisotropic;
    MaxAnisotropy = 16;
};

sampler MeshSTextureSampler =
sampler_state
{
    Texture = <g_SpecularTexture>;
    MipFilter = Anisotropic;
    MinFilter = Anisotropic;
    MagFilter = Anisotropic;
    MaxAnisotropy = 16;
};

//--------------------------------------------------------------------------------------
// Vertex Shader Ausgabe-Struktur
//--------------------------------------------------------------------------------------
struct VS_OUTPUT
{
    float4 Position   : POSITION;   // Vertex Position
    float2 TextureUV  : TEXCOORD0;  // Vertex Textur Koord
    float3 Light      : TEXCOORD1;   // Lichtvektor
    float3 View       : TEXCOORD2;   // Viewvektor

};


//--------------------------------------------------------------------------------------
// Vertex Shader
//--------------------------------------------------------------------------------------
VS_OUTPUT RenderSceneVS( float4 vPos : POSITION,
                         float3 vNormal : NORMAL,
                         float3 vTangent : TANGENT,
                         float2 vTexCoord0 : TEXCOORD0)
{
    VS_OUTPUT Output;
    float3 vBinormal;

    // Transform the position from object space to homogeneous projection space
    Output.Position = mul(vPos, g_mWorldViewProjection);
   
    // Set up the binormal
    vBinormal = cross(vNormal, vTangent);
    
    // Set up the tangent frame , transforms from object to tangent space
    float3x3 objToTangent = transpose(float3x3(vTangent, vBinormal, vNormal));

    // Build the light vector from Light Source to Vertex, calc in object space, transform to tangent space
    float3 l = mul(g_LightPos - vPos.xyz, objToTangent);
   
    // Build view vector
    float3 v = mul(g_ViewPos - vPos.xyz, objToTangent);
    
    // Normalize vectors
    Output.Light = normalize(l);
    Output.View = normalize(v); 
   
    // Textur Koordinaten setzen
    Output.TextureUV = vTexCoord0;
   
    return Output;   
}


//--------------------------------------------------------------------------------------
// Pixel Shader Ausgabe-Struktur
//--------------------------------------------------------------------------------------
struct PS_OUTPUT
{
    float4 RGBColor : COLOR0;  // Pixel color   
};


//--------------------------------------------------------------------------------------
// Pixel Shader
//--------------------------------------------------------------------------------------
PS_OUTPUT RenderScenePS( VS_OUTPUT In)
{
    PS_OUTPUT Output;

    // load normal map
    float3 vTexNormal = tex2D(MeshNTextureSampler, In.TextureUV);
   
    // Load gloss map and calc gloss value
    float3 vTexGloss = tex2D(MeshSTextureSampler, In.TextureUV);
    float fTexGloss = 0.299f * vTexGloss.x * g_fPhongExponent;
   
    // normalize light and view vectors
    float3 vLight = normalize(In.Light); 
    float3 vView = normalize(In.View);
   
    // we are using a signed texture, we must unbias the normal map data
    vTexNormal = (vTexNormal*2)-1;

    // Streukomponente
    float fNdotV = saturate(dot(vTexNormal, vLight));
    float3 vDiffuse = g_MaterialDiffuseColor * g_LightDiffuse * fNdotV;

    // Glanzkomponente
    float3 vReflectionWorldSpace = 2*vTexNormal*fNdotV - vLight;
    float3 vSpecular = g_MaterialSpecularColor * g_LightSpecular * pow(max(0,dot(vReflectionWorldSpace, vView)), fTexGloss);

    // End-Farbe
    float3 vrgb =  vDiffuse + vSpecular;
   
    // Textur laden, mit End-Farbe multiplizieren
    Output.RGBColor.rgb = tex2D(MeshTextureSampler, In.TextureUV) * vrgb;
    
    Output.RGBColor.a = 1.0f;

    return Output;
}


//--------------------------------------------------------------------------------------
// Rendern
//--------------------------------------------------------------------------------------
technique RenderSceneWithTexture1Light2
{
    pass P0
    {         
        VertexShader = compile vs_2_0 RenderSceneVS();
        PixelShader  = compile ps_2_0 RenderScenePS();
    }
}

Beiträge: 774

Beruf: Student

  • Private Nachricht senden

5

29.01.2007, 18:29

Quellcode

1
float3x3 objToTangent = transpose(float3x3(vTangent, vBinormal, vNormal));


Ich bin mir nicht sicher ... muss man den die Matrix wirklich transponieren?

6

29.01.2007, 19:28

also wenn man sie transponiert schauts so aus:

http://img254.imageshack.us/img254/8430/screenshot16kr9.jpg
http://img254.imageshack.us/img254/1061/screenshot17jr7.jpg
http://img166.imageshack.us/img166/1138/screenshot18fn4.jpg

wenn man sie NICHT transponiert so:

http://img265.imageshack.us/img265/3010/screenshot14cv8.jpg
http://img234.imageshack.us/img234/7690/screenshot15tx3.jpg

der specular highlight beim 2.fall gefällt mir irgendwie besser, da ist der higlight nicht so komisch elliptisch verzerrt.

man beachte wieder, dass sind ausschnitte von der sich um die lichtquelle bewegende halbkugel.

allerdings scheint sich, wi schon gesagt, die lichtquelle mitzubewegen, komisch.

dot

Supermoderator

Beiträge: 9 757

Wohnort: Graz

  • Private Nachricht senden

7

29.01.2007, 20:36

Quellcode

1
float fTexGloss = 0.299f * vTexGloss.x * g_fPhongExponent;


normalerweise multipliziert man den fertigen specular anteil mit dem gloss faktor und nicht den exponenten...

Zitat

// we are using a signed texture, we must unbias the normal map data


wenn du schon signed werte hast, für was musst du die dann unbiasen !? ;)

kann sein, dass ich grad genau falsch rum denke, aber dein tangent space sollte doch linkshändig sein oder?

Quellcode

1
vBinormal = cross(vNormal, vTangent); 


müsste das dann nicht tangent x normal heißen?

wie berechnest du deine tangent vektoren?

8

29.01.2007, 22:25

wenn ich

Quellcode

1
vTexNormal = (2*vTexNormal)-1;
verwende und die normalen als output durchgebe, schauts so aus:
http://img292.imageshack.us/img292/4826/screenshot20yz7.jpg

sonst:
http://img481.imageshack.us/img481/2756/screenshot19dq0.jpg


die png file (=normal map) ist:
http://img58.imageshack.us/img58/9112/16tile01bumpiz1.png



Zitat von »"dot"«

normalerweise multipliziert man den fertigen specular anteil mit dem gloss faktor und nicht den exponenten...


ja habs ausgebesser, der code schaut jetzt folgendermassen aus:

Quellcode

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
//--------------------------------------------------------------------------------------
// Pixel Shader
//--------------------------------------------------------------------------------------
PS_OUTPUT RenderScenePS( VS_OUTPUT In)
{
    PS_OUTPUT Output;

    // load normal map; we are using a signed texture, we must unbias the normal map data
    float3 vTexNormal = tex2D(MeshNTextureSampler, In.TextureUV);
    vTexNormal = (2*vTexNormal)-1;
   
    // Load gloss map and calc gloss value
    float3 vTexGloss = tex2D(MeshSTextureSampler, In.TextureUV);
    float fTexGloss = 0.299f * vTexGloss.x;
   
    // normalize light and view vectors
    float3 vLight = normalize(In.Light); 
    float3 vView = normalize(In.View);

    // diffuse part
    float fNdotL = saturate(dot(vTexNormal, vLight));
    float3 vDiffuse = g_MaterialDiffuseColor * g_LightDiffuse * fNdotL;

    // specular part
    float3 vReflectionWorldSpace = 2*fNdotL*vTexNormal - vLight;
    float3 vSpecular = g_MaterialSpecularColor * g_LightSpecular;
    // ... and multiply with factor
    vSpecular = vSpecular * fTexGloss * pow(saturate(dot(vView, vReflectionWorldSpace)), g_fPhongExponent);

    // final color
    float3 vrgb =  vDiffuse + vSpecular;
   
    // load texture, multiply with final color
    Output.RGBColor.rgb = tex2D(MeshTextureSampler, In.TextureUV) * vrgb;
    
    Output.RGBColor.a = 1.0f;

    return Output;
}



Zitat von »"dot"«


kann sein, dass ich grad genau falsch rum denke, aber dein tangent space sollte doch linkshändig sein oder?

Quellcode

1
vBinormal = cross(vNormal, vTangent); 


müsste das dann nicht tangent x normal heißen?

wie berechnest du deine tangent vektoren?


Gute Frage, hmm, ich hab da einen eigenen algorithmus, der den halbkreis berechnet, die tangenten setz ich für alle vertices einfach auf (1,0,0)

Quellcode

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
tbResult CPhongLightingMesh::KugelHalbe_Mantel(float fRadius, int iSegmentY, int iSegmentR)
{
    char acString[64];
    
    //Halbkugelparameter prüfen - min 1 iSegmentY, min 3 iSegmentR)
    if ((fRadius<=0.0f)||(iSegmentY < 1)||(iSegmentR < 3))
        TB_ERROR("Halbkugel mit falschem Parameter intitalisiert!", TB_ERROR);

    //Halbkugelparameter setzen
    m_fHaKuRadius = fRadius;
    m_iHaKuSegmentY = iSegmentY;
    m_iHaKuSegmentR = iSegmentR;
    
    // Effekt Index und VertexBuffer erstellen
    m_pPSQEffect = new tbEffect;
    m_pKVB = new tbVertexBuffer;
    m_pKIB = new tbIndexBuffer;

    //VB initialisieren - Oberseite der Kugel: 1 + m_iHaKuSegmentY*m_iHaKuSegmentR
    int NumVertices = 1 + m_iHaKuSegmentY*m_iHaKuSegmentR;
    if(m_pKVB->Init(NumVertices * sizeof(SPSQVertex), sizeof(SPSQVertex), PSQ_VERTEX_FVF,
                        D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, D3DPOOL_DEFAULT))
    {
        return TB_ERROR;
    }

    //IB initialisieren - Bei 1. SegmentY: m_iHaKuSegmentR-Dreiecke
    //Plus jedes weitere SegmentY: 2*m_iHaKuSegmentR-Dreiecke
    int NumIndices = 3*m_iHaKuSegmentR;
    for (int i=1; i<m_iHaKuSegmentY; i++)
        NumIndices += 6*m_iHaKuSegmentR;

    if(m_pKIB->Init(NumIndices * sizeof(WORD), sizeof(WORD), D3DFMT_INDEX16))
    {
        return TB_ERROR;
    }

    //Indizies
    WORD wIndex;
    //1.Segment
    for (int i=0; i<m_iHaKuSegmentR; i++)
    {
        wIndex = i+1;   m_pKIB->AddIndex(&wIndex); 
        wIndex = 0;     m_pKIB->AddIndex(&wIndex); 
        if (i != m_iHaKuSegmentR-1)
            wIndex = i+2;
        else
            wIndex = 1;
        m_pKIB->AddIndex(&wIndex);
    }

    //weitere Segmente
    for (int j=1; j<m_iHaKuSegmentY; j++)
    {
        for (int i=0; i<m_iHaKuSegmentR; i++)
        {
            wIndex = i+1+m_iHaKuSegmentR*j;     m_pKIB->AddIndex(&wIndex);
            wIndex = i+1+m_iHaKuSegmentR*(j-1); m_pKIB->AddIndex(&wIndex);
            if (i != m_iHaKuSegmentR-1)
                wIndex = i+2+m_iHaKuSegmentR*j;
            else
                wIndex = i+2+m_iHaKuSegmentR*(j-1);
            m_pKIB->AddIndex(&wIndex);

            wIndex = i+1+m_iHaKuSegmentR*(j-1); m_pKIB->AddIndex(&wIndex);
            if (i != m_iHaKuSegmentR-1)
                wIndex = i+2+m_iHaKuSegmentR*(j-1);
            else
                wIndex = i+2+m_iHaKuSegmentR*(j-2);
            m_pKIB->AddIndex(&wIndex); 
            if (i != m_iHaKuSegmentR-1)
                wIndex = i+2+m_iHaKuSegmentR*j;
            else
                wIndex = i+2+m_iHaKuSegmentR*(j-1);
            m_pKIB->AddIndex(&wIndex); 
        }
    }

    // Den Index-Buffer aktualisieren
    if(m_pKIB->Update()) return TB_ERROR;
    
    // Textur laden
    m_pPSQTex = tbTextureManager::GetTexture(TEXTURE, 1);
    m_pPSQNTex = tbTextureManager::GetTexture(NORMAL_TEXTURE, 1);
    m_pPSQSTex = tbTextureManager::GetTexture(SPECULAR_TEXTURE, 1);

    // Effekt laden
    if(m_pPSQEffect == NULL) return TB_ERROR;
    if(m_pPSQEffect->Init("Data\\PhongLighting.fx")) return TB_ERROR;

    // Vertices
    SPSQVertex  Vertex;
    // Spitze
    Vertex.vPosition = tbVector3(0.0f, m_fHaKuRadius, 0.0f);
    Vertex.vNormal = tbVector3(0.0f, 1.0f, 0.0f);
    Vertex.vTangent = tbVector3(1.0f, 0.0f, 0.0f);//<------------------------------------HIER WIRD DIE TANGENTE GESETZT
    Vertex.vTexture = tbVector2(0.0f, 0.0f);    
    m_pKVB->SetVertex(0, &Vertex);
    // Segmentbildende Vertices
    float val = (TB_PI/2.0f)/m_iHaKuSegmentY;
    float valR = (2.0f*TB_PI)/m_iHaKuSegmentR;
    for (int j=m_iHaKuSegmentY-1; j>=0; j--)
    {
        float height = m_fHaKuRadius* sinf(val*j);
        for (int i=0; i<m_iHaKuSegmentR; i++)
        {
            Vertex.vPosition = tbVector3(m_fHaKuRadius* cosf(valR*i)*cosf(val*j), height, m_fHaKuRadius* sinf(valR*i)*cosf(val*j));
            Vertex.vNormal = tbVector3(cosf(valR*i)*cosf(val*j), height, sinf(valR*i)*cosf(val*j));
            Vertex.vTexture = tbVector2(cosf(valR*i)*cosf(val*j), sinf(valR*i)*cosf(val*j));
            m_pKVB->SetVertex(i+1+m_iHaKuSegmentR*(m_iHaKuSegmentY-j-1), &Vertex);
            
        }
    }

    if(m_pKVB->Update()) return TB_ERROR;

    TB_INFO("Halbkugel initialisiert");
    
    m_pPSQNumTechniques = m_pPSQEffect->GetNumTechniques();
    m_pPSQTechnique = 0;
    m_fShininess = 128;

    return TB_OK;
}

dot

Supermoderator

Beiträge: 9 757

Wohnort: Graz

  • Private Nachricht senden

9

29.01.2007, 22:29

Zitat

wenn ich
Code:
vTexNormal = (2*vTexNormal)-1;
verwende und die normalen als output durchgebe, schauts so aus:
http://img292.imageshack.us/img292/4826/screenshot20yz7.jpg

sonst:
http://img481.imageshack.us/img481/2756/screenshot19dq0.jpg


es is schon ok was du machst. nur sind die daten in der textur ja unsigned, sonst müsstest du sie nicht unbiasen.
tut nix zur sache, wollt nur drauf hinweisen, dass das kommentar dort etwas komisch is ;)

Zitat


die png file (=normal map) ist:
http://img58.imageshack.us/img58/9112/16tile01bumpiz1.png


ich glaub nicht, dass das eine normalmap is ;)

Zitat

die tangenten setz ich für alle vertices einfach auf (1,0,0)


und du erwartest ernsthaft, dass das dann funktioniert?

https://www.spieleprogrammierer.de/phpBB…opic.php?t=6600

10

29.01.2007, 22:40

dann is es ne bump map, oder?
Aber, kann ich die denn nicht auch für lichtberechnungnen verwenden?

wie kann ich mir denn die tangenten berechnen, ich meine die normalen sind ja noch einfach aber die tangenten zu berechnen, da blick ich nicht mehr durch.

die tangente ist ja die u-kooridate der textur, die binormale die v koordinate

so etwas
----------- u
|
|
|
|
v

und die normale ist normale auf u und v

hier steh ich mit meinem wissen an...

edit1
sorry hab den link nicht gelesne, uups

Werbeanzeige