Wednesday 15 July 2015

c++ - Weird lighting effect using a point light on a skydome -



c++ - Weird lighting effect using a point light on a skydome -

i have problem lighting. when lite goes on vertex point or line lighting gets weird look. (it's supposed round.) i've been trying kind of things can't find out why happening.

i create skydome , normalize possition

double deltalatitude = xm_pi / segments; double deltalongitude = xm_pi * 2.0 / segments; int index = 0; d3dxcolor bottomcolor = d3dxcolor(0.529f, 0.807f, 0.821f, 1.0f); d3dxcolor topcolor = d3dxcolor(0.179f, 0.557f, 1.0f, 1.0f); (int = 1; < segments; i++) { double r0 = sin(i * deltalatitude); double y0 = cos(i * deltalatitude); (int j = 0; j < segments; j++) { double x0 = r0 * sin(j * deltalongitude); double z0 = r0 * cos(j * deltalongitude); d3dxvector3 v = d3dxvector3(x0, y0, z0);//create normal based on possition (-1 <> 1) d3dxvec3normalize(&v, &v);//normalize vertpos.push_back(vertex(x0, y0, z0, lerp(bottomcolor, topcolor, y0), v)); } } vertpos.push_back(vertex(0, 1, 0, lerp(bottomcolor, topcolor, 1), d3dxvector3(0.0f, 1.0f, 0.0f))); vertpos.push_back(vertex(0, -1, 0, lerp(bottomcolor, topcolor, -1), d3dxvector3(0.0f, -1.0f, 0.0f))); (int = 0; < segments - 2; i++) { (int j = 0; j < segments; j++) { indices.push_back(segments * + j); indices.push_back(segments * + (j + 1) % segments); indices.push_back(segments * (i + 1) + (j + 1) % segments); indices.push_back(segments * + j); indices.push_back(segments * (i + 1) + (j + 1) % segments); indices.push_back(segments * (i + 1) + j); } } // create faces of top of dome (int = 0; < segments; i++) { indices.push_back(segments * (segments - 1)); indices.push_back((i + 1) % segments); indices.push_back(i); } // create faces of bottom of dome (int = 0; < segments; i++) { indices.push_back(segments * (segments - 1) + 1); indices.push_back(segments * (segments - 2) + i); indices.push_back(segments * (segments - 2) + (i + 1) % segments); }

vertex shader:

vs_output vs(float4 inpos : position, float2 intexcoord : texcoord, float4 incolor : color, float3 normal : normal) { vs_output output; output.pos = mul(inpos, wvp); output.worldpos = mul(inpos, world); output.normal = mul(normal, world); output.color = incolor; homecoming output; }

and here pixel shader:

float4 ps(vs_output input) : sv_target { input.normal = normalize(input.normal); float4 diffuse = input.color; float skycolor = light.spos.a; float4 suncolor = float4(500.0f, 100.0f, 100.0f, 1.0f); float3 finalcolor = float3(0.0f, 0.0f, 0.0f); float3 finalambient = diffuse * skycolor; float sunrange = 1000.0f; float3 lighttopixelvec = light.spos - input.worldpos; float d = length(lighttopixelvec); if( d > sunrange ){ homecoming float4(finalambient, diffuse.a); } //turn lighttopixelvec unit length vector describing //the pixels direction lights position lighttopixelvec /= d; //calculate how much lite pixel gets angle //in lite strikes pixels surface float howmuchlight = dot(lighttopixelvec, input.normal); //if lite striking front end side of pixel if( howmuchlight > 0.0f ) { //add lite finalcolor of pixel finalcolor += howmuchlight * finalambient * suncolor; //calculate light's falloff factor finalcolor /= 10.0f + (0.01 * d) + (0.01 * (d*d)); } finalcolor = saturate(finalcolor + finalambient); homecoming float4(finalcolor, diffuse.a); }

you don't need think faces normals @ vertices. it's hemisphere, right, normal vector centre of sphere (the origin guess) vertex, should normalise length of 1. normal of vertex coordinates normalised.

c++ opengl directx shader lighting

No comments:

Post a Comment