fixed lighting. The ball is be-au-ti-ful

master
Martin Felis 2016-11-30 22:11:08 +01:00
parent 507b475539
commit 273a8e010f
7 changed files with 51 additions and 18 deletions

View File

@ -101,13 +101,15 @@ void main()
vec2 texelSize = vec2_splat(1.0/u_shadowMapSize); vec2 texelSize = vec2_splat(1.0/u_shadowMapSize);
float visibility = PCF(u_shadowMap, v_shadowcoord, u_shadowMapBias, texelSize); float visibility = PCF(u_shadowMap, v_shadowcoord, u_shadowMapBias, texelSize);
vec3 ambient = 0.1 * color; vec3 ambient = 0.05 * color;
vec3 brdf = color * visibility; vec3 brdf = (lc.x + lc.y) * color * visibility;
#if TEXTURED #if TEXTURED
vec4 texcolor = toLinear (texture2D(sceneDefaultTexture, v_texcoord0) ); vec4 texcolor = toLinear (texture2D(sceneDefaultTexture, v_texcoord0) );
brdf = brdf * texcolor.xyz; brdf = brdf * texcolor.xyz;
ambient = ambient * texcolor.xyz;
#endif #endif
vec3 final = toGamma(abs(ambient + brdf) ); vec3 final = toGamma(abs(ambient + brdf) );
gl_FragColor = vec4(final, 1.0); gl_FragColor = vec4(final, 1.0);
} }

View File

@ -1,8 +1,8 @@
vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0); vec3 v_view : TEXCOORD0 = vec3(0.0, 0.0, 0.0);
vec3 v_dir : TEXCOORD1 = vec3(0.0, 0.0, 0.0); vec4 v_shadowcoord : TEXCOORD1 = vec4(0.0, 0.0, 0.0, 0.0);
vec3 v_view : TEXCOORD1 = vec3(0.0, 0.0, 0.0); vec4 v_position : TEXCOORD2 = vec4(0.0, 0.0, 0.0, 0.0);
vec4 v_shadowcoord : TEXCOORD2 = vec4(0.0, 0.0, 0.0, 0.0); vec3 v_dir : TEXCOORD3 = vec3(0.0, 0.0, 0.0);
vec4 v_position : TEXCOORD3 = vec4(0.0, 0.0, 0.0, 0.0); vec2 v_texcoord0 : TEXCOORD4 = vec2(0.0, 0.0);
vec3 v_normal : NORMAL = vec3(0.0, 0.0, 1.0); vec3 v_normal : NORMAL = vec3(0.0, 0.0, 1.0);
vec4 v_color0 : COLOR0 = vec4(1.0, 0.0, 0.0, 1.0); vec4 v_color0 : COLOR0 = vec4(1.0, 0.0, 0.0, 1.0);

View File

@ -15,7 +15,7 @@ void main()
gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0) ); gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0) );
vec4 normal = a_normal * 2.0 - 1.0; vec4 normal = a_normal * 2.0 - 1.0;
v_normal = normalize(mul(u_modelView, vec4(normal.xyz, 0.0) ).xyz); v_normal = normalize(normal.xyz);
v_view = mul(u_modelView, vec4(a_position, 1.0)).xyz; v_view = mul(u_modelView, vec4(a_position, 1.0)).xyz;
const float shadowMapOffset = 0.001; const float shadowMapOffset = 0.001;

View File

@ -9,13 +9,14 @@ $output v_view, v_normal, v_shadowcoord, v_texcoord0
#include "../common/common.sh" #include "../common/common.sh"
uniform mat4 u_lightMtx; uniform mat4 u_lightMtx;
uniform vec4 u_lightPos;
void main() void main()
{ {
gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0) ); gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0) );
vec4 normal = a_normal * 2.0 - 1.0; vec4 normal = a_normal * 2.0 - 1.0;
v_normal = normalize(mul(u_modelView, vec4(normal.xyz, 0.0) ).xyz); v_normal = normalize(normal.xyz);
v_view = mul(u_modelView, vec4(a_position, 1.0)).xyz; v_view = mul(u_modelView, vec4(a_position, 1.0)).xyz;
const float shadowMapOffset = 0.001; const float shadowMapOffset = 0.001;

View File

@ -466,10 +466,10 @@ bgfx::VertexDecl PosColorTexCoord0Vertex::ms_decl;
// Plane // Plane
PosNormalColorTexcoordVertex s_hplaneVertices[] = PosNormalColorTexcoordVertex s_hplaneVertices[] =
{ {
{ -1.0f, 0.0f, 1.0f, packF4u(0.0f, 1.0f, 0.0f), packF4u(1.0f, 1.0f, 1.0f), 0.f, 0.f }, { -1.0f, 0.0f, 1.0f, packF4u(0.0f, -1.0f, 0.0f), packF4u(1.0f, 1.0f, 1.0f), 0.f, 0.f },
{ 1.0f, 0.0f, 1.0f, packF4u(0.0f, 1.0f, 0.0f), packF4u(1.0f, 1.0f, 1.0f), 10.f, 0.f }, { 1.0f, 0.0f, 1.0f, packF4u(0.0f, -1.0f, 0.0f), packF4u(1.0f, 1.0f, 1.0f), 10.f, 0.f },
{ -1.0f, 0.0f, -1.0f, packF4u(0.0f, 1.0f, 0.0f), packF4u(1.0f, 1.0f, 1.0f), 0.f, 10.f}, { -1.0f, 0.0f, -1.0f, packF4u(0.0f, -1.0f, 0.0f), packF4u(1.0f, 1.0f, 1.0f), 0.f, 10.f},
{ 1.0f, 0.0f, -1.0f, packF4u(0.0f, 1.0f, 0.0f), packF4u(1.0f, 1.0f, 1.0f), 10.f, 10.f }, { 1.0f, 0.0f, -1.0f, packF4u(0.0f, -1.0f, 0.0f), packF4u(1.0f, 1.0f, 1.0f), 10.f, 10.f },
}; };
const uint16_t s_planeIndices[] = const uint16_t s_planeIndices[] =
@ -755,7 +755,7 @@ void Renderer::setupShaders() {
std::cout << "Creating light uniforms..." << std::endl; std::cout << "Creating light uniforms..." << std::endl;
lights[0].u_shadowMap = bgfx::createUniform("u_shadowMap", bgfx::UniformType::Int1); lights[0].u_shadowMap = bgfx::createUniform("u_shadowMap", bgfx::UniformType::Int1);
lights[0].u_shadowMapParams = bgfx::createUniform("u_shadowMapParams", bgfx::UniformType::Vec4); lights[0].u_shadowMapParams = bgfx::createUniform("u_shadowMapParams", bgfx::UniformType::Vec4);
lights[0].u_lightPos = bgfx::createUniform("u_lightPos", bgfx::UniformType::Int1); lights[0].u_lightPos = bgfx::createUniform("u_lightPos", bgfx::UniformType::Vec4);
lights[0].u_lightMtx = bgfx::createUniform("u_lightMtx", bgfx::UniformType::Int1); lights[0].u_lightMtx = bgfx::createUniform("u_lightMtx", bgfx::UniformType::Int1);
// Setup the light probe pass // Setup the light probe pass
@ -1103,7 +1103,7 @@ void Renderer::paintGL() {
float eye[3]; float eye[3];
eye[0] = lights[i].pos[0]; eye[0] = lights[i].pos[0];
eye[1] = lights[i].pos[1]; eye[1] = lights[i].pos[1];
eye[2] = lights[0].pos[2]; eye[2] = lights[i].pos[2];
float at[3]; float at[3];
at[0] = - lights[i].pos[0] + lights[i].dir[0]; at[0] = - lights[i].pos[0] + lights[i].dir[0];
@ -1168,6 +1168,7 @@ void Renderer::paintGL() {
// Floor. // Floor.
bx::mtxMul(lightMtx, mtxFloor, lights[0].mtxShadow); bx::mtxMul(lightMtx, mtxFloor, lights[0].mtxShadow);
bgfx::setUniform(lights[0].u_lightMtx, lightMtx); bgfx::setUniform(lights[0].u_lightMtx, lightMtx);
bgfx::setUniform(lights[0].u_lightPos, lights[0].pos);
// Clear backbuffer and shadowmap framebuffer at beginning. // Clear backbuffer and shadowmap framebuffer at beginning.
bgfx::setViewClear(RenderState::Skybox bgfx::setViewClear(RenderState::Skybox
@ -1237,6 +1238,7 @@ void Renderer::paintGL() {
} }
bgfx::setUniform(lights[0].u_lightMtx, lightMtx); bgfx::setUniform(lights[0].u_lightMtx, lightMtx);
bgfx::setUniform(lights[0].u_lightPos, lights[0].pos);
bgfx::setUniform(u_color, Vector4f(1.f, 1.f, 1.f, 1.f).data(), 4); bgfx::setUniform(u_color, Vector4f(1.f, 1.f, 1.f, 1.f).data(), 4);
bgfx::setIndexBuffer(plane_ibh); bgfx::setIndexBuffer(plane_ibh);
bgfx::setVertexBuffer(plane_vbh); bgfx::setVertexBuffer(plane_vbh);
@ -1246,15 +1248,19 @@ void Renderer::paintGL() {
// render entities // render entities
for (size_t i = 0; i < entities.size(); i++) { for (size_t i = 0; i < entities.size(); i++) {
float mtxLightViewProjInv[16];
float light_pos_world[3];
// shadow map pass // shadow map pass
bx::mtxMul(lightMtx, entities[i]->transform, lights[0].mtxShadow); bx::mtxMul(lightMtx, entities[i]->transform, lights[0].mtxShadow);
bgfx::setUniform(lights[0].u_lightMtx, lightMtx); bgfx::setUniform(lights[0].u_lightMtx, lightMtx);
bgfx::setUniform(lights[0].u_lightPos, lights[0].pos);
bgfx::setUniform(u_color, entities[i]->color, 4); bgfx::setUniform(u_color, entities[i]->color, 4);
meshSubmit(entities[i]->mesh, &s_renderStates[RenderState::ShadowMap], 1, entities[i]->transform); meshSubmit(entities[i]->mesh, &s_renderStates[RenderState::ShadowMap], 1, entities[i]->transform);
// scene pass // scene pass
bx::mtxMul(lightMtx, entities[i]->transform, lights[0].mtxShadow); bx::mtxMul(lightMtx, entities[i]->transform, lights[0].mtxShadow);
bgfx::setUniform(lights[0].u_lightMtx, lightMtx); bgfx::setUniform(lights[0].u_lightMtx, lightMtx);
bgfx::setUniform(lights[0].u_lightPos, lights[0].pos);
bgfx::setUniform(u_color, entities[i]->color, 4); bgfx::setUniform(u_color, entities[i]->color, 4);
meshSubmit(entities[i]->mesh, &s_renderStates[RenderState::Scene], 1, entities[i]->transform); meshSubmit(entities[i]->mesh, &s_renderStates[RenderState::Scene], 1, entities[i]->transform);
} }
@ -1385,6 +1391,25 @@ void Renderer::paintGL() {
ImGui::Begin("Render Settings"); ImGui::Begin("Render Settings");
if(ImGui::DragFloat3 ("Light0 Pos", lights[0].pos, 1.0f, -10.0f, 10.0f)) {
}
if(ImGui::DragFloat3 ("Light0 Dir", lights[0].dir, 1.0f, -10.0f, 10.0f)) {
}
float light_at[3];
light_at[0] = lights[0].dir[0] - lights[0].pos[0];
light_at[1] = lights[0].dir[1] - lights[0].pos[1];
light_at[2] = lights[0].dir[2] - lights[0].pos[2];
if(ImGui::DragFloat3 ("Light0 At", light_at, 1.0f, -10.0f, 10.0f)) {
lights[0].dir[0] = lights[0].pos[0]- light_at[0];
lights[0].dir[1] = lights[0].pos[1]- light_at[1];
lights[0].dir[2] = lights[0].pos[2]- light_at[2];
}
assert (lights.size() == 1);
ImGui::Checkbox("Draw Debug", &drawDebug); ImGui::Checkbox("Draw Debug", &drawDebug);
for (int i = 0; i < lights.size(); i++) { for (int i = 0; i < lights.size(); i++) {

View File

@ -53,7 +53,7 @@ struct Camera {
up {0.f, 1.f, 0.f}, up {0.f, 1.f, 0.f},
near (0.1f), near (0.1f),
far (150.f), far (150.f),
fov (70.f), fov (60.f),
orthographic (false), orthographic (false),
width (-1.f), width (-1.f),
height (-1.f), height (-1.f),
@ -86,7 +86,7 @@ struct Light {
bgfx::TextureHandle shadowMapTexture; bgfx::TextureHandle shadowMapTexture;
bgfx::FrameBufferHandle shadowMapFB; bgfx::FrameBufferHandle shadowMapFB;
float pos[3]; float pos[4];
float dir[3]; float dir[3];
float mtxView[16]; float mtxView[16];
@ -108,7 +108,7 @@ struct Light {
u_lightMtx (BGFX_INVALID_HANDLE), u_lightMtx (BGFX_INVALID_HANDLE),
shadowMapTexture (BGFX_INVALID_HANDLE), shadowMapTexture (BGFX_INVALID_HANDLE),
shadowMapFB (BGFX_INVALID_HANDLE), shadowMapFB (BGFX_INVALID_HANDLE),
pos {10.f, 10.f, 10.f}, pos {0.f, 10.f, 10.f, 1.0f},
dir {-1.f, -1.f, -1.f}, dir {-1.f, -1.f, -1.f},
mtxView { mtxView {
1.f, 0.f, 0.f, 0.f, 1.f, 0.f, 0.f, 0.f,

View File

@ -275,11 +275,14 @@ static void module_reload(struct module_state *state) {
cout << "Creating render entity mesh ..." << endl; cout << "Creating render entity mesh ..." << endl;
state->character->entity->mesh = bgfxutils::createUVSphere (45, 45); state->character->entity->mesh = bgfxutils::createUVSphere (45, 45);
// state->character->entity->mesh = bgfxutils::createCuboid (1.f, 1.f, 1.f);
// state->character->entity->mesh = bgfxutils::createCylinder (20); // state->character->entity->mesh = bgfxutils::createCylinder (20);
cout << "Creating render entity mesh ... success!" << endl; cout << "Creating render entity mesh ... success!" << endl;
// load the state of the entity // load the state of the entity
SerializeVec3(*gReadSerializer, "protot.TestModule.entity.position", state->character->position); SerializeVec3(*gReadSerializer, "protot.TestModule.entity.position", state->character->position);
SerializeBool(*gReadSerializer, "protot.TestModule.modules_window.visible", state->modules_window_visible);
SerializeInt(*gReadSerializer, "protot.TestModule.modules_window.selection_index", state->modules_window_selected_index);
glfwSetScrollCallback (gWindow, mouse_scroll_callback); glfwSetScrollCallback (gWindow, mouse_scroll_callback);
} }
@ -289,6 +292,8 @@ static void module_unload(struct module_state *state) {
// serialize the state of the entity // serialize the state of the entity
SerializeVec3(*gWriteSerializer, "protot.TestModule.entity.position", state->character->position); SerializeVec3(*gWriteSerializer, "protot.TestModule.entity.position", state->character->position);
SerializeBool(*gWriteSerializer, "protot.TestModule.modules_window.visible", state->modules_window_visible);
SerializeInt(*gWriteSerializer, "protot.TestModule.modules_window.selection_index", state->modules_window_selected_index);
// clean up // clean up
cout << "destroying render entity " << state->character->entity << endl; cout << "destroying render entity " << state->character->entity << endl;