shader phong per pixel(ARB)

Aus DGL Wiki
Wechseln zu: Navigation, Suche

Per-Pixel-Lighting

Zurück zur Shadersammlung

Beschreibung Autor Version
Per-Pixel-Beleuchtung mit einem ARB-Shader. dj3hut1 1.0

Bilder

Utah-Teapot mit Per-Pixel-Beleuchtung.

Beschreibung

Dieser Shader berechnet eine standardmässige Beleuchtung in einem Fragment-Shader ( Phong-Beleuchtung ) und berücksichtigt zusätzlich noch Attenuation-Faktoren.

Besondere Vorraussetzungen

Für die Shader werden nur die Erweiterungen GL_ARB_fragment_program und GL_ARB_vertex_program benötigt.

Code

Vertexprogramm

phong.vp

!!ARBvp1.0

ATTRIB iV = vertex.position;
ATTRIB iN = vertex.normal;

OUTPUT oPos = result.position;
OUTPUT oV = result.texcoord[0];
OUTPUT oN = result.texcoord[1];

PARAM mvp[4] = { state.matrix.mvp };
PARAM mv[4] = { state.matrix.modelview };
PARAM mvinv[4] = { state.matrix.modelview.invtrans };

TEMP N;

# transform vertex with mvp
DP4 oPos.x, iV, mvp[0];
DP4 oPos.y, iV, mvp[1];
DP4 oPos.z, iV, mvp[2];
DP4 oPos.w, iV, mvp[3];

# transform vertex with mv
DP4 oV.x, iV, mv[0];
DP4 oV.y, iV, mv[1];
DP4 oV.z, iV, mv[2];
DP4 oV.w, iV, mv[3];

# transform the normal to eye coordinates.
DP3 N.x, mvinv[0], iN;
DP3 N.y, mvinv[1], iN;
DP3 N.z, mvinv[2], iN;

# normalize
DP3 N.w, N, N;
RSQ N.w, N.w;
MUL oN, N, N.w;

END

Fragmentprogramm

phong.fp

!!ARBfp1.0

ATTRIB iPos = fragment.position;
ATTRIB iV = fragment.texcoord[0];
ATTRIB iN = fragment.texcoord[1];

OUTPUT oCol = result.color;

TEMP LC, L, E, R, rN, amb, diff, spec, col, attenuation, d, d2;

# L - V
SUB L, state.light[0].position, iV;

# attenuation 
DP3 d, L, L;
RSQ d2, d.x;
DST d, d, d2;
DP3 attenuation, state.light[0].attenuation, d;
RCP attenuation, attenuation.x;

# normalize L
DP3 L.w, L, L;
RSQ L.w, L.w;
MUL L, L, L.w;

# E eye vector
SUB E, 0, iV;

# normalize E
DP3 E.w, E, E;
RSQ E.w, E.w;
MUL E, E, E.w;

# nDotLN
DP3 LC.x, iN, L;

# R reflectance ( L - 2 * nDotLN * N )
MUL rN, iN, LC.x;
MUL rN, rN, 2.0;
SUB R, L, rN;

# normalize R
DP3 R.w, R, R;
RSQ R.w, R.w;
MUL R, R, R.w;

# -R
SUB R, 0, R;

# nDotER
DP3 LC.y, E, R;

# shininess
MOV LC.w, state.material.shininess.x;

# light coefficents
LIT LC, LC;

# ambient
MUL amb, state.lightprod[0].ambient, LC.x;
MUL amb, amb, attenuation;

# diffuse
MUL diff, state.lightprod[0].diffuse, LC.y;
MUL diff, diff, attenuation;

# specular
MUL spec, state.lightprod[0].specular, LC.z;
MUL spec, spec, attenuation;

# result color
ADD col, state.lightmodel.scenecolor, amb;
ADD col, col, diff;
#MUL col, col, iN;
ADD oCol, col, spec;

END