Working directional light

This commit is contained in:
Tleety
2016-01-19 12:32:34 +01:00
parent 4b29d48f27
commit 46e17b7e0d
6 changed files with 27 additions and 29 deletions
@@ -15,7 +15,9 @@ struct DirectionalLightJob : RenderJob
DirectionalLightJob(ComponentWrapper transformComponent, ComponentWrapper directionalLightComponent, World* m_World)
: RenderJob()
{
Direction = glm::vec4((glm::vec3)directionalLightComponent["Direction"], 0.f);
Direction = glm::vec4(0,0,-1,0) * Transform::AbsoluteOrientation(m_World, transformComponent.EntityID);
//Direction = glm::vec4((glm::vec3)directionalLightComponent["Direction"], 0.f);
Color = (glm::vec4)directionalLightComponent["Color"];
Intensity = (double)directionalLightComponent["Intensity"];
};
+5 -5
View File
@@ -46,8 +46,8 @@ private:
int m_NumberOfTiles = 0;
struct Plane {
glm::vec3 Normal;
float d;
glm::vec3 Normal = glm::vec3(0.f);
float d = 0;
};
struct Frustum {
@@ -68,9 +68,9 @@ private:
std::vector<LightSource> m_LightSources;
struct LightGrid {
float Start;
float Amount;
glm::vec2 Padding;
float Start = 0;
float Amount = 0;
glm::vec2 Padding = glm::vec2(1.f, 2.f);
};
LightGrid* m_LightGrid;
@@ -1,5 +1,4 @@
<c:DirectionalLight>
<Direction X="5" Y="5" Z="5"/>
<Color R="1" G="1" B="1" A="1"/>
<Intensity>0.8</Intensity>
<Visible>true</Visible>
@@ -9,7 +9,6 @@
</xs:annotation>
<xs:complexType>
<xs:all>
<xs:element name="Direction" type="t:Vector" minOccurs="0"/>
<xs:element name="Color" type="t:Color" minOccurs="0"/>
<xs:element name="Intensity" type="t:double" minOccurs="0"/>
<xs:element name="Visible" type="t:bool" minOccurs="0"/>
+11 -9
View File
@@ -86,9 +86,9 @@ LightResult CalcPointLightSource(vec4 lightPos, float lightRadius, vec4 lightCol
return result;
}
LightResult CalcDirectionalLightSource(vec4 direction, vec4 color, float intensity, vec4 viewVec, vec4 vertPosition, vec4 vertNormal)
LightResult CalcDirectionalLightSource(vec4 direction, vec4 color, float intensity, vec4 viewVec, vec4 vertNormal)
{
vec4 L = normalize( vec4(direction.xyz, 1) );
vec4 L = normalize( -direction );
LightResult result;
result.Diffuse = CalcDiffuse(color, L, vertNormal) * intensity;
@@ -105,8 +105,8 @@ void main()
vec4 viewVec = normalize(-position);
vec2 tilePos;
tilePos.x = int(gl_FragCoord.x/16);
tilePos.y = int(gl_FragCoord.y/16);
tilePos.x = int(gl_FragCoord.x/TILE_SIZE);
tilePos.y = int(gl_FragCoord.y/TILE_SIZE);
LightResult totalLighting;
totalLighting.Diffuse = scene_ambient;
@@ -117,12 +117,13 @@ void main()
//for(int i = 0; i < 3; i++)
for(int i = start; i < start + amount; i++) {
int l = int(LightIndex[i]);
LightSource light = LightSources.List[l];
LightResult result;
if(LightSources.List[i].Type == 1) { // point
result = CalcPointLightSource(V * LightSources.List[l].Position, LightSources.List[l].Radius, LightSources.List[l].Color, LightSources.List[l].Intensity, viewVec, position, normal, LightSources.List[i].Falloff);
} else if (LightSources.List[i].Type == 2) { //Directional
result = CalcDirectionalLightSource(V * LightSources.List[l].Direction, LightSources.List[i].Color, LightSources.List[i].Intensity, viewVec, position, normal);
if(light.Type == 1) { // point
result = CalcPointLightSource(V * light.Position, light.Radius, light.Color, light.Intensity, viewVec, position, normal, light.Falloff);
} else if (light.Type == 2) { //Directional
result = CalcDirectionalLightSource(V * light.Direction, light.Color, light.Intensity, viewVec, normal);
}
totalLighting.Diffuse += result.Diffuse;
totalLighting.Specular += result.Specular;
@@ -132,8 +133,9 @@ void main()
//fragmentColor += Input.DiffuseColor;
fragmentColor += Input.DiffuseColor * (totalLighting.Diffuse + totalLighting.Specular) * texel * Color;
//fragmentColor += Input.DiffuseColor * (totalLighting.Diffuse) * texel * Color;
//fragmentColor += Input.DiffuseColor + vec4(0.0, LightGrids.Data[currentTile].Start/.0, 0, 1);
//fragmentColor += Input.DiffuseColor + vec4(0.0, LightGrids.Data[currentTile].Amount/3, 0, 1);
//fragmentColor = texel * Input.DiffuseColor * Color;
//fragmentColor += vec4(currentTile/3600.f, 0, 0, 1);
if(int(gl_FragCoord.x)%16 == 0 || int(gl_FragCoord.y)%16 == 0 ) {
//fragmentColor += vec4(0.5, 0, 0, 0);
} else {
+8 -12
View File
@@ -40,15 +40,14 @@ void LightCullingPass::OnResolutionChange()
void LightCullingPass::SetSSBOSizes()
{
m_NumberOfTiles = (int)(m_Renderer->Resolution().Width*m_Renderer->Resolution().Height)/TILE_SIZE;
//m_Frustums = new Frustum[s];
//m_LightGrid = new LightGrid[s];
//m_LightIndex = new float[s*200];
m_NumberOfTiles = (int)(m_Renderer->Resolution().Width/TILE_SIZE) * (int)(m_Renderer->Resolution().Height/TILE_SIZE);
m_Frustums = new Frustum[m_NumberOfTiles];
m_LightGrid = new LightGrid[m_NumberOfTiles];
m_LightIndex = new float[m_NumberOfTiles*MAX_LIGHTS_PER_TILE];
for (int i = 0; i < m_NumberOfTiles*MAX_LIGHTS_PER_TILE; i++) {
m_LightIndex[i] = -1;
}
}
void LightCullingPass::CullLights(RenderScene& scene)
@@ -75,7 +74,7 @@ void LightCullingPass::CullLights(RenderScene& scene)
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, m_LightGridSSBO);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, m_LightOffsetSSBO);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, m_LightIndexSSBO);
glDispatchCompute(m_Renderer->Resolution().Width / TILE_SIZE, m_Renderer->Resolution().Height / TILE_SIZE, 1);
glDispatchCompute(glm::ceil(m_Renderer->Resolution().Width / TILE_SIZE), glm::ceil(m_Renderer->Resolution().Height / TILE_SIZE), 1);
GLERROR("CullLights Error: End");
}
@@ -117,25 +116,22 @@ void LightCullingPass::InitializeSSBOs()
{
glGenBuffers(1, &m_FrustumSSBO);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_FrustumSSBO);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(Frustum)*m_NumberOfTiles, m_Frustums, GL_DYNAMIC_COPY);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(Frustum)*m_NumberOfTiles, nullptr, GL_DYNAMIC_COPY);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
GLERROR("m_FrustumSSBO");
glGenBuffers(1, &m_LightSSBO);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_LightSSBO);
if(m_LightSources.size() > 0) {
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(LightSource) * m_LightSources.size(), &(m_LightSources[0]), GL_DYNAMIC_COPY);
}
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(LightSource) * 200, nullptr, GL_DYNAMIC_COPY);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
GLERROR("m_LightSSBO");
glGenBuffers(1, &m_LightGridSSBO);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_LightGridSSBO);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(LightGrid)*m_NumberOfTiles, m_LightGrid, GL_DYNAMIC_COPY);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(LightGrid)*m_NumberOfTiles, nullptr, GL_DYNAMIC_COPY);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
GLERROR("m_LightGridSSBO");
glGenBuffers(1, &m_LightOffsetSSBO);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_LightOffsetSSBO);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(m_LightOffset), &m_LightOffset, GL_DYNAMIC_COPY);