Saturday 15 August 2015

c++ - Lighting a cube with a shader issue -



c++ - Lighting a cube with a shader issue -

i've been next tutorials on directx 11 book , trying give code lighting effects shader. problem shows buffer , doens't seem te create utilize of lighting shader file @ all. trying lite cube. help already. hlsl:

struct directionallight { float4 ambient; float4 diffuse; float4 specular; float3 direction; float pad; }; struct pointlight { float4 ambient; float4 diffuse; float4 specular; float3 position; float range; float3 att; float pad; }; struct spotlight { float4 ambient; float4 diffuse; float4 specular; float3 position; float range; float3 direction; float spot; float3 att; float pad; }; struct material { float4 ambient; float4 diffuse; float4 specular; float4 reflect; }; void computedirectionallight( material mat, directionallight l, float3 normal, float3 toeye, out float4 ambient, out float4 diffuse, out float4 spec) { ambient = float4(0.0f, 0.0f, 0.0f, 0.0f); diffuse = float4(0.0f, 0.0f, 0.0f, 0.0f); spec = float4(0.0f, 0.0f, 0.0f, 0.0f); float3 lightvec = -l.direction; ambient = mat.ambient * l.ambient; float diffusefactor = dot(lightvec, normal); [flatten] if( diffusefactor > 0.0f ) { float3 v = reflect(-lightvec, normal); float specfactor = pow(max(dot(v, toeye), 0.0f), mat.specular.w); diffuse = diffusefactor * mat.diffuse * l.diffuse; spec = specfactor * mat.specular * l.specular; } } void computepointlight(material mat, pointlight l, float3 pos, float3 normal, float3 toeye, out float4 ambient, out float4 diffuse, out float4 spec) { ambient = float4(0.0f, 0.0f, 0.0f, 0.0f); diffuse = float4(0.0f, 0.0f, 0.0f, 0.0f); spec = float4(0.0f, 0.0f, 0.0f, 0.0f); float3 lightvec = l.position - pos; float d = length(lightvec); if( d > l.range ) return; lightvec /= d; ambient = mat.ambient * l.ambient; float diffusefactor = dot(lightvec, normal); [flatten] if(diffusefactor > 0.0f) { float3 v = reflect(-lightvec, normal); float specfactor = pow(max(dot(v, toeye), 0.0f), mat.specular.w); diffuse = diffusefactor * mat.diffuse * l.diffuse; spec = specfactor * mat.specular * l.specular; } float att = 1.0f / dot(l.att, float3(1.0f, d, d*d)); diffuse *= att; spec *= att; } void computespotlight(material mat, spotlight l, float3 pos, float3 normal, float3 toeye, out float4 ambient, out float4 diffuse, out float4 spec) { ambient = float4(0.0f, 0.0f, 0.0f, 0.0f); diffuse = float4(0.0f, 0.0f, 0.0f, 0.0f); spec = float4(0.0f, 0.0f, 0.0f, 0.f); float3 lightvec = l.position - pos; float d = length(lightvec); if( d > l.range ) return; lightvec /= d; ambient = mat.ambient * l.ambient; float diffusefactor = dot(lightvec, normal); [flatten] if( diffusefactor > 0.0f ) { float3 v = reflect(-lightvec, normal); float specfactor = pow(max(dot(v, toeye), 0.0f), mat.specular.w); diffuse = diffusefactor * mat.diffuse * l.diffuse; spec = specfactor * mat.specular * l.specular; } float spot = pow(max(dot(-lightvec, l.direction), 0.0f), l.spot); float att = spot / dot(l.att, float3(1.0f, d, d*d)); ambient *= spot; diffuse *= att; spec *= att; } cbuffer cbperframe { directionallight gdirlight; pointlight gpointlight; spotlight gspotlight; float3 geyeposw; }; cbuffer cbperobject { float4x4 gworld; float4x4 gworldinvtranspose; float4x4 gworldviewproj; material gmaterial; }; struct vertexin { float3 posl : position; float3 normall : normal; }; struct vertexout { float4 posh : sv_position; float3 posw : position; float3 normalw: normal; }; vertexout vs(vertexin vin) { vertexout vout; vout.posw = mul(float4(vin.posl, 1.0f), gworld).xyz; vout.normalw = mul(vin.normall, (float3x3)gworldinvtranspose); vout.posh = mul(float4(vin.posl, 1.0f), gworldviewproj); homecoming vout; } float4 ps(vertexout pin) : sv_target { pin.normalw = normalize(pin.normalw); float3 toeyew = normalize(geyeposw - pin.posw); float4 ambient = float4(0.0f, 0.0f, 0.0f, 0.0f); float4 diffuse = float4(0.0f, 0.0f, 0.0f, 0.0f); float4 spec = float4(0.0f, 0.0f, 0.0f, 0.0f); float4 a, d, s; computedirectionallight(gmaterial, gdirlight, pin.normalw, toeyew, a, d, s); ambient += a; diffuse += d; spec += s; computepointlight(gmaterial, gpointlight, pin.posw, pin.normalw, toeyew, a, d, s); ambient += a; diffuse += d; spec += s; computespotlight(gmaterial, gspotlight, pin.posw, pin.normalw, toeyew, a, d, s); ambient += a; diffuse += d; spec += s; float4 litcolor = ambient + diffuse + spec; litcolor.a = gmaterial.diffuse.a; homecoming litcolor; }

code in cpp file:

struct vertex { xmfloat3 pos; xmfloat3 normal; }; struct cbuffer{ xmmatrix gworldviewproj; }; class boxapp : public framework_app { public: boxapp(hinstance hinstance); ~boxapp(); bool init(); void onresize(); void updatescene(float dt); void drawscene(); void onmousedown(wparam btnstate, int x, int y); void onmouseup(wparam btnstate, int x, int y); void onmousemove(wparam btnstate, int x, int y); private: void buildgeometrybuffers(); void buildfx(); private: id3d11buffer* mboxvb; id3d11buffer* mboxib; id3d11buffer* mboxcb = nullptr; id3d11vertexshader* pvs; id3d11pixelshader* pps; id3d11inputlayout* minputlayout; xmfloat4x4 mworld; xmfloat4x4 mview; xmfloat4x4 mproj; xmfloat3 meyeposw; xmfloat3 getnormal(float x, float z)const; //licht variablen directionallight mdirlight; pointlight mpointlight; spotlight mspotlight; material mlandmat; material mwavesmat; float mtheta; float mphi; float mradius; float getheight(float x, float z)const; point mlastmousepos; }; int winapi winmain(hinstance hinstance, hinstance previnstance, pstr cmdline, int showcmd) { boxapp theapp(hinstance); if (!theapp.init()) homecoming 0; homecoming theapp.app_run(); } boxapp::boxapp(hinstance hinstance) : framework_app(hinstance), mboxvb(0), mboxib(0), mboxcb(0) ,pvs(0), pps(0), minputlayout(0), meyeposw(0.0f, 0.0f, 0.0f), mtheta(1.5f*mathtools::halftau), mphi(0.25f*mathtools::halftau), mradius(5.0f) { mlastmousepos.x = 0; mlastmousepos.y = 0; xmmatrix = xmmatrixidentity(); xmstorefloat4x4(&mworld, i); xmstorefloat4x4(&mview, i); xmstorefloat4x4(&mproj, i); //directional lite mdirlight.ambient = xmfloat4(0.2f, 0.2f, 0.2f, 1.0f); mdirlight.diffuse = xmfloat4(0.5f, 0.5f, 0.5f, 1.0f); mdirlight.specular = xmfloat4(0.5f, 0.5f, 0.5f, 1.0f); mdirlight.direction = xmfloat3(0.57735f, -0.57735f, 0.57735f); //point lite mpointlight.ambient = xmfloat4(0.3f, 0.3, 0.3f, 1.0f); mpointlight.diffuse = xmfloat4(0.7f, 0.7f, 0.7f, 1.0f); mpointlight.specular = xmfloat4(0.7f, 0.7f, 0.7f, 1.0f); mpointlight.att = xmfloat3(0.0f, 0.1f, 0.0f); mpointlight.range = 25.0f; //spot lite mspotlight.ambient = xmfloat4(0.0f, 0.0f, 0.0f, 1.0f); mspotlight.diffuse = xmfloat4(1.0f, 1.0f, 0.0f, 1.0f); mspotlight.specular = xmfloat4(1.0f, 1.0f, 1.0f, 1.0f); mspotlight.att = xmfloat3(1.0f, 0.0f, 0.0f); mspotlight.spot = 96.0f; mspotlight.range = 1000.0f; //materials mlandmat.ambient = xmfloat4(0.48f, 0.77f, 0.46f, 1.0f); mlandmat.diffuse = xmfloat4(0.48f, 0.77f, 0.46f, 1.0f); mlandmat.specular = xmfloat4(0.2f, 0.2f, 0.2f, 16.0f); } boxapp::~boxapp() { releasecom(mboxvb); releasecom(mboxib); releasecom(mboxcb); releasecom(pvs); releasecom(pps); releasecom(minputlayout); } bool boxapp::init() { if (!framework_app::app_init()) homecoming false; buildgeometrybuffers(); buildfx(); homecoming true; } void boxapp::onresize() { framework_app::onresize(); // window resized, update aspect ratio , recompute projection matrix. xmmatrix p = xmmatrixperspectivefovlh(0.25f*mathtools::halftau, aspectratio(), 1.0f, 1000.0f); xmstorefloat4x4(&mproj, p); } void boxapp::updatescene(float dt) { // convert spherical cartesian coordinates. float x = mradius*sinf(mphi)*cosf(mtheta); float z = mradius*sinf(mphi)*sinf(mtheta); float y = mradius*cosf(mphi); meyeposw = xmfloat3(x, y ,z); // build view matrix. xmvector pos = xmvectorset(x, y, z, 1.0f); xmvector target = xmvectorzero(); xmvector = xmvectorset(0.0f, 1.0f, 0.0f, 0.0f); xmmatrix v = xmmatrixlookatlh(pos, target, up); xmstorefloat4x4(&mview, v); //update lights mpointlight.position.x = 70.0f*cosf(0.2f*mtimer.totaltime()); mpointlight.position.z = 70.0f*sinf(0.2f*mtimer.totaltime()); mpointlight.position.y = mathtools::max(getheight(mpointlight.position.x, mpointlight.position.z), -3.0f) + 10.0f; mspotlight.position = meyeposw; xmstorefloat3(&mspotlight.direction, xmvector3normalize(target - pos)); } void boxapp::drawscene() { maindevcontext->clearrendertargetview(mrendertargetview, reinterpret_cast<const float*>(&colors::lightsteelblue)); maindevcontext->cleardepthstencilview(mdepthstencilview, d3d11_clear_depth | d3d11_clear_stencil, 1.0f, 0); maindevcontext->iasetinputlayout(minputlayout); maindevcontext->iasetprimitivetopology(d3d11_primitive_topology_trianglelist); uint stride = sizeof(vertex); uint offset = 0; maindevcontext->iasetvertexbuffers(0, 1, &mboxvb, &stride, &offset); maindevcontext->iasetindexbuffer(mboxib, dxgi_format_r32_uint, 0); // set constants xmmatrix world = xmloadfloat4x4(&mworld); xmmatrix view = xmloadfloat4x4(&mview); xmmatrix proj = xmloadfloat4x4(&mproj); xmmatrix worldviewproj = world*view*proj; xmmatrix worldviewprojtrns = xmmatrixtranspose(worldviewproj); cbuffer cbuffer; cbuffer.gworldviewproj = worldviewprojtrns; maindevcontext->updatesubresource(mboxcb, 0, 0, &cbuffer, 0, 0); maindevcontext->drawindexed(36, 0, 0); mswapchain->present(0, 0); } void boxapp::onmousedown(wparam btnstate, int x, int y) { mlastmousepos.x = x; mlastmousepos.y = y; setcapture(frmewrkmainwnd); } void boxapp::onmouseup(wparam btnstate, int x, int y) { releasecapture(); } void boxapp::onmousemove(wparam btnstate, int x, int y) { if ((btnstate & mk_lbutton) != 0) { // create each pixel correspond quarter of degree. float dx = xmconverttoradians(0.25f*static_cast<float>(x - mlastmousepos.x)); float dy = xmconverttoradians(0.25f*static_cast<float>(y - mlastmousepos.y)); // update angles based on input orbit photographic camera around box. mtheta += dx; mphi += dy; // restrict angle mphi. mphi = mathtools::clamp(mphi, 0.1f, mathtools::halftau - 0.1f); } else if ((btnstate & mk_rbutton) != 0) { // create each pixel correspond 0.005 unit in scene. float dx = 0.005f*static_cast<float>(x - mlastmousepos.x); float dy = 0.005f*static_cast<float>(y - mlastmousepos.y); // update photographic camera radius based on input. mradius += dx - dy; // restrict radius. mradius = mathtools::clamp(mradius, 3.0f, 15.0f); } mlastmousepos.x = x; mlastmousepos.y = y; } void boxapp::buildgeometrybuffers() { // create vertex buffer vertex vertices[] = { { xmfloat3(-1.0f, -1.0f, -1.0f), (const float*)&colors::white }, { xmfloat3(-1.0f, +1.0f, -1.0f), (const float*)&colors::black }, { xmfloat3(+1.0f, +1.0f, -1.0f), (const float*)&colors::red }, { xmfloat3(+1.0f, -1.0f, -1.0f), (const float*)&colors::green }, { xmfloat3(-1.0f, -1.0f, +1.0f), (const float*)&colors::blue }, { xmfloat3(-1.0f, +1.0f, +1.0f), (const float*)&colors::yellow }, { xmfloat3(+1.0f, +1.0f, +1.0f), (const float*)&colors::cyan }, { xmfloat3(+1.0f, -1.0f, +1.0f), (const float*)&colors::magneta } }; d3d11_buffer_desc vbd; vbd.usage = d3d11_usage_immutable; vbd.bytewidth = sizeof(vertex)* 8; vbd.bindflags = d3d11_bind_vertex_buffer; vbd.cpuaccessflags = 0; vbd.miscflags = 0; vbd.structurebytestride = 0; d3d11_subresource_data vinitdata; vinitdata.psysmem = vertices; maind3ddevice->createbuffer(&vbd, &vinitdata, &mboxvb); // create index buffer uint indices[] = { // front end face 0, 1, 2, 0, 2, 3, // face 4, 6, 5, 4, 7, 6, // left face 4, 5, 1, 4, 1, 0, // right face 3, 2, 6, 3, 6, 7, // top face 1, 5, 6, 1, 6, 2, // bottom face 4, 0, 3, 4, 3, 7 }; d3d11_buffer_desc ibd; ibd.usage = d3d11_usage_immutable; ibd.bytewidth = sizeof(uint)* 36; ibd.bindflags = d3d11_bind_index_buffer; ibd.cpuaccessflags = 0; ibd.miscflags = 0; ibd.structurebytestride = 0; d3d11_subresource_data iinitdata; iinitdata.psysmem = indices; maind3ddevice->createbuffer(&ibd, &iinitdata, &mboxib); } float boxapp::getheight(float x, float z)const { homecoming 0.3f*(z*sinf(0.1f*x) + x*cosf(0.1*z)); } xmfloat3 boxapp::getnormal(float x, float z)const { xmfloat3 n(-0.03f*z*cosf(0.1f*x) - 0.3f*cosf(0.1f*z), 1.0f, -0.3f*sinf(0.1f*x) - 0.03f*x*sinf(0.1f*z)); xmvector unitnormal = xmvector3normalize(xmloadfloat3(&n)); xmstorefloat3(&n, unitnormal); homecoming n; } void boxapp::buildfx() { id3d10blob* vs; id3d10blob* ps; d3dx11compilefromfile((lpstr)"mcolor.shader",0,0, "vs", "vs_4_0", 0, 0, 0, &vs, 0, 0); d3dx11compilefromfile((lpstr)"mcolor.shader", 0, 0, "ps", "ps_4_0", 0, 0, 0, &ps, 0, 0); maind3ddevice->createvertexshader(vs->getbufferpointer(), vs->getbuffersize(), null, &pvs); maind3ddevice->createpixelshader(ps->getbufferpointer(), ps->getbuffersize(), null, &pps); maindevcontext->vssetshader(pvs, 0, 0); maindevcontext->pssetshader(pps, 0, 0); d3d11_input_element_desc vertexdesc[] = { { "position", 0, dxgi_format_r32g32b32_float, 0, 0, d3d11_input_per_vertex_data, 0 }, { "normal", 0, dxgi_format_r32g32b32a32_float, 0, 12, d3d11_input_per_vertex_data, 0 } }; maind3ddevice->createinputlayout(vertexdesc, 2, vs->getbufferpointer(), vs->getbuffersize() ,&minputlayout); maindevcontext->iasetinputlayout(minputlayout); d3d11_buffer_desc bd; zeromemory(&bd, sizeof(bd)); bd.usage = d3d11_usage_default; bd.bytewidth = sizeof(cbuffer); bd.bindflags = d3d11_bind_constant_buffer; maind3ddevice->createbuffer(&bd, nullptr, &mboxcb); maindevcontext->vssetconstantbuffers(0, 1, &mboxcb); }

i think have alot of errors in code.

folow these tutorials aswell, might easier whole view then: http://web.archive.org/web/20140213145557/http://rastertek.com/tutdx11.html

one example, run buildfx() @ start, pretty sure should set these 2 functions every frame

maindevcontext->vssetshader(pvs, 0, 0); maindevcontext->pssetshader(pps, 0, 0);

have succeded rendered cube pure colors yet? if should build there. there alot of code mention lastly 5 functions run every frame those:

devicecontext->iasetinputlayout(m_layout); devicecontext->vssetshader(m_vertexshader, null, 0); devicecontext->pssetshader(m_pixelshader, null, 0); devicecontext->pssetsamplers(0, 1, &m_samplestate); devicecontext->drawindexed(indexcount, 0, 0);

read link , cut down downwards errors. havent read of code.

c++ visual-c++ directx directx-11

No comments:

Post a Comment